简体   繁体   English

如何在Linux中调试正在运行的C ++程序?

[英]How to debug a running C++ program in Linux?

I have a question about debugging a running C++ program in Linux. 我有一个关于在Linux中调试正在运行的C ++程序的问题。 If a programming is already running and can't be interrupted, how to do that. 如果编程已经在运行并且无法中断,那么该怎么做。

I can find three ways, but I don't know too much about details, I am grateful if any one can elaborate it deeper. 我可以找到三种方法,但我对细节知之甚少,如果有人能够更深入地阐述它,我将不胜感激。

1) we can use GDB by specifying the process ID 1)我们可以通过指定进程ID来使用GDB

gdb -p PID

In this case, what's the difference between this and attach PID ? 在这种情况下,这和attach PID什么区别?

2) We can use pstat, however, I am using Ubuntu, no pstat, but only mpstat 2)我们可以使用pstat,但是,我使用的是Ubuntu,没有pstat,只有mpstat

it seems that mpstat does not provide too much information, and not so many options. 似乎mpstat没有提供太多的信息,也没有太多的选择。

3) check the details information under directory ./proc 3)检查目录./proc下的详细信息

In this case, just go to the directory with the PID. 在这种情况下,只需转到带有PID的目录。 However, should this be done mannually? 但是,这应该是手动完成吗?

I can't find -p option in gdb man or their documentation, but it does work! 我在gdb man或他们的文档中找不到-p选项,但它确实有效! I've tried it many times with older versions on RedHat and 7.0.1 on Debian. 我在RedHat和Debian上的7.0.1上使用旧版本尝试了很多次。

I'm not sure how exactly it finds the exe by PID (maybe /proc/<PID>/exe ), but it does. 我不确定它是如何通过PID找到exe(也许是/proc/<PID>/exe ),但它确实如此。 Since it's not described in their documentation, perhaps it not the most recommended way, but I haven't had any problems with it. 由于它没有在他们的文档中描述,也许它不是最推荐的方式,但我没有遇到任何问题。

There's no noticeable difference between gdb -p <PID> and running gdb and in the their shell typing attach <PID> . gdb -p <PID>和运行gdb之间没有明显区别,在shell中键入attach <PID>

I personally prefer ps xa| grep myprogram 我个人更喜欢ps xa| grep myprogram ps xa| grep myprogram for getting the PID 获取PID的ps xa| grep myprogram

In regards to technique 1, there is no -p flag and you still need the name of the program: 关于技术1,没有-p标志,你仍然需要程序的名称:

gdb prog PID

There is no difference between doing that vs running gdb prog and then telling gdb attach pid . 这样做与运行gdb prog之间没有区别,然后告诉gdb attach pid

Use ps -ef | grep <your program> 使用ps -ef | grep <your program> ps -ef | grep <your program> to get the PID. ps -ef | grep <your program>获取PID。 Then run gdb <your program> <PID> . 然后运行gdb <your program> <PID> pstat is not a standard command. pstat不是标准命令。 I've only used it with Solaris. 我只在Solaris上使用它。

eg 例如

gayan@gayan:~/FE/bin> ./fe&
[1] 5866                                 
gayan@gayan:~/FE/bin> ps -ef | grep fe
gayan     5866  5836  2 10:19 pts/3    00:00:00 ./fe
gayan     5871  5836  0 10:19 pts/3    00:00:00 grep fe
gayan@gayan:~/FE/bin> gdb fe 5866
GNU gdb (GDB; openSUSE 11.1) 6.8.50.20081120-cvs       
Copyright (C) 2008 Free Software Foundation, Inc.      
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.           
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"   
and "show warranty" for details.                                             
This GDB was configured as "i586-suse-linux".                                
For bug reporting instructions, please see:                                  
<http://bugs.opensuse.org/>...                                               
Attaching to program: /home/gayan/FE/bin/fe, process 5866

The above was run on openSuse but should work on Ubuntu. 以上是在openSuse上运行但应该在Ubuntu上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM