Awk Saves the Day

Awk saved the day during some serious bit-twiddling debugging last week. I was at a client's location trying to debug the bootloader code on a custom Linux board, and the gdb debugger couldn't talk to the JTAG debugging device correctly. All I could get out was the CPU program counter and the CPU registers after single stepping by each assembly instruction.

Something like:

PC = 17800000, CPSR = 600001D3 (SVC mode, ARM FIQ dis. IRQ dis.)
R0 = 0091FA94, R1 = 00000001, R2 = 00000018, R3 = 17800000

etc.

With an annotated assembly source listing I could manually compare the program counter with the assembly code (and the C code that generated it) and go through line by line.

But I also knew there had to be some way to make the computer do that work for me and save a serious number of keystrokes.

Eventually I found a solution:

All of that was a bash one-liner:

tail -f gdb.log | awk '/PC =/ { sub(",", "", $3); print $3; system("grep-C3 ^" tolower($3) ": ~/path/to/code.S") }'

I was pretty happy with that, because it saved me a ton of time.

Turns out the bootloader was running thousands of instructions before dying, which was much later than we thought.

(Normally GDB will tell you all of that information and much more with a much nicer interface, but as I said, something was seriously broken with the setup and I needed a workaround quick.)

© Copyright 2024, Remington Furman

blog@remcycles.net

@remcycles@subdued.social