gdb: testsuite: fix wrong expected result in attach-pie-noexec.exp
authorTiezhu Yang <yangtiezhu@loongson.cn>
Tue, 1 Mar 2022 07:05:00 +0000 (15:05 +0800)
committerTiezhu Yang <yangtiezhu@loongson.cn>
Tue, 1 Mar 2022 07:21:16 +0000 (15:21 +0800)
If /proc/sys/kernel/yama/ptrace_scope is 1, when execute the test case
gdb.base/attach-pie-noexec.exp without superuser, the gdb.log shows the
following info:

  (gdb) attach 6500
  Attaching to process 6500
  ptrace: Operation not permitted.
  (gdb) PASS: gdb.base/attach-pie-noexec.exp: attach

It is obviously wrong, the expected result should be UNSUPPORTED in such
a case.

It is better to make can_spawn_for_attach to return false for this case.
It would have to setup a small test program, compile it to exec, spawn it
and try to attach to it.

With this patch, we can see "Operation not permitted" in the log info,
and then we can do the following processes to test:
(1) set ptrace_scope as 0
    $ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    $ make check-gdb TESTS="gdb.base/attach-pie-noexec.exp"
(2) use sudo
    $ sudo make check-gdb TESTS="gdb.base/attach-pie-noexec.exp"

Additionally, handle the other cases when test with RUNTESTFLAGS=
"--target_board=native-extended-gdbserver".

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
gdb/testsuite/lib/gdb.exp

index 8fafcc03a1828d0654cdba46e9ed8203aeae5fee..848cd0800001129a9d5942745d184898b075b699 100644 (file)
@@ -5081,7 +5081,7 @@ proc gdb_exit { } {
 # Return true if we can spawn a program on the target and attach to
 # it.
 
-proc can_spawn_for_attach { } {
+gdb_caching_proc can_spawn_for_attach {
     # We use exp_pid to get the inferior's pid, assuming that gives
     # back the pid of the program.  On remote boards, that would give
     # us instead the PID of e.g., the ssh client, etc.
@@ -5098,6 +5098,42 @@ proc can_spawn_for_attach { } {
        return 0
     }
 
+    set me "can_spawn_for_attach"
+    set src { int main (void) { sleep (600); return 0; } }
+    if {![gdb_simple_compile $me $src executable]} {
+        return 0
+    }
+
+    set test_spawn_id [spawn_wait_for_attach $obj]
+    set test_pid [spawn_id_get_pid $test_spawn_id]
+
+    gdb_start
+    file delete $obj
+    gdb_test_multiple "attach $test_pid" "can spawn for attach" {
+        -re -wrap "Attaching to process $test_pid\r\n.*No executable file now.*" {
+          pass $gdb_test_name
+          kill_wait_spawned_process $test_spawn_id
+          return 1
+        }
+        -re -wrap "Attaching to process $test_pid\r\n.*ptrace: Operation not permitted\\." {
+          unsupported "$gdb_test_name (Operation not permitted)"
+          kill_wait_spawned_process $test_spawn_id
+          return 0
+        }
+        -re -wrap "Attaching to process $test_pid\r\n.*Attaching to process $test_pid failed" {
+          unsupported "$gdb_test_name (Attaching to process failed)"
+          kill_wait_spawned_process $test_spawn_id
+          return 0
+        }
+        -re -wrap "Attaching to process $test_pid\r\n.*XML support was disabled at compile time.*" {
+          pass $gdb_test_name
+          kill_wait_spawned_process $test_spawn_id
+          return 1
+        }
+    }
+
+    kill_wait_spawned_process $test_spawn_id
+
     # Assume yes.
     return 1
 }
@@ -5147,12 +5183,6 @@ proc spawn_id_get_pid { spawn_id } {
 proc spawn_wait_for_attach { 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