\u6267\u884c\u8fdb\u7a0b\u7684\u4ee3\u7801<\/strong><\/div>\n\u6211\u5c06\u7f16\u5199\u4e00\u4e2a\u793a\u4f8b\uff0c\u5b9e\u73b0\u4e00\u4e2a\u5728\u201c\u8ddf\u8e2a\u201d\u6a21\u5f0f\u4e0b\u8fd0\u884c\u7684\u8fdb\u7a0b\u3002\u5728\u8fd9\u4e2a\u6a21\u5f0f\u4e0b\uff0c\u6211\u4eec\u5c06\u5355\u6b65\u6267\u884c\u8fdb\u7a0b\u7684\u4ee3\u7801\uff0c\u5c31\u50cf\u673a\u5668\u7801\uff08\u6c47\u7f16\u4ee3\u7801\uff09\u88ab CPU \u6267\u884c\u65f6\u4e00\u6837\u3002\u6211\u5c06\u5206\u6bb5\u5c55\u793a\u3001\u8bb2\u89e3\u793a\u4f8b\u4ee3\u7801\uff0c\u5728\u6587\u7ae0\u7684\u672b\u5c3e\u4e5f\u6709\u5b8c\u6574 c \u6587\u4ef6\u7684\u4e0b\u8f7d\u94fe\u63a5\uff0c\u4f60\u53ef\u4ee5\u7f16\u8bd1\u3001\u6267\u884c\u6216\u8005\u968f\u5fc3\u6240\u6b32\u7684\u66f4\u6539\u3002<\/p>\n
\u66f4\u8fdb\u4e00\u6b65\u7684\u8ba1\u5212\u662f\u5b9e\u73b0\u4e00\u6bb5\u4ee3\u7801\uff0c\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u521b\u5efa\u53ef\u6267\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u4ee4\u7684\u5b50\u8fdb\u7a0b\uff0c\u540c\u65f6\u7236\u8fdb\u7a0b\u53ef\u4ee5\u8ddf\u8e2a\u5b50\u8fdb\u7a0b\u3002\u9996\u5148\u662f\u4e3b\u51fd\u6570\uff1a<\/p>\n
\r\nint main(int argc, char** argv)\r\n{\r\npid_t child_pid;\r\n\r\nif (argc < 2) { fprintf(stderr, \"Expected a program name as argument\/n\"); return -1; } child_pid = fork(); if (child_pid == 0) run_target(argv[1]); else if (child_pid > 0)\r\nrun_debugger(child_pid);\r\nelse {\r\nperror(\"fork\");\r\nreturn -1;\r\n}\r\n\r\nreturn 0;\r\n}\r\n<\/pre>\n\u770b\u8d77\u6765\u76f8\u5f53\u7684\u7b80\u5355\uff1a\u6211\u4eec\u7528 fork \u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\uff08\u8fd9\u7bc7\u6587\u7ae0\u5047\u5b9a\u8bfb\u8005\u6709\u4e00\u5b9a\u7684 Unix\/Linux \u7f16\u7a0b\u7ecf\u9a8c\u3002\u6211\u5047\u5b9a\u4f60\u77e5\u9053\u6216\u81f3\u5c11\u4e86\u89e3 fork\u3001exec \u65cf\u51fd\u6570\u4e0e Unix \u4fe1\u53f7\uff09\u3002if \u8bed\u53e5\u7684\u5206\u652f\u6267\u884c\u5b50\u8fdb\u7a0b\uff08\u8fd9\u91cc\u79f0\u4e4b\u4e3a \u201ctarget\u201d\uff09\uff0celse if \u7684\u5206\u652f\u6267\u884c\u7236\u8fdb\u7a0b\uff08\u8fd9\u91cc\u79f0\u4e4b\u4e3a \u201cdebugger\u201d\uff09\u3002<\/p>\n
\u4e0b\u9762\u662f target \u8fdb\u7a0b\u7684\u4ee3\u7801\uff1a<\/p>\n
\r\nvoid run_target(const char* programname)\r\n{\r\nprocmsg(\"target started. will run '%s'\/n\", programname);\r\n\r\n\/* Allow tracing of this process *\/\r\nif (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {\r\nperror(\"ptrace\");\r\nreturn;\r\n}\r\n\r\n\/* Replace this process's image with the given program *\/\r\nexecl(programname, programname, 0);\r\n}\r\n<\/pre>\n\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u6700\u503c\u5f97\u6ce8\u610f\u7684\u662f ptrace \u8c03\u7528\u3002\u5728 sys\/ptrace.h \u4e2d\uff0cptrace \u662f\u5982\u4e0b\u5b9a\u4e49\u7684\uff1a<\/p>\n
\r\nlong ptrace(enum __ptrace_request request, pid_t pid,\r\nvoid *addr, void *data);\r\n<\/pre>\n\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f _request_\uff0c\u8fd9\u662f\u8bb8\u591a\u9884\u5b9a\u4e49\u7684 PTRACE_* \u5e38\u91cf\u4e2d\u7684\u4e00\u4e2a\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8bf7\u6c42\u5206\u914d\u8fdb\u7a0b ID\u3002\u7b2c\u4e09\u4e2a\u4e0e\u7b2c\u56db\u4e2a\u53c2\u6570\u662f\u5730\u5740\u4e0e\u6570\u636e\u6307\u9488\uff0c\u7528\u4e8e\u64cd\u4f5c\u5185\u5b58\u3002\u4e0a\u9762\u4ee3\u7801\u6bb5\u4e2d\u7684 ptrace \u8c03\u7528\u53d1\u8d77\u4e86 PTRACE_TRACEME \u8bf7\u6c42\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u5b50\u8fdb\u7a0b\u8bf7\u6c42\u7cfb\u7edf\u5185\u6838\u8ba9\u5176\u7236\u8fdb\u7a0b\u8ddf\u8e2a\u81ea\u5df1\u3002\u5e2e\u52a9\u9875\u9762\u4e0a\u5bf9\u4e8e request \u7684\u63cf\u8ff0\u5f88\u6e05\u695a\uff1a<\/p>\n
\u610f\u5473\u7740\u8be5\u8fdb\u7a0b\u88ab\u5176\u7236\u8fdb\u7a0b\u8ddf\u8e2a\u3002\u4efb\u4f55\u4f20\u9012\u7ed9\u8be5\u8fdb\u7a0b\u7684\u4fe1\u53f7\uff08\u9664\u4e86 SIGKILL\uff09\u90fd\u5c06\u901a\u8fc7 wait() \u65b9\u6cd5\u963b\u585e\u8be5\u8fdb\u7a0b\u5e76\u901a\u77e5\u5176\u7236\u8fdb\u7a0b\u3002\u6b64\u5916\uff0c\u8be5\u8fdb\u7a0b\u7684\u4e4b\u540e\u6240\u6709\u8c03\u7528 exec() \u52a8\u4f5c\u90fd\u5c06\u5bfc\u81f4 SIGTRAP \u4fe1\u53f7\u53d1\u9001\u5230\u6b64\u8fdb\u7a0b\u4e0a\uff0c\u4f7f\u5f97\u7236\u8fdb\u7a0b\u5728\u65b0\u7684\u7a0b\u5e8f\u6267\u884c\u524d\u5f97\u5230\u53d6\u5f97\u63a7\u5236\u6743\u7684\u673a\u4f1a\u3002\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u5e76\u4e0d\u9700\u8981\u5b83\u7684\u7684\u7236\u8fdb\u7a0b\u8ddf\u8e2a\u5b83\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8fdb\u7a0b\u4e0d\u5e94\u8be5\u53d1\u9001\u8fd9\u4e2a\u8bf7\u6c42\u3002\uff08pid\u3001addr \u4e0e data \u6682\u4e14\u4e0d\u63d0\uff09<\/p>\n
\u6211\u9ad8\u4eae\u4e86\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u9700\u8981\u6ce8\u610f\u7684\u90e8\u5206\u3002\u5728 ptrace \u8c03\u7528\u540e\uff0crun_target \u63a5\u4e0b\u6765\u8981\u505a\u7684\u5c31\u662f\u901a\u8fc7 execl \u4f20\u53c2\u5e76\u8c03\u7528\u3002\u5982\u540c\u9ad8\u4eae\u90e8\u5206\u6240\u8bf4\u660e\uff0c\u8fd9\u5c06\u5bfc\u81f4\u7cfb\u7edf\u5185\u6838\u5728 execl \u521b\u5efa\u8fdb\u7a0b\u524d\u6682\u65f6\u505c\u6b62\uff0c\u5e76\u5411\u7236\u8fdb\u7a0b\u53d1\u9001\u4fe1\u53f7\u3002<\/p>\n
\u662f\u65f6\u5019\u770b\u770b\u7236\u8fdb\u7a0b\u505a\u4ec0\u4e48\u4e86\u3002<\/p>\n
\r\nvoid run_debugger(pid_t child_pid)\r\n{\r\nint wait_status;\r\nunsigned icounter = 0;\r\nprocmsg(\"debugger started\/n\");\r\n\r\n\/* Wait for child to stop on its first instruction *\/\r\nwait(&wait_status);\r\n\r\nwhile (WIFSTOPPED(wait_status)) {\r\nicounter++;\r\n\/* Make the child execute another instruction *\/\r\nif (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) {\r\nperror(\"ptrace\");\r\nreturn;\r\n}\r\n\r\n\/* Wait for child to stop on its next instruction *\/\r\nwait(&wait_status);\r\n}\r\n\r\nprocmsg(\"the child executed %u instructions\/n\", icounter);\r\n}\r\n<\/pre>\n\u5982\u524d\u6587\u6240\u8ff0\uff0c\u4e00\u65e6\u5b50\u8fdb\u7a0b\u8c03\u7528\u4e86 exec\uff0c\u5b50\u8fdb\u7a0b\u4f1a\u505c\u6b62\u5e76\u88ab\u53d1\u9001 SIGTRAP \u4fe1\u53f7\u3002\u7236\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u8be5\u8fc7\u7a0b\u7684\u53d1\u751f\u5e76\u5728\u7b2c\u4e00\u4e2a wait() \u5904\u7b49\u5f85\u3002\u4e00\u65e6\u4e0a\u8ff0\u4e8b\u4ef6\u53d1\u751f\u4e86\uff0cwait() \u4fbf\u4f1a\u8fd4\u56de\uff0c\u7531\u4e8e\u5b50\u8fdb\u7a0b\u505c\u6b62\u4e86\u7236\u8fdb\u7a0b\u4fbf\u4f1a\u6536\u5230\u4fe1\u53f7\uff08\u5982\u679c\u5b50\u8fdb\u7a0b\u7531\u4e8e\u4fe1\u53f7\u7684\u53d1\u9001\u505c\u6b62\u4e86\uff0cWIFSTOPPED \u5c31\u4f1a\u8fd4\u56de true\uff09\u3002<\/p>\n
\u7236\u8fdb\u7a0b\u63a5\u4e0b\u6765\u7684\u52a8\u4f5c\u5c31\u662f\u6574\u7bc7\u6587\u7ae0\u6700\u9700\u8981\u5173\u6ce8\u7684\u90e8\u5206\u4e86\u3002\u7236\u8fdb\u7a0b\u4f1a\u5c06 PTRACE_SINGLESTEP \u4e0e\u5b50\u8fdb\u7a0b ID \u4f5c\u4e3a\u53c2\u6570\u8c03\u7528 ptrace \u65b9\u6cd5\u3002\u8fd9\u5c31\u4f1a\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u201c\u8bf7\u6062\u590d\u5b50\u8fdb\u7a0b\uff0c\u4f46\u5728\u5b83\u6267\u884c\u4e0b\u4e00\u6761\u6307\u4ee4\u524d\u963b\u585e\u201d\u3002\u5468\u800c\u590d\u59cb\u5730\uff0c\u7236\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u963b\u585e\uff0c\u5faa\u73af\u7ee7\u7eed\u3002\u5f53 wait() \u4e2d\u4f20\u51fa\u7684\u4fe1\u53f7\u4e0d\u518d\u662f\u5b50\u8fdb\u7a0b\u7684\u505c\u6b62\u4fe1\u53f7\u65f6\uff0c\u5faa\u73af\u7ec8\u6b62\u3002\u5728\u8ddf\u8e2a\u5668\uff08\u7236\u8fdb\u7a0b\uff09\u8fd0\u884c\u671f\u95f4\uff0c\u8fd9\u5c06\u4f1a\u662f\u88ab\u8ddf\u8e2a\u8fdb\u7a0b\uff08\u5b50\u8fdb\u7a0b\uff09\u4f20\u9012\u7ed9\u8ddf\u8e2a\u5668\u7684\u7ec8\u6b62\u4fe1\u53f7\uff08\u5982\u679c\u5b50\u8fdb\u7a0b\u7ec8\u6b62 WIFEXITED \u5c06\u8fd4\u56de true\uff09\u3002<\/p>\n
icounter \u5b58\u50a8\u4e86\u5b50\u8fdb\u7a0b\u6267\u884c\u6307\u4ee4\u7684\u6b21\u6570\u3002\u8fd9\u4e48\u770b\u6765\u6211\u4eec\u5c0f\u5c0f\u7684\u4f8b\u5b50\u4e5f\u5b8c\u6210\u4e86\u4e9b\u6709\u7528\u7684\u4e8b\u60c5 - \u5728\u547d\u4ee4\u884c\u4e2d\u6307\u5b9a\u7a0b\u5e8f\uff0c\u5b83\u5c06\u6267\u884c\u8be5\u7a0b\u5e8f\u5e76\u8bb0\u5f55\u5b83\u4ece\u5f00\u59cb\u5230\u7ed3\u675f\u6240\u9700\u8981\u7684 cpu \u6307\u4ee4\u6570\u91cf\u3002\u63a5\u4e0b\u6765\u5c31\u8ba9\u6211\u4eec\u8fd9\u4e48\u505a\u5427\u3002<\/p>\n
\u6d4b\u8bd5<\/strong><\/div>\n\u6211\u7f16\u8bd1\u4e86\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u5e76\u5229\u7528\u8ddf\u8e2a\u5668\u8fd0\u884c\u5b83\uff1a<\/p>\n
\r\n#include\r\n\r\nint main()\r\n{\r\nprintf(\"Hello, world!\/n\");\r\nreturn 0;\r\n}\r\n<\/pre>\n\u4ee4\u6211\u60ca\u8bb6\u7684\u662f\uff0c\u8ddf\u8e2a\u5668\u82b1\u4e86\u76f8\u5f53\u957f\u7684\u65f6\u95f4\uff0c\u5e76\u62a5\u544a\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u5171\u6709\u8d85\u8fc7 100,000 \u6761\u6307\u4ee4\u6267\u884c\u3002\u4ec5\u4ec5\u662f\u4e00\u6761\u8f93\u51fa\u8bed\u53e5\uff1f\u4ec0\u4e48\u9020\u6210\u4e86\u8fd9\u79cd\u60c5\u51b5\uff1f\u7b54\u6848\u5f88\u6709\u8da3\uff08\u81f3\u5c11\u4f60\u540c\u6211\u4e00\u6837\u75f4\u8ff7\u4e0e\u673a\u5668\/\u6c47\u7f16\u8bed\u8a00\uff09\u3002Linux \u7684 gcc \u9ed8\u8ba4\u4f1a\u52a8\u6001\u7684\u5c06\u7a0b\u5e8f\u4e0e c \u7684\u8fd0\u884c\u65f6\u5e93\u52a8\u6001\u5730\u94fe\u63a5\u3002\u8fd9\u5c31\u610f\u5473\u7740\u4efb\u4f55\u7a0b\u5e8f\u8fd0\u884c\u524d\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u9700\u8981\u52a8\u6001\u5e93\u52a0\u8f7d\u5668\u53bb\u67e5\u627e\u7a0b\u5e8f\u8fd0\u884c\u6240\u9700\u8981\u7684\u5171\u4eab\u5e93\u3002\u8fd9\u4e9b\u4ee3\u7801\u7684\u6570\u91cf\u5f88\u5927 - \u522b\u5fd8\u4e86\u6211\u4eec\u7684\u8ddf\u8e2a\u5668\u8981\u8ddf\u8e2a\u6bcf\u4e00\u6761\u6307\u4ee4\uff0c\u4e0d\u4ec5\u4ec5\u662f\u4e3b\u51fd\u6570\u7684\uff0c\u800c\u662f\u201c\u6574\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u6307\u4ee4\u201d\u3002<\/p>\n
\u6240\u4ee5\u5f53\u6211\u5c06\u6d4b\u8bd5\u7a0b\u5e8f\u4f7f\u7528\u9759\u6001\u7f16\u8bd1\u65f6\uff08\u901a\u8fc7\u6bd4\u8f83\uff0c\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u591a\u51fa 500 KB \u5de6\u53f3\u7684\u5927\u5c0f\uff0c\u8fd9\u90e8\u5206\u662f C \u8fd0\u884c\u65f6\u5e93\u7684\u9759\u6001\u94fe\u63a5\uff09\uff0c\u8ddf\u8e2a\u5668\u63d0\u793a\u53ea\u6709\u5927\u6982 7000 \u6761\u6307\u4ee4\u88ab\u6267\u884c\u3002\u8fd9\u4e2a\u6570\u76ee\u4ecd\u7136\u4e0d\u5c0f\uff0c\u4f46\u662f\u8003\u8651\u5230\u5728\u4e3b\u51fd\u6570\u6267\u884c\u524d libc \u7684\u521d\u59cb\u5316\u4ee5\u53ca\u4e3b\u51fd\u6570\u6267\u884c\u540e\u7684\u6e05\u9664\u4ee3\u7801\uff0c\u8fd9\u4e2a\u6570\u76ee\u5df2\u7ecf\u662f\u76f8\u5f53\u4e0d\u9519\u4e86\u3002\u6b64\u5916\uff0cprintf \u4e5f\u662f\u4e00\u4e2a\u590d\u6742\u7684\u51fd\u6570\u3002<\/p>\n
\u4ecd\u7136\u4e0d\u6ee1\u610f\u7684\u8bdd\uff0c\u6211\u9700\u8981\u7684\u662f\u201c\u53ef\u4ee5\u6d4b\u8bd5\u201d\u7684\u4e1c\u897f - \u4f8b\u5982\u53ef\u4ee5\u5b8c\u6574\u8bb0\u5f55\u6bcf\u4e00\u4e2a\u6307\u4ee4\u8fd0\u884c\u7684\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u3002\u8fd9\u5f53\u7136\u53ef\u4ee5\u901a\u8fc7\u6c47\u7f16\u4ee3\u7801\u5b8c\u6210\u3002\u6240\u4ee5\u6211\u627e\u5230\u4e86\u8fd9\u4e2a\u7248\u672c\u7684 \u201cHello, world!\u201d \u5e76\u7f16\u8bd1\u4e86\u5b83\u3002<\/p>\n
\r\nsection .text\r\n; The _start symbol must be declared for the linker (ld)\r\nglobal _start\r\n\r\n_start:\r\n\r\n; Prepare arguments for the sys_write system call:\r\n; - eax: system call number (sys_write)\r\n; - ebx: file descriptor (stdout)\r\n; - ecx: pointer to string\r\n; - edx: string length\r\nmov edx, len\r\nmov ecx, msg\r\nmov ebx, 1\r\nmov eax, 4\r\n\r\n; Execute the sys_write system call\r\nint 0x80\r\n\r\n; Execute sys_exit\r\nmov eax, 1\r\nint 0x80\r\n\r\nsection .data\r\nmsg db 'Hello, world!', 0xa\r\nlen equ $ - msg\r\n<\/pre>\n\u5f53\u7136\uff0c\u73b0\u5728\u8ddf\u8e2a\u5668\u63d0\u793a 7 \u6761\u6307\u4ee4\u88ab\u6267\u884c\u4e86\uff0c\u8fd9\u6837\u4e00\u6765\u5f88\u5bb9\u6613\u533a\u5206\u5b83\u4eec\u3002<\/p>\n
\u6df1\u5165\u6307\u4ee4\u6d41<\/strong><\/div>\n\u4e0a\u9762\u90a3\u4e2a\u6c47\u7f16\u8bed\u8a00\u7f16\u5199\u7684\u7a0b\u5e8f\u4f7f\u5f97\u6211\u53ef\u4ee5\u5411\u4f60\u4ecb\u7ecd ptrace \u7684\u53e6\u5916\u4e00\u4e2a\u5f3a\u5927\u7684\u7528\u9014 - \u8be6\u7ec6\u663e\u793a\u88ab\u8ddf\u8e2a\u8fdb\u7a0b\u7684\u72b6\u6001\u3002\u4e0b\u9762\u662f run_debugger \u51fd\u6570\u7684\u53e6\u4e00\u4e2a\u7248\u672c\uff1a<\/p>\n
\r\nvoid run_debugger(pid_t child_pid)\r\n{\r\nint wait_status;\r\nunsigned icounter = 0;\r\nprocmsg(\"debugger started\/n\");\r\n\r\n\/* Wait for child to stop on its first instruction *\/\r\nwait(&wait_status);\r\n\r\nwhile (WIFSTOPPED(wait_status)) {\r\nicounter++;\r\nstruct user_regs_struct regs;\r\nptrace(PTRACE_GETREGS, child_pid, 0, ®s);\r\nunsigned instr = ptrace(PTRACE_PEEKTEXT, child_pid, regs.eip, 0);\r\n\r\nprocmsg(\"icounter = %u. EIP = 0x%08x. instr = 0x%08x\/n\",\r\nicounter, regs.eip, instr);\r\n\r\n\/* Make the child execute another instruction *\/\r\nif (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) {\r\nperror(\"ptrace\");\r\nreturn;\r\n}\r\n\r\n\/* Wait for child to stop on its next instruction *\/\r\nwait(&wait_status);\r\n}\r\n\r\nprocmsg(\"the child executed %u instructions\/n\", icounter);\r\n}\r\n<\/pre>\n\u4e0d\u540c\u4ec5\u4ec5\u5b58\u5728\u4e8e while \u5faa\u73af\u7684\u5f00\u59cb\u51e0\u884c\u3002\u8fd9\u4e2a\u7248\u672c\u91cc\u589e\u52a0\u4e86\u4e24\u4e2a\u65b0\u7684 ptrace \u8c03\u7528\u3002\u7b2c\u4e00\u6761\u5c06\u8fdb\u7a0b\u7684\u5bc4\u5b58\u5668\u503c\u8bfb\u53d6\u8fdb\u4e86\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e2d\u3002 sys\/user.h \u5b9a\u4e49\u6709 user_regs_struct\u3002\u5982\u679c\u4f60\u67e5\u770b\u5934\u6587\u4ef6\uff0c\u5934\u90e8\u7684\u6ce8\u91ca\u8fd9\u4e48\u5199\u5230\uff1a<\/p>\n
\/* \u8fd9\u4e2a\u6587\u4ef6\u53ea\u4e3a\u4e86 GDB \u800c\u521b\u5efa<\/p>\n
\u4e0d\u7528\u8be6\u7ec6\u7684\u9605\u8bfb.\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4f60\u5728\u5e72\u561b<\/p>\n
\u4e0d\u8981\u5728\u9664\u4e86 GDB \u4ee5\u5916\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u6b64\u6587\u4ef6 *\/<\/p>\n
\u4e0d\u77e5\u9053\u4f60\u505a\u4f55\u611f\u60f3\uff0c\u4f46\u8fd9\u8ba9\u6211\u89c9\u5f97\u6211\u4eec\u627e\u5bf9\u5730\u65b9\u4e86\u3002\u56de\u5230\u4f8b\u5b50\u4e2d\uff0c\u4e00\u65e6\u6211\u4eec\u5728 regs \u53d8\u91cf\u4e2d\u53d6\u5f97\u4e86\u5bc4\u5b58\u5668\u7684\u503c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u5c06 PTRACE_PEEKTEXT \u4f5c\u4e3a\u53c2\u6570\u3001 regs.eip\uff08x86 \u4e0a\u7684\u6269\u5c55\u6307\u4ee4\u6307\u9488\uff09\u4f5c\u4e3a\u5730\u5740\uff0c\u8c03\u7528 ptrace \uff0c\u8bfb\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u5f53\u524d\u6307\u4ee4\uff08\u8b66\u544a\uff1a\u5982\u540c\u6211\u4e0a\u9762\u6240\u8bf4\uff0c\u6587\u7ae0\u5f88\u5927\u7a0b\u5ea6\u4e0a\u662f\u5e73\u53f0\u76f8\u5173\u7684\u3002\u6211\u7b80\u5316\u4e86\u4e00\u4e9b\u8bbe\u5b9a - \u4f8b\u5982\uff0cx86 \u6307\u4ee4\u96c6\u4e0d\u9700\u8981\u8c03\u6574\u5230 4 \u5b57\u8282\uff0c\u6211\u768432\u4f4d Ubuntu unsigned int \u662f 4 \u5b57\u8282\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8bb8\u591a\u5e73\u53f0\u90fd\u4e0d\u9700\u8981\u3002\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u6307\u4ee4\u9700\u8981\u9884\u5148\u5b89\u88c5\u5b8c\u6574\u7684\u53cd\u6c47\u7f16\u5668\u3002\u6211\u4eec\u8fd9\u91cc\u6ca1\u6709\uff0c\u4f46\u5b9e\u9645\u7684\u8c03\u8bd5\u5668\u662f\u6709\u7684\uff09\u3002\u4e0b\u9762\u662f\u65b0\u8ddf\u8e2a\u5668\u6240\u5c55\u793a\u51fa\u7684\u8c03\u8bd5\u6548\u679c\uff1a<\/p>\n
\r\n$ simple_tracer traced_helloworld\r\n[5700] debugger started\r\n[5701] target started. will run 'traced_helloworld'\r\n[5700] icounter = 1\/. EIP = 0x08048080\/. instr = 0x00000eba\r\n[5700] icounter = 2\/. EIP = 0x08048085\/. instr = 0x0490a0b9\r\n[5700] icounter = 3\/. EIP = 0x0804808a. instr = 0x000001bb\r\n[5700] icounter = 4\/. EIP = 0x0804808f. instr = 0x000004b8\r\n[5700] icounter = 5\/. EIP = 0x08048094\/. instr = 0x01b880cd\r\nHello, world!\r\n[5700] icounter = 6\/. EIP = 0x08048096\/. instr = 0x000001b8\r\n[5700] icounter = 7\/. EIP = 0x0804809b. instr = 0x000080cd\r\n[5700] the child executed 7 instructions\r\n\u73b0\u5728\uff0c\u9664\u4e86 icounter\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u89c2\u5bdf\u5230\u6307\u4ee4\u6307\u9488\u4e0e\u5b83\u6bcf\u4e00\u6b65\u6240\u6307\u5411\u7684\u6307\u4ee4\u3002\u600e\u4e48\u6765\u5224\u65ad\u8fd9\u4e2a\u7ed3\u679c\u5bf9\u4e0d\u5bf9\u5462\uff1f\u4f7f\u7528 objdump -d \u5904\u7406\u53ef\u6267\u884c\u6587\u4ef6\uff1a\r\n\r\n$ objdump -d traced_helloworld\r\n\r\ntraced_helloworld: file format elf32-i386\r\n\r\nDisassembly of section .text:\r\n\r\n08048080 :\r\n8048080: ba 0e 00 00 00 mov $0xe,%edx\r\n8048085: b9 a0 90 04 08 mov $0x80490a0,%ecx\r\n804808a: bb 01 00 00 00 mov $0x1,%ebx\r\n804808f: b8 04 00 00 00 mov $0x4,%eax\r\n8048094: cd 80 int $0x80\r\n8048096: b8 01 00 00 00 mov $0x1,%eax\r\n804809b: cd 80 int $0x80\r\n<\/pre>\n\u8fd9\u4e2a\u7ed3\u679c\u548c\u6211\u4eec\u8ddf\u8e2a\u5668\u7684\u7ed3\u679c\u5c31\u5f88\u5bb9\u6613\u6bd4\u8f83\u4e86\u3002<\/p>\n
\u5c06\u8ddf\u8e2a\u5668\u5173\u8054\u5230\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b<\/p>\n
\u5982\u4f60\u6240\u77e5\uff0c\u8c03\u8bd5\u5668\u4e5f\u80fd\u5173\u8054\u5230\u5df2\u7ecf\u8fd0\u884c\u7684\u8fdb\u7a0b\u3002\u73b0\u5728\u4f60\u5e94\u8be5\u4e0d\u4f1a\u60ca\u8bb6\uff0cptrace \u901a\u8fc7\u4ee5 PTRACE_ATTACH \u4e3a\u53c2\u6570\u8c03\u7528\u4e5f\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u8fd9\u91cc\u6211\u4e0d\u4f1a\u5c55\u793a\u793a\u4f8b\u4ee3\u7801\uff0c\u901a\u8fc7\u4e0a\u6587\u7684\u793a\u4f8b\u4ee3\u7801\u5e94\u8be5\u5f88\u5bb9\u6613\u5b9e\u73b0\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u51fa\u4e8e\u5b66\u4e60\u76ee\u7684\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u65b9\u6cd5\u66f4\u7b80\u4fbf\uff08\u56e0\u4e3a\u6211\u4eec\u5728\u5b50\u8fdb\u7a0b\u521a\u5f00\u59cb\u5c31\u53ef\u4ee5\u8ba9\u5b83\u505c\u6b62\uff09\u3002<\/p>\n