gdb & nice Vim macro

Imagine we have a simple program like the one below:

<code>#include &lt;stdio.h&gt;

int main(int argc, char **argv)
{

    char *str = "Hello guys, this is just a bit large string to test a nice Vim macro";

    printf("%sn", str);

    return (0);
}</code>

This program is pretty simple, but it will serve its purpose. Imagine we have to inspect the content of str or for the same matter, any other memory address. We want to do it while we are inside a gdb session or while we are analyzing a core file. The simplest case is to do something like the following:

<code>(gdb) p str
$1 = 0x400642 "Hello guys, this is just a bit large string to test a nice Vim macro"</code>

But sometimes, we have to inspect a raw memory address, so we have to tell gdb to print just bytes. We can do this with the following command (the actual output may differ depending on the version of gdb you use):

<code>x/100c str

0x400642 &lt;.rodata&gt;:     72 'H'  101 'e' 108 'l' 108 'l' 111 'o' 32 ' '  103 'g' 117 'u'
0x40064a &lt;.rodata+8&gt;:   121 'y' 115 's' 44 ','  32 ' '  116 't' 104 'h' 105 'i' 115 's'
0x400652 &lt;.rodata+16&gt;:  32 ' '  105 'i' 115 's' 32 ' '  106 'j' 117 'u' 115 's' 116 't'
0x40065a &lt;.rodata+24&gt;:  32 ' '  97 'a'  32 ' '  98 'b'  105 'i' 116 't' 32 ' '  108 'l'
0x400662 &lt;.rodata+32&gt;:  97 'a'  114 'r' 103 'g' 101 'e' 32 ' '  115 's' 116 't' 114 'r'
0x40066a &lt;.rodata+40&gt;:  105 'i' 110 'n' 103 'g' 32 ' '  116 't' 111 'o' 32 ' '  116 't'
0x400672 &lt;.rodata+48&gt;:  101 'e' 115 's' 116 't' 32 ' '  97 'a'  32 ' '  110 'n' 105 'i'
0x40067a &lt;.rodata+56&gt;:  99 'c'  101 'e' 32 ' '  86 'V'  105 'i' 109 'm' 32 ' '  109 'm'
0x400682 &lt;.rodata+64&gt;:  97 'a'  99 'c'  114 'r' 111 'o' 0 ''  37 '%'  115 's' 10 'n'
0x40068a &lt;.rodata+72&gt;:  0 ''  0 ''  1 '01'        27 '33'       3 '03'        59 ';'  24 '30'       0 ''
0x400692 &lt;.eh_frame_hdr+6&gt;:     0 ''  0 ''  2 '02'        0 ''  0 ''  0 ''  20 '24'       -2 'th'
0x40069a &lt;.eh_frame_hdr+14&gt;:    -1 '"y'  -1 '"y'  52 '4'  0 ''  0 ''  0 ''  36 '$'  -1 '"y'
0x4006a2 &lt;.eh_frame_hdr+22&gt;:    -1 '"y'  -1 '"y'  116 't' 0 ''</code>

Above we just told gdb to print the first hundred bytes starting at str. However, the output of the command is not easy to read or deal with. We can copy that text into Vim and set the following map:

<code>:map &lt;F7&gt; /'.'&lt;CR&gt;&lt;ESC&gt;lvy&lt;ESC&gt;maG$p`a</code>

Now, place the cursor at the beginning of the file (<ESC>gg) and press F7 until you get the whole string written at the end.

Explanation:

This macro does the following:

  • /’.'<CR><ESC> Places the cursor at the first occurrence of ‘<any character>’. This is, the “letters” we are looking for
  • lvy Copies the character under the cursor
  • <ESC>ma Sets a mark at the position of the cursor so we can go back to it later.
  • G$p Goes to the end of the file (last character of the last line) and pastes the character
  • `a Goes back to the mark

This way, every time we press F7, we print a new character at the end of the file. I have to inspect memory addresses at work very often looking for very long strings and this map saves me a lot of time.

Enjoy!