From 179e184e30e3606de88deb424ee0b9952bcec7ce Mon Sep 17 00:00:00 2001 From: Flavio Ceolin Date: Fri, 31 Jul 2020 14:27:38 -0700 Subject: [PATCH] samples: debug: Add a simple gdbstub sample Sample example built with gdbstub enabled. Two serials are used, one for normal output and another one that is used talk with gdb in the host machine. Signed-off-by: Flavio Ceolin --- samples/debug/gdbstub/CMakeLists.txt | 12 +++++++++ samples/debug/gdbstub/README.rst | 31 +++++++++++++++++++++++ samples/debug/gdbstub/prj.conf | 4 +++ samples/debug/gdbstub/run.gdbinit | 17 +++++++++++++ samples/debug/gdbstub/sample.yaml | 13 ++++++++++ samples/debug/gdbstub/src/main.c | 37 ++++++++++++++++++++++++++++ samples/debug/index.rst | 10 ++++++++ samples/index.rst | 1 + 8 files changed, 125 insertions(+) create mode 100644 samples/debug/gdbstub/CMakeLists.txt create mode 100644 samples/debug/gdbstub/README.rst create mode 100644 samples/debug/gdbstub/prj.conf create mode 100644 samples/debug/gdbstub/run.gdbinit create mode 100644 samples/debug/gdbstub/sample.yaml create mode 100644 samples/debug/gdbstub/src/main.c create mode 100644 samples/debug/index.rst diff --git a/samples/debug/gdbstub/CMakeLists.txt b/samples/debug/gdbstub/CMakeLists.txt new file mode 100644 index 0000000000..c442f5739a --- /dev/null +++ b/samples/debug/gdbstub/CMakeLists.txt @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) + +if(BOARD MATCHES "qemu_x86") + list(APPEND QEMU_EXTRA_FLAGS -serial tcp:127.0.0.1:1234,server) +endif() + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(debug) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/debug/gdbstub/README.rst b/samples/debug/gdbstub/README.rst new file mode 100644 index 0000000000..d6d85f5209 --- /dev/null +++ b/samples/debug/gdbstub/README.rst @@ -0,0 +1,31 @@ +.. _debug_sample: + +Debug Sample +############ + +Overview +******** + +A simple sample that can be used with qemu to show debug using gdb +remote serial protocol capabilities. + +Building and Running +******************** + +This application can be built and executed on QEMU as follows: + +.. zephyr-app-commands:: + :zephyr-app: samples/debug + :host-os: unix + :board: qemu_x86 + :goals: run + :compact: + +Open a new terminal and use gdb to connect to the running qemu as follows: + +.. code-block:: bash + + gdb build/zephyr/zephyr.elf + (gdb) target remote :1234 + +Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`. diff --git a/samples/debug/gdbstub/prj.conf b/samples/debug/gdbstub/prj.conf new file mode 100644 index 0000000000..bf3116addb --- /dev/null +++ b/samples/debug/gdbstub/prj.conf @@ -0,0 +1,4 @@ +CONFIG_GDBSTUB=y +CONFIG_GDBSTUB_SERIAL_BACKEND_NAME="UART_1" +CONFIG_NO_OPTIMIZATIONS=y +CONFIG_USERSPACE=y diff --git a/samples/debug/gdbstub/run.gdbinit b/samples/debug/gdbstub/run.gdbinit new file mode 100644 index 0000000000..49181157ae --- /dev/null +++ b/samples/debug/gdbstub/run.gdbinit @@ -0,0 +1,17 @@ +set pagination off +symbol-file zephyr/zephyr.elf +target remote 127.0.0.1:1234 +b test +b main.c:33 +c + +s +set var a = 2 +c +if ret == 6 + printf "PASSED\n" + quit 0 +else + printf "FAILED\n" + quit 1 +end diff --git a/samples/debug/gdbstub/sample.yaml b/samples/debug/gdbstub/sample.yaml new file mode 100644 index 0000000000..6709029d6f --- /dev/null +++ b/samples/debug/gdbstub/sample.yaml @@ -0,0 +1,13 @@ +# +# Copyright (c) 2020 intel Corporation. +# +# SPDX-License-Identifier: Apache-2.0 +# + +sample: + name: gdbstub sample +tests: + sample.debug.gdbstub: + build_only: true + platform_allow: qemu_x86 + tags: debug diff --git a/samples/debug/gdbstub/src/main.c b/samples/debug/gdbstub/src/main.c new file mode 100644 index 0000000000..bb8916c0de --- /dev/null +++ b/samples/debug/gdbstub/src/main.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 Intel Corporation. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define STACKSIZE 512 + +static int test(void) +{ + int a; + int b; + + a = 10; + b = a * 2; + + return a + b; +} + +static void thread_entry(void *p1, void *p2, void *p3) +{ + printk("Hello from user thread!\n"); +} + +void main(void) +{ + int ret; + + ret = test(); + printk("%d\n", ret); +} + +K_THREAD_DEFINE(thread, STACKSIZE, thread_entry, NULL, NULL, NULL, + 7, K_USER, 0); diff --git a/samples/debug/index.rst b/samples/debug/index.rst new file mode 100644 index 0000000000..3e28648b1f --- /dev/null +++ b/samples/debug/index.rst @@ -0,0 +1,10 @@ +.. _debug-samples: + +Debug Samples +################# + +.. toctree:: + :maxdepth: 1 + :glob: + + **/* diff --git a/samples/index.rst b/samples/index.rst index deccd1cb1b..8581e763db 100644 --- a/samples/index.rst +++ b/samples/index.rst @@ -27,6 +27,7 @@ Samples and Demos scheduler/* smp/* tfm_integration/tfm_integration.rst + debug/* .. comment To add a new sample document, please use the template available under