7

I have a question about debugging a running C++ program in Linux. 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

gdb -p PID

In this case, what's the difference between this and attach PID?

2) We can use pstat, however, I am using Ubuntu, no pstat, but only mpstat

it seems that mpstat does not provide too much information, and not so many options.

3) check the details information under directory ./proc

In this case, just go to the directory with the PID. However, should this be done mannually?

4
  • I don't think I've ever seen the -p option. Attaching to a process by PID is plenty common though. What exactly is the problem with doing that? Commented Mar 18, 2010 at 3:16
  • I'm not sure what your question really is. You seem to already have working methods for debugging a C++ program, thus your title is self-answered, and then your post body asks something unrelated. Can you rephrase the title or your question to be clearer? Commented Mar 18, 2010 at 3:33
  • I've used strace -p PID to trace the system calls of a running C++ application. I found that a call to open() was failing and the error case wasn't being handled appropriately. Commented Mar 18, 2010 at 4:02
  • I think skydoor's problem lies in obtaining the PID of the running process. pstat is not available in Ubuntu and mpstat doesn't seem to give a process's PID (correct me if I'm wrong since I don't have a way to verify this) Commented Mar 18, 2010 at 5:15

3 Answers 3

2

I can't find -p option in gdb man or their documentation, but it does work! I've tried it many times with older versions on RedHat and 7.0.1 on Debian.

I'm not sure how exactly it finds the exe by PID (maybe /proc/<PID>/exe), but it does. 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>.

I personally prefer ps xa| grep myprogram for getting the PID

Sign up to request clarification or add additional context in comments.

Comments

1

In regards to technique 1, there is no -p flag and you still need the name of the program:

gdb prog PID

There is no difference between doing that vs running gdb prog and then telling gdb attach pid.

Comments

1

Use ps -ef | grep <your program> to get the PID. Then run gdb <your program> <PID>. pstat is not a standard command. I've only used it with Solaris.

e.g.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.