Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-15-2004, 06:19 AM
|
#1
|
LQ Newbie
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10
Rep:
|
How to programmatically break into gdb from gcc C++ source
When I'm developing a C++ program and debugging under gdb, I want to be able to get the program to fall into gdb.
In MS Visual Studio you can do this from a C++ app using asm { int 3 }
How, if at all, can you do this with gcc/gdb ?
|
|
|
09-15-2004, 08:16 AM
|
#2
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
There is a way to attach the debugger process explicitly (I've seen it done but have no idea how to do it).
If you're starting from the debugger, then you can signal() your own process with SIGINT to interrupt it and return control to the debugger, effectively a breakpoint. That won't work very well if you're doing signal-handling though. See the signal manpage for more.
|
|
|
09-15-2004, 09:05 AM
|
#3
|
LQ Newbie
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10
Original Poster
Rep:
|
Basically what I want is for the program to cause a break into the debugger. E.g. is MS Visual Studio I can have a function like this:
void call_debugger()
{
_asm{ int 3 };
}
which breaks into the debugger. Ie has the same effect as a breakpoint set in the debugger itself.
This is incredibly useful and I cant believe this is not possible in gcc/gdb, yet I havent found out how.
|
|
|
09-15-2004, 09:09 AM
|
#4
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
Sending a signal of SIGINT to your own PID will do this. You just need to run the program from inside gdb initially, with the run command.
|
|
|
09-15-2004, 02:42 PM
|
#5
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
from the top of my head:
you start the program in gdb :
then you make a break at the line that you would like to break
then you run again
everything inside gdb
i think it works.
sorry i have no ressources to test it.
regards
slackie1000
ps. if i misunderstood your question is due the lack of english knowledge. 
|
|
|
09-15-2004, 03:52 PM
|
#6
|
Member
Registered: May 2002
Posts: 964
Rep:
|
It's a one-line in any Unix
try:
Code:
#include <signal.h>
#define BREAK_HERE raise(SIGINT)
int main(int argc, char *argv[]){
int i,j;
for(j=0,i=0;i<20;i++)
{
j+=i;
if(j>20)
BREAK_HERE;
}
return 0;
}
at the command line:
Code:
gdb mycode
gdb> run
Program received signal SIGINT, Interrupt.
0x705b28a0 in kill () from /usr/lib/libc.2
... you are now in the debugger right after the BREAK_HERE statement
You will have to step twice to come back out of the raise() call into your code.
|
|
|
09-15-2004, 05:23 PM
|
#7
|
LQ Newbie
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10
Original Poster
Rep:
|
Thanks - just what I wanted. Knew it must be possible somehow.
|
|
|
09-16-2004, 05:15 AM
|
#8
|
LQ Newbie
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10
Original Poster
Rep:
|
Just a note on this. I'm using ddd, and I find that after the SIGINT, I have to use 'stepi' 4 times (ie step 4 instructions), and then 'step' once (i.e step to next source line). This gets ddd synchronised with the source line.
Otherwise I get "Cannot find bounds of current function" errors in ddd.
Would be nice if there was a way to get ddd to go straight to the offending source line on the SIGINT, but I can live with this.
|
|
|
All times are GMT -5. The time now is 08:24 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|