我尝试获取进程所有线程的回溯。 我写了一个测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int func_a(int *a)
{
*a++;
return 0;
}
void* test_thread(void* args)
{
int a = 0;
while(1) {
func_a(&a);
//sleep(1);
}
fprintf(stdout, "%d\n", a);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pid = 0;
pthread_create(&pid, NULL, test_thread, NULL);
pthread_create(&pid, NULL, test_thread, NULL);
pthread_create(&pid, NULL, test_thread, NULL);
pause();
return 0;
}
然后我运行程序并执行
pidof ./1|xargs -n1 sudo gdb --batch -ex "thread apply all bt full" -p
我看到了回溯。(太多的输出我只是选择了一些
Thread 2 (Thread 0xb459db40 (LWP 25272)):
#0 func_a (a=0xb459d354) at 1.c:6
No locals.
#1 0x0804871f in test_thread (args=0x0) at 1.c:21
a = 382337846
#2 0xb775bf72 in start_thread (arg=0xb459db40) at pthread_create.c:312
__res = <optimized out>
pd = 0xb459db40
now = <optimized out>
unwind_buf = {cancel_jmp_buf = {{jmp_buf = {-1216946176, -1269179584, 4001536, -1269181400, -56721544, -1538858626}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0, 0x0, 0x0}, data = {prev = 0x0, cleanup = 0x0, canceltype = 0}}}
not_first_call = <optimized out>
pagesize_m1 = <optimized out>
sp = <optimized out>
freesize = <optimized out>
__PRETTY_FUNCTION__ = "start_thread"
#3 0xb7691f8e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:129
No locals.
但是当我在test_thread函数中启用sleep语句时,它无法获取回溯。
Thread 3 (Thread 0xb4d29b40 (LWP 26045)):
#0 0xb7716cf9 in ?? ()
No symbol table info available.
#1 0x00000000 in ?? ()
No symbol table info available.
Thread 2 (Thread 0xb4528b40 (LWP 26046)):
#0 0xb7716cf9 in ?? ()
No symbol table info available.
#1 0x00000000 in ?? ()
No symbol table info available.
有人知道为什么吗
感谢@Employed Russian指出缺陷。环境如下:
我使用ubuntu 14.04