{"id":64,"date":"2008-11-07T13:48:35","date_gmt":"2008-11-07T04:18:35","guid":{"rendered":"http:\/\/www.clearchain.com\/blog\/?p=64"},"modified":"2009-02-03T10:24:35","modified_gmt":"2009-02-03T00:54:35","slug":"gdb","status":"publish","type":"post","link":"https:\/\/www.clearchain.com\/blog\/posts\/gdb","title":{"rendered":"gdb"},"content":{"rendered":"<p>This page documents some tips on how to use GDB for advanced debugging. It was created as all information I could find on the web lacked some useful information.<\/p>\n<p>\u00a0<\/p>\n<p><!--more--><\/p>\n<h1><span class=\"mw-headline\">Assembly Debugging<\/span><\/h1>\n<p>\u00a0<\/p>\n<h2><span class=\"mw-headline\">Starting Out<\/span><\/h2>\n<p>When debugging assembly in gdb, it&#8217;s useful to learn about TUI mode. TUI mode gives gdb text based windows. This allows you to see both the disassembled coded as well as the ability to issue commands at the same time &#8211; a must have for debugging.<\/p>\n<p>To enter into TUI mode use:<\/p>\n<pre>CTRL-X CTRL-1<\/pre>\n<p>This will create a single window. (To get rid of it use<\/p>\n<pre>CTRL-X a<\/pre>\n<p>Initially this window will not be in the right\u00a0<em>layout<\/em>. (In gdb TUI mode, a\u00a0<em>layout<\/em>\u00a0is like a mode, ie assembler mode, etc).<\/p>\n<p>You can tell gdb to change to assembly mode using:<\/p>\n<pre>layout asm<\/pre>\n<p>Now you should be able to see your program in a disassembled text window.<\/p>\n<ul>\n<li><strong>Tip<\/strong>: If you want to see registers as well use:\u00a0<code>layout regs<\/code><\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<h2><span class=\"mw-headline\">Common Tasks<\/span><\/h2>\n<p>In assembly debugging, theres a few common tasks that you need to these are listed below:<\/p>\n<h3><strong>View registers<\/strong><\/h3>\n<p>To view registers you can either use:<\/p>\n<pre>info all-registers     |   info reg     |  i r<\/pre>\n<p>to view the all\/some of the registers. You can use:<\/p>\n<pre> layout reg<\/pre>\n<p>to bring up a TUI window with all the registers. This window also shows what changes between instructions when your executing them.<\/p>\n<p>To examine the current value of a register you can use:<\/p>\n<pre> info reg es<\/pre>\n<p>You can examine the memory the register points to using the\u00a0<strong>x<\/strong>\u00a0instruction ie:<\/p>\n<pre> x\/x $esp    # View mem in hex\r\n x\/b $esp    # View mem in bin\r\n x\/i $esp    # View instruction at address<\/pre>\n<p>You can set a register using:<\/p>\n<pre> set $eip = 0x90   # Set current eip to address 0x90<\/pre>\n<p>\u00a0<\/p>\n<h3><span class=\"mw-headline\">Memory Addresses<\/span><\/h3>\n<p>If you recall the early lessons in computing there&#8217;s two main memory regions in a program &#8211; the code segment and the data segment.<\/p>\n<p>The code segment normally consists of executable instructions and static things like constants and strings. You can view the instructions at a memory address using:<\/p>\n<pre>disassemble 0x123123<\/pre>\n<p>where 0x123123 is an address in memory. This will show you a number of instructions at that address and onwards till the end of the function ends.<\/p>\n<p>You can examine a memory address using<\/p>\n<pre> x  0x123123<\/pre>\n<p>The\u00a0<strong>x<\/strong>\u00a0command takes a number of different parameters\/options. It can output binary, hex, strings, instructions etc. For more help on how to use it try:\u00a0<code>help x<\/code>\u00a0in gdb.<br \/>\nYou can also print out the value of an address using:<\/p>\n<pre> p 0x123123<\/pre>\n<p>like\u00a0<em>x<\/em>\u00a0the\u00a0<strong>p<\/strong>\u00a0(or print) command the p command takes a number of options for displaying hex, binary etc.<\/p>\n<p>\u00a0<\/p>\n<h3><span class=\"mw-headline\">Altering program execution<\/span><\/h3>\n<p>Say you&#8217;ve been debugging a program in assembly. You&#8217;ve setup\u00a0<strong>b<\/strong>reak points, either to memory or functions. Now you&#8217;ve found the instruction you want to change. There&#8217;s a number of ways you can change it. Chances are the instruction is some type of compare, (cmp, cmpl, jge, jmp, etc). You could possible make the instruction a no instruction (nop) or modify the register the compare relies on to be something else.<\/p>\n<p>In order to alter the line to be a nop you can do something like:<\/p>\n<pre> set *0xbxxxx = 0x90<\/pre>\n<p>In this case the address is 0xbxxxx and the instruction (under x86 arch) is 0x90 (nop). However this could cause an issue if the previous instruction was 2 bytes wide. ie:<\/p>\n<pre>1&gt; 0x126c\u00a0:   incl   0xfffff3dc(%ebp)\r\n2&gt; 0x126d\u00a0:   testb  %al,%al\r\n3&gt; 0x126f\u00a0:   jne    0x125c<\/pre>\n<p>In the above the instruction at line 1 is a 1 byte instruction. The instruction at line 2 is a two byte instruction. How? Looks at the difference between the addresses:<\/p>\n<pre> 0x126d - 0x126c = 1 byte\r\n 0x126f - 0x126d = 2 byte<\/pre>\n<p>In x86 arch, 0x90 (nop) is only a 1 byte instruction.<\/p>\n<p>If you wanted to modify line 2, you&#8217;ld have to set the al register so the test would succeed.<\/p>\n<p><strong>Managing Signals<\/strong><\/p>\n<p>You can enable\/disable signals using the &#8216;<strong>handle<\/strong>&#8216; command. ie:<\/p>\n<pre style=\"padding-left: 30px; \">handle SIGALARM stop<\/pre>\n<p>The options to handle are:<\/p>\n<p>\u00a0<\/p>\n<dl>\n<dt style=\"padding-left: 30px; \"><code>nostop<\/code> <\/dt>\n<dd style=\"padding-left: 30px; \"> GDB should not stop your program when this signal happens.  It may still print a message telling you that the signal has come in. <\/dd>\n<dt style=\"padding-left: 30px; \"><code>stop<\/code> <\/dt>\n<dd style=\"padding-left: 30px; \"> GDB should stop your program when this signal happens.  This implies the <code>print<\/code> keyword as well. <\/dd>\n<dt style=\"padding-left: 30px; \"><code>print<\/code> <\/dt>\n<dd style=\"padding-left: 30px; \"> GDB should print a message when this signal happens. <\/dd>\n<dt style=\"padding-left: 30px; \"><code>noprint<\/code> <\/dt>\n<dd style=\"padding-left: 30px; \"> GDB should not mention the occurrence of the signal at all.  This implies the <code>nostop<\/code> keyword as well. <\/dd>\n<dt style=\"padding-left: 30px; \"><code>pass<\/code> <\/dt>\n<dd style=\"padding-left: 30px; \"> GDB should allow your program to see this signal; your program can handle the signal, or else it may terminate if the signal is fatal and not handled. <\/dd>\n<dt style=\"padding-left: 30px; \"><code>nopass<\/code> <\/dt>\n<dd style=\"padding-left: 30px; \"> GDB should not allow your program to see this signal.\u00a0<\/dd>\n<\/dl>\n<p>And to see what signals are currently set and their default setup use:<\/p>\n<pre style=\"padding-left: 30px; \">info signals\u00a0<\/pre>\n<p>\u00a0<\/p>\n<h1><span class=\"mw-headline\">Helpful External Links<\/span><\/h1>\n<p><a class=\"external free\" title=\"http:\/\/insecure.org\/stf\/mudge_buffer_overflow_tutorial.html\" rel=\"nofollow\" href=\"http:\/\/insecure.org\/stf\/mudge_buffer_overflow_tutorial.html\">http:\/\/insecure.org\/stf\/mudge_buffer_overflow_tutorial.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Helpful hints for when using GDB<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,7],"tags":[62,60,61],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-computers","category-programming","tag-debugging","tag-gui","tag-tui"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>gdb - ClearChain<\/title>\n<meta name=\"description\" content=\"Helpful hints for when using GDB\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.clearchain.com\/blog\/posts\/gdb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/fb6b5d38a724caeb1ba78d08334ad8d3\"},\"headline\":\"gdb\",\"datePublished\":\"2008-11-07T04:18:35+00:00\",\"dateModified\":\"2009-02-03T00:54:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb\"},\"wordCount\":719,\"commentCount\":1,\"keywords\":[\"debugging\",\"gui\",\"tui\"],\"articleSection\":[\"Computers\",\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/gdb#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb\",\"url\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb\",\"name\":\"gdb - ClearChain\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\"},\"datePublished\":\"2008-11-07T04:18:35+00:00\",\"dateModified\":\"2009-02-03T00:54:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/fb6b5d38a724caeb1ba78d08334ad8d3\"},\"description\":\"Helpful hints for when using GDB\",\"breadcrumb\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/gdb\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/gdb#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.clearchain.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"gdb\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\",\"url\":\"https:\/\/www.clearchain.com\/blog\/\",\"name\":\"ClearChain\",\"description\":\"-= Daily Happenings =-\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/fb6b5d38a724caeb1ba78d08334ad8d3\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"gdb - ClearChain","description":"Helpful hints for when using GDB","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.clearchain.com\/blog\/posts\/gdb","twitter_misc":{"Written by":"admin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.clearchain.com\/blog\/posts\/gdb#article","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/gdb"},"author":{"name":"admin","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/fb6b5d38a724caeb1ba78d08334ad8d3"},"headline":"gdb","datePublished":"2008-11-07T04:18:35+00:00","dateModified":"2009-02-03T00:54:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/gdb"},"wordCount":719,"commentCount":1,"keywords":["debugging","gui","tui"],"articleSection":["Computers","Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.clearchain.com\/blog\/posts\/gdb#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.clearchain.com\/blog\/posts\/gdb","url":"https:\/\/www.clearchain.com\/blog\/posts\/gdb","name":"gdb - ClearChain","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/#website"},"datePublished":"2008-11-07T04:18:35+00:00","dateModified":"2009-02-03T00:54:35+00:00","author":{"@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/fb6b5d38a724caeb1ba78d08334ad8d3"},"description":"Helpful hints for when using GDB","breadcrumb":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/gdb#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.clearchain.com\/blog\/posts\/gdb"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.clearchain.com\/blog\/posts\/gdb#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.clearchain.com\/blog"},{"@type":"ListItem","position":2,"name":"gdb"}]},{"@type":"WebSite","@id":"https:\/\/www.clearchain.com\/blog\/#website","url":"https:\/\/www.clearchain.com\/blog\/","name":"ClearChain","description":"-= Daily Happenings =-","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/fb6b5d38a724caeb1ba78d08334ad8d3","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","caption":"admin"},"sameAs":["http:\/\/"]}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":5,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":167,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions\/167"}],"wp:attachment":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}