gdb/testsuite/
[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 {} "segv"]
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_attach {file} {
262 global board_info
263
264 gdb_exit
265
266 set test "sleep function started"
267
268 set command "${file} sleep"
269 set res [remote_spawn host $command];
270 if { $res < 0 || $res == "" } {
271 perror "Spawning $command failed."
272 fail $test
273 return
274 }
275 set pid [exp_pid -i $res]
276 gdb_expect {
277 -re "sleeping\r\n" {
278 pass $test
279 }
280 eof {
281 fail "$test (eof)"
282 return
283 }
284 timeout {
285 fail "$test (timeout)"
286 return
287 }
288 }
289
290 gdb_exit
291 gdb_start
292 gdb_test "attach $pid" "Attaching to process $pid\r\n.*" "attach"
293 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
294 gdb_exit
295
296 remote_exec host "kill -9 $pid"
297 }
298
299 proc test_ld {file ifmain trynosym} {
300 global srcdir subdir gdb_prompt
301
302 # First test normal `file'-command loaded $FILE with symbols.
303
304 gdb_exit
305 gdb_start
306 # Clear it to never find any separate debug infos in $debug_root.
307 gdb_test "set debug-file-directory"
308 gdb_reinitialize_dir $srcdir/$subdir
309 gdb_load $file
310
311 reach "dl_main" "run segv"
312
313 gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
314
315 if $ifmain {
316 reach "main" continue
317
318 reach "libfunc" continue
319
320 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
321
322 test_core $file
323
324 test_attach $file
325 }
326
327 if !$trynosym {
328 return
329 }
330
331 global pf_prefix
332 set old_ldprefix $pf_prefix
333 lappend pf_prefix "symbol-less:"
334
335 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
336 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
337
338 gdb_exit
339 gdb_start
340 # Clear it to never find any separate debug infos in $debug_root.
341 gdb_test "set debug-file-directory"
342 gdb_reinitialize_dir $srcdir/$subdir
343
344 # Test no (error) message has been printed by `exec-file'.
345 set escapedfile [string_to_regexp $file]
346 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
347
348 if $ifmain {
349 reach "dl_main" run
350
351 set test "info files"
352 set entrynohex ""
353 gdb_test_multiple $test $test {
354 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
355 set entrynohex $expect_out(1,string)
356 pass $test
357 }
358 }
359 if {$entrynohex != ""} {
360 gdb_test "break *0x$entrynohex" "" "break at entry point"
361 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
362 }
363 } else {
364 # There is no symbol to break at ld.so. Moreover it can exit with an
365 # error code.
366 gdb_test "run" "Program exited (normally|with code \[0-9\]+)\\." "ld.so exit"
367 }
368
369 set pf_prefix $old_ldprefix
370 }
371
372 # Create separate binaries for each testcase - to make the possible reported
373 # problem reproducible after the whole test run finishes.
374
375 set old_ldprefix $pf_prefix
376 foreach ldprelink {NO YES} {
377 foreach ldsepdebug {NO IN SEP} {
378 # Skip running the ldsepdebug test if we do not have system separate
379 # debug info available.
380 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
381 continue
382 }
383
384 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
385 set interp $binprefix-$ldname
386
387 # prelink needs to always prelink all the dependencies to do any file
388 # modifications of its files. ld.so also needs all the dependencies to
389 # be prelinked to omit the relocation process. In-memory file offsets
390 # are not dependent whether ld.so went the prelink way or through the
391 # relocation process.
392 #
393 # For GDB we are not interested whether prelink succeeds as it is
394 # transparent to GDB. GDB is being tested for differences of file
395 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
396 # the BIN modification to happen but we need to restore the original
397 # possibly unprelinked ld.so to test all the combinations for GDB.
398 set interp_saved ${interp}-saved
399
400 set pf_prefix $old_ldprefix
401 lappend pf_prefix "$ldname:"
402
403 if {$ldsepdebug == "NO"} {
404 copy $interp_system $interp
405 # Never call strip-debug before unprelink:
406 # prelink: ...: Section .note.gnu.build-id created after prelinking
407 if ![prelinkNO $interp] {
408 continue
409 }
410 strip_debug $interp
411 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
412 copy $interp_system $interp
413 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
414 copy $interp_system $interp
415 copy $interp_system_debug "${interp}.debug"
416 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
417 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
418 continue
419 }
420 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
421 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
422 verbose -log "command is $command"
423 if [catch $command] {
424 setup_xfail *-*-*
425 fail $test
426 continue
427 } else {
428 pass $test
429 }
430 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
431 copy $interp_system $interp
432 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
433 if ![prelinkNO $interp] {
434 continue
435 }
436 gdb_gnu_strip_debug $interp
437 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
438 copy $interp_system $interp
439 copy $interp_system_debug "${interp}.debug"
440 }
441
442 if {$ldsepdebug == "SEP"} {
443 if ![prelinkNO "${interp}.debug"] {
444 continue
445 }
446 } else {
447 file delete "${interp}.debug"
448 }
449
450 if ![prelink$ldprelink $interp] {
451 continue
452 }
453 test_ld $interp 0 [expr {$ldsepdebug == "NO"}]
454
455 if ![copy $interp $interp_saved] {
456 continue
457 }
458 set old_binprefix $pf_prefix
459 foreach binprelink {NO YES} {
460 foreach binsepdebug {NO IN SEP} {
461 foreach binpie {NO YES} {
462 # This combination is not possible, non-PIE (fixed address)
463 # binary cannot be prelinked to any (other) address.
464 if {$binprelink == "YES" && $binpie == "NO"} {
465 continue
466 }
467
468 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
469 set exec $binprefix-$binname
470 set dir ${exec}.d
471
472 set pf_prefix $old_binprefix
473 lappend pf_prefix "$binname:"
474
475 set opts "additional_flags=-Wl,--dynamic-linker,$interp,-rpath,$dir"
476 lappend opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
477 if {$binsepdebug != "NO"} {
478 lappend opts {debug}
479 }
480 if {$binpie == "YES"} {
481 lappend opts {additional_flags=-fPIE -pie}
482 }
483 if {[build_executable ${test}.exp [file tail $exec] $srcfile $opts] == -1} {
484 continue;
485 }
486 if {$binsepdebug == "SEP"} {
487 gdb_gnu_strip_debug $exec
488 # Just a sanity check. As gdb_gnu_strip_debug uses the
489 # "[file dirname $exec]/.debug/[file tail $exec].debug"
490 # variant delete the higher-priority exec.debug file.
491 file delete "$exec.debug"
492 }
493
494 # Supply a self-sufficent directory $dir with the required
495 # libraries. To make an executable properly prelinked all
496 # its dependencies on libraries must be also prelinked. If
497 # some of the system libraries is currently not prelinked
498 # we have no right to prelink (modify it) at its current
499 # system place.
500
501 file delete -force $dir
502 file mkdir $dir
503
504 set command "ldd $exec"
505 set test "ldd [file tail $exec]"
506 set result [catch "exec $command" output]
507 verbose -log "result of $command is $result"
508 verbose -log "output of $command is $output"
509 if {$result != 0 || $output == ""} {
510 fail $test
511 } else {
512 pass $test
513 }
514
515 # gdb testsuite will put there also needless -lm.
516 set test "$test output contains libc"
517 set libc [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
518 if {[llength $libc] == 0} {
519 fail $test
520 } else {
521 pass $test
522 }
523
524 set dests {}
525 for {set i 1} {$i < [llength $libc]} {incr i 2} {
526 set abspath [lindex $libc $i]
527 set dest "$dir/[file tail $abspath]"
528 copy $abspath $dest
529 lappend dests $dest
530 }
531
532 if {[prelink$binprelink "--dynamic-linker=$interp --ld-library-path=$dir $exec $interp [concat $dests]" [file tail $exec]]
533 && [copy $interp_saved $interp]} {
534 test_ld $exec 1 [expr {$binsepdebug == "NO"}]
535 }
536 }
537 }
538 }
539
540 file delete $interp_saved
541 }
542 }
543 set pf_prefix $old_ldprefix