Created
May 13, 2016 19:03
-
-
Save logc/c37ef4f5604430bfbf5625bf7546d4cd to your computer and use it in GitHub Desktop.
How to add a new structure to a GDB session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct sample { | |
int i; | |
int j; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() | |
{ | |
int i = 1337; | |
int j = 24; | |
int k = i + j; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "structs.h" | |
struct sample foo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment