Hacker News new | past | comments | ask | show | jobs | submit login
Learning C with gdb (recurse.com)
238 points by luu on May 17, 2015 | hide | past | favorite | 24 comments



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`.


tui mode is great. I would recommend this tutorial by beej [0] as an intro to gdb for those unfamiliar with it, it also has a very handy cheatsheet at the bottom [1].

[0] http://beej.us/guide/bggdb/

[1] http://beej.us/guide/bggdb/#qref


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!


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


And if that kind of "game" is your thing, you may also enjoy https://microcorruption.com


Another indispensable tool is this site http://gcc.godbolt.org


Never seen before, very nice to be able to enjoy the differences between optimization flags. -Os makes a big difference.


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.


I'm well aware of that. This is much more interactive though. The color coding is also dank.


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.


One might also be interested in the basics of the Acid debugger: http://doc.cat-v.org/inferno/4th_edition/kernel_debugging/


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.


I have a feeling that it was your first debugger, and that you've not used any others...


What other debuggers stand out as the best, in your mind?

(I'm thinking specifically for C-on-Unix, but whatever you wish...)


Anything with a UI.

I'm not saying gdb is not a great tool. It is especially useful when you don't have a debugger at all.


I quite like Eclipse's debugging support (for Java), though I haven't used it for C. I think you can get GDB integrated into Eclipse's CDT with breakpoints etc.

Haven't used DDD either but it looks interesting too:

http://www.gnu.org/software/ddd/


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!


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.


Another good interface to GDB, especially if you like vi-style navigation, is cgdb: https://cgdb.github.io/


I had an issue with codesigning, this document helped: http://wiki.lazarus.freepascal.org/GDB_on_OS_X_Mavericks_and...


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.


I mean, the initial release of gdb was 29 years ago ;)


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


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: