Hacker News new | past | comments | ask | show | jobs | submit login
Beej's Quick Guide to GDB (2009) (beej.us)
241 points by mooreds on Nov 5, 2023 | hide | past | favorite | 29 comments



Thank you so much. I didn't know about layouts at all! The source code and disassembly layout is amazing and it's exactly what I want to see when I launch a debugger. I wonder why it's not the default. GDB has a huge number of features but it's quite hard to use. That'd go a long way towards making it easier.

Is there's a data structure explorer panel? I went to rather insane lengths to develop pretty printing scripts for my data structures. It's got support for 3 scripting languages: Python, Guile Scheme and its very own GDB command file language. The first two require libc. My project was freestanding so I had to parse bits and bytes with GDB's arcane syntax. A proper structure visualizer would be so helpful...


> I wonder why it's not the default.

You can toss something like this in ~/.config/gdb/gdbinit:

  tui new-layout default regs 1 {-horizontal src 1 asm 1} 2 status 0 cmd 1
  tui layout default
  tui enable


Awesome guide :)

I these days use coredumpctl with gdb to debug seg faults.

So if a program crashes, I simply do:

$ coredumpctl debug

Now it launches gdb (or your default debugger) on the latest core dump. Then I use `bt` and others to debug the issue.

https://www.freedesktop.org/software/systemd/man/latest/core...


One thing I think is sorely lacking is approachable documentation for writing python extensions to GDB.

I've managed to cobble together a collection of data structure explorers written in Python but it is barely an improvement over straight gdb scripting due to the way all structs end up as dictionaries. Some higher level libraries for inspection might be nice. Automatic integration with something like graphviz would be amazing. For instance, it would be cool to just describe my data structure(specifying 'next' pointers or how to access edge lists for a graph) and just have a library dump a visual representation for me. That said, I wonder if half of that logic is best left in a debug module inside the application or library where it remains more tightly coupled to the actual layout.


ChatGPT might work well for this.


Funny you say that. Literally the first thing I tried to get ChatGPT to do for me was to give me an example gdb extension written in Python. It hallucinated badly and as I pushed it, it produced increasingly erroneous errors. That was about 6-8 months ago though. It might be better now.


Make sure you try gpt4 if you didn't.

I've had partial success in getting gpt4 to write rubocop cops (i.e. custom linting rules for ruby). I had to iterate a bit but eventually got something working and wouldn't have tried if I didn't have gpt helping me.


I think it was gpt3 at the time. I just tried 4 and it got it right!


3 is unusable and mostly just a curiosity. 4 is scary smart.

There's 3 years between them. Bit worried about 2026.


I wish that there was a performant and useful front end to GDB for Linux. There isn't, so I'm stuck using GDB manually. Maybe CLion is good?


I wrote https://github.com/daym/idea-native2-debugger as a stop-gap. It uses gdb and works in IntelliJ IDEA Community edition. Setting it up the first time is kinda weird since you need to add a new run/debug configuration "Native2Debugger". I could not figure out how to hook this directly into the existing run configuration that you use to run your program to begin with. Otherwise, I like how it turned out.

If you want a standalone frontend instead, https://github.com/epasveer/seer is extremely good.

And emacs has gdb integration. By now I tried it, and... I guess it's better than nothing.


I'll preface this with 'it might not be what you're looking for, but I've found useful in this space', but gdb has multiple interfaces built in to the normal binary. It's not just the raw gdb command prompt, but it has a pretty nice curses mode that'll display registers, asm, source with breakpoints, etc. that you expect from a debugger interface. It's very much a hidden gem of gdb.

https://sourceware.org/gdb/current/onlinedocs/gdb.html/TUI.h...


gf2 is worth looking at: https://github.com/nakst/gf


Wow! I just tried it out, and that was like a dream come true. Thank you for linking this.


If you need something text based, cgdb is nice: http://cgdb.github.io/


Qt Creator is not terrible. I've yet to come across a GDB front-end I would qualify as "good".

It is a full IDE for writing Qt apps, but it also works for non-Qt applications in C or C++ (cmake recommended), and it has some of the best GDB integration. It is also free (GPLv3).


I think gdbgui [0] is useful. I'm not sure about its performance though.

[0] https://www.gdbgui.com/


Have you tried the Emacs gdb mode?


VS Code has been sufficient for me, at least for hobby stuff. I used it solely as a debugger before I switched over to using it as an editor as well.


There is also GEF, which is widely used by the reverse engineering and CTF community.

https://github.com/hugsy/gef


Sounds like you want a full UI, but GEF is quite good for making GDB pleasant to use.


CLion is definitely good.


Beej’s guide for networking really saved me:

https://beej.us/guide/bgnet/

I continue to find that knowledge useful in my day job working on distributed systems.


This guide is the only reason I passed my school's version of CMU's CS:APP (malloc)

Huge shoutout to Beej!


I didn't know about `jump`, that is super useful.


Anyone know which is "better", GDB or LDB?


For some reason, I initially read the title as “…Guide to PBJ” and I got really excited about a potential guide to better indulgent sandwiches… but nope.


Very lightly toasting the bread makes them tastier. That's just a general sandwich tip, really. But with a PBJ you want it to stay soft enough that the filling doesn't squeeze out the side.


Beej has lots of great guides, check them out:

https://beej.us/guide/

Everyone knows the networking guide, but Beej also has: C, UNIX, Python, and many more.




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

Search: