{"id":207242,"date":"2025-07-08T11:06:23","date_gmt":"2025-07-08T03:06:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207242\/"},"modified":"2025-07-08T11:06:23","modified_gmt":"2025-07-08T03:06:23","slug":"ptracing-%e9%95%bf%e6%97%b6%e9%97%b4%e8%bf%90%e8%a1%8c%e7%9a%84%e8%bf%9b%e7%a8%8b%e6%8c%82%e8%b5%b7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207242\/","title":{"rendered":"ptracing \u957f\u65f6\u95f4\u8fd0\u884c\u7684\u8fdb\u7a0b\u6302\u8d77"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>ptracing \u957f\u65f6\u95f4\u8fd0\u884c\u7684\u8fdb\u7a0b\u6302\u8d77<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 08:18:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aptracing \u957f\u65f6\u95f4\u8fd0\u884c\u7684\u8fdb\u7a0b\u6302\u8d77\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 go \u7684\u7cfb\u7edf\u8c03\u7528\u5305 ptrace \u63a5\u53e3\u6765\u8ddf\u8e2a\u8fdb\u7a0b\u3002\u95ee\u9898\u662f\uff0c\u5982\u679c\u88ab\u8ddf\u8e2a\u8005\u957f\u65f6\u95f4\u8fd0\u884c\uff0c\u8ddf\u8e2a\u4f3c\u4e4e\u4f1a\u6302\u8d77\u3002\u6211\u5c1d\u8bd5\u7528 c \u5b9e\u73b0\u6765\u590d\u5236\u8be5\u95ee\u9898\uff0c\u4f46\u4e00\u5207\u4f3c\u4e4e\u90fd\u5de5\u4f5c\u6b63\u5e38\u3002<\/p>\n<p>\u8fd9\u662f\u91cd\u73b0\u8be5\u95ee\u9898\u7684 go \u4ee3\u7801\uff1a<\/p>\n<pre>import (\n    \"fmt\"\n    \"os\"\n    \"os\/exec\"\n    \"syscall\"\n)\n\nfunc main() {\n    len := \"9999999\"\n    cmd := exec.command(\"openssl\", \"rand\", \"-hex\", len)\n    cmd.sysprocattr = &amp;syscall.sysprocattr{ptrace: true}\n    cmd.stdout = os.stdout\n    cmd.stdin = os.stdin\n    cmd.start()\n    pid, _ := syscall.wait4(-1, nil, syscall.wall, nil)\n\n    for {\n        syscall.ptracesyscall(pid, 0)\n        _, err := syscall.wait4(-1, nil, syscall.wall, nil) \n\n        if err != nil {\n            fmt.println(err)\n            break\n        }\n    }\n}<\/pre>\n<p>\u5f53\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u8be5\u8fc7\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u5b8c\u6210\uff0c\u5e76\u4e14\u5fc5\u987b\u88ab\u4e2d\u65ad\u3002\u5982\u679c\u5c06 <code>len<\/code> \u53d8\u91cf\u66f4\u6539\u4e3a\u8f83\u5c0f\u7684\u503c\uff0c\u4f8b\u5982 <code>9<\/code>\uff0c\u5219\u8be5\u8fc7\u7a0b\u5c06\u987a\u5229\u5b8c\u6210\uff0c\u5e76\u4e14\u8f93\u51fa\u5c06\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>$ go run main.go\nd2ff963e65e8e1926b\nno child processes<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u627e\u5230\u4e86\u3002\u5f53 go \u8fd0\u884c\u65f6\u66f4\u6539 goroutine \u8fd0\u884c\u7684\u7ebf\u7a0b\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6302\u8d77\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u5faa\u73af\u5185\u6253\u5370 <code>fmt.println(syscall.gettid())<\/code> \u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\u8fdb\u884c\u9a8c\u8bc1\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"os\/exec\"\n    \"syscall\"\n)\n\nfunc main() {\n    len := \"9999999\"\n    cmd := exec.command(\"openssl\", \"rand\", \"-hex\", len)\n    cmd.sysprocattr = &amp;syscall.sysprocattr{ptrace: true}\n    cmd.start()\n\n    pid, _ := syscall.wait4(-1, nil, syscall.wall, nil)\n\n    for {\n        fmt.println(syscall.gettid())\n        syscall.ptracesyscall(pid, 0)\n        _, err := syscall.wait4(-1, nil, syscall.wall, nil)\n\n        if err != nil {\n            fmt.println(err)\n            break\n        }\n    }\n}<\/pre>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528<code>runtime.lockosthread()<\/code>\u5c06goroutine\u7684\u6267\u884c\u9501\u5b9a\u5230\u5f53\u524d\u7ebf\u7a0b\uff1a<\/p>\n<pre>....\nfunc main() {\n    runtime.LockOSThread()\n    len := \"9999999\"\n    cmd := exec.Command(\"openssl\", \"rand\", \"-hex\", len)\n....<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207242","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207242"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207242\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}