# GDB Tutorial

In this tutorial, we will walk through some of the features of `gdb` by running a program called `highscore`.

First get a copy of the program by running the following commands in your CS Linux account.

``````cd ~/cs224
wget https://cs224.cs.vassar.edu/labs/highscore.tar
tar xf highscore.tar
cd highscore
``````

Try running the program.

``````./highscore
``````

The program takes as input four integers. The goal is to find the four numbers that gives you the highest possible score (100).

``````./highscore 1 2 3 4
You entered the integers 1, 2, 3, and 4.
``````

To solve this problem we are going to need to figure out how the program works. We can start by looking at `highscore.c`.

``````#include <stdio.h>
#include <stdlib.h>

#include "score.h"

int main(int argc, char *argv[])
{
long i, j, k, l, s;

/* Make sure that we have exactly 5 arguments: the name of the
executable, and 4 numbers */
if (argc != 5) {
printf("Usage: %s i j k l\n", argv);
printf("where i,j,k,l are integers.\n");
printf("Try to get as high a score as you can.\n");
exit(8);
}

/* Convert the inputs to ints */
i = strtol(argv, NULL, 0);
j = strtol(argv, NULL, 0);
k = strtol(argv, NULL, 0);
l = strtol(argv, NULL, 0);

printf("You entered the integers %ld, %ld, %ld, and %ld.\n", i, j, k, l);

s = score1(i);
s += score2(j);
s += score3(k);
s += score4(l);

return 0;
}
``````

Unfortunately, you only have source code for the `main()` function and not the `score` functions. But no worries! We’ll figure out what is going on by looking at the assembly version of those functions.

To do this, we are going to use `gdb` the Gnu Debugger.

To run under the debugger, use the following command:

``````gdb -tui highscore
``````

This starts the debugger in “text UI mode”. The screen is divided into two parts. The top part shows you the the C code that we have provided for you, and the bottom half shows you the command interface to the debugger. This window allows you to enter `gdb` commands to run and step through your program.

Now run the program from within `gdb` with the `run` command.

``````(gdb) run 1 2 3 4
``````

What comes after the `run` command are the command line arguments that are passed into the program.

You should have got an output that looked something like this:

``````(gdb) run 1 2 3 4
Starting program: /home/jawaterman/cs224/highscore/highscore 1 2 3 4
[Inferior 1 (process 18273) exited normally]
(gdb)
``````

Let’s set a breakpoint and run our program and run our program again.

``````(gdb) b main
(gdb) run 1 2 3 4
``````

This time the program didn’t run all the way. You output should look something like this:

``````(gdb) b main
Breakpoint 1 at 0x4005d6: file highscore.c, line 11.
(gdb) run 1 2 3 4
Starting program: /home/jawaterman/cmpu-224_sp22/labs/highscore/highscore 1 2 3 4

Breakpoint 1, main (argc=5, argv=0x7fffffffe108) at highscore.c:11
(gdb)
``````

When we ran our program this time, it stopped at the start of main. `gdb` is now waiting for us to enter a gdb command.

Execute one line of the program with the `next` (n) command.

Let’s also set a breakpoint at `score1` function.

``````(gdb) n
``````