6 ms·
Learning C with gdb
- kjak 12y agoI 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!
- vezzy-fnord 12y agoOne 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/
- adamnemecek 12y agoAnother indispensable tool is this site http://gcc.godbolt.org http://gcc.godbolt.org
- agumonkey 12y agoNever seen before, very nice to be able to enjoy the differences between optimization flags. -Os makes a big difference.
- castratikron 12y agoYou 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 12y agoI'm well aware of that. This is much more interactive though. The color coding is also dank.
- cubano 12y agoThis 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 12y agoThat'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.
- dmix 12y agoIf 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.
- nhebb 12y agoOne 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 12y agoAnd if that kind of "game" is your thing, you may also enjoy https://microcorruption.com https://microcorruption.com
- justinmk 12y agoThe 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 12y agoWow, -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!
- skeuomorf 12y agotui 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
- umanwizard 12y agoI'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.
- jplahn 12y agoInteresting 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 12y agoI have a feeling that it was your first debugger, and that you've not used any others...
- calibraxis 12y agoWhat other debuggers stand out as the best, in your mind? (I'm thinking specifically for C-on-Unix, but whatever you wish...)
- dvdfvo 12y agoAnything 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.
- harkyns_castle 12y agoI 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/ http://www.gnu.org/software/ddd/
- niix 12y agoI had an issue with codesigning, this document helped: http://wiki.lazarus.freepascal.org/GDB_on_OS_X_Mavericks_and_Xcode_5 http://wiki.lazarus.freepascal.org/GDB_on_OS_X_Mavericks_and...
- roblatham 11y agoAnother good interface to GDB, especially if you like vi-style navigation, is cgdb: https://cgdb.github.io/ https://cgdb.github.io/