【问题标题】:how to get all threads pthread_t id of a process without gdb/pstack on linux如何在linux上获取没有gdb/pstack的进程的所有线程pthread_t id
【发布时间】:2018-06-05 09:28:00
【问题描述】:

我想打印一个进程的所有线程堆栈,没有 pstack、gdb。我想使用 pthread_kill 向所有线程发送自定义信号,并使用 signal_handler 接受它然后打印当前线程堆栈, 但问题是如何获取所有线程 ID? 我已经尝试过:读取 /proc/$pid/task 下的子目录,但这代表 LWP 线程,而不是 pthread_t。

【问题讨论】:

    标签: multithreading


    【解决方案1】:
    // 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多