Learn with SHAKTI
Debugging an ASM Program with GDB
Debugging with GDB is more of a real time debugging, where the debugger takes control of the system. To debug a C or Assembly program, RISC-V GDB, spike and OpenOCD are required. Learn more about spike, vim editor, RISC-V GDB and OpenOCD!
- spike simulates a real hardware.
- OpenOCD (Open On-Chip Debugger) is open-source software that is needed to enter into the debug mode in the hardware.
Before starting Debugging session with RISC-V GDB. Please ensure example.elf is generated as described in previous section.
STEP 1: Building the boot image.
The necessary files to compile and simulate an ASM program in spike environment, are hosted inside the spiking folder. Please ensure spiking folder is downloaded.
cd spiking
To update the spiking folder:
git pull
bootload.S:
There is an assembly file named bootload.S in the spiking folder. This assembly file contains the actual boot code for our simulation. The example.elf is the application, which we are going to debug using GDB.
The contents of the bootload.S is listed below.
#This program runs in infinite loop _start: addi x10, x0, 1 # x10 = x0+1 bnez x10, _start # jump to _start if branch not equal to zero
Compiling bootload.S and generating bootload.elf:
riscv64-unknown-elf-gcc -nostdlib -nostartfiles -T spike.lds bootload.S -o bootload.elf
Note: Alternatively, "make" command can be used to compile all programs in the spiking folder
STEP 2: Debugging
If you face any error while running any command, Please refer FAQ section. What are the commands used in debugging with RISC-V GDB?
$(which spike) --rbb-port=9824 -m0x10010000:0x20000 bootload.elf $(which pk)
Listening for remote bitbang connection on port 9824.
warning: tohost and fromhost symbols not in ELF; can't communicate with target
sudo $(which openocd) -f spike.cfg
[sudo] password for rise:
Open On-Chip Debugger 0.10.0+dev-00615-gfd9c54b (2020-01-10-16:29) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html Warn : Adapter driver 'remote_bitbang' did not declare which transports it allows; assuming legacy JTAG-only Info : only one transport option; autoselect 'jtag' Info : Initializing remote_bitbang driver Info : Connecting to localhost:9824 Info : remote_bitbang driver initialized Info : This adapter doesn't support configurable speed Info : JTAG tap: riscv.cpu tap/device found: 0xdeadbeef (mfg: 0x777 (), part: 0xeadb, ver: 0xd) Warn : JTAG tap: riscv.cpu UNEXPECTED: 0xdeadbeef (mfg: 0x777 ( ), part: 0xeadb, ver: 0xd) Error: JTAG tap: riscv.cpu expected 1 of 1: 0x10e31913 (mfg: 0x489 (SiFive, Inc.), part: 0x0e31, ver: 0x1) Error: Trying to use configured scan chain anyway... Warn : Bypassing JTAG setup events due to errors Info : datacount=2 progbufsize=2 Info : Disabling abstract command reads from CSRs. Info : Examined RISC-V core; found 1 harts Info : hart 0: XLEN=64, misa=0x800000000014112d Info : Listening on port 3333 for gdb connections Info : Listening on port 6666 for tcl connections Info : Listening on port 4444 for telnet connections Info : accepting 'gdb' connection on tcp/3333 Info : dropped 'gdb' connection Info : accepting 'gdb' connection on tcp/3333 Info : Disabling abstract command writes to CSRs.
riscv64-unknown-elf-gdb
GNU gdb (GDB) 9.1 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> 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-pc-linux-gnu --target=riscv64-unknown-elf". Type "show configuration" for configuration details. For bug reporting instructions, please see: < http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: < http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word".
(gdb) target remote localhost:3333
Remote debugging using localhost:3333 warning: No executable has been specified and target does not support determining executable automatically. Try using the "file" command. 0x0000000010010004 in ?? ()
(gdb) file example.elf
A program is being debugged already. Are you sure you want to change the file? (y or n) y Reading symbols from example.elf... (No debugging symbols found in example.elf)
(gdb) load
Loading section .text, size 0x44 lma 0x10010000 Loading section .data, size 0x10 lma 0x10010044 Start address 0x10010000, load size 84 Transfer rate: 1 KB/sec, 42 bytes/write.
(gdb) si
0x0000000010010004 in _start ()
(gdb) si
0x0000000010010008 in _start ()
(gdb) si
0x000000001001000c in _start ()
(gdb) si
0x0000000010010010 in _start ()
(gdb) si
0x0000000010010014 in _start ()
(gdb) si
0x0000000010010016 in _start ()
(gdb) si
0x000000001001001a in _start ()
(gdb) si
0x000000001001001e in _start ()
(gdb) si
0x0000000010010022 in _start ()
(gdb) si
0x0000000010010026 in _start ()
(gdb) si
0x000000001001002a in _start ()
(gdb) si
0x000000001001002e in loop ()
(gdb) si
0x0000000010010032 in loop ()
(gdb) si
0x0000000010010034 in loop ()
(gdb) info reg
ra 0x0 0x0 sp 0x0 0x0 gp 0x0 0x0 tp 0x0 0x0 t0 0x10010000 268500992 t1 0x0 0 t2 0x0 0 fp 0x0 0x0 s1 0x0 0 a0 0x1 1 a1 0x1020 4128 a2 0xe 14 a3 0x0 0 a4 0x0 0 a5 0x10010048 268501064 a6 0x7 7 a7 0x10 16 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x0 0 t4 0x0 0 t5 0x0 0 t6 0x0 0 pc 0x10010034 0x10010034 < loop+6>
(gdb) si
0x0000000010010036 in loop ()
(gdb) info reg
ra 0x0 0x0 sp 0x0 0x0 gp 0x0 0x0 tp 0x0 0x0 t0 0x10010000 268500992 t1 0x0 0 t2 0x0 0 fp 0x0 0x0 s1 0x0 0 a0 0x1 1 a1 0x1020 4128 a2 0xe 14 a3 0x0 0 a4 0x4 4 a5 0x10010048 268501064 a6 0x7 7 a7 0x10 16 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x0 0 t4 0x0 0 t5 0x0 0 t6 0x0 0 pc 0x10010036 0x10010036 < loop+8>
(gdb) si
0x000000001001002e in loop ()
(gdb) si
0x0000000010010032 in loop ()
(gdb) info reg
ra 0x0 0x0 sp 0x0 0x0 gp 0x0 0x0 tp 0x0 0x0 t0 0x10010000 268500992 t1 0x0 0 t2 0x0 0 fp 0x0 0x0 s1 0x0 0 a0 0x1 1 a1 0x1020 4128 a2 0xe 14 a3 0x0 0 a4 0x4 4 a5 0x10010048 268501064 a6 0x6 6 a7 0x10 16 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x0 0 t4 0x0 0 t5 0x0 0 t6 0x0 0 pc 0x10010032 0x10010032 < loop+4>
(gdb) si
0x0000000010010034 in loop ()
(gdb) si
0x0000000010010036 in loop ()
(gdb) si
0x000000001001002e in loop ()
(gdb) si
0x0000000010010032 in loop ()
(gdb) si
0x0000000010010034 in loop ()
(gdb) si
0x0000000010010036 in loop ()
(gdb) si
0x000000001001002e in loop ()
(gdb) si
0x0000000010010032 in loop ()
(gdb) si
0x0000000010010034 in loop ()
(gdb) si
0x0000000010010036 in loop ()
(gdb) si
0x000000001001003a in loop ()
(gdb) si
0x000000001001003e in loop ()
(gdb) info reg
ra 0x0 0x0 sp 0x0 0x0 gp 0x0 0x0 tp 0x0 0x0 t0 0x10010000 268500992 t1 0x0 0 t2 0x0 0 fp 0x0 0x0 s1 0x0 0 a0 0x1 1 a1 0x1020 4128 a2 0xe 14 a3 0x0 0 a4 0x10 16 a5 0x10010054 268501076 a6 0x4 4 a7 0x10 16 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x0 0 t4 0x0 0 t5 0x0 0 t6 0x0 0 pc 0x1001003e 0x1001003e < loop+16>
(gdb) si
0x0000000010010040 in loop ()
(gdb) si
0x0000000010010000 in _start ()
(gdb) info reg
ra 0x0 0x0 sp 0x0 0x0 gp 0x0 0x0 tp 0x0 0x0 t0 0x10010000 268500992 t1 0x0 0 t2 0x0 0 fp 0x0 0x0 s1 0x0 0 a0 0x1 1 a1 0x1020 4128 a2 0x10 16 a3 0x0 0 a4 0x10 16 a5 0x10010054 268501076 a6 0x4 4 a7 0x10 16 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x0 0 t4 0x0 0 t5 0x0 0 t6 0x0 0 pc 0x10010000 0x10010000 <_start>