gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 1 | # Qemu Vector Testing and Debugging |
| 2 | |
| 3 | 1. [Qemu Setup](#qemu-setup) |
| 4 | 1. [Build steps:](#build-steps) |
| 5 | 2. [Start Qemu](#start-qemu) |
| 6 | 3. [Exiting Qemu](#exiting-qemu) |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 7 | 2. [Running GDB](#running-gdb) |
| 8 | 1. [Use the following command to run GDB](#use-the-following-command-to-run-gdb) |
| 9 | 3. [GDBGUI Setup](#gdbgui-setup) |
| 10 | 1. [Installing gdbgui](#installing-gdbgui) |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 11 | 2. [Running gdbgui](#running-gdbgui) |
| 12 | 3. [gdbgui window](#gdbgui-window) |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 13 | 4. [GDB Usage](#gdb-usage) |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 14 | 1. [Some Useful GDB instructions](#some-useful-gdb-instructions) |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 15 | 5. [Aliases for convenience](#aliases-for-convenience) |
| 16 | 6. [Advanced Usage](#advanced-usage) |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 17 | 1. [Debugging Qemu](#debugging-qemu) |
| 18 | |
| 19 | ## Qemu Setup |
| 20 | |
| 21 | ### Build steps: |
| 22 | |
| 23 | ``` |
| 24 | source build/setup.sh |
| 25 | m tools |
| 26 | m qemu |
| 27 | ``` |
| 28 | |
| 29 | ### Start Qemu |
| 30 | |
| 31 | Use the following command to start qemu with debugging: |
| 32 | |
| 33 | ``` |
| 34 | qemu-system-riscv32 -M opentitan -kernel $ROOTDIR/out/shodan/build-out/sw_shodan/device/examples/hello_vector/hello_vector_sim_verilator.elf -bios $ROOTDIR/out/shodan/build-bin/sw/device/boot_rom/boot_rom_fpga_nexysvideo.elf -nographic -cpu rv32,x-v=true,vlen=512,vext_spec=v1.0 -s -S |
| 35 | ``` |
| 36 | |
| 37 | Notes on above command: |
| 38 | |
| 39 | - `-s` - starts the gdb server |
| 40 | - `-S` - waits on execution for a client to connect |
| 41 | |
| 42 | The above commands can be omitted to run qemu without starting a gdb server. |
| 43 | |
| 44 | ### Exiting Qemu |
| 45 | |
| 46 | To exit qemu, in the same teriminal, press `Ctrl-a` then `Ctrl-x`. |
| 47 | |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 48 | ## Running GDB |
| 49 | |
| 50 | ### Use the following command to run GDB |
| 51 | |
| 52 | Run GDB to create a gdb session in the CLI: |
| 53 | |
| 54 | ``` |
| 55 | $ROOTDIR/cache/toolchain_vp/bin/riscv32-unknown-elf-gdb $ROOTDIR/out/shodan/build-bin/sw/device/examples/hello_vector/hello_vector_sim_verilator.elf --eval-command "target remote :1234" |
| 56 | ``` |
| 57 | |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 58 | ## GDBGUI Setup |
| 59 | |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 60 | ### Installing gdbgui |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 61 | |
| 62 | `python3 -m pip install gdbgui` |
| 63 | |
| 64 | ### Running gdbgui |
| 65 | |
| 66 | Use the following command to begin gdbgui: |
| 67 | |
| 68 | ``` |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 69 | export PURE_PYTHON=1; gdbgui -g "$ROOTDIR/out/host/toolchain_vp/bin/riscv32-unknown-elf-gdb $ROOTDIR/out/shodan/build-bin/sw/device/examples/hello_vector/hello_vector_sim_verilator.elf --eval-command \"target remote :1234\"" |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 70 | ``` |
| 71 | |
| 72 | ### gdbgui window |
| 73 | |
| 74 | lower left window takes conventional gdb commands. |
| 75 | |
| 76 | To start, set up a breakpoint (e.g. `b main`), then hit continue (`c`) |
| 77 | |
| 78 |  |
| 79 | |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 80 | ## GDB Usage |
| 81 | |
| 82 | With one of the gdb methods working, you can now step through and check |
| 83 | register contents. |
| 84 | |
| 85 | ### Some Useful GDB instructions |
| 86 | |
| 87 | - `break main` (or `b main` for short) - makes a breakpoint at the main, more |
| 88 | info [here](https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_27.html) |
| 89 | - `continue` (or `c` for short) - runs until breakpoint |
| 90 | - `step` (or `s` for short) - runs one more line, will "step into" subroutines |
| 91 | - `next` (or `n` for short) - runs one more line, will "step over" subroutines |
| 92 | - `info reg` - print all registers |
| 93 | - `info reg a1` - print scalar register a1 |
| 94 | - `info vector` - print all vector registers and vector csr's |
| 95 | |
| 96 | ## Aliases for convenience |
| 97 | |
| 98 | First source `build/setup.sh` to set up the enviroment in the respective |
| 99 | terminal, and then these aliases can speed up the workflow: |
| 100 | |
| 101 | ``` |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 102 | alias run_gdb="$ROOTDIR/cache/toolchain_vp/bin/riscv32-unknown-elf-gdb $ROOTDIR/out/shodan/build-bin/sw/device/examples/hello_vector/hello_vector_sim_verilator.elf --eval-command \"target remote :1234\"" |
| 103 | alias run_gdb_gui="export PURE_PYTHON=1; gdbgui -g '$ROOTDIR/cache/toolchain_vp/bin/riscv32-unknown-elf-gdb $ROOTDIR/out/shodan/build-bin/sw/device/examples/hello_vector/hello_vector_sim_verilator.elf --eval-command \"target remote :1234\"'" |
gkielian | 1c4a1fd | 2021-03-24 14:35:53 -0700 | [diff] [blame] | 104 | alias run_qemu="qemu-system-riscv32 -M opentitan -kernel $ROOTDIR/out/shodan/build-out/sw_shodan/device/examples/hello_vector/hello_vector_sim_verilator.elf -bios $ROOTDIR/out/shodan/build-bin/sw/device/boot_rom/boot_rom_fpga_nexysvideo.elf -nographic -cpu rv32,x-v=true,vlen=512,vext_spec=v1.0 -s -S" |
| 105 | ``` |
| 106 | |
| 107 | ## Advanced Usage |
| 108 | |
| 109 | ### Debugging Qemu |
| 110 | |
| 111 | ``` |
| 112 | gdb --eval-command "b helper_vsetvl" --eval-command "run" --args qemu-system-riscv32 -s -S -nographic -cpu rv32,x-v=true,vlen=512,vext_spec=v1.0,s=true,mmu=true -M opentitan -kernel out/shodan/build-out/sw_shodan/device/examples/hello_vector/hello_vector_sim_verilator.elf -bios out/shodan/build-bin/sw/device/boot_rom/boot_rom_fpga_nexysvideo.elf -nographic |
| 113 | ``` |
| 114 | |
| 115 | Above starts up qemu with the debugger and breaks the simulator on the |
| 116 | `vsetvl` instruction so that you can check the state of the cpu. |
| 117 | |
gkielian | d5dc616 | 2021-04-01 15:07:58 -0700 | [diff] [blame] | 118 | This may be useful when debugging qemu itself. |