C语言 使用 pid 启动 gdb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2152582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:17:34  来源:igfitidea点击:

start gdb using a pid

cprocessgdbdbx

提问by Vijay

In general i see the process's pidwhich is running in the background and start dbx on that process using the command dbx -a <pid>

一般来说,我会看到pid在后台运行的进程,并使用命令在该进程上启动 dbxdbx -a <pid>

similarly how could i do it using gdb?

同样,我如何使用 gdb 做到这一点?

采纳答案by R Samuel Klatchko

There are two ways.

有两种方法。

From the command line, include the pid as an argument after the executable name:

在命令行中,在可执行文件名称后包含 pid 作为参数:

gdb /path/to/prog PID

From within gdb, you can use the attach command:

在 gdb 中,您可以使用 attach 命令:

gdb /path/to/prog
gdb> attach PID

While the specifying on the command line is more concise, there is a slight risk that if you have a core file that has a name that is the same as the pid (i.e. for pid 2345, the core file would have to be named "2345") then gdb will open the core file. Admittedly, the chance of this happening is minuscule.

虽然在命令行上指定更简洁,但如果您有一个名称与 pid 相同的核心文件(即对于 pid 2345,核心文件必须命名为“2345 ") 然后 gdb 将打开核心文件。诚然,这种情况发生的可能性微乎其微。

回答by zakkak

In addition to the previous you can directly use

除了前面的你可以直接使用

gdb -p <pid>

回答by janm

From the gdb man page:

从 gdb 手册页:

You can, instead, specify a process ID as a second argument, if you want to debug a running process:

如果要调试正在运行的进程,则可以将进程 ID 指定为第二个参数:

gdb program 1234