-
gcc -g myprogram.c, compiling program with debugging option to let you use variables and function names inside GDB, instead of raw memory locations -
gdb a.outopensa.outin debugger, doesn't start running -
can run
helpto list topics, with whichhelp <topic>can be used for actual commands lists -
to run program in debugger, usually breakpoints are set before running
r ## to plainly run
r arg1 arg2 ## to pass arguments while running
r < somefile ## feed in a file with arguments
qto quit GDB
to trace path and reach point of origin for concern
-
llist 10 lines of code at current code point,l 25to use line:25 as center line,l myfuncto list the function -
nextrun until next line and pause, if current line is a function execute entire and then pause -
stepruns the next instruction, it dives into function if next instruction happens to be function -
finishfinish executing current function and pause, if wanna finish rest of function at once
to quickly enquire a certain code position, need to pause there
-
break 35sets breakpoint at line:35,break myfunctionto break atmyfunction -
watch x == 3sets a watchpoint onx == 3, pauses the program when condition changes -
continueresume run until hit another breakpoint/watchpoint -
delete Nto clear out a breakpoint
viewing and changing states at runtime, typically when paused
-
print xto show value of varx, compile need to have debug information to use real names -
set x = 3to changex to 3,set x = yto change value to variabley -
call myfunc()to call user-defined function,call urfunc(x)to call with arg; beware of calling buggy function -
display xto show value ofxafter every step or pause,undisplay xto stop the regular show
shows frame of a single function call when in program
-
btprints current stack,bt fullshows full backtrace with local variables -
upanddownto move to next or previous frame respectively -
returnto be back from current function
core dump can be read and give the line number of crash, argument passed and more
-
gdb myprogram coreto debug myprogram withcoreas dump file -
can use
btto examine details
events thrown at certain events, GDB might pause and can be ignored
-
handle <signalname> <action> -
varied levels of ingoring as
handle SIGUSR1 nostop
handle SIGUSR1 noprint
handle SIGUSR1 ignore
-
prefer watchpoints over breakpoints
-
printfworks well for tracing, wrapprintfin alogfunction for flexibility -
pass a log level with message (1 most important, 3 least, 0 off), tweak log function to persist errors
-
use
#define LOG_LEVEL LOG_WARNto display warning and above,#define LOG_LEVEL LOG_NONEto turn of debugging
#include
#define LOG_NONE 0
#define LOG_ERROR 1
#define LOG_WARN 2
#define LOG_INFO 3
#define LOG_LEVEL LOG_WARN
// shows msg if allowed by LOG_LEVEL
int log(char *msg, int level){
if (LOG_LEVEL >= level){
printf("LOG %d: %s\n", level, msg);
// could also log to file
}
return 0;
}
int main(int argc, char** argv){
printf("EHLO!\n");
log("Really bad error!", LOG_ERROR);
log("Warning, not so serious.", LOG_WARN);
log("Just some info, not that important.", LOG_INFO);
return 0;
}