【问题标题】:Mapping thread id from top to gdb将线程 id 从顶部映射到 gdb
【发布时间】:2011-11-29 18:59:37
【问题描述】:

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

  top -H -p `pgrep app.out`

它为每个线程显示一些 pid,例如

4015
4016

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

我该怎么做?

如果我触发线程 4015,它显示没有线程。因为我需要在 gdb 中提供线程 ID。

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

【问题讨论】:

    标签: multithreading gdb


    【解决方案1】:

    做一个

    ps xjf 
    

    这将为您提供所有进程的树及其 pid 和父 pid。

    【讨论】:

    • 绝对是一个有用的命令。我更进一步,做了ps axjf 并让它吐出所有东西。但ps xjf 肯定更专注。
    【解决方案2】:

    您应该能够将 GDB 中显示的 LWPtop 信息匹配:

    根据我对 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
    

    在 GDB 中 info threads:

     22   Thread 0x7fe3d2393700 (LWP 6492) "firefox" pthread_cond_timedwait...
    ...
    * 1   Thread 0x7fe3dd868740 (LWP 6470) "firefox" __GI___poll ()...
    

    编辑:仅供您使用,这里有一个全新的 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()
    

    (至少在 trunk 版本的 GDB 上工作,不确定官方版本!

    【讨论】:

    • 是否需要手动映射。还是有一个自动功能?
    猜你喜欢
    • 2011-01-12
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多