]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/break-interp.exp
testsuite: tcl exec& -> 'kill -9 $pid' is racy (attach-many-short-lived-thread.exp...
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / break-interp.exp
1 # Copyright 2010-2015 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] || [target_info exists use_gdb_stub]
18 || ![istarget *-linux*] || [skip_shlib_tests]} {
19 continue
20 }
21
22 load_lib prelink-support.exp
23
24 set test "break-interp"
25 set binprefix [standard_output_file ${test}]
26 # Only to get the $interp_system name.
27 set srcfile_test "start.c"
28 set binfile_test ${test}-test
29 set binfile_lib ${binprefix}.so
30 set srcfile "${test}-main.c"
31 set srcfile_lib "${test}-lib.c"
32
33 if [get_compiler_info] {
34 return -1
35 }
36
37 # Use -soname so that the new library gets copied by build_executable_own_libs.
38
39 if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug ldflags=-Wl,-soname,${test}.so]] != ""} {
40 return -1
41 }
42
43 if {[build_executable ${test}.exp $binfile_test ${srcfile_test} {}] == -1} {
44 return -1
45 }
46
47 # Note: The separate debug info file content build-id/crc32 are not verified
48 # contrary to the GDB search algorithm skipping non-matching ones.
49 proc system_debug_get {exec} {
50 global debug_root
51
52 set exec_build_id_debug [build_id_debug_filename_get $exec]
53 set debug_base "[file tail $exec].debug"
54 set exec_dir [file dirname $exec]
55
56 # isfile returns 1 even for symlinks to files.
57 set retval $debug_root/$exec_build_id_debug
58 if [file isfile $retval] {
59 return $retval
60 }
61 set retval $exec_dir/$debug_base
62 if [file isfile $retval] {
63 return $retval
64 }
65 set retval $exec_dir/.debug/$debug_base
66 if [file isfile $retval] {
67 return $retval
68 }
69 set retval $debug_root/$exec_dir/$debug_base
70 if [file isfile $retval] {
71 return $retval
72 }
73 return ""
74 }
75
76 gdb_exit
77 gdb_start
78 set debug_root ""
79 set test "show debug-file-directory"
80 gdb_test_multiple $test $test {
81 -re "The directory where separate debug symbols are searched for is \"(.*)\".\r\n$gdb_prompt $" {
82 set debug_root $expect_out(1,string)
83 }
84 }
85
86 set interp_system [section_get [standard_output_file $binfile_test] .interp]
87 set interp_system_debug [system_debug_get $interp_system]
88 verbose -log "$interp_system has debug $interp_system_debug"
89
90 proc prelinkNO {arg {name ""}} {
91 return [prelink_no $arg $name]
92 }
93
94 proc prelinkYES {arg {name ""}} {
95 return [prelink_yes $arg $name]
96 }
97
98 proc strip_debug {dest} {
99 set test "strip [file tail $dest]"
100 set strip_program [transform strip]
101 set command "exec $strip_program --strip-debug $dest"
102 verbose -log "command is $command"
103 if [catch $command] {
104 fail $test
105 return 0
106 } else {
107 pass $test
108 return 1
109 }
110 }
111
112 # The marker function for the standard runtime linker interface is
113 # _dl_debug_state. The probes-based interface has no specific marker
114 # function; the probe we will stop on (init_start) is in dl_main so we
115 # check for that.
116
117 set solib_bp {(_dl_debug_state|dl_main)}
118
119 # Implementation of reach.
120
121 proc reach_1 {func command displacement} {
122 global gdb_prompt expect_out solib_bp
123
124 if {$func == $solib_bp} {
125 # Breakpoint on _dl_debug_state can have problems due to its overlap
126 # with the existing internal breakpoint from GDB.
127 gdb_test_no_output "set stop-on-solib-events 1"
128 } elseif {! [gdb_breakpoint $func allow-pending]} {
129 return
130 }
131
132 set test "reach"
133 set test_displacement "seen displacement message as $displacement"
134 set debug_state_count 0
135 gdb_test_multiple $command $test {
136 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
137 # Missing "$gdb_prompt $" is intentional.
138 if {$expect_out(1,string) == "0x0"} {
139 set case "ZERO"
140 } else {
141 set case "NONZERO"
142 }
143 if {$displacement == $case || $displacement == "PRESENT"} {
144 pass $test_displacement
145 set displacement "FOUND-$displacement"
146 } else {
147 fail $test_displacement
148 }
149 exp_continue
150 }
151 -re "Breakpoint \[0-9\]+, \\.?(__GI_)?$func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
152 if {$func == $solib_bp} {
153 fail $test
154 } else {
155 pass $test
156 }
157 }
158 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in \\.?(__GI_)?$func \\(\\).*\r\n$gdb_prompt $" {
159 if {$func == $solib_bp} {
160 fail $test
161 } else {
162 pass $test
163 }
164 }
165 -re "Stopped due to (spurious )?shared library event.*\r\n$gdb_prompt $" {
166 if {$func == $solib_bp} {
167 if {$debug_state_count == 0} {
168 # First stop does not yet relocate the _start function
169 # descriptor on ppc64.
170 set debug_state_count 1
171 send_gdb "continue\n"
172 exp_continue
173 } else {
174 pass $test
175 }
176 } else {
177 fail $test
178 }
179 }
180 }
181 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
182 fail $test_displacement
183 }
184
185 if {$func == $solib_bp} {
186 gdb_test_no_output "set stop-on-solib-events 0"
187 }
188 }
189
190 # `runto' does not check we stopped really at the function we specified.
191 # DISPLACEMENT can be "NONE" for no message to be present, "ZERO" for
192 # displacement of 0 bytes to be present, "NONZERO" for displacement of non-0
193 # bytes to be present and "PRESENT" if both "ZERO" and "NONZERO" are valid.
194 proc reach {func command displacement} {
195 with_test_prefix "reach-$func" {
196 reach_1 $func $command $displacement
197 }
198 }
199
200 proc test_core {file displacement} {
201 with_test_prefix "core" {
202 global srcdir subdir gdb_prompt expect_out
203
204 set corefile [core_find $file {} "segv"]
205 if {$corefile == ""} {
206 return
207 }
208
209 gdb_exit
210 gdb_start
211 # Clear it to never find any separate debug infos in $debug_root.
212 gdb_test_no_output "set debug-file-directory" \
213 "set debug-file-directory for core"
214 gdb_reinitialize_dir $srcdir/$subdir
215 gdb_load $file
216
217 # Print the "PIE (Position Independent Executable) displacement" message.
218 gdb_test_no_output "set verbose on"
219
220 set test "core loaded"
221 set test_displacement "seen displacement message as $displacement"
222 gdb_test_multiple "core-file $corefile" $test {
223 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
224 # Missing "$gdb_prompt $" is intentional.
225 if {$expect_out(1,string) == "0x0"} {
226 set case "ZERO"
227 } else {
228 set case "NONZERO"
229 }
230 if {$displacement == $case || $displacement == "PRESENT"} {
231 pass $test_displacement
232 set displacement "FOUND-$displacement"
233 } else {
234 fail $test_displacement
235 }
236 exp_continue
237 }
238 -re "Core was generated by .*\r\n#0 .*$gdb_prompt $" {
239 # Do not check the binary filename as it may be truncated.
240 pass $test
241 }
242 }
243 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
244 fail $test_displacement
245 }
246
247 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
248 }
249 }
250
251 proc test_attach_gdb {file pid displacement prefix} {
252 with_test_prefix "$prefix" {
253 global gdb_prompt expect_out
254
255 gdb_exit
256 gdb_start
257
258 # Print the "PIE (Position Independent Executable) displacement" message.
259 gdb_test_no_output "set verbose on"
260
261 gdb_test "file $file" "Reading symbols from .*done\\." "file"
262
263 set test "attach"
264 gdb_test_multiple "attach $pid" $test {
265 -re "Attaching to (program: .*, )?process $pid\r\n" {
266 # Missing "$gdb_prompt $" is intentional.
267 pass $test
268 }
269 }
270
271 set test "attach final prompt"
272 set test_displacement "seen displacement message as $displacement"
273 gdb_test_multiple "" $test {
274 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
275 # Missing "$gdb_prompt $" is intentional.
276 if {$expect_out(1,string) == "0x0"} {
277 set case "ZERO"
278 } else {
279 set case "NONZERO"
280 }
281 if {$displacement == $case || $displacement == "PRESENT"} {
282 pass $test_displacement
283 set displacement "FOUND-$displacement"
284 } else {
285 fail $test_displacement
286 }
287 exp_continue
288 }
289 -re "$gdb_prompt $" {
290 pass $test
291 }
292 }
293 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
294 fail $test_displacement
295 }
296
297 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
298 gdb_exit
299 }
300 }
301
302 proc test_attach {file displacement {relink_args ""}} {
303 global board_info
304 global exec
305
306 gdb_exit
307
308 set test "sleep function started"
309
310 set command "${file} sleep"
311 set test_spawn_id [remote_spawn host $command]
312 if { $test_spawn_id < 0 || $test_spawn_id == "" } {
313 perror "Spawning $command failed."
314 fail $test
315 return
316 }
317 set pid [spawn_id_get_pid $test_spawn_id]
318 gdb_expect {
319 -re "sleeping\r\n" {
320 pass $test
321 }
322 eof {
323 fail "$test (eof)"
324 wait -i $test_spawn_id
325 return
326 }
327 timeout {
328 fail "$test (timeout)"
329 kill_wait_spawned_process $test_spawn_id
330 return
331 }
332 }
333
334 if {$relink_args == ""} {
335 test_attach_gdb $exec $pid $displacement "attach"
336 } else {
337 # These could be rather passed as arguments.
338 global interp_saved interp
339
340 foreach relink {YES NO} {
341 # Formerly this test was testing only prelinking of $EXEC. As the
342 # prelink command automatically prelinks all of $EXEC's libraries,
343 # even $INTERP got prelinked. Therefore, we formerly had to
344 # `[file_copy $interp_saved $interp]' to make $INTERP not affected
345 # by this prelinking of $EXEC.
346 #
347 # But now we need to test even prelinking of $INTERP. We could
348 # create a separate test to test just the $INTERP prelinking. For
349 # test simplicity, we merged this test and the test above by not
350 # restoring $INTERP after $EXEC prelink. $INTERP gets restored
351 # later below.
352 #
353 # `(wrong library or version mismatch?)' messages are printed for
354 # $binfile_lib on platforms converting REL->RELA relocations by
355 # prelink (such as on i386). There is no reliable way to verify
356 # the library file matches the running library in such case but
357 # GDB at least attempts to set the right displacement. We test
358 # `libfunc' is present in the backtrace and therefore the
359 # displacement has been guessed right.
360
361 if [prelink$relink $relink_args [file tail $exec]] {
362 # /proc/PID/exe cannot be loaded as it is "EXECNAME (deleted)".
363 test_attach_gdb $exec $pid $displacement "attach-relink$relink"
364 }
365 }
366 file_copy $interp_saved $interp
367 }
368
369 kill_wait_spawned_process $test_spawn_id
370 }
371
372 proc test_ld {file ifmain trynosym displacement} {
373 global srcdir subdir gdb_prompt expect_out inferior_exited_re solib_bp
374
375 # First test normal `file'-command loaded $FILE with symbols.
376
377 gdb_exit
378 gdb_start
379 # Clear it to never find any separate debug infos in $debug_root.
380 gdb_test_no_output "set debug-file-directory"
381 gdb_reinitialize_dir $srcdir/$subdir
382 gdb_load $file
383
384 # Print the "PIE (Position Independent Executable) displacement" message.
385 gdb_test_no_output "set verbose on"
386
387 # We want to test the re-run of a PIE in the case where the executable
388 # is loaded with a different displacement, but disable-randomization
389 # prevents that from happening. So turn it off.
390 gdb_test "set disable-randomization off"
391
392 if $ifmain {
393 gdb_test_no_output "set args segv"
394 } else {
395 global binfile_test
396
397 # ld.so needs some executable to run to reach _dl_debug_state.
398 gdb_test_no_output "set args [standard_output_file $binfile_test]" \
399 "set args OBJDIR/${subdir}/$binfile_test"
400 }
401
402 reach $solib_bp "run" $displacement
403
404 gdb_test "bt" "#0 +\[^\r\n\]*\\m(__GI_)?$solib_bp\\M.*" "dl bt"
405
406 if $ifmain {
407 reach "main" continue "NONE"
408
409 reach "libfunc" continue "NONE"
410
411 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
412 }
413
414 # Try re-run if the new PIE displacement takes effect.
415 gdb_test "kill" "" "kill" {Kill the program being debugged\? \(y or n\) } "y"
416 reach $solib_bp "run" $displacement
417
418 if $ifmain {
419 test_core $file $displacement
420
421 test_attach $file $displacement
422 }
423
424 if !$trynosym {
425 return
426 }
427
428 with_test_prefix "symbol-less" {
429 # Test also `exec-file'-command loaded $FILE - therefore
430 # without symbols. SYMBOL_OBJFILE is not available and only
431 # EXEC_BFD must be used.
432
433 gdb_exit
434 gdb_start
435 # Clear it to never find any separate debug infos in $debug_root.
436 gdb_test_no_output "set debug-file-directory"
437 gdb_reinitialize_dir $srcdir/$subdir
438
439 # Print the "PIE (Position Independent Executable)
440 # displacement" message.
441 gdb_test_no_output "set verbose on"
442
443 # Test no (error) message has been printed by `exec-file'.
444 set escapedfile [string_to_regexp $file]
445 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
446
447 if $ifmain {
448 reach $solib_bp run $displacement
449
450 # Use two separate gdb_test_multiple statements to avoid timeouts due
451 # to slow processing of wildcard capturing long output
452 set test "info files"
453 set entrynohex ""
454 gdb_test_multiple $test $test {
455 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n" {
456 set entrynohex $expect_out(1,string)
457 gdb_test_multiple "" $test {
458 -re "\r\n$gdb_prompt $" {
459 pass $test
460 }
461 }
462 }
463 }
464
465 # `info sym' cannot be tested for .opd as the binary may not have
466 # symbols.
467 if {[istarget powerpc64-*] && [is_lp64_target]} {
468 set test "convert entry point"
469 gdb_test_multiple "p *(void(*)(void) *) 0x$entrynohex" $test {
470 -re " =( \\(\[^0-9\]*\\))? 0x(\[0-9a-f\]+)( < \[^\r\n\]*)?\r\n$gdb_prompt $" {
471 set entrynohex $expect_out(2,string)
472 pass $test
473 }
474 }
475 }
476 if {$entrynohex != ""} {
477 gdb_test "break *0x$entrynohex" "" "break at entry point"
478 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
479 }
480 } else {
481 # There is no symbol to break at ld.so. Moreover it can
482 # exit with an error code.
483
484 set test "ld.so exit"
485 set test_displacement "seen displacement message as $displacement"
486 gdb_test_multiple "run" $test {
487 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
488 # Missing "$gdb_prompt $" is intentional.
489 if {$expect_out(1,string) == "0x0"} {
490 set case "ZERO"
491 } else {
492 set case "NONZERO"
493 }
494 if {$displacement == $case || $displacement == "PRESENT"} {
495 pass $test_displacement
496 set displacement "FOUND-$displacement"
497 } else {
498 fail $test_displacement
499 }
500 exp_continue
501 }
502 -re "$inferior_exited_re (normally|with code \[0-9\]+).\r\n$gdb_prompt $" {
503 # Do not check the binary filename as it may be truncated.
504 pass $test
505 }
506 }
507 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
508 fail $test_displacement
509 }
510 }
511 }
512 }
513
514 # Create separate binaries for each testcase - to make the possible reported
515 # problem reproducible after the whole test run finishes.
516
517 foreach ldprelink {NO YES} {
518 foreach ldsepdebug {NO IN SEP} {
519 # Skip running the ldsepdebug test if we do not have system separate
520 # debug info available.
521 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
522 continue
523 }
524
525 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
526 set interp $binprefix-$ldname
527
528 # prelink needs to always prelink all the dependencies to do any file
529 # modifications of its files. ld.so also needs all the dependencies to
530 # be prelinked to omit the relocation process. In-memory file offsets
531 # are not dependent whether ld.so went the prelink way or through the
532 # relocation process.
533 #
534 # For GDB we are not interested whether prelink succeeds as it is
535 # transparent to GDB. GDB is being tested for differences of file
536 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
537 # the BIN modification to happen but we need to restore the original
538 # possibly unprelinked ld.so to test all the combinations for GDB.
539 set interp_saved ${interp}-saved
540
541 with_test_prefix "$ldname" {
542 if {$ldsepdebug == "NO"} {
543 file_copy $interp_system $interp
544 # Never call strip-debug before unprelink:
545 # prelink: ...: Section .note.gnu.build-id created after prelinking
546 if ![prelinkNO $interp] {
547 continue
548 }
549 strip_debug $interp
550 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
551 file_copy $interp_system $interp
552 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
553 file_copy $interp_system $interp
554 file_copy $interp_system_debug "${interp}.debug"
555 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
556 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
557 continue
558 }
559 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
560 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
561 verbose -log "command is $command"
562 if [catch $command] {
563 setup_xfail *-*-*
564 fail $test
565 continue
566 } else {
567 pass $test
568 }
569 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
570 file_copy $interp_system $interp
571 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
572 if ![prelinkNO $interp] {
573 continue
574 }
575 gdb_gnu_strip_debug $interp
576 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
577 file_copy $interp_system $interp
578 file_copy $interp_system_debug "${interp}.debug"
579 }
580
581 if {$ldsepdebug == "SEP"} {
582 if ![prelinkNO "${interp}.debug"] {
583 continue
584 }
585 } else {
586 file delete "${interp}.debug"
587 }
588
589 if ![prelink$ldprelink $interp] {
590 continue
591 }
592 if {$ldprelink == "NO"} {
593 set displacement "NONZERO"
594 } else {
595 # x86* kernel loads prelinked PIE binary at its
596 # prelinked address but ppc* kernel loads it at a
597 # random address. prelink normally skips PIE binaries
598 # during the system scan.
599 set displacement "PRESENT"
600 }
601 test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
602
603 if ![file_copy $interp $interp_saved] {
604 continue
605 }
606
607 foreach binprelink {NO YES} {
608 foreach binsepdebug {NO IN SEP} {
609 # "ATTACH" is like "YES" but it is modified during
610 # run. It cannot be used for problem
611 # reproducibility after the testcase ends.
612 foreach binpie {NO YES ATTACH} {
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
622 with_test_prefix "$binname" {
623 set opts "ldflags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
624 if {$binsepdebug != "NO"} {
625 lappend opts {debug}
626 }
627 if {$binpie != "NO"} {
628 lappend opts {additional_flags=-fPIE -pie}
629 }
630
631 set dir ${exec}.d
632 set relink_args [build_executable_own_libs ${test}.exp [file tail $exec] $srcfile $opts $interp $dir]
633 if {$relink_args == ""} {
634 continue
635 }
636
637 if {$binsepdebug == "SEP"} {
638 gdb_gnu_strip_debug $exec
639 }
640
641 if {$binpie == "NO"} {
642 set displacement "NONE"
643 } elseif {$binprelink == "NO"} {
644 set displacement "NONZERO"
645 } else {
646 # x86* kernel loads prelinked PIE binary at its prelinked
647 # address but ppc* kernel loads it at a random address.
648 # prelink normally skips PIE binaries during the system scan.
649 set displacement "PRESENT"
650 }
651
652 if {[prelink$binprelink $relink_args [file tail $exec]]
653 && [file_copy $interp_saved $interp]} {
654 if {$binpie != "ATTACH"} {
655 test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
656 } else {
657 # If the file has been randomly prelinked it must be
658 # "NONZERO". We could see "ZERO" only if it was unprelinked
659 # and it is now running at the same address - which is 0 but
660 # executable can never run at address 0.
661
662 set displacement "NONZERO"
663 test_attach $exec $displacement $relink_args
664
665 # ATTACH means that executables and libraries have been
666 # modified after they have been run. They cannot be reused
667 # for problem reproducibility after the testcase ends in
668 # the ATTACH case. Therefore they are rather deleted not
669 # to confuse after the run finishes.
670 set exec_debug [system_debug_get $exec]
671 if {$exec_debug != ""} {
672 # `file delete [glob "${exec_debug}*"]' does not work.
673 foreach f [glob "${exec_debug}*"] {
674 file delete $f
675 }
676 }
677 file delete -force $dir
678 # `file delete [glob "${exec}*"]' does not work.
679 foreach f [glob "${exec}*"] {
680 file delete $f
681 }
682 }
683 }
684 }
685 }
686 }
687 }
688
689 file delete $interp_saved
690 }
691 }
692 }