1# Test `info auxv' and related functionality.
2
3# Copyright
4#	1992,1993,1994,1995,1996,1997,1998,1999,2000,2004,2007,2008,2009,2010
5#	Free Software Foundation, Inc.
6
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20# This file is based on corefile.exp which was written by Fred
21# Fish. (fnf@cygnus.com)
22
23if { ! [istarget "*-*-linux*"] && ! [istarget "*-*-solaris*"] } {
24    verbose "Skipping auxv.exp because of lack of support."
25    return
26}
27
28if $tracelevel then {
29	strace $tracelevel
30}
31
32
33set testfile "auxv"
34set srcfile ${testfile}.c
35set binfile ${objdir}/${subdir}/${testfile}
36set corefile ${objdir}/${subdir}/${testfile}.corefile
37set gcorefile ${objdir}/${subdir}/${testfile}.gcore
38
39if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
40    untested "couldn't compile ${srcdir}/${subdir}/${srcfile}"
41    return -1
42}
43
44# Use a fresh directory to confine the native core dumps.
45# Make it the working directory for gdb and its child.
46set coredir "${objdir}/${subdir}/coredir.[getpid]"
47file mkdir $coredir
48set core_works [expr [isnative] && ! [is_remote target]]
49
50# Run GDB on the test program up to where it will dump core.
51
52gdb_exit
53gdb_start
54gdb_reinitialize_dir $srcdir/$subdir
55gdb_load ${binfile}
56gdb_test_no_output "set print sevenbit-strings"
57gdb_test_no_output "set width 0"
58
59if {$core_works} {
60    if {[gdb_test "cd $coredir" ".*Working directory .*" \
61	     "cd to temporary directory for core dumps"]} {
62	set core_works 0
63    }
64}
65
66if { ![runto_main] } then {
67    gdb_suppress_tests;
68}
69set print_core_line [gdb_get_line_number "ABORT;"]
70gdb_test "tbreak $print_core_line"
71gdb_test continue ".*ABORT;.*"
72
73proc fetch_auxv {test} {
74    global gdb_prompt
75
76    set auxv_lines {}
77    set bad -1
78    # Former trailing `\[\r\n\]+' may eat just \r leaving \n in the buffer
79    # corrupting the next matches.
80    if {[gdb_test_multiple "info auxv" $test {
81	-re "info auxv\r\n" {
82	    exp_continue
83	}
84	-ex "The program has no auxiliary information now" {
85	    set bad 1
86	    exp_continue
87	}
88	-ex "Auxiliary vector is empty" {
89	    set bad 1
90	    exp_continue
91	}
92	-ex "No auxiliary vector found" {
93	    set bad 1
94	    exp_continue
95	}
96	-re "^\[0-9\]+\[ \t\]+(AT_\[^ \t\]+)\[^\r\n\]+\r\n" {
97	    lappend auxv_lines $expect_out(0,string)
98	    exp_continue
99	}
100	-re "^\[0-9\]+\[ \t\]+\\?\\?\\?\[^\r\n\]+\r\n" {
101	    warning "Unrecognized tag value: $expect_out(0,string)"
102	    set bad 1
103	    lappend auxv_lines $expect_out(0,string)
104	    exp_continue
105	}
106	-re "$gdb_prompt $" {
107	    incr bad
108	}
109	-re "^\[^\r\n\]+\r\n" {
110	    if {!$bad} {
111		warning "Unrecognized output: $expect_out(0,string)"
112		set bad 1
113	    }
114	    exp_continue
115	}
116    }] != 0} {
117	return {}
118    }
119
120    if {$bad} {
121	fail $test
122	return {}
123    }
124
125    pass $test
126    return $auxv_lines
127}
128
129set live_data [fetch_auxv "info auxv on live process"]
130
131# Now try gcore.
132set gcore_works 0
133set escapedfilename [string_to_regexp $gcorefile]
134gdb_test_multiple "gcore $gcorefile" "gcore" {
135    -re "Saved corefile ${escapedfilename}\[\r\n\]+$gdb_prompt $" {
136	pass "gcore"
137	set gcore_works 1
138    }
139    -re "Can't create a corefile\[\r\n\]+$gdb_prompt $" {
140	unsupported "gcore"
141    }
142    -re "Undefined command: .*\[\r\n\]+$gdb_prompt $" {
143	unsupported "gcore"
144    }
145}
146
147# Let the program continue and die.
148gdb_test continue ".*Program received signal.*"
149gdb_test continue ".*Program terminated with signal.*"
150
151# Now collect the core dump it left.
152set test "generate native core dump"
153if {$core_works} {
154    # Find the
155    set names [glob -nocomplain -directory $coredir *core*]
156    if {[llength $names] == 1} {
157	set file [file join $coredir [lindex $names 0]]
158	remote_exec build "mv $file $corefile"
159	pass $test
160    } else {
161	set core_works 0
162	warning "can't generate a core file - core tests suppressed - check ulimit -c"
163	fail $test
164    }
165} else {
166    unsupported $test
167}
168remote_exec build "rm -rf $coredir"
169
170# Now we can examine the core files and check that their data matches what
171# we saw in the process.  Note that the exact data can vary between runs,
172# so it's important that the native core dump file and the gcore-created dump
173# both be from the same run of the program as we examined live.
174
175proc do_core_test {works corefile test1 test2} {
176    if {! $works} {
177	unsupported $test1
178	unsupported $test2
179    } else {
180	gdb_test "core $corefile" "Core was generated by.*" \
181	    "load core file for $test1" \
182	    "A program is being debugged already.*" "y"
183	set core_data [fetch_auxv $test1]
184	global live_data
185	if {$core_data == $live_data} {
186	    pass $test2
187	} else {
188	    fail $test2
189	}
190    }
191}
192
193do_core_test $core_works $corefile \
194    "info auxv on native core dump" "matching auxv data from live and core"
195
196do_core_test $gcore_works $gcorefile \
197    "info auxv on gcore-created dump" "matching auxv data from live and gcore"
198