From e308536005020bb03fd34304fd81e17716e620a9 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Tue, 28 Dec 2021 04:49:09 -0800 Subject: [PATCH] Ports/gdb: Add basic ptrace based native target for SerenityOS/i386 This patch adds a ptrace based gdb backend, which is then enlightended to known how to read the serenity i386 registers via ptrace. This is just a basic implementation to get the port bootstrapped. --- .../basic-ptrace-i386-native-target.patch | 421 ++++++++++++++++++ 1 file changed, 421 insertions(+) create mode 100644 Ports/gdb/patches/basic-ptrace-i386-native-target.patch diff --git a/Ports/gdb/patches/basic-ptrace-i386-native-target.patch b/Ports/gdb/patches/basic-ptrace-i386-native-target.patch new file mode 100644 index 0000000000..b3dec2238c --- /dev/null +++ b/Ports/gdb/patches/basic-ptrace-i386-native-target.patch @@ -0,0 +1,421 @@ +From 581bbef892960b1533aa9d9918f5eeee4bff9cce Mon Sep 17 00:00:00 2001 +From: Brian Gianforcaro +Date: Tue, 28 Dec 2021 04:35:47 -0800 +Subject: [PATCH 2/3] serenity: Add basic ptrace based native target for + SerenityOS/i386 + +--- + gdb/configure.host | 3 ++ + gdb/configure.nat | 11 +++++ + gdb/configure.tgt | 4 ++ + gdb/i386-serenity-nat.c | 101 +++++++++++++++++++++++++++++++++++++++ + gdb/i386-serenity-tdep.c | 43 +++++++++++++++++ + gdb/i386-serenity-tdep.h | 8 ++++ + gdb/osabi.c | 1 + + gdb/osabi.h | 1 + + gdb/serenity-nat.c | 13 +++++ + gdb/serenity-nat.h | 34 +++++++++++++ + gdb/serenity-tdep.c | 28 +++++++++++ + gdb/serenity-tdep.h | 24 ++++++++++ + 12 files changed, 271 insertions(+) + create mode 100644 gdb/i386-serenity-nat.c + create mode 100644 gdb/i386-serenity-tdep.c + create mode 100644 gdb/i386-serenity-tdep.h + create mode 100644 gdb/serenity-nat.c + create mode 100644 gdb/serenity-nat.h + create mode 100644 gdb/serenity-tdep.c + create mode 100644 gdb/serenity-tdep.h + +diff --git a/gdb/configure.host b/gdb/configure.host +index e94a19b..06b87ca 100644 +--- a/gdb/configure.host ++++ b/gdb/configure.host +@@ -85,6 +85,7 @@ case "${host}" in + *-*-darwin*) gdb_host=darwin ;; + + aarch64*-*-linux*) gdb_host=linux ;; ++aarch64*-*-serenity*) gdb_host=serenity ;; + aarch64*-*-freebsd*) gdb_host=fbsd ;; + + alpha*-*-linux*) gdb_host=alpha-linux ;; +@@ -117,6 +118,7 @@ i[34567]86-*-linux*) gdb_host=linux ;; + i[34567]86-*-gnu*) gdb_host=i386gnu ;; + i[3456]86-*-nto*) gdb_host=nto ;; + i[34567]86-*-openbsd*) gdb_host=obsd ;; ++i[34567]86-*-serenity*) gdb_host=serenity ;; + i[34567]86-*-solaris2* | x86_64-*-solaris2*) + gdb_host=sol2 ;; + i[34567]86-*-cygwin*) gdb_host=cygwin ;; +@@ -185,6 +187,7 @@ x86_64-*-mingw*) gdb_host=mingw64 + gdb_host_obs=mingw-hdep.o + ;; + x86_64-*-cygwin*) gdb_host=cygwin64 ;; ++x86_64-*-serenity*) gdb_host=serenity ;; + m32r*-*-linux*) gdb_host=linux ;; + + xtensa*-*-linux*) gdb_host=linux ;; +diff --git a/gdb/configure.nat b/gdb/configure.nat +index e34cccf..38b687e 100644 +--- a/gdb/configure.nat ++++ b/gdb/configure.nat +@@ -86,6 +86,9 @@ case ${gdb_host} in + darwin) + NATDEPFILES='fork-child.o nat/fork-inferior.o darwin-nat.o \ + darwin-nat-info.o' ++ ;; ++ serenity) ++ NATDEPFILES='fork-child.o nat/fork-inferior.o inf-ptrace.o' + ;; + sol2) + NATDEPFILES='fork-child.o nat/fork-inferior.o \ +@@ -477,6 +480,14 @@ case ${gdb_host} in + ;; + esac + ;; ++ serenity) ++ case ${gdb_host_cpu} in ++ i386) ++ # Host: SerenityOS/x86_64 ELF ++ NATDEPFILES="${NATDEPFILES} amd64-nat.o serenity-nat.o i386-serenity-nat.o" ++ ;; ++ esac ++ ;; + sol2) + case ${gdb_host_cpu} in + i386) +diff --git a/gdb/configure.tgt b/gdb/configure.tgt +index 97a5a57..886542f 100644 +--- a/gdb/configure.tgt ++++ b/gdb/configure.tgt +@@ -291,6 +291,10 @@ i[34567]86-*-nto*) + gdb_target_obs="solib-svr4.o \ + i386-nto-tdep.o nto-tdep.o" + ;; ++i[34567]86-*-serenity*) ++ # Target: SerenityOS/i386 ++ gdb_target_obs="i386-serenity-tdep.o serenity-tdep.o" ++ ;; + i[34567]86-*-solaris2* | x86_64-*-solaris2*) + # Target: Solaris x86_64 + gdb_target_obs="${i386_tobjs} ${amd64_tobjs} \ +diff --git a/gdb/i386-serenity-nat.c b/gdb/i386-serenity-nat.c +new file mode 100644 +index 0000000..034252a +--- /dev/null ++++ b/gdb/i386-serenity-nat.c +@@ -0,0 +1,101 @@ ++/* Native-dependent code for SerenityOS/i386. ++ ++ Copyright (C) 2002-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 . */ ++ ++#include "defs.h" ++#include "gdbcore.h" ++#include "regcache.h" ++#include "regset.h" ++#include "target.h" ++ ++#include ++#include ++ ++#include "i386-tdep.h" ++#include "serenity-nat.h" ++ ++/* Register maps. */ ++ ++static const struct regcache_map_entry i386_serenity_gregmap[] = ++{ ++ { 1, I386_EAX_REGNUM, 0 }, ++ { 1, I386_ECX_REGNUM, 0 }, ++ { 1, I386_EDX_REGNUM, 0 }, ++ { 1, I386_EBX_REGNUM, 0 }, ++ { 1, I386_ESP_REGNUM, 0 }, ++ { 1, I386_EBP_REGNUM, 0 }, ++ { 1, I386_ESI_REGNUM, 0 }, ++ { 1, I386_EDI_REGNUM, 0 }, ++ { 1, I386_EIP_REGNUM, 0 }, ++ { 1, I386_EFLAGS_REGNUM, 0 }, ++ { 1, I386_CS_REGNUM, 0 }, ++ { 1, I386_SS_REGNUM, 0 }, ++ { 1, I386_DS_REGNUM, 0 }, ++ { 1, I386_ES_REGNUM, 0 }, ++ { 1, I386_FS_REGNUM, 0 }, ++ { 1, I386_GS_REGNUM, 0 }, ++ { 0 }, ++}; ++ ++const struct regset i386_serenity_gregset = ++{ ++ i386_serenity_gregmap, regcache_supply_regset, regcache_collect_regset ++}; ++ ++class i386_serenity_nat_target final : public serenity_nat_target ++{ ++ void fetch_registers (struct regcache* cache, int regnum) override ++ { ++ if (regnum == -1) { ++ pid_t pid = get_ptrace_pid (cache->ptid ()); ++ PtraceRegisters regs; ++ ++ if (ptrace (PT_GETREGS, pid, ®s, 0) == -1) ++ perror_with_name (_("Couldn't get registers")); ++ ++ cache->supply_regset (&i386_serenity_gregset, regnum, ®s, ++ sizeof (regs)); ++ } ++ }; ++ ++ void store_registers (struct regcache* cache, int regnum) override ++ { ++ if (regnum == -1) { ++ pid_t pid = get_ptrace_pid (cache->ptid ()); ++ PtraceRegisters regs {}; ++ ++ if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) ++ perror_with_name (_("Couldn't get registers")); ++ ++ cache->collect_regset (&i386_serenity_gregset, regnum, ®s, ++ sizeof (regs)); ++ ++ if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) ++ perror_with_name (_("Couldn't write registers")); ++ } ++ }; ++}; ++ ++static i386_serenity_nat_target the_i386_serenity_nat_target; ++ ++void _initialize_i386_serenity_nat (); ++void ++_initialize_i386_serenity_nat () ++{ ++ add_inf_child_target (&the_i386_serenity_nat_target); ++} +diff --git a/gdb/i386-serenity-tdep.c b/gdb/i386-serenity-tdep.c +new file mode 100644 +index 0000000..d384061 +--- /dev/null ++++ b/gdb/i386-serenity-tdep.c +@@ -0,0 +1,43 @@ ++/* Target-dependent code for SerenityOS/i386. ++ ++ Copyright (C) 2003-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 . */ ++ ++#include "defs.h" ++#include "arch-utils.h" ++#include "gdbcore.h" ++#include "osabi.h" ++#include "regcache.h" ++ ++#include "serenity-tdep.h" ++ ++ ++/* Implement the 'init_osabi' method of struct gdb_osabi_handler. */ ++static void ++i386_serenity_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) ++{ ++ /* Generic SerenityOS support. */ ++ serenity_init_abi (info, gdbarch); ++} ++ ++void _initialize_i386_serenity_tdep (); ++void ++_initialize_i386_serenity_tdep () ++{ ++ gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SERENITYOS, ++ i386_serenity_init_abi); ++} +diff --git a/gdb/i386-serenity-tdep.h b/gdb/i386-serenity-tdep.h +new file mode 100644 +index 0000000..4ee3fba +--- /dev/null ++++ b/gdb/i386-serenity-tdep.h +@@ -0,0 +1,8 @@ ++/* Target-dependent code for SernityOS i386. */ ++ ++#ifndef I386_SERENITY_TDEP_H ++#define I386_SERENITY_TDEP_H ++ ++//extern const struct regset i386_serenity_gregset; ++ ++#endif /* i386-serenity-tdep.h */ +diff --git a/gdb/osabi.c b/gdb/osabi.c +index aabf895..28789e8 100644 +--- a/gdb/osabi.c ++++ b/gdb/osabi.c +@@ -82,6 +82,7 @@ static const struct osabi_names gdb_osabi_names[] = + { "Newlib", NULL }, + { "SDE", NULL }, + { "PikeOS", NULL }, ++ { "SerenityOS", NULL }, + + { "", NULL } + }; +diff --git a/gdb/osabi.h b/gdb/osabi.h +index 1ecbed4..73c5549 100644 +--- a/gdb/osabi.h ++++ b/gdb/osabi.h +@@ -46,6 +46,7 @@ enum gdb_osabi + GDB_OSABI_NEWLIB, + GDB_OSABI_SDE, + GDB_OSABI_PIKEOS, ++ GDB_OSABI_SERENITYOS, + + GDB_OSABI_INVALID /* keep this last */ + }; +diff --git a/gdb/serenity-nat.c b/gdb/serenity-nat.c +new file mode 100644 +index 0000000..ff740d4 +--- /dev/null ++++ b/gdb/serenity-nat.c +@@ -0,0 +1,13 @@ ++/* Native-dependent code for SerenityOS */ ++ ++#include "defs.h" ++#include "gdbthread.h" ++#include "inferior.h" ++#include "target.h" ++ ++#include ++#include ++#include "gdbsupport/gdb_wait.h" ++ ++#include "inf-child.h" ++#include "serenity-nat.h" +\ No newline at end of file +diff --git a/gdb/serenity-nat.h b/gdb/serenity-nat.h +new file mode 100644 +index 0000000..ac3cfaa +--- /dev/null ++++ b/gdb/serenity-nat.h +@@ -0,0 +1,34 @@ ++/* Native-dependent code for SerenityOS. */ ++ ++#ifndef SERENITYOS_NAT_H ++#define SERENITYOS_NAT_H ++ ++#include "inf-ptrace.h" ++ ++/* A prototype generic Serenity target. ++ A concrete instance should override it with local methods. ++*/ ++ ++class serenity_nat_target : public inf_ptrace_target ++{ ++#if 0 ++ /* Override some methods to support threads. */ ++ std::string pid_to_str (ptid_t) override; ++ void update_thread_list () override; ++ ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override; ++ ++#ifdef PT_GET_PROCESS_STATE ++ void follow_fork (bool, bool) override; ++ ++ int insert_fork_catchpoint (int) override; ++ ++ int remove_fork_catchpoint (int) override; ++ ++ void post_startup_inferior (ptid_t) override; ++ ++ void post_attach (int) override; ++#endif ++#endif ++}; ++ ++#endif /* serenity-nat.h */ +diff --git a/gdb/serenity-tdep.c b/gdb/serenity-tdep.c +new file mode 100644 +index 0000000..45981b2 +--- /dev/null ++++ b/gdb/serenity-tdep.c +@@ -0,0 +1,28 @@ ++/* Target-dependent code for SerenityOS, architecture-independent. ++ ++ Copyright (C) 2002-2021 Free Software Foundation, Inc. ++ ++ 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 . */ ++ ++ ++#include "defs.h" ++#include "arch-utils.h" ++#include "gdbcore.h" ++ ++#include "serenity-tdep.h" ++ ++void serenity_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) ++{ ++ /* NOP for now */ ++} +diff --git a/gdb/serenity-tdep.h b/gdb/serenity-tdep.h +new file mode 100644 +index 0000000..43048c6 +--- /dev/null ++++ b/gdb/serenity-tdep.h +@@ -0,0 +1,24 @@ ++/* Target-dependent code for SerenityOS, architecture independent. ++ ++ Copyright (C) 2009-2021 Free Software Foundation, Inc. ++ ++ 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 . */ ++ ++ ++#ifndef SERENITY_TDEP_H ++#define SERENITY_TDEP_H ++ ++extern void serenity_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch); ++ ++#endif /* serenity-tdep.h */ +-- +2.32.0 +