1# Copyright 2012-2013 Free Software Foundation, Inc.
2
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.
7#
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.
12#
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/>.
15
16if [is_remote target] {
17    # The test always runs locally.
18    return 0
19}
20
21set test valgrind-infcall
22set srcfile $test.c
23set executable $test
24set binfile ${objdir}/${subdir}/${executable}
25if {[build_executable $test.exp $executable $srcfile {debug}] == -1} {
26    return -1
27}
28
29set test "spawn valgrind"
30set cmd "valgrind --vgdb-error=0 $binfile"
31set res [remote_spawn host $cmd];
32if { $res < 0 || $res == "" } {
33    verbose -log "Spawning $cmd failed."
34    unsupported $test
35    return -1
36}
37pass $test
38# Declare GDB now as running.
39set gdb_spawn_id -1
40
41# GDB started by vgdb stops already after the startup is executed, like with
42# non-extended gdbserver.  It is also not correct to run/attach the inferior.
43set use_gdb_stub 1
44
45set test "valgrind started"
46# The trailing '.' differs for different memcheck versions.
47gdb_test_multiple "" $test {
48    -re "Memcheck, a memory error detector\\.?\r\n" {
49	pass $test
50    }
51    -re "valgrind: failed to start tool 'memcheck' for platform '.*': No such file or directory" {
52	unsupported $test
53	return -1
54    }
55    -re "valgrind: wrong ELF executable class" {
56	unsupported $test
57	return -1
58    }
59    -re "command not found" {
60	# The spawn succeeded, but then valgrind was not found - e.g. if
61	# we spawned SSH to a remote system.
62	unsupported $test
63	return -1
64    }
65    -re "valgrind: Bad option.*--vgdb-error=0" {
66	# valgrind is not >= 3.7.0.
67	unsupported $test
68	return -1
69    }
70}
71
72set test "vgdb prompt"
73# The trailing '.' differs for different memcheck versions.
74gdb_test_multiple "" $test {
75    -re "  (target remote | \[^\r\n\]*/vgdb \[^\r\n\]*)\r\n" {
76	set vgdbcmd $expect_out(1,string)
77	pass $test
78    }
79}
80
81# Do not kill valgrind.
82set valgrind_pid [exp_pid -i [board_info host fileid]]
83unset gdb_spawn_id
84set board [host_info name]
85unset_board_info fileid
86
87clean_restart $executable
88
89gdb_test "$vgdbcmd" " in \\.?_start .*" "target remote for vgdb"
90
91gdb_test "monitor v.set gdb_output" "valgrind output will go to gdb.*"
92
93set continue_count 1
94set loop 1
95while {$loop && $continue_count < 100} {
96    set test "continue #$continue_count"
97    gdb_test_multiple "continue" "" {
98	-re "Invalid free\\(\\).*: main .*\r\n$gdb_prompt $" {
99	    pass $test
100	    set loop 0
101	}
102	-re "Remote connection closed.*\r\n$gdb_prompt $" {
103	    fail "$test (remote connection closed)"
104	    # Only if valgrind got stuck.
105	    remote_exec host "kill -9 ${valgrind_pid}"
106	    return -1
107	}
108	-re "The program is not being run\\.\r\n$gdb_prompt $" {
109	    fail "$test (valgrind vgdb has terminated)"
110	    # Only if valgrind got stuck.
111	    remote_exec host "kill -9 ${valgrind_pid}"
112	    return -1
113	}
114	-re "\r\n$gdb_prompt $" {
115	    pass "$test (false warning)"
116	}
117    }
118    set continue_count [expr $continue_count + 1]
119}
120
121set test "p gdb_test_infcall ()"
122gdb_test_multiple $test $test {
123    -re "unhandled instruction bytes.*\r\n$gdb_prompt $" {
124	fail $test
125    }
126    -re "Continuing \\.\\.\\..*\r\n\\\$1 = 2\r\n$gdb_prompt $" {
127	pass $test
128    }
129}
130
131# Only if valgrind got stuck.
132remote_exec host "kill -9 ${valgrind_pid}"
133