]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/dlmopen.exp
gdb, gdbserver: support dlmopen()
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / dlmopen.exp
1 # This testcase is part of GDB, the GNU debugger.
2 #
3 # Copyright 2021-2022 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 #
19 # Test shared libraries loaded into different namespaces with dlmopen().
20 #
21 # We test that GDB shows the correct number of instances of the libraries
22 # the test loaded while unloading them one-by-one.
23
24 if { [skip_dlmopen_tests] } {
25 unsupported "target does not support dlmopen debugging"
26 return -1
27 }
28
29 standard_testfile
30
31 set basename_lib dlmopen-lib
32 set srcfile_lib $srcdir/$subdir/$basename_lib.c
33 set binfile_lib1 [standard_output_file $basename_lib.1.so]
34 set binfile_lib2 [standard_output_file $basename_lib.2.so]
35
36 if { [gdb_compile_shlib $srcfile_lib $binfile_lib1 {debug}] != "" } {
37 untested "failed to prepare shlib"
38 return -1
39 }
40
41 if { [gdb_compile_shlib $srcfile_lib $binfile_lib2 {debug}] != "" } {
42 untested "failed to prepare shlib"
43 return -1
44 }
45
46 if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
47 [list additional_flags=-DDSO1_NAME=\"$binfile_lib1\" \
48 additional_flags=-DDSO2_NAME=\"$binfile_lib2\" \
49 libs=-ldl debug]] } {
50 return -1
51 }
52
53 if { ![runto_main] } {
54 return -1
55 }
56
57 # Check that 'info shared' show NUM occurrences of DSO.
58 proc check_dso_count { dso num } {
59 global gdb_prompt hex
60
61 set count 0
62 gdb_test_multiple "info shared" "info shared" {
63 -re "$hex $hex Yes \[^\r\n\]*$dso\r\n" {
64 # use longer form so debug remote does not interfere
65 set count [expr $count + 1]
66 exp_continue
67 }
68 -re "$gdb_prompt " {
69 verbose -log "library: $dso, expected: $num, found: $count"
70 gdb_assert {$count == $num} "$gdb_test_name"
71 }
72 }
73 }
74
75 # The DSO part of the test. We run it once per DSO call.
76 proc test_dlmopen_one { ndso1 ndso2 } {
77 global srcfile_lib srcfile_lib basename_lib bp_inc
78
79 # Try to reach the breakpoint in the dynamically loaded library.
80 gdb_continue_to_breakpoint "cont to bp.inc" \
81 ".*$srcfile_lib:$bp_inc\r\n.*"
82
83 # We opened all DSOs initially and close them one by one.
84 with_test_prefix "dso 1" { check_dso_count $basename_lib.1.so $ndso1 }
85 with_test_prefix "dso 2" { check_dso_count $basename_lib.2.so $ndso2 }
86
87 # This might help debugging.
88 gdb_test "info breakpoints" ".*"
89 gdb_test "print \$pc" ".*"
90 }
91
92 # The actual test. We run it twice.
93 proc test_dlmopen {} {
94 global srcfile basename_lib bp_main
95
96 with_test_prefix "dlmopen 1" { test_dlmopen_one 3 1 }
97 with_test_prefix "dlmopen 2" { test_dlmopen_one 2 1 }
98 with_test_prefix "dlmopen 3" { test_dlmopen_one 1 1 }
99 with_test_prefix "dlmopen 4" { test_dlmopen_one 0 1 }
100
101 with_test_prefix "main" {
102 # Try to reach the breakpoint in the dynamically loaded library.
103 gdb_continue_to_breakpoint "cont to bp.main" \
104 ".*$srcfile:$bp_main\r\n.*"
105
106 # The library should not be listed.
107 with_test_prefix "dso 1" { check_dso_count $basename_lib.1.so 0 }
108 with_test_prefix "dso 2" { check_dso_count $basename_lib.2.so 0 }
109 }
110 }
111
112 # Remove the pause. We only need it for the attach test.
113 gdb_test "print wait_for_gdb = 0" "\\\$1 = 0"
114
115 # Break in the to-be-loaded library and at the end of main.
116 set bp_inc [gdb_get_line_number "bp.inc" $srcfile_lib]
117 set bp_main [gdb_get_line_number "bp.main" $srcfile]
118
119 delete_breakpoints
120 gdb_breakpoint $srcfile_lib:$bp_inc allow-pending
121 gdb_breakpoint $srcfile:$bp_main
122
123 test_dlmopen
124
125 # Try the same again when attaching after dlmopen().
126 if { ![can_spawn_for_attach] } {
127 unsupported "target does not support attach"
128 return -1
129 }
130
131 clean_restart $binfile
132
133 # Start the test program.
134 set test_spawn_id [spawn_wait_for_attach $binfile]
135 set testpid [spawn_id_get_pid $test_spawn_id]
136
137 # Attach.
138 gdb_test "attach $testpid" "Attaching to program.*, process $testpid.*"
139
140 with_test_prefix "attach" {
141 # Remove the pause. We no longer need it.
142 gdb_test "print wait_for_gdb = 0" "\\\$1 = 0"
143
144 # Set the same breakpoints again. This time, however, we do not allow the
145 # breakpoint to be pending since the library has already been loaded.
146 gdb_breakpoint $srcfile_lib:$bp_inc
147 gdb_breakpoint $srcfile:$bp_main
148
149 test_dlmopen
150 }