From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id nMOlLKZ8vGcYIDwAWB0awg (envelope-from ) for ; Mon, 24 Feb 2025 09:05:26 -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=JY7mcGWI; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id A73E51E105; Mon, 24 Feb 2025 09:05:26 -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 8D46E1E05C for ; Mon, 24 Feb 2025 09:05:24 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 162D93858D35 for ; Mon, 24 Feb 2025 14:05:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 162D93858D35 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=JY7mcGWI 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 6E9043858D26 for ; Mon, 24 Feb 2025 14:04:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E9043858D26 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 6E9043858D26 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=1740405887; cv=none; b=IiHVzjaCwh9MBVJfORKdPmKewa9C/qNH4MogCkxH0Lz4T1KSEk8usv3IK+X0/Z4FApAzSCyic1V3qRr7IaNjCcNewCzXBMCiHIfxT72AJgUnpDdJ5mf8NLGeHOZcjiwXA4x6+qE7jQsSHGwKBEbY4DMmevv6Ty2tkagRZJKZCnI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740405887; c=relaxed/simple; bh=gLQUcu6wKCG5v0xAlr45U2hLomHVwwWV2N2CfREfosw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DDH+AT3oEUnstMk+FiyCkp/moVYkafe0fCmQPCIuwhznmoPlt+v87FJjgzBD0sj1vnlqdOaE2Mo7+MHf63hoReyZCvMIL4ZIQeJSD0JPH21vEY87ZFrfIza6S+d2iniGwNBughkSstv8wb7pQoVwCA4Gr7NhStGA3wiW6BIqBtc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E9043858D26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740405887; 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; bh=4SlZMQtMgdoH9FlBhgFdCYF3uSRA9FsAao8ZNULduG4=; b=JY7mcGWI1//p/8Mgvu/1iyA7ggDs+EX3wp1Zxn2yF2ssnBmOK54SaEVDb0GPKwUsIRVSHR FEV1s3KybEQIYMII9Yq0gm0mp3yI+ks2kKrOUB5pttfHVCaF0K+KfiW4psKM2CbR4x6av7 HdGDh5qw50zZ8pan8nt8zXFi8pI0pfs= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-513-XHJS9ZUDPV6YaU-Xw8sCwQ-1; Mon, 24 Feb 2025 09:04:45 -0500 X-MC-Unique: XHJS9ZUDPV6YaU-Xw8sCwQ-1 X-Mimecast-MFC-AGG-ID: XHJS9ZUDPV6YaU-Xw8sCwQ_1740405885 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D49421800874 for ; Mon, 24 Feb 2025 14:04:44 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.96.134.132]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 91DC4300018D; Mon, 24 Feb 2025 14:04:43 +0000 (UTC) From: Guinevere Larsen To: gdb-patches@sourceware.org Cc: Guinevere Larsen Subject: [PATCH] gdb/testsuite: add test for memory requirements of gcore Date: Mon, 24 Feb 2025 11:04:07 -0300 Message-ID: <20250224140407.252987-1-guinevere@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 36tJj6fOJLPXWXnOxEm5TJ68v90jl_XzzZGp_wgCTag_1740405885 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. --- gdb/testsuite/gdb.base/gcore-memory-usage.c | 37 +++++++ gdb/testsuite/gdb.base/gcore-memory-usage.exp | 97 +++++++++++++++++++ 2 files changed, 134 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..514fdb905cf --- /dev/null +++ b/gdb/testsuite/gdb.base/gcore-memory-usage.c @@ -0,0 +1,37 @@ +/* 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 main(int argc, char **argv) +{ + /* 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); + + /* Wait a long time so GDB can attach to this. */ + sleep (10); + + /* 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..cdb6d73c11b --- /dev/null +++ b/gdb/testsuite/gdb.base/gcore-memory-usage.exp @@ -0,0 +1,97 @@ +# 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*"} + +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 bin [standard_output_file $::testfile] + set corefile [standard_output_file "${::testfile}.core"] + set inferior_pid [eval exec $bin $megs &] + set gdb_pid [exp_pid -i [board_info host fileid]] + + # wait for memory allocation to finish. + sleep 1 + + # Get the important info. + gdb_test "attach $inferior_pid" "Attaching to.*" + 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 as much memory as the inferior" + + # Kill the inferior so we don't have to wait until the process + # finishes. + gdb_test "signal SIGKILL" ".*The program no longer exists." + } + 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