gdb
Debug
// main.c
#include <stdio.h>
int doubleNum(int n)
{
int temp;
temp = 2*n;
return temp;
}
int main ()
{
int a = 10;
int b = doubleNum(a);
printf("%d\n", b);
return 0;
}
// Exercise 1
gcc -g main.c // generate the executable file a.out
gdb a.out // load the program into gdb
(gdb) list // list the first 10 lines of the code
(gdb) list // list the next 10 lines of the code
(gdb) run // run the program
(gdb) quit // quit gdb
// Exercise 3
gdb a.out // load the program into gdb
(gdb) list main // list the code of the main function
(gdb) break 15 // set up a breakpoint before line 15
(gdb) run // run program, pause at the breakpoint
(gdb) print a // print out the value of variable a
(gdb) whatis a // print out the data type of variable a
(gdb) next // run the current instruction and move to the start of the next instruction
(gdb) next
(gdb) print b
(gdb) next
(gdb) quit
// Exercise 3
gdb a.out // load the program into gdb
(gdb) list doubleNum
(gdb) break 8
(gdb) break 17
(gdb) info break
(gdb) run // run program, pause at line 8
(gdb) print temp
(gdb) continue // continue run program, pause at line 17
(gdb) print b
(gdb) continue
(gdb) info break
(gdb) disable 1 // disable the fist breakpoint
(gdb) info break
(gdb) run // run program, pause at line 17
(gdb) continue
(gdb) quit
// Exercise 4
gdb a.out // load the program into gdb
(gdb) l
(gdb) b 15 // setup a breakpoint before line 15
(gdb) r
(gdb) set var a = 100
(gdb) n
(gdb) print b
(gdb) c
(gdb) q
Makefile
// main.c
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
int main()
{
int *array = getArray(10);
change(array, 10);
return 0;
}
// util.h
#ifndef UTIL_H
#define UTIL_H
int *getArray(int size);
void change(int *array, int size);
#endif
// util.c
#include <stdlib.h>
#include "util.h"
int *getArray(int size)
{
int *ptr = (int *) malloc(size*sizeof(int));
for(int i = 0; i < size; i++)
ptr[i] = i;
return ptr;
}
void change(int *array, int size)
{
for(int i = 0; i < size; i++)
array[i] *= 10;
}
# Makefile
vpath %.h include
vpath %.c src
SRC = src
OBJECTIVES = main.o util.o
CFLAGS = -g -I include
CC = gcc
PROG = Array
$(PROG): $(OBJECTIVES)
$(CC) -o $(PROG) $(OBJECTIVES)
main.o: main.c util.h
$(CC) -c $(SRC)/main.c $(CFLAGS)
util.o: util.c util.h
$(CC) -c $(SRC)/util.c $(CFLAGS)
.PHONY: clean
clean:
-rm -f *.o $(PROG)
// Exercise 6
make
gdb Array // load the program into gdb
(gdb) l util.c:1 // list the code near to line 1 in util.c
(gdb) b 10 // setup a breakpoint at line 10 in util.c
(gdb) info b
(gdb) r
(gdb) p i
(gdb) p ptr[i]
(gdb) c
(gdb) c
(gdb) p i
(gdb) p ptr[i-1]
(gdb) q
Commands
gcc -g, compile the program to generate debug information
# open program with gdb
gdb executableFile, debug program
# check code
list or l, display next 10 lines of code
list lineNumber, display the code near the specified line number
list functionName, display the code of a function
list fileName:lineNumber, display the code in a specific file
# setup breakpoints
break or b + lineNumber, set up a breakpoint before a specific line
info b, display breakpoints
disable index, disable a breakpoint
enable index, enable a breakpoint
clear, clear all breakpoints
# run code
run or r, run program, will stop at a breakpoint
next or n, run next line of code, do not dive into function
step or s, run next instruction, dive into function
continue or c, continue run the code until next breakpoint
whatis i, check the data type of variable i
# quit gdb
quit or q
Reference