lab4_debugging
lab4_debugging
1 Introduction
Your task is to:
3. Write a corrected version of the program in C that compiles and runs correctly (no segmen-
tation faults or incorrect results).
2 Lab Setup
Files Provided in the tar file
• mystery1 (compiled binary; the debug information is kept intact. "layout next" will show the
actual program)
No source files are provided. You will have to reverse-engineer, debug, and recreate the
.c code from what you discover with GDB (and from observed runtime behavior).
3 Lab Outline
3.1 Part 1: Analyzing mystery1
Hints/Clues
• Pointer Arithmetic Issue: The program might be reading or writing one element too far.
1
Instructions
1. Run mystery1 without arguments first. See if it crashes or outputs an error.
2. Start GDB:
$ gdb ./mystery1
3. Look for main: break main, run, step into suspicious calls or loops.
5. Once you identify the bug, try to deduce what the code was supposed to do. Write a
new mystery1_fixed.c with the corrected pointer usage.
6. Compile, run, and test your mystery1_fixed to ensure it produces correct results (or at least
no crash).
Instructions
1. Run mystery2 in GDB.
$ gdb ./mystery2
2. Set breakpoints inside loops or suspect functions (use disassemble main to guess the loop
area or info functions to find function boundaries).
5. Create mystery2_fixed.c, ensuring you handle the array within correct boundaries.
2
Instructions
1. Execute mystery3 to see if it prints partial results or crashes.
4. Compare with how they’re used. If, for example, a function expects three parameters but you
only see two in info args, that’s a clue.
5. Investigate the function’s prototype vs. usage. Possibly they used int but it’s returning
char*, etc.
Instructions
1. Again, gdb ./mystery4, break main, run.
2. Look for suspicious pointer increments, out-of-bounds pointer usage, or incorrectly declared
function parameters.
4. If it segfaults, use backtrace, then examine local variables and memory near the address that
caused the crash (x/10x address).
• Examine memory: x/16bx &array to see 16 bytes in hex from array’s address.
• Disassemble: If you get stuck, do disassemble /m main to see assembly interleaved with
(best-guess) source lines. This can reveal function calls and indexing logic.
3
– finish runs until the current function returns.
• Document your process: Write notes on how you discovered each bug, which lines in your
final source code fix them, etc.
By reconstructing and fixing each program, you’ll practice real-world skills that blend reverse-
engineering and C debugging. Have fun!