Skip to contentSkip to conversation

OpenRISC Linux Native and GDBServer Support

PATCH New Stafford Horne 2 participants 10 messages Sep 13, 2021 – Oct 20, 2021

lore
Share & tools
lore
#0 lore raw

cover OpenRISC Linux Native and GDBServer Support

Hello,

As I have been working on the OpenRISC glibc port over the last few years now I have had the need to have Linux native debugging. At first I started with the gdbserver, but quickly grew out of that and just settled on running gdb native on the platform.

These are the patches I have been using.

Stafford Horne (5):
gdb: or1k: implement gdb server
gdb: or1k: fixup linux regcache comment
gdb: or1k: add generated linux descriptor file
gdb: or1k: add native linux support
gdb: or1k: add single step for linux native debugging

 gdb/Makefile.in               |   2 +
 gdb/configure.host            |   2 +
 gdb/configure.nat             |   4 +
 gdb/features/Makefile         |   1 +
 gdb/features/or1k-linux.c     |  78 ++++++++++
 gdb/features/or1k-linux.xml   |  13 ++
 gdb/or1k-linux-nat.c          | 210 +++++++++++++++++++++++++++
 gdb/or1k-linux-tdep.c         |  14 +-
 gdb/or1k-linux-tdep.h         |  26 ++++
 gdb/or1k-tdep.c               |  66 ++++++---
 gdb/or1k-tdep.h               |   5 +
 gdb/regformats/or1k-linux.dat |  40 +++++
 gdbserver/Makefile.in         |   1 +
 gdbserver/configure.srv       |   8 +
 gdbserver/linux-or1k-low.cc   | 266 ++++++++++++++++++++++++++++++++++
 15 files changed, 714 insertions(+), 22 deletions(-)
 create mode 100644 gdb/features/or1k-linux.c
 create mode 100644 gdb/features/or1k-linux.xml
 create mode 100644 gdb/or1k-linux-nat.c
 create mode 100644 gdb/or1k-linux-tdep.h
 create mode 100644 gdb/regformats/or1k-linux.dat
 create mode 100644 gdbserver/linux-or1k-low.cc
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/5 gdb: or1k: implement gdb server

This patch adds gdbserver support for OpenRISC. This has been used for debugging the glibc port that in being worked on here:

https://github.com/openrisc/or1k-glibc/tree/or1k-port-2

Hence the comment about registers definitions being inline with glibc.

---
 gdb/features/Makefile         |   1 +
 gdb/features/or1k-linux.xml   |  13 ++
 gdb/regformats/or1k-linux.dat |  40 +++++
 gdbserver/Makefile.in         |   1 +
 gdbserver/configure.srv       |   8 +
 gdbserver/linux-or1k-low.cc   | 266 ++++++++++++++++++++++++++++++++++
 6 files changed, 329 insertions(+)
 create mode 100644 gdb/features/or1k-linux.xml
 create mode 100644 gdb/regformats/or1k-linux.dat
 create mode 100644 gdbserver/linux-or1k-low.cc
gdb/features/Makefile +1 -0
index aa38d176539..d467fe7145f 100644
@@ -47,6 +47,7 @@
 WHICH = mips-linux mips-dsp-linux \
 	mips64-linux mips64-dsp-linux \
 	nios2-linux \
+	or1k-linux \
 	rs6000/powerpc-32 \
 	rs6000/powerpc-32l rs6000/powerpc-altivec32l rs6000/powerpc-e500l \
 	rs6000/powerpc-64l rs6000/powerpc-altivec64l rs6000/powerpc-vsx32l \
gdb/features/or1k-linux.xml new file +13 -0
new file mode 100644
index 00000000000..598538d124f
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2021 Free Software Foundation, Inc.
+
+     Copying and distribution of this file, with or without modification,
+     are permitted in any medium without royalty provided the copyright
+     notice and this notice are preserved.  -->
+
+<!DOCTYPE target SYSTEM "gdb-target.dtd">
+<target>
+  <architecture>or1k</architecture>
+  <osabi>GNU/Linux</osabi>
+  <xi:include href="or1k-core.xml"/>
+</target>
gdb/regformats/or1k-linux.dat new file +40 -0
new file mode 100644
index 00000000000..7b64646fc65
@@ -0,0 +1,40 @@
+# THIS FILE IS GENERATED.  -*- buffer-read-only: t -*- vi :set ro:
+# Generated from: or1k-linux.xml
+name:or1k_linux
+xmltarget:or1k-linux.xml
+expedite:r1,npc
+32:r0
+32:r1
+32:r2
+32:r3
+32:r4
+32:r5
+32:r6
+32:r7
+32:r8
+32:r9
+32:r10
+32:r11
+32:r12
+32:r13
+32:r14
+32:r15
+32:r16
+32:r17
+32:r18
+32:r19
+32:r20
+32:r21
+32:r22
+32:r23
+32:r24
+32:r25
+32:r26
+32:r27
+32:r28
+32:r29
+32:r30
+32:r31
+32:ppc
+32:npc
+32:sr
gdbserver/Makefile.in +1 -0
index 12e9b2777ae..d524a7e8775 100644
@@ -196,6 +196,7 @@ SFILES = \
 	$(srcdir)/linux-m68k-low.cc \
 	$(srcdir)/linux-mips-low.cc \
 	$(srcdir)/linux-nios2-low.cc \
+	$(srcdir)/linux-or1k-low.cc \
 	$(srcdir)/linux-ppc-low.cc \
 	$(srcdir)/linux-riscv-low.cc \
 	$(srcdir)/linux-s390-low.cc \
gdbserver/configure.srv +8 -0
index 971f537bffa..6e09b0eeb79 100644
@@ -175,6 +175,14 @@ case "${gdbserver_host}" in
 			srv_linux_usrregs=yes
 			srv_linux_thread_db=yes
 			;;
+  or1k*-*-linux*)	srv_regobj="or1k-linux.o"
+			srv_tgtobj="$srv_linux_obj linux-or1k-low.o"
+			srv_xmlfiles="or1k-linux.xml"
+			srv_xmlfiles="${srv_xmlfiles} or1k-core.xml"
+			srv_linux_regsets=yes
+			srv_linux_usrregs=yes
+			srv_linux_thread_db=yes
+			;;
   powerpc*-*-linux*)	srv_regobj="powerpc-32l.o"
 			srv_regobj="${srv_regobj} powerpc-altivec32l.o"
 			srv_regobj="${srv_regobj} powerpc-vsx32l.o"
gdbserver/linux-or1k-low.cc new file +266 -0

266 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw

PATCH 1/5 gdb: or1k: implement gdb server

Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

">" == Stafford Horne via Gdb-patches <[email protected]> writes:

This patch adds gdbserver support for OpenRISC. This has been used for debugging the glibc port that in being worked on here:

https://github.com/openrisc/or1k-glibc/tree/or1k-port-2

Hence the comment about registers definitions being inline with glibc.

Thanks for the patch. I had one minor comment.

Quoted 8 lines

gdbserver/linux-or1k-low.cc line 158

Quoted 8 lines
gdbserver/linux-or1k-low.cc new file +7 -0
+#define or1k_breakpoint_len 4
 
+bool
+or1k_target::low_breakpoint_at (CORE_ADDR where)
+{
+  unsigned int insn;
+
+  read_memory (where, (unsigned char *) &insn, 4);

Perhaps this ought to use or1k_breakpoint_len here.

Quoted 6 lines

gdbserver/linux-or1k-low.cc line 169

Quoted 6 lines
gdbserver/linux-or1k-low.cc new file +6 -0
+/* Fetch the thread-local storage pointer for libthread_db.  */
+
+ps_err_e
+ps_get_thread_area (struct ps_prochandle *ph,
+		    lwpid_t lwpid, int idx, void **base)
+{

Not your problem but it seems like a shame that this exact implementation is repeated several times in gdbserver.

Tom

#3 lore raw

PATCH 1/5 gdb: or1k: implement gdb server

Quoted 13 lines · Tom Tromey
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

">" == Stafford Horne via Gdb-patches <[email protected]> writes:

This patch adds gdbserver support for OpenRISC. This has been used for debugging the glibc port that in being worked on here:

https://github.com/openrisc/or1k-glibc/tree/or1k-port-2

Hence the comment about registers definitions being inline with glibc.

Thanks for the patch. I had one minor comment.

Quoted 1 line
Quoted 1 line

+#define or1k_breakpoint_len 4

Quoted 8 lines
Quoted 6 lines

gdbserver/linux-or1k-low.cc line 158

Quoted 6 lines
gdbserver/linux-or1k-low.cc new file +6 -0
+bool
+or1k_target::low_breakpoint_at (CORE_ADDR where)
+{
+  unsigned int insn;
+
+  read_memory (where, (unsigned char *) &insn, 4);

Perhaps this ought to use or1k_breakpoint_len here.

Right, I will update that before pushing.

Quoted 9 lines
Quoted 6 lines

gdbserver/linux-or1k-low.cc line 169

Quoted 6 lines
gdbserver/linux-or1k-low.cc new file +6 -0
+/* Fetch the thread-local storage pointer for libthread_db.  */
+
+ps_err_e
+ps_get_thread_area (struct ps_prochandle *ph,
+		    lwpid_t lwpid, int idx, void **base)
+{

Not your problem but it seems like a shame that this exact implementation is repeated several times in gdbserver.

I agree.

-Stafford

#4 lore raw

PATCH 2/5 gdb: or1k: fixup linux regcache comment

In reply to Stafford Horne

The old comment was not properly updated from the RISC-V example used. Update it to match OpenRISC.

---
 gdb/or1k-linux-tdep.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
gdb/or1k-linux-tdep.c +4 -3
index 2ad55f0d9ec..2efb4d0b0f9 100644
@@ -27,9 +27,10 @@
 #include "trad-frame.h"
 #include "gdbarch.h"
 
-/* Define the general register mapping.  The kernel puts the PC at offset 0,
-   gdb puts it at offset 32.  Register x0 is always 0 and can be ignored.
-   Registers x1 to x31 are in the same place.  */
+/* Define the general register mapping.  The kernel and GDB put registers
+   r1 to r31 in the same place.  The NPC register is stored at index 32 in
+   linux and 33 in GDB, in GDB 32 is for PPC which is not popupated from linux.
+   Register r0 is always 0 and can be ignored.  */
 
 static const struct regcache_map_entry or1k_linux_gregmap[] =
 {
Signature · 1 line
2.31.1
#5 lore raw

PATCH 3/5 gdb: or1k: add generated linux descriptor file

In reply to Stafford Horne
---
 gdb/features/or1k-linux.c | 78 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
 create mode 100644 gdb/features/or1k-linux.c
gdb/features/or1k-linux.c new file +78 -0
new file mode 100644
index 00000000000..e718d317990
@@ -0,0 +1,78 @@
+/* THIS FILE IS GENERATED.  -*- buffer-read-only: t -*- vi:set ro:
+  Original: or1k-linux.xml */
+
+#include "defs.h"
+#include "osabi.h"
+#include "target-descriptions.h"
+
+struct target_desc *tdesc_or1k_linux;
+static void
+initialize_tdesc_or1k_linux (void)
+{
+  target_desc_up result = allocate_target_description ();
+  set_tdesc_architecture (result.get (), bfd_scan_arch ("or1k"));
+
+  set_tdesc_osabi (result.get (), osabi_from_tdesc_string ("GNU/Linux"));
+
+  struct tdesc_feature *feature;
+
+  feature = tdesc_create_feature (result.get (), "org.gnu.gdb.or1k.group0");
+  tdesc_type_with_fields *type_with_fields;
+  type_with_fields = tdesc_create_flags (feature, "sr_flags", 4);
+  tdesc_add_flag (type_with_fields, 0, "SM");
+  tdesc_add_flag (type_with_fields, 1, "TEE");
+  tdesc_add_flag (type_with_fields, 2, "IEE");
+  tdesc_add_flag (type_with_fields, 3, "DCE");
+  tdesc_add_flag (type_with_fields, 4, "ICE");
+  tdesc_add_flag (type_with_fields, 5, "DME");
+  tdesc_add_flag (type_with_fields, 6, "IME");
+  tdesc_add_flag (type_with_fields, 7, "LEE");
+  tdesc_add_flag (type_with_fields, 8, "CE");
+  tdesc_add_flag (type_with_fields, 9, "F");
+  tdesc_add_flag (type_with_fields, 10, "CY");
+  tdesc_add_flag (type_with_fields, 11, "OV");
+  tdesc_add_flag (type_with_fields, 12, "OVE");
+  tdesc_add_flag (type_with_fields, 13, "DSX");
+  tdesc_add_flag (type_with_fields, 14, "EPH");
+  tdesc_add_flag (type_with_fields, 15, "FO");
+  tdesc_add_flag (type_with_fields, 16, "SUMRA");
+  tdesc_add_bitfield (type_with_fields, "CID", 28, 31);
+
+  tdesc_create_reg (feature, "r0", 0, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r1", 1, 1, NULL, 32, "data_ptr");
+  tdesc_create_reg (feature, "r2", 2, 1, NULL, 32, "data_ptr");
+  tdesc_create_reg (feature, "r3", 3, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r4", 4, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r5", 5, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r6", 6, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r7", 7, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r8", 8, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r9", 9, 1, NULL, 32, "code_ptr");
+  tdesc_create_reg (feature, "r10", 10, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r11", 11, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r12", 12, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r13", 13, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r14", 14, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r15", 15, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r16", 16, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r17", 17, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r18", 18, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r19", 19, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r20", 20, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r21", 21, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r22", 22, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r23", 23, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r24", 24, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r25", 25, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r26", 26, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r27", 27, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r28", 28, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r29", 29, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r30", 30, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "r31", 31, 1, NULL, 32, "int");
+  tdesc_create_reg (feature, "ppc", 32, 1, NULL, 32, "code_ptr");
+  tdesc_create_reg (feature, "npc", 33, 1, NULL, 32, "code_ptr");
+  tdesc_create_reg (feature, "sr", 34, 1, NULL, 32, "sr_flags");
+
+  tdesc_or1k_linux = result.release ();
+}
Signature · 1 line
2.31.1
#6 lore raw

PATCH 4/5 gdb: or1k: add native linux support

In reply to Stafford Horne

This patch adds support for running gdb natively on OpenRISC linux. Debugging support is provided via the linux PTRACE interface which is mostly handled by GDB genric code. This patch provides the logic of how to read and write the ptrace registers between linux and GDB.

Single stepping is privided in a separate patch.

---
 gdb/Makefile.in       |   2 +
 gdb/configure.host    |   2 +
 gdb/configure.nat     |   4 +
 gdb/or1k-linux-nat.c  | 210 ++++++++++++++++++++++++++++++++++++++++++
 gdb/or1k-linux-tdep.c |   5 +
 gdb/or1k-linux-tdep.h |  26 ++++++
 6 files changed, 249 insertions(+)
 create mode 100644 gdb/or1k-linux-nat.c
 create mode 100644 gdb/or1k-linux-tdep.h
gdb/Makefile.in +2 -0
index 320d3326a81..69cc1402594 100644
@@ -1377,6 +1377,7 @@ HFILES_NO_SRCDIR = \
 	objfiles.h \
 	obsd-nat.h \
 	obsd-tdep.h \
+	or1k-linux-tdep.h \
 	osabi.h \
 	osdata.h \
 	p-lang.h \
@@ -2247,6 +2248,7 @@ ALLDEPFILES = \
 	nios2-tdep.c \
 	obsd-nat.c \
 	obsd-tdep.c \
+	or1k-linux-nat.c \
 	posix-hdep.c \
 	ppc-fbsd-nat.c \
 	ppc-fbsd-tdep.c \
gdb/configure.host +2 -0
index 1fc83601e0e..be40cc814fc 100644
@@ -130,6 +130,8 @@ mips*-*-netbsdaout* | mips*-*-knetbsd*-gnu)
 mips*-*-freebsd*)	gdb_host=fbsd ;;
 mips64*-*-openbsd*)	gdb_host=obsd64 ;;
 
+or1k-*-linux*)		gdb_host=linux ;;
+
 powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
 			gdb_host=aix ;;
 powerpc*-*-freebsd*)	gdb_host=fbsd ;;
gdb/configure.nat +4 -0
index 655c75dd1ab..d42cf13e691 100644
@@ -271,6 +271,10 @@ case ${gdb_host} in
 		NATDEPFILES="${NATDEPFILES} linux-nat-trad.o \
 		mips-linux-nat.o nat/mips-linux-watch.o"
 		;;
+	    or1k)
+		# Host: Linux/OpenRISC
+		NATDEPFILES="${NATDEPFILES} or1k-linux-nat.o"
+		;;
 	    pa)
 		# Host: Hewlett-Packard PA-RISC machine, running Linux
 		NATDEPFILES="${NATDEPFILES} hppa-linux-nat.o"
gdb/or1k-linux-nat.c new file +210 -0

210 lines not on this page · raw message

gdb/or1k-linux-tdep.c +5 -0
index 2efb4d0b0f9..a71a8202b1e 100644
@@ -27,6 +27,8 @@
 #include "trad-frame.h"
 #include "gdbarch.h"
 
+#include "features/or1k-linux.c"
+
 /* Define the general register mapping.  The kernel and GDB put registers
    r1 to r31 in the same place.  The NPC register is stored@index 32 in
    linux and 33 in GDB, in GDB 32 is for PPC which is not popupated from linux.
@@ -170,4 +172,7 @@ _initialize_or1k_linux_tdep ()
 {
   gdbarch_register_osabi (bfd_arch_or1k, 0, GDB_OSABI_LINUX,
 			  or1k_linux_init_abi);
+
+  /* Initialize the standard target descriptions.  */
+  initialize_tdesc_or1k_linux ();
 }
gdb/or1k-linux-tdep.h new file +26 -0
new file mode 100644
index 00000000000..19294669e64
@@ -0,0 +1,26 @@
+/* Target-dependent code for GNU/Linux on OpenRISC.
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef OR1K_LINUX_TDEP_H
+#define OR1K_LINUX_TDEP_H
+
+/* Target descriptions.  */
+extern struct target_desc *tdesc_or1k_linux;
+
+#endif /* OR1K_LINUX_TDEP_H */
Signature · 1 line
2.31.1
#7 lore raw

PATCH 5/5 gdb: or1k: add single step for linux native debugging

In reply to Stafford Horne

Needed for single stepping in Linux, this adds the or1k implementation of or1k_software_single_step. Most of the implementation is borrowed from the bare metal single step code from or1k_single_step_through_delay which has been extracted and shared in helper function or1k_delay_slot_p.

---
 gdb/or1k-linux-tdep.c |  2 ++
 gdb/or1k-tdep.c       | 66 ++++++++++++++++++++++++++++++-------------
 gdb/or1k-tdep.h       |  5 ++++
 3 files changed, 54 insertions(+), 19 deletions(-)
gdb/or1k-linux-tdep.c +2 -0
index a71a8202b1e..63c4171e295 100644
@@ -154,6 +154,8 @@ or1k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
 
+  set_gdbarch_software_single_step (gdbarch, or1k_software_single_step);
+
   /* Enable TLS support.  */
   set_gdbarch_fetch_tls_load_module_address (gdbarch,
 					     svr4_fetch_objfile_link_map);
gdb/or1k-tdep.c +47 -19

87 lines not on this page · raw message

gdb/or1k-tdep.h +5 -0
index d9dc1aca3c3..81d902ae49e 100644
@@ -52,4 +52,9 @@
 #define OR1K_NUM_TAP_RECORDS         8
 #define OR1K_FRAME_RED_ZONE_SIZE     2536
 
+/* Single step based on where the current instruction will take us.  */
+extern std::vector<CORE_ADDR> or1k_software_single_step
+  (struct regcache *regcache);
+
+
 #endif /* OR1K_TDEP_H */
Signature · 1 line
2.31.1
#8 lore raw

cover OpenRISC Linux Native and GDBServer Support

In reply to Stafford Horne
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

">" == Stafford Horne via Gdb-patches <[email protected]> writes:

Hello,
As I have been working on the OpenRISC glibc port over the last few years now I have had the need to have Linux native debugging. At first I started with the gdbserver, but quickly grew out of that and just settled on running gdb native on the platform.

I read through these and it seemed basically ok to me. I sent one little nit. I don't think it needs re-review if you want to fix that.

thanks,
Tom

#9 lore raw

cover OpenRISC Linux Native and GDBServer Support

Quoted 10 lines · Tom Tromey
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

">" == Stafford Horne via Gdb-patches <[email protected]> writes:

Hello,
As I have been working on the OpenRISC glibc port over the last few years now I have had the need to have Linux native debugging. At first I started with the gdbserver, but quickly grew out of that and just settled on running gdb native on the platform.

I read through these and it seemed basically ok to me. I sent one little nit. I don't think it needs re-review if you want to fix that.

Thanks for reviewing. I will give it a day or two for more comments before pushing upstream.

-Stafford