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