Skip to content

Instantly share code, notes, and snippets.

@logc
Created May 13, 2016 19:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save logc/c37ef4f5604430bfbf5625bf7546d4cd to your computer and use it in GitHub Desktop.
Save logc/c37ef4f5604430bfbf5625bf7546d4cd to your computer and use it in GitHub Desktop.
How to add a new structure to a GDB session
❯ gcc -g minimal.c -o minimal
❯ sudo gdb minimal
Password:
(gdb) break main
Breakpoint 1 at 0x100000f90: file minimal.c, line 3.
(gdb) run
Starting program: /private/tmp/c-repl/minimal
Breakpoint 1, main () at minimal.c:3
3 int i = 1337;
### Ctrl-Z to suspend GDB session; edit structs.h and struct.c; and do:
### ❯ gcc -c -g structs.c -o structs.o
### then bring GDB back to foreground with `fg`
(gdb) add-symbol-file structs.o 0
add symbol table from file "structs.o" at
.text_addr = 0x0
(y or n) y
Reading symbols from structs.o...done.
(gdb) p (struct sample *){23, 34}
$1 = (struct sample *) 0x100400000
(gdb) set $foo = *$1
(gdb) ptype $foo
type = struct sample {
int i;
int j;
}
(gdb) p $foo.i
$2 = 23
(gdb) p $foo.j
$3 = 34
(gdb) quit
struct sample {
int i;
int j;
};
int main()
{
int i = 1337;
int j = 24;
int k = i + j;
return 0;
}
#include "structs.h"
struct sample foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment