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