﻿{"id":1501,"date":"2017-02-28T14:41:16","date_gmt":"2017-02-28T06:41:16","guid":{"rendered":"http:\/\/nick.txtcc.com\/?p=1501"},"modified":"2017-02-28T15:39:58","modified_gmt":"2017-02-28T07:39:58","slug":"%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e7%94%a8strace%e8%af%8a%e6%96%ad%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/linux\/1501","title":{"rendered":"\u624b\u628a\u624b\u6559\u4f60\u7528Strace\u8bca\u65ad\u95ee\u9898"},"content":{"rendered":"<p>\u65e9\u4e9b\u5e74\uff0c\u5982\u679c\u4f60\u77e5\u9053\u6709\u4e2a strace \u547d\u4ee4\uff0c\u5c31\u5f88\u725b\u4e86\uff0c\u800c\u73b0\u5728\u5927\u5bb6\u57fa\u672c\u90fd\u77e5\u9053 strace \u4e86\uff0c\u5982\u679c\u4f60\u9047\u5230\u6027\u80fd\u95ee\u9898\u6c42\u52a9\u522b\u4eba\uff0c\u5341\u6709\u516b\u4e5d\u4f1a\u5efa\u8bae\u4f60\u7528 strace \u6302\u4e0a\u53bb\u770b\u770b\uff0c\u4e0d\u8fc7\u5f53\u4f60\u6302\u4e0a\u53bb\u4e86\uff0c\u770b\u7740\u6ee1\u5c4f\u7ffb\u6eda\u7684\u5b57\u7b26\uff0c\u5374\u5341\u6709\u516b\u4e5d\u770b\u4e0d\u51fa\u4e2a\u6240\u4ee5\u7136\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u6848\u4f8b\uff0c\u5411\u4f60\u5c55\u793a\u4e00\u4e0b\u5728\u7528 strace \u8bca\u65ad\u95ee\u9898\u65f6\u7684\u4e00\u4e9b\u5957\u8def\u3002<\/p>\n<p>strace\u662f<a title=\"linux\" href=\"http:\/\/www.ttlsa.com\/linux\/\" target=\"_blank\">Linux<\/a>\u73af\u5883\u4e0b\u7684\u4e00\u6b3e\u7a0b\u5e8f\u8c03\u8bd5\u5de5\u5177\uff0c\u7528\u6765\u76d1\u5bdf\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6240\u4f7f\u7528\u7684\u7cfb\u7edf\u8c03\u7528\u53ca\u5b83\u6240\u63a5\u6536\u7684\u7cfb\u7edf\u4fe1\u606f\u3002\u8ffd\u8e2a\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\uff0c\u8f93\u51fa\u6bcf\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u540d\u5b57\uff0c\u53c2\u6570\uff0c\u8fd4\u56de\u503c\u548c\u6267\u884c\u6d88\u8017\u7684\u65f6\u95f4\u7b49\u3002\nstrace\u5e38\u7528\u53c2\u6570\uff1a\n-p \u8ddf\u8e2a\u6307\u5b9a\u7684\u8fdb\u7a0b\n-f \u8ddf\u8e2a\u7531fork\u5b50\u8fdb\u7a0b\u7cfb\u7edf\u8c03\u7528\n-F \u5c1d\u8bd5\u8ddf\u8e2avfork\u5b50\u8fdb\u7a0b\u7cfb\u7edf\u8c03\u5438\u5165\uff0c\u4e0e-f\u540c\u65f6\u51fa\u73b0\u65f6, vfork\u4e0d\u88ab\u8ddf\u8e2a\n-o filename \u9ed8\u8ba4strace\u5c06\u7ed3\u679c\u8f93\u51fa\u5230stdout\u3002\u901a\u8fc7-o\u53ef\u4ee5\u5c06\u8f93\u51fa\u5199\u5165\u5230filename\u6587\u4ef6\u4e2d\n-ff \u5e38\u4e0e-o\u9009\u9879\u4e00\u8d77\u4f7f\u7528\uff0c\u4e0d\u540c\u8fdb\u7a0b(\u5b50\u8fdb\u7a0b)\u4ea7\u751f\u7684\u7cfb\u7edf\u8c03\u7528\u8f93\u51fa\u5230filename.PID\u6587\u4ef6\n-r \u6253\u5370\u6bcf\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u76f8\u5bf9\u65f6\u95f4\n-t \u5728\u8f93\u51fa\u4e2d\u7684\u6bcf\u4e00\u884c\u524d\u52a0\u4e0a\u65f6\u95f4\u4fe1\u606f\u3002 -tt \u65f6\u95f4\u786e\u5b9a\u5230\u5fae\u79d2\u7ea7\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528-ttt\u6253\u5370\u76f8\u5bf9\u65f6\u95f4\n-v \u8f93\u51fa\u6240\u6709\u7cfb\u7edf\u8c03\u7528\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e9b\u9891\u7e41\u8c03\u7528\u7684\u7cfb\u7edf\u8c03\u7528\u4e0d\u4f1a\u8f93\u51fa\n-s \u6307\u5b9a\u6bcf\u4e00\u884c\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6,\u9ed8\u8ba4\u662f32\u3002\u6587\u4ef6\u540d\u4e00\u76f4\u5168\u90e8\u8f93\u51fa\n-c \u7edf\u8ba1\u6bcf\u79cd\u7cfb\u7edf\u8c03\u7528\u6240\u6267\u884c\u7684\u65f6\u95f4\uff0c\u8c03\u7528\u6b21\u6570\uff0c\u51fa\u9519\u6b21\u6570\u3002\n-e expr \u8f93\u51fa\u8fc7\u6ee4\u5668\uff0c\u901a\u8fc7\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u51fa\u6389\u4f60\u4e0d\u60f3\u8981\u8f93\u51fa<\/p>\n<p>\u5982\u4e0b\u771f\u5b9e\u6848\u4f8b\uff0c\u5982\u6709\u96f7\u540c\uff0c\u5b9e\u5c5e\u5fc5\u7136\uff01\u8ba9\u6211\u4eec\u770b\u4e00\u53f0\u9ad8\u8d1f\u8f7d\u670d\u52a1\u5668\u7684 top \u7ed3\u679c\uff1a<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2017\/02\/064118b9h.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-475\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2017\/02\/064118b9h.jpg\" alt=\"top\" width=\"682\" height=\"583\" \/><\/a><\/p>\n<p>\u6280\u5de7\uff1a\u8fd0\u884c top \u65f6\uff0c\u6309\u300c1\u300d\u6253\u5f00 CPU \u5217\u8868\uff0c\u6309\u300cshift+p\u300d\u4ee5 CPU \u6392\u5e8f\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\u5927\u5bb6\u5f88\u5bb9\u6613\u53d1\u73b0 CPU \u4e3b\u8981\u662f\u88ab\u82e5\u5e72\u4e2a PHP \u8fdb\u7a0b\u5360\u7528\u4e86\uff0c\u540c\u65f6 PHP \u8fdb\u7a0b\u5360\u7528\u7684\u6bd4\u8f83\u591a\u7684\u5185\u5b58\uff0c\u4e0d\u8fc7\u7cfb\u7edf\u5185\u5b58\u5c1a\u6709\u7ed3\u4f59\uff0cSWAP \u4e5f\u4e0d\u4e25\u91cd\uff0c\u8fd9\u5e76\u4e0d\u662f\u95ee\u9898\u4e3b\u56e0\u3002<\/p>\n<p>\u4e0d\u8fc7\u5728 CPU \u5217\u8868\u4e2d\u80fd\u770b\u5230 CPU \u4e3b\u8981\u6d88\u8017\u5728\u5185\u6838\u6001\u300csy\u300d\uff0c\u800c\u4e0d\u662f\u7528\u6237\u6001\u300cus\u300d\uff0c\u548c\u6211\u4eec\u7684\u7ecf\u9a8c\u4e0d\u7b26\u3002Linux \u64cd\u4f5c\u7cfb\u7edf\u6709\u5f88\u591a\u7528\u6765\u8ddf\u8e2a\u7a0b\u5e8f\u884c\u4e3a\u7684\u5de5\u5177\uff0c\u5185\u6838\u6001\u7684\u51fd\u6570\u8c03\u7528\u8ddf\u8e2a\u7528\u300cstrace\u300d\uff0c\u7528\u6237\u6001\u7684\u51fd\u6570\u8c03\u7528\u8ddf\u8e2a\u7528\u300cltrace\u300d\uff0c\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u5e94\u8be5\u7528\u300cstrace\u300d\uff1a<\/p>\n<pre>shell> strace -p <PID><\/pre>\n<p>\u4e0d\u8fc7\u5982\u679c\u76f4\u63a5\u7528 strace \u8ddf\u8e2a\u67d0\u4e2a\u8fdb\u7a0b\u7684\u8bdd\uff0c\u90a3\u4e48\u7b49\u5f85\u4f60\u7684\u5f80\u5f80\u662f\u6ee1\u5c4f\u7ffb\u6eda\u7684\u5b57\u7b26\uff0c\u60f3\u4ece\u8fd9\u91cc\u770b\u51fa\u95ee\u9898\u7684\u75c7\u7ed3\u5e76\u4e0d\u662f\u4e00\u4ef6\u5bb9\u6613\u7684\u4e8b\u60c5\uff0c\u597d\u5728 strace \u00a0\u53ef\u4ee5\u6309\u64cd\u4f5c\u6c47\u603b\u65f6\u95f4\uff1a<\/p>\n<pre>shell> strace -cp <PID><\/pre>\n<p>\u901a\u8fc7\u300cc\u300d\u9009\u9879\u7528\u6765\u6c47\u603b\u5404\u4e2a\u64cd\u4f5c\u7684\u603b\u8017\u65f6\uff0c\u8fd0\u884c\u540e\u7684\u7ed3\u679c\u5927\u6982\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2017\/02\/064119lhO.jpg\"><img decoding=\"async\" class=\" wp-image-476\" style=\"border: 1px solid #eeeeee; max-width: calc(100% - 14px); height: 476px; width: 849px; display: block; padding: 6px;\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2017\/02\/064119lhO.jpg\" alt=\"strace -cp <PID>&#8221; width=&#8221;1080&#8243; height=&#8221;600&#8243; \/><\/a><\/p>\n<p>\u5f88\u660e\u663e\uff0c\u6211\u4eec\u80fd\u770b\u5230 CPU \u4e3b\u8981\u88ab clone \u64cd\u4f5c\u6d88\u8017\u4e86\uff0c\u8fd8\u53ef\u4ee5\u5355\u72ec\u8ddf\u8e2a\u4e00\u4e0b clone\uff1a<\/p>\n<pre>shell> strace -T -e clone -p <PID><\/pre>\n<p>\u901a\u8fc7\u300cT\u300d\u9009\u9879\u53ef\u4ee5\u83b7\u53d6\u64cd\u4f5c\u5b9e\u9645\u6d88\u8017\u7684\u65f6\u95f4\uff0c\u901a\u8fc7\u300ce\u300d\u9009\u9879\u53ef\u4ee5\u8ddf\u8e2a\u67d0\u4e2a\u64cd\u4f5c\uff1a<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2017\/02\/064120pv1.jpg\"><img decoding=\"async\" class=\" wp-image-477\" style=\"border: 1px solid #eeeeee; max-width: calc(100% - 14px); height: 208px; width: 860px; display: block; padding: 6px;\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2017\/02\/064120pv1.jpg\" alt=\"strace -T -e clone -p <PID>&#8221; width=&#8221;1740&#8243; height=&#8221;410&#8243; \/><\/a><\/p>\n<p>\u5f88\u660e\u663e\uff0c\u4e00\u4e2a clone \u64cd\u4f5c\u9700\u8981\u51e0\u767e\u6beb\u79d2\uff0c\u81f3\u4e8e clone \u7684\u542b\u4e49\uff0c\u53c2\u8003 man \u6587\u6863\uff1a<\/p>\n<blockquote>clone() creates a new process, in a manner similar to fork(2). It is actually a library function layered on top of the underlying clone() system call, hereinafter\u00a0referred to as sys_clone. A description of sys_clone is given towards the end of this page.\n\nUnlike fork(2), these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file\u00a0descriptors, and the table of signal handlers. (Note that on this manual page, \u201ccalling process\u201d normally corresponds to \u201cparent process\u201d. But see the description of\u00a0CLONE_PARENT below.)<\/blockquote>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u3002\u90a3\u4e48\u5728 PHP \u91cc\u4ec0\u4e48\u65f6\u5019\u4f1a\u51fa\u73b0\u6b64\u7c7b\u7cfb\u7edf\u8c03\u7528\u5462\uff1f\u67e5\u8be2\u4e1a\u52a1\u4ee3\u7801\u770b\u5230\u4e86\u00a0<a href=\"http:\/\/php.net\/manual\/en\/function.exec.php\" target=\"_blank\">exec<\/a>\u00a0\u51fd\u6570\uff0c\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u9a8c\u8bc1\u5b83\u786e\u5b9e\u4f1a\u5bfc\u81f4 clone \u7cfb\u7edf\u8c03\u7528\uff1a<\/p>\n<pre>shell> strace -eclone php -r 'exec(\"ls\");'<\/pre>\n<p>\u5982\u679c\u60f3\u8981\u8ffd\u8e2a\u591a\u4e2afpm\u8fdb\u7a0b\u53ef\u4ee5\u7528\u4ee5\u4e0b\u811a\u672c:<\/p>\n<p class=\"p1\"><span class=\"s1\">strace -eall -c $(ps auxf|grep -E &#8216;(9013|9014|9015)&#8217;|awk &#8216;{if ($1=&#8221;www-data&#8221;) print &#8220;-p &#8220;$2}&#8217;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>\u6700\u540e\u518d\u8003\u5927\u5bb6\u4e00\u4e2a\u9898\uff1a\u5982\u679c\u6211\u4eec\u7528 strace \u8ddf\u8e2a\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8f93\u51fa\u7ed3\u679c\u5f88\u5c11\uff0c\u662f\u4e0d\u662f\u8bf4\u660e\u8fdb\u7a0b\u5f88\u7a7a\u95f2\uff1f\u5176\u5b9e\u8bd5\u8bd5 ltrace\uff0c\u53ef\u80fd\u4f1a\u53d1\u73b0\u522b\u6709\u6d1e\u5929\u3002\u8bb0\u4f4f\u6709\u5185\u6838\u6001\u548c\u7528\u6237\u6001\u4e4b\u5206\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u65e9\u4e9b\u5e74\uff0c\u5982\u679c\u4f60\u77e5\u9053\u6709\u4e2a strace \u547d\u4ee4\uff0c\u5c31\u5f88\u725b\u4e86\uff0c\u800c\u73b0\u5728\u5927\u5bb6\u57fa\u672c\u90fd\u77e5\u9053 strace \u4e86\uff0c\u5982\u679c\u4f60\u9047\u5230\u6027\u80fd\u95ee\u9898\u6c42\u52a9\u522b\u4eba\uff0c\u5341\u6709\u516b\u4e5d\u4f1a\u5efa\u8bae\u4f60\u7528 strace \u6302\u4e0a\u53bb\u770b\u770b\uff0c\u4e0d\u8fc7\u5f53\u4f60\u6302\u4e0a\u53bb\u4e86\uff0c\u770b\u7740\u6ee1\u5c4f\u7ffb\u6eda\u7684&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1501","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=1501"}],"version-history":[{"count":4,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1501\/revisions"}],"predecessor-version":[{"id":1511,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1501\/revisions\/1511"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=1501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=1501"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=1501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}