Browse Source

Updated “Debugging With Gdb” to use semihosting

- make the sample program as a typical C program
  that uses the stack and semihosting (#155).
- use the default crt0.o and  linker script.

Signed-off-by: Hiroo HAYASHI <24754036+hirooih@users.noreply.github.com>
pull/2234/head
Hiroo HAYASHI 2 years ago
parent
commit
82742081e9
  1. 104
      README.md

104
README.md

@ -218,12 +218,12 @@ Debugging With Gdb
------------------ ------------------
An alternative to interactive debug mode is to attach using gdb. Because spike An alternative to interactive debug mode is to attach using gdb. Because spike
tries to be like real hardware, you also need OpenOCD to do that. OpenOCD tries to be like real hardware, you also need OpenOCD to do that.
doesn't currently know about address translation, so it's not possible to We'll use the following test program:
easily debug programs that are run under `pk`. We'll use the following test
program:
``` ```
$ cat rot13.c $ cat rot13.c
#include <stdio.h>
char text[] = "Vafgehpgvba frgf jnag gb or serr!"; char text[] = "Vafgehpgvba frgf jnag gb or serr!";
// Don't use the stack, because sp isn't set up. // Don't use the stack, because sp isn't set up.
@ -231,10 +231,6 @@ volatile int wait = 1;
int main() int main()
{ {
while (wait)
;
// Doesn't actually go on the stack, because there are lots of GPRs.
int i = 0; int i = 0;
while (text[i]) { while (text[i]) {
char lower = text[i] | 32; char lower = text[i] | 32;
@ -244,28 +240,17 @@ int main()
text[i] -= 13; text[i] -= 13;
i++; i++;
} }
done: done:
while (!wait) printf("decoded text: %s\n", text);
;
}
$ cat spike.lds
OUTPUT_ARCH( "riscv" )
SECTIONS
{
. = 0x10110000;
.text : { *(.text) }
.data : { *(.data) }
} }
$ riscv64-unknown-elf-gcc -g -Og -o rot13-64.o -c rot13.c $ riscv64-unknown-elf-gcc -g -Og --specs=semihost.specs -o rot13 rot13.c
$ riscv64-unknown-elf-gcc -g -Og -T spike.lds -nostartfiles -o rot13-64 rot13-64.o
``` ```
To debug this program, first run spike telling it to listen for OpenOCD: To debug this program, first run spike telling it to listen for OpenOCD:
``` ```
$ spike --rbb-port=9824 -m0x10100000:0x20000 rot13-64 $ spike --rbb-port=9824 -m0x10000:0x20000 rot13
Listening for remote bitbang connection on port 9824. Listening for remote bitbang connection on port 9824.
...
``` ```
In a separate shell run OpenOCD with the appropriate configuration file: In a separate shell run OpenOCD with the appropriate configuration file:
@ -281,54 +266,59 @@ jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0xdeadbeef
set _TARGETNAME $_CHIPNAME.cpu set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME riscv -chain-position $_TARGETNAME target create $_TARGETNAME riscv -chain-position $_TARGETNAME
gdb_report_data_abort enable gdb report_data_abort enable
init init
arm semihosting enable
halt halt
$ openocd -f spike.cfg $ openocd -f spike.cfg
Open On-Chip Debugger 0.10.0-dev-00002-gc3b344d (2017-06-08-12:14) Open On-Chip Debugger 0.12.0
...
Info : starting gdb server for riscv.cpu on 3333
Info : Listening on port 3333 for gdb connections
riscv.cpu halted due to debug-request. Semihosting is active.
... ...
riscv.cpu: target state: halted riscv.cpu: target state: halted
``` ```
In yet another shell, start your gdb debug session: In yet another shell, start your gdb debug session:
``` ```
tnewsome@compy-vm:~/SiFive/spike-test$ riscv64-unknown-elf-gdb rot13-64 $ riscv64-unknown-elf-gdb rot13
GNU gdb (GDB) 8.0.50.20170724-git ...
Copyright (C) 2017 Free Software Foundation, Inc. Reading symbols from rot13...
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> (gdb) target extended-remote localhost:3333
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" (gdb) load
and "show warranty" for details. ...
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv64-unknown-elf". (gdb) set $sp=0x2fff0
Type "show configuration" for configuration details. (gdb) b main
For bug reporting instructions, please see: Breakpoint 1 at 0x10202: file rot13.c, line 5.
<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"...
Reading symbols from rot13-64...done.
(gdb) target remote localhost:3333
Remote debugging using localhost:3333
0x0000000010010004 in main () at rot13.c:8
8 while (wait)
(gdb) print wait
$1 = 1
(gdb) print wait=0
$2 = 0
(gdb) print text
$3 = "Vafgehpgvba frgf jnag gb or serr!"
(gdb) b done
Breakpoint 1 at 0x10110064: file rot13.c, line 22.
(gdb) c (gdb) c
Continuing. Continuing.
Disabling abstract command writes to CSRs. Disabling abstract command writes to CSRs.
Breakpoint 1, main () at rot13.c:23 Breakpoint 1, main () at rot13.c:5
23 while (!wait) 5 {
(gdb) print wait
$4 = 0
(gdb) print text (gdb) print text
$1 = "Vafgehpgvba frgf jnag gb or serr!"
(gdb) until done
[riscv.cpu] Found 4 triggers
main () at rot13.c:16
16 printf("decoded text: %s\n", text);
(gdb) c
Continuing.
Program received signal SIGTRAP, Trace/breakpoint trap.
0x00019ff8 in _exit ()
(gdb)
...
```
On the OpenOCD terminal you will have:
```
... ...
decoded text: Instruction sets want to be free!
semihosting: *** application exited with 0 ***
riscv.cpu halted due to breakpoint. Semihosting is active.
``` ```

Loading…
Cancel
Save