1 # Copyright 2009-2023 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Generic subroutines for handling valgrind vgdb server.
19 # Start a vgdb server, and connect gdb to it. Return 0 on success, and -1 on
22 proc vgdb_start { {active_at_startup 1} } {
23 global binfile use_gdb_stub board testfile
24 global valgrind_spawn_id gdb_spawn_id
27 set test "spawn valgrind"
29 lappend cmd_list "valgrind"
30 if { $active_at_startup } {
31 lappend cmd_list "--vgdb-error=0"
33 lappend cmd_list $binfile
34 set cmd [join $cmd_list]
35 set res [remote_spawn host $cmd]
36 if { $res < 0 || $res == "" } {
37 verbose -log "Spawning $cmd failed."
42 # Declare GDB now as running.
45 # GDB started by vgdb stops already after the startup is executed, like with
46 # non-extended gdbserver. It is also not correct to run/attach the inferior.
49 set test "valgrind started"
50 # The trailing '.' differs for different memcheck versions.
51 gdb_test_multiple "" $test {
52 -re "==($decimal)== Memcheck, a memory error detector\\.?\r\n" {
53 set vgdbpid $expect_out(1,string)
56 -re "valgrind: failed to start tool 'memcheck' for platform '.*': No such file or directory" {
60 -re "valgrind: wrong ELF executable class" {
64 -re "command not found" {
65 # The spawn succeeded, but then valgrind was not found - e.g. if
66 # we spawned SSH to a remote system.
70 -re "valgrind: Bad option.*--vgdb-error=0" {
71 # valgrind is not >= 3.7.0.
77 # Do not kill valgrind.
78 set valgrind_spawn_id [board_info host fileid]
80 set board [host_info name]
81 unset_board_info fileid
83 clean_restart $testfile
87 # Make sure we're disconnected, in case we're testing with the
88 # native-extended-gdbserver board, where gdb_start/gdb_load spawn
89 # gdbserver and connect to it.
90 gdb_test "disconnect" ".*"
92 set vgdbcmd "target remote | vgdb --wait=2 --max-invoke-ms=2500 --pid=$vgdbpid"
94 if { $active_at_startup } {
95 gdb_test "$vgdbcmd" " in \\.?_start .*" "target remote for vgdb"
97 # Let $binfile run a bit before attaching. This is a bit of a hack,
98 # in that it lets test-case valgrind-infcall-2.exp run to the point of
99 # nanosleep, which seems to be required to trigger the error condition.
100 # So, without this, we hit
101 # "UNSUPPORTED: gdb.base/valgrind-infcall-2.exp: do printf".
104 # Connect to vgdb. Don't expect to be anywhere in particular.
105 gdb_test "$vgdbcmd" "" "target remote for vgdb"
108 gdb_test "monitor v.set gdb_output" "valgrind output will go to gdb.*"
116 global valgrind_spawn_id
118 # Only if valgrind got stuck.
119 kill_wait_spawned_process $valgrind_spawn_id