Update copyright year range in all GDB files.
[external/binutils.git] / gdb / testsuite / gdb.base / attach.exp
index bc7d4eb..2343354 100644 (file)
@@ -1,5 +1,4 @@
-# Copyright 1997, 1999, 2002, 2003, 2004, 2007, 2008, 2009, 2010
-# Free Software Foundation, Inc.
+# Copyright 1997-2019 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
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-if $tracelevel then {
-    strace $tracelevel
-}
-
-
-# On HP-UX 11.0, this test is causing a process running the program
-# "attach" to be left around spinning.  Until we figure out why, I am
-# commenting out the test to avoid polluting tiamat (our 11.0 nightly
-# test machine) with these processes. RT
-#
-# Setting the magic bit in the target app should work.  I added a
-# "kill", and also a test for the R3 register warning.  JB
-if { [istarget "hppa*-*-hpux*"] } {
+if {![can_spawn_for_attach]} {
     return 0
 }
 
-# are we on a target board
-if [is_remote target] then {
-    return 0
-}
-
-set testfile "attach"
-set srcfile  ${testfile}.c
-set srcfile2 ${testfile}2.c
-set binfile  ${objdir}/${subdir}/${testfile}
-set binfile2 ${objdir}/${subdir}/${testfile}2
-set escapedbinfile  [string_to_regexp ${objdir}/${subdir}/${testfile}]
+standard_testfile attach.c attach2.c
+set binfile2 ${binfile}2
+set escapedbinfile  [string_to_regexp $binfile]
 
 #execute_anywhere "rm -f ${binfile} ${binfile2}"
 remote_exec build "rm -f ${binfile} ${binfile2}"
@@ -51,18 +30,18 @@ remote_exec build "rm -f ${binfile} ${binfile2}"
 # build the first test case
 #
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
-    untested attach.exp
+    untested "failed to compile"
     return -1
 }
 
 # Build the in-system-call test
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
-    untested attach.exp
+    untested "failed to compile in-system-call test"
     return -1
 }
 
-if [get_compiler_info ${binfile}] {
+if [get_compiler_info] {
     return -1
 }
 
@@ -72,20 +51,25 @@ proc do_attach_tests {} {
     global escapedbinfile
     global srcfile
     global testfile
-    global objdir
     global subdir
     global timeout
+    global decimal
     
+    # Figure out a regular expression that will match the sysroot,
+    # noting that the default sysroot is "target:", and also noting
+    # that GDB will strip "target:" from the start of filenames when
+    # operating on the local filesystem.  However the default sysroot
+    # can be set via configure option --with-sysroot, which can be "/".
+    # If $binfile is a absolute path, so pattern
+    # "$sysroot$escapedbinfile" below is wrong.  Use [^\r\n]* to make
+    # $sysroot simple.
+    set sysroot "\[^\r\n\]*"
+
     # Start the program running and then wait for a bit, to be sure
     # that it can be attached to.
 
-    set testpid [eval exec $binfile &]
-    exec sleep 2
-    if { [istarget "*-*-cygwin*"] } {
-       # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
-       # different due to the way fork/exec works.
-       set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
-    }
+    set test_spawn_id [spawn_wait_for_attach $binfile]
+    set testpid [spawn_id_get_pid $test_spawn_id]
 
     # Verify that we cannot attach to nonsense.
 
@@ -164,6 +148,10 @@ proc do_attach_tests {} {
            # Response expected on Cygwin
            pass "$test"
        }
+       -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
+           # Response expected on the extended-remote target.
+           pass "$test"
+       }
     }
     
     # Verify that we can attach to the process by first giving its
@@ -177,10 +165,10 @@ proc do_attach_tests {} {
     set test "set file, before attach1"
     gdb_test_multiple "file $binfile" "$test" {
        -re "Load new symbol table from.*y or n. $" {
-           gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
+           gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
                "$test (re-read)"
        }
-       -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
+       -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
            pass "$test"
        }
     }
@@ -204,7 +192,7 @@ proc do_attach_tests {} {
     # Detach the process.
    
     gdb_test "detach" \
-       "Detaching from program: .*$escapedbinfile, process $testpid" \
+       "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
        "attach1 detach"
 
     # Wait a bit for gdb to finish detaching
@@ -228,18 +216,35 @@ proc do_attach_tests {} {
     # Verify that we can attach to the process just by giving the
     # process ID.
    
-    set test "set file, before attach2"
+    set test "attach2, with no file"
+    set found_exec_file 0
     gdb_test_multiple "attach $testpid" "$test" {
-       -re "Attaching to process $testpid.*Load new symbol table from \"$escapedbinfile\.exe\".*y or n. $" {
+       -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
            # On Cygwin, the DLL's symbol tables are loaded prior to the
            # executable's symbol table.  This in turn always results in
            # asking the user for actually loading the symbol table of the
            # executable.
-           gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
+           gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \
                "$test (reset file)"
+
+           set found_exec_file 1
        }
-       -re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
+       -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
            pass "$test"
+           set found_exec_file 1
+       }
+    }
+
+    if {$found_exec_file == 0} {
+       set test "load file manually, after attach2"
+       gdb_test_multiple "file $binfile" "$test" {
+           -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
+               gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \
+                   "$test (re-read)"
+           }
+           -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" {
+               pass "$test"
+           }
        }
     }
 
@@ -250,39 +255,28 @@ proc do_attach_tests {} {
 
     # Verify that the modification really happened.
 
-    gdb_test "tbreak 19" "Temporary breakpoint .*at.*$srcfile, line 19.*" \
-       "after attach2, set tbreak postloop"
-
-    gdb_test "continue" "main.*at.*$srcfile:19.*" \
-       "after attach2, reach tbreak postloop"
+    gdb_breakpoint [gdb_get_line_number "postloop"] temporary
+    gdb_continue_to_breakpoint "postloop" ".* postloop .*"
 
     # Allow the test process to exit, to cleanup after ourselves.
 
-    gdb_test "continue" "Program exited normally." "after attach2, exit"
+    gdb_continue_to_end "after attach2, exit"
 
     # Make sure we don't leave a process around to confuse
     # the next test run (and prevent the compile by keeping
     # the text file busy), in case the "set should_exit" didn't
     # work.
-   
-    remote_exec build "kill -9 ${testpid}"
 
-    # Start the program running and then wait for a bit, to be sure
-    # that it can be attached to.
-   
-    set testpid [eval exec $binfile &]
-    exec sleep 2
-    if { [istarget "*-*-cygwin*"] } {
-       # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
-       # different due to the way fork/exec works.
-       set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
-    }
+    kill_wait_spawned_process $test_spawn_id
+
+    set test_spawn_id [spawn_wait_for_attach $binfile]
+    set testpid [spawn_id_get_pid $test_spawn_id]
 
     # Verify that we can attach to the process, and find its a.out
     # when we're cd'd to some directory that doesn't contain the
     # a.out.  (We use the source path set by the "dir" command.)
     
-    gdb_test "dir ${objdir}/${subdir}" "Source directories searched: .*" \
+    gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
        "set source path"
 
     gdb_test "cd /tmp" "Working directory /tmp." \
@@ -305,7 +299,7 @@ proc do_attach_tests {} {
        "before attach3, flush exec"
 
     gdb_test "attach $testpid" \
-       "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*" \
+       "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
        "attach when process' a.out not in cwd"
 
     set test "after attach3, exit"
@@ -316,27 +310,19 @@ proc do_attach_tests {} {
        "y"
     
     # Another "don't leave a process around"
-    remote_exec build "kill -9 ${testpid}"
+    kill_wait_spawned_process $test_spawn_id
 }
 
 proc do_call_attach_tests {} {
     global gdb_prompt
     global binfile2
     
-    # Start the program running and then wait for a bit, to be sure
-    # that it can be attached to.
-   
-    set testpid [eval exec $binfile2 &]
-    exec sleep 2
-    if { [istarget "*-*-cygwin*"] } {
-       # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
-       # different due to the way fork/exec works.
-       set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
-    }
+    set test_spawn_id [spawn_wait_for_attach $binfile2]
+    set testpid [spawn_id_get_pid $test_spawn_id]
 
     # Attach
    
-    gdb_test "file $binfile2" "" "force switch to gdb64, if necessary"
+    gdb_test "file $binfile2" ".*" "load file"
     set test "attach call"
     gdb_test_multiple "attach $testpid" "$test" {
        -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
@@ -365,13 +351,89 @@ proc do_call_attach_tests {} {
     # Get rid of the process
     
     gdb_test "p should_exit = 1"
-    gdb_test "c" "Program exited normally."
+    gdb_continue_to_end
    
     # Be paranoid
    
-    remote_exec build "kill -9 ${testpid}"
+    kill_wait_spawned_process $test_spawn_id
+}
+
+proc do_command_attach_tests {} {
+    global gdb_prompt
+    global binfile
+    global verbose
+    global GDB
+    global INTERNAL_GDBFLAGS
+    global GDBFLAGS
+
+    if ![isnative] then {
+       unsupported "command attach test"
+       return 0
+    }
+
+    set test_spawn_id [spawn_wait_for_attach $binfile]
+    set testpid [spawn_id_get_pid $test_spawn_id]
+
+    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"
+       }
+    }
+
+    # Get rid of the process
+    kill_wait_spawned_process $test_spawn_id
 }
 
+# Test ' gdb --pid PID -ex "run" '.  GDB used to have a bug where
+# "run" would run before the attach finished - PR17347.
+
+proc test_command_line_attach_run {} {
+    global gdb_prompt
+    global binfile
+
+    if ![isnative] then {
+       unsupported "commandline attach run test"
+       return 0
+    }
+
+    with_test_prefix "cmdline attach run" {
+       set test_spawn_id [spawn_wait_for_attach $binfile]
+       set testpid [spawn_id_get_pid $test_spawn_id]
+
+       set test "run to prompt"
+       gdb_exit
+
+       set res [gdb_spawn_with_cmdline_opts \
+                    "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
+       if { $res != 0} {
+           fail $test
+           kill_wait_spawned_process $test_spawn_id
+           return $res
+       }
+       gdb_test_multiple "" $test {
+           -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
+               pass $test
+           }
+       }
+
+       send_gdb "y\n"
+
+       set test "run to main"
+       gdb_test_multiple "" $test {
+           -re "Temporary breakpoint .* main .*$gdb_prompt $" {
+               pass $test
+           }
+       }
+
+       # Get rid of the process
+       kill_wait_spawned_process $test_spawn_id
+    }
+}
 
 # Start with a fresh gdb
 
@@ -392,4 +454,10 @@ gdb_start
 gdb_reinitialize_dir $srcdir/$subdir
 do_call_attach_tests
 
+# Test "gdb --pid"
+
+do_command_attach_tests
+
+test_command_line_attach_run
+
 return 0