$25
Console Debugging
Overview
In most cases, you’ve written C/C++ code that was definitely broken, and it led to hours of hair pulling, and mundane searching for a simple issue that could have been easily identified by larger scaled debugging tools. Well thanks to GDB, there’s less monotony! This exercise will teach you the fundamental, and very powerful tools that the GNU Debugger offers. You will analyze, deconstruct, and debug given programs to help you learn the necessary skills of using a console debugger that will help you with future projects, and any code that you will write. At the end of the exercise, you’ll submit several annotated screenshots.
Structure
This exercise will follow this basic structure:
1) Install GDB 9.1 and its utilities
2) Research on how to utilize GDB and its commands, a helpful link will be provided
3) Create a simple program, and play around with GDB’s features
4) Debug process.c and explain the issue encountered / what it was trying to do 5) Analyze password by digging through its code in order to obtain the key
Installation (GDB)
Within Reptilian, use the apt-get command to update the Linux package list, and install GDB
$ sudo apt update
$ sudo apt install gdb
Test GDB to make sure that it’s the correct version by prompting it:
$ gdb -version
Your output should look something like this
Utilizing GDB
Utilizing the debugger is actually quite simple, in order to start up the debugger on a sample program myProgram we would type:
$ gdb myProgram
From there, we can learn more about GDB and how to use it by keying:
(gdb) help
We highly suggest you reference the bottom link to the GDB manual, and the help command if you ever get stuck, or want to learn more about its features. Using the debugging tool follows a simple guideline that will be outlined below:
• Run the program and analyze what it’s doing
• Set up necessary breakpoints, and step through the program
• Find the issues utilizing the various tools that GDB offers
Running
To run the code, simply key in:
(gdb) run
This will run the code as your normal shell would, except it will point out several helpful debugging hints whenever you compile your program with the “-g” flag.
Breaking
After running the code, you may choose an address, or function name in which you want to place your breakpoint:
(gdb) break main
This will place a breakpoint inside the program at the address of main, and from there, you will be able to step through the code, or run whatever code the method contains
Stepping
The most useful command next to creating breakpoints, stepping allows the user to step through each line of code to find what the outputs are, and what possible issues the code contains:
(gdb) step
After stepping through the program, you may delete the breakpoints that are not necessary to you
(gdb) delete
Useful Commands
- run
- break
- step
- delete
- next
- up
- down
- print
- info
- quit
Application
Now that we have learned some basic commands, it’s time to apply our newfound knowledge to debug some programs that you will make, and that we provide
Part 1 – Simple Program
Create your own “Hello World” program and play around with the capabilities of GDB. When you’re done, take a screenshot of your program being run in GDB with a breakpoint being set in main.
Compiling your program uses these commands:
$ gcc -o myProggy.o myProggy.c
$ gcc -g -o myProggy.o myProggy.c
Part 2 – Process Table
Download the process.c code from the source folder of the exercise and compile it with the commands given at the bottom of this page. This program emulates a (very) rudimentary process table, however, there’s a catch. Inside the program is a bug, and you must use the GDB to find the issue.
When you’re done, you’re going to take a screenshot of the issue in GDB, explain why it broke the code (in a canvas submission comment), fix the code (only one line), and submit another screenshot of the working code’s output.
To compile process.c
$ gcc -w -g -o process process.c -lpthread -lrt
Part 3 – Password
Download the source file password from the same folder as process.c. As you will notice, this file is an output, and it was compiled without debugging symbols. In order to complete this part, you will need to use GDB to find a password that is hidden WITHIN the program itself (you should start by decompiling that code). Look into the info of the program to see what it contains. See if you can track its motion through compilation. You will take a screenshot of where you found the password before decoding it, and the message received after typing in the correct password.
Hint: Use a HEX translator, like the one provided at the bottom of the page