From patchwork Thu Apr 18 06:32:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 88647 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 C19D6388187D for ; Thu, 18 Apr 2024 06:32:45 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 63B02387542F for ; Thu, 18 Apr 2024 06:32:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63B02387542F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 63B02387542F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713421942; cv=none; b=MyICP3lqR3K3iwTXMUzLaM6q4F/ptkB4XTNPeHo7OMmFkWnutDNmEeEiHT4MuSgwU0KSRUO012rGzU2g8aBxWUayDRaxo+hYjlQGaq/u9A2dOJGUh2mjDbkW1DuGnl1qDPTKxhwH0yHU8YwdImZV4jBBEkoeZz/i6Pn1pWTHyhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713421942; c=relaxed/simple; bh=xh/ZhTrRZsrH6P1zlL8G2A2sSZ68kKb94TmuRM4ERwg=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=LCwCMyaP5fJoi189SPA4cICaZg4BOVTTSowR6wRnysHPl47SsJdTLVPzazr76CNd1Ebgc4x2wuWpxOsMiq9Wb6lhOyTVDQUP3PCux0muVNEmfr4a9wj8y/XgfC1wRR94MPOnNMWgdzbyqB5hePdOzf46XV6mgUJ27fV0XWwZ5EU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id F108C34A41 for ; Thu, 18 Apr 2024 06:32:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1713421937; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=cY3WAg5ApzkKvd+PIy91uerkIk23ZhH8N223+uaQt9g=; b=XtbV+nvVAFWiPJ9yfBqE3mNmxOBQeG5yoinfdzNVJd1NmxX6ErfgFM/8hhz2BOYmUQWzgN s/Cb/r/hEcdJTgfUcVsI63kwOHTizy/G1s3dXiRTTFL3NSYnuHLq8Marhs+xilNJz9CMVS ZN1BmV0aeT0kPPpBIzsDg8a8CNNnnCQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1713421937; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=cY3WAg5ApzkKvd+PIy91uerkIk23ZhH8N223+uaQt9g=; b=N+8oETrPk7Y+7494JbgzIQlD8gd3OSEVwlS1Gd1+7mSeqOyOi/Ge9YVZCyo5JkBsI8wtkJ hTnChWPWedMSqRCA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=hlXQux50; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=dAnv3DAN DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1713421936; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=cY3WAg5ApzkKvd+PIy91uerkIk23ZhH8N223+uaQt9g=; b=hlXQux50k4tXyJgQrRBdowhLxnpTl4k8AfzVmHKbtKVO1hpqw9xHM4xXWv/tIVz/p1f1PD Ig2hjMKrnwoHn6aXT+ZYncpJXqssDpDy0IMrOPpdcvArDFLREFPf259cJin6K0azJO6W9P FIw+g6F2LjWQy+NmTc6J3Xy4WW2WYHg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1713421936; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=cY3WAg5ApzkKvd+PIy91uerkIk23ZhH8N223+uaQt9g=; b=dAnv3DAN9Flgd9WOzdI2sqsOwurewE/v4SmkNsQmQwLYXCX8ypfPwl2VIutALapAn1/QyG EXNHfUS3ItPey2Aw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id DBD8A13687 for ; Thu, 18 Apr 2024 06:32:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Qkp1NHC+IGa7dAAAD6G6ig (envelope-from ) for ; Thu, 18 Apr 2024 06:32:16 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v2] [gdb/testsuite] Handle ptrace operation not permitted in can_spawn_for_attach Date: Thu, 18 Apr 2024 08:32:37 +0200 Message-Id: <20240418063237.1545-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Level: X-Spamd-Result: default: False [-4.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; DWL_DNSWL_LOW(-1.00)[suse.de:dkim]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim]; TO_DN_NONE(0.00)[]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Action: no action X-Rspamd-Queue-Id: F108C34A41 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -4.01 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP 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 When running the testsuite on a system with kernel.yama.ptrace_scope set to 1, we run into attach failures. Fix this by recognizing "ptrace: Operation not permitted" in can_spawn_for_attach. Tested on aarch64-linux. --- gdb/testsuite/gdb.base/break-interp.exp | 4 + gdb/testsuite/gdb.base/dprintf-detach.exp | 2 +- .../run-control-while-bg-execution.exp | 10 ++- .../gdb.multi/attach-while-running.exp | 2 +- .../gdb.threads/attach-into-signal.exp | 3 +- .../gdb.threads/attach-slow-waitpid.exp | 3 +- gdb/testsuite/gdb.threads/attach-stopped.exp | 3 +- .../gdb.threads/check-libthread-db.exp | 42 ++++----- gdb/testsuite/lib/gdb.exp | 87 ++++++++++++++++--- 9 files changed, 116 insertions(+), 40 deletions(-) base-commit: ebf18671351d94185823d364b75369abc1baba31 diff --git a/gdb/testsuite/gdb.base/break-interp.exp b/gdb/testsuite/gdb.base/break-interp.exp index addacde552d..d7f84db4770 100644 --- a/gdb/testsuite/gdb.base/break-interp.exp +++ b/gdb/testsuite/gdb.base/break-interp.exp @@ -318,6 +318,10 @@ proc test_attach_gdb {file pid displacement prefix} { } proc test_attach {file displacement {relink_args ""}} { + if { ![can_spawn_for_attach] } { + return + } + global board_info global exec diff --git a/gdb/testsuite/gdb.base/dprintf-detach.exp b/gdb/testsuite/gdb.base/dprintf-detach.exp index 550d319a895..b4184d698df 100644 --- a/gdb/testsuite/gdb.base/dprintf-detach.exp +++ b/gdb/testsuite/gdb.base/dprintf-detach.exp @@ -21,7 +21,7 @@ load_lib gdbserver-support.exp # The test relies on "detach/attach". -require !use_gdb_stub +require can_spawn_for_attach standard_testfile set escapedbinfile [string_to_regexp ${binfile}] diff --git a/gdb/testsuite/gdb.base/run-control-while-bg-execution.exp b/gdb/testsuite/gdb.base/run-control-while-bg-execution.exp index f1cbd9351d3..a36c4ee3614 100644 --- a/gdb/testsuite/gdb.base/run-control-while-bg-execution.exp +++ b/gdb/testsuite/gdb.base/run-control-while-bg-execution.exp @@ -108,8 +108,14 @@ proc do_test { action1 action2 } { } } -foreach_with_prefix action1 { kill detach add none } { - foreach_with_prefix action2 { start run attach } { +set actions1 { kill detach add none } +set actions2 { start run } +if { [can_spawn_for_attach] } { + lappend actions2 attach +} + +foreach_with_prefix action1 $actions1 { + foreach_with_prefix action2 $actions2 { do_test $action1 $action2 } } diff --git a/gdb/testsuite/gdb.multi/attach-while-running.exp b/gdb/testsuite/gdb.multi/attach-while-running.exp index eade8b42a18..ca4fa635467 100644 --- a/gdb/testsuite/gdb.multi/attach-while-running.exp +++ b/gdb/testsuite/gdb.multi/attach-while-running.exp @@ -36,7 +36,7 @@ standard_testfile -require !use_gdb_stub +require can_spawn_for_attach if { [build_executable "failed to prepare" ${testfile} ${srcfile}] } { return diff --git a/gdb/testsuite/gdb.threads/attach-into-signal.exp b/gdb/testsuite/gdb.threads/attach-into-signal.exp index 87e34070548..91da960e09a 100644 --- a/gdb/testsuite/gdb.threads/attach-into-signal.exp +++ b/gdb/testsuite/gdb.threads/attach-into-signal.exp @@ -17,7 +17,8 @@ # This file was created by Jan Kratochvil . # This test only works on Linux -require !use_gdb_stub isnative +require can_spawn_for_attach +require isnative require {!is_remote host} require {istarget *-linux*} diff --git a/gdb/testsuite/gdb.threads/attach-slow-waitpid.exp b/gdb/testsuite/gdb.threads/attach-slow-waitpid.exp index dc3e62a7b7e..28d70daad8c 100644 --- a/gdb/testsuite/gdb.threads/attach-slow-waitpid.exp +++ b/gdb/testsuite/gdb.threads/attach-slow-waitpid.exp @@ -37,7 +37,8 @@ # during the attach phase. # This test only works on Linux -require !use_gdb_stub isnative +require can_spawn_for_attach +require isnative require {!is_remote host} require {istarget *-linux*} diff --git a/gdb/testsuite/gdb.threads/attach-stopped.exp b/gdb/testsuite/gdb.threads/attach-stopped.exp index 78e194c992f..0421ffc3794 100644 --- a/gdb/testsuite/gdb.threads/attach-stopped.exp +++ b/gdb/testsuite/gdb.threads/attach-stopped.exp @@ -18,7 +18,8 @@ # This file was updated by Jan Kratochvil . # This test only works on Linux -require !use_gdb_stub isnative +require can_spawn_for_attach +require isnative require {!is_remote host} require {istarget *-linux*} diff --git a/gdb/testsuite/gdb.threads/check-libthread-db.exp b/gdb/testsuite/gdb.threads/check-libthread-db.exp index 5662eeda077..6976fe6f83b 100644 --- a/gdb/testsuite/gdb.threads/check-libthread-db.exp +++ b/gdb/testsuite/gdb.threads/check-libthread-db.exp @@ -102,25 +102,27 @@ with_test_prefix "automated load-time check" { } # Automated load-time check with NPTL fully operational. - with_test_prefix "libpthread.so fully initialized" { - clean_restart ${binfile} - - gdb_test_no_output "maint set check-libthread-db 1" - gdb_test_no_output "set debug libthread-db 1" - - set test_spawn_id [spawn_wait_for_attach $binfile] - set testpid [spawn_id_get_pid $test_spawn_id] - - gdb_test_sequence "attach $testpid" \ - "check debug libthread-db output" { - "\[\r\n\]+Running libthread_db integrity checks:" - "\[\r\n\]+\[ \]+Got thread 0x\[1-9a-f\]\[0-9a-f\]+ => \[0-9\]+ => 0x\[1-9a-f\]\[0-9a-f\]+ ... OK" - "\[\r\n\]+\[ \]+Got thread 0x\[1-9a-f\]\[0-9a-f\]+ => \[0-9\]+ => 0x\[1-9a-f\]\[0-9a-f\]+ ... OK" - "\[\r\n\]+libthread_db integrity checks passed." - "\[\r\n\]+[Thread debugging using libthread_db enabled]" - } - - gdb_exit - kill_wait_spawned_process $test_spawn_id + if { [can_spawn_for_attach] } { + with_test_prefix "libpthread.so fully initialized" { + clean_restart ${binfile} + + gdb_test_no_output "maint set check-libthread-db 1" + gdb_test_no_output "set debug libthread-db 1" + + set test_spawn_id [spawn_wait_for_attach $binfile] + set testpid [spawn_id_get_pid $test_spawn_id] + + gdb_test_sequence "attach $testpid" \ + "check debug libthread-db output" { + "\[\r\n\]+Running libthread_db integrity checks:" + "\[\r\n\]+\[ \]+Got thread 0x\[1-9a-f\]\[0-9a-f\]+ => \[0-9\]+ => 0x\[1-9a-f\]\[0-9a-f\]+ ... OK" + "\[\r\n\]+\[ \]+Got thread 0x\[1-9a-f\]\[0-9a-f\]+ => \[0-9\]+ => 0x\[1-9a-f\]\[0-9a-f\]+ ... OK" + "\[\r\n\]+libthread_db integrity checks passed." + "\[\r\n\]+[Thread debugging using libthread_db enabled]" + } + + gdb_exit + kill_wait_spawned_process $test_spawn_id + } } } diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index d48ea37c0cc..937add7a272 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -6131,6 +6131,43 @@ proc gdb_exit { } { catch default_gdb_exit } +# Helper function for can_spawn_for_attach. Try to spawn and attach, and +# return 0 only if we cannot attach because it's unsupported. + +gdb_caching_proc can_spawn_for_attach_1 {} { + # Assume yes. + set res 1 + + set me "can_spawn_for_attach" + set src { int main (void) { sleep (600); return 0; } } + if {![gdb_simple_compile $me $src executable]} { + return $res + } + + set test_spawn_id [spawn_wait_for_attach_1 $obj] + file delete $obj + + gdb_start + + set test_pid [spawn_id_get_pid $test_spawn_id] + set attaching_re "Attaching to process $test_pid" + gdb_test_multiple "attach $test_pid" "can spawn for attach" { + -re -wrap "$attaching_re\r\n.*ptrace: Operation not permitted\\." { + # Not permitted. + set res 0 + } + -re -wrap "" { + # Don't know. + } + } + + gdb_exit + + kill_wait_spawned_process $test_spawn_id + + return $res +} + # Return true if we can spawn a program on the target and attach to # it. @@ -6151,8 +6188,25 @@ proc can_spawn_for_attach { } { return 0 } - # Assume yes. - return 1 + # By making can_spawn_for_attach_1 a gdb_caching_proc, we make it + # unpredictable which test-case will call it first, and consequently a + # test-case may pass in say a full test run, but fail when run + # individually, due to a can_spawn_for_attach call in location where a + # gdb_exit (as can_spawn_for_attach_1 does) breaks things. + # To avoid this, we move the initial gdb_exit out of + # can_spawn_for_attach_1, guaranteeing that we end up in the same state + # regardless of whether can_spawn_for_attach_1 is called. However, that + # is only necessary for the first call in a test-case, so cache the result + # in a global (which should be reset after each test-case) to keep track + # of that. + global cache_can_spawn_for_attach_1 + if { [info exists cache_can_spawn_for_attach_1] } { + return $cache_can_spawn_for_attach_1 + } + gdb_exit + + set cache_can_spawn_for_attach_1 [can_spawn_for_attach_1] + return $cache_can_spawn_for_attach_1 } # Centralize the failure checking of "attach" command. @@ -6265,20 +6319,12 @@ proc spawn_id_get_pid { spawn_id } { return $testpid } -# Start a set of programs running and then wait for a bit, to be sure -# that they can be attached to. Return a list of processes spawn IDs, -# one element for each process spawned. It's a test error to call -# this when [can_spawn_for_attach] is false. +# Helper function for spawn_wait_for_attach. As spawn_wait_for_attach, but +# doesn't check for can_spawn_for_attach. -proc spawn_wait_for_attach { executable_list } { +proc spawn_wait_for_attach_1 { executable_list } { set spawn_id_list {} - if ![can_spawn_for_attach] { - # The caller should have checked can_spawn_for_attach itself - # before getting here. - error "can't spawn for attach with this target/board" - } - foreach {executable} $executable_list { # Note we use Expect's spawn, not Tcl's exec, because with # spawn we control when to wait for/reap the process. That @@ -6292,6 +6338,21 @@ proc spawn_wait_for_attach { executable_list } { return $spawn_id_list } +# Start a set of programs running and then wait for a bit, to be sure +# that they can be attached to. Return a list of processes spawn IDs, +# one element for each process spawned. It's a test error to call +# this when [can_spawn_for_attach] is false. + +proc spawn_wait_for_attach { executable_list } { + if ![can_spawn_for_attach] { + # The caller should have checked can_spawn_for_attach itself + # before getting here. + error "can't spawn for attach with this target/board" + } + + return [spawn_wait_for_attach_1 $executable_list] +} + # # gdb_load_cmd -- load a file into the debugger. # ARGS - additional args to load command.