{"id":56,"date":"2024-10-30T00:50:00","date_gmt":"2024-10-29T13:50:00","guid":{"rendered":"https:\/\/davidshoon.com\/?p=56"},"modified":"2026-10-20T09:50:27","modified_gmt":"2026-10-19T22:50:27","slug":"using-gdb","status":"publish","type":"post","link":"https:\/\/davidshoon.com\/?p=56","title":{"rendered":"Using gdb"},"content":{"rendered":"<h1 style=\"margin: 0px;text-align: left;text-indent: 0px\">Using gdb<\/h1>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">I figured I&#8217;ll write a mini-tutorial on how to use gdb, because there&#8217;s not that many places where they teach you how to use gdb effectively.<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Let&#8217;s say you have a program and it crashes, what do you do?<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Example code:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">void func() { char *p = 0; *p = 0x69; }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">int main() { func(); }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Next:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">gdb a.out<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Followed by the `run` command:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) run<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Starting program: \/home\/d\/a.out <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">[Thread debugging using libthread_db enabled]<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Using host libthread_db library &#8220;\/lib\/x86_64-linux-gnu\/libthread_db.so.1&#8221;.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Program received signal SIGSEGV, Segmentation fault.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">0x000055555555513d in func () at test.c:1<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">1\tvoid func() { char *p = 0; *p = 0x69; }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">You can see where it crashes, but you&#8217;d like a stacktrace&#8230;<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">gdb) bt<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">#0  0x000055555555513d in func () at test.c:1<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">#1  0x0000555555555155 in main () at test.c:3<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">(NB: `bt` stands for backtrace)<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">You can go up a frame and check local variables:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) up<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">#1  0x0000555555555155 in main () at test.c:3<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">3\tint main() { func(); }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) info local<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">No locals.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Or down a frame and check local variables:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) down<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">#0  0x000055555555513d in func () at test.c:1<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">1\tvoid func() { char *p = 0; *p = 0x69; }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) info local<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">p = 0x0<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">You can continue after the segfault:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) cont<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Continuing.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Program terminated with signal SIGSEGV, Segmentation fault.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">The program no longer exists.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Now we can re-run it, but before we do, we can set a breakpoint:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) break func<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Breakpoint 1 at 0x555555555131: file test.c, line 1.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Now we run it again, it will stop at the breakpoint:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) run<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Starting program: \/home\/d\/a.out <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">[Thread debugging using libthread_db enabled]<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Using host libthread_db library &#8220;\/lib\/x86_64-linux-gnu\/libthread_db.so.1&#8221;.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Breakpoint 1, func () at test.c:1<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">1\tvoid func() { char *p = 0; *p = 0x69; }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">We can single step through it:<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) step<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\"><br \/><\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">Program received signal SIGSEGV, Segmentation fault.<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">0x000055555555513d in func () at test.c:1<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">1\tvoid func() { char *p = 0; *p = 0x69; }<\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><span style=\"font-family: courier\">(gdb) <\/span><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&#8220;`<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">We can also try the command `next`, which is similar to `step` but skips over subroutines.<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">We can get help from gdb at any time using `help`.<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Another useful `info` command is `info reg`, which shows CPU registers.<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\"><\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">Also useful is `disas` command, which disassembles the code.<\/p>\n<p style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin: 0px;text-indent: 0px\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using gdb I figured I&#8217;ll write a mini-tutorial on how to use gdb, because there&#8217;s not that many places where they teach you how to use gdb effectively. Let&#8217;s say you have a program and it crashes, what do you do? Example code: &#8220;` void func() { char *p = 0; *p = 0x69; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":1,"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":57,"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions\/57"}],"wp:attachment":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}