From b750766ac9652def5307925b8fc5c215fbcef8df Mon Sep 17 00:00:00 2001 From: Lancelot SIX Date: Thu, 14 Apr 2022 10:41:48 +0100 Subject: [PATCH] gdb/testsuite: Introduce and use gdb_spawn_attach_cmdline Following a7e6a19e87f3d719ea23c65b580a6d9bca4ccab3 "gdb: testsuite: add new gdb_attach to check "attach" command", this commit proposes to introduce the gdb_spawn_attach_cmdline helper and use it in gdb.base/attach.exp. This helper starts GDB and adds the "--pid=$PID" argument. Also note that both the original and new implementation use gdb_spawn_with_cmdline_opts, which in the end uses default_gdb_spawn. This makes sure that we use $INTERNAL_GDBFLAGS, which by default already contain "-iex \"set height 0\" -iex \"set width 0\"". To avoid repetition of those arguments, gdb_spawn_attach_cmdline does not repeat those arguments. To maintain a behavior similat to what gdb.base/attach.exp used to do, gdb_spawn_attach_cmdline keeps the -quiet flag. Tested on x86_64-gnu-linux Change-Id: I1fdcdb71c86d9c5d34bb28fc86fac68bcec37358 --- gdb/testsuite/gdb.base/attach.exp | 11 ++------ gdb/testsuite/lib/gdb.exp | 46 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/gdb/testsuite/gdb.base/attach.exp b/gdb/testsuite/gdb.base/attach.exp index d01060aba53..7b661e99a1c 100644 --- a/gdb/testsuite/gdb.base/attach.exp +++ b/gdb/testsuite/gdb.base/attach.exp @@ -467,14 +467,9 @@ proc_with_prefix do_command_attach_tests {} { gdb_exit - set res [gdb_spawn_with_cmdline_opts \ - "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid"] - set test "starting with --pid" - gdb_test_multiple "" $test { - -re "Reading symbols from.*$gdb_prompt $" { - pass "$test" - } - } + # gdb_spawn_attach_cmdline records test results. No need to explicitly + # call pass/fail here. + gdb_spawn_attach_cmdline $testpid # Get rid of the process kill_wait_spawned_process $test_spawn_id diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 10f78736629..0c00d599ca5 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -5186,6 +5186,52 @@ proc gdb_attach { testpid args } { return 0 } +# Start gdb with "--pid $TESTPID" on the command line and wait for the prompt. +# Return 1 if GDB managed to start and attach to the process, 0 otherwise. + +proc_with_prefix gdb_spawn_attach_cmdline { testpid } { + 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" + } + + set test "start gdb with --pid" + set res [gdb_spawn_with_cmdline_opts "-quiet --pid=$testpid"] + if { $res != 0 } { + fail $test + return 0 + } + + gdb_test_multiple "" "$test" { + -re -wrap "ptrace: Operation not permitted\\." { + untested "$gdb_test_name (operation not permitted)" + return 0 + } + -re -wrap "ptrace: No such process\\." { + fail "$gdb_test_name (no such process)" + return 0 + } + -re -wrap "Attaching to process $testpid\r\n.*" { + pass $gdb_test_name + } + } + + # Check that we actually attached to a process, in case the + # error message is not caught by the patterns above. + gdb_test_multiple "info thread" "" { + -re -wrap "No threads\\." { + fail "$gdb_test_name (no thread)" + } + -re -wrap "Id.*" { + pass $gdb_test_name + return 1 + } + } + + return 0 +} + # Kill a progress previously started with spawn_wait_for_attach, and # reap its wait status. PROC_SPAWN_ID is the spawn id associated with # the process. -- 2.30.2