title: accessing command-line arguments with gdb
body: <p>I am on linux using gdb version 6.8-debian. I have been curious about how the main function in a c-program gets executed and playing around and looking in different places, I learned that the function __libc_start_main is responsiple for this. The arguments to __libc_start_main are, among others: The address of main (like we know from c, the path is always given as argv[0]), next argc which should reside in the register ESI, and next address of argv which should be in ECX. </p>
  
  <p>To play around I made the following simple program, cmdargs.c, which simply outputs the first command-line argument given at start:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main (int argc, char *argv[])
  {
      printf("%s: %s\n", "argv[1]", *++argv);
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Now I start to debug cmdargs and set a breakpoint on main and __libc_start_main (info from starting gdb removed):</p>
  
  <blockquote>
    <p>gdb cmdargs</p>
  </blockquote>
  
  <pre><code>(gdb) b main
  Breakpoint 1 at 0x80483d2
  (gdb) b __libc_start_main
  Breakpoint 2 at 0xb7f3f5a8
  (gdb) r qwerty
  </code></pre>
  
  <p>Here i hit the Breakpoint 2 in __libc_start_main and can view argc and argv[0] with</p>
  
  <pre><code>(gdb) p $esi
  </code></pre>
  
  <p>and</p>
  
  <pre><code>(gdb) x/s *($ecx)
  </code></pre>
  
  <p>This works as expected, but how do I access the first non-implicit commandline-argument "qwerty" ? I have tried continuing to the breakpoint at main and stepping in, but argc and argv are not recognised (Why?). Can someone tell me whats going on ?</p>
  
  <pre><code>Breakpoint 1, 0x080483d2 in main ()
  (gdb) stepi
  0x080483d5 in main () 
  (gdb) p argc
  No symbol "argc" in current context.
  (gdb) p argv
  No symbol "argv" in current context.
  (gdb) 
  </code></pre>