From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id CNN7HKXW4V+BcgAAWB0awg (envelope-from ) for ; Tue, 22 Dec 2020 06:21:09 -0500 Received: by simark.ca (Postfix, from userid 112) id 1DAE31F0AA; Tue, 22 Dec 2020 06:21:08 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id D30431E552 for ; Tue, 22 Dec 2020 06:21:07 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8BBD53875449; Tue, 22 Dec 2020 11:21:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BBD53875449 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1608636067; bh=Ux4gm5sqSlDWcw2s8xAXNatUOlnNtWCMbW9eaOHz74w=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=j4ZFk1jcWf9jm6tnlgb5sF1zPJuUo1dqr8kSKNZVChkS3wJbuvQ4f6brX+St0m0bT Cfy84n318N+4ybBkSY+VWvkydtOi8wzMNg09K4HzG/bK7g1AzPc2PikOtOfbLjsoFW 6YYYM3b+3624+ByS9Q/POHqwdtTsMbvUMkWJh7Bw= Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 6165B3875449 for ; Tue, 22 Dec 2020 11:21:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6165B3875449 Received: by mail-ej1-x635.google.com with SMTP id n26so17748802eju.6 for ; Tue, 22 Dec 2020 03:21:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ux4gm5sqSlDWcw2s8xAXNatUOlnNtWCMbW9eaOHz74w=; b=RdToX9lf+fQalIdyjOoPnw9UvcpIv3qRaMYyLNltljVD+eRjBJ5fBo8BkaZIdO9//S ehZ4lFJWXsSZ7uVOXcCtwykF620TMK6pY9Gil25uMYvnDSoTBgdOg9yG72mlKswZ9DlY XNtlyZ8PjZOB5SdgrVIN81H/gMCmLQDCGXTFGicWBOJOzbpNHqKY0a0Fy2ppFG5wyM1g LSS8LjQBFXrmFGoOE1rqhmmL0CmLQLBAmOlPWHRoTNG/ocQ4om0b2IE0AepVQpS87NTQ UiZ3jfLeH0G5bNjXY3fPs5jWz66QbCd6fc3l8B9E9Bd4BGWVMJXbW6LC4qeGYHLvMT+R V6yg== X-Gm-Message-State: AOAM533+b8eePP/4/8rz+rwKLabWJGNkmz5iIpnp5rSIU4is6ZrtoEwE JyJ900ye336weEcNPiGGIph/Z63czr/1qs6C X-Google-Smtp-Source: ABdhPJzAhX02HNEbz/p1vW/AbcsdujX/63iyvXv96nMUCqTr7kn6XXFW/jwjTgbvibbC+K1QVKlM9Q== X-Received: by 2002:a17:906:4e52:: with SMTP id g18mr19937373ejw.385.1608636062336; Tue, 22 Dec 2020 03:21:02 -0800 (PST) Received: from atlantis.home ([2a03:1b20:1:f410::21d]) by smtp.gmail.com with ESMTPSA id a10sm10292346ejk.92.2020.12.22.03.21.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Dec 2020 03:21:01 -0800 (PST) To: gdb-patches@sourceware.org Subject: [PUSHED v2 2/2] gdb: Add native support for ARC in GNU/Linux Date: Tue, 22 Dec 2020 12:21:02 +0100 Message-Id: <20201222112102.13411-2-shahab.vahedi@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201222112102.13411-1-shahab.vahedi@gmail.com> References: <20201112134811.9074-1-shahab.vahedi@gmail.com> <20201222112102.13411-1-shahab.vahedi@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Shahab Vahedi via Gdb-patches Reply-To: Shahab Vahedi Cc: Shahab Vahedi , Shahab Vahedi , Anton Kolesov , Tom Tromey , Francois Bedard Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" From: Anton Kolesov With this patch in place it is possible to build a GDB that can run on ARC (GNU/Linux) hosts for debugging ARC targets. The "arc-linux-nat.c" is a rather small one that mostly deals with registers and a few thread related hooks. v2 [1]: - Remove "void" from the input of "_initialize_arc_linux_nat ()" [1] Tom's remark after the first patch https://sourceware.org/pipermail/gdb-patches/2020-November/173223.html gdb/ChangeLog: * Makefile.in (ALLDEPFILES): Add arc-linux-nat.c. * configure.host (host to gdb names): Add arc*-*-linux*. * configure.nat (gdb_host_cpu): Add arc. * arc-linux-nat.c: New. --- gdb/Makefile.in | 1 + gdb/arc-linux-nat.c | 320 ++++++++++++++++++++++++++++++++++++++++++++ gdb/configure.host | 3 + gdb/configure.nat | 4 + 4 files changed, 328 insertions(+) create mode 100644 gdb/arc-linux-nat.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index c46935efafa..44ba2f04ac9 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -2144,6 +2144,7 @@ ALLDEPFILES = \ amd64-ravenscar-thread.c \ amd64-sol2-tdep.c \ amd64-tdep.c \ + arc-linux-nat.c \ arc-tdep.c \ arm.c \ arm-bsd-tdep.c \ diff --git a/gdb/arc-linux-nat.c b/gdb/arc-linux-nat.c new file mode 100644 index 00000000000..41301fd4fed --- /dev/null +++ b/gdb/arc-linux-nat.c @@ -0,0 +1,320 @@ +/* Native-dependent code for GNU/Linux ARC. + + Copyright 2020 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 "frame.h" +#include "inferior.h" +#include "gdbcore.h" +#include "regcache.h" +#include "gdbsupport/gdb_assert.h" +#include "target.h" +#include "linux-nat.h" +#include "nat/gdb_ptrace.h" + +#include +#include +#include +#include +#include +#include +#include "gdbsupport/gdb_wait.h" +#include +#include +#include + +#include "gregset.h" +#include "arc-tdep.h" +#include "arc-linux-tdep.h" +#include "arch/arc.h" + +/* Defines ps_err_e, struct ps_prochandle. */ +#include "gdb_proc_service.h" + +/* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30, + R58 and R59 registers, which are specific to ARC HS and aren't + available in ARC 700. */ +#if defined (NT_ARC_V2) && defined (__ARCHS__) +#define ARC_HAS_V2_REGSET +#endif + +class arc_linux_nat_target final : public linux_nat_target +{ +public: + /* Add ARC register access methods. */ + void fetch_registers (struct regcache *, int) override; + void store_registers (struct regcache *, int) override; + + const struct target_desc *read_description () override; + + /* Handle threads */ + void low_prepare_to_resume (struct lwp_info *lp) override; +}; + +static arc_linux_nat_target the_arc_linux_nat_target; + +/* Read general registers from target process/thread (via ptrace) + into REGCACHE. */ + +static void +fetch_gregs (struct regcache *regcache, int regnum) +{ + const int tid = get_ptrace_pid (regcache->ptid ()); + struct iovec iov; + gdb_gregset_t regs; + + iov.iov_base = ®s; + iov.iov_len = sizeof (gdb_gregset_t); + + if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (void *) &iov) < 0) + perror_with_name (_("Couldn't get general registers")); + else + arc_linux_supply_gregset (NULL, regcache, regnum, ®s, 0); +} + +#ifdef ARC_HAS_V2_REGSET +/* Read ARC v2 registers from target process/thread (via ptrace) + into REGCACHE. */ + +static void +fetch_v2_regs (struct regcache *regcache, int regnum) +{ + const int tid = get_ptrace_pid (regcache->ptid ()); + struct iovec iov; + bfd_byte v2_buffer[ARC_LINUX_SIZEOF_V2_REGSET]; + + iov.iov_base = &v2_buffer; + iov.iov_len = ARC_LINUX_SIZEOF_V2_REGSET; + + if (ptrace (PTRACE_GETREGSET, tid, NT_ARC_V2, (void *) &iov) < 0) + perror_with_name (_("Couldn't get ARC HS registers")); + else + arc_linux_supply_v2_regset (NULL, regcache, regnum, v2_buffer, 0); +} +#endif + +/* Store general registers from REGCACHE into the target process/thread. */ + +static void +store_gregs (const struct regcache *regcache, int regnum) +{ + const int tid = get_ptrace_pid (regcache->ptid ()); + struct iovec iov; + gdb_gregset_t regs; + + iov.iov_base = ®s; + iov.iov_len = sizeof (gdb_gregset_t); + + if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (void *) &iov) < 0) + perror_with_name (_("Couldn't get general registers")); + else + { + arc_linux_collect_gregset (NULL, regcache, regnum, regs, 0); + + if (ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, (void *) &iov) < 0) + perror_with_name (_("Couldn't write general registers")); + } +} + +#ifdef ARC_HAS_V2_REGSET +/* Store ARC v2 registers from REGCACHE into the target process/thread. */ + +static void +store_v2_regs (const struct regcache *regcache, int regnum) +{ + const int tid = get_ptrace_pid (regcache->ptid ()); + struct iovec iov; + bfd_byte v2_buffer[ARC_LINUX_SIZEOF_V2_REGSET]; + + iov.iov_base = &v2_buffer; + iov.iov_len = ARC_LINUX_SIZEOF_V2_REGSET; + + if (ptrace (PTRACE_GETREGSET, tid, NT_ARC_V2, (void *) &iov) < 0) + perror_with_name (_("Couldn't get ARC HS registers")); + else + { + arc_linux_collect_v2_regset (NULL, regcache, regnum, v2_buffer, 0); + + if (ptrace (PTRACE_SETREGSET, tid, NT_ARC_V2, (void *) &iov) < 0) + perror_with_name (_("Couldn't write ARC HS registers")); + } +} +#endif + +/* Target operation: Read REGNUM register (all registers if REGNUM == -1) + from target process into REGCACHE. */ + +void +arc_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum) +{ + + if (regnum == -1 || regnum <= ARC_LAST_REGNUM) + fetch_gregs (regcache, regnum); + +#ifdef ARC_HAS_V2_REGSET + if (regnum == -1 + || regnum == ARC_R30_REGNUM + || regnum == ARC_R58_REGNUM + || regnum == ARC_R59_REGNUM) + fetch_v2_regs (regcache, regnum); +#endif +} + +/* Target operation: Store REGNUM register (all registers if REGNUM == -1) + to the target process from REGCACHE. */ + +void +arc_linux_nat_target::store_registers (struct regcache *regcache, int regnum) +{ + if (regnum == -1 || regnum <= ARC_LAST_REGNUM) + store_gregs (regcache, regnum); + +#ifdef ARC_HAS_V2_REGSET + if (regnum == -1 + || regnum == ARC_R30_REGNUM + || regnum == ARC_R58_REGNUM + || regnum == ARC_R59_REGNUM) + store_v2_regs (regcache, regnum); +#endif +} + +/* Copy general purpose register(s) from REGCACHE into regset GREGS. + This function is exported to proc-service.c */ + +void +fill_gregset (const struct regcache *regcache, + gdb_gregset_t *gregs, int regnum) +{ + arc_linux_collect_gregset (NULL, regcache, regnum, gregs, 0); +} + +/* Copy all the general purpose registers from regset GREGS into REGCACHE. + This function is exported to proc-service.c. */ + +void +supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs) +{ + arc_linux_supply_gregset (NULL, regcache, -1, gregs, 0); +} + +/* ARC doesn't have separate FP registers. This function is exported + to proc-service.c. */ + +void +fill_fpregset (const struct regcache *regcache, + gdb_fpregset_t *fpregsetp, int regnum) +{ + if (arc_debug) + debug_printf ("arc-linux-nat: fill_fpregset called."); + return; +} + +/* ARC doesn't have separate FP registers. This function is exported + to proc-service.c. */ + +void +supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp) +{ + if (arc_debug) + debug_printf ("arc-linux-nat: supply_fpregset called."); + return; +} + +/* Implement the "read_description" method of linux_nat_target. */ + +const struct target_desc * +arc_linux_nat_target::read_description () +{ + /* This is a native target, hence description is hardcoded. */ +#ifdef __ARCHS__ + arc_arch_features features (4, ARC_ISA_ARCV2); +#else + arc_arch_features features (4, ARC_ISA_ARCV1); +#endif + return arc_lookup_target_description (features); +} + +/* As described in arc_linux_collect_gregset(), we need to write resume-PC + to ERET. However by default GDB for native targets doesn't write + registers if they haven't been changed. This is a callback called by + generic GDB, and in this callback we have to rewrite PC value so it + would force rewrite of register on target. It seems that the only + other arch that utilizes this hook is x86/x86-64 for HW breakpoint + support. But then, AFAIK no other arch has this stop_pc/eret + complexity. + + No better way was found, other than this fake write of register value, + to force GDB into writing register to target. Is there any? */ + +void +arc_linux_nat_target::low_prepare_to_resume (struct lwp_info *lwp) +{ + /* When new processes and threads are created we do not have the address + space for them and calling get_thread_regcache will cause an internal + error in GDB. It looks like that checking for last_resume_kind is the + sensible way to determine processes for which we cannot get regcache. + Ultimately, a better way would be removing the need for + low_prepare_to_resume in the first place. */ + if (lwp->last_resume_kind == resume_stop) + return; + + struct regcache *regcache = get_thread_regcache (this, lwp->ptid); + struct gdbarch *gdbarch = regcache->arch (); + + /* Read current PC value, then write it back. It is required to call + invalidate(), otherwise GDB will note that new value is equal to old + value and will skip write. */ + ULONGEST new_pc; + regcache_cooked_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), + &new_pc); + regcache->invalidate (gdbarch_pc_regnum (gdbarch)); + regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch), + new_pc); +} + +/* Fetch the thread-local storage pointer for libthread_db. Note that + this function is not called from GDB, but is called from libthread_db. + This is required to debug multithreaded applications with NPTL. */ + +ps_err_e +ps_get_thread_area (struct ps_prochandle *ph, lwpid_t lwpid, int idx, + void **base) +{ + if (arc_debug >= 2) + debug_printf ("arc-linux-nat: ps_get_thread_area called"); + + if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0) + return PS_ERR; + + /* IDX is the bias from the thread pointer to the beginning of the + thread descriptor. It has to be subtracted due to implementation + quirks in libthread_db. */ + *base = (void *) ((char *) *base - idx); + + return PS_OK; +} + +/* Suppress warning from -Wmissing-prototypes. */ +void _initialize_arc_linux_nat (); +void +_initialize_arc_linux_nat () +{ + /* Register the target. */ + linux_target = &the_arc_linux_nat_target; + add_inf_child_target (&the_arc_linux_nat_target); +} diff --git a/gdb/configure.host b/gdb/configure.host index ce528237291..e94a19b0332 100644 --- a/gdb/configure.host +++ b/gdb/configure.host @@ -60,6 +60,7 @@ case "${host_cpu}" in aarch64*) gdb_host_cpu=aarch64 ;; alpha*) gdb_host_cpu=alpha ;; +arc*) gdb_host_cpu=arc ;; arm*) gdb_host_cpu=arm ;; hppa*) gdb_host_cpu=pa ;; i[34567]86*) gdb_host_cpu=i386 ;; @@ -91,6 +92,8 @@ alpha*-*-netbsd* | alpha*-*-knetbsd*-gnu) gdb_host=nbsd ;; alpha*-*-openbsd*) gdb_host=nbsd ;; +arc*-*-linux*) gdb_host=linux ;; + arm*-*-freebsd*) gdb_host=fbsd ;; arm*-*-linux*) gdb_host=linux ;; arm*-*-netbsdelf* | arm*-*-knetbsd*-gnu) diff --git a/gdb/configure.nat b/gdb/configure.nat index ef2218f0b8d..0e36e09b692 100644 --- a/gdb/configure.nat +++ b/gdb/configure.nat @@ -238,6 +238,10 @@ case ${gdb_host} in nat/aarch64-linux.o \ nat/aarch64-sve-linux-ptrace.o" ;; + arc) + # Host: ARC based machine running GNU/Linux + NATDEPFILES="${NATDEPFILES} arc-linux-nat.o" + ;; arm) # Host: ARM based machine running GNU/Linux NATDEPFILES="${NATDEPFILES} arm-linux-nat.o \ -- 2.29.2