Homemade Debugger
You'll use ptrace to stop another program at a line you chose, step it, read its registers and memory, and print a backtrace, so gdb stops being magic.
About
A debugger is a process that asks the kernel for permission to meddle in another one, and the call is ptrace. You'll launch a program under your control, patch an int3 into its code to make a breakpoint, step one instruction at a time, read its registers and memory, and finally translate an address back to a line in the source using the DWARF info the compiler left behind. Linux is the easy platform for this (macOS wants code signing and entitlements), so use a VM if you need to. The DWARF reading is where the season goes. The details here are x86-64; on an ARM machine like a Raspberry Pi the breakpoint instruction is brk and the registers come through PTRACE_GETREGSET, and the rest is the same.
At a glance
- Per requirement
- 50 XP
- Completion bonus
- 600 XP
- Requirements
- 6
- Takes
- A season
- Skill level
- Advanced (years of practice)
- Cost
- Free
- Supplies
- You need some
- Where
- At home
- Other people
- On your own
Requirements
-
1
Launch a program under ptrace and stop it at its first instruction. Fork, call ptrace with PTRACE_TRACEME in the child, exec the target, and waitpid in the parent. The child stops before running a line of its own. Print its PID and the instruction pointer from PTRACE_GETREGS. Take a screenshot of the output. Photo
-
2
Set a breakpoint at an address by writing an int3 and restoring the byte when it's hit. Read the word at the address with PEEKTEXT, save it, write 0xCC into the low byte, and continue. When the child stops, put the byte back, move the instruction pointer back by one, and you're there. On ARM64 the program counter already points at the breakpoint, so skip the step back. Take a screenshot of the child stopping at an address you chose. Photo
-
3
Single-step and print the registers after each instruction for twenty steps. Use PTRACE_SINGLESTEP and GETREGS in a loop. Compare the instruction pointer against objdump output so you can tell which instruction you're on. Take a screenshot of the twenty register dumps next to the disassembly. Photo
-
4
Read and write a variable in the child's memory by address. Find a global's address with nm, read it with PEEKDATA, change it with POKEDATA, and let the program continue to print the new value. Take a screenshot of the program printing the value you set. Photo
-
5
Parse enough DWARF to map an address to a file and line, and set a breakpoint by line number. Use libdwarf or libelfin rather than parsing it raw, unless you want to. Accept a command like break main.c:12 and show it stops there with the source line printed. Take a screenshot of the stop with the line. Photo
-
6
Debug a real crash from one of your earlier programs with your tool and print a backtrace. Build the crasher with -fno-omit-frame-pointer, take a segfault from the Pointer Walk or Leak Free badge, run it under your debugger, and walk the frame pointers to print the call stack with function names. Then write what gdb does that yours doesn't. Film a short clip of the session. Video