Is it possible to add GDB breakpoints in a C files, before compilation? Currently, I have to specify breakpoints after compilation through GDB.
I'm looking for something similar to JavaScript's debugger;
statement.
Is it possible to add GDB breakpoints in a C files, before compilation? Currently, I have to specify breakpoints after compilation through GDB.
I'm looking for something similar to JavaScript's debugger;
statement.
Not as such, but you can insert some helpful function calls:
One option is to create a function:
void break_here ()
{
/* do nothing */
}
then call it wherever you want, but be careful it doesn't get inlined (put it in a different file, or add a "noinline" attribute).
Then, in GDB, just set a breakpoint on break_here
, and you're done. If you find it tedious to set that breakpoint every time, you can create a file named .gdbinit
in your home directory or in the current working directory that contains the breakpoint command.
Another option that works on Linux is to use a signal:
raise (SIGUSR1);
You could use any signal you like, but it's better to use one that doesn't kill your program (although you can configure GDB to not pass them onto your program, if you choose).
I'm not aware of a way to directly set a breakpoint, but GDB will catch interrupts, so that is one possible solution.
Something like this should work. You'll need to include signal.h:
raise(SIGABRT);
on linux, which I'm assuming you're on since you're using gdb you could make a script to do it with comments in the code
#!/bin/bash
#debug: Script to run programs in gdb with comments to specify break
points
echo "file ./a.out" > run
grep -nrIH "/\*GDB\*/" |
sed "s/\(^[^:]\+:[^:]\+\):.*$/\1/g" |
awk '{print "b" " " $1 }'|
grep -v $(echo $0| sed "s/.*\///g") >> run
gdb --init-command ./run -ex=r
exit 0
then wherever you need a breakpoint, just add /*GDB*/
, the script will use grep to find the line numbers and files with those comments, and set a break point on each line of each file it finds them on, then start gdb