title: How to debug cross-compiled QEMU program with GDB?
body: <p>I'm having trouble debugging a simple program running in QEMU with GDB.  GDB seems unable to find where I am in the program (in that it always displays <code>??</code> as my current location), and it never hits any breakpoint I set.</p>
  
  <p>In one terminal, I run QEMU:</p>
  
  <pre><code>$ cat add.c
  int main() {
      int x = 9;
      int v = 1;
      while (1) {
          int q = x + v;
      }
      return 0;
  }
  
  $ riscv64-unknown-elf-gcc add.c -g
  $ qemu-system-riscv64 -gdb tcp::1234 -drive file=a.out,format=raw
  </code></pre>
  
  <p>And in another terminal, I run GDB:</p>
  
  <pre><code>$ riscv64-unknown-elf-gdb a.out
  GNU gdb (GDB) 8.2.90.20190228-git
  Copyright (C) 2019 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.
  Type "show copying" and "show warranty" for details.
  This GDB was configured as "--host=x86_64-apple-darwin17.7.0 --target=riscv64-unknown-elf".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;.
  Find the GDB manual and other documentation resources online at:
      &lt;http://www.gnu.org/software/gdb/documentation/&gt;.
  
  For help, type "help".
  Type "apropos word" to search for commands related to "word"...
  Reading symbols from a.out...
  (gdb) target remote :1234
  Remote debugging using :1234
  0x0000000000000000 in ?? ()
  (gdb) list
  1       int main() {
  2           int x = 9;
  3           int v = 1;
  4           while (1) {
  5               int q = x + v;
  6           }
  7           return 0;
  8       }
  (gdb) b main
  Breakpoint 1 at 0x1018e: file add.c, line 2.
  (gdb) b 5
  Breakpoint 2 at 0x1019a: file add.c, line 5.
  (gdb) b _start
  Breakpoint 3 at 0x10114
  (gdb) b 4
  Breakpoint 4 at 0x101a8: file add.c, line 4.
  (gdb) c
  Continuing.
  </code></pre>
  
  <p>I never hit a breakpoint, even though the program should be looping infinitely.  It seems odd that it's displaying <code>0x0000000000000000 in ?? ()</code>...but maybe that's okay?</p>
  
  <p>What am I doing wrong here?  How can I step through this program?</p>