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