From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id z+ztNQRVv2eCpj4AWB0awg (envelope-from ) for ; Wed, 26 Feb 2025 12:53:08 -0500 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=Vm9B/w/f; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id CE50F1E105; Wed, 26 Feb 2025 12:53:08 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.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 autolearn=ham autolearn_force=no version=4.0.0 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 C3C971E05C for ; Wed, 26 Feb 2025 12:53:06 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5B2063858D26 for ; Wed, 26 Feb 2025 17:53:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B2063858D26 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=Vm9B/w/f 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 33F683858D26 for ; Wed, 26 Feb 2025 17:52:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 33F683858D26 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 33F683858D26 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=1740592351; cv=none; b=PVHWJGD1HbIUoNGG8FNv1XB84pYQwaU/PZDy16RyglsBvRbNSuF7zT4ShAriYji6TAv+dIBCW7f8dFA0CHFhLvwSMV7zRisjH1t5nOS+ElOgKlkKaCBeMxg2Ta02rIcLucYHBQrY5MCaXMhv7yIE+ScEE4aoooxME36uIZghhvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740592351; c=relaxed/simple; bh=8bBRjFPNc+VqyUlFiYFtRxM1Q6s2hY0UX1PXw5xed18=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=C1TYmEA40IaSWWoFjHX00iFRM7wIRy4HcDaDFcXUVBTjf6hmPbVOH0uc7Ls+DJUVAz2Uku2CrM22UJGMs+p7EdvXG2phtM7KMSWjBg4AQKwJMpxh9jHXDqq7IShRiWMxPaqFV4VdTyGgBK+szZ2UzlYx+VbHbi7CrRfJ55fnVm4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33F683858D26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740592350; 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=eWQXdI65TCpOVGs13GrJdRZmgd3RBvz651tvhFVGpg8=; b=Vm9B/w/frgf7cK2Fa3qaTTTOgR7hSfENhNA4xwO3hYZbdMJ1dW62ZgNeYgMN1XdWItpPlg B0MhZMoLi9EZ92zzeKoNeRjXc3pCaU4wy9NLkJpYZDIxXBbNnoCBabRgymxU5ylhyLcTYa wQ61r5ondF7K8eMVumyHr3ZPl5p6pNs= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-551-6GXbTTY0McKU6a1M6SH6pg-1; Wed, 26 Feb 2025 12:52:29 -0500 X-MC-Unique: 6GXbTTY0McKU6a1M6SH6pg-1 X-Mimecast-MFC-AGG-ID: 6GXbTTY0McKU6a1M6SH6pg_1740592348 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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 mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C5890190F9CB for ; Wed, 26 Feb 2025 17:52:28 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.96.134.114]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 43DB11955DCE; Wed, 26 Feb 2025 17:52:26 +0000 (UTC) From: Guinevere Larsen To: gdb-patches@sourceware.org Cc: Guinevere Larsen Subject: [PATCH v3] gdb/testsuite: add test for memory requirements of gcore Date: Wed, 26 Feb 2025 14:50:59 -0300 Message-ID: <20250226175058.3060581-2-guinevere@redhat.com> In-Reply-To: <20250225134544.2129276-1-guinevere@redhat.com> References: <20250225134544.2129276-1-guinevere@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1IbTKJkShJxxsBME-F6xQQyYXrabzIQInyiWa3bStQo_1740592348 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 For a long time, Fedora has been carrying an out-of-tree patch with a similar test to the one proposed in this patch, that ensures that the memory requirements don't grow with the inferior's memory. It's been so long that the context for why this test exists has been lost, but it looked like it could be interesting for upstream. The test runs twice, once with the inferior allocating 4Mb of memory, and the other allocating 64Mb. My plan was to find the rate at which things increase based on inferior size, and have that tested to ensure we're not growing that requirement accidentally, but my testing actually showed memory requirements going down as the inferior increases, so instead I just hardcoded that we need less than 2Mb for the command, and it can be tweaked later if necessary. --- Linaro flagged an issue in v2, where GDB couldn't set a breakpoint in line 51 (where i was incremented). That sounds like a gcc issue, optimizing i away because it was unused, but to avoid having this come and go as GCC changes, I changed to set a bp directly on the sleep line. This should work now. --- gdb/testsuite/gdb.base/gcore-memory-usage.c | 53 ++++++++++ gdb/testsuite/gdb.base/gcore-memory-usage.exp | 96 +++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 gdb/testsuite/gdb.base/gcore-memory-usage.c create mode 100644 gdb/testsuite/gdb.base/gcore-memory-usage.exp diff --git a/gdb/testsuite/gdb.base/gcore-memory-usage.c b/gdb/testsuite/gdb.base/gcore-memory-usage.c new file mode 100644 index 00000000000..a4a197243a2 --- /dev/null +++ b/gdb/testsuite/gdb.base/gcore-memory-usage.c @@ -0,0 +1,53 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 + +int spin = 1; + +int +main (int argc, char **argv) +{ + /* Small quality of life thing for people re-running tests + manually. */ + if (argc < 2) + { + printf("Usage: %s [Megs]", argv[0]); + return 1; + } + /* Use argv to calculate how many megabytes to allocate. + Do this to see how much gcore memory usage increases + based on inferior dynamic memory. */ + int megs = atoi (argv[1]); + char *p = malloc (megs * 1024 * 1024); + + /* Setup an alarm, in case something goes wrong with testing. */ + alarm (300); + + /* Wait a long time so GDB can attach to this. + We need to have the second statement inside of the loop + to sidestep PR breakpoints/32744. */ + while (spin) + sleep (1);/* TAG: BREAK HERE */ + + /* Let's be nice citizens :). */ + free (p); + return 0; +} diff --git a/gdb/testsuite/gdb.base/gcore-memory-usage.exp b/gdb/testsuite/gdb.base/gcore-memory-usage.exp new file mode 100644 index 00000000000..1e31578d4a9 --- /dev/null +++ b/gdb/testsuite/gdb.base/gcore-memory-usage.exp @@ -0,0 +1,96 @@ +# Copyright (C) 2025 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 . + +# Test that gcore doesn't end up using excessive memory. + +require {istarget "*-*-linux*"} +require can_spawn_for_attach + +standard_testfile + +if {[build_executable "failed to prepare" $testfile $srcfile {debug}] == -1} { + return -1 +} + +# Read the proc_pid_status page, to find how much memory the given +# PID is using. This is meant to be used to find the +# memory usage for the GDB in this test. +# Returns memory usage in Kb, or -1 if it couldn't be found. +proc get_mem_usage {pid prefix} { + set fd [open "/proc/$pid/status"] + set memory -1 + while {[gets $fd line] != -1} { + if {[regexp {VmSize:\s*([0-9]+) kB} $line full mem]} { + set memory $mem + break + } + } + close $fd + + gdb_assert {$memory != -1} "$prefix: Managed to read the memory usage" + + return $memory +} + +# This proc restarts GDB, runs the inferior with the desired +# amount of memory, then checks how much memory is necessary +# to run the gcore command. It will return -1 if the gcore +# command fails, 0 otherwise. +proc run_test {megs} { + with_test_prefix "$megs Mb" { + clean_restart $::testfile + + set corefile [standard_output_file "${::testfile}.core"] + set inferior_spawn_id [spawn_wait_for_attach [list "$::binfile $megs"]] + set inferior_pid [spawn_id_get_pid $inferior_spawn_id] + set gdb_pid [exp_pid -i [board_info host fileid]] + + gdb_test "attach $inferior_pid" "Attaching to.*" + set line [gdb_get_line_number "TAG: BREAK HERE" $::testfile.c] + gdb_breakpoint "$line" "break at to known line" + gdb_continue_to_breakpoint "continue to known line" + + # Get the important info. + set mem_before [get_mem_usage $gdb_pid before] + if {![gdb_gcore_cmd $corefile "create the corefile"]} { + return -1 + } + set mem_after [get_mem_usage $gdb_pid after] + + # Do the main part of the test: How much is the memory + # usage of GDB going to grow after using the gcore command. + set diff_k [expr $mem_after - $mem_before] + set diff [expr $diff_k/1024] + verbose -log "The gcore command used $diff Mb ($diff_k Kb)" + # The original plan was to compare to a multiple of MEGS + # but since the requirements don't seem to go up as the + # inferior allocated more memory, we instead just hardcode + # 2 megs, since sometimes 1 is used. + gdb_assert {$diff < 2} "gdb did not use too much memory" + + gdb_test_no_output "set spin=0" "Allow program to exit" + } + return 0 +} + +# If we couldn't create the first corefile, there's no point +# in running the second part of the test. +if {[run_test 4] != 0} { + return +} +# Surprisingly enough, the larger inferior doesn't seem to use +# any extra memory, it usually uses less memory. Which is good, +# it means our memory requirements aren't growing with the inferior. +run_test 64 -- 2.48.1