我想打印一个进程的所有线程堆栈,没有pstack,gdb。我想使用pthread_kill向所有线程发送自定义信号,并使用signal_handler接受它,然后打印当前线程堆栈,但问题是如何获得所有线程的id?我已经尝试过了:读取/proc/$pid/task下的子目录,但这表示的是LWP线程,而不是pthread_t。
发布于 2018-06-05 20:42:45
// try to print all threads backtrace
if (DIR* dir = opendir("/proc/self/task")) {
int thread_id;
while (dirent* entry = readdir(dir)) {
if (entry->d_name[0] == '.')
continue;
try {
thread_id = std::stol(entry->d_name);
ldout(m_cct, 1) << "send SIGUSR2 to " << thread_id << dendl;
syscall(SYS_tgkill, getpid(), thread_id, SIGUSR2);
t.sleep();
} catch (const std::exception &e) {
// pass
}
}
closedir(dir);
}https://stackoverflow.com/questions/50696693
复制相似问题