top | item 9560708

Learning C with gdb

238 points| luu | 11 years ago |recurse.com | reply

24 comments

order
[+] justinmk|11 years ago|reply
The very last line mentions:

> Check out gdb’s “tui” mode

But that should be in the first paragraph. TUI mode shows the source code and the current line. This is like the difference between `ed` and `vi`: you can see the source code that you're debugging instead of mashing `bt` or whatever again and again.

To use gdb's TUI mode, simply include the `-tui` argument at the command line:

    gdb -tui path/to/bin
or, at any time while running gdb, press `ctrl-x ctrl-a`.
[+] ge0rg|11 years ago|reply
Wow, -tui is a real game-changer. I've struggled with tools like ddd and nemiver, looking for a sensible UI, and it was there all along!
[+] nhebb|11 years ago|reply
One of the assignments I enjoyed the most was the "bomb lab". The bomb is a C program (w/ some Assembly required) that you have to defuse at certain phases in order for it to run to completion. It felt like a game more than an assignment, using and learning gdb along the way.

This isn't the oourse I took, but here's a good example of the lab: http://condor.depaul.edu/glancast/373class/docs/lab2.html

[+] adamnemecek|11 years ago|reply
Another indispensable tool is this site http://gcc.godbolt.org
[+] agumonkey|11 years ago|reply
Never seen before, very nice to be able to enjoy the differences between optimization flags. -Os makes a big difference.
[+] castratikron|11 years ago|reply
You can also use the -S flag of gcc to output assembly instead of binary, or if the program has already been compiled you can use objdump on it to read the assembly alongside the source code.
[+] dmix|11 years ago|reply
If you like this, coursera has a great course called the "Hardware Software Interface".

http://coursera.org/course/hwswinterface

The course takes a similar approach of starting with analyzing how C interacts with memory.

[+] jplahn|11 years ago|reply
Interesting resource! I like this take on learning C.

On another note, I LOVE gdb. One of my prof's begged and pleaded for us to use gdb with all of our assignments because he claimed it would make life easier. For our first two computer org classes, you might be able to skate by without it (except for the buffer bomb and binary bomb), but once you reached the third semester and beyond, those that didn't spend a little time acclimating themselves with gdb began to hate life.

I really do love gdb. It saved my ass more times than I can remember.

[+] userbinator|11 years ago|reply
I have a feeling that it was your first debugger, and that you've not used any others...
[+] kjak|11 years ago|reply
I taught myself C as a young teenager (some time ago), only to much later learn languages with REPLs like Ruby, Scheme and Common Lisp. Using gdb in this way would have been an interesting learning experience... because back then I didn't know there was a such thing as a REPL!
[+] cubano|11 years ago|reply
This article reminded me of all the nasty headaches that C caused me back in the 80's, especially with how it handles arrays and memory management.

Say what you will about PHP but even with all the inconsistencies of the language and syntax, the way it handles complex arrays is nothing short of magical when coming from a C background.

[+] nailer|11 years ago|reply
That's really nice, being able to 'stop and look around' in ipdb for Python, node inspector for node etc has made those respectively so much easier to understand. Being able to do something similar for C is great.
[+] bpicolo|11 years ago|reply
I mean, the initial release of gdb was 29 years ago ;)
[+] sydcanem|11 years ago|reply
If you're in osx, Xcode has a nice debugger for c programs. Did I say you can also add breakpoints line by line? :D
[+] umanwizard|11 years ago|reply
I'm not a C expert, but I think you can prevent your assignment from being optimized away by declaring the variable as "volatile".

Edit: -O0 to disable most optimizations is also helpful when debugging.