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     # We want to test the re-run of a PIE in the case where the executable
341     # is loaded with a different displacement, but disable-randomization
342     # prevents that from happening.  So turn it off.
343     gdb_test "set disable-randomization off"
344
345     reach "dl_main" "run segv" $displacement
346
347     gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
348
349     if $ifmain {
350         reach "main" continue "NONE"
351
352         reach "libfunc" continue "NONE"
353
354         gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
355     }
356
357     # Try re-run if the new PIE displacement takes effect.
358     gdb_test "kill" "" "kill" {Kill the program being debugged\? \(y or n\) } "y"
359     reach "dl_main" "run segv" $displacement
360
361     if $ifmain {
362         test_core $file $displacement
363
364         test_attach $file $displacement
365     }
366
367     if !$trynosym {
368         return
369     }
370
371     global pf_prefix
372     set old_ldprefix $pf_prefix
373     lappend pf_prefix "symbol-less:"
374
375     # Test also `exec-file'-command loaded $FILE - therefore without symbols.
376     # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
377
378     gdb_exit
379     gdb_start
380     # Clear it to never find any separate debug infos in $debug_root.
381     gdb_test_no_output "set debug-file-directory"
382     gdb_reinitialize_dir $srcdir/$subdir
383
384     # Print the "PIE (Position Independent Executable) displacement" message.
385     gdb_test_no_output "set verbose on"
386
387     # Test no (error) message has been printed by `exec-file'.
388     set escapedfile [string_to_regexp $file]
389     gdb_test "exec-file $file" "exec-file $escapedfile" "load"
390
391     if $ifmain {
392         reach "dl_main" run $displacement
393
394         set test "info files"
395         set entrynohex ""
396         gdb_test_multiple $test $test {
397             -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
398                 set entrynohex $expect_out(1,string)
399                 pass $test
400             }
401         }
402         # `info sym' cannot be tested for .opd as the binary may not have
403         # symbols.
404         if [istarget powerpc64-*] {
405             set test "convert entry point"
406             gdb_test_multiple "p *(void(*)(void) *) 0x$entrynohex" $test {
407                 -re " =( \\(\[^0-9\]*\\))? 0x(\[0-9a-f\]+)( < \[^\r\n\]*)?\r\n$gdb_prompt $" {
408                     set entrynohex $expect_out(2,string)
409                     pass $test
410                 }
411             }
412         }
413         if {$entrynohex != ""} {
414             gdb_test "break *0x$entrynohex" "" "break at entry point"
415             gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
416         }
417     } else {
418         # There is no symbol to break at ld.so.  Moreover it can exit with an
419         # error code.
420
421         set test "ld.so exit"
422         set test_displacement "seen displacement message as $displacement"
423         gdb_test_multiple "run" $test {
424             -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
425                 # Missing "$gdb_prompt $" is intentional.
426                 if {$expect_out(1,string) == "0x0"} {
427                     set case "ZERO"
428                 } else {
429                     set case "NONZERO"
430                 }
431                 if {$displacement == $case} {
432                     pass $test_displacement
433                     set displacement "FOUND-$displacement"
434                 } else {
435                     fail $test_displacement
436                 }
437                 exp_continue
438             }
439             -re "Program exited (normally|with code \[0-9\]+)\\.\r\n$gdb_prompt $" {
440                 # Do not check the binary filename as it may be truncated.
441                 pass $test
442             }
443         }
444         if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
445             fail $test_displacement
446         }
447     }
448
449     set pf_prefix $old_ldprefix
450 }
451
452 # Create separate binaries for each testcase - to make the possible reported
453 # problem reproducible after the whole test run finishes.
454
455 set old_ldprefix $pf_prefix
456 foreach ldprelink {NO YES} {
457     foreach ldsepdebug {NO IN SEP} {
458         # Skip running the ldsepdebug test if we do not have system separate
459         # debug info available.
460         if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
461             continue
462         }
463
464         set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
465         set interp $binprefix-$ldname
466
467         # prelink needs to always prelink all the dependencies to do any file
468         # modifications of its files.  ld.so also needs all the dependencies to
469         # be prelinked to omit the relocation process.  In-memory file offsets
470         # are not dependent whether ld.so went the prelink way or through the
471         # relocation process.
472         #
473         # For GDB we are not interested whether prelink succeeds as it is
474         # transparent to GDB.  GDB is being tested for differences of file
475         # offsets vs. in-memory offsets.  So we have to prelink even ld.so for
476         # the BIN modification to happen but we need to restore the original
477         # possibly unprelinked ld.so to test all the combinations for GDB.
478         set interp_saved ${interp}-saved
479
480         set pf_prefix $old_ldprefix
481         lappend pf_prefix "$ldname:"
482
483         if {$ldsepdebug == "NO"} {
484             file_copy $interp_system $interp
485             # Never call strip-debug before unprelink:
486             # prelink: ...: Section .note.gnu.build-id created after prelinking
487             if ![prelinkNO $interp] {
488                 continue
489             }
490             strip_debug $interp
491         } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
492             file_copy $interp_system $interp
493         } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
494             file_copy $interp_system $interp
495             file_copy $interp_system_debug "${interp}.debug"
496             # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
497             if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
498                 continue
499             }
500             set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
501             set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
502             verbose -log "command is $command"
503             if [catch $command] {
504                 setup_xfail *-*-*
505                 fail $test
506                 continue
507             } else {
508                 pass $test
509             }
510         } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
511             file_copy $interp_system $interp
512             # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
513             if ![prelinkNO $interp] {
514                 continue
515             }
516             gdb_gnu_strip_debug $interp
517         } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
518             file_copy $interp_system $interp
519             file_copy $interp_system_debug "${interp}.debug"
520         }
521
522         if {$ldsepdebug == "SEP"} {
523             if ![prelinkNO "${interp}.debug"] {
524                 continue
525             }
526         } else {
527             file delete "${interp}.debug"
528         }
529
530         if ![prelink$ldprelink $interp] {
531             continue
532         }
533         if {$ldprelink == "NO"} {
534             set displacement "NONZERO"
535         } else {
536             set displacement "ZERO"
537         }
538         test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
539
540         if ![file_copy $interp $interp_saved] {
541             continue
542         }
543         set old_binprefix $pf_prefix
544         foreach binprelink {NO YES} {
545             foreach binsepdebug {NO IN SEP} {
546                 # "ATTACH" is like "YES" but it is modified during run.
547                 # It cannot be used for problem reproducibility after the
548                 # testcase ends.
549                 foreach binpie {NO YES ATTACH} {
550                     # This combination is not possible, non-PIE (fixed address)
551                     # binary cannot be prelinked to any (other) address.
552                     if {$binprelink == "YES" && $binpie == "NO"} {
553                         continue
554                     }
555
556                     set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
557                     set exec $binprefix-$binname
558
559                     set pf_prefix $old_binprefix
560                     lappend pf_prefix "$binname:"
561
562                     set opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
563                     if {$binsepdebug != "NO"} {
564                         lappend opts {debug}
565                     }
566                     if {$binpie != "NO"} {
567                         lappend opts {additional_flags=-fPIE -pie}
568                     }
569
570                     set dir ${exec}.d
571                     set relink_args [build_executable_own_libs ${test}.exp [file tail $exec] $srcfile $opts $interp $dir]
572                     if {$relink_args == ""} {
573                         continue;
574                     }
575
576                     if {$binsepdebug == "SEP"} {
577                         gdb_gnu_strip_debug $exec
578                     }
579
580                     if {$binpie == "NO"} {
581                         set displacement "NONE"
582                     } elseif {$binprelink == "NO"} {
583                         set displacement "NONZERO"
584                     } else {
585                         set displacement "ZERO"
586                     }
587
588                     if {[prelink$binprelink $relink_args [file tail $exec]]
589                         && [file_copy $interp_saved $interp]} {
590                         if {$binpie != "ATTACH"} {
591                             test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
592                         } else {
593                             # If the file has been randomly prelinked it must
594                             # be "NONZERO".  We could see "ZERO" only if it was
595                             # unprelinked and it is now running at the same
596                             # address - which is 0 but executable can never run
597                             # at address 0.
598
599                             set displacement "NONZERO"
600                             test_attach $exec $displacement $relink_args
601
602                             # ATTACH means that executables and libraries have
603                             # been modified after they have been run.  They
604                             # cannot be reused for problem reproducibility after
605                             # the testcase ends in the ATTACH case.  Therefore
606                             # they are rather deleted not to confuse after the
607                             # run finishes.
608                             set exec_debug [system_debug_get $exec]
609                             if {$exec_debug != ""} {
610                                 # `file delete [glob "${exec_debug}*"]' does not work.
611                                 foreach f [glob "${exec_debug}*"] {
612                                     file delete $f
613                                 }
614                             }
615                             file delete -force $dir
616                             # `file delete [glob "${exec}*"]' does not work.
617                             foreach f [glob "${exec}*"] {
618                                 file delete $f
619                             }
620                         }
621                     }
622                 }
623             }
624         }
625
626         file delete $interp_saved
627     }
628 }
629 set pf_prefix $old_ldprefix