Alright, enough theory. Time to actually debug something!
It seems only fitting that we debug the one program which I deliberately wrote with major (critical?) bugs.
To start, we navigate to our Program directory and run the command:
First, let’s run the program as we intended it. We simply tell gdb to “run”, and it acts normally.
Now we’ll run it normally, but in a way that we know it should not work. Specifically, we’ll feed a character string to the age input, and a super-long string to the last-name input.
So we know that didn’t work properly. Now let’s set a breakpoint at line 27.
Now we’ll run it again, but check on some of the inputs using print.
set variable age=28
Note: The debugger has a default variable named age. We have to use “set variable” instead of plain “set” to differentiate between the two.
It looks fine, so let’s skip on down with next.
“Bob was here” <ENTER>
Boring. Let’s just let it continue normally until the loop is over using until.
Yes, it’s the same screenshot three times in a row. It can be remarkably difficult to remember to hit the Print Screen button.
Boom! No more loop. Now let’s skip down a ways to the first name input.
Now we have filled all of our variables. Let’s look at last and last_copy before we compare them.
We know it will work right now, because last and last_copy agree. However, just for giggles, we should change last_copy using set.
set last_copy = “Hi Folks”
Now, look at that. We managed to make our code do a quick replacement. Now, we can see the error-tree execute properly.
That’s about all you need to know to get very familiar with gdb. Just use these techniques and Google everything you can’t quite figure out.