Learn with SHAKTI
Debugging a C Program with GDB
The necessary files to compile and simulate a C program in spike environment, are hosted inside the spiking folder.
Please ensure spiking folder is downloaded
STEP 1: Building the executables.
sample.c is a C file in spiking folder. Run the below commands to compile sample.c. This will generate sample.elf. sample.c is a simple c file with while loops and if statements.
cd spiking
riscv64-unknown-elf-gcc -g -Og -o sample.o -c sample.c
riscv64-unknown-elf-gcc -g -Og -T spike.lds -nostartfiles -o sample.elf sample.o
bootload.elf is the bootimage. Run the below command to generate it:
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 execution, Please refer FAQ section.
$(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 Python Exception'gdb.Inferior' object has no attribute 'progspace': 0x0000000000000000 in ?? ()
(gdb) file sample.elf
A program is being debugged already. Are you sure you want to change the file? (y or n) y Reading symbols from sample.elf...(no debugging symbols found)...done.
(gdb) load
Loading section .text, size 0x144 lma 0x10010000 Loading section .data, size 0xa4 lma 0x10010148 Loading section .sdata, size 0x4 lma 0x100101ec Start address 0x10010000, load size 492 Transfer rate: 1 KB/sec, 164 bytes/write.
(gdb) print flag
$1 = 1
(gdb) print string
$3 = "This Is $hakti, The 1st Ever 'Made In India' Computer Chip"
(gdb) print result
$4 = '\000'
(gdb) b end
Breakpoint 1 at 0x100100a0: file sample.c, line 26.
(gdb) set flag=0
Python Exception'gdb.Inferior' object has no attribute 'progspace':
(gdb) print flag
$5 = 0
(gdb) print string
$6 = "This Is $hakti, The 1st Ever 'Made In India' Computer Chip"
(gdb) print result
$7 = '\000'
(gdb) p i
$8 = 0
(gdb) print wait=0
No symbol "wait" in current context.
(gdb) print flag=0
Python Exception'gdb.Inferior' object has no attribute 'progspace': $9 = 0
(gdb) b end
Note: breakpoint 1 also set at pc 0x100100a0. Breakpoint 2 at 0x100100a0: file sample.c, line 26.
(gdb) print i
$10 = 0
(gdb) bt
Python Exception'gdb.Inferior' object has no attribute 'progspace': Python Exception 'gdb.Inferior' object has no attribute 'progspace': #0 0x0000000010010004 in main () at sample.c:9
(gdb) info reg
ra 0x0000000000000000 0 sp 0x0000000000000000 0 gp 0x0000000000000000 0 tp 0x0000000000000000 0 t0 0x0000000010010000 268500992 t1 0x0000000000000000 0 t2 0x0000000000000000 0 fp 0x0000000000000000 0 s1 0x0000000000000000 0 a0 0x0000000000000000 0 a1 0x0000000000001020 4128 a2 0x0000000000000000 0 a3 0x0000000000000000 0 a4 0x0000000000000000 0 a5 0x0000000010010000 268500992 a6 0x0000000000000000 0 a7 0x0000000000000000 0 s2 0x0000000000000000 0 s3 0x0000000000000000 0 s4 0x0000000000000000 0 s5 0x0000000000000000 0 s6 0x0000000000000000 0 s7 0x0000000000000000 0 s8 0x0000000000000000 0 s9 0x0000000000000000 0 s10 0x0000000000000000 0 s11 0x0000000000000000 0 t3 0x0000000000000000 0 t4 0x0000000000000000 0 t5 0x0000000000000000 0 t6 0x0000000000000000 0 pc 0x0000000010010004 268500996 priv 0x10010003 prv:3 [Machine]
(gdb) si
Disabling abstract command writes to CSRs. Python Exception'gdb.Inferior' object has no attribute 'progspace': 0x0000000010010008 9 while (flag);
(gdb) si
Python Exception'gdb.Inferior' object has no attribute 'progspace': 0x000000001001000a 9 while (flag);
(gdb) si
Python Exception'gdb.Inferior' object has no attribute 'progspace': 11 int i = 0;
(gdb) si
Python Exception'gdb.Inferior' object has no attribute 'progspace': 0x000000001001000e 11 int i = 0;
(gdb) c
Continuing. Breakpoint 1, Python Exception'gdb.Inferior' object has no attribute 'progspace': main () at sample.c:27 27 while (!flag);
(gdb) print result
$11 = "ulllsulsslllllssullsnllsulllssulllsulsullllssulllllllsulllssssss", 'l'
(gdb) print i
$12 = 100