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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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
slide-2
SLIDE 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 eg. 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

¡ ¡ ¡

slide-3
SLIDE 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(); std::cin >> bad; input = bad;

Type in “step” until you get to this line

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 print_message(); std::cin >> bad; input = bad;

Type in “print input”. The variable input should be an empty string.

for(i = input.length(); i >= 0; i--){ std::cout << input[i]; } std::cout << std::endl; return 0; }

15

slide-4
SLIDE 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; for(i = input.length(); i

Restart the program and step

>=

  • v0

e; r p

i

r- in

  • t_

){

message() this time

std::cout << } input[i];

by using “step” instead of “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; input = bad;

At this point, the debugger is expecting you to type something in, so type in a

for(i = input.length(); i >= 0; i--){

string.

std::cout << input[i]; } std::cout << return 0;

The program

std::endl;

will crash at this point

}

17

slide-5
SLIDE 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

slide-6
SLIDE 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