From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id WbXaCNEVt2j+ThIAWB0awg (envelope-from ) for ; Tue, 02 Sep 2025 12:05:37 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=WOLLD3yx; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 1E4EA1E087; Tue, 02 Sep 2025 12:05:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_LOW,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id EC2271E057 for ; Tue, 02 Sep 2025 12:05:35 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E2C3385482F for ; Tue, 2 Sep 2025 16:05:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E2C3385482F Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=WOLLD3yx Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id F18A8385EC23 for ; Tue, 2 Sep 2025 16:04:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F18A8385EC23 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F18A8385EC23 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756829045; cv=none; b=e+W8/mWjcTbp0SUAWo81JSeofmz/njVkz1VvBsJlC8BDIU7w5GCXF0OXlk92cOswd2JeulZVqBobhVECG208GTKpnO1Fq95TLMKQjcnKXQrgOtjVtruleTjnLW7wTfnl0Dms54CGNpDsOWj+2zL78vykTUyTwv+doM0V+dJAah0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756829045; c=relaxed/simple; bh=gon1XF7t835+AzcS4fELPoERuQvblOUw4LVzD8mDx0M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xDL6TpScvj5NhWmybWqIgrxnMKExPo0Gec/6fh0ZKcpcowGR8bexjjyf7eyKTMyHK5feVMh2ovvrq8cf9a4WtCiDZcBfGZ08scElaA0oCRVrDQ01fc/5brfGXy5+5Yj/ZF/ASDDGgmYv5OAngctq66fo7V+6RO+7eZh5rDjL6AQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F18A8385EC23 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756829044; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=95NEo4ezxyKNhYnn2wehriwIKomvDNhNaaFYmy99LOQ=; b=WOLLD3yx3BNAS+FlYIa5JHcWbCzJjmRQ94aMj3VtkVNWEOFDDqSGJnCFEWT/hIqRaEaXgw 1mc0j9UUIV1KaEzDo4+dP4vmc3BCJM59dUJ44I8HEQr0J91HcbIk5b+ZvpLHdd7SOHZh1u 3l/y2oB6XOyrWOr+j2oO5DrZbfwC1uI= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-370-NnPzmrYkNomELSwV0JiiFg-1; Tue, 02 Sep 2025 12:04:02 -0400 X-MC-Unique: NnPzmrYkNomELSwV0JiiFg-1 X-Mimecast-MFC-AGG-ID: NnPzmrYkNomELSwV0JiiFg_1756829039 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3db89e4f443so290365f8f.1 for ; Tue, 02 Sep 2025 09:04:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756829038; x=1757433838; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=95NEo4ezxyKNhYnn2wehriwIKomvDNhNaaFYmy99LOQ=; b=djVK+DeNzfTmq1X+YCOoAVRb/RgfMHR+B07y6stmzlJKBMqBuTGTeqNLy0FDIya9eq uWIoyOxGl/YqWOrXX5wDtCN24FfrQoh/RaOsF00as5TQuufLy7nocbP6F3k90t7LnNvU AiB2Bj4hrxLhVOH/PrNGiRcWL1DuCmgZ8Fgfl/NurUz2lxRYEF06qdbtLIpprKC1FyzP qjZWtXS6fpddk7Y6jpqaq8+H0tWZ3KByQi9lEqUbn37esNyp9W++hQWAerZfbBAHa1QL CrIvmQcoH6b9QAmfgmqeRZF/nLfYIWbiEQYemVXiUAX6RTRDbLow1IKOBRICgYqjCy+N HxvA== X-Gm-Message-State: AOJu0Yxiiv/Ys8EKgOGpV9615waB9Un7eYHgzNi6iGJxqPRP+MaMb2Sg DNYGPlOEq8RbkVWU1s5aW71PlwwHifEBZ61Ot1vjoKyO+QvwcQmc0GQItoKb6cdwpeEhKqlBRRX 3IQg6Y7XFfjwQnwiKfysidLIvnR6ofJ4mrXfsL2ESLThtXBvvbo13afUlFFSD6De7KVrS9uj/y3 Re15KrizcTxPTC0UUYFvF6t/Xn5lQ3d1Mm2CnopbQnuCg4s1I= X-Gm-Gg: ASbGncsfwcAo5PShisCigo5JSKi36PkfDgHek1lnIxqjbt6yJuQd+w+c9xSnyN9pxAj +7kW3IZ0WrrrMfjWuvCzAM/Ue7ywxm/B6FFIxa19luckIaGbWN3pKPw4ql7z53HOpIrzc+5uyW9 vgqO/DV+MSo7bfLaO9/EPUg80UxWt2ocNIm7P4fUeMUt0L8ODqs7ErXH3qfpT+1e8l5cxB3K2Jd Y5tNaePIh53l5gJ65b/nRJXgxSgB/ww/CQSu2VfH4AlV/IzemnwPRKylFTeBBgwbFaDWwD89elT R4UI7GUEmQ1IeKJw0o5P4Asztyj9BLi4BAY= X-Received: by 2002:a05:6000:2506:b0:3d1:1685:2da3 with SMTP id ffacd0b85a97d-3d1e07a5642mr9136576f8f.52.1756829038364; Tue, 02 Sep 2025 09:03:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGTHqXpQzaDrUm9jW+BxpPkhRYBw8sdKvlbIm2ie+Atay+ETUgJ06f5XkYls67pjKGV7TFGFw== X-Received: by 2002:a05:6000:2506:b0:3d1:1685:2da3 with SMTP id ffacd0b85a97d-3d1e07a5642mr9136533f8f.52.1756829037694; Tue, 02 Sep 2025 09:03:57 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3cf33fb9d37sm20433001f8f.49.2025.09.02.09.03.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 09:03:57 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/3] gdb: make structured core file mappings processing global Date: Tue, 2 Sep 2025 17:03:50 +0100 Message-ID: <77ac5e8b357c0cb36a14529958cb1f0510daa535.1756828929.git.aburgess@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: U7ztCF5PfeW0-eX0CtEh2xDzKVzvvZsYqAOiJBowf1s_1756829039 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org In corelow.c, within core_target::build_file_mappings, we have code that wraps around a call to gdbarch_read_core_file_mappings and provides more structure to the results. Specifically, gdbarch_read_core_file_mappings calls a callback once for every region of every mapped file. The wrapper code groups all of the mappings for one file into an instance of 'struct mapped_file', this allows all of the mapped regions to be associated with the build-id and filename of a file. In the next commit I plan to make this information available via the Python API, and so I need to allow access to this structured wrapping outside of corelow.c. This commit renames 'struct mapped_file' to 'struct core_mapped_file' and moves the struct into gdbcore.h. Then a new global function gdb_read_core_file_mappings is created into which I move the code to build the structured data. Then corelow.c is updated to call gdb_read_core_file_mappings. This commit does not extend the Python API, that is for the next commit. There should be no user visible changes after this commit. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32844 --- gdb/corelow.c | 218 +++++++++++++++++++++++++++----------------------- gdb/gdbcore.h | 43 ++++++++++ 2 files changed, 160 insertions(+), 101 deletions(-) diff --git a/gdb/corelow.c b/gdb/corelow.c index 111999c202c..e7a129c2784 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -366,108 +366,27 @@ core_target::core_target () void core_target::build_file_mappings () { - /* Type holding information about a single file mapped into the inferior - at the point when the core file was created. Associates a build-id - with the list of regions the file is mapped into. */ - struct mapped_file - { - /* Type for a region of a file that was mapped into the inferior when - the core file was generated. */ - struct region - { - /* Constructor. See member variables for argument descriptions. */ - region (CORE_ADDR start_, CORE_ADDR end_, CORE_ADDR file_ofs_) - : start (start_), - end (end_), - file_ofs (file_ofs_) - { /* Nothing. */ } - - /* The inferior address for the start of the mapped region. */ - CORE_ADDR start; - - /* The inferior address immediately after the mapped region. */ - CORE_ADDR end; - - /* The offset within the mapped file for this content. */ - CORE_ADDR file_ofs; - }; - - /* If not nullptr, then this is the build-id associated with this - file. */ - const bfd_build_id *build_id = nullptr; - - /* If true then we have seen multiple different build-ids associated - with the same filename. The build_id field will have been set back - to nullptr, and we should not set build_id in future. */ - bool ignore_build_id_p = false; - - /* All the mapped regions of this file. */ - std::vector regions; - }; - gdb::unordered_map bfd_map; gdb::unordered_set unavailable_paths; /* All files mapped into the core file. The key is the filename. */ - gdb::unordered_map mapped_files; + std::vector mapped_files + = gdb_read_core_file_mappings (m_core_gdbarch, + current_program_space->core_bfd ()); - /* See linux_read_core_file_mappings() in linux-tdep.c for an example - read_core_file_mappings method. */ - gdbarch_read_core_file_mappings (m_core_gdbarch, - current_program_space->core_bfd (), - - /* After determining the number of mappings, read_core_file_mappings - will invoke this lambda. */ - [&] (ULONGEST) - { - }, - - /* read_core_file_mappings will invoke this lambda for each mapping - that it finds. */ - [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs, - const char *filename, const bfd_build_id *build_id) - { - /* Architecture-specific read_core_mapping methods are expected to - weed out non-file-backed mappings. */ - gdb_assert (filename != nullptr); - - /* Add this mapped region to the data for FILENAME. */ - mapped_file &file_data = mapped_files[filename]; - file_data.regions.emplace_back (start, end, file_ofs); - if (build_id != nullptr && !file_data.ignore_build_id_p) - { - if (file_data.build_id == nullptr) - file_data.build_id = build_id; - else if (!build_id_equal (build_id, file_data.build_id)) - { - warning (_("Multiple build-ids found for %ps"), - styled_string (file_name_style.style (), filename)); - file_data.build_id = nullptr; - file_data.ignore_build_id_p = true; - } - } - }); - - /* Get the build-id of the core file. */ - const bfd_build_id *core_build_id - = build_id_bfd_get (current_program_space->core_bfd ()); - - for (const auto &[filename, file_data] : mapped_files) + for (const core_mapped_file &file_data : mapped_files) { - /* If this mapped file has the same build-id as was discovered for - the core-file itself, then we assume this is the main - executable. Record the filename as we can use this later. */ - if (file_data.build_id != nullptr - && m_expected_exec_filename.empty () - && build_id_equal (file_data.build_id, core_build_id)) - m_expected_exec_filename = filename; + /* If this mapped file is marked as the main executable then record + the filename as we can use this later. */ + if (file_data.is_main_exec && m_expected_exec_filename.empty ()) + m_expected_exec_filename = file_data.filename; /* Use exec_file_find() to do sysroot expansion. It'll also strip the potential sysroot "target:" prefix. If there is no sysroot, an equivalent (possibly more canonical) pathname will be provided. */ gdb::unique_xmalloc_ptr expanded_fname - = exec_file_find (filename.c_str (), nullptr); + = exec_file_find (file_data.filename.c_str (), nullptr); bool build_id_mismatch = false; if (expanded_fname != nullptr && file_data.build_id != nullptr) @@ -509,7 +428,7 @@ core_target::build_file_mappings () { abfd = find_objfile_by_build_id (current_program_space, file_data.build_id, - filename.c_str ()); + file_data.filename.c_str ()); if (abfd != nullptr) { @@ -527,7 +446,7 @@ core_target::build_file_mappings () } std::vector ranges; - for (const mapped_file::region ®ion : file_data.regions) + for (const core_mapped_file::region ®ion : file_data.regions) ranges.emplace_back (region.start, region.end - region.start); if (expanded_fname == nullptr @@ -545,7 +464,7 @@ core_target::build_file_mappings () bool content_is_in_core_file_p = true; /* Record all regions for this file as unavailable. */ - for (const mapped_file::region ®ion : file_data.regions) + for (const core_mapped_file::region ®ion : file_data.regions) { /* Check to see if the region is available within the core file. */ @@ -577,33 +496,33 @@ core_target::build_file_mappings () if (build_id_mismatch) { if (expanded_fname == nullptr - || filename == expanded_fname.get ()) + || file_data.filename == expanded_fname.get ()) warning (_("File %ps doesn't match build-id from core-file " "during file-backed mapping processing"), styled_string (file_name_style.style (), - filename.c_str ())); + file_data.filename.c_str ())); else warning (_("File %ps which was expanded to %ps, doesn't match " "build-id from core-file during file-backed " "mapping processing"), styled_string (file_name_style.style (), - filename.c_str ()), + file_data.filename.c_str ()), styled_string (file_name_style.style (), expanded_fname.get ())); } else if (!content_is_in_core_file_p) { if (expanded_fname == nullptr - || filename == expanded_fname.get ()) + || file_data.filename == expanded_fname.get ()) warning (_("Can't open file %ps during file-backed mapping " "note processing"), styled_string (file_name_style.style (), - filename.c_str ())); + file_data.filename.c_str ())); else warning (_("Can't open file %ps which was expanded to %ps " "during file-backed mapping note processing"), styled_string (file_name_style.style (), - filename.c_str ()), + file_data.filename.c_str ()), styled_string (file_name_style.style (), expanded_fname.get ())); } @@ -617,7 +536,7 @@ core_target::build_file_mappings () abfd.get ()); /* Create sections for each mapped region. */ - for (const mapped_file::region ®ion : file_data.regions) + for (const core_mapped_file::region ®ion : file_data.regions) { /* Make new BFD section. All sections have the same name, which is permitted by bfd_make_section_anyway(). */ @@ -653,7 +572,7 @@ core_target::build_file_mappings () soname = gdb_bfd_read_elf_soname (actual_filename); } - m_mapped_file_info.add (soname.get (), filename.c_str (), + m_mapped_file_info.add (soname.get (), file_data.filename.c_str (), actual_filename, std::move (ranges), file_data.build_id); } @@ -2154,6 +2073,103 @@ mapped_file_info::lookup (const char *filename, /* See gdbcore.h. */ +std::vector +gdb_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd) +{ + std::vector results; + + /* A map entry used while building RESULTS. */ + struct map_entry + { + explicit map_entry (core_mapped_file *ptr) + : file_data (ptr) + { /* Nothing. */ } + + /* Points to an entry in RESULTS, this allows entries to be quickly + looked up and updated as new mappings are read. */ + core_mapped_file *file_data = nullptr; + + /* If true then we have seen multiple different build-ids associated + with the filename of FILE_DATA. The FILE_DATA->build_id field will + have been set to nullptr, and we should not set FILE_DATA->build_id + in future. */ + bool ignore_build_id_p = false; + }; + + /* All files mapped into the core file. The key is the filename. */ + gdb::unordered_map mapped_files; + + /* Get the build-id of the core file. At least on Linux, this will be + the build-id for the main executable. If other targets add the + gdbarch_read_core_file_mappings method, then it might turn out that + this logic is no longer true, in which case this might need to move + into the gdbarch_read_core_file_mappings method. */ + const bfd_build_id *core_build_id = build_id_bfd_get (cbfd); + + /* See linux_read_core_file_mappings() in linux-tdep.c for an example + read_core_file_mappings method. */ + gdbarch_read_core_file_mappings (gdbarch, cbfd, + /* After determining the number of mappings, read_core_file_mappings + will invoke this lambda. */ + [&] (ULONGEST) + { + }, + + /* read_core_file_mappings will invoke this lambda for each mapping + that it finds. */ + [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs, + const char *filename, const bfd_build_id *build_id) + { + /* Architecture-specific read_core_mapping methods are expected to + weed out non-file-backed mappings. */ + gdb_assert (filename != nullptr); + + /* Add this mapped region to the data for FILENAME. */ + auto iter = mapped_files.find (filename); + if (iter == mapped_files.end ()) + { + /* Create entry in results list. */ + results.emplace_back (); + + /* The entry to be added to the lookup map. */ + map_entry entry (&results.back ()); + entry.file_data->filename = filename; + + /* Add entry to the quick lookup map and update ITER. */ + auto inserted_result + = mapped_files.insert ({filename, std::move (entry)}); + gdb_assert (inserted_result.second); + iter = inserted_result.first; + } + + core_mapped_file &file_data = *iter->second.file_data; + bool &ignore_build_id_p = iter->second.ignore_build_id_p; + + file_data.regions.emplace_back (start, end, file_ofs); + if (build_id != nullptr && !ignore_build_id_p) + { + if (file_data.build_id == nullptr) + file_data.build_id = build_id; + else if (!build_id_equal (build_id, file_data.build_id)) + { + warning (_("Multiple build-ids found for %ps"), + styled_string (file_name_style.style (), filename)); + file_data.build_id = nullptr; + ignore_build_id_p = true; + } + } + + if (build_id != nullptr + && core_build_id != nullptr + && build_id_equal (build_id, core_build_id)) + file_data.is_main_exec = true; + }); + + return results; +} + +/* See gdbcore.h. */ + std::optional core_target_find_mapped_file (const char *filename, std::optional addr) diff --git a/gdb/gdbcore.h b/gdb/gdbcore.h index e0e3843c97b..23432ecbd07 100644 --- a/gdb/gdbcore.h +++ b/gdb/gdbcore.h @@ -258,4 +258,47 @@ std::optional core_target_find_mapped_file (const char *filename, std::optional addr); +/* Type holding information about a single file mapped into the inferior + at the point when the core file was created. Associates a build-id + with the list of regions the file is mapped into. */ +struct core_mapped_file +{ + /* Type for a region of a file that was mapped into the inferior when + the core file was generated. */ + struct region + { + /* Constructor. See member variables for argument descriptions. */ + region (CORE_ADDR start_, CORE_ADDR end_, CORE_ADDR file_ofs_) + : start (start_), + end (end_), + file_ofs (file_ofs_) + { /* Nothing. */ } + + /* The inferior address for the start of the mapped region. */ + CORE_ADDR start; + + /* The inferior address immediately after the mapped region. */ + CORE_ADDR end; + + /* The offset within the mapped file for this content. */ + CORE_ADDR file_ofs; + }; + + /* The filename as recorded in the core file. */ + std::string filename; + + /* If not nullptr, then this is the build-id associated with this + file. */ + const bfd_build_id *build_id = nullptr; + + /* All the mapped regions of this file. */ + std::vector regions; + + /* True if this is the main executable. */ + bool is_main_exec = false; +}; + +extern std::vector gdb_read_core_file_mappings + (struct gdbarch *gdbarch, struct bfd *cbfd); + #endif /* GDB_GDBCORE_H */ -- 2.47.1