gpt4 book ai didi

multithreading - 将线程 ID 从顶部映射到 gdb

转载 作者:行者123 更新时间:2023-12-01 22:43:26 25 4
gpt4 key购买 nike

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

  top -H -p `pgrep app.out`

它为每个线程显示了一些 pid

4015
4016

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

我该怎么做?

如果我启动线程 4015,它不会显示任何线程。因为我需要在 gdb 中提供线程 ID。

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

最佳答案

您应该能够匹配 LWP在 GDB 中显示为 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

还有 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()

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

关于multithreading - 将线程 ID 从顶部映射到 gdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7633092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com