【问题标题】:Pthread Program CounterPthread 程序计数器
【发布时间】:2017-01-26 19:24:04
【问题描述】:

在 C 中使用 pthreads,有没有办法访问特定线程的程序计数器/指令指针?

例子:

void *thread_main(void *arg) {
  long thread = (long)arg;

  lock (thread);
  ***print (thread.pc);*** 
  critical_section (thread);
  ***print (thread.pc);***  
  unlock (thread);

  return NULL;
}

【问题讨论】:

  • 您是在寻找跨平台解决方案,还是只需要它在特定平台(例如 Windows 等)上工作。
  • C 没有“程序计数器”的概念。您可以使用__FILE____func____LINE__ 宏来识别当前正在执行的代码部分。
  • @i_am_jorf 我有一台 MacOS 和一台 Debian 机器。所以两者都可以接受:)
  • 写一个从栈中获取返回地址的函数能解决你的问题吗?
  • 您可以使用these 解决方案访问寄存器,但据我所知,您需要在您关心的线程上执行此操作。

标签: c multithreading pthreads program-counter


【解决方案1】:

查看backtrace();你的两个平台都支持它。

    #include <execinfo.h>
    ....
        void * pc;
        backtrace(&pc, 1);
    ....

详情请见man 3 backtrace。请注意,运行相同thread_main 的所有线程将报告相同的pc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多