【发布时间】:2018-06-05 09:28:00
【问题描述】:
我想打印一个进程的所有线程堆栈,没有 pstack、gdb。我想使用 pthread_kill 向所有线程发送自定义信号,并使用 signal_handler 接受它然后打印当前线程堆栈, 但问题是如何获取所有线程 ID? 我已经尝试过:读取 /proc/$pid/task 下的子目录,但这代表 LWP 线程,而不是 pthread_t。
【问题讨论】:
标签: multithreading
我想打印一个进程的所有线程堆栈,没有 pstack、gdb。我想使用 pthread_kill 向所有线程发送自定义信号,并使用 signal_handler 接受它然后打印当前线程堆栈, 但问题是如何获取所有线程 ID? 我已经尝试过:读取 /proc/$pid/task 下的子目录,但这代表 LWP 线程,而不是 pthread_t。
【问题讨论】:
标签: multithreading
// 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);
}
【讨论】: