From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id cRPsOphx/Gn8qx4AWB0awg (envelope-from ) for ; Thu, 07 May 2026 07:03:52 -0400 Received: by simark.ca (Postfix, from userid 112) id EC1961E093; Thu, 07 May 2026 07:03:52 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, 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 vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (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 815051E093 for ; Thu, 07 May 2026 07:03:46 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 06CD04BA2E1C for ; Thu, 7 May 2026 11:03:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06CD04BA2E1C Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by sourceware.org (Postfix) with ESMTPS id 337D64BA5436 for ; Thu, 7 May 2026 11:03:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 337D64BA5436 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 337D64BA5436 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778151799; cv=none; b=TSqG3b7bg0vCu+da2IJV8aSSGWehlxpUvvUSesRcq/dxl5Jhj0vL4HpEhkMXJb6StYmSbKd1wHfYj0AFEwbsv+Idt2zgTU1YZyca90rb8SAazUHPnKc7MQhxkGR2/0+yqzLphPuL51wOz6TI2mAuCb+0S+IpEhhYuU1aE6DDJH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778151799; c=relaxed/simple; bh=1d0Pn8IJacejYfM6yls6zpiMFLdj3/8rujV8CSe2JMI=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=UuXb0/aOxr2eAWXEwbiil9fgflO0E3jfZz6JpMvENtLatH1SpXKxnOyyAdGX6pDCqwXVWFZfjCcVEW7PP7WPifyom8XFXFNOoewX1HI9Y4h6UtbmEkiUqyvWU90NyORaMn25mFeiYi14Om4EvNH75TIShN0IglAGaw4o61Y4UgA= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 337D64BA5436 Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4891d7164ddso4305665e9.3 for ; Thu, 07 May 2026 04:03:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778151798; x=1778756598; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7VTs7TfD3PlL7jtcSGRpuYBH9aaYsbM9cbc1s3nN21A=; b=roAaoNKHJA/wCI/hKHn9zXaQ9AUm/gNUvL9jOo1++8Hlw7janYDx7ExnYKDo6SHQCz OUhYxZZoFHffi16HyJvjGFqamReRHEQtr2FaxFeCHyYnlWA9ClUK3yzJLfKPfbHGGgIJ ZdmXl4RwDPLXTU9yGEPOhbTpFmT7fzzCAz79iQgYTIfTFpRPVCLjxokPlzfYlVhKUGa2 gIsobMYgdLg+N/zvLxIBjNJKt+4CEuTwapRclLltYPLsRbu9wnzKlZyN6g0QH6ygqus9 B4MN6a+EKY/pv1s3eJ16iHNgpk3/aMgjgdc/yKmKLUB86Pv2cYY861e9I0gb1JB+7laf igHw== X-Gm-Message-State: AOJu0YzMWq6/J8avY6DaAuhBz4MRo8LUHDO7GOC1DMGNraH/n1RTYhtl L4dIn0GCIKHneKvzryH127iMSwWbZYE5Up3wXloajNIzNxnYFidA9Mn04vVwDw== X-Gm-Gg: AeBDiesJxxj4FYBhRrSCf2XBXkay2KbsKmCx6IuJQnYnKHKdrQayqJGRJ7PImRqQo+z CP0s5ggQH8B4/3N9uiHH1HWJ/6Tn0njM+/ykpwvyxBOx3MKwKm8kAloV9icm6ZbeZmE8JjhABHZ 2PapGw7VOFcRZF5Bl5pxUq5n16hErHZKWq5LuXELYzjf8WdJRv+aGzmcooyQmhdic3lbA9gAu2x avv6vFeNtgXKRfuV6UuDU1shndMPwTe86118d8egGQAt03FeM4nXJ/1xc2O/RYdlvrzEMpJI8Hl FmbjLmQN3QdMyW32qH9U32whiSxhYYwYguYVuiis7psnpb7+wJFWHreJauw+CawqxorTkPZpcRT X8eEirlcfyQjR7OrWDtVcRaV37ccc+xwUM2HKbNjCqZuiWp9gvbm2WTJ0hQAlvYwvjZIHIuJzK3 28NIxbTvEciutRINiA6u2UNTLyqisS3oFkuNlSwOOisw== X-Received: by 2002:a05:600c:46d2:b0:485:364e:9328 with SMTP id 5b1f17b1804b1-48e51f32aebmr116868925e9.16.1778151797068; Thu, 07 May 2026 04:03:17 -0700 (PDT) Received: from localhost ([2001:8a0:facb:a800:251:1007:919b:653b]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-48e5312daf8sm38360915e9.20.2026.05.07.04.03.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 May 2026 04:03:16 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH] gcore: handle known-all-zeroes hole mappings Date: Thu, 7 May 2026 12:03:01 +0100 Message-ID: <20260507110301.345066-1-pedro@palves.net> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 This commit improves core generation on Linux. The issue this addresses became apparent when dumping core of HIP programs on AMD GPUs, but it's strictly a CPU-side issue. The AMD GPU runtime used by HIP (and other languages) programs creates these big mappings in every program, the size of GPU VRAM. E.g., on gfx942 with 256GB VRAM, we end up with 256GB of such mappings: (gdb) info proc mappings ... 0x00007fbfe7000000 0x00007fc7e7000000 0x800000000 0x0 ---p 0x00007fc7e7200000 0x00007fcfe7200000 0x800000000 0x0 ---p 0x00007fcfe7400000 0x00007fd7e7400000 0x800000000 0x0 ---p 0x00007fd7e7600000 0x00007fdfe7600000 0x800000000 0x0 ---p 0x00007fdfe7800000 0x00007fe7e7800000 0x800000000 0x0 ---p 0x00007fe7e7a00000 0x00007fefe7a00000 0x800000000 0x0 ---p 0x00007fefe7c00000 0x00007ff7e7c00000 0x800000000 0x0 ---p ... They show up like this in /proc/pid/smaps ... 7fbfe7000000-7fc7e7000000 ---p 00000000 00:00 0 Size: 33554432 kB KernelPageSize: 4 kB MMUPageSize: 4 kB Rss: 0 kB Pss: 0 kB Pss_Dirty: 0 kB Shared_Clean: 0 kB Shared_Dirty: 0 kB Private_Clean: 0 kB Private_Dirty: 0 kB Referenced: 0 kB Anonymous: 0 kB KSM: 0 kB LazyFree: 0 kB AnonHugePages: 0 kB ShmemPmdMapped: 0 kB FilePmdMapped: 0 kB Shared_Hugetlb: 0 kB Private_Hugetlb: 0 kB Swap: 0 kB SwapPss: 0 kB Locked: 0 kB THPeligible: 0 ProtectionKey: 0 VmFlags: mr mw me nr sd ... Note the mappings in question have no backing data: the Rss: and Swap: fields are zero. The mappings exist only to reserve VMAs on the CPU side. When the Linux kernel itself dumps core for such a process, here's what objdump -h on the core shows for such mappings: Idx Name Size VMA LMA File off Algn ... 39 load17 800000000 000070aeb3200000 0000000000000000 17555000 2**12 ALLOC, READONLY 40 load18 800000000 000070b6b3400000 0000000000000000 17555000 2**12 ALLOC, READONLY ... GDB's gcore, not knowing the mappings contain only zeroes, reads all their contents from inferior memory, all 256GB of it. On gfx942, that makes the gcore command take around 1 minute when dumping a small HIP program. GDB's gcore currently generates load segments of these mappings like this: Idx Name Size VMA LMA File off Algn ... 40 load17 800000000 00007fcfe7400000 0000000000000000 101e0a7bd0 2**0 CONTENTS, ALLOC, LOAD, READONLY 41 load18 800000000 00007fd7e7600000 0000000000000000 181e0a7bd0 2**0 CONTENTS, ALLOC, LOAD, READONLY ... The fact that GDB makes CONTENTS/LOAD segments makes it so that the resulting core files are much larger than what the Linux kernel produces (note "File off" column), even though it's mostly apparent size as gdb knows how to produce sparse cores: $ ls -als --hu core/* 440M -rw------- 1 pedalves pedalves 474M Mar 24 08:14 core/kernel.core 51M -rw-rw-r-- 1 pedalves pedalves 257G Mar 24 08:16 core/gdb-before.core ^^^^ real ^^^^ apparent This commit teaches linux-tdep.c to identify anonymous mappings that have no backing data from /proc/pid/smaps (which we already parse), so that the gcore code can skip reading inferior memory for them, and their load segments can be emitted with no CONTENTS/LOAD. The skip-reading-inferior-memory part speeds up core dumping of small HIP inferiors on gfx942 GPUs by a large factor. E.g.: With: $ time rocgdb --batch -q small-test-program \ -ex "with breakpoint pending on -- b gpu_code" \ -ex "r" \ -ex "gcore" \ -ex "k" On gfx942, before the patch: real 1m6.518s user 0m5.984s sys 1m0.218s On gfx942, after the patch: real 0m4.456s user 0m2.219s sys 0m1.829s And the fact that we no longer emit segments with CONTENTS/ALLOW makes the resulting gdb-generated cores's apparent size be much smaller, closer to kernel-generated cores's: $ ls -als --hu core/* 440M -rw------- 1 pedalves pedalves 474M Mar 24 08:14 core/kernel.core 51M -rw-rw-r-- 1 pedalves pedalves 257G Mar 24 08:16 core/gdb-before.core 52M -rw-rw-r-- 1 pedalves pedalves 642M Mar 24 09:12 core/gdb-after.core This commit also adds a new testcase that exercises both the scenario in question (without relying on the HIP runtime), and the converse of making sure that we don't skip dumping anonymous private PROT_NONE mappings with backing data, by mistake. Change-Id: I2cf21409af36266094bcff5614770605fab4030e commit-id: d3d471d8 --- gdb/find-memory-region.h | 5 +- gdb/gcore.c | 28 ++-- gdb/linux-tdep.c | 85 +++++++++-- .../gdb.base/gcore-anon-priv-protnone.c | 60 ++++++++ .../gdb.base/gcore-anon-priv-protnone.exp | 133 ++++++++++++++++++ 5 files changed, 288 insertions(+), 23 deletions(-) create mode 100644 gdb/testsuite/gdb.base/gcore-anon-priv-protnone.c create mode 100644 gdb/testsuite/gdb.base/gcore-anon-priv-protnone.exp diff --git a/gdb/find-memory-region.h b/gdb/find-memory-region.h index 23776caebb2..85f4a4acd0f 100644 --- a/gdb/find-memory-region.h +++ b/gdb/find-memory-region.h @@ -27,11 +27,14 @@ MEMORY_TAGGED is true if the memory region contains memory tags, false otherwise. + HOLE is true if the memory region is known to be all zeroes, false + otherwise. + Return true on success, false otherwise. */ using find_memory_region_ftype = gdb::function_view; + bool memory_tagged, bool hole)>; #endif /* GDB_FIND_MEMORY_REGION_H */ diff --git a/gdb/gcore.c b/gdb/gcore.c index 149ade2a08f..5c0fe325639 100644 --- a/gdb/gcore.c +++ b/gdb/gcore.c @@ -403,10 +403,13 @@ make_output_phdrs (bfd *obfd, asection *osec) static bool gcore_create_callback (CORE_ADDR vaddr, unsigned long size, bool read, bool write, bool exec, bool modified, - bool memory_tagged, bfd *obfd) + bool memory_tagged, bool hole, bfd *obfd) { asection *osec; - flagword flags = SEC_ALLOC | SEC_HAS_CONTENTS | SEC_LOAD; + flagword flags = SEC_ALLOC; + + if (!hole) + flags |= SEC_HAS_CONTENTS | SEC_LOAD; /* If the memory segment has no permissions set, ignore it, otherwise when we later try to access it for read/write, we'll get an error @@ -421,7 +424,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size, bool read, return true; } - if (!write && !modified && !solib_keep_data_in_core (vaddr, size)) + if (!hole && !write && !modified && !solib_keep_data_in_core (vaddr, size)) { /* See if this region of memory lies inside a known file on disk. If so, we can avoid copying its contents by clearing SEC_LOAD. */ @@ -493,7 +496,7 @@ static bool gcore_create_memtag_section_callback (CORE_ADDR vaddr, unsigned long size, bool read, bool write, bool exec, bool modified, bool memory_tagged, - bfd *obfd) + bool hole, bfd *obfd) { /* Are there memory tags in this particular memory map entry? */ if (!memory_tagged) @@ -550,7 +553,8 @@ objfile_find_memory_regions (struct target_ops *self, (flags & SEC_READONLY) == 0, /* Writable. */ (flags & SEC_CODE) != 0, /* Executable. */ true, /* MODIFIED is unknown, pass it as true. */ - false /* No memory tags in the object file. */); + false, /* No memory tags in the object file. */ + false /* Not known to be an all-zeroes hole. */); if (!ret) return ret; } @@ -563,7 +567,8 @@ objfile_find_memory_regions (struct target_ops *self, true, /* Stack section will be writable. */ false, /* Stack section will not be executable. */ true, /* Stack section will be modified. */ - false /* No memory tags in the object file. */)) + false, /* No memory tags in the object file. */ + false /* Not known to be an all-zeroes hole. */)) return false; /* Make a heap segment. */ @@ -574,7 +579,8 @@ objfile_find_memory_regions (struct target_ops *self, true, /* Heap section will be writable. */ false, /* Heap section will not be executable. */ true, /* Heap section will be modified. */ - false /* No memory tags in the object file. */)) + false, /* No memory tags in the object file. */ + false /* Not known to be an all-zeroes hole. */)) return false; return true; @@ -845,10 +851,10 @@ static bool gcore_memory_sections (bfd *obfd) { auto cb = [obfd] (CORE_ADDR vaddr, unsigned long size, bool read, bool write, - bool exec, bool modified, bool memory_tagged) + bool exec, bool modified, bool memory_tagged, bool hole) { return gcore_create_callback (vaddr, size, read, write, exec, modified, - memory_tagged, obfd); + memory_tagged, hole, obfd); }; /* Try gdbarch method first, then fall back to target method. */ @@ -862,11 +868,11 @@ gcore_memory_sections (bfd *obfd) auto cb_memtag = [obfd] (CORE_ADDR vaddr, unsigned long size, bool read, bool write, bool exec, bool modified, - bool memory_tagged) + bool memory_tagged, bool hole) { return gcore_create_memtag_section_callback (vaddr, size, read, write, exec, modified, - memory_tagged, obfd); + memory_tagged, hole, obfd); }; /* Take care of dumping memory tags, if there are any. */ diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 4bc5a2b6948..1abb9fba067 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -121,6 +121,9 @@ struct smaps_data ULONGEST inode; ULONGEST offset; + + ULONGEST rss; + ULONGEST swap; }; /* Whether to take the /proc/PID/coredump_filter into account when @@ -1425,12 +1428,46 @@ linux_core_xfer_siginfo (struct gdbarch *gdbarch, struct bfd &cbfd, } using linux_find_memory_region_ftype - = gdb::function_view; + = gdb::function_view; typedef bool linux_dump_mapping_p_ftype (filter_flags filterflags, const smaps_data &map); +/* Parse a KEY value out of a /proc/pid/smaps line. KEYWORD is the + keyword that was extracted out of the LINE we're considering. + MAPS_FILENAME is the /proc/pid/smaps filename. The result is + written to *VALUE. Returns true if LINE is a line for KEY, false + otherwise. */ + +static bool +parse_smaps_key_value (const char *keyword, const char *line, + const char *key, + const std::string &maps_filename, + ULONGEST *value) +{ + if (!streq (keyword, key)) + return false; + + const char *startptr = skip_spaces (line + strlen (key)); + const char *endptr; + *value = strtoulst (startptr, &endptr, 0); + if (endptr == startptr) + { + warning (_("Error parsing {s,}maps file '%s' number"), + maps_filename.c_str ()); + } + return true; +} + /* Helper function to parse the contents of /proc//smaps into a data structure, for easy access. @@ -1456,6 +1493,8 @@ parse_smaps_data (const char *data, int has_anonymous = 0; int mapping_anon_p; int mapping_file_p; + ULONGEST rss = -1; + ULONGEST swap = -1; memset (&v, 0, sizeof (v)); struct mapping m = read_mapping (line); @@ -1513,6 +1552,16 @@ parse_smaps_data (const char *data, else if (streq (keyword, "VmFlags:")) decode_vmflags (line, &v); + if (parse_smaps_key_value (keyword, line, "Rss:", + maps_filename, + &rss)) + continue; + + if (parse_smaps_key_value (keyword, line, "Swap:", + maps_filename, + &swap)) + continue; + if (streq (keyword, "AnonHugePages:") || streq (keyword, "Anonymous:")) { @@ -1562,6 +1611,8 @@ parse_smaps_data (const char *data, map.mapping_file_p = mapping_file_p? true : false; map.offset = m.offset; map.inode = m.inode; + map.rss = rss; + map.swap = swap; smaps.emplace_back (map); } @@ -1692,12 +1743,23 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch, for (const struct smaps_data &map : smaps) { /* Invoke the callback function to create the corefile segment. */ - if (should_dump_mapping_p (filterflags, map) - && !func (map.start_address, map.end_address - map.start_address, - map.offset, map.read, map.write, map.exec, - /* MODIFIED is true because we want to dump the mapping. */ - true, map.vmflags.memory_tagging != 0, map.filename)) - return false; + if (should_dump_mapping_p (filterflags, map)) + { + /* If this an anonymous PROT_NONE private mapping, check if + the mapping has any physical memory backing. If not, + then we know that reading it would just yield zeroes, so + we can later skip reading it. */ + bool hole = (map.mapping_anon_p && map.priv + && !map.read && !map.write && !map.exec + && map.rss == 0 && map.swap == 0); + if (!func (map.start_address, map.end_address - map.start_address, + map.offset, map.read, map.write, map.exec, + /* MODIFIED is true because we want to dump the + mapping. */ + true, map.vmflags.memory_tagging != 0, hole, + map.filename)) + return false; + } } return true; @@ -1712,8 +1774,9 @@ linux_find_memory_regions (struct gdbarch *gdbarch, { auto cb = [&] (ULONGEST vaddr, ULONGEST size, ULONGEST offset, bool read, bool write, bool exec, bool modified, bool memory_tagged, - const std::string &filename) - { return func (vaddr, size, read, write, exec, modified, memory_tagged); }; + bool hole, const std::string &filename) + { return func (vaddr, size, read, write, exec, modified, memory_tagged, + hole); }; return linux_find_memory_regions_full (gdbarch, dump_mapping_p, cb); } @@ -1741,7 +1804,7 @@ linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, auto cb = [&] (ULONGEST vaddr, ULONGEST size, ULONGEST offset, bool read, bool write, bool exec, bool modified, bool memory_tagged, - const std::string &filename) + bool hole, const std::string &filename) { gdb_assert (filename.length () > 0); diff --git a/gdb/testsuite/gdb.base/gcore-anon-priv-protnone.c b/gdb/testsuite/gdb.base/gcore-anon-priv-protnone.c new file mode 100644 index 00000000000..38cd5da4fb0 --- /dev/null +++ b/gdb/testsuite/gdb.base/gcore-anon-priv-protnone.c @@ -0,0 +1,60 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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 +#include +#include +#include +#include + +void +done () +{ +} + +void *mmapped_data; + +int +main () +{ + /* 10 pages on most systems. */ + size_t sz = 4096 * 10; + + /* Allocate anonymous memory. */ + mmapped_data = mmap (NULL, sz, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (mmapped_data == MAP_FAILED) + { + perror ("mmap"); + return 1; + } + +#ifdef FILL_WITH_DATA + /* Fill with a recognizable pattern. */ + memset (mmapped_data, 0xab, sz); +#endif + + /* Remove all permissions. */ + if (mprotect (mmapped_data, sz, PROT_NONE) == -1) + { + perror ("mprotect"); + return 1; + } + + done (); + return 0; +} diff --git a/gdb/testsuite/gdb.base/gcore-anon-priv-protnone.exp b/gdb/testsuite/gdb.base/gcore-anon-priv-protnone.exp new file mode 100644 index 00000000000..d6c32b20cf4 --- /dev/null +++ b/gdb/testsuite/gdb.base/gcore-anon-priv-protnone.exp @@ -0,0 +1,133 @@ +# Copyright 2026 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 . + +# Make sure that: +# +# data mode - gcore dumps 'PROT_NONE anonymous private mappings that +# have resident data' as segments with data. +# +# nodata mode - gcore dumps 'PROT_NONE anonymous private mappings that +# have no resident data' as segments with no data (not even zeroes). +# +# Also make sure that GDB is able to read back the produced cores and +# read the contents of the mappings. In nodata mode, GDB should read +# back zeroes. + +standard_testfile + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return +} + +# MODE is either data or nodata. +proc test {mode} { + global srcfile binfile + + set options {debug} + if { $mode == "data" } { + lappend options "additional_flags=-DFILL_WITH_DATA" + } + + if { [build_executable "failed to prepare" ${binfile}-$mode ${srcfile} $options] } { + return + } + + clean_restart + gdb_load $binfile-$mode + + # Get the core into the output directory. + set_inferior_cwd_to_output_dir + + if {![runto done]} { + return + } + + if { $mode == "data" } { + set val "0xab" + } else { + set val "0x0" + } + + gdb_test "p/x *(unsigned char *) mmapped_data@40960" \ + "\\{$val \\}" \ + "access mmapped data, live" + + set corefile [standard_output_file ${binfile}-$mode.corefile] + gdb_gcore_cmd "$corefile" "gcore corefile" + + gdb_test "core $corefile" \ + "Core was generated by.*" \ + "load corefile" \ + "A program is being debugged already. Kill it. .y or n. " \ + "y" + + gdb_test "p/x *(unsigned char *) mmapped_data@40960" \ + "\\{$val \\}" \ + "access mmapped data, core" + + # Use readelf to make sure the load segment for our mapping in the + # core file looks like what we want it to. + + # Get the address and strip the '0x'. + set mapped_addr_no_0x "UNKNOWN" + gdb_test_multiple "print/x mmapped_data" "get address" { + -re -wrap " = 0x(\[0-9a-f\]+).*" { + set mapped_addr_no_0x $expect_out(1,string) + pass "$gdb_test_name" + } + } + + # Construct a regex that matches "0x" followed by optional leading + # zeros, then our specific hex address. For example, if + # mapped_addr_no_0x is 7ffff7fb3000, this matches + # 0x00007ffff7fb3000 and 0x7ffff7fb3000. + set mapped_addr_re "0x0*${mapped_addr_no_0x}" + + set readelf_program [gdb_find_readelf] + set cmd [list $readelf_program -lW $corefile] + set res [catch {exec {*}$cmd} output] + verbose -log "running: $cmd" + verbose -log "result: $res" + verbose -log "output: $output" + if { $res == 0 } { + # Look for LOAD up to MemSiz, and capture FileSiz and MemSiz, in: + # + # Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align + # + # Where VirtAddr is our mapping's address. + set ws "\[\t \]+" + set p_header_re \ + "LOAD${ws}$::hex${ws}${mapped_addr_re}${ws}$::hex${ws}($::hex)${ws}($::hex)${ws}" + set res [regexp $p_header_re $output full filesiz memsiz] + set test "segment has expected sizes" + if { $res } { + verbose -log "res = $res" + verbose -log "filesiz: $filesiz" + verbose -log "memsiz: $memsiz" + + if { $mode == "data" } { + gdb_assert { $filesiz == $memsiz && $filesiz != 0 } $test + } else { + gdb_assert { $filesiz == 0 } $test + } + } else { + fail "$test (regexp failed)" + } + } +} + +foreach_with_prefix mode { "data" "nodata" } { + test $mode +} base-commit: e1428067748d6b713637241855d1c315fb657c8b -- 2.53.0