2010-06-01 Michael Snyder <msnyder@vmware.com>
[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[^\r\n]*: [^ ]* 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 # DISPLACEMENT can be "NONE", "ZERO" or "NONZERO"
225 proc reach {func command displacement} {
226 global gdb_prompt expect_out
227
228 global pf_prefix
229 set old_ldprefix $pf_prefix
230 lappend pf_prefix "reach-$func:"
231
232 if [gdb_breakpoint $func allow-pending] {
233 set test "reach"
234 set test_displacement "seen displacement message as $displacement"
235 gdb_test_multiple $command $test {
236 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
237 # Missing "$gdb_prompt $" is intentional.
238 if {$expect_out(1,string) == "0x0"} {
239 set case "ZERO"
240 } else {
241 set case "NONZERO"
242 }
243 if {$displacement == $case} {
244 pass $test_displacement
245 # Permit multiple such messages.
246 set displacement "FOUND-$displacement"
247 } elseif {$displacement != "FOUND-$case"} {
248 fail $test_displacement
249 }
250 exp_continue
251 }
252 -re "Breakpoint \[0-9\]+, $func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
253 pass $test
254 }
255 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in $func \\(\\)( from .*)?\r\n$gdb_prompt $" {
256 pass $test
257 }
258 }
259 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
260 fail $test_displacement
261 }
262 }
263
264 set pf_prefix $old_ldprefix
265 }
266
267 proc test_core {file displacement} {
268 global srcdir subdir gdb_prompt expect_out
269
270 set corefile [core_find $file {} "segv"]
271 if {$corefile == ""} {
272 return
273 }
274
275 global pf_prefix
276 set old_ldprefix $pf_prefix
277 lappend pf_prefix "core:"
278
279 gdb_exit
280 gdb_start
281 # Clear it to never find any separate debug infos in $debug_root.
282 gdb_test_no_output "set debug-file-directory" \
283 "set debug-file-directory for core"
284 gdb_reinitialize_dir $srcdir/$subdir
285 gdb_load $file
286
287 # Print the "PIE (Position Independent Executable) displacement" message.
288 gdb_test_no_output "set verbose on"
289
290 set test "core loaded"
291 set test_displacement "seen displacement message as $displacement"
292 gdb_test_multiple "core-file $corefile" $test {
293 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
294 # Missing "$gdb_prompt $" is intentional.
295 if {$expect_out(1,string) == "0x0"} {
296 set case "ZERO"
297 } else {
298 set case "NONZERO"
299 }
300 if {$displacement == $case} {
301 pass $test_displacement
302 # Permit multiple such messages.
303 set displacement "FOUND-$displacement"
304 } elseif {$displacement != "FOUND-$case"} {
305 fail $test_displacement
306 }
307 exp_continue
308 }
309 -re "Core was generated by .*\r\n#0 .*$gdb_prompt $" {
310 # Do not check the binary filename as it may be truncated.
311 pass $test
312 }
313 }
314 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
315 fail $test_displacement
316 }
317
318 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
319
320 set pf_prefix $old_ldprefix
321 }
322
323 proc test_attach {file displacement} {
324 global board_info gdb_prompt expect_out
325
326 gdb_exit
327
328 set test "sleep function started"
329
330 set command "${file} sleep"
331 set res [remote_spawn host $command];
332 if { $res < 0 || $res == "" } {
333 perror "Spawning $command failed."
334 fail $test
335 return
336 }
337 set pid [exp_pid -i $res]
338 gdb_expect {
339 -re "sleeping\r\n" {
340 pass $test
341 }
342 eof {
343 fail "$test (eof)"
344 return
345 }
346 timeout {
347 fail "$test (timeout)"
348 return
349 }
350 }
351
352 global pf_prefix
353 set old_ldprefix $pf_prefix
354 lappend pf_prefix "attach:"
355
356 gdb_exit
357 gdb_start
358
359 # Print the "PIE (Position Independent Executable) displacement" message.
360 gdb_test_no_output "set verbose on"
361
362 set test "attach"
363 gdb_test_multiple "attach $pid" $test {
364 -re "Attaching to process $pid\r\n" {
365 # Missing "$gdb_prompt $" is intentional.
366 pass $test
367 }
368 }
369
370 set test "attach final prompt"
371 set test_displacement "seen displacement message as $displacement"
372 gdb_test_multiple "" $test {
373 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
374 # Missing "$gdb_prompt $" is intentional.
375 if {$expect_out(1,string) == "0x0"} {
376 set case "ZERO"
377 } else {
378 set case "NONZERO"
379 }
380 if {$displacement == $case} {
381 pass $test_displacement
382 # Permit multiple such messages.
383 set displacement "FOUND-$displacement"
384 } elseif {$displacement != "FOUND-$case"} {
385 fail $test_displacement
386 }
387 exp_continue
388 }
389 -re "$gdb_prompt $" {
390 pass $test
391 }
392 }
393 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
394 fail $test_displacement
395 }
396
397 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
398 gdb_exit
399
400 remote_exec host "kill -9 $pid"
401
402 set pf_prefix $old_ldprefix
403 }
404
405 proc test_ld {file ifmain trynosym displacement} {
406 global srcdir subdir gdb_prompt expect_out
407
408 # First test normal `file'-command loaded $FILE with symbols.
409
410 gdb_exit
411 gdb_start
412 # Clear it to never find any separate debug infos in $debug_root.
413 gdb_test_no_output "set debug-file-directory"
414 gdb_reinitialize_dir $srcdir/$subdir
415 gdb_load $file
416
417 # Print the "PIE (Position Independent Executable) displacement" message.
418 gdb_test_no_output "set verbose on"
419
420 reach "dl_main" "run segv" $displacement
421
422 gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
423
424 if $ifmain {
425 # Displacement message will be printed the second time on initializing
426 # the linker from svr4_special_symbol_handling. If any ANOFFSET has
427 # been already set as non-zero the detection will no longer be run.
428 if {$displacement == "NONZERO"} {
429 set displacement_main "NONE"
430 } else {
431 set displacement_main $displacement
432 }
433 reach "main" continue $displacement_main
434
435 reach "libfunc" continue "NONE"
436
437 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
438
439 test_core $file $displacement
440
441 test_attach $file $displacement
442 }
443
444 if !$trynosym {
445 return
446 }
447
448 global pf_prefix
449 set old_ldprefix $pf_prefix
450 lappend pf_prefix "symbol-less:"
451
452 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
453 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
454
455 gdb_exit
456 gdb_start
457 # Clear it to never find any separate debug infos in $debug_root.
458 gdb_test_no_output "set debug-file-directory"
459 gdb_reinitialize_dir $srcdir/$subdir
460
461 # Print the "PIE (Position Independent Executable) displacement" message.
462 gdb_test_no_output "set verbose on"
463
464 # Test no (error) message has been printed by `exec-file'.
465 set escapedfile [string_to_regexp $file]
466 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
467
468 if $ifmain {
469 reach "dl_main" run $displacement
470
471 set test "info files"
472 set entrynohex ""
473 gdb_test_multiple $test $test {
474 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
475 set entrynohex $expect_out(1,string)
476 pass $test
477 }
478 }
479 if {$entrynohex != ""} {
480 gdb_test "break *0x$entrynohex" "" "break at entry point"
481 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
482 }
483 } else {
484 # There is no symbol to break at ld.so. Moreover it can exit with an
485 # error code.
486
487 set test "ld.so exit"
488 set test_displacement "seen displacement message as $displacement"
489 gdb_test_multiple "run" $test {
490 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
491 # Missing "$gdb_prompt $" is intentional.
492 if {$expect_out(1,string) == "0x0"} {
493 set case "ZERO"
494 } else {
495 set case "NONZERO"
496 }
497 if {$displacement == $case} {
498 pass $test_displacement
499 # Permit multiple such messages.
500 set displacement "FOUND-$displacement"
501 } elseif {$displacement != "FOUND-$case"} {
502 fail $test_displacement
503 }
504 exp_continue
505 }
506 -re "Program exited (normally|with code \[0-9\]+)\\.\r\n$gdb_prompt $" {
507 # Do not check the binary filename as it may be truncated.
508 pass $test
509 }
510 }
511 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
512 fail $test_displacement
513 }
514 }
515
516 set pf_prefix $old_ldprefix
517 }
518
519 # Create separate binaries for each testcase - to make the possible reported
520 # problem reproducible after the whole test run finishes.
521
522 set old_ldprefix $pf_prefix
523 foreach ldprelink {NO YES} {
524 foreach ldsepdebug {NO IN SEP} {
525 # Skip running the ldsepdebug test if we do not have system separate
526 # debug info available.
527 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
528 continue
529 }
530
531 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
532 set interp $binprefix-$ldname
533
534 # prelink needs to always prelink all the dependencies to do any file
535 # modifications of its files. ld.so also needs all the dependencies to
536 # be prelinked to omit the relocation process. In-memory file offsets
537 # are not dependent whether ld.so went the prelink way or through the
538 # relocation process.
539 #
540 # For GDB we are not interested whether prelink succeeds as it is
541 # transparent to GDB. GDB is being tested for differences of file
542 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
543 # the BIN modification to happen but we need to restore the original
544 # possibly unprelinked ld.so to test all the combinations for GDB.
545 set interp_saved ${interp}-saved
546
547 set pf_prefix $old_ldprefix
548 lappend pf_prefix "$ldname:"
549
550 if {$ldsepdebug == "NO"} {
551 copy $interp_system $interp
552 # Never call strip-debug before unprelink:
553 # prelink: ...: Section .note.gnu.build-id created after prelinking
554 if ![prelinkNO $interp] {
555 continue
556 }
557 strip_debug $interp
558 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
559 copy $interp_system $interp
560 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
561 copy $interp_system $interp
562 copy $interp_system_debug "${interp}.debug"
563 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
564 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
565 continue
566 }
567 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
568 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
569 verbose -log "command is $command"
570 if [catch $command] {
571 setup_xfail *-*-*
572 fail $test
573 continue
574 } else {
575 pass $test
576 }
577 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
578 copy $interp_system $interp
579 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
580 if ![prelinkNO $interp] {
581 continue
582 }
583 gdb_gnu_strip_debug $interp
584 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
585 copy $interp_system $interp
586 copy $interp_system_debug "${interp}.debug"
587 }
588
589 if {$ldsepdebug == "SEP"} {
590 if ![prelinkNO "${interp}.debug"] {
591 continue
592 }
593 } else {
594 file delete "${interp}.debug"
595 }
596
597 if ![prelink$ldprelink $interp] {
598 continue
599 }
600 if {$ldprelink == "NO"} {
601 set displacement "NONZERO"
602 } else {
603 set displacement "ZERO"
604 }
605 test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
606
607 if ![copy $interp $interp_saved] {
608 continue
609 }
610 set old_binprefix $pf_prefix
611 foreach binprelink {NO YES} {
612 foreach binsepdebug {NO IN SEP} {
613 foreach binpie {NO YES} {
614 # This combination is not possible, non-PIE (fixed address)
615 # binary cannot be prelinked to any (other) address.
616 if {$binprelink == "YES" && $binpie == "NO"} {
617 continue
618 }
619
620 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
621 set exec $binprefix-$binname
622 set dir ${exec}.d
623
624 set pf_prefix $old_binprefix
625 lappend pf_prefix "$binname:"
626
627 set opts "additional_flags=-Wl,--dynamic-linker,$interp,-rpath,$dir"
628 lappend opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
629 if {$binsepdebug != "NO"} {
630 lappend opts {debug}
631 }
632 if {$binpie == "YES"} {
633 lappend opts {additional_flags=-fPIE -pie}
634 }
635 if {[build_executable ${test}.exp [file tail $exec] $srcfile $opts] == -1} {
636 continue;
637 }
638 if {$binsepdebug == "SEP"} {
639 gdb_gnu_strip_debug $exec
640 }
641
642 # Supply a self-sufficent directory $dir with the required
643 # libraries. To make an executable properly prelinked all
644 # its dependencies on libraries must be also prelinked. If
645 # some of the system libraries is currently not prelinked
646 # we have no right to prelink (modify it) at its current
647 # system place.
648
649 file delete -force $dir
650 file mkdir $dir
651
652 set command "ldd $exec"
653 set test "ldd [file tail $exec]"
654 set result [catch "exec $command" output]
655 verbose -log "result of $command is $result"
656 verbose -log "output of $command is $output"
657 if {$result != 0 || $output == ""} {
658 fail $test
659 } else {
660 pass $test
661 }
662
663 # gdb testsuite will put there also needless -lm.
664 set test "$test output contains libc"
665 set libc [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
666 if {[llength $libc] == 0} {
667 fail $test
668 } else {
669 pass $test
670 }
671
672 set dests {}
673 for {set i 1} {$i < [llength $libc]} {incr i 2} {
674 set abspath [lindex $libc $i]
675 set dest "$dir/[file tail $abspath]"
676 copy $abspath $dest
677 lappend dests $dest
678 }
679
680 if {[prelink$binprelink "--dynamic-linker=$interp --ld-library-path=$dir $exec $interp [concat $dests]" [file tail $exec]]
681 && [copy $interp_saved $interp]} {
682 if {$binpie == "NO"} {
683 set displacement "NONE"
684 } elseif {$binprelink == "NO"} {
685 set displacement "NONZERO"
686 } else {
687 set displacement "ZERO"
688 }
689 test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
690 }
691 }
692 }
693 }
694
695 file delete $interp_saved
696 }
697 }
698 set pf_prefix $old_ldprefix