doc_gdb

Embed Size (px)

DESCRIPTION

gdb commands, valgrind

Citation preview

gdb is used to debug the program excutablesgdb requires executable file as inputGDB was first written by Richard Stallman in 1986 as part of his GNU systemtgdb = gnu debuggergdb is terminal based debuggerto have gui(gdb) applications are Nemiver Kdbg DDD, b main Put a breakpoint at the beginning of the programb Put a breakpoint at the current lineb N Put a breakpoint at line Nb +N Put a breakpoint N lines down from the current lineb fn Put a breakpoint at the beginning of function "fn"d N delete breakpoint number Ninfo break list breakpointsr Run the program until a breakpoint or errorc continue running the program until the next breakpoint or errorf Run until the current function is finished (free run function)s run the next line of the programs N run the next N lines of the programn like s, but don't step into functions()u N run until you get N lines in front of the current linep var print the current value of the variable "var"bt print a stack traceup go up a level in the stackdown go down a level in the stackq Quit gdbinfo register = dump all registerr A start with command line argumentdon not step over ndo step in sstep out of function finishl 5 list 10 lines context around line no. 5list 4 same as l 5#to enable reverse-stepb mainrunrecordto stop recor write stop recordto see current line of execution frameto reverse the executeiob by one step reverse-step(i)reverse back to previous statement reverse next(i)reverse back to calling stetment reverse-finish(i)simple blank enter will fire privious command usedexplain1st slide==========|-> what is debugger(keil,turboc)|-> To resolve run time error (segmentation fault and exception)|-> To understand or decode programs logic|-> what is GDB|-> Who developed it?(1986)2nd slide=========|-> how to start with GDB|-> GDB needs compiled or executable file|-> command line GDB demands that both executable must be at same place|-> GUI GDB are Nemiver(c,c++),code-blocks,kDbg, xcode ,DDD etc.|-> GDB allows run time variable change and monitoring variable, register ,memory etc.|-> compile c,c++ file with '-g' flag on => Ex. gcc -g -o 3rd slide=========|-> start with exception file+>./0exeception.out+> gcc -g 0exception.c -o exception.out+> gdb ./0exception.out|-> explain=> gdb ./a.out=> q to quit=> r or run (restart)=> where=> bt (break trace)=> up and down (to change frames)4th slide=========|-> start with 1segment.c file+> gcc -g 1segment.c -o 1segment.out+> gdb ./1segment.out=> Breakpoints+> break (add break poins to next line)+> b n+> b +n +> b name+> info b+> d or d n or delete 3+> clear (will delete the upcomming breakpoint)+> enable or disable breakpoint=> continue or c=> step in+> s+> s n=> step over+> n+> n number=> list lines+> l n or lines n+> l listsize range+> l+> l function_name+> l 1,2 (range)5th slide==========|-> explain 2printf.c file without register +> gcc -g 2.printf.c -o 2printf.out+> gdb ./2printf.out|-> set break points(b mian,21,27,34,39)|-> info register|-> blank enter|-> frame|-> set variable from GDB=>p variable = value|-> watch or del (brakepoint no.)|-> p|-> diplay or undisplay|-> info locals|-> step out+> finish |-> record=> reverse-next (last step)=> reverse-step (last statement)=> reverse-finish (last calling)=> reverse-continue (last breakpoint)|-> untill to step over loop|-> explain with setting register type6th slide=========|-> advantage of GDB => step through code line by line=> exmines register values=> examine memory=> exmine call stacks and frames=> setting up breakpoints=> debugging segmentation fault=> watching variables and setting variables|-> disadvantage of GDB=> memory leak can not be detected|-> show nemiver 7th slide=========|-> valgrind allows memory leak detection|-> it is tool designed specially for memory debgging|-> run memory_leak.c program with gdb=> gcc -g memory_leak.c -o memory_leak.out=> GDB runs program without any error|-> run same program with valgrind=> gcc -g memory_leak.c -o memory_leak.out=> valgrind --leak-check=yes ./memory_leak.outor=> valgrind --tool=memcheck ./memory_leak.out=> ==process-id==|-> runt uninitialzed_variable.c file=> gcc -g uninitialized_variable.c -o uninitialized_variable.out=> valgrind --leak-check=yes ./uninitialized_variable.out|-> ng gdb with valgrindvalgrind --tool=memcheck --leak-check=yes --vgdb=yes --vgdb-error=0 ./uninitialized_variable.out|-> in another shell start program with gdb=>gdb ./uninitialized_variable.out+>(gdb) target remote | vgdb --pid=|-> in combination of valgrind and gdb 'run' command doesn't workbut use continue command to start up|-> this combination will produce memory leackage related error runtime. in the shell in which valgrind error is runing|-> explain massif(memory analysys tool ) with massif_.c program=>gcc -g massif_.c -o massif_.out=>valgrind --tool=massif ./massif_.out=>valgrind --tool=massif --time-unit=B ./massif_.out=> ms_print massif.out.=>valgrind --leak-check=full ./massif_.out|-> try it with freeing memory