aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/tls-dlobj.exp
blob: 02f2ff81219f56eb24bc612131d2c34177d7550f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Copyright 2024 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# Test that the GDB-internal TLS link map to module id mapping code
# works correctly when debugging a program which is linked against
# shared objects and which also loads and unloads other shared objects
# in different orders.  For targets which have GDB-internal TLS
# support, it'll check both GDB-internal TLS support as well as that
# provided by a helper library such as libthread_db.

source $srcdir/$subdir/tls-common.exp.tcl

require allow_shlib_tests

standard_testfile

set libsrc "${srcdir}/${subdir}/${testfile}-lib.c"

# These will be dlopen'd:
set lib1obj [standard_output_file "${testfile}1-lib.so"]
set lib2obj [standard_output_file "${testfile}2-lib.so"]
set lib3obj [standard_output_file "${testfile}3-lib.so"]
set lib4obj [standard_output_file "${testfile}4-lib.so"]

# These will be dynamically linked with the main program:
set lib10obj [standard_output_file "${testfile}10-lib.so"]
set lib11obj [standard_output_file "${testfile}11-lib.so"]

# Due to problems with some versions of glibc, we expect some tests to
# fail due to TLS storage not being allocated/initialized.  Test
# command CMD using regular expression RE, and use XFAIL instead of
# FAIL when the relevant RE is matched and COND is true when evaluated
# in the upper level.

proc gdb_test_with_xfail { cmd re cond} {
    gdb_test_multiple $cmd $cmd {
	-re -wrap $re {
	    pass $gdb_test_name
	}
	-re -wrap "The inferior has not yet allocated storage for thread-local variables.*" {
	    if [ uplevel 1 [list expr $cond]] {
		xfail $gdb_test_name
	    } else {
		fail $gdb_test_name
	    }
	}
    }
}

proc do_tests {force_internal_tls} {
    clean_restart $::binfile
    if ![runto_main] {
	return
    }

    if $force_internal_tls {
	gdb_test_no_output "maint set force-internal-tls-address-lookup on"
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-1"]
    gdb_continue_to_breakpoint "main-breakpoint-1"

    with_test_prefix "before assignments" {
	gdb_test "print tls_main_tbss_1" ".* = 0"
	gdb_test "print tls_main_tbss_2" ".* = 0"
	gdb_test "print tls_main_tdata_1" ".* = 96"
	gdb_test "print tls_main_tdata_2" ".* = 97"

	# For these tests, where we're attempting to access TLS vars
	# in a dlopen'd library, but before assignment to any of the
	# vars, so it could happen that storage hasn't been allocated
	# yet.  But it might also work.  (When testing against MUSL,
	# things just work; GLIBC ends to produce the TLS error.) So
	# accept either the right answer or a TLS error message.

	set tlserr "The inferior has not yet allocated storage for thread-local variables.*"
	foreach n {1 2 3 4} {
	    gdb_test "print tls_lib${n}_tbss_1" \
		     "0|${tlserr}"
	    gdb_test "print tls_lib${n}_tbss_2" \
		     "0|${tlserr}"
	    gdb_test "print tls_lib${n}_tdata_1" \
		     "96|${tlserr}"
	    gdb_test "print tls_lib${n}_tdata_2" \
		     "97|${tlserr}"
	}
	foreach n {10 11} {
	    gdb_test "print tls_lib${n}_tbss_1" ".* = 0"
	    gdb_test "print tls_lib${n}_tbss_2" ".* = 0"
	    gdb_test "print tls_lib${n}_tdata_1" ".* = ${n}96"
	    gdb_test "print tls_lib${n}_tdata_2" ".* = ${n}97"
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-2"]
    gdb_continue_to_breakpoint "main-breakpoint-2"

    with_test_prefix "at main-breakpoint-2" {
	gdb_test "print tls_main_tbss_1" ".* = 11"
	gdb_test "print tls_main_tbss_2" ".* = 12"
	gdb_test "print tls_main_tdata_1" ".* = 13"
	gdb_test "print tls_main_tdata_2" ".* = 14"

	foreach n {1 2 3 4 10 11} {
	    gdb_test "print tls_lib${n}_tbss_1" ".* = ${n}11"
	    gdb_test "print tls_lib${n}_tbss_2" ".* = ${n}12"
	    gdb_test "print tls_lib${n}_tdata_1" ".* = ${n}13"
	    gdb_test "print tls_lib${n}_tdata_2" ".* = ${n}14"
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-3"]
    gdb_continue_to_breakpoint "main-breakpoint-3"

    # At this point lib2 and lib3 have been unloaded.  Also, TLS vars
    # in remaining libraries have been changed.

    with_test_prefix "at main-breakpoint-3" {
	gdb_test "print tls_main_tbss_1" ".* = 21"
	gdb_test "print tls_main_tbss_2" ".* = 22"
	gdb_test "print tls_main_tdata_1" ".* = 23"
	gdb_test "print tls_main_tdata_2" ".* = 24"

	foreach n {1 4 10 11} {
	    gdb_test "print tls_lib${n}_tbss_1" ".* = ${n}21"
	    gdb_test "print tls_lib${n}_tbss_2" ".* = ${n}22"
	    gdb_test "print tls_lib${n}_tdata_1" ".* = ${n}23"
	    gdb_test "print tls_lib${n}_tdata_2" ".* = ${n}24"
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-4"]
    gdb_continue_to_breakpoint "main-breakpoint-4"

    # lib3 has been loaded again; lib2 is the only one not loaded.

    with_test_prefix "at main-breakpoint-4" {
	gdb_test "print tls_main_tbss_1" ".* = 31"
	gdb_test "print tls_main_tbss_2" ".* = 32"
	gdb_test "print tls_main_tdata_1" ".* = 33"
	gdb_test "print tls_main_tdata_2" ".* = 34"

	set cond { $n == 3 }
	foreach n {1 3 4 10 11} {
	    gdb_test_with_xfail "print tls_lib${n}_tbss_1" ".* = ${n}31" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tbss_2" ".* = ${n}32" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_1" ".* = ${n}33" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_2" ".* = ${n}34" $cond
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-5"]
    gdb_continue_to_breakpoint "main-breakpoint-5"

    # lib2 and lib3 are loaded; lib1 and lib4 are not.

    with_test_prefix "at main-breakpoint-5" {
	gdb_test "print tls_main_tbss_1" ".* = 41"
	gdb_test "print tls_main_tbss_2" ".* = 42"
	gdb_test "print tls_main_tdata_1" ".* = 43"
	gdb_test "print tls_main_tdata_2" ".* = 44"

	set cond { $n == 2 || $n == 3 }
	foreach n {2 3 10 11} {
	    gdb_test_with_xfail "print tls_lib${n}_tbss_1" ".* = ${n}41" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tbss_2" ".* = ${n}42" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_1" ".* = ${n}43" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_2" ".* = ${n}44" $cond
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-6"]
    gdb_continue_to_breakpoint "main-breakpoint-6"

    # lib1, lib3 and lib4 are loaded; lib2 is not loaded.

    with_test_prefix "at main-breakpoint-6" {
	gdb_test "print tls_main_tbss_1" ".* = 51"
	gdb_test "print tls_main_tbss_2" ".* = 52"
	gdb_test "print tls_main_tdata_1" ".* = 53"
	gdb_test "print tls_main_tdata_2" ".* = 54"

	set cond { $n == 1 || $n == 3 || $n == 4}
	foreach n {1 3 4 10 11} {
	    gdb_test_with_xfail "print tls_lib${n}_tbss_1" ".* = ${n}51" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tbss_2" ".* = ${n}52" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_1" ".* = ${n}53" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_2" ".* = ${n}54" $cond
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-7"]
    gdb_continue_to_breakpoint "main-breakpoint-7"

    # lib2 and lib3 are loaded; lib1 and lib4 are not.

    with_test_prefix "at main-breakpoint-7" {
	gdb_test "print tls_main_tbss_1" ".* = 61"
	gdb_test "print tls_main_tbss_2" ".* = 62"
	gdb_test "print tls_main_tdata_1" ".* = 63"
	gdb_test "print tls_main_tdata_2" ".* = 64"

	set cond { $n == 2 || $n == 3 }
	foreach n {2 3 10 11} {
	    gdb_test_with_xfail "print tls_lib${n}_tbss_1" ".* = ${n}61" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tbss_2" ".* = ${n}62" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_1" ".* = ${n}63" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_2" ".* = ${n}64" $cond
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-8"]
    gdb_continue_to_breakpoint "main-breakpoint-8"

    # lib1, lib2, lib3, and lib4 are all loaded.

    with_test_prefix "at main-breakpoint-8" {
	gdb_test "print tls_main_tbss_1" ".* = 71"
	gdb_test "print tls_main_tbss_2" ".* = 72"
	gdb_test "print tls_main_tdata_1" ".* = 73"
	gdb_test "print tls_main_tdata_2" ".* = 74"

	foreach n {1 2 3 4 10 11} {
	    gdb_test "print tls_lib${n}_tbss_1" ".* = ${n}71"
	    gdb_test "print tls_lib${n}_tbss_2" ".* = ${n}72"
	    gdb_test "print tls_lib${n}_tdata_1" ".* = ${n}73"
	    gdb_test "print tls_lib${n}_tdata_2" ".* = ${n}74"
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-9"]
    gdb_continue_to_breakpoint "main-breakpoint-9"

    # lib2 is loaded; lib1, lib3, and lib4 are not.

    with_test_prefix "at main-breakpoint-9" {
	gdb_test "print tls_main_tbss_1" ".* = 81"
	gdb_test "print tls_main_tbss_2" ".* = 82"
	gdb_test "print tls_main_tdata_1" ".* = 83"
	gdb_test "print tls_main_tdata_2" ".* = 84"

	foreach n {2 10 11} {
	    gdb_test "print tls_lib${n}_tbss_1" ".* = ${n}81"
	    gdb_test "print tls_lib${n}_tbss_2" ".* = ${n}82"
	    gdb_test "print tls_lib${n}_tdata_1" ".* = ${n}83"
	    gdb_test "print tls_lib${n}_tdata_2" ".* = ${n}84"
	}
    }

    gdb_breakpoint [gdb_get_line_number "main-breakpoint-10"]
    gdb_continue_to_breakpoint "main-breakpoint-10"

    # lib3 and lib4 are loaded; lib1 and lib2 are not.

    with_test_prefix "at main-breakpoint-10" {
	gdb_test "print tls_main_tbss_1" ".* = 91"
	gdb_test "print tls_main_tbss_2" ".* = 92"
	gdb_test "print tls_main_tdata_1" ".* = 93"
	gdb_test "print tls_main_tdata_2" ".* = 94"

	set cond { $n == 3 || $n == 4 }
	foreach n {3 4 10 11} {
	    gdb_test_with_xfail "print tls_lib${n}_tbss_1" ".* = ${n}91" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tbss_2" ".* = ${n}92" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_1" ".* = ${n}93" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_2" ".* = ${n}94" $cond
	}
    }

    # gdb_interact

    set corefile ${::binfile}.core
    set core_supported 0
    if { ![is_remote host] } {
	set core_supported [gdb_gcore_cmd $corefile "save corefile"]
    }

    # Finish test early if no core file was made.
    if !$core_supported {
	return
    }

    clean_restart $::binfile

    set core_loaded [gdb_core_cmd $corefile "load corefile"]
    if { $core_loaded == -1 } {
	return
    }

    with_test_prefix "core file" {
	if $force_internal_tls {
	    gdb_test_no_output "maint set force-internal-tls-address-lookup on"
	}

	gdb_test "print tls_main_tbss_1" ".* = 91"
	gdb_test "print tls_main_tbss_2" ".* = 92"
	gdb_test "print tls_main_tdata_1" ".* = 93"
	gdb_test "print tls_main_tdata_2" ".* = 94"

	set cond { $n == 3 || $n == 4 }
	foreach n {3 4 10 11} {
	    gdb_test_with_xfail "print tls_lib${n}_tbss_1" ".* = ${n}91" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tbss_2" ".* = ${n}92" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_1" ".* = ${n}93" $cond
	    gdb_test_with_xfail "print tls_lib${n}_tdata_2" ".* = ${n}94" $cond
	}
    }
}

# Build shared objects for dlopen:
if { [gdb_compile_shlib $libsrc $lib1obj [list debug additional_flags=-DN=1]] != "" } {
    untested "failed to compile shared object"
    return -1
}
if { [gdb_compile_shlib $libsrc $lib2obj [list debug additional_flags=-DN=2]] != "" } {
    untested "failed to compile shared object"
    return -1
}
if { [gdb_compile_shlib $libsrc $lib3obj [list debug additional_flags=-DN=3]] != "" } {
    untested "failed to compile shared object"
    return -1
}
if { [gdb_compile_shlib $libsrc $lib4obj [list debug additional_flags=-DN=4]] != "" } {
    untested "failed to compile shared object"
    return -1
}

# Build shared objects to link against main program:
if { [gdb_compile_shlib $libsrc $lib10obj [list debug additional_flags=-DN=10]] != "" } {
    untested "failed to compile shared object"
    return -1
}
if { [gdb_compile_shlib $libsrc $lib11obj [list debug additional_flags=-DN=11]] != "" } {
    untested "failed to compile shared object"
    return -1
}

# Use gdb_compile_pthreads to build and link the main program for
# testing.  It's also possible to run the tests using plain old
# gdb_compile, but this adds complexity with setting up additional
# KFAILs.  (When run using GLIBC versions earlier than 2.34, a program
# that's not dynamically linked against libpthread will lack a working
# libthread_db, and, therefore, won't be able to access thread local
# storage without GDB-internal TLS support.  Additional complications
# arise from when testing on x86_64 with -m32, which tends to work
# okay on GLIBC 2.34 and newer, but not older versions.  It gets messy
# to properly sort out all of these cases.)
#
# This test was originally written to do it both ways, i.e. with both
# both gdb_compile and gdb_compile_pthreads, but the point of this
# test is to check that the link map address to TLS module id mapping
# code works correctly in programs which use lots of dlopen and
# dlclose calls in various orders - and that can be done using just
# gdb_compile_pthreads.

if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable \
			    [list debug shlib_load \
				  shlib=${lib10obj} \
				  shlib=${lib11obj} \
				  additional_flags=-DOBJ1=\"${lib1obj}\" \
				  additional_flags=-DOBJ2=\"${lib2obj}\" \
				  additional_flags=-DOBJ3=\"${lib3obj}\" \
				  additional_flags=-DOBJ4=\"${lib4obj}\" \
								]] != "" } {
    untested "failed to compile"
} else {
    foreach_with_prefix force_internal_tls $internal_tls_iters {
	do_tests $force_internal_tls
    }
}