From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail0.khirnov.net (mail0.khirnov.net [IPv6:2001:67c:1138:4304::3]) by sourceware.org (Postfix) with ESMTPS id 4DBBD387544E for ; Wed, 12 Jun 2024 10:53:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DBBD387544E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=khirnov.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=khirnov.net ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4DBBD387544E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:1138:4304::3 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718189598; cv=none; b=a5wTaOyHFCw0KFNfBBsiQ7LUdOvlJrSnVMCjsjBaVmaesDufOjAZ+ER+Sd7PU1ir50ZISqqgIwKL0MI8RVA0a12vhvRg/KnduRKbsqVw9KesZPaQB9tF8YNTKb417Z41AUY4bC8GKvAUApBW1M9+yvt1G+DFgRfLnri6DN98YGg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718189598; c=relaxed/simple; bh=iNyIjjuTbuQF+H60JcG7hH6PAa4cl+S+d6lqqAB+DX8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LFKepiCuG7/1cgt/AG5uCplALFbcQ/13wA8NgVfiPU9WlmxPC4f6iCUaiQiYc5KY7PctROraBoD7s1KHYms9ZpqXDIjmQz9fC2IsbiEhP6jQnouu71gEFwGy3555ZfXXQnSc+qyo0/sRC/yyUfle4BHaSN2wiCbrRMNHczoATfk= ARC-Authentication-Results: i=1; server2.sourceware.org Authentication-Results: mail0.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=IVWAjCTf; dkim-atps=neutral Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9D7FF240DB4; Wed, 12 Jun 2024 12:53:12 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id kKYx_B6ETNeY; Wed, 12 Jun 2024 12:53:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1718189590; bh=iNyIjjuTbuQF+H60JcG7hH6PAa4cl+S+d6lqqAB+DX8=; h=From:To:Cc:Subject:Date:From; b=IVWAjCTfjgsx/5XjK987TSvPe+cAVFz73TTthdgLSobZd3gtIlC1Td9zL83hzYoXN 1Di9kXdOVfEelZaE5DNDwAZj9ckyyAcAY/opWOyL49A6AIrqJhOIaDwbkLJikuxm2i b63yUukYwxLg8sWyUdggWuWyMZtS2DJsXCUZf6H4Q9HE+PPvoHzY5DWcvHJdkRoseq pDIIDYRikzJjdyTaBNeEv8C29vqR3ndOcEvyE/zMUE23+YMkVdG1MLPvJ9/m96JmUn F481XkVppT0a0ulM69nzI3wFeblUP62FJPiSyEr0Xy8If5EryUcufdog0GTngQP9WZ DJzwpBS32XHGA== Received: from fedora.khirnov.net (ip-244-183.pel.cz [IPv6:2002:b061:f0a:201::d5e2:f4b7]) (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 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "sasshka SMTP cert", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id ED0E52404E5; Wed, 12 Jun 2024 12:53:09 +0200 (CEST) Received: from fedora.redhat.com (localhost [127.0.0.1]) by fedora.khirnov.net (Postfix) with ESMTP id D359296A8A9; Wed, 12 Jun 2024 12:53:06 +0200 (CEST) From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com, Jan Kratochvil Subject: [PATCH v7] Add a test for the gcore script Date: Wed, 12 Jun 2024 12:53:03 +0200 Message-ID: <20240612105305.253857-1-ahajkova@khirnov.net> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_SHORT,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Alexandra Hájková It also tests the gcore script being run without its accessible terminal. This test was written by Jan Kratochvil a long time ago. I modernized the test making it use various procs from lib/gdb.exp, reorganizing it and added some comments. Modify the gcore script to make it possible to pass the --data-directory to it. This prevents a lot of these warnings: Python Exception : module 'gdb' has no attribute '_handle_missing_debuginfo' Tested by using make check-all-boards. Co-Authored-By: Jan Kratochvil --- v7: - use require can_spawn_for_attach to prevent the test from failing in a case of `sysctl kernel.yama.ptrace_scope=1` - wait for the child to exit with waitpid() instead of creating the pipe - use foreach_with_prefix gdb/doc/gdb.texinfo | 2 +- gdb/gcore.in | 12 +++-- gdb/testsuite/gdb.base/gcorebg.c | 83 +++++++++++++++++++++++++++++ gdb/testsuite/gdb.base/gcorebg.exp | 84 ++++++++++++++++++++++++++++++ gdb/testsuite/lib/gdb.exp | 18 +++++++ 5 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 gdb/testsuite/gdb.base/gcorebg.c create mode 100644 gdb/testsuite/gdb.base/gcorebg.exp diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 750f368f980..67edfdaf800 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -50984,7 +50984,7 @@ Richard M. Stallman and Roland H. Pesch, July 1991. @format @c man begin SYNOPSIS gcore -gcore [-a] [-o @var{prefix}] @var{pid1} [@var{pid2}...@var{pidN}] +gcore [-a] [-o @var{prefix}] [-d @var{path}] @var{pid1} [@var{pid2}...@var{pidN}] @c man end @end format diff --git a/gdb/gcore.in b/gdb/gcore.in index 982c854eb70..0c40e5a54cd 100644 --- a/gdb/gcore.in +++ b/gdb/gcore.in @@ -27,7 +27,9 @@ prefix=core # to ensure gdb dumps all mappings (OS dependent). dump_all_cmds=() -while getopts :ao: opt; do +data_directory_opt=() + +while getopts :ao:d: opt; do case "$opt" in a) case "$OSTYPE" in @@ -40,8 +42,11 @@ while getopts :ao: opt; do o) prefix=$OPTARG ;; + d) + data_directory_opt=("--data-directory" "$OPTARG") + ;; *) - echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]" + echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] [-d data-directory] pid1 [pid2...pidN]" exit 2 ;; esac @@ -51,7 +56,7 @@ shift $((OPTIND-1)) if [ "$#" -eq "0" ] then - echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]" + echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] [-d data-directory] pid1 [pid2...pidN]" exit 2 fi @@ -98,6 +103,7 @@ do # `. */ + +#include +#include +#include +#include +#include +#include +#include + +/* Expects 4 arguments: + + 1. Either 'standard' or 'detached', where 'standard' tests + a general gcore script spawn with its controlling terminal available + and 'detached' tests gcore script spawn without its controlling + terminal available. + 2. Path to the gcore script. + 3. Path to the data-directory to pass to the gcore script. + 4. The core file output name. */ + +int +main (int argc, char **argv) +{ + pid_t pid = 0; + pid_t ppid; + char buf[1024*2 + 500]; + int gotint, res; + int fd[2]; + + assert (argc == 5); + + if (pipe(fd) == -1) + { + perror ("pipe err\n"); + exit (1); + } + pid = fork (); + + switch (pid) + { + case 0: + close (fd[0]); + if (strcmp (argv[1], "detached") == 0) + setpgrp (); + ppid = getppid (); + gotint = snprintf (buf, sizeof (buf), "%s -d %s -o %s %d", + argv[2], argv[3], argv[4], (int) ppid); + assert (gotint < sizeof (buf)); + res = system (buf); + assert (res != -1); + break; + + case -1: + perror ("fork err\n"); + exit (1); + break; + + default: + do + { + res = waitpid (pid, NULL, 0); + } + while (res == -1 && errno == EINTR); + + assert (res == pid); + break; + } + + return 0; +} diff --git a/gdb/testsuite/gdb.base/gcorebg.exp b/gdb/testsuite/gdb.base/gcorebg.exp new file mode 100644 index 00000000000..64a10b492b1 --- /dev/null +++ b/gdb/testsuite/gdb.base/gcorebg.exp @@ -0,0 +1,84 @@ +# Copyright 2007-2024 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 . +# +# This is a test for the gcore script (not the gcore command from +# inside GDB). It also tests the gcore script being run without its +# accessible terminal. + +standard_testfile +require {!is_remote host} +require {!is_remote target} +require has_gcore_script +require can_spawn_for_attach + +set corefile [standard_output_file ${testfile}.core] + +if {[build_executable "failed to build" $testfile ${srcfile}] == -1 } { + return -1 +} + +# Cleanup. + +proc core_clean {} { + global corefile + + foreach file [glob -nocomplain [join [list $corefile *] ""]] { + verbose "Delete file $file" 1 + remote_file target delete $file + } +} +core_clean + +# Generate the core file. +proc test_body { detached } { + global binfile + global GCORE + global corefile + global GDB_DATA_DIRECTORY + + with_test_prefix "detached = $detached" { + # We can't use gdb_test_multiple here because GDB is not started. + set res [remote_spawn target "$binfile $detached $GCORE $GDB_DATA_DIRECTORY $corefile"] + if { $res < 0 || $res == "" } { + fail "Spawning gcore" + return 1 + } + pass "Spawned gcore" + + set saw_corefile_created false + set testname "Spawned gcore finished" + remote_expect target 20 { + timeout { + fail "$testname (timeout)" + remote_exec target "kill -9 -[exp_pid -i $res]" + return + } + -re "Saved corefile \[^\r\n\]+\r\n" { + set saw_corefile_created true + exp_continue + } + eof { + gdb_assert { $saw_corefile_created } $testname + } + } + + gdb_assert {1 == [llength [glob -nocomplain [join [list $corefile *] ""]]]} "Core file generated by gcore" + core_clean + } +} + +foreach_with_prefix detached { detached standard } { + test_body $detached +} diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 590cd458374..5d1920022fd 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -158,6 +158,24 @@ load_lib gdb-utils.exp load_lib memory.exp load_lib check-test-names.exp +# The path to the GCORE script to test. +global GCORE +if {![info exists GCORE]} { + set GCORE [findfile $base_dir/../../gdb/gcore] +} else { + set GCORE "" +} +verbose "using GCORE = $GCORE" 2 + +proc has_gcore_script {} { + global GCORE + if {$GCORE == ""} { + return 0 + } else { + return 1 + } +} + # The path to the GDB binary to test. global GDB -- 2.45.0