LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-15-2004, 06:19 AM   #1
cj12345
LQ Newbie
 
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10

Rep: Reputation: 0
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 ?
 
Old 09-15-2004, 08:16 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
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.
 
Old 09-15-2004, 09:05 AM   #3
cj12345
LQ Newbie
 
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 09-15-2004, 09:09 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
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.
 
Old 09-15-2004, 02:42 PM   #5
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,

from the top of my head:

you start the program in gdb :
Code:
run
then you make a break at the line that you would like to break
Code:
 break $line_nr
then you run again
Code:
run
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.
 
Old 09-15-2004, 03:52 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 09-15-2004, 05:23 PM   #7
cj12345
LQ Newbie
 
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks - just what I wanted. Knew it must be possible somehow.
 
Old 09-16-2004, 05:15 AM   #8
cj12345
LQ Newbie
 
Registered: Sep 2004
Location: cambridge, england
Distribution: suse, mandrake
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
normal gdb and spec gdb for kgdb Igor007 Linux - Newbie 1 09-23-2005 01:41 PM
GCC GDB error codes morph_ind Programming 3 07-07-2005 12:57 AM
gcc gdb errno & rpm difficulties jg167 Fedora 0 10-26-2004 04:46 AM
GCC, GDB GUIs Geeky_Kid Programming 4 12-28-2003 03:42 AM
can't find source files / line numbers with gdb dnijaguar Linux - Software 0 07-14-2003 01:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration