
Learning C with gdb - luu
https://www.recurse.com/blog/5-learning-c-with-gdb
======
justinmk
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`.

~~~
skeuomorf
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/](http://beej.us/guide/bggdb/)

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

------
nhebb
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](http://condor.depaul.edu/glancast/373class/docs/lab2.html)

~~~
eckzow
And if that kind of "game" is your thing, you may also enjoy
[https://microcorruption.com](https://microcorruption.com)

------
adamnemecek
Another indispensable tool is this site
[http://gcc.godbolt.org](http://gcc.godbolt.org)

~~~
castratikron
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.

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

------
dmix
If you like this, coursera has a great course called the "Hardware Software
Interface".

[http://coursera.org/course/hwswinterface](http://coursera.org/course/hwswinterface)

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

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

------
jplahn
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
I have a feeling that it was your first debugger, and that you've not used any
others...

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

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

~~~
dvdfvo
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.

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

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

------
niix
I had an issue with codesigning, this document helped:
[http://wiki.lazarus.freepascal.org/GDB_on_OS_X_Mavericks_and...](http://wiki.lazarus.freepascal.org/GDB_on_OS_X_Mavericks_and_Xcode_5)

------
nailer
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
I mean, the initial release of gdb was 29 years ago ;)

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

