title: GDB: Attempt to dereference generic pointer
body: <p>How can I make GDB do extra dereferences in a printing function like
  <code>x/s</code>?</p>
  
  <p>When I try explicit dereferences in <code>x/</code> I get the error "Attempt to
  dereference a generic pointer". Using <code>x/</code> multiple times works, since
  each use includes an implicit dereference, but this is annoying since
  I have to copy and paste each intermediate result.</p>
  
  <h1>Example</h1>
  
  <p>Consider the very useful C program, <code>example.c</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(int argc, char **argv) {
    printf("argv[0] = %s\n", argv[0]);
  }
  </code></pre>
  
  <p>If I build it and load it into GDB, I see that <code>argv</code> is stored at
  <code>0xc(%ebp)</code>, since a double dererence of that is passed as the second
  argument to <code>printf</code> (i.e. in <code>0x4(%esp)</code>) on line 26:</p>
  
  <pre><code>$ gcc -o example example.c
  $ gdb example
  
  (gdb) disass main
  Dump of assembler code for function main:
     0x080483e4 &lt;+0&gt;:   push   %ebp
     0x080483e5 &lt;+1&gt;:   mov    %esp,%ebp
     0x080483e7 &lt;+3&gt;:   and    $0xfffffff0,%esp
     0x080483ea &lt;+6&gt;:   sub    $0x10,%esp
     0x080483ed &lt;+9&gt;:   mov    0xc(%ebp),%eax
     0x080483f0 &lt;+12&gt;:  mov    (%eax),%edx
     0x080483f2 &lt;+14&gt;:  mov    $0x80484e0,%eax
     0x080483f7 &lt;+19&gt;:  mov    %edx,0x4(%esp)
     0x080483fb &lt;+23&gt;:  mov    %eax,(%esp)
     0x080483fe &lt;+26&gt;:  call   0x8048300 &lt;printf@plt&gt;
     0x08048403 &lt;+31&gt;:  leave  
     0x08048404 &lt;+32&gt;:  ret    
  End of assembler dump.
  </code></pre>
  
  <p>I break at <code>printf</code> and run the program with arguments <code>first</code> and
  <code>second</code>:</p>
  
  <pre><code>(gdb) break *main + 26
  Breakpoint 1 at 0x80483fe
  
  (gdb) run first second
  Starting program: /var/tmp/SO-attempt-to-dereference-generic-pointer/example first second
  </code></pre>
  
  <p>I attempt to print <code>argv[0]</code> in GDB, but I get the "generic pointer"
  error:</p>
  
  <pre><code>Breakpoint 1, 0x080483e5 in main ()
  (gdb) x/s **(0xc + $ebp)
  Attempt to dereference a generic pointer.
  </code></pre>
  
  <p>However, by using 'x/xw' to manually dereference a few times, I'm
  eventually able to print <code>argv[0]</code> (and <code>argv[1]</code>):</p>
  
  <pre><code>(gdb) x/xw 0xc + $ebp
  0xbfffeba4: 0xbfffec34
  (gdb) x/xw 0xbfffec34
  0xbfffec34: 0xbfffedc8
  (gdb) x/s 0xbfffedc8
  0xbfffedc8:  "/var/tmp/SO-attempt-to-dereference-generic-pointer/example"
  
  (gdb) x/xw 0xbfffec34 + 4
  0xbfffec38: 0xbfffee03
  (gdb) x/s 0xbfffee03
  0xbfffee03:  "first"
  (gdb) 
  </code></pre>
  
  <p>But this is annoying and indirect (as pointer programming is wont to be?)</p>