You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
3 years ago | |
---|---|---|
Makefile | 3 years ago | |
README.md | 3 years ago | |
main.c | 3 years ago |
README.md
title | date | author | documentclass | lang | papersize |
---|---|---|---|---|---|
Debuging with gdb | \today | Jasper Levin Spahl | scrartcl | en | a4 |
How to debug with gdb
Given we have as simple C program with a bug
/* main.c */
#include <stdio.h>
int main()
{
int balance = 100;
int target = 1000;
float rate = 0.1;
int yeat = 0;
do
{
float interest = balance * rate;
balance = balance + interest;
year++
} while ( balance >= target)
printf("%d No. of years to achieve target balance.\n", year);
return 0;
}
To find bug we first have to compile the program with
gcc -g -o program main.c
.
The -g
flag is used to compile the program with debug symbols.
Now that we have compiled the program we can open int gdb
Because we don't want to type all these long commands everytime we
compile we can use a Makefile
to make our lifes easyer.
# Makefile
program: main.c
gcc -g -o program main.c
debug: program
gdb program
Now writing make debug
in the terminal will compile the program and
open it in gdb.
To debug the program we first have to set a breakpoint
(gdb) b main
Breakpoint 1 at 0x1141: file main.c, line4.
To run the program in gdb type run
(gdb) run
Starting program: program
Breakpoint 1, main () at main.c:4
4 int balance = 100;
To step with s
(gdb) s
5 int target=1000;
(gdb) s
6 float rate = 0.1;
(gdb) s
7 int year = 0;
(gdb) s
10 float interest = balance * rate;
(gdb) s
11 balance = balance + interest;
(gdb) s
12 year++;
At line 12
we want to take a look at the variables.
The gdb command for that is p
(gdb) p balance
$1 = 110
(gdb) p rate
$2 = 0.100000001
(gdb) p interest
$3 = 10