{"id":158,"date":"2024-09-18T20:43:45","date_gmt":"2024-09-18T12:43:45","guid":{"rendered":"https:\/\/newstrong.xyz\/?p=158"},"modified":"2024-09-18T20:43:45","modified_gmt":"2024-09-18T12:43:45","slug":"%e4%bf%a1%e5%8f%b7%e6%9c%ba%e5%88%b6%e5%92%8candroid-natvie-crash%e6%8d%95%e6%8d%89","status":"publish","type":"post","link":"https:\/\/newstrong.top\/index.php\/2024\/09\/18\/%e4%bf%a1%e5%8f%b7%e6%9c%ba%e5%88%b6%e5%92%8candroid-natvie-crash%e6%8d%95%e6%8d%89\/","title":{"rendered":"\u4fe1\u53f7\u673a\u5236\u548cAndroid natvie crash\u6355\u6349"},"content":{"rendered":"<h2>\u4e00\u3001\u4fe1\u53f7\u673a\u5236<\/h2>\n<div class=\"image-package\">\n<div class=\"image-container\" style=\"max-width: 700px; max-height: 261px;\">\n<div class=\"image-view\" data-width=\"1916\" data-height=\"714\"><img decoding=\"async\" src=\"https:\/\/newstrong.xyz\/wp-content\/uploads\/2024\/09\/frc-289e11544cea163becd5b7b33647958c.png\"><\/div>\n<\/div>\n<div class=\"image-caption\">image.png<\/div>\n<\/div>\n<p>\u51fd\u6570\u8fd0\u884c\u5728\u7528\u6237\u6001,\u5f53\u9047\u5230\u7cfb\u7edf\u8c03\u7528\u3001\u4e2d\u65ad\u6216\u662f\u5f02\u5e38\u7684\u60c5\u51b5\u65f6,\u7a0b\u5e8f\u4f1a\u8fdb\u5165\u5185\u6838\u6001\u3002\u4fe1\u53f7\u6d89\u53ca\u5230\u4e86\u8fd9\u4e24\u79cd\u72b6\u6001\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<h3>1\u3001\u4fe1\u53f7\u7684\u63a5\u6536<\/h3>\n<p>\u63a5\u6536\u4fe1\u53f7\u7684\u4efb\u52a1\u662f\u7531\u5185\u6838\u4ee3\u7406\u7684\uff0c\u5f53\u5185\u6838\u63a5\u6536\u5230\u4fe1\u53f7\u540e\uff0c\u4f1a\u5c06\u5176\u653e\u5230\u5bf9\u5e94\u8fdb\u7a0b\u7684\u4fe1\u53f7\u961f\u5217\u4e2d\uff0c\u540c\u65f6\u5411\u8fdb\u7a0b\u53d1\u9001\u4e00\u4e2a\u4e2d\u65ad\uff0c\u4f7f\u5176\u9677\u5165\u5185\u6838\u6001\u3002<\/p>\n<p>\u6b64\u65f6\u4fe1\u53f7\u8fd8\u53ea\u662f\u5728\u961f\u5217\u4e2d\uff0c\u5bf9\u8fdb\u7a0b\u6765\u8bf4\u6682\u65f6\u662f\u4e0d\u77e5\u9053\u6709\u4fe1\u53f7\u5230\u6765\u7684\u3002<\/p>\n<h3>2\u3001\u4fe1\u53f7\u7684\u68c0\u6d4b<\/h3>\n<p>\u8fdb\u7a0b\u9677\u5165\u5185\u6838\u6001\u540e\uff0c\u6709\u4e24\u79cd\u573a\u666f\u4f1a\u5bf9\u4fe1\u53f7\u8fdb\u884c\u68c0\u6d4b\uff1a<\/p>\n<ul>\n<li>\u8fdb\u7a0b\u4ece\u5185\u6838\u6001\u8fd4\u56de\u5230\u7528\u6237\u6001\u524d\u8fdb\u884c\u4fe1\u53f7\u68c0\u6d4b<\/li>\n<li>\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u4e2d\uff0c\u4ece\u7761\u7720\u72b6\u6001\u88ab\u5524\u9192\u7684\u65f6\u5019\u8fdb\u884c\u4fe1\u53f7\u68c0\u6d4b<br \/>\n\u5f53\u53d1\u73b0\u6709\u65b0\u4fe1\u53f7\u65f6\uff0c\u4fbf\u4f1a\u8fdb\u5165\u4e0b\u4e00\u6b65\uff0c\u4fe1\u53f7\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n<h3>3\u3001\u4fe1\u53f7\u7684\u5904\u7406<\/h3>\n<p>\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u662f\u8fd0\u884c\u5728\u7528\u6237\u6001\u7684\uff0c\u8c03\u7528\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u524d\uff0c\u5185\u6838\u4f1a\u5c06\u5f53\u524d\u7684\u5185\u6838\u6808\u7684\u5185\u5bb9\u5907\u4efd\u62f7\u8d1d\u5230\u7528\u6237\u6808\uff0c\u7136\u540e\u4fee\u6539\u547d\u4ee4\u5bc4\u5b58\u5668(eip)\u6307\u5411\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<br \/>\n\u63a5\u4e0b\u6765\u8fdb\u7a0b\u8fd4\u56de\u5230\u7528\u6237\u6001\u4e2d\uff0c\u6267\u884c\u76f8\u5e94\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<br \/>\n\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\uff0c\u8fd8\u9700\u8981\u8fd4\u56de\u5185\u6838\u6001\uff0c\u68c0\u67e5\u662f\u5426\u8fd8\u6709\u5176\u5b83\u4fe1\u53f7\u672a\u5904\u7406\u3002<br \/>\n\u5982\u679c\u6240\u6709\u4fe1\u53f7\u90fd\u5904\u7406\u5b8c\u6210\uff0c\u5c31\u4f1a\u5c06\u5185\u6838\u6808\u6062\u590d\uff08\u4ece\u7528\u6237\u6808\u7684\u5907\u4efd\u62f7\u8d1d\u56de\u6765\uff09\uff0c\u540c\u65f6\u6062\u590d\u6307\u4ee4\u5bc4\u5b58\u5668\uff08eip\uff09\u5c06\u5176\u6307\u5411\u4e2d\u65ad\u524d\u7684\u8fd0\u884c\u4f4d\u7f6e\uff0c\u6700\u540e\u56de\u5230\u7528\u6237\u6001\u7ee7\u7eed\u6267\u884c\u8fdb\u7a0b\u3002<\/p>\n<p>\u4e00\u4e2a\u5b8c\u6574\u7684\u4fe1\u53f7\u5904\u7406\u6d41\u7a0b\u4fbf\u7ed3\u675f\u4e86\uff0c\u5982\u679c\u540c\u65f6\u6709\u591a\u4e2a\u4fe1\u53f7\u5230\u8fbe\uff0c\u4e0a\u9762\u7684\u5904\u7406\u6d41\u7a0b\u4f1a\u5728\u7b2c2\u6b65\u548c\u7b2c3\u6b65\u9aa4\u95f4\u91cd\u590d\u8fdb\u884c\u3002<\/p>\n<h2>\u4e8c\u3001\u4fe1\u53f7\u5b9a\u4e49\u548c\u884c\u4e3a<\/h2>\n<h3>1\u3001\u4fe1\u53f7\u7684\u5b9a\u4e49<\/h3>\n<p>\u6240\u6709\u7684\u7b26\u5408Unix\u89c4\u8303\uff08\u5982POSIX\uff09\u7684\u7cfb\u7edf\u90fd\u7edf\u4e00\u5b9a\u4e49\u4e86SIGNAL\u7684\u6570\u91cf\u3001\u542b\u4e49\u548c\u884c\u4e3a\u3002<br \/>\nAndroid\u4ee3\u7801\u4e2d\uff0csignal\u7684\u5b9a\u4e49\u4e00\u822c\u5728 signum.h\uff08Android\u4ee3\u7801\u4e2d\uff0csignal\u7684\u5b9a\u4e49\u4e00\u822c\u5728 signum.h\uff09\u4e2d\u3002<\/p>\n<p>\u4e00\u5171\u670931\u4e2a\u4fe1\u53f7\uff0c\u5176\u4e2d1~15\u53f7\u4fe1\u53f7\u4e3a\u5e38\u7528\u4fe1\u53f7<\/p>\n<pre><code>\/* Signals.  *\/  \n#define SIGHUP      1   \/* Hangup (POSIX).  *\/   \n#define SIGINT      2   \/* Interrupt (ANSI).  *\/   \n#define SIGQUIT     3   \/* Quit (POSIX).  *\/   \n#define SIGILL      4   \/* Illegal instruction (ANSI).  *\/   \n#define SIGTRAP     5   \/* Trace trap (POSIX).  *\/   \n#define SIGABRT     6   \/* Abort (ANSI).  *\/   \n#define SIGIOT      6   \/* IOT trap (4.2 BSD).  *\/   \n#define SIGBUS      7   \/* BUS error (4.2 BSD).  *\/   \n#define SIGFPE      8   \/* Floating-point exception (ANSI).  *\/   \n#define SIGKILL     9   \/* Kill, unblockable (POSIX).  *\/   \n#define SIGUSR1     10  \/* User-defined signal 1 (POSIX).  *\/   \n#define SIGSEGV     11  \/* Segmentation violation (ANSI).  *\/   \n#define SIGUSR2     12  \/* User-defined signal 2 (POSIX).  *\/   \n#define SIGPIPE     13  \/* Broken pipe (POSIX).  *\/   \n#define SIGALRM     14  \/* Alarm clock (POSIX).  *\/   \n#define SIGTERM     15  \/* Termination (ANSI).  *\/   \n#define SIGSTKFLT   16  \/* Stack fault.  *\/   \n#define SIGCLD      SIGCHLD \/* Same as SIGCHLD (System V).  *\/   \n#define SIGCHLD     17  \/* Child status has changed (POSIX).  *\/   \n#define SIGCONT     18  \/* Continue (POSIX).  *\/   \n#define SIGSTOP     19  \/* Stop, unblockable (POSIX).  *\/   \n#define SIGTSTP     20  \/* Keyboard stop (POSIX).  *\/   \n#define SIGTTIN     21  \/* Background read from tty (POSIX).  *\/   \n#define SIGTTOU     22  \/* Background write to tty (POSIX).  *\/   \n#define SIGURG      23  \/* Urgent condition on socket (4.2 BSD).  *\/   \n#define SIGXCPU     24  \/* CPU limit exceeded (4.2 BSD).  *\/   \n#define SIGXFSZ     25  \/* File size limit exceeded (4.2 BSD).  *\/   \n#define SIGVTALRM   26  \/* Virtual alarm clock (4.2 BSD).  *\/   \n#define SIGPROF     27  \/* Profiling alarm clock (4.2 BSD).  *\/   \n#define SIGWINCH    28  \/* Window size change (4.3 BSD, Sun).  *\/   \n#define SIGPOLL     SIGIO   \/* Pollable event occurred (System V).  *\/   \n#define SIGIO       29  \/* I\/O now possible (4.2 BSD).  *\/   \n#define SIGPWR      30  \/* Power failure restart (System V).  *\/   \n#define SIGSYS      31  \/* Bad system call.  *\/   \n#define SIGUNUSED   31  \n<\/code><\/pre>\n<pre><code>\u63d2\u66f2:\u4ec0\u4e48\u662fPOSIX\nPOSIX\u8868\u793a\u53ef\u79fb\u690d\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3\uff08Portable Operating System Interface of UNIX\uff0c\u7f29\u5199\u4e3a POSIX \uff09\uff0cPOSIX\u6807\u51c6\u5b9a\u4e49\u4e86\u64cd\u4f5c\u7cfb\u7edf\u5e94\u8be5\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u7684\u63a5\u53e3\u6807\u51c6\u3002\nPOSIX\u6807\u51c6\u610f\u5728\u671f\u671b\u83b7\u5f97\u6e90\u4ee3\u7801\u7ea7\u522b\u7684\u8f6f\u4ef6\u53ef\u79fb\u690d\u6027\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e3a\u4e00\u4e2aPOSIX\u517c\u5bb9\u7684\u64cd\u4f5c\u7cfb\u7edf\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u5e94\u8be5\u53ef\u4ee5\u5728\u4efb\u4f55\u5176\u5b83\u7684POSIX\u64cd\u4f5c\u7cfb\u7edf\uff08\u5373\u4f7f\u662f\u6765\u81ea\u53e6\u4e00\u4e2a\u5382\u5546\uff09\u4e0a\u7f16\u8bd1\u6267\u884c\u3002\n\n\u7b80\u5355\u6765\u8bf4:\n\u5b8c\u6210\u540c\u4e00\u529f\u80fd\uff0c\u4e0d\u540c\u5185\u6838\u63d0\u4f9b\u7684\u7cfb\u7edf\u8c03\u7528\uff08\u4e5f\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\uff09\u662f\u4e0d\u540c\u7684\u3002\u4f8b\u5982\u521b\u5efa\u8fdb\u7a0b\uff0clinux\u4e0b\u662ffork\u51fd\u6570\uff0cwindows\u4e0b\u662fcreatprocess\u51fd\u6570\u3002\nPOSIX \u8981\u6c42 linux\u548cwindows\u90fd\u8981\u5b9e\u73b0\u57fa\u672c\u7684posix\u6807\u51c6\uff0clinux\u628afork\u51fd\u6570\u5c01\u88c5\u6210posix_fork\uff08\u968f\u4fbf\u8bf4\u7684\uff09\uff0cwindows\u628acreatprocess\u51fd\u6570\u4e5f\u5c01\u88c5\u6210posix_fork\uff0c\u90fd\u58f0\u660e\u5728unistd.h\u91cc\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u5458\u7f16\u5199\u666e\u901a\u5e94\u7528\u65f6\u5019\uff0c\u53ea\u7528\u5305\u542bunistd.h\uff0c\u8c03\u7528posix_fork\u51fd\u6570\uff0c\u7a0b\u5e8f\u5c31\u5728\u6e90\u4ee3\u7801\u7ea7\u522b\u53ef\u79fb\u690d\u4e86\u3002\n\n<\/code><\/pre>\n<h3>2\u3001\u5e38\u89c1\u4fe1\u53f7\u7684\u542b\u4e49<\/h3>\n<h4>SIGHUP &#8211; 1<\/h4>\n<p>\u672c\u4fe1\u53f7\u5728\u7528\u6237\u7ec8\u7aef\u8fde\u63a5(\u6b63\u5e38\u6216\u975e\u6b63\u5e38)\u7ed3\u675f\u65f6\u53d1\u51fa, \u901a\u5e38\u662f\u5728\u7ec8\u7aef\u7684\u63a7\u5236\u8fdb\u7a0b\u7ed3\u675f\u65f6, \u901a\u77e5\u540c\u4e00session\u5185\u7684\u5404\u4e2a\u4f5c\u4e1a, \u8fd9\u65f6\u5b83\u4eec\u4e0e\u63a7\u5236\u7ec8\u7aef\u4e0d\u518d\u5173\u8054\u3002<br \/>\n\u767b\u5f55Linux\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5206\u914d\u7ed9\u767b\u5f55\u7528\u6237\u4e00\u4e2a\u7ec8\u7aef(Session)\u3002\u5728\u8fd9\u4e2a\u7ec8\u7aef\u8fd0\u884c\u7684\u6240\u6709\u7a0b\u5e8f\uff0c\u5305\u62ec\u524d\u53f0\u8fdb\u7a0b\u7ec4\u548c\u540e\u53f0\u8fdb\u7a0b\u7ec4\uff0c\u4e00\u822c\u90fd\u5c5e\u4e8e\u8fd9\u4e2aSession\u3002\u5f53\u7528\u6237\u9000\u51faLinux\u767b\u5f55\u65f6\uff0c\u524d\u53f0\u8fdb\u7a0b\u7ec4\u548c\u540e\u53f0\u6709\u5bf9\u7ec8\u7aef\u8f93\u51fa\u7684\u8fdb\u7a0b\u5c06\u4f1a\u6536\u5230SIGHUP\u4fe1\u53f7\u3002\u8fd9\u4e2a\u4fe1\u53f7\u7684\u9ed8\u8ba4\u64cd\u4f5c\u4e3a\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u56e0\u6b64\u524d\u53f0\u8fdb\u7a0b\u7ec4\u548c\u540e\u53f0\u6709\u7ec8\u7aef\u8f93\u51fa\u7684\u8fdb\u7a0b\u5c31\u4f1a\u4e2d\u6b62\u3002\u4e0d\u8fc7\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u4fe1\u53f7\uff0c\u6bd4\u5982wget\u80fd\u6355\u83b7SIGHUP\u4fe1\u53f7\uff0c\u5e76\u5ffd\u7565\u5b83\uff0c\u8fd9\u6837\u5c31\u7b97\u9000\u51fa\u4e86Linux\u767b\u5f55\uff0cwget\u4e5f\u80fd\u7ee7\u7eed\u4e0b\u8f7d\u3002<br \/>\n\u6b64\u5916\uff0c\u5bf9\u4e8e\u4e0e\u7ec8\u7aef\u8131\u79bb\u5173\u7cfb\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u8fd9\u4e2a\u4fe1\u53f7\u7528\u4e8e\u901a\u77e5\u5b83\u91cd\u65b0\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<h4>SIGINT &#8211; 2<\/h4>\n<p>\u7a0b\u5e8f\u7ec8\u6b62(interrupt)\u4fe1\u53f7\uff0c\u901a\u5e38\u662fCtrl-C)\u65f6\u53d1\u51fa\uff0c\u7528\u4e8e\u901a\u77e5\u524d\u53f0\u8fdb\u7a0b\u7ec4\u7ec8\u6b62\u8fdb\u7a0b\u3002<\/p>\n<h4>SIGQUI &#8211; 3<\/h4>\n<p>\u548cSIGINT\u7c7b\u4f3c, \u4f46\u7531QUIT\u5b57\u7b26(\u901a\u5e38\u662fCtrl-)\u6765\u63a7\u5236.\u8fdb\u7a0b\u5728\u56e0\u6536\u5230SIGQUIT\u9000\u51fa\u65f6\u4f1a\u4ea7\u751fcore\u6587\u4ef6, \u5728\u8fd9\u4e2a\u610f\u4e49\u4e0a\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u7a0b\u5e8f\u9519\u8bef\u4fe1\u53f7\u3002<\/p>\n<h4>SIGILL &#8211; 4<\/h4>\n<p>\u6267\u884c\u4e86\u975e\u6cd5\u6307\u4ee4. \u901a\u5e38\u662f\u56e0\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u672c\u8eab\u51fa\u73b0\u9519\u8bef, \u6216\u8005\u8bd5\u56fe\u6267\u884c\u6570\u636e\u6bb5. \u5806\u6808\u6ea2\u51fa\u65f6\u4e5f\u6709\u53ef\u80fd\u4ea7\u751f\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/p>\n<h4>SIGTRAP &#8211; 5<\/h4>\n<p>\u7531\u65ad\u70b9\u6307\u4ee4\u6216\u5176\u5b83trap\u6307\u4ee4\u4ea7\u751f. \u7531debugger\u4f7f\u7528\u3002<\/p>\n<h4>SIGABRT &#8211; 6<\/h4>\n<p>\u8c03\u7528abort\u51fd\u6570\u751f\u6210\u7684\u4fe1\u53f7\u3002<\/p>\n<h4>SIGBUS &#8211; 7<\/h4>\n<p>\u975e\u6cd5\u5730\u5740\uff0c\u5305\u62ec\u5185\u5b58\u5730\u5740\u5bf9\u9f50(alignment)\u51fa\u9519\u3002\u6bd4\u5982\u8bbf\u95ee\u4e00\u4e2a\u56db\u4e2a\u5b57\u957f\u7684\u6574\u6570, \u4f46\u5176\u5730\u5740\u4e0d\u662f4\u7684\u500d\u6570\u3002<br \/>\n\u5b83\u4e0eSIGSEGV\u7684\u533a\u522b\u5728\u4e8e\u540e\u8005\u662f\u7531\u4e8e\u5bf9\u5408\u6cd5\u5b58\u50a8\u5730\u5740\u7684\u975e\u6cd5\u8bbf\u95ee\u89e6\u53d1\u7684(\u5982\u8bbf\u95ee\u4e0d\u5c5e\u4e8e\u81ea\u5df1\u5b58\u50a8\u7a7a\u95f4\u6216\u53ea\u8bfb\u5b58\u50a8\u7a7a\u95f4)\u3002<\/p>\n<h4>SIGFPE &#8211; 8<\/h4>\n<p>\u7b97\u672f\u8fd0\u7b97\u9519\u8bef\u3002\u4e0d\u4ec5\u5305\u62ec\u6d6e\u70b9\u8fd0\u7b97\u9519\u8bef, \u8fd8\u5305\u62ec\u6ea2\u51fa\u53ca\u9664\u6570\u4e3a0\u7b49\u5176\u5b83\u6240\u6709\u7684\u7b97\u672f\u7684\u9519\u8bef\u3002<\/p>\n<h4>SIGKILL &#8211; 9<\/h4>\n<p>\u7528\u6765\u7acb\u5373\u7ed3\u675f\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u672c\u4fe1\u53f7\u4e0d\u80fd\u88ab\u963b\u585e\u3001\u5904\u7406\u548c\u5ffd\u7565\u3002\u5982\u679c\u7ba1\u7406\u5458\u53d1\u73b0\u67d0\u4e2a\u8fdb\u7a0b\u7ec8\u6b62\u4e0d\u4e86\uff0c\u53ef\u5c1d\u8bd5\u53d1\u9001\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/p>\n<h4>SIGUSR1 &#8211; 10<\/h4>\n<p>\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4fe1\u53f71<\/p>\n<h4>SIGSEGV &#8211; 11<\/h4>\n<p>\u8bbf\u95ee\u975e\u6cd5\u5730\u5740\u3002\u8bd5\u56fe\u8bbf\u95ee\u672a\u5206\u914d\u7ed9\u81ea\u5df1\u7684\u5185\u5b58, \u6216\u8bd5\u56fe\u5f80\u6ca1\u6709\u5199\u6743\u9650\u7684\u5185\u5b58\u5730\u5740\u5199\u6570\u636e.<\/p>\n<h4>SIGUSR2 -12<\/h4>\n<p>\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4fe1\u53f71<\/p>\n<h4>SIGPIPE &#8211; 13<\/h4>\n<p>\u7ba1\u9053\u7834\u88c2\u3002\u8fd9\u4e2a\u4fe1\u53f7\u901a\u5e38\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ea7\u751f\uff0c\u6bd4\u5982\u91c7\u7528FIFO(\u7ba1\u9053)\u901a\u4fe1\u7684\u4e24\u4e2a\u8fdb\u7a0b\uff0c\u8bfb\u7ba1\u9053\u6ca1\u6253\u5f00\u6216\u8005\u610f\u5916\u7ec8\u6b62\u5c31\u5f80\u7ba1\u9053\u5199\uff0c\u5199\u8fdb\u7a0b\u4f1a\u6536\u5230SIGPIPE\u4fe1\u53f7\u3002\u6b64\u5916\u7528Socket\u901a\u4fe1\u7684\u4e24\u4e2a\u8fdb\u7a0b\uff0c\u5199\u8fdb\u7a0b\u5728\u5199Socket\u7684\u65f6\u5019\uff0c\u8bfb\u8fdb\u7a0b\u5df2\u7ecf\u7ec8\u6b62\u3002<\/p>\n<h4>SIGALRM -14<\/h4>\n<p>\u65f6\u949f\u5b9a\u65f6\u4fe1\u53f7, \u8ba1\u7b97\u7684\u662f\u5b9e\u9645\u7684\u65f6\u95f4\u6216\u65f6\u949f\u65f6\u95f4. alarm\u51fd\u6570\u4f7f\u7528\u8be5\u4fe1\u53f7.<\/p>\n<h4>SIGTERM &#8211; 15<\/h4>\n<p>\u7a0b\u5e8f\u7ed3\u675f(terminate)\u4fe1\u53f7, \u4e0eSIGKILL\u4e0d\u540c\u7684\u662f\u8be5\u4fe1\u53f7\u53ef\u4ee5\u88ab\u963b\u585e\u548c\u5904\u7406\u3002\u901a\u5e38\u7528\u6765\u8981\u6c42\u7a0b\u5e8f\u81ea\u5df1\u6b63\u5e38\u9000\u51fa<\/p>\n<h3>3\u3001\u5982\u4f55\u4ea7\u751f\u4fe1\u53f7<\/h3>\n<ul>\n<li>\u5728native\u5e94\u7528\u4e2d \u4f7f\u7528 kill() \u6216\u8005raise()<\/li>\n<\/ul>\n<pre><code>raise(SIGILL);\nkill(SIGILL);\n<\/code><\/pre>\n<ul>\n<li>java \u5e94\u7528\u4e2d\u4f7f\u7528 Procees.sendSignal()\u7b49<\/li>\n<li>adb shell kill \u547d\u4ee4\u5411\u5176\u4ed6\u8fdb\u7a0b\u53d1\u9001singal<\/li>\n<\/ul>\n<pre><code>adb root  \nadb shell ps  \nadb shell kill -3 513  \n\u9996\u5148\u662f\u5207\u6362\u5230root\u7528\u6237 \uff08\u666e\u901a\u8fdb\u7a0b\u53ea\u80fd\u53d1\u4e2a\u81ea\u5df1\u6216\u8005\u540c\u7ec4\u8fdb\u7a0b\uff0c\u800croot\u53ef\u4ee5\u53d1\u9001signal\u7ed9\u4efb\u4f55\u8fdb\u7a0b\uff09\u3002\u7136\u540e\u7528 ps\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8fdb\u7a0b\u4fe1\u606f\u3002\u6700\u540e\u7528kill\u547d\u4ee4\u53d1\u9001SIGQUIT\u7ed9\u8fdb\u7a0b\u53f7\u4e3a513\u7684\u8fdb\u7a0b\u3002\n<\/code><\/pre>\n<h3>4\u3001\u4fe1\u53f7\u5904\u7406\u884c\u4e3a<\/h3>\n<h4>4.1 \u4fe1\u53f7\u5904\u7406\u7684\u65b9\u5f0f\u4e00\u822c\u6709\u4e09\u79cd\uff1a<\/h4>\n<ul>\n<li>\u5ffd\u7565 \u63a5\u6536\u5230\u4fe1\u53f7\u540e\u4e0d\u505a\u4efb\u4f55\u53cd\u5e94<\/li>\n<li>\u81ea\u5b9a\u4e49  \u7528\u81ea\u5b9a\u4e49\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6765\u6267\u884c\u7279\u5b9a\u7684\u52a8\u4f5c<\/li>\n<li>\u9ed8\u8ba4 \u6536\u5230\u4fe1\u53f7\u540e\u6309\u9ed8\u8ba4\u5f97\u884c\u4e3a\u5904\u7406\u8be5\u4fe1\u53f7\u3002 \u8fd9\u662f\u591a\u6570\u5e94\u7528\u91c7\u53d6\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<pre><code>\u4fe1\u53f7\u5904\u7406\u7684\u884c\u4e3a\u662f\u4ee5\u8fdb\u7a0b\u7ea7\u7684\u3002\u5c31\u662f\u8bf4\u4e0d\u540c\u7684\u8fdb\u7a0b\u53ef\u4ee5\u5206\u522b\u8bbe\u7f6e\u4e0d\u540c\u7684\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u800c\u4e92\u4e0d\u5e72\u6270\u3002\u540c\u4e00\u8fdb\u7a0b\u4e2d\u7684\u4e0d\u540c\u7ebf\u7a0b\u867d\u7136\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u4fe1\u53f7\u5c4f\u853d\u5b57\uff0c\u4f46\u662f\u5374\u5171\u4eab\u76f8\u540c\u7684\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f \u3002\n<\/code><\/pre>\n<h4>4.2 \u9488\u5bf9Android\u7cfb\u7edf,\u4fe1\u53f7\u7684\u7279\u6b8a\u884c\u4e3a\u3002<\/h4>\n<p>Android\u4e5f\u662fLinux\u7cfb\u7edf\u3002\u4e3a\u4e86\u5f00\u53d1\u548c\u8c03\u8bd5\u7684\u9700\u8981\uff0candroid\u5bf9\u4e00\u4e9b\u4fe1\u53f7\u7684\u5904\u7406\u5b9a\u4e49\u4e86\u989d\u5916\u7684\u884c\u4e3a\u3002<\/p>\n<h5>1. SIGQUIT \uff08 \u6574\u578b\u503c\u4e3a 3\uff09  \u6253\u5370trace\u4fe1\u606f<\/h5>\n<p>\u4f20\u7edfUNIX\u7cfb\u7edf\u5e94\u7528\uff0c\u5bf9SIGQUIT\u4fe1\u53f7\u7684\u9ed8\u8ba4\u884c\u4e3a\u662f &#8220;\u7ec8\u6b62 + CORE&#8221;\uff0c\u4e5f\u5c31\u662f\u4ea7\u751fcore dump\u6587\u4ef6\u540e\uff0c\u7acb\u5373\u7ec8\u4e8e\u8fd0\u884c\u3002<\/p>\n<p>Android Dalvik\u5e94\u7528\u6536\u5230\u8be5\u4fe1\u53f7\u540e\uff0c\u4f1a \u6253\u5370\u6539\u5e94\u7528\u4e2d\u6240\u6709\u7ebf\u7a0b\u7684\u5f53\u524d\u72b6\u6001\uff0c\u5e76\u4e14\u5e76\u4e0d\u662f\u5f3a\u5236\u9000\u51fa\u3002<br \/>\n\u8fd9\u4e9b\u72b6\u6001\u901a\u5e38\u4fdd\u5b58\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u53eb\u505atrace\u7684\u6587\u4ef6\u4e2d\u3002\u4e00\u822c\u7684\u8def\u5f84\u662f\/data\/anr\/trace.txt<\/p>\n<pre><code>feifeideMacBook-Pro:s1 feifei$ adb shell ps | grep com.sogou.translate.example\nu0_a70       18621  2972 4466496 109576 SyS_epoll_wait 7aced42ec0 S com.sogou.translate.example\nu0_a70       18640  2972 4364644  69144 SyS_epoll_wait 7aced42ec0 S com.sogou.translate.example:logservice\nfeifeideMacBook-Pro:s1 feifei$ adb shell kill -3 18621\n<\/code><\/pre>\n<pre><code>S1 AI Recorder:\/data\/anr # cd \/data\/anr\/                                                                          \nS1 AI Recorder:\/data\/anr # ls\ntrace_01 \n<\/code><\/pre>\n<pre><code>\n----- pid 18621 at 2019-12-04 10:27:19 -----\nCmd line: com.sogou.translate.example\nBuild fingerprint: 'Sogou\/sl8541e_1h10_oversea\/sl8541e_1h10:8.1.0\/OPM2.171019.012\/02421:userdebug\/test-keys'\nABI: 'arm64'\n\n\"main\" prio=5 tid=1 Native\n  | group=\"main\" sCount=1 dsCount=0 flags=1 obj=0x7241fed0 self=0x7a4e8bea00\n  | sysTid=18621 nice=-10 cgrp=default sched=0\/0 handle=0x7ad35279a8\n  | state=S schedstat=( 786261704 135017223 573 ) utm=71 stm=7 core=2 HZ=100\n  | stack=0x7ff1799000-0x7ff179b000 stackSize=8MB\n  | held mutexes=\n  kernel: __switch_to+0xb0\/0xbc\n  kernel: SyS_epoll_wait+0x29c\/0x378\n  kernel: SyS_epoll_pwait+0xbc\/0x130\n  kernel: el0_svc_naked+0x24\/0x28\n  native: #00 pc 0000000000069ec0  \/system\/lib64\/libc.so (__epoll_pwait+8)\n  native: #01 pc 000000000001f734  \/system\/lib64\/libc.so (epoll_pwait+52)\n  native: #02 pc 0000000000015dcc  \/system\/lib64\/libutils.so (android::Looper::pollInner(int)+144)\n  native: #03 pc 0000000000015cb4  \/system\/lib64\/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+108)\n  native: #04 pc 000000000010f75c  \/system\/lib64\/libandroid_runtime.so (???)\n  native: #05 pc 0000000000bd096c  \/data\/dalvik-cache\/arm64\/system@framework@boot.oat (Java_android_os_MessageQueue_nativePollOnce__JI+140)\n  at android.os.MessageQueue.nativePollOnce(Native method)\n  at android.os.MessageQueue.next(MessageQueue.java:325)\n  at android.os.Looper.loop(Looper.java:142)\n  at android.app.ActivityThread.main(ActivityThread.java:6789)\n  at java.lang.reflect.Method.invoke(Native method)\n  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)\n  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)\n<\/code><\/pre>\n<h5>2\u3001\u5bf9\u4e8e\u5f88\u591a\u5176\u4ed6\u7684\u5f02\u5e38\u4fe1\u53f7 \uff08SIGILL, SIGABRT, SIGBUS, SIGFPE, SIGSEGV, SIGSTKFLT ), Android\u8fdb\u7a0b \u5728\u9000\u51fa\u524d\uff0c\u4f1a\u5728\/data\/tombstones\uff0c\u751f\u6210 tombstone\u6587\u4ef6\u3002<\/h5>\n<pre><code> adb shell ps | grep com.sogou.translate.example\n adb shell kill -11 18621\n adb pull \/data\/tombstones\n<\/code><\/pre>\n<pre><code>*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\nNative Crash TIME: 19573247\n*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\nBuild fingerprint: 'Sogou\/sl8541e_1h10_oversea\/sl8541e_1h10:8.1.0\/OPM2.171019.012\/02421:userdebug\/test-keys'\nRevision: '0'\nABI: 'arm64'\npid: 22302, tid: 22319, name: Binder:22302_4  &gt;&gt;&gt; com.sogou.iot.b1pro.launcher:ttsservice &lt;&lt;&lt;\nsignal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x7b29df9480\n    x0   00000000ffffffff  x1   0000000000000081  x2   000000007fffffff  x3   0000000000000000\n    x4   0000000000000000  x5   0000000000000000  x6   0000000000000000  x7   0000007a45ff5000\n    x8   000000003f4faf20  x9   0000000000000002  x10  0000007a2ca0d800  x11  0000000000000002\n    x12  0000000000000001  x13  0000007a2c5ca800  x14  0000000000000000  x15  0000000000000001\n    x16  0000007aceda4240  x17  0000007acecf67d0  x18  0000000000000004  x19  0000007a32ef9278\n    x20  0000007a34d6bd98  x21  0000000000000000  x22  0000007a32ef9310  x23  00000000ffffffff\n    x24  0000007a45e85000  x25  0000007a34d6b910  x26  000000000000013c  x27  0000000000000000\n    x28  0000000000000050  x29  0000007a34d6bd80  x30  0000007a3244d5b4\n    sp   0000007a34d6b4c0  pc   0000007a3244d728  pstate 00000000a0000000\n    v0   00000000000000000000000000000000  v1   00000000211712c600000000211712c6\n    v2   0000000000ffff5a0000000000000000  v3   00000000030000000000000000000000\n    v4   00000000000000010000000000000000  v5   00000000000000030000000000000000\n    v6   00000000000000000000000043420c74  v7   0000000000000000000000003ba3d70a\n    v8   0000000000000000401921fb54442d18  v9   00000000000000000000000000000000\n    v10  00000000000000000000000000000000  v11  00000000000000000000000000000000\n    v12  00000000000000000000000000000000  v13  00000000000000000000000000000000\n    v14  00000000000000000000000000000000  v15  00000000000000000000000000000000\n    v16  000000000000000000000000433d0bdb  v17  0000000000000000000000004300f791\n    v18  ffffffffffffffffffffffffffffffff  v19  00000000000000000000000000000000\n    v20  000000000000000000000000429b0da8  v21  0000007a3244bad40000000040066666\n    v22  3d9a026fbe6e77413de4d4f6bde37556  v23  be46eb12bab5227e3e42ccdd3ddc0298\n    v24  bd81aadc3baa94113d553e38bd7e0cfa  v25  3d973dfebe6f2a7a3df6b7edbdeade23\n    v26  be3550e13b58e3993e4a9fc03dce668e  v27  bd667f7d3c8acb883d67db0dbd3870f1\n    v28  3d95a576be6d76b93e067243bdeb7287  v29  be2c0caf3b2d9cfa3e47bc863da722ea\n    v30  bd2c2f113d0578df3d823731bcdbef07  v31  3d93ca5dbe68d8923e133c79bde8fffc\n    fpsr 00000017  fpcr 00000000\n\nbacktrace:\n    #00 pc 0000000000241728  \/data\/app\/com.sogou.iot.b1pro.launcher-fmvk7osx2wimDuqXl484ZQ==\/lib\/arm64\/libttsoff.so\n    #01 pc 00000000002436dc  \/data\/app\/com.sogou.iot.b1pro.launcher-fmvk7osx2wimDuqXl484ZQ==\/lib\/arm64\/libttsoff.so\n    #02 pc 0000000000253a38  \/data\/app\/com.sogou.iot.b1pro.launcher-fmvk7osx2wimDuqXl484ZQ==\/lib\/arm64\/libttsoff.so\n    #03 pc 0000000000252308  \/data\/app\/com.sogou.iot.b1pro.launcher-fmvk7osx2wimDuqXl484ZQ==\/lib\/arm64\/libttsoff.so\n    #04 pc 00000000002545fc  \/data\/app\/com.sogou.iot.b1pro.launcher-fmvk7osx2wimDuqXl484ZQ==\/lib\/arm64\/libttsoff.so (Java_com_sogou_speech_tts_TTSOffline_nativeSynthesize+124)\n    #05 pc 000000000005d9b4  \/data\/app\/com.sogou.iot.b1pro.launcher-fmvk7osx2wimDuqXl484ZQ==\/oat\/arm64\/base.odex (offset 0x5a000)\n\nstack:\n         0000007a34d6b440  0000007a34d6b4b0\n         0000007a34d6b448  f1ecdfe5bc67f748\n         0000007a34d6b450  0000000000000000\n         0000007a34d6b458  401921fb54442d18\n         0000007a34d6b460  0000000000000050\n         0000007a34d6b468  0000000000000000\n         0000007a34d6b470  000000000000013c\n<\/code><\/pre>\n<h4>4.3  android \u6355\u6349native crash \u9700\u8981\u6ce8\u518c\u4e0b\u97626\u4e2a\u4fe1\u53f7<\/h4>\n<p>Android \u5e73\u53f0 \u6355\u6349natvie crash \u4e00\u822c\u53ea\u9700\u8981\u5b89\u88c56\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4fe1\u53f7<\/th>\n<th>\u4fe1\u53f7\u503c<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u5907\u6ce8<\/th>\n<th>\u5728Android\u4e2d\u9ed8\u8ba4\u884c\u4e3a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>SIGSEGV<\/td>\n<td>11<\/td>\n<td>\u8bbf\u95ee\u65e0\u6548\u5730\u5740<\/td>\n<td>\u5982\u8bd5\u56fe\u8bbf\u95ee\u672a\u5206\u914d\u7ed9\u81ea\u5df1\u7684\u5185\u5b58<\/td>\n<td>\u751f\u6210tombstone\u6587\u4ef6,\u7136\u540e\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>SIGBUS<\/td>\n<td>7<\/td>\n<td>\u975e\u6cd5\u5730\u5740<\/td>\n<td>\u5305\u62ec\u5185\u5b58\u5730\u5740\u5bf9\u9f50(alignment)\u51fa\u9519\u3002<\/td>\n<td>\u751f\u6210tombstone\u6587\u4ef6,\u7136\u540e\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>SIGABRT<\/td>\n<td>6<\/td>\n<td>\u8c03\u7528abort\u51fd\u6570\u751f\u6210\u7684\u4fe1\u53f7\u3002<\/td>\n<td><\/td>\n<td>\u751f\u6210tombstone\u6587\u4ef6,\u7136\u540e\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>SIGFPE<\/td>\n<td>8<\/td>\n<td>\u6d6e\u70b9\u8ba1\u7b97\u9519\u8bef\u3002<\/td>\n<td>\u5305\u62ec\u6d6e\u70b9\u8fd0\u7b97\u9519\u8bef, \u8fd8\u5305\u62ec\u6ea2\u51fa\u53ca\u9664\u6570\u4e3a0\u7b49\u7b97\u6570\u8fd0\u7b97\u9519\u8bef<\/td>\n<td>\u751f\u6210tombstone\u6587\u4ef6,\u7136\u540e\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>SIGILL<\/td>\n<td>4<\/td>\n<td>\u975e\u6cd5\u6307\u4ee4\u9519\u8bef\u3002<\/td>\n<td>\u975e\u6cd5\u6307\u4ee4\u9519\u8bef\u3002<\/td>\n<td>\u751f\u6210tombstone\u6587\u4ef6,\u7136\u540e\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>SIGTRAP<\/td>\n<td>5<\/td>\n<td>\u786c\u4ef6\u9519\u8bef\uff08\u901a\u5e38\u4e3a\u65ad\u70b9\u6307\u4ee4\uff09<\/td>\n<td><\/td>\n<td>\u751f\u6210tombstone\u6587\u4ef6,\u7136\u540e\u9000\u51fa<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e09\u3001\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u4f7f\u7528<\/h2>\n<h3>3.1\u3001\u5b89\u88c5\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/h3>\n<h4>(1)sigaction\u51fd\u6570\u7684\u529f\u80fd\u662f\u68c0\u67e5\u6216\u4fee\u6539\u4e0e\u6307\u5b9a\u4fe1\u53f7\u76f8\u5173\u8054\u7684\u5904\u7406\u52a8\u4f5c\uff08\u53ef\u540c\u65f6\u4e24\u79cd\u64cd\u4f5c\uff09<\/h4>\n<pre><code>int sigaction(int signum, const struct sigaction *act,\n                     struct sigaction *oldact);\n<\/code><\/pre>\n<p>signum\u53c2\u6570\u6307\u51fa\u8981\u6355\u83b7\u7684\u4fe1\u53f7\u7c7b\u578b\uff0cact\u53c2\u6570\u6307\u5b9a\u65b0\u7684\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\uff0coldact\u53c2\u6570\u8f93\u51fa\u5148\u524d\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\uff08\u5982\u679c\u4e0d\u4e3aNULL\u7684\u8bdd\uff09\u3002<\/p>\n<h4>(2)struct sigaction\u7ed3\u6784\u4f53\u4ecb\u7ecd<\/h4>\n<pre><code>struct sigaction {\n    void (*sa_handler)(int);\n    void (*sa_sigaction)(int, siginfo_t *, void *);\n    sigset_t sa_mask;\n    int sa_flags;\n    void (*sa_restorer)(void);\n}\n\n<\/code><\/pre>\n<h5>sa_handler\u4ee3\u8868\u65b0\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u4ec5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570.<\/h5>\n<pre><code>\u5982:\nvoid show_handler(int signo)\n{\n    printf(\"I got signal %dn\", signo);\n}\n<\/code><\/pre>\n<h5>sa_sigaction \u540c\u6837\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u76f8\u5bf9\u4e8esa_handler\u53ef\u4ee5\u83b7\u5f97\u66f4\u591a\u7684\u4fe1\u606f\uff1a<\/h5>\n<pre><code>void handler(int signo, siginfo_t *info, void *context);\n<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u4e00\u4e2asiginfo_t\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8be5\u7ed3\u6784\u63cf\u8ff0\u4e86\u4fe1\u53f7\u4ea7\u751f\u7684\u539f\u56e0<\/p>\n<pre><code>struct siginfo_t\n{\n    int     si_signo;       \/\/ signal number\n    int     si_errno;       \/\/ if nonzero, errno value from <errno.h>\n    int     si_code;        \/\/ additional info (depends on signal)\n    pid_t   si_pid;         \/\/ sending process ID\n    uid_t   si_uid;         \/\/ sending process real user ID\n    void    *si_addr;       \/\/ address that cased the fault\n    int     si_status;      \/\/ exit value or signal number\n    long    si_band;        \/\/ band number for SIGPOLL\n\n    \/* possibly other fileds also *\/\n\n}\n<\/errno.h><\/code><\/pre>\n<p>\u4e00\u822csiginfo_t\u7ed3\u6784\u81f3\u5c11\u5305\u542bsi_signo\u548csi_code\u6210\u5458\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570context\u662f\u4e00\u4e2a\u65e0\u7c7b\u578b\u7684\u6307\u9488\uff0c\u5b83\u53ef\u4ee5\u88ab\u5f3a\u5236\u8f6c\u6362\u4e3aucntext_t\u7ed3\u6784\u7c7b\u578b\uff0c\u7528\u4e8e\u6807\u8bc6\u4fe1\u53f7\u4f20\u9012\u65f6\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u5f53sig_action.sa_flags = SA_SIGINFO \u65f6,\u4f1a\u4f7f\u7528sa_sigaction\u4f5c\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570;\u5426\u5219\u4f7f\u7528 sa_handler \u4f5c\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<\/p>\n<pre><code>\n\u5907\u6ce8\uff1a\nsa_sigaction\u548csa_handler\u5b57\u6bb5\uff0c\u5176\u5b9e\u73b0\u53ef\u80fd\u4f7f\u7528\u540c\u4e00\u5b58\u50a8\u533a\uff0c\u6240\u4ee5\u5e94\u7528\u7a0b\u5e8f\u53ea\u80fd\u4e00\u6b21\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u4e2d\u7684\u4e00\u4e2a\u3002\n<\/code><\/pre>\n<h5>sa_mask \u7528\u6765\u8bbe\u7f6e\u5728\u5904\u7406\u8be5\u4fe1\u53f7\u65f6\u6682\u65f6\u5c06sa_mask \u6307\u5b9a\u7684\u4fe1\u53f7\u96c6\u6401\u7f6e<\/h5>\n<h5>sa_flags \u7528\u6765\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u7684\u5176\u4ed6\u76f8\u5173\u64cd\u4f5c\uff0c\u4e0b\u5217\u7684\u6570\u503c\u53ef\u7528:<\/h5>\n<ul>\n<li>SA_RESETHAND\uff1a\u5f53\u8c03\u7528\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u65f6\uff0c\u5c06\u4fe1\u53f7\u7684\u5904\u7406\u51fd\u6570\u91cd\u7f6e\u4e3a\u7f3a\u7701\u503cSIG_DFL\uff08\u9ed8\u8ba4\u5904\u7406\u65b9\u5f0f\uff09<\/li>\n<li>SA_RESTART\uff1a\u5982\u679c\u4fe1\u53f7\u4e2d\u65ad\u4e86\u8fdb\u7a0b\u7684\u67d0\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u5219\u7cfb\u7edf\u81ea\u52a8\u542f\u52a8\u8be5\u7cfb\u7edf\u8c03\u7528<\/li>\n<li>SA_NODEFER \uff1a\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c \u5f53\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd0\u884c\u65f6\uff0c\u5185\u6838\u5c06\u963b\u585e\u8be5\u7ed9\u5b9a\u4fe1\u53f7\u3002\u4f46\u662f\u5982\u679c\u8bbe\u7f6e\u4e86 SA_NODEFER\u6807\u8bb0\uff0c \u90a3\u4e48\u5728\u8be5\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd0\u884c\u65f6\uff0c\u5185\u6838\u5c06\u4e0d\u4f1a\u963b\u585e\u8be5\u4fe1\u53f7<\/li>\n<li>SA_SIGINFO\uff1a\u8bbe\u7f6e\u9009\u62e9sa_sigaction \u4f5c\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u5426\u5219\u9009\u62e9sa_handler\u4f5c\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<\/li>\n<li>SA_RESETHAND\uff1a\u4fe1\u53f7\u5904\u7406\u4e4b\u540e\u91cd\u65b0\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th>\u9009\u9879<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>SA_INTERRUPT<\/td>\n<td>\u7531\u6b64\u4fe1\u53f7\u4e2d\u65ad\u7684\u7cfb\u7edf\u8c03\u7528\u4e0d\u4f1a\u81ea\u52a8\u91cd\u542f<\/td>\n<\/tr>\n<tr>\n<td>SA_NOCLDSTOP<\/td>\n<td>\u82e5signo\u662fSIGCHLD\uff0c\u5f53\u5b50\u8fdb\u7a0b\u505c\u6b62\uff08\u4f5c\u4e1a\u63a7\u5236\uff09\u65f6\uff0c\u4e0d\u4ea7\u751f\u6b64\u4fe1\u53f7\u3002\u5f53\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u4ecd\u4ea7\u751f\u6b64\u4fe1\u53f7\uff08\u53c2\u52a0SA_NOCLDWAIT\u8bf4\u660e\uff09\u3002\u82e5\u5df2\u8bbe\u7f6e\u6b64\u6807\u5fd7\uff0c\u5219\u5f53\u505c\u6b62\u7684\u8fdb\u7a0b\u7ee7\u7eed\u8fd0\u884c\u65f6\uff0c\u4f5c\u4e3aXSI\u6269\u5c55\uff0c\u4e0d\u53d1\u9001SIGCHLD\u4fe1\u53f7\u3002<\/td>\n<\/tr>\n<tr>\n<td>SA_NOCLDWAIT<\/td>\n<td>\u82e5signo\u662fSIGCHLD\uff0c\u5219\u5f53\u8c03\u7528\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u4e0d\u521b\u5efa\u50f5\u5c38\u8fdb\u7a0b\u3002\u82e5\u8c03\u7528\u8fdb\u7a0b\u5728\u540e\u9762\u8c03\u7528wait\uff0c\u5219\u8c03\u7528\u8fdb\u7a0b\u963b\u585e\uff0c\u76f4\u5230\u5176\u6240\u6709\u5b50\u8fdb\u7a0b\u90fd\u7ec8\u6b62\uff0c\u6b64\u65f6\u8fd4\u56de-1\uff0c\u5e76\u5c06errno\u8bbe\u7f6e\u4e3aECHILD\u3002<\/td>\n<\/tr>\n<tr>\n<td>SA_NODEFER<\/td>\n<td>\u5f53\u6355\u6349\u5230\u6b64\u4fe1\u53f7\u65f6\uff0c\u5728\u6267\u884c\u5176\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u4e0d\u81ea\u52a8\u963b\u585e\u6b64\u4fe1\u53f7\uff08\u9664\u975esa_mask\u5305\u62ec\u4e86\u6b64\u4fe1\u53f7\uff09\u3002<\/td>\n<\/tr>\n<tr>\n<td>SA_ONSTACK<\/td>\n<td>\u82e5\u7528sigaltstack\u58f0\u660e\u4e86\u66ff\u6362\u6808\uff0c\u5219\u5c06\u6b64\u4fe1\u53f7\u9012\u9001\u7ed9\u66ff\u6362\u6808\u4e0a\u7684\u8fdb\u7a0b\u3002<\/td>\n<\/tr>\n<tr>\n<td>SA_RESETHAND<\/td>\n<td>\u5728\u6b64\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u5165\u53e3\u5904\uff0c\u5c06\u6b64\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u590d\u4f4d\u4e3aSIG_DEF\uff0c\u5e76\u6e05\u9664SA_SIGINFO\u6807\u5fd7\u3002\u4f46\u662f\uff0c\u4e0d\u80fd\u81ea\u52a8\u590d\u4f4dSIGILL\u548cSIGTRAP\u8fd9\u4e24\u4e2a\u4fe1\u53f7\u7684\u914d\u7f6e\u3002\u8bbe\u7f6e\u6b64\u6807\u5fd7\u662fsigaction\u7684\u884c\u4e3a\u5982\u540cSA_NODEFER\u6807\u5fd7\u4e5f\u8bbe\u7f6e\u4e86\u4e00\u6837\u3002<\/td>\n<\/tr>\n<tr>\n<td>SA_RESTART<\/td>\n<td>\u7531\u6b64\u4fe1\u53f7\u4e2d\u65ad\u7684\u7cfb\u7edf\u8c03\u7528\u4f1a\u81ea\u52a8\u91cd\u542f\u52a8\u3002<\/td>\n<\/tr>\n<tr>\n<td>SA_SIGINFO<\/td>\n<td>\u8bbe\u7f6e\u4e86\u8be5\u6807\u5fd7\u540e,\u4f1a\u9009\u62e9sa_sigaction \u4f5c\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570,\u5426\u5219\u9009\u62e9sa_handler\u4f5c\u4e3a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>(3)\u8981\u7528\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6355\u83b7\u5230native crash(SIGSEGV, SIGBUS\u7b49) \u53ef\u4ee5\u4f7f\u7528sig_action \uff0c\u5982\u4e0b:<\/h4>\n<p>\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/p>\n<pre><code>void posix_signal_handler(int sig, siginfo_t *siginfo, void *context)\n{\n    (void)context;\n    switch(sig)\n    {\n        case SIGSEGV:\n            fputs(\"Caught SIGSEGV: Segmentation Faultn\", stderr);\n            break;\n        case SIGINT:\n            fputs(\"Caught SIGINT: Interactive attention signal, (usually ctrl+c)n\",\n                  stderr);\n            break;\n        case SIGFPE:\n            switch(siginfo-&gt;si_code)\n            {\n                case FPE_INTDIV:\n                    fputs(\"Caught SIGFPE: (integer divide by zero)n\", stderr);\n                    break;\n                case FPE_INTOVF:\n                    fputs(\"Caught SIGFPE: (integer overflow)n\", stderr);\n                    break;\n                case FPE_FLTDIV:\n                    fputs(\"Caught SIGFPE: (floating-point divide by zero)n\", stderr);\n                    break;\n                case FPE_FLTOVF:\n                    fputs(\"Caught SIGFPE: (floating-point overflow)n\", stderr);\n                    break;\n                case FPE_FLTUND:\n                    fputs(\"Caught SIGFPE: (floating-point underflow)n\", stderr);\n                    break;\n                case FPE_FLTRES:\n                    fputs(\"Caught SIGFPE: (floating-point inexact result)n\", stderr);\n                    break;\n                case FPE_FLTINV:\n                    fputs(\"Caught SIGFPE: (floating-point invalid operation)n\", stderr);\n                    break;\n                case FPE_FLTSUB:\n                    fputs(\"Caught SIGFPE: (subscript out of range)n\", stderr);\n                    break;\n                default:\n                    fputs(\"Caught SIGFPE: Arithmetic Exceptionn\", stderr);\n                    break;\n            }\n        case SIGILL:\n            switch(siginfo-&gt;si_code)\n            {\n                case ILL_ILLOPC:\n                    fputs(\"Caught SIGILL: (illegal opcode)n\", stderr);\n                    break;\n                case ILL_ILLOPN:\n                    fputs(\"Caught SIGILL: (illegal operand)n\", stderr);\n                    break;\n                case ILL_ILLADR:\n                    fputs(\"Caught SIGILL: (illegal addressing mode)n\", stderr);\n                    break;\n                case ILL_ILLTRP:\n                    fputs(\"Caught SIGILL: (illegal trap)n\", stderr);\n                    break;\n                case ILL_PRVOPC:\n                    fputs(\"Caught SIGILL: (privileged opcode)n\", stderr);\n                    break;\n                case ILL_PRVREG:\n                    fputs(\"Caught SIGILL: (privileged register)n\", stderr);\n                    break;\n                case ILL_COPROC:\n                    fputs(\"Caught SIGILL: (coprocessor error)n\", stderr);\n                    break;\n                case ILL_BADSTK:\n                    fputs(\"Caught SIGILL: (internal stack error)n\", stderr);\n                    break;\n                default:\n                    fputs(\"Caught SIGILL: Illegal Instructionn\", stderr);\n                    break;\n            }\n            break;\n        case SIGTERM:\n            fputs(\"Caught SIGTERM: a termination request was sent to the programn\",\n                  stderr);\n            break;\n        case SIGABRT:\n            fputs(\"Caught SIGABRT: usually caused by an abort() or assert()n\", stderr);\n            break;\n        default:\n            break;\n    }\n\n    _Exit(1);\n}\n<\/code><\/pre>\n<p>\u5b89\u88c5\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/p>\n<pre><code>\n{\n        struct sigaction sig_action = {};\n        sig_action.sa_sigaction = posix_signal_handler;\n        sigemptyset(&amp;sig_action.sa_mask);\n\n#ifdef __APPLE__\n        \/* for some reason we backtrace() doesn't work on osx\n           when we use an alternate stack *\/\n        sig_action.sa_flags = SA_SIGINFO;\n#else\n        sig_action.sa_flags = SA_SIGINFO | SA_ONSTACK;\n#endif\n\n        if (sigaction(SIGSEGV, &amp;sig_action, NULL) != 0) { err(1, \"sigaction\"); }\n}\n<\/code><\/pre>\n<h3>3.2 \u5904\u7406\u6808\u6ea2\u51fa  &#8211;  sigaltstack()<\/h3>\n<p>Native crash \u4e2d\u6709\u4e00\u79cd\u662f\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u8c03\u7528\u51fd\u6570\u65f6\u4f1a\u5c06\u88ab\u8c03\u7528\u51fd\u6570\u5165\u6808\uff0c\u5e76\u4fdd\u5b58\u8be5\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u7b49\u4fe1\u606f\u3002\u5f53\u6808\u6ee1\u4e86\uff08\u592a\u591a\u6b21\u9012\u5f52\uff0c\u6808\u4e0a\u592a\u591a\u5bf9\u8c61\uff09\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u540c\u4e00\u4e2a\u5df2\u7ecf\u6ee1\u4e86\u7684\u6808\u4e0a\u8c03\u7528SIGSEGV\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u53c8\u518d\u4e00\u6b21\u5f15\u8d77\u540c\u6837\u7684\u4fe1\u53f7\u3002<\/p>\n<p>sigaltstack() \u5141\u8bb8\u8fdb\u7a0b\u521b\u5efa\u4e00\u4e2a\u5907\u7528\u7684\u6808,\u4f9b\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4f7f\u7528\u3002<\/p>\n<pre><code>int sigaltstack(const stack_t *ss, stack_t *oss);\n<\/code><\/pre>\n<p>\u8be5\u51fd\u6570\u4e24\u4e2a\u4e2a\u53c2\u6570\u4e3a\u5747\u4e3astack_t\u7c7b\u578b\u7684\u7ed3\u6784\u4f53<\/p>\n<pre><code>typedef struct {\n   void  *ss_sp;     \/* Base address of stack *\/\n   int    ss_flags;  \/* Flags *\/\n   size_t ss_size;   \/* Number of bytes in stack *\/\n}\n<\/code><\/pre>\n<p>\u8981\u60f3\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u53ef\u66ff\u6362\u4fe1\u53f7\u6808\uff0css_flags\u5fc5\u987b\u8bbe\u7f6e\u4e3a0\uff0css_sp\u548css_size\u5206\u522b\u6307\u660e\u53ef\u66ff\u6362\u4fe1\u53f7\u6808\u7684\u8d77\u59cb\u5730\u5740\u548c\u6808\u5927\u5c0f\u3002<\/p>\n<p>sigaltstack\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u521b\u5efa\u7684\u65b0\u7684\u53ef\u66ff\u6362\u4fe1\u53f7\u6808\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u4e3aNULL\uff0c\u5982\u679c\u4e0d\u4e3aNULL\u7684\u8bdd\uff0c\u5c06\u4f1a\u5c06\u65e7\u7684\u53ef\u66ff\u6362\u4fe1\u53f7\u6808\u7684\u4fe1\u606f\u4fdd\u5b58\u5728\u91cc\u9762\u3002\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de-1.<\/p>\n<p>\u4f7f\u7528\u53ef\u66ff\u6362\u4fe1\u53f7\u6808\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5728\u5185\u5b58\u4e2d\u5206\u914d\u4e00\u5757\u533a\u57df\u4f5c\u4e3a\u53ef\u66ff\u6362\u4fe1\u53f7\u6808<\/li>\n<li>\u4f7f\u7528sinalstack()\u51fd\u6570\u901a\u77e5\u7cfb\u7edf \u5b58\u5728\u4e00\u4e2a\u53ef\u4ee5\u66ff\u6362\u7684\u4fe1\u53f7\u6808\u3002<\/li>\n<li>\u4f7f\u7528sigaction()\u51fd\u6570\u5efa\u7acb\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u65f6\u5019\uff0c\u901a\u8fc7\u5c06sa_flags\u8bbe\u7f6e\u4e3aSA_ONSTACK\u6765\u544a\u8bc9\u7cfb\u7edf\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u5c06\u5728\u53ef\u66ff\u6362\u4fe1\u53f7\u6808\u4e0a\u9762\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801:<\/p>\n<pre><code>\nstack_t stack;\nmemset(&amp;stack, 0, sizeof(stack));\n\/* Reserver the system default stack size. We don't need that much by the way. *\/\nstack.ss_size = SIGSTKSZ;\nstack.ss_sp = malloc(stack.ss_size);\nstack.ss_flags = 0;\n\/* Install alternate stack size. Be sure the memory region is valid until you revert it. *\/\nif (stack.ss_sp != NULL &amp;&amp; sigaltstack(&amp;stack, NULL) == 0) {\n  ...\n}\n\n<\/code><\/pre>\n<h3>3.3 \u517c\u5bb9\u65e7\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/h3>\n<p>\u6211\u4eec\u5728java\u865a\u62df\u673a\u4e0a\u8fd0\u884c\uff0c\u67d0\u4e9b\u4fe1\u53f7\u53ef\u80fd\u5728\u4e4b\u524d\u5df2\u7ecf\u88ab\u5b89\u88c5\u8fc7\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002\u4f8b\u5982\uff0cSIGSEGV\u7ecf\u5e38\u7528\u4e8e\u5904\u7406NullPointerException.<br \/>\n\u6240\u4ee5\uff0c\u4f60\u5fc5\u987b\u5148\u8c03\u7528\u65e7\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u4ee5\u9632\u628a\u4e0a\u4e0b\u6587\u73af\u5883\u641e\u4e71\u3002\u65e7\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8981\u4e48\u4e0d\u8fdb\u884c\u5904\u7406\u76f4\u63a5\u8fd4\u56de\uff0c\u8981\u4e48\u8c03\u7528abort()\uff08\u8fd9\u6837\u6211\u4eec\u5c31\u6709\u6700\u540e\u4e00\u6b21\u673a\u4f1a\u901a\u8fc7SIGABRT\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u5904\u7406\u8fd9\u4e2a\u4fe1\u53f7\uff0c\u6240\u4ee5\u5728\u6355\u83b7SIGABRT\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u91cc\u8fb9\uff0c\u6211\u4eec\u662f\u6700\u540e\u624d\u8c03\u7528\u65e7\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff09<\/p>\n<pre><code>static void my_handler(const int code, siginfo_t *const si, void *const sc) {\n  \/* Call previous handler. *\/\n  old_handler.sa_sigaction(code, si, sc);\n...\n\n}\n<\/code><\/pre>\n<h3>3.4 \u63d0\u53d6\u5d29\u6e83\u4fe1\u606f<\/h3>\n<p>\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u6709\u4e30\u5bcc\u7684\u4fe1\u606f\uff0c\u4f9b\u6211\u4eec\u5206\u6790crash\u53d1\u751f\u7684\u539f\u56e0<\/p>\n<pre><code>\/*\u4fe1\u53f7\u5904\u7406\u51fd\u6570*\/\nvoid (*sa_sigaction)(const int code, siginfo_t *const si, void * const sc) \n\nsiginfo_t {\n   int      si_signo;     \/* Signal number \u4fe1\u53f7\u91cf *\/\n   int      si_errno;     \/* An errno value *\/\n   int      si_code;      \/* Signal code \u9519\u8bef\u7801 *\/\n   }\n\n<\/code><\/pre>\n<h4>1\u3001 signo \u548ccode<\/h4>\n<p>\u53d1\u751fnative crash\u4e4b\u540e\uff0clogcat\u4e2d\u4f1a\u6253\u51fa\u5982\u4e0b\u4e00\u53e5\u4fe1\u606f\uff1a<br \/>\nsignal 11 (SIGSEGV), code 0 (SI_USER), fault addr 0x0<br \/>\nsigaction \u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3asiginfo_t\u7ed3\u6784\u4f53\uff0c\u5176\u4e2d\u7684 si_signo\u548csi_code \u53ef\u4ee5\u5f97\u51facrash\u7684\u5927\u81f4\u539f\u56e0<\/p>\n<h4>2\u3001 \u6536\u96c6\u53d1\u751fcrash\u7684\u5730\u5740(pc):<\/h4>\n<p>sigaction\u56de\u8c03\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u6307\u5411ucontext_t\u7684\u6307\u9488\uff0cucontext_t\u6536\u96c6\u4e86\u5bc4\u5b58\u5668\u6570\u503c\uff08\u8fd8\u6709\u5404\u79cd\u5904\u7406\u5668\u7279\u5b9a\u7684\u4fe1\u606f\uff09\u3002<br \/>\n\u5728x86-64\u67b6\u6784\uff0cpc\u503c\u662f\u5b58\u5728uc_mcontext.gregs[REG_RIP]\uff1b<br \/>\n\u5728arm\u67b6\u6784\uff0c\u5219\u662fuc_mcontext.arm_pc\u3002\u4e0d\u8fc7\u5728Android\u4e0a\uff0cucontext_t\u7ed3\u6784\u4f53\u6ca1\u6709\u5728\u4efb\u4f55\u7cfb\u7edf\u5934\u6587\u4ef6\u5b9a\u4e49\uff0c\u6240\u4ee5\u8981\u81ea\u5df1\u53bb\u5f15\u5165\u4e00\u4efd\u5b9a\u4e49\u3002<\/p>\n<h4>3\u3001\u627e\u51facrash\u7684pc\uff0c\u5c5e\u4e8e\u54ea\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u6216so\u3002\u5206\u6790\u662f\u54ea\u4e2aso\u5e93\u51fa\u73b0\u4e86\u5954\u6e83\uff0c\u5954\u6e83\u5728\u54ea\u4e2a\u51fd\u6570\u3002<\/h4>\n<h5>\uff081\uff09 dladdr()  \u6839\u636epc\u503c\u83b7\u5f97\u5171\u4eab\u5e93\u540d\u5b57\u548c\u76f8\u5bf9\u504f\u79fb\u5730\u5740<\/h5>\n<p>dladdr \u53ef\u4ee5\u5c06pc\u7a0b\u5e8f\u8ba1\u6570\u5668\u6307\u5411\u7684\u51b3\u5b9a\u5730\u5740 \u8f6c\u6362\u6210Dl_info \u5bf9\u8c61\u3002\u4ece\u800c\u8ba1\u7b97\u51fapc\u6307\u5411\u5730\u5740 \u6240\u5728\u7684\u52a8\u6001\u5e93\u540d\u5b57\uff0c\u8be5\u52a8\u6001\u5e93\u7684\u57fa\u5730\u5740\uff0cpc\u5730\u5740\u6700\u8fd1\u7684\u7b26\u53f7\u540d\u7b49\u3002<\/p>\n<p>Dl_info \u7ed3\u6784\u4f53\u4e2a\u5b57\u6bb5\u7684\u610f\u4e49<\/p>\n<pre><code>typedef struct {\n  \/* Pathname of shared object that contains address. *\/\n  const char* dli_fname; \/\/\u5171\u4eab\u5e93\u7684\u5b8c\u5168\u8def\u5f84\u540d\n  \/* Address at which shared object is loaded. *\/\n  void* dli_fbase;     \/\/\u8be5\u5171\u4eab\u5e93\u7684\u57fa\u5730\u5740\n  \/* Name of nearest symbol with address lower than addr. *\/\n  const char* dli_sname; \/\/\u548c\u6307\u5b9apc\u503c\u6700\u8fd1\u7684\u7b26\u53f7\u540d\n  \/* Exact address of symbol named in dli_sname. *\/\n  void* dli_saddr;      \/\/dli_name \u7b26\u53f7\u540d\u7684\u7edd\u5bf9\u5730\u5740\n} Dl_info;\n\n<\/code><\/pre>\n<p>dladdr()\u7528\u6cd5:<\/p>\n<pre><code>    Dl_info info;  \nif (dladdr(addr, &amp;info) != 0 &amp;&amp; info.dli_fname != NULL) {  \n  void * const nearest = info.dli_saddr;  \n  \/\/\u76f8\u5bf9\u504f\u79fb\u5730\u5740\n  const uintptr_t addr_relative =  \n    ((uintptr_t) addr - (uintptr_t) info.dli_fbase);  \n  ...  \n}\n\n<\/code><\/pre>\n<h3>4\u3001\u5982\u4f55\u6839\u636epc \u624b\u52a8\u5206\u6790\u51fa \u52a8\u6001\u5e93\u7684\u540d\u5b57\u548c\u76f8\u5bf9\u5730\u5740<\/h3>\n<h5>\uff081\uff09linux\u8fdb\u7a0b\u7684\u5730\u5740\u5206\u5e03\u7a7a\u95f4<\/h5>\n<div class=\"image-package\">\n<div class=\"image-container\" style=\"max-width: 700px; max-height: 405px;\">\n<div class=\"image-view\" data-width=\"1884\" data-height=\"1090\"><img decoding=\"async\" src=\"https:\/\/newstrong.xyz\/wp-content\/uploads\/2024\/09\/frc-fcfd7f368b1998096aaddebf8310d035.png\"><\/div>\n<\/div>\n<div class=\"image-caption\">image.png<\/div>\n<\/div>\n<p>\u7528\u6237\u8fdb\u7a0b\u90e8\u5206\u5206\u6bb5\u5b58\u50a8\u5185\u5bb9\u5982\u4e0b\u8868\u6240\u793a(\u6309\u5730\u5740\u9012\u51cf\u987a\u5e8f)\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u540d\u79f0<\/th>\n<th>\u5b58\u50a8\u5185\u5bb9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6808<\/td>\n<td>\u5c40\u90e8\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u3001\u8fd4\u56de\u5730\u5740\u7b49<\/td>\n<\/tr>\n<tr>\n<td>\u5806<\/td>\n<td>\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58<\/td>\n<\/tr>\n<tr>\n<td>BSS\u6bb5<\/td>\n<td>\u672a\u521d\u59cb\u5316\u6216\u521d\u503c\u4e3a0\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u5c40\u90e8\u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u6bb5<\/td>\n<td>\u5df2\u521d\u59cb\u5316\u4e14\u521d\u503c\u975e0\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u5c40\u90e8\u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u4ee3\u7801\u6bb5<\/td>\n<td>\u6267\u884c\u4ee3\u7801\u3001\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u3001\u53ea\u8bfb\u53d8\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4efb\u4f55\u4e00\u4e2a\u7a0b\u5e8f\u901a\u5e38\u90fd\u5305\u62ec\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\uff0c\u8fd9\u4e9b\u4ee3\u7801\u548c\u6570\u636e\u672c\u8eab\u90fd\u662f\u9759\u6001\u7684\u3002\u7a0b\u5e8f\u8981\u60f3\u8fd0\u884c\uff0c\u9996\u5148\u8981\u7531\u64cd\u4f5c\u7cfb\u7edf\u8d1f\u8d23\u4e3a\u5176\u521b\u5efa\u8fdb\u7a0b\uff0c\u5e76\u5728\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u4e3a\u5176\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\u5efa\u7acb\u6620\u5c04\u3002\u5149\u6709\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\u662f\u4e0d\u591f\u7684\uff0c\u8fdb\u7a0b\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fd8\u8981\u6709\u5176\u52a8\u6001\u73af\u5883\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u5c31\u662f\u5806\u6808\u3002<\/p>\n<p>\u4e0a\u56fe\u4e2dRandom stack offset\u548cRandom mmap offset\u7b49\u968f\u673a\u503c\u610f\u5728\u9632\u6b62\u6076\u610f\u7a0b\u5e8f\u3002Linux\u901a\u8fc7\u5bf9\u6808\u3001\u5185\u5b58\u6620\u5c04\u6bb5\u3001\u5806\u7684\u8d77\u59cb\u5730\u5740\u52a0\u4e0a\u968f\u673a\u504f\u79fb\u91cf\u6765\u6253\u4e71\u5e03\u5c40\uff0c\u4ee5\u514d\u6076\u610f\u7a0b\u5e8f\u901a\u8fc7\u8ba1\u7b97\u8bbf\u95ee\u6808\u3001\u5e93\u51fd\u6570\u7b49\u5730\u5740\u3002<\/p>\n<p>\u6808(stack)\uff0c\u4f5c\u4e3a\u8fdb\u7a0b\u7684\u4e34\u65f6\u6570\u636e\u533a,\u589e\u957f\u65b9\u5411\u662f\u4ece\u9ad8\u5730\u5740\u5230\u4f4e\u5730\u5740\u3002<\/p>\n<h5>(2) \/proc\/self\/maps: \u67e5\u770b\u5404\u6a21\u5757\u52a0\u8f7d\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u8303\u56f4(\u7edd\u5bf9\u5730\u5740\u8303\u56f4)<\/h5>\n<p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\/proc\/self\/maps\u4fdd\u5b58\u4e86\u5404\u4e2a\u7a0b\u5e8f\u6bb5\u5728\u5185\u5b58\u4e2d\u7684\u52a0\u8f7d\u5730\u5740\u8303\u56f4\uff0cgrep\u51fa\u5171\u4eab\u5e93\u7684\u540d\u5b57\uff0c\u5c31\u53ef\u4ee5\u77e5\u9053\u5171\u4eab\u5e93\u7684\u52a0\u8f7d\u57fa\u503c\u662f\u591a\u5c11\u3002<\/p>\n<pre><code>feifeideMacBook-Pro:s1 feifei$ adb shell ps | grep com.sogou.translate.example\nu0_a83       24460  2977 4455820 111148 0                   0 S com.sogou.translate.example\nu0_a83       24478  2977 4362888  71852 0                   0 S com.sogou.translate.example:logservice\n<\/code><\/pre>\n<pre><code>\u67e5\u770b24460 com.sogou.translate.example \u8fdb\u7a0b\u5bf9\u5e94\u7684map\u6587\u4ef6\nadb shell cat \/proc\/24460\/maps\n\n6fb7751000-6fb77e6000 r-xp 00000000 103:19 61608                         \/data\/app\/com.sogou.translate.example-xMTUqjEw8dDDCbBi01nLgg==\/lib\/arm64\/libbreakpad-core.so\n6fb77e6000-6fb77f6000 ---p 00000000 00:00 0 \n6fb77f6000-6fb77fb000 r--p 00095000 103:19 61608                         \/data\/app\/com.sogou.translate.example-xMTUqjEw8dDDCbBi01nLgg==\/lib\/arm64\/libbreakpad-core.so\n6fb77fb000-6fb77fc000 rw-p 0009a000 103:19 61608                         \/data\/app\/com.sogou.translate.example-xMTUqjEw8dDDCbBi01nLgg==\/lib\/arm64\/libbreakpad-core.so\n6fb77fc000-6fb77fd000 rw-p 00000000 00:00 0                              [anon:.bss]\n6fb7800000-6fb7a00000 rw-p 00000000 00:00 0                              [anon:libc_malloc]\n6fb7a45000-6fb7a47000 r-xp 00000000 103:14 1811                          \/system\/lib64\/vndk-sp\/libion.so\n6fb7a47000-6fb7a48000 r--p 00001000 103:14 1811                          \/system\/lib64\/vndk-sp\/libion.so\n6fb7a48000-6fb7a49000 rw-p 00002000 103:14 1811                          \/system\/lib64\/vndk-sp\/libion.so\n6fb7ab5000-6fb7ab6000 ---p 00000000 00:00 0                              [anon:thread stack guard page]\n<\/code><\/pre>\n<p>\u53ef\u89c1libbreakpad-core.so\u7684\u5730\u5740\u8303\u56f4\u662f6fb7751000-6fb77e6000,\u57fa\u5730\u5740\u4e3a6fb7751000<\/p>\n<h5>(3) \u5229\u7528readelf\u67e5\u770b\u5171\u4eab\u5e93\u7684\u7b26\u53f7\u8868<\/h5>\n<h6>\u5b89\u88c5<\/h6>\n<p>\u5728linux\u4e0b\uff0c\u7528readelf\u6765\u770bELF\u5934\u90e8\u6216\u8005\u5176\u5b83\u5404section\u7684\u5185\u5bb9\uff0c\u7528objdump\u6765\u5bf9\u6307\u5b9a\u7684\u5185\u5bb9\uff08.text, .data\u7b49\uff09\u8fdb\u884c\u53cd\u6c47\u7f16\u3002<\/p>\n<p>\u4f46\u662fmac os X\u4e0b\u6ca1\u6709\u8fd9\u4e24\u4e2a\u547d\u4ee4\uff0c\u53ef\u4ee5\u7528brew\u6765\u5b89\u88c5<\/p>\n<pre><code>brew update &amp;&amp; brew install binutils\n<\/code><\/pre>\n<p>\u5c06libnutils \u5b89\u88c5\u8def\u5f84\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf<\/p>\n<pre><code>vim ~\/.bash_profile\n\n\u6dfb\u52a0\u4e00\u884c\uff1a\nPATH=${PATH}:\/usr\/local\/Cellar\/binutils\/2.33.1\/bin\n\n\u7136\u540e\u6267\u884c\uff1a\nsource ~\/.bash_profile\n<\/code><\/pre>\n<h5>readelf -s \u67e5\u770bso\u5e93\u7684\u7b26\u53f7\u8868<\/h5>\n<p>\u53c2\u6570 -s,symbols \u663e\u793a\u7b26\u53f7\u8868\u6bb5\u4e2d\u7684\u9879\uff08\u5982\u679c\u6709\u6570\u636e\u7684\u8bdd\uff09<\/p>\n<pre><code> readelf -s libbreakpad-core.so \n\n<\/code><\/pre>\n<p>\u90e8\u5206\u7ed3\u679c\u5982\u4e0b:<\/p>\n<pre><code> 354: 000000000007d794     3 OBJECT  GLOBAL DEFAULT   12 _ZTSDu\n   355: 000000000004a180   176 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk116allocator_t\n   356: 00000000000298a0   812 FUNC    GLOBAL DEFAULT   11 _Z7tryDumpv\n   357: 00000000000385e4    88 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk19allocatorINS\n   358: 0000000000041920    52 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk116allocator_t\n   359: 00000000000a5940    24 OBJECT  GLOBAL DEFAULT   18 _ZTIN10__cxxabiv116__shim\n   360: 000000000002a89c    44 FUNC    GLOBAL DEFAULT   11 _Z23call_dangerous_functi\n   361: 0000000000061f78    84 FUNC    GLOBAL DEFAULT   11 _ZNSt12length_errorD2Ev\n   362: 0000000000038f88   316 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk112basic_strin\n   363: 000000000004f6f8   704 FUNC    WEAK   DEFAULT   11 _ZN15google_breakpad6CpuS\n   364: 0000000000045500    52 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk116allocator_t\n   365: 0000000000045b2c   140 FUNC    WEAK   DEFAULT   11 _ZNKSt6__ndk16vectorIPN15\n   366: 0000000000057890    32 FUNC    WEAK   DEFAULT   11 _ZN15google_breakpad9GetO\n   367: 00000000000367cc   132 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk117__compresse\n   368: 0000000000049290   168 FUNC    GLOBAL DEFAULT   11 _ZN15google_breakpad13Wri\n   369: 000000000003e018   180 FUNC    WEAK   DEFAULT   11 _ZN15google_breakpad15was\n   370: 0000000000047ca0    96 FUNC    WEAK   DEFAULT   11 _ZNSt6__ndk116allocator_t\n   371: 00000000000aa048     1 OBJECT  GLOBAL DEFAULT   22 global_interruptSystemCra\n<\/code><\/pre>\n<p>dangerous_function \u548c<br \/>\nglobal_interruptSystemCrash \u90fd\u662fso\u5e93\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u770b\u5230\u8fd9\u4e24\u4e2a\u51fd\u6570\u7b26\u53f7\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684\u504f\u79fb\u91cf\u3002<\/p>\n<h2>\u56db \u6f14\u793ademo \u4ee3\u7801<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2amac \u6216linux\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684sigaction()\u7684\u5c0fdemo\uff0c\u7528\u6765\u719f\u6089sinal\u5904\u7406\u51fd\u6570\u7684\u7279\u6027\u3002<\/p>\n<p>TestSignalPosix.c<\/p>\n<pre><code>\/\/\n\/\/ Created by \u98de\u98de on 2020-01-22.\n\/\/\n\n#include <stdio.h>\n#include <signal.h>\n#include \n#include <stdlib.h>\n#include <err.h>\n#include <execinfo.h>\n#include <string.h>\n\nint  divide_by_zero();\nvoid cause_segfault();\nvoid stack_overflow();\nvoid infinite_loop();\nvoid illegal_instruction();\nvoid cause_calamity();\n\nvoid set_signal_handler_4_posix();\nvoid posix_signal_handler(int sig, siginfo_t *siginfo, void *context);\n\nstatic char const * icky_global_program_name;\nint addr2line(char const * const program_name, void const * const addr);\nvoid posix_print_stack_trace();\n\nconst int kExceptionSignals[] = {\n        SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, SIGTRAP\n};\n\n\/\/--- \u4fe1\u53f7\u91cf\u4e2a\u6570\nconst int kNumHandledSignals =\n        sizeof(kExceptionSignals) \/ sizeof(kExceptionSignals[0]);\n\n\/\/--- \u6bcf\u4e2a\u4fe1\u53f7\u7684\u539f\u6709\u4fe1\u53f7\u5904\u7406\u51fd\u6570\nstruct sigaction old_handlers[kNumHandledSignals];\n\nint main(int argc, char * argv[])\n{\n    (void)argc;\n\n    \/* store off program path so we can use it later *\/\n    icky_global_program_name = argv[0];\n    char* exceptionType = argv[1];\n\n    set_signal_handler_4_posix();\n\n    cause_calamity(exceptionType);\n\n    printf(\"OMG! Nothing bad happend!\");\n    return 0;\n}\n\nvoid cause_calamity(char* type)\n{\n    \/* uncomment one of the following error conditions to cause a calamity of\n     your choosing! *\/\n\n    if(strcmp(type,\"1\") == 0){\n        fputs(\"cause_calamity type = 1 ,create a  divide_by_zero error n\", stderr);\n        (void)divide_by_zero();\n    } else if(strcmp(type,\"2\") == 0){\n        fputs(\"cause_calamity type = 2 ,create a  cause_segfault error n\", stderr);\n         cause_segfault();\n    } else if(strcmp(type,\"3\") == 0){\n        fputs(\"cause_calamity type = 3 ,create a assert(false)  errorn\", stderr);\n\/\/         assert(false);\n    } else if(strcmp(type,\"4\") == 0){\n        fputs(\"cause_calamity type = 4 ,,create a infinite_loop n\", stderr);\n        infinite_loop();\n    } else if(strcmp(type,\"5\") == 0){\n        fputs(\"cause_calamity type = 5 ,,create a illegal_instruction n\", stderr);\n        illegal_instruction();\n    } else if(strcmp(type,\"6\") == 0){\n        fputs(\"cause_calamity type = 6 ,,create a stack_overflown\", stderr);\n        stack_overflow();\n    }\n\n    \/\/ infinite_loop();\n    \/\/ illegal_instruction();\n    \/\/ stack_overflow();\n}\n\nint divide_by_zero()\n{\n    int a = 1;\n    int b = 0;\n    return a \/ b;\n}\n\nvoid cause_segfault()\n{\n    int * p = (int*)0x12345678;\n    *p = 0;\n}\n\nvoid stack_overflow();\nvoid stack_overflow()\n{\n    int foo[1000]; \/\/allocate something big on the stack\n    (void)foo;\n    stack_overflow();\n}\n\n\/* break out with ctrl+c to test SIGINT handling *\/\nvoid infinite_loop()\n{\n    while(1) {};\n}\n\nvoid illegal_instruction()\n{\n    \/* I couldn't find an easy way to cause this one, so I'm cheating *\/\n    raise(SIGILL);\n}\n\nvoid posix_signal_handler(int sig, siginfo_t *siginfo, void *context)\n{\n\n    fputs(\"nn\",stderr);\n\n    printf(\"received signal no:%d,code:%dn\",sig,siginfo-&gt;si_code);\n\n    fputs(\"nn\",stderr);\n    (void)context;\n    switch(sig)\n    {\n        case SIGSEGV:\n            fputs(\"Caught SIGSEGV: Segmentation Faultn\", stderr);\n            break;\n        case SIGINT:\n            fputs(\"Caught SIGINT: Interactive attention signal, (usually ctrl+c)n\",\n                  stderr);\n            break;\n        case SIGFPE:\n            switch(siginfo-&gt;si_code)\n            {\n                case FPE_INTDIV:\n                    fputs(\"Caught SIGFPE: (integer divide by zero)n\", stderr);\n                    break;\n                case FPE_INTOVF:\n                    fputs(\"Caught SIGFPE: (integer overflow)n\", stderr);\n                    break;\n                case FPE_FLTDIV:\n                    fputs(\"Caught SIGFPE: (floating-point divide by zero)n\", stderr);\n                    break;\n                case FPE_FLTOVF:\n                    fputs(\"Caught SIGFPE: (floating-point overflow)n\", stderr);\n                    break;\n                case FPE_FLTUND:\n                    fputs(\"Caught SIGFPE: (floating-point underflow)n\", stderr);\n                    break;\n                case FPE_FLTRES:\n                    fputs(\"Caught SIGFPE: (floating-point inexact result)n\", stderr);\n                    break;\n                case FPE_FLTINV:\n                    fputs(\"Caught SIGFPE: (floating-point invalid operation)n\", stderr);\n                    break;\n                case FPE_FLTSUB:\n                    fputs(\"Caught SIGFPE: (subscript out of range)n\", stderr);\n                    break;\n                default:\n                    fputs(\"Caught SIGFPE: Arithmetic Exceptionn\", stderr);\n                    break;\n            }\n        case SIGILL:\n            switch(siginfo-&gt;si_code)\n            {\n                case ILL_ILLOPC:\n                    fputs(\"Caught SIGILL: (illegal opcode)n\", stderr);\n                    break;\n                case ILL_ILLOPN:\n                    fputs(\"Caught SIGILL: (illegal operand)n\", stderr);\n                    break;\n                case ILL_ILLADR:\n                    fputs(\"Caught SIGILL: (illegal addressing mode)n\", stderr);\n                    break;\n                case ILL_ILLTRP:\n                    fputs(\"Caught SIGILL: (illegal trap)n\", stderr);\n                    break;\n                case ILL_PRVOPC:\n                    fputs(\"Caught SIGILL: (privileged opcode)n\", stderr);\n                    break;\n                case ILL_PRVREG:\n                    fputs(\"Caught SIGILL: (privileged register)n\", stderr);\n                    break;\n                case ILL_COPROC:\n                    fputs(\"Caught SIGILL: (coprocessor error)n\", stderr);\n                    break;\n                case ILL_BADSTK:\n                    fputs(\"Caught SIGILL: (internal stack error)n\", stderr);\n                    break;\n                default:\n                    fputs(\"Caught SIGILL: Illegal Instructionn\", stderr);\n                    break;\n            }\n            break;\n        case SIGTERM:\n            fputs(\"Caught SIGTERM: a termination request was sent to the programn\",\n                  stderr);\n            break;\n        case SIGABRT:\n            fputs(\"Caught SIGABRT: usually caused by an abort() or assert()n\", stderr);\n            break;\n        default:\n            break;\n    }\n    posix_print_stack_trace();\n\n    fputs(\"nn\",stderr);\n\n    _Exit(1);\n\n}\n\nstatic uint8_t alternate_stack[SIGSTKSZ];\nvoid set_signal_handler_4_posix()\n{\n    \/* setup alternate stack *\/\n    {\n        stack_t ss = {};\n        \/* malloc is usually used here, I'm not 100% sure my static allocation\n           is valid but it seems to work just fine. *\/\n        ss.ss_sp = (void*)alternate_stack;\n        ss.ss_size = SIGSTKSZ;\n        ss.ss_flags = 0;\n\n        if (sigaltstack(&amp;ss, NULL) != 0) { err(1, \"sigaltstack\"); }\n    }\n\n    \/* register our signal handlers *\/\n    {\n        struct sigaction sig_action = {};\n        sig_action.sa_sigaction = posix_signal_handler;\n        sigemptyset(&amp;sig_action.sa_mask);\n\n#ifdef __APPLE__\n        \/* for some reason we backtrace() doesn't work on osx\n           when we use an alternate stack *\/\n        sig_action.sa_flags = SA_SIGINFO;\n#else\n        sig_action.sa_flags = SA_SIGINFO | SA_ONSTACK;\n#endif\n\n        for(int i = 0;i<knumhandledsignals printf sigaction for signal if sinalaction failed return max_stack_frames static void posix_print_stack_trace fputs int i trace_size=\"0;\" char messages=\"backtrace_symbols(stack_traces,\" skip the first couple stack frames they are this function and our handler also last frame as it junk. we use now so you can see what going on stack_traces error determining line for: free resolve symbol name source location given path to executable an address addr2line const program_name addr addr2line_cmd have map relent in code __apple__ apple does things differently... sprintf will print a nicely formatted string specifying of system><\/knumhandledsignals><\/string.h><\/execinfo.h><\/err.h><\/stdlib.h><\/signal.h><\/stdio.h><\/code><\/pre>\n<p>\u7f16\u8bd1\u6307\u4ee4:<\/p>\n<pre><code>mac\u4e0a:\ngcc -lpthread  -g -fno-pie TestSignalPosix.c -o TestSignalPosix\n\nlinux\u4e0a:\ngcc -lpthread  -g  TestSignalPosix.c -o TestSignalPosix\n\n<\/code><\/pre>\n<p>gcc \u53c2\u6570<\/p>\n<ul>\n<li>-l \u6307\u5b9agcc \u9700\u8981\u52a0\u8f7d\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u3002<\/li>\n<\/ul>\n<ul>\n<li>-g \u4e3agdb\u8c03\u8bd5\u7528\uff0c\u4f1a\u505a\u4e24\u4ef6\u4e8b\u60c5:\u521b\u5efa\u7b26\u53f7\u8868\uff0c\u7b26\u53f7\u8868\u5305\u542b\u4e86\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u540d\u79f0\u7684\u5217\u8868;\u5173\u95ed\u6240\u6709\u7684\u4f18\u5316\u673a\u5236\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u4e25\u683c\u6309\u7167\u539f\u6765\u7684C\u4ee3\u7801\u8fdb\u884c\u3002<\/li>\n<\/ul>\n<p>\u6d4b\u8bd5\u7ed3\u679c:<\/p>\n<pre><code>feifeideMacBook-Pro:cpp feifei$ .\/TestSignalPosix 2\ninstall sigaction for signal  11\ninstall sigaction for signal  6\ninstall sigaction for signal  8\ninstall sigaction for signal  4\ninstall sigaction for signal  10\ninstall sigaction for signal  5\ncause_calamity type = 2 ,create a  cause_segfault error \n\nreceived signal no:11,code:1\n\nCaught SIGSEGV: Segmentation Fault\n\nposix_print_stack_trace (in TestSignalPosix) (TestSignalPosix.c:271)\nposix_signal_handler (in TestSignalPosix) (TestSignalPosix.c:217)\n0x7fff7024cb5d\n0x0000ffff (in TestSignalPosix)\ncause_calamity (in TestSignalPosix) (TestSignalPosix.c:67)\nmain (in TestSignalPosix) (TestSignalPosix.c:52)\n0x7fff700673d5\n\n<\/code><\/pre>\n<h2>\u4e94\u3001 \u53c2\u8003\u6587\u732e:<\/h2>\n<p>https:\/\/www.jianshu.com\/p\/78a363ea48df<\/p>\n<p>sigaction demo:https:\/\/spin.atomicobject.com\/2013\/01\/13\/exceptions-stack-traces-c\/<\/p>\n<p>\u4fe1\u53f7\u5b9a\u4e49\u548c\u884c\u4e3a:https:\/\/blog.csdn.net\/yeyuwuhen1203\/article\/details\/78031391<\/p>\n<p>\u4fe1\u53f7\u5904\u7406\u51fd\u6570:https:\/\/blog.csdn.net\/weibo1230123\/article\/details\/81411827<\/p>\n<p>natvie crash \u6355\u83b7:https:\/\/blog.csdn.net\/mba16c35\/article\/details\/54178067<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4fe1\u53f7\u673a\u5236 image.png \u51fd\u6570\u8fd0\u884c\u5728\u7528\u6237\u6001,\u5f53\u9047\u5230\u7cfb\u7edf\u8c03\u7528\u3001\u4e2d\u65ad\u6216\u662f\u5f02\u5e38\u7684\u60c5\u51b5\u65f6,\u7a0b\u5e8f\u4f1a\u8fdb\u5165\u5185\u6838\u6001\u3002\u4fe1&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":0,"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/media\/159"}],"wp:attachment":[{"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/newstrong.top\/index.php\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}