Skip to content

mohitmishra786/low-level-dev-skills / skills/debuggers/gdb

GDB debugger skill for C/C++ programs. Use when starting a GDB session, setting breakpoints, stepping through code, inspecting variables, debugging crashes, using reverse debugging (record/replay), remote debugging with gdbserver, or loading core dumps. Activates on queries about GDB commands, segfaults, hangs, watchpoints, conditional breakpoints, pretty-printers, Python GDB scripting, or multi-threaded debugging.

FileKindSize
SKILL.mdskill md6.2 KB
references/cheatsheet.mdmarkdown5.5 KB
references/scripting.mdmarkdown1.9 KB

references/scripting.md

View on GitHub
# GDB Python Scripting and Pretty-Printers

Source: <https://sourceware.org/gdb/current/onlinedocs/gdb.html/Python.html>

## GDB Python API basics

GDB embeds a Python interpreter. Scripts can be loaded with `source script.py` or placed in `~/.gdb_scripts/`.

```python
import gdb

# Execute GDB command
gdb.execute('bt full')

# Evaluate expression
val = gdb.parse_and_eval('x')
print(val)

# Read inferior memory
inf = gdb.selected_inferior()
mem = inf.read_memory(0x601060, 16)

# List threads
for thr in gdb.selected_inferior().threads():
    print(thr.num, thr.name)
```

## Custom pretty-printer

```python
import gdb
import gdb.printing

class MyVectorPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        data = self.val['_data']
        size = int(self.val['_size'])
        return f'MyVector of {size} elements'

    def children(self):
        data = self.val['_data']
        size = int(self.val['_size'])
        for i in range(size):
            yield f'[{i}]', (data + i).dereference()

    def display_hint(self):
        return 'array'

def build_printer(val):
    t = val.type.strip_typedefs()
    if t.name == 'MyVector':
        return MyVectorPrinter(val)
    return None

gdb.pretty_printers.append(build_printer)
```

Load in GDB:

```gdb
source /path/to/printer.py
```

## STL pretty-printers (libstdc++)

```bash
# Install python3-gdb package (Debian/Ubuntu)
sudo apt install python3-gdb

# Or use the GCC source tree printers
# They load automatically if libstdc++ was built with GDB support
```

## Breakpoint commands

```gdb
break foo
commands
  silent
  print "hit foo, x =", x
  continue
end
```

## Convenience variables

```gdb
set $i = 0
while $i < 10
  print arr[$i]
  set $i = $i + 1
end
```

## Useful define aliases

```gdb
define pbt
  thread apply all bt full
end

define hex
  print/x $arg0
end
```

Place in `~/.gdbinit`.
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall