to use it you must compile your code with the g option
play

To use it, you must compile your code with the -g option CXXFLAGS - PDF document

To use it, you must compile your code with the -g option CXXFLAGS += -g g++ -g debug.cpp -o debug To run, type in gdb ./debug After typing in gdb ./debug you will enter the debugger Type run and it will run


  1. ¡ ¡ • To use it, you must compile your code with the -g option CXXFLAGS += -g g++ -g debug.cpp -o debug • To run, type in “gdb ./debug” • After typing in “gdb ./debug” you will enter the debugger • Type run and it will run your program from the beginning • If your program takes command line arguments you can type: run <command line arguments> eg. run x y • Runs all the way through, so it’s not very interesting • You want to add breakpoints to stop the program at strategic places to inspect things • Type in break main to add a breakpoint at the start of the main function Ways to add breakpoints 1. Add a breakpoint at a function: ¡ break <function name> eg. break main 2. Add a breakpoint at a line in the current file: break <line number> eg. break 6

  2. 3. Add a breakpoint at a line in a specified file: break <file>:<line number> eg. break debug.cpp:6 • Breakpoints are numbered based on when they were created • To see all the breakpoints: info breakpoints • To delete a breakpoint, you need to know its number delete 1 • To delete all breakpoints: delete Other commands • print <variable>: prints the value of a variable e g. print i • step: execute the next line of code and goes into the function call • next: execute the next line of code but doesn’t go into the function call • continue: resume execution from where you stopped. Note: run runs the program from the beginning • quit: quits gdb • bt: prints the call stack of functions value of a variable • up: moves up the call stack • down: moves down the call stack ¡ ¡ ¡

  3. Running through debug.cpp int main(int argc, char **argv){ char *bad; std::string input; First, start the gdb. Type in int i = 0; “break main” and then run. print_message(); Type in “step” until you get to std::cin >> bad; this line input = bad; for(i = input.length(); i >= 0; i--){ std::cout << input[i]; } std::cout << std::endl; return 0; } 14 ¡ ¡ ¡ ¡ ¡ ¡ ¡ int main(int argc, char **argv){ char *bad; std::string input; Type in “print i”. The variable i int i = 0; should be 0 Type in “print input”. The print_message(); variable input should be an std::cin >> bad; empty string. input = bad; for(i = input.length(); i >= 0; i--){ std::cout << input[i]; } std::cout << std::endl; return 0; } 15 ¡ ¡ ¡ ¡ ¡

  4. int main(int argc, char **argv){ char *bad; std::string input; int i = 0; Type in “step” to go into print_message(). Once inside print_message(), type in “bt” to print_message(); see the call stack. std::cin >> bad; input = bad; Restart the program and step >= ov 0 e ; i r - - ){ for(i = input.length(); i r p in t_ message() this time input[i]; by using “step” instead of std::cout << “next”. } std::cout << std::endl; return 0; } 16 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ int main(int argc, char **argv){ char *bad; std::string input; Get to the std::cin line and type int i = 0; “step” again. print_message(); std::cin >> bad; At this point, the debugger is input = bad; expecting you to type something in, so type in a for(i = input.length(); i >= 0; i--){ string. std::cout << input[i]; } The program std::endl; std::cout << will crash at return 0; this point } 17 ¡ ¡ ¡ ¡ ¡ ¡ ¡

  5. ¡ What went wrong? • No memory allocated for the bad array • Fix by declaring bad to be: char bad[100]; • And not char* bad; • Now restart the program, put a breakpoint at line 16 and run • Type in “step” to make sure the line doesn’t crash • You can inspect the input array – Type in print input to see the whole string – Type in print input[i] (notice i is a variable) to see the ith element in the array • Note: hitting return re-executes the previous command • If you get the following error message: Missing separate debuginfos, use: debuginfo-install boost-date-time-1.41.0-17.el6_4.i686 • To fix, type in: sudo debuginfo-install –nogpgcheck –enablerep=debug glibc-2.12

  6. 1.80.el6_3.7.i686 libgcc-4.4.6-4.el6.i686 libstdc++-4.4.6-4.el6.i686 ¡ Other resources If you want to find out more about gdb commnds: • http://www.yolinux.com/TUTORIALS/GDBCommands. html • http://web.cecs.pdx.edu/~jrb/cs201/lectures/handouts/gdbcomm.txt

Recommend


More recommend