从顶部到gdb映射线程ID

Viv*_*oel 0 multithreading gdb

我正在使用top查看使用的线程明智的cpu用法

  top -H -p `pgrep app.out`
Run Code Online (Sandbox Code Playgroud)

它为每个线程显示一些pid

4015
4016
Run Code Online (Sandbox Code Playgroud)

我已经使用gdb attach命令将gdb附加到应用程序。现在我想切换到顶部o / p内部显示的线程4015。

我怎样才能做到这一点 ?

如果我启动线程4015,则表明没有线程。因为我需要在gdb中提供线程ID。

那么如何将顶级线程ID映射到gdb线程ID?

Kev*_*vin 5

您应该能够将LWPGDB中显示的top信息与以下信息进行匹配:

根据我对Firefox的快速测试,您可以在以下代码中看到它top -H -p

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
6492 kevin     20   0 1242m 386m  31m S  0.3  4.9   0:09.00 firefox
6470 kevin     20   0 1242m 386m  31m S  5.7  4.9   5:04.89 firefox
Run Code Online (Sandbox Code Playgroud)

以及在GDB中info threads

 22   Thread 0x7fe3d2393700 (LWP 6492) "firefox" pthread_cond_timedwait...
...
* 1   Thread 0x7fe3dd868740 (LWP 6470) "firefox" __GI___poll ()...
Run Code Online (Sandbox Code Playgroud)

编辑:专为您而设,这是gdb的全新命令lwp_to_id <lwp>

import gdb
class lwp_to_id (gdb.Command):
    def __init__(self):
        gdb.Command.__init__(self, "lwp_to_id", gdb.COMMAND_OBSCURE)

    def invoke(self, args, from_tty):
        lwp = int(args)
        for thr in gdb.selected_inferior().threads():
            if thr.ptid[1] == lwp:
                print "LWP %s maps to thread #%d" % (lwp, thr.num)
                return
        else:
            print "LWP %s doesn't match any threads in the current inferior." % lwp
lwp_to_id()
Run Code Online (Sandbox Code Playgroud)

(至少在trunkGDB版本上有效,不确定官方发行版!