Linux system: The pstack command traces the call stack of the process.

The pstack command is used to display the stack (user mode) trace of the process . The pstack command must be run by the owner or root account of the corresponding process. You can use pstack to determine where the process hangs. This command has only one parameter, and that is pid. The pid here can be the process pid or the thread tid. When the parameter is tid, pstack will print out the call stack of the current thread. When the parameter is pid, it will print out all the call stacks of all threads in the current process. . A brief introduction about pstack can be found in the man manual. A simple multi-threaded example is as follows:

#include<stdio.h>
#include<stdlib.h>
#include <pthread.h>
 
void* task_entry(void* arg);  
 
int main(){  
    pthread_t pid1,pid2;  
 
    pthread_create(&pid1,NULL,task_entry,(void *)1);  
    pthread_create(&pid2,NULL,task_entry,(void *)2);  
 
    pthread_join(pid1,NULL);  
    pthread_join(pid2,NULL);  
 
    return 0;  
}
  
void* task_entry(void* arg){  
	while(1){
		sleep(1);
	}
}

The usage of pstack is as follows, the parameters are the thread tid and the pid of the process:

[root@localhost some_func]# gcc multithreading.c -o multithreading -lpthread
[root@localhost some_func]# ./multithreading &
[1] 23539
[root@localhost some_func]# pstree -p 23539
multithreading(23539)─┬─{multithreading}(23540)
                      └─{multithreading}(23541)
//线程
[root@localhost some_func]# pstack 23541
Thread 1 (process 23541):
#0  0x00000031ce8aca7d in nanosleep () from /lib64/libc.so.6
#1  0x00000031ce8ac8f0 in sleep () from /lib64/libc.so.6
#2  0x0000000000400646 in task_entry ()
#3  0x00000031cf407aa1 in start_thread () from /lib64/libpthread.so.0
#4  0x00000031ce8e893d in clone () from /lib64/libc.so.6

//进程
[root@localhost some_func]# pstack 23539
Thread 3 (Thread 0x7f115e248700 (LWP 23540)):
#0  0x00000031ce8aca7d in nanosleep () from /lib64/libc.so.6
#1  0x00000031ce8ac8f0 in sleep () from /lib64/libc.so.6
#2  0x0000000000400646 in task_entry ()
#3  0x00000031cf407aa1 in start_thread () from /lib64/libpthread.so.0
#4  0x00000031ce8e893d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f115d847700 (LWP 23541)):
#0  0x00000031ce8aca7d in nanosleep () from /lib64/libc.so.6
#1  0x00000031ce8ac8f0 in sleep () from /lib64/libc.so.6
#2  0x0000000000400646 in task_entry ()
#3  0x00000031cf407aa1 in start_thread () from /lib64/libpthread.so.0
#4  0x00000031ce8e893d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f115e24a700 (LWP 23539)):
#0  0x00000031cf4082fd in pthread_join () from /lib64/libpthread.so.0
#1  0x0000000000400613 in main ()

Note: What pstack tracks is the user mode call stack

Guess you like

Origin blog.csdn.net/wangquan1992/article/details/108415936