From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id bJYGI3qk0miySQ8AWB0awg (envelope-from ) for ; Tue, 23 Sep 2025 09:45:30 -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=Mr9Cp5qJ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 7089C1E0BA; Tue, 23 Sep 2025 09:45:30 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 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_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham 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 DA5A41E047 for ; Tue, 23 Sep 2025 09:45:28 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 15939385841F for ; Tue, 23 Sep 2025 13:45:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15939385841F 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=Mr9Cp5qJ Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id D34903858421 for ; Tue, 23 Sep 2025 13:44:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D34903858421 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 D34903858421 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758635060; cv=none; b=kRBqmEx0Mbfwmu8EhK/usLiQvSSHPLnQ/H2Ny5YdukcWtjDQJU0DDUo+YFIrt/27OmSXh5vvRUoGPtTRmMI/++hruWcPIuu43D1npbM5l09Ml8D54eQpTuxXpTVRl8O7yABZxPUEWIZj7S5tZ54VZvVaJ95QLcPVNjk9s2HjShc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758635060; c=relaxed/simple; bh=f1yEgeOTWEeUyPURgGjl7PkCmPh1NqHSuAebNN7taOQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UmaDMctTXnx6exEcZlvDCoH9grFenLLiQpi8S2LHkHzI6AuPlFi2HdvR4fJV7aiswNEwdavVTqT9WcRbbw94q8v2elDsitG+bozwLVWc00ApCrQOSRSGLH1RbbBbjNF7opDlKPLBPLn4OuyHyIQRAH+RQlYzbNtAlNi+nDOCPYs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D34903858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758635060; 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=MiT0FqtolnWjahpAG0U2dlZQ0pFdR2G3N0tz52Dml2M=; b=Mr9Cp5qJUuMvnqoV2UF3+Ktik1R05n3rDm607nXjZqgi+EtMUFTaNOdFd/XaBwxm+fGU0x jIKCKDo/iyWvRSsXYtoE5+GPzb+mgmpE8SRDxGv+x4rz8p19oMxAOYettP2ROjiPwRpmfJ x3ZIW8lYiaZ+ZWPEnj8SeK5TdymhwIs= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-Xq2mOZZ4Oqut8Na5Qi9KmQ-1; Tue, 23 Sep 2025 09:44:19 -0400 X-MC-Unique: Xq2mOZZ4Oqut8Na5Qi9KmQ-1 X-Mimecast-MFC-AGG-ID: Xq2mOZZ4Oqut8Na5Qi9KmQ_1758635058 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-45e037fd142so48673305e9.3 for ; Tue, 23 Sep 2025 06:44:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758635057; x=1759239857; 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=MiT0FqtolnWjahpAG0U2dlZQ0pFdR2G3N0tz52Dml2M=; b=Siyfln3+qkTfQXn7JS6Mc4ATp/1L29Z/wTo5Ed6EYTGh9xBeOkgTGXzoIrOmAcajXX n6JZ4HMIFYJoeV1QaKPEKLMAI7/hTzXbxcsj/5xW9oc59zPPZOFKhksBU+I8VbW5HpZh G3xqXd4n8Lq9GDpkZdcPyX2pAj8pvvnuKa0lf2Ad7+jzpcLYAmG3akBhxpffHIQ9+ZrO llWo6jsAvTWe5ccVkqw4BAGGr98h9q5AyLCPfEKpfwnz7GqPk1GKy6j2H/GkVOSq/uOG yTAQvPYrv+bFkALYb8sBlCov5dV7dB1wiViOHX1cU5xqWUgIWEhBXvQAP/gxEYLvF9vQ gvGw== X-Gm-Message-State: AOJu0Yyl3DWcIwy2pUgwBbXVnAAC7D/+Fj7GmL5oeb5RZ5HDVBtTOzr0 w3rajegzTXgw6fhNNyESobdaAUb37soif+m7u7xJ3evW9KYwIrRQjSyNMnvOb2LeZ8aVp6LNmTt VK+3G8PpSy+YPnRMkRGdxZgAqRXZsstDDhPnNEMM1IJdr1wpbQuiDGc+IslyCBeCkC4goYvzDCP iUjj6v3VUYCi9OpA6BFExjGD3tte6i4B5ntEOoCIJIo7TWnMk= X-Gm-Gg: ASbGncujXz7WOfPWo1Tl710YLi7ImKxz7MfcqY6WYGLuAV+4T53pcDdys2iE2ABzsTe 2Hxmai9uxF1D19pUDUzJecKPIpzU2LZF+vFtcOfBo5fMGZ2DD3440eKYH4a/sMhTp8BMJkBDzjE /8HMPFmpx8oiuX2quKW3eBpxNHUzA9DvzmDtcoTMX4J94rXepzfQ3Zx9qSxbPRvzk2jzdYauNyp epEDBR39Cz2bDFBQHaLgfZognMmcunldzBJvjvzfkYieVwbYr02TuQJA1Fzu4QhX4eEsUfWSABH kVS7hSpN+3I559dzh2LousjxCLAV25nTEvQ= X-Received: by 2002:a05:600c:c83:b0:46d:5572:547 with SMTP id 5b1f17b1804b1-46e1dab275bmr33541985e9.24.1758635057312; Tue, 23 Sep 2025 06:44:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzVy2uzZZ52xVXUofVQB5+buGBTly6l+by/PJi/M4n83XGSt0z5nQ15ADrubltA9ZOuH0iXQ== X-Received: by 2002:a05:600c:c83:b0:46d:5572:547 with SMTP id 5b1f17b1804b1-46e1dab275bmr33541465e9.24.1758635056641; Tue, 23 Sep 2025 06:44:16 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-46e1ce0f87csm17314245e9.0.2025.09.23.06.44.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Sep 2025 06:44:16 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Tom Tromey Subject: [PATCHv2 2/3] gdb: make structured core file mappings processing global Date: Tue, 23 Sep 2025 14:44:07 +0100 Message-ID: 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: AXSykwJZ3MPMFZv19gp-AlFQYfRCraU7l38Hw-L_XW8_1758635058 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 Approved-By: Tom Tromey --- 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 2f202dc1fbf..ee97e29556e 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); } @@ -2163,6 +2082,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