Logging
1 2 3 4 5 | |
Breaking Execution
Function Call
1 | |
1 | |
1 | |
1 2 | |
Specific Line
1 | |
1 | |
When Symbol Comes into Scope
1 | |
When Symbol Changes Value
1 2 3 | |
Requires variable to have a stable memory address (e.g., global or in-scope local).
I/O
Set Pretty Printing
1 | |
1 | |
Limits number of elements printed (default: 200).
Dump Binary Memory
1 | |
Example:
1 | |
Note
- Use pointer arithmetic to calculate start and end addresses
- A useful use-case is that you can dump containers to read them back into python
1 2 3 | |
TL;DR (First Principles):
When GDB executes a program, it maps your variables—including arrays and matrices—into memory. You can tell GDB to dump raw memory of any variable to a binary file. This lets you analyze the data outside GDB (e.g., with Python + NumPy) without GDB choking on printing large data inline.
1. Understand What You're Dumping
You need:
- The starting memory address (&my_matrix)
- The exact byte size to dump (sizeof(my_matrix))
-
Example: for a float my_matrix[100][100]:
1 | |
2. Basic Syntax
1 | |
Or if you know the size:
1 | |
But GDB doesn't support pointer arithmetic like that directly on sizeof. Instead:
1 | |
(You can replace 40000 with any exact byte count)
Or calculate in GDB:
1 | |
Then manually use the value:
1 | |
What Happens Internally
- GDB interprets the address
&my_matrixas a raw memory location. - It writes the raw byte sequence (just 1s and 0s) to
matrix.bin.
Load It in Python (Post-GDB)
1 2 3 4 5 | |
Make sure dtype and shape match your C++ matrix layout.
🧪 Sanity Check in GDB
Want to confirm it’s dumping the correct values?
1 | |
Then check if those match the first line of matrix[0][:10] in NumPy.
Pitfalls:
- Don’t dump pointers unless they point to contiguous memory blocks.
- Avoid structs with padding unless you understand layout.
- Beware of endian issues if moving between architectures.