From 129d3df8a6858e4997b0b53b0fea1118b01b43c8 Mon Sep 17 00:00:00 2001
From: Jerome Forissier <jerome.forissier@linaro.org>
Date: Tue, 21 Aug 2018 09:54:43 +0200
Subject: [PATCH] buildroot: add support for debugging Normal World
 applications

Build with GDBSERVER=y to enable remote debugging of Normal World
applications. gdbserver is added to the root filesystem.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
---
 br-ext/configs/gdbserver.conf |  6 ++++++
 br-ext/configs/toolchain-br   |  3 +++
 common.mk                     | 13 ++++++++++++-
 docs/qemu.md                  | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 br-ext/configs/gdbserver.conf
 create mode 100644 br-ext/configs/toolchain-br

diff --git a/br-ext/configs/gdbserver.conf b/br-ext/configs/gdbserver.conf
new file mode 100644
index 0000000..726736a
--- /dev/null
+++ b/br-ext/configs/gdbserver.conf
@@ -0,0 +1,6 @@
+BR2_ENABLE_DEBUG=y
+BR2_PACKAGE_GDB=y
+BR2_PACKAGE_HOST_GDB=y
+BR2_TOOLCHAIN_BUILDROOT_CXX=y
+BR2_TOOLCHAIN_BUILDROOT_GLIBC=y
+BR2_PACKAGE_GLIBC=y
diff --git a/br-ext/configs/toolchain-br b/br-ext/configs/toolchain-br
new file mode 100644
index 0000000..c617d08
--- /dev/null
+++ b/br-ext/configs/toolchain-br
@@ -0,0 +1,3 @@
+BR2_TOOLCHAIN_BUILDROOT=y
+BR2_GCC_VERSION_6_X=y
+BR2_KERNEL_HEADERS_4_10=y
diff --git a/common.mk b/common.mk
index f4232f9..c170f0b 100644
--- a/common.mk
+++ b/common.mk
@@ -183,11 +183,17 @@ busybox-cleaner-common:
 # Build root
 ################################################################################
 BUILDROOT_ARCH=aarch$(COMPILE_NS_USER)
+ifeq ($(GDBSERVER),y)
+BUILDROOT_TOOLCHAIN=toolchain-br # Use toolchain supplied by buildroot
+DEFCONFIG_GDBSERVER=--br-defconfig build/br-ext/configs/gdbserver.conf
+else
+# Local toolchains (downloaded by "make toolchains")
 ifeq ($(COMPILE_LEGACY),)
 BUILDROOT_TOOLCHAIN=toolchain-aarch$(COMPILE_NS_USER)
 else
 BUILDROOT_TOOLCHAIN=toolchain-aarch$(COMPILE_NS_USER)-legacy
 endif
+endif
 BUILDROOT_GETTY_PORT ?= \
 	$(if $(CFG_NW_CONSOLE_UART),ttyAMA$(CFG_NW_CONSOLE_UART),ttyAMA0)
 .PHONY: buildroot
@@ -230,6 +236,7 @@ endif
 		--br-defconfig build/br-ext/configs/optee_$(BUILDROOT_ARCH) \
 		--br-defconfig build/br-ext/configs/optee_generic \
 		--br-defconfig build/br-ext/configs/$(BUILDROOT_TOOLCHAIN) \
+		$(DEFCONFIG_GDBSERVER) \
 		--br-defconfig out-br/extra.conf \
 		--make-cmd $(MAKE))
 	@$(MAKE) -C ../out-br all
@@ -306,8 +313,12 @@ QEMU_EXTRA_ARGS +=\
 	-device virtio-9p-device,fsdev=fsdev0,mount_tag=host
 endif
 
+ifeq ($(GDBSERVER),y)
+HOSTFWD := ,hostfwd=tcp::12345-:12345
+endif
 # Enable QEMU SLiRP user networking
-QEMU_EXTRA_ARGS += -netdev user,id=vmnic -device virtio-net-device,netdev=vmnic
+QEMU_EXTRA_ARGS +=\
+	-netdev user,id=vmnic$(HOSTFWD) -device virtio-net-device,netdev=vmnic
 
 define run-help
 	@echo
diff --git a/docs/qemu.md b/docs/qemu.md
index f1cdc2c..0d3f612 100644
--- a/docs/qemu.md
+++ b/docs/qemu.md
@@ -53,6 +53,38 @@ gateway to the host network [SLiRP].
 Please note that ICMP won't work in the guest unless additional configuration is
 made, so the `ping` utility won't work.
 
+# 6. Remote debugging of Normal World applications
+If you need to debug a client application, using GDB in a remote debugging
+configuration may be useful. Remote debugging means `gdb` runs on your PC, where
+it can access the source code, while the program being debugged runs on the remote
+system (in this case, in the QEMU environment in normal world).
+Here is how to do that. On your PC, build with `GDBSERVER=y`:
+```
+$ cd build
+$ make -j8 run GDBSERVER=y
+[...]
+(qemu) c
+```
+Inside QEMU, run your application with gdbserver (for example `xtest 4002`):
+```
+# gdbserver :12345 xtest 4002
+Process xtest created; pid = 654
+Listening on port 12345
+
+```
+Back on your PC, in another terminal, start GDB and connect to the target:
+```
+$ ../out-br/host/bin/arm-buildroot-linux-gnueabihf-gdb
+(gdb) set sysroot ../out-br/host/arm-buildroot-linux-gnueabihf/sysroot
+(gdb) target remote :12345
+```
+Now GDB is connected to the remote application. You may use GDB normally.
+```
+(gdb) b main
+(gdb) c
+etc.
+```
+
 [bios]: https://github.com/linaro-swg/bios_qemu_tz_arm
 [README.md]: ../README.md
 [SLiRP]: https://wiki.qemu.org/Documentation/Networking#User_Networking_.28SLIRP.29
-- 
GitLab