From patchwork Wed Feb 21 15:01:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 86172 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 502F93858417 for ; Wed, 21 Feb 2024 15:05:16 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B881D3858CDA for ; Wed, 21 Feb 2024 15:04:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B881D3858CDA 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 B881D3858CDA 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=1708527887; cv=none; b=UGUUVuzomKatTm6j0eUVRhlESKnjIFjS9wmdSDYWqfhf6nD1Bznn1mxFVPYapjsoclecb9r2FunOu1/AdE3CMvdhvfDZqixY+3VhaevrkmUdctIbb8KuZ/iB+tX0MKGaXdPYLni3gsRz3fFNcBnuZIve6KVppDB9waB3TSN82iw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708527887; c=relaxed/simple; bh=vY/JSXM42lze3NtkxR0EENE8POvWIfR2FWCmbHCzgGc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ChS24/HJ98YtwaLkJnE8g/ZVRz3vxgV3CYUi6U6ozCt//cRTUGyzDSjZ6oKLKekScC7Do2q95wQ5LUESlK8LQ+oj2iQykKTtCDV2wXUEQr5vO6+f/GCXFa7cIUCjpGqCd/QiL7wsAGbMl1ElMzUJ6S1nvazGDcSXYl6p6tC5QLM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708527876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Y/EJfVyPmHGtv6zXuazwN4Xrf8pmmE45uh9v0AQRza4=; b=EP2EIErK4hIGDbbohatBeNySYYpbAckPtpcaTu0K67XW3g2Mj+s7PFH7fXr0LCS/RmOmLL 7nFuxPPGm/SfG39fJcFEB/VFIUW/oHxKiGyRrsWbn/X9d1Xkx70WxqZg9AJjlkrFM3Xp0+ D44cgvsYviITLoOCAV9SAYLG4Cmpvy8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-461-0hPW-9ZmMZ2ap4qv9VzFDA-1; Wed, 21 Feb 2024 10:04:29 -0500 X-MC-Unique: 0hPW-9ZmMZ2ap4qv9VzFDA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 49BAF88B7A3 for ; Wed, 21 Feb 2024 15:04:29 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 20592492BCA for ; Wed, 21 Feb 2024 15:04:18 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2] Add a test for the gcore script Date: Wed, 21 Feb 2024 16:01:26 +0100 Message-ID: <20240221150402.1375797-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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 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+patchwork=sourceware.org@sourceware.org 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, reoirganising it and added some comments. Tested by using make check-all-boards. Co-Authored-By: Jan Kratochvil --- v2: - cosmetics - Added Jan Kratochvil as the Co-Author - added asserts - added more comments - removed unnecessary code gdb/testsuite/gdb.base/gcorebg.c | 68 +++++++++++++++++++++++++ gdb/testsuite/gdb.base/gcorebg.exp | 81 ++++++++++++++++++++++++++++++ gdb/testsuite/lib/gdb.exp | 17 +++++++ 3 files changed, 166 insertions(+) create mode 100644 gdb/testsuite/gdb.base/gcorebg.c create mode 100644 gdb/testsuite/gdb.base/gcorebg.exp diff --git a/gdb/testsuite/gdb.base/gcorebg.c b/gdb/testsuite/gdb.base/gcorebg.c new file mode 100644 index 00000000000..0d5168c63ad --- /dev/null +++ b/gdb/testsuite/gdb.base/gcorebg.c @@ -0,0 +1,68 @@ +/* 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 . */ + +#include +#include +#include +#include +#include +#include +#include + +int +main (int argc, char **argv) +{ + pid_t pid = 0; + pid_t ppid; + char buf[1024*2 + 500]; + int gotint, res; + + if (argc != 4) + { + fprintf (stderr, "Syntax: %s {standard|detached} \n", + argv[0]); + exit (1); + } + + pid = fork (); + + switch (pid) + { + case 0: + if (strcmp (argv[1], "detached") == 0) + setpgrp (); + ppid = getppid (); + gotint = snprintf (buf, sizeof (buf), "sh %s -o %s %d", argv[2], argv[3], (int) ppid); + assert (gotint < sizeof (buf)); + res = system (buf); + assert (res != -1); + fprintf (stderr, "Killing parent PID %d\n", ppid); + kill (ppid, SIGTERM); + break; + + case -1: + perror ("fork err\n"); + exit (1); + break; + + default: + fprintf (stderr, "Sleeping as PID %d\n", getpid ()); + /* Wait here until the child is done with gcore-ing us. If things go right, + the child kills us with SIGTERM before this sleep returns. */ + sleep (60); + } + + return 0; +} diff --git a/gdb/testsuite/gdb.base/gcorebg.exp b/gdb/testsuite/gdb.base/gcorebg.exp new file mode 100644 index 00000000000..bcfbe994c9e --- /dev/null +++ b/gdb/testsuite/gdb.base/gcorebg.exp @@ -0,0 +1,81 @@ +# 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 form +# 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 + +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 + + 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 $corefile"] + if { $res < 0 || $res == "" } { + fail "Spawning gcore" + return 1 + } + pass "Spawned gcore" + + remote_expect target 20 { + timeout { + fail "Spawned gcore finished (timeout)" + remote_exec target "kill -9 -[exp_pid -i $res]" + return 1 + } + "Saved corefile .*\r\nKilling parent PID " { + pass "Spawned gcore finished" + } + } + + gdb_assert {1 == [llength [glob -nocomplain [join [list $corefile *] ""]]]} "Core file generated by gcore" + core_clean + } +} + +# First a general gcore script spawn with its controlling terminal available. + +test_body standard + +# And now gcore script spawn without its controlling terminal available. +# It is spawned through `gcorebg.c' using setpgrp (). + +test_body detached diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 9a906f0f349..7943fcbbe74 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -146,6 +146,23 @@ 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/../../gcore] +} else { + set GCORE "" +} +verbose "using GCORE = $GCORE" 2 + +proc has_gcore_script {} { + if { $::GCORE != "" } { + return 1 + } else { + return 0 + } +} + # The path to the GDB binary to test. global GDB