【问题标题】:How does one obtain the pid of the currently debugged process in gdb?如何在gdb中获取当前被调试进程的pid?
【发布时间】:2016-04-18 21:05:01
【问题描述】:

在gdb内部,什么命令会提供被调试进程的pid?

大多数google results 只讨论一旦我们知道 pid 如何将 gdb 附加到进程。

【问题讨论】:

    标签: c++ linux gdb


    【解决方案1】:

    一种简单的方法是info inferior。这里我自己调试 gdb,这个命令显示了被调试对象的 PID:

    (top-gdb) info inferior
      Num  Description       Executable        
    * 1    process 14068     /home/tromey/gdb/build/gdb/gdb 
    

    你也可以只调用普通的 C 函数:

    (top-gdb) print getpid()
    $3 = 14068
    

    【讨论】:

    • 请注意,如果程序没有运行,描述将是<null>。 / 缩写为i i
    • getpid "print (int)getpid()" 需要强制转换
    【解决方案2】:

    另一种方法:

    (gdb) python print(gdb.selected_inferior().pid)
    32737
    

    查看有关 gdb Python 劣质 API 的更多信息: https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html#Inferiors-In-Python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多