Lecture 21 GDB

Joseph Haugh

University of New Mexico

Free Recall

Review Quiz Question 1: Hexadecimal

Which of the following hexadecimal number is the largest?

  1. 0xDEADBEEF
  2. 0xFEEDFACE
  3. 0xBADF00D
  4. 0xDEADC0DE
  5. 0xFACADE

Review Quiz Question 1: Hexadecimal

Which of the following hexadecimal number is the largest?

  1. 0xFEEDFACE

Review Quiz Question 2: Hex to Binary

How many ones appear in the binary representation of the hexadecimal value 0x01239ACF?

Hex 0 1 2 3 9 A C F
Binary 0000 0001 0010 0011 1001 1010 1100 1111
Ones 0 1 1 2 2 2 2 4
Total 14

Review Quiz Question 3: Quicksort

void quicksort(int v[], 
               int left, int right) {
  int i, last;
  printArray(v, left, right);
  if(left >= right) return;
  swap(v, left, (left+right)/2);
  last = left;
  for (i = left+1; i <= right; i++) { 
    if(v[i] < v[left]) { 
      last++;
      swap(v, last, i);
      printArray(v, left, right);
    }
  }
  swap(v, left, last);
  quicksort(v, left, last-1);
  quicksort(v, last+1, right);
}

If output from line 4 is [75 62 33 41 24], what is the output the next time line 12 is reached?

  1. [33 62 75 24 41]
  2. [33 24 75 41 62]
  3. [33 62 24 41 75]
  4. [33 41 24 63 75]
  5. [33 24 41 62 75]

Review Quiz Question 3: Quicksort

void quicksort(int v[], 
               int left, int right) {
  int i, last;
  printArray(v, left, right);
  if(left >= right) return;
  swap(v, left, (left+right)/2);
  last = left;
  for (i = left+1; i <= right; i++) { 
    if(v[i] < v[left]) { 
      last++;
      swap(v, last, i);
      printArray(v, left, right);
    }
  }
  swap(v, left, last);
  quicksort(v, left, last-1);
  quicksort(v, last+1, right);
}

If output from line 4 is [75 62 33 41 24], what is the output the next time line 12 is reached?

  1. [33 24 75 41 62]

What is GDB?

  • Stands for the GNU debugger
  • Allows you to inspect the during execution
  • Works for several languages, including C and C++

Compiling for Debugging

When you are going to use the debugger, compile your code with the -g option to include debugging information in your executable.

gcc -g -o myprog myprog.c

or with all the flags the standard demands:

gcc -Wall -ansi -pedantic -g -o myprog myprog.c

Starting GDB

  • Generally, you’ll start gdb specifying the program to debug.
> gdb myprog
(gdb)
  • Alternatively, you can specify the program after starting the debugger.
> gdb
(gdb) file myprog
  • Use the quit command to exit.

Getting Help With GDB Commands

  • gdb is an interactive shell, similar to the shell you use in a linux terminal.
    • Recall history with arrow keys
    • Auto-complete with TAB
    • Give short versions of commands
  • If you need more information while using the debugger, use the help command.
  • For information on a particular command, use help commandname

Running the Program

  • Run the program with the run command.
  • You can give command line arguments to the program here.
  • If program runs normally outside of the debugger, it should run fine here too.
  • If program crashes, you’ll get useful information about where it crashed.

Segfault Example

int main() {
  char *str = "value";
  int i;

  str[3] = 'x';

  for(i = 0; i < 5; i++) {
    printf("%c\n", str[i]);
  }
  return 0;
}
Program received signal SIGSEGV, Segmentation fault.
main () at str-broken.c:7
7         str[3] = 'x';

Breakpoints

  • You can set a breakpoint at a given line or function with the break command.
    • break 21
    • break myfile.c:32
    • break myfunction
  • You can set as many breakpoints as you want.
  • If the program reaches a breakpoint while running, it will pause and prompt you for another command.

Reached a breakpoint, now what?

  • Resume until next breakpoint with continue
  • Use step to execute the next line of code, possible entering another function.
  • Use next to execute the next line of code, treating the function call as a single line.

Removing Breakpoints

  • To remove a breakpoint first run: info break to view breakpoints
  • Then del 2 to delete the second breakpoint

Inspecting Data

  • The print command prints the value of an expression.
  • Use to inspect value of variables.
  • Can dereference pointers, access array elements, etc.

Where am I?

  • Use list to display source around the currently suspended line.
  • Use backtrace to show the current stack.

Watchpoints

  • Watchpoints pause the program whenever a watched variable’s value is modified.
  • Use watch myvar to start watching variable named myvar
  • Whenever myvar’s value changes, the program will pause and print out the old and new values.

Conditional Breakpoints

  • Perhaps you know the problem only happens under a certain condition.
  • You can create a conditional breakpoint that will only trigger a condition is true.
  • (gdb) break 6 if i == 10 will pause on line 6 only if the value of the variable i is equal to 10.

Wrong Result Example

int factorial(int n) {
  int result = 1;
  while(n--) {
    result *= n;
  }
  return result;
}

int main() {
  int n = 5;
  int fact = factorial(5);
  printf("%d! = %d\n", n, fact);
  return 0;
}

Recursive Example

int fib(int n) {
  if(n < 2) return 1;
  else return fib(n-1) + fib(n-2);
}

int main() {
  int n = 5;
  printf("fib(%d) = %d\n", n, fib(n));
  return 0;
}

Other GDB Interfaces

Sometimes, you want a friendlier display when debugging.

  • You can use the GDB Terminal User Interface (TUI) to show separate windows in your terminal.
    • gdb -tui will enable TUI mode.
    • Alternatively, if you have already started gdb, turn on with command tui enable