linux之使用ptrace 跟蹤多執行緒程式

2021-07-12 05:35:32 字數 2092 閱讀 6275

1.ptrace 原型說明

#include long ptrace(enum __ptrace_requestrequest, pid_tpid, void *addr, void *data);
在使用ptrace_traceme引數時,跟蹤多執行緒程式

需要使用ptrace_setoptions來設定ptrace相關屬性。

ptrace_setoptions 是將父程序內由data指向的值設定為ptrace 選項,data作為掩碼來解釋,由下面的標誌來指定:

(1) ptrace_o_exitkill:當跟蹤程序退出時,向所有被跟蹤程序傳送sigkill訊號將其退出,這個引數可以防止被跟蹤程序脫離跟蹤程序的控制。

(2) ptrace_o_traceclone:被跟蹤程序在下一次呼叫clone()時將其停止,並自動跟蹤新產生的程序,新產生的程序剛開始收到sigstop訊號。其新產生的程序的pid可以  

通過ptrace_geteventmsg得到。

(3) ptrace_o_traceexec:被跟蹤程序在下一次呼叫exec()函式時使其停止。

(4) ptrace_o_traceexit:被跟蹤程序在退出是停止其執行,被跟蹤程序的退出狀態可通過ptrace_geteventmsg獲得。

(5) ptrace_o_tracefork:被跟蹤程序在下次呼叫fork()時停止執行,並自動跟蹤新產生的程序,新產生的程序剛開始收到sigstop訊號。其新產生的程序的pid可以  

通過ptrace_geteventmsg得到。

(6) ptrace_o_tracevfork:被跟蹤程序在下次呼叫vfork()時停止執行,並自動跟蹤新產生的程序,新產生的程序剛開始收到sigstop訊號。其新產生的程序的pid可以  

通過ptrace_geteventmsg得到。

ptrace_geteventmsg:獲取剛剛發生的ptrace事件訊息,並存放在跟蹤程序由data指向的位置,addr引數被忽略。對於

ptrace_event_fork,ptrace_event_vfork,ptrace_event_vforkdown和ptrace_event_clone,data是新程序的pid.

#include #include #include #include #include #include #include #include void *thread_function(void *arg);

char message = "hello";

int main()

printf("wait for thread to finish...\n");

//等待上面執行緒結束

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined, it returned %s\n", (char *) thread_result);

exit(exit_success);

//return 0;

}void *thread_function(void *arg)

#include #include #include #include #include #include #include #include //

int main()

else

if (wifsignaled(status))

if (wifstopped(status) && wstopsig(status) == sigstop)

if (wifstopped(status) && wstopsig(status) == sigtrap)}}

if (child_waited == -1)

break;

if (wifexited(status))

ptrace(ptrace_cont, child_waited, 1, null);

} }return 0;

}

linux下注入so之ptrace

注入so,也就是讓另乙個正在執行的程序載入我們指定的so,之後我們的so裡面的 就可以自動執行,這個主要是用來進行api攔截的。windows下的api攔截非常方便,而linux下的就比較複雜。如果去網上搜,基本上找到的是這個是2002年的,它的思路是使用ptrace,attach目標程序,然後搜尋...

Linux上程式除錯的基石 1 ptrace

1.在linux系統中,程序狀態除了我們所熟知的task running,task interruptible,task stopped等,還有乙個task traced。這表明這個程序處於什麼狀態?2.strace可以方便的幫助我們 記錄程序所執行的系統呼叫 它是如何跟蹤到程序執行的?3.gdb是...

Linux之使用網路

linux有好多命令可以讓你方便的使用網路,常見的有ssh,rsync,wget,curl等等,但是telnet等方式並不適用於網路互動的使用,因為它會暴露你的使用者名稱密碼等。所以一般使用安全的命令來進行網路的操作。1 安全的登入到另一台計算機。ssh secure shell 使用的格式為 ss...