gdb/
[binutils-gdb.git] / gdb / testsuite / gdb.base / break-interp.exp
1 # Copyright 2010 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
16 # This test only works on GNU/Linux.
17 if { ![isnative] || [is_remote host] || ![istarget *-linux*] || [skip_shlib_tests]} {
18 continue
19 }
20
21 set test "break-interp"
22 set binprefix ${objdir}/${subdir}/${test}
23 # Only to get the $interp_system name.
24 set srcfile_test "start.c"
25 set binfile_test ${test}-test
26 set binfile_lib ${objdir}/${subdir}/${test}.so
27 set srcfile "${test}-main.c"
28 set srcfile_lib "${test}-lib.c"
29
30 if [get_compiler_info ${binfile_lib}] {
31 return -1
32 }
33
34 # Use -soname so that it is listed with " => " by ldd and this testcase makes
35 # a copy of ${binfile_lib} for each prelink variant.
36
37 if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug additional_flags=-Wl,-soname,${test}.so]] != ""} {
38 return -1
39 }
40
41 if {[build_executable ${test}.exp $binfile_test ${srcfile_test} {}] == -1} {
42 return -1
43 }
44
45 # Return the interpreter filename string.
46 # Return "" if no interpreter was found.
47 proc section_get {exec section} {
48 global objdir
49 global subdir
50 set tmp "${objdir}/${subdir}/break-interp.interp"
51 set objcopy_program [transform objcopy]
52
53 set command "exec $objcopy_program -O binary --set-section-flags $section=A --change-section-address $section=0 -j $section $exec $tmp"
54 verbose -log "command is $command"
55 set result [catch $command output]
56 verbose -log "result is $result"
57 verbose -log "output is $output"
58 if {$result == 1} {
59 return ""
60 }
61 set fi [open $tmp]
62 fconfigure $fi -translation binary
63 set data [read $fi]
64 close $fi
65 #file delete $tmp
66 # .interp has size $len + 1 but .gnu_debuglink contains garbage after \000.
67 set len [string first \000 $data]
68 if {$len < 0} {
69 verbose -log "section $section not found"
70 return ""
71 }
72 set retval [string range $data 0 [expr $len - 1]]
73 verbose -log "section $section is <$retval>"
74 return $retval
75 }
76
77 # Note: The separate debug info file content build-id/crc32 are not verified
78 # contrary to the GDB search algorithm skipping non-matching ones.
79 proc system_debug_get {exec} {
80 global debug_root
81
82 set exec_build_id_debug [build_id_debug_filename_get $exec]
83 set debug_base "[file tail $exec].debug"
84 set exec_dir [file dirname $exec]
85
86 # isfile returns 1 even for symlinks to files.
87 set retval $debug_root/$exec_build_id_debug
88 if [file isfile $retval] {
89 return $retval
90 }
91 set retval $exec_dir/$debug_base
92 if [file isfile $retval] {
93 return $retval
94 }
95 set retval $exec_dir/.debug/$debug_base
96 if [file isfile $retval] {
97 return $retval
98 }
99 set retval $debug_root/$exec_dir/$debug_base
100 if [file isfile $retval] {
101 return $retval
102 }
103 return ""
104 }
105
106 gdb_exit
107 gdb_start
108 set debug_root ""
109 set test "show debug-file-directory"
110 gdb_test_multiple $test $test {
111 -re "The directory where separate debug symbols are searched for is \"(.*)\".\r\n$gdb_prompt $" {
112 set debug_root $expect_out(1,string)
113 }
114 }
115
116 set interp_system [section_get ${objdir}/${subdir}/$binfile_test .interp]
117 set interp_system_debug [system_debug_get $interp_system]
118 verbose -log "$interp_system has debug $interp_system_debug"
119
120 proc prelinkNO_run {arg} {
121 set command "exec /usr/sbin/prelink -uN $arg"
122 verbose -log "command is $command"
123 set result [catch $command output]
124 verbose -log "result is $result"
125 verbose -log "output is $output"
126 return [list $result $output]
127 }
128
129 proc prelinkNO {arg {name {}}} {
130 if {$name == ""} {
131 set name [file tail $arg]
132 }
133 set test "unprelink $name"
134 set run [prelinkNO_run $arg]
135 set result [lindex $run 0]
136 set output [lindex $run 1]
137 if {$result == 0 && $output == ""} {
138 verbose -log "$name has been now unprelinked"
139 set run [prelinkNO_run $arg]
140 set result [lindex $run 0]
141 set output [lindex $run 1]
142 }
143 # Last line does miss the trailing \n.
144 if {$result == 1 && [regexp {^(/usr/sbin/prelink: [^ ]* does not have .gnu.prelink_undo section\n?)*$} $output]} {
145 pass $test
146 return 1
147 } else {
148 fail $test
149 return 0
150 }
151 }
152
153 proc prelinkYES {arg {name ""}} {
154 if {$name == ""} {
155 set name [file tail $arg]
156 }
157 set test "prelink $name"
158 set command "exec /usr/sbin/prelink -qNR --no-exec-shield $arg"
159 verbose -log "command is $command"
160 set result [catch $command output]
161 verbose -log "result is $result"
162 verbose -log "output is $output"
163 if {$result == 0 && $output == ""} {
164 pass $test
165 return 1
166 } else {
167 fail $test
168 return 0
169 }
170 }
171
172 # Resolve symlinks.
173 proc symlink_resolve {file} {
174 set loop 0
175 while {[file type $file] == "link"} {
176 set target [file readlink $file]
177 if {[file pathtype $target] == "relative"} {
178 set src2 [file dirname $file]/$target
179 } else {
180 set src2 $target
181 }
182 verbose -log "Resolved symlink $file targetting $target as $src2"
183 set file $src2
184
185 set loop [expr $loop + 1]
186 if {$loop > 30} {
187 fail "Looping symlink resolution for $file"
188 return ""
189 }
190 }
191 return $file
192 }
193
194 proc copy {src dest} {
195 set src [symlink_resolve $src]
196 # Test name would contain build-id hash for symlink-unresolved $src.
197 set test "copy [file tail $src] to [file tail $dest]"
198 set command "file copy -force $src $dest"
199 verbose -log "command is $command"
200 if [catch $command] {
201 fail $test
202 return 0
203 } else {
204 pass $test
205 return 1
206 }
207 }
208
209 proc strip_debug {dest} {
210 set test "strip [file tail $dest]"
211 set strip_program [transform strip]
212 set command "exec $strip_program --strip-debug $dest"
213 verbose -log "command is $command"
214 if [catch $command] {
215 fail $test
216 return 0
217 } else {
218 pass $test
219 return 1
220 }
221 }
222
223 # `runto' does not check we stopped really at the function we specified.
224 proc reach {func command} {
225 global gdb_prompt
226
227 if [gdb_breakpoint $func allow-pending] {
228 set test "reach $func"
229 gdb_test_multiple $command $test {
230 -re "Breakpoint \[0-9\]+, $func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
231 pass $test
232 }
233 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in $func \\(\\)( from .*)?\r\n$gdb_prompt $" {
234 pass $test
235 }
236 }
237 }
238 }
239
240 proc test_core {file} {
241 global srcdir subdir gdb_prompt
242
243 set corefile [core_find $file]
244 if {$corefile == ""} {
245 return
246 }
247
248 gdb_exit
249 gdb_start
250 # Clear it to never find any separate debug infos in $debug_root.
251 gdb_test "set debug-file-directory" "" "set debug-file-directory for core"
252 gdb_reinitialize_dir $srcdir/$subdir
253 gdb_load $file
254
255 # Do not check the binary filename as it may be truncated.
256 gdb_test "core-file $corefile" "Core was generated by .*\r\n#0 .*" "core loaded"
257
258 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
259 }
260
261 proc test_ld {file ifmain trynosym} {
262 global srcdir subdir gdb_prompt
263
264 # First test normal `file'-command loaded $FILE with symbols.
265
266 gdb_exit
267 gdb_start
268 # Clear it to never find any separate debug infos in $debug_root.
269 gdb_test "set debug-file-directory"
270 gdb_reinitialize_dir $srcdir/$subdir
271 gdb_load $file
272
273 reach "dl_main" run
274
275 gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
276
277 if $ifmain {
278 reach "main" continue
279
280 reach "libfunc" continue
281
282 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
283
284 test_core $file
285 }
286
287 if !$trynosym {
288 return
289 }
290
291 global pf_prefix
292 set old_ldprefix $pf_prefix
293 lappend pf_prefix "symbol-less:"
294
295 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
296 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
297
298 gdb_exit
299 gdb_start
300 # Clear it to never find any separate debug infos in $debug_root.
301 gdb_test "set debug-file-directory"
302 gdb_reinitialize_dir $srcdir/$subdir
303
304 # Test no (error) message has been printed by `exec-file'.
305 set escapedfile [string_to_regexp $file]
306 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
307
308 if $ifmain {
309 reach "dl_main" run
310
311 set test "info files"
312 set entrynohex ""
313 gdb_test_multiple $test $test {
314 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
315 set entrynohex $expect_out(1,string)
316 pass $test
317 }
318 }
319 if {$entrynohex != ""} {
320 gdb_test "break *0x$entrynohex" "" "break at entry point"
321 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
322 }
323 } else {
324 # There is no symbol to break at ld.so. Moreover it can exit with an
325 # error code.
326 gdb_test "run" "Program exited (normally|with code \[0-9\]+)\\." "ld.so exit"
327 }
328
329 set pf_prefix $old_ldprefix
330 }
331
332 # Create separate binaries for each testcase - to make the possible reported
333 # problem reproducible after the whole test run finishes.
334
335 set old_ldprefix $pf_prefix
336 foreach ldprelink {NO YES} {
337 foreach ldsepdebug {NO IN SEP} {
338 # Skip running the ldsepdebug test if we do not have system separate
339 # debug info available.
340 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
341 continue
342 }
343
344 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
345 set interp $binprefix-$ldname
346
347 # prelink needs to always prelink all the dependencies to do any file
348 # modifications of its files. ld.so also needs all the dependencies to
349 # be prelinked to omit the relocation process. In-memory file offsets
350 # are not dependent whether ld.so went the prelink way or through the
351 # relocation process.
352 #
353 # For GDB we are not interested whether prelink succeeds as it is
354 # transparent to GDB. GDB is being tested for differences of file
355 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
356 # the BIN modification to happen but we need to restore the original
357 # possibly unprelinked ld.so to test all the combinations for GDB.
358 set interp_saved ${interp}-saved
359
360 set pf_prefix $old_ldprefix
361 lappend pf_prefix "$ldname:"
362
363 if {$ldsepdebug == "NO"} {
364 copy $interp_system $interp
365 # Never call strip-debug before unprelink:
366 # prelink: ...: Section .note.gnu.build-id created after prelinking
367 if ![prelinkNO $interp] {
368 continue
369 }
370 strip_debug $interp
371 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
372 copy $interp_system $interp
373 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
374 copy $interp_system $interp
375 copy $interp_system_debug "${interp}.debug"
376 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
377 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
378 continue
379 }
380 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
381 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
382 verbose -log "command is $command"
383 if [catch $command] {
384 setup_xfail *-*-*
385 fail $test
386 continue
387 } else {
388 pass $test
389 }
390 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
391 copy $interp_system $interp
392 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
393 if ![prelinkNO $interp] {
394 continue
395 }
396 gdb_gnu_strip_debug $interp
397 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
398 copy $interp_system $interp
399 copy $interp_system_debug "${interp}.debug"
400 }
401
402 if {$ldsepdebug == "SEP"} {
403 if ![prelinkNO "${interp}.debug"] {
404 continue
405 }
406 } else {
407 file delete "${interp}.debug"
408 }
409
410 if ![prelink$ldprelink $interp] {
411 continue
412 }
413 test_ld $interp 0 [expr {$ldsepdebug == "NO"}]
414
415 if ![copy $interp $interp_saved] {
416 continue
417 }
418 set old_binprefix $pf_prefix
419 foreach binprelink {NO YES} {
420 foreach binsepdebug {NO IN SEP} {
421 foreach binpie {NO YES} {
422 # This combination is not possible, non-PIE (fixed address)
423 # binary cannot be prelinked to any (other) address.
424 if {$binprelink == "YES" && $binpie == "NO"} {
425 continue
426 }
427
428 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
429 set exec $binprefix-$binname
430 set dir ${exec}.d
431
432 set pf_prefix $old_binprefix
433 lappend pf_prefix "$binname:"
434
435 set opts "additional_flags=-Wl,--dynamic-linker,$interp,-rpath,$dir"
436 lappend opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
437 if {$binsepdebug != "NO"} {
438 lappend opts {debug}
439 }
440 if {$binpie == "YES"} {
441 lappend opts {additional_flags=-fPIE -pie}
442 }
443 if {[build_executable ${test}.exp [file tail $exec] $srcfile $opts] == -1} {
444 continue;
445 }
446 if {$binsepdebug == "SEP"} {
447 gdb_gnu_strip_debug $exec
448 # Just a sanity check. As gdb_gnu_strip_debug uses the
449 # "[file dirname $exec]/.debug/[file tail $exec].debug"
450 # variant delete the higher-priority exec.debug file.
451 file delete "$exec.debug"
452 }
453
454 # Supply a self-sufficent directory $dir with the required
455 # libraries. To make an executable properly prelinked all
456 # its dependencies on libraries must be also prelinked. If
457 # some of the system libraries is currently not prelinked
458 # we have no right to prelink (modify it) at its current
459 # system place.
460
461 file delete -force $dir
462 file mkdir $dir
463
464 set command "ldd $exec"
465 set result [catch "exec $command" output]
466 verbose -log "result of $command is $result"
467 verbose -log "output of $command is $output"
468 if {$result != 0 || $output == ""} {
469 fail $command
470 } else {
471 pass $command
472 }
473
474 # gdb testsuite will put there also needless -lm.
475 set test "$command output contains libc"
476 set libc [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
477 if {[llength $libc] == 0} {
478 fail $test
479 } else {
480 pass $test
481 }
482
483 set dests {}
484 for {set i 1} {$i < [llength $libc]} {incr i 2} {
485 set abspath [lindex $libc $i]
486 set dest "$dir/[file tail $abspath]"
487 copy $abspath $dest
488 lappend dests $dest
489 }
490
491 if {[prelink$binprelink "--dynamic-linker=$interp --ld-library-path=$dir $exec $interp [concat $dests]" $exec]
492 && [copy $interp_saved $interp]} {
493 test_ld $exec 1 [expr {$binsepdebug == "NO"}]
494 }
495 }
496 }
497 }
498
499 file delete $interp_saved
500 }
501 }
502 set pf_prefix $old_ldprefix