From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id aMyTN3yvKmCIMwAAWB0awg (envelope-from ) for ; Mon, 15 Feb 2021 12:29:32 -0500 Received: by simark.ca (Postfix, from userid 112) id 852B71EF78; Mon, 15 Feb 2021 12:29:31 -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.4 required=5.0 tests=DKIM_SIGNED,MAILING_LIST_MULTI, RDNS_NONE,T_DKIM_INVALID,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 4EF171E54D for ; Mon, 15 Feb 2021 12:29:31 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E81E396AC0F; Mon, 15 Feb 2021 17:29:26 +0000 (GMT) Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id 2C5A1395543D for ; Mon, 15 Feb 2021 17:29:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2C5A1395543D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wm1-x329.google.com with SMTP id h67so5220492wmh.1 for ; Mon, 15 Feb 2021 09:29:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IuxvGffSpAeYdAc9JSQWVVv1OYuS1Der423frdI9dzI=; b=O2MNW7D6OM6BidEgFi72yOCgOU9ip/CPjISy7tj1rp11ihNSW0cznh5BuQteZhtEnO Uxou1c7wDSf6alP1E5m8i4Bvb+unMWpl3VPJWiZRzv7leTxRFyzNqcCGMb5YRRReMT0L pk/CKIyT9FSMtOEwE4lTtPSmMn6qMq5c2LYxg4l+GSLBiMDONE1hFcEnHGn/fo5omqyK S6iLhpwBgIC4TwSYlQmu3gRiJ+AVRs9qmHdPOVwkQ8s2MNAoxfnukAmSxYjwi8c8jtwb 0NZqXHbZkPoAWgFlERSW8u1hKkhGsBindF+TQwiVEEj9k7do6I7NL4lzS2tWjei5kdKN u1bw== 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=IuxvGffSpAeYdAc9JSQWVVv1OYuS1Der423frdI9dzI=; b=CJub5Pw0UgU9+onoEtJ0SOXkxmwIQRl5BtmcTgT1tvFdbyPJhwV5FWx5NkIpAjOXlJ CgFByQZK93BQ/A7ki9jGpMOF3yvYLw1blWIzD+ab5/YrU8AU4yNCWOmR0VHrqioiWp4k O/mhDRhZ4n63W9YsrByp2pPZjdKyLu3R1Of0yG5YclaujnHuk5ET/n8VlGDBQ0F3oDiA iJ6MbeEBaodU/59bfprrb1GIWqneEtM7R1C7Qw9cMlI3ilRU4ExEk/tHPMw4WiHWzzt1 8bgYlyoSWVQECr/wBIWS6w0UaD8G1rcg+fQ7UHqmXpQrNS6XLuHqKj8cvT54j6777cB+ O7Lg== X-Gm-Message-State: AOAM531D0n2ujfezwdU6UP+fOPq+BJ9uob0tJBT3uQ1Az5oQq3nOB7Wt ZYbbWDRhVhydWijreICt8S88NjuF4o7WmQ== X-Google-Smtp-Source: ABdhPJwj8efFNC8Cxdk1dMpNaiGfHY023gsRk1WsJdyqyCYrH9hF/r2w4d4BzSXxI4zrS1iF4U7j8g== X-Received: by 2002:a7b:c08f:: with SMTP id r15mr1299157wmh.73.1613410161868; Mon, 15 Feb 2021 09:29:21 -0800 (PST) Received: from localhost (host86-186-80-154.range86-186.btcentralplus.com. [86.186.80.154]) by smtp.gmail.com with ESMTPSA id b11sm10071167wrw.68.2021.02.15.09.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Feb 2021 09:29:21 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org, binutils@sourceware.org Subject: [PATCHv3 3/9] gdb: write target description into core file Date: Mon, 15 Feb 2021 17:29:06 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: 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: , Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" When a core file is created from within GDB add the target description into a note within the core file. When loading a core file, if the target description note is present then load the target description from the core file. The benefit of this is that we can be sure that, when analysing the core file within GDB, that we are using the exact same target description as was in use at the time the core file was created. GDB already supports a mechanism for figuring out the target description from a given corefile; gdbarch_core_read_description. This new mechanism (GDB adding the target description) is not going to replace the old mechanism. Core files generated outside of GDB will not include a target description, and so GDB still needs to be able to figure out a target description for these files. My primary motivation for adding this feature is that, in a future commit, I will be adding support for bare metal core dumps on some targets. For RISC-V specifically, I want to be able to dump all the available control status registers. As different targets will present different sets of register in their target description, including registers that are possibly not otherwise known to GDB I wanted a way to capture these registers in the core dump. I therefore need a mechanism to write out an arbitrary set of registers, and to then derive a target description from this arbitrary set when later loading the core file. The obvious approach (I think) is to just reuse the target description. Once I'd decided to add support for writing out the target description I could either choose to make this RISC-V only, or make it generic. I figure that having the target description in the core file doesn't hurt, and _might_ be helpful. So that's how I got here, general support for including the target description in GDB generated core files. In previous versions of this patch I added the target description from generic code (in gcore.c). However, doing this creates a dependency between GDB's common code and bfd ELF support. As ELF support in gdb is optional (for example the target x86_64-apple-darwin20.3.0 does not include ELF support) then having gcore.c require ELF support would break the GDB build in some cases. Instead, in this version of the patch, writing the target description note is done from each specific targets make notes function. Each of these now calls a common function in gcore-elf.c (which is only linked in when bfd has ELF support). And so only targets that are ELF based will call the new function and we can therefore avoid an unconditional dependency on ELF support. gdb/ChangeLog: * corelow.c: Add 'xml-tdesc.h' include. (core_target::read_description): Load the target description from the core file when possible. * fbsd-tdep.c (fbsd_make_corefile_notes): Add target description note. * gcore-elf.c: Add 'gdbsupport/tdesc.h' include. (gcore_elf_make_tdesc_note): New function. * gcore-elf.h (gcore_elf_make_tdesc_note): Declare. * linux-tdep.c (linux_make_corefile_notes): Add target description note. --- gdb/ChangeLog | 14 ++++++++++++++ gdb/corelow.c | 24 ++++++++++++++++++++++++ gdb/fbsd-tdep.c | 3 +++ gdb/gcore-elf.c | 30 ++++++++++++++++++++++++++++++ gdb/gcore-elf.h | 8 ++++++++ gdb/linux-tdep.c | 3 +++ 6 files changed, 82 insertions(+) diff --git a/gdb/corelow.c b/gdb/corelow.c index a63eab4852b..a2d2d20afc6 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -49,6 +49,7 @@ #include #include #include "gdbcmd.h" +#include "xml-tdesc.h" #ifndef O_LARGEFILE #define O_LARGEFILE 0 @@ -1000,6 +1001,29 @@ core_target::thread_alive (ptid_t ptid) const struct target_desc * core_target::read_description () { + /* If the core file contains a target description note then we will use + that in preference to anything else. */ + bfd_size_type tdesc_note_size = 0; + struct bfd_section *tdesc_note_section + = bfd_get_section_by_name (core_bfd, ".gdb-tdesc"); + if (tdesc_note_section != nullptr) + tdesc_note_size = bfd_section_size (tdesc_note_section); + if (tdesc_note_size > 0) + { + gdb::char_vector contents (tdesc_note_size + 1); + if (bfd_get_section_contents (core_bfd, tdesc_note_section, + contents.data (), (file_ptr) 0, + tdesc_note_size)) + { + /* Ensure we have a null terminator. */ + contents[tdesc_note_size] = '\0'; + const struct target_desc *result + = string_read_description_xml (contents.data ()); + if (result != nullptr) + return result; + } + } + if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch)) { const struct target_desc *result; diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c index dc4278cd644..170e8f5e84e 100644 --- a/gdb/fbsd-tdep.c +++ b/gdb/fbsd-tdep.c @@ -712,6 +712,9 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) return NULL; } + /* Include the target description when possible. */ + gcore_elf_make_tdesc_note (obfd, ¬e_data, note_size); + return note_data; } diff --git a/gdb/gcore-elf.c b/gdb/gcore-elf.c index ebc94277d35..036a40ca115 100644 --- a/gdb/gcore-elf.c +++ b/gdb/gcore-elf.c @@ -24,6 +24,7 @@ #include "gdbthread.h" #include "inferior.h" #include "regset.h" +#include "gdbsupport/tdesc.h" /* Structure for passing information from GCORE_COLLECT_THREAD_REGISTERS via an iterator to GCORE_COLLECT_REGSET_SECTION_CB. */ @@ -134,3 +135,32 @@ gcore_elf_build_thread_register_notes gcore_elf_collect_thread_registers (regcache, info->ptid, obfd, note_data, note_size, stop_signal); } + +/* See gcore-elf.h. */ + +void +gcore_elf_make_tdesc_note (bfd *obfd, + gdb::unique_xmalloc_ptr *note_data, + int *note_size) +{ + /* Append the target description to the core file. */ + const struct target_desc *tdesc = gdbarch_target_desc (target_gdbarch ()); + const char *tdesc_xml + = tdesc == nullptr ? nullptr : tdesc_get_features_xml (tdesc); + if (tdesc_xml != nullptr && *tdesc_xml != '\0') + { + /* Skip the leading '@'. */ + if (*tdesc_xml == '@') + ++tdesc_xml; + + /* Include the null terminator in the length. */ + size_t tdesc_len = strlen (tdesc_xml) + 1; + + /* Now add the target description into the core file. */ + note_data->reset (elfcore_write_register_note (obfd, + note_data->release (), + note_size, + ".gdb-tdesc", tdesc_xml, + tdesc_len)); + } +} diff --git a/gdb/gcore-elf.h b/gdb/gcore-elf.h index d667686adc7..b750165323e 100644 --- a/gdb/gcore-elf.h +++ b/gdb/gcore-elf.h @@ -36,4 +36,12 @@ extern void gcore_elf_build_thread_register_notes (struct gdbarch *gdbarch, struct thread_info *info, gdb_signal stop_signal, bfd *obfd, gdb::unique_xmalloc_ptr *note_data, int *note_size); +/* Add content to *NOTE_DATA (and update *NOTE_SIZE) to include a note + containing the current targtet's target description. The core file is + being written to OBFD. If something goes wrong then *NOTE_DATA can be + set to nullptr. */ + +extern void gcore_elf_make_tdesc_note + (bfd *obfd, gdb::unique_xmalloc_ptr *note_data, int *note_size); + #endif /* GCORE_ELF_H */ diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 5bfd82d1673..ab3402a41c2 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -1935,6 +1935,9 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) /* File mappings. */ linux_make_mappings_corefile_notes (gdbarch, obfd, note_data, note_size); + /* Target description. */ + gcore_elf_make_tdesc_note (obfd, ¬e_data, note_size); + return note_data; } -- 2.25.4