【问题标题】:getting and settings CPU registers of multiple threads using ptrace使用 ptrace 获取和设置多个线程的 CPU 寄存器
【发布时间】:2011-10-25 16:12:36
【问题描述】:

我有兴趣在另一个监控进程的监督下运行多线程应用程序。监控进程应该能够获取和设置被监控应用程序中所有线程的 CPU 寄存器。我知道如何为单线程应用程序执行此操作。但我很想知道如何为多线程应用程序扩展它。

【问题讨论】:

  • 好像我问了一个非常难的问题。

标签: c linux pthreads ptrace


【解决方案1】:

您可以在 ptrace 中使用线程 id 而不是 pid,它应该可以正常工作。但是线程管理需要您自己完成。

【讨论】:

  • 所以你的意思是我必须使用 waitpid 分别等待每个线程来获取或修改它们的寄存器?
【解决方案2】:

在 ptrace 中使用线程 id 代替 pid 不是解决方案。 因为在 Linux-64 中,pthread_t--unsigned long,pid_t--unsigned int。 我也想知道这个问题。 我有另一种方法来获取线程注册信息,使用 gdb。 这是我的代码:

void *ThrFunc(void *para)
{
    printf("hello world.\n");
    sleep(-1);      // suspend the thread.
}
int main()
{
    pthread_t ptid;
    int ret = pthread_create(&ptid, NULL, ThrFunc, NULL);
    if(ret != 0)
    {   
        exit(errno);
    }   

    pthread_join(ptid, NULL);// suspend the main thread.

    return 0;
}

以下是gdb调试详情:

(gdb) info thread
  2 Thread 0x7ffff7fe9700 (LWP 4533)  0x00000033d98ab91d in nanosleep () from /lib64/libc.so.6
* 1 Thread 0x7ffff7feb720 (LWP 4530)  0x00000033d9c080ad in pthread_join () from /lib64/libpthread.so.0
(gdb) info reg
rax            0xfffffffffffffe00   -512
...
rip            0x33d9c080ad 0x33d9c080ad <pthread_join+269>
eflags         0x246    [ PF ZF IF ]
...
(gdb) thread 2
[Switching to thread 2 (Thread 0x7ffff7fe9700 (LWP 4533))]#0  0x00000033d98ab91d in nanosleep () from /lib64/libc.so.6
(gdb) info thread
* 2 Thread 0x7ffff7fe9700 (LWP 4533)  0x00000033d98ab91d in nanosleep () from /lib64/libc.so.6
  1 Thread 0x7ffff7feb720 (LWP 4530)  0x00000033d9c080ad in pthread_join () from /lib64/libpthread.so.0
(gdb) info reg
rax            0xfffffffffffffdfc   -516
...
rip            0x33d98ab91d 0x33d98ab91d <nanosleep+45>
eflags         0x293    [ CF AF SF IF ]
...

我希望这会对你有所帮助。 对了,我也想知道:如何使用ptrace()来获取线程寄存器的详细信息?

【讨论】:

    猜你喜欢
    • 2019-10-02
    • 2012-05-09
    • 2010-12-22
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2019-11-09
    • 2011-11-09
    • 2021-03-25
    相关资源
    最近更新 更多