title: How to script gdb (with python)? Example add breakpoints, run, what breakpoint did we hit?
body: <p>I'm trying to create a little unit test with gdb, 
  for a embedded mcu that is controlled by <a href="http://openocd.berlios.de/web/" rel="noreferrer">OpenOCD</a> (that gives me control over my target via a gdb server).</p>
  
  <p>So I would like to automate this with some scripting of gdb.</p>
  
  <p>I would like to write some kind of script for gdb that more or less does this:</p>
  
  <ol>
  <li>Add a couple of breakpoints</li>
  <li>Start the program</li>
  <li>When we stop, where did it stop (get the frame info)</li>
  <li>Quit. </li>
  </ol>
  
  <p>Any ideas? </p>
  
  <p>A example on how to do this in python gdb scripting would be nice.</p>
  
  <p>Thanks
  Johan</p>
  
  <hr>
  
  <p><em>Note</em>: </p>
  
  <p>Let's say that we have this basic structure, 
  that more or less goes into test_failed() or test_success()
  depending on what the function start_test() returns.</p>
  
  <pre><code>void test_failed() {    
      while(1);    
  }
  
  void test_success() {    
      while(1);    
  }
  
  int main(void) {    
      int status = start_test();    
  
      if( status &gt; 0 ) {    
          test_failed();    
      }    
      test_success();
  
      while(1);    
  }
  </code></pre>
  
  <p>To do this manually in gdb is very strait forward, </p>
  
  <pre><code>(gdb) break test_success
  Breakpoint 1 at 0x20: file src/main.c, line 9.
  (gdb) break test_failed
  Breakpoint 2 at 0x18: file src/main.c, line 5.
  (gdb) cont
  Continuing.
  
  Breakpoint 1, test_success () at src/main.c:9
  9       while(1);
  (gdb) frame
  #0  test_success () at src/main.c:9
  9       while(1);
  (gdb) 
  </code></pre>
  
  <p>So the next step I tried was to add those gdb commands into a gdb startup script that more or less just looked like this.</p>
  
  <pre><code>break test_success
  break test_failed
  target remote localhost:3333
  cont 
  frame
  </code></pre>
  
  <p>and start it with </p>
  
  <pre><code>arm-none-eabi-gdb --batch --command=commands.gdb main.elf
  </code></pre>
  
  <p>And this kind of works, but it is not very nice.
  How do I do this with the "new and cool" python scripts, 
  that gdb seem to support.</p>