-
additional step when compiling, like
-gswitch forgccto enable built-in debugging support needed bygdb -
starting up
gdband load a program to debug using commandfileas(gdb) file myprog.x
can directly start with
gdb myprog.x
-
auto-complete words with TAB key, can check help with
help <command> -
runcommand runs the loaded program, in case of issues it shall show some facts about it -
can break program in middle with
break file1.c:15, if program ever reaches mentioned file:line pair it will pause and prompt -
to make gdb break at a function call use
break my_func
can use
runagain to continue after pause, to reach to next breakpoint usecontinuecan use
stepwith fine-grained control to proceed, similarlynextsingle-steps as well the next sub-routine instead just next instruction
-
print <var>command prints value ofvar,print/xprints value in hexadecimal -
set watchpoint on state like value of variables changing, example
(gdb) watch my_var, it will interrup run and print old/new values -
other useful commands
backtraceproduces a stack trace related to seg fault
whereshows the next statement to be executed, where seg fault occured
finishcompletes fun till current function's end
deleteto remove a breakpoint
info breakpointsshows info on all declared breakpoints
- using conditional breakpoints allow more fluidity, like
(gdb) break file1.c:6 if i >= SOMESIZE
- assume following struct
struct book {
int uuid;
char *name;
float prince;
int pages;
}
-
assume we are in gdb at some point in execution after a line that looks like
struct book * b1 = <somebook>; -
to see value of pointer
(gdb) print b1 -
checking particular field of struct by referencing
(gdb) print b1->uuid
(gdb) print b1->name
(gdb) print b1->price
(gdb) print b1->pages
- can also use
(gdb) print (*b1).uuid
(gdb) print (*b1).name
(gdb) print (*b1).price
(gdb) print (*b1).pages
-
to see entire content of struct
(gdb) print *b1 -
can follow pointers iteratively as
(gdb) print list_ptr->next->next->data, for something like linked list