aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/py-missing-debug.exp
blob: 0aa4aaa010b0f9ce92692f57ed577e7774f6c30c (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Copyright (C) 2023-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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

load_lib gdb-python.exp

require allow_python_tests

standard_testfile

if {[build_executable "failed to prepare" ${testfile} ${srcfile}]} {
    return -1
}

# Remove debug information from BINFILE and place it into
# BINFILE.debug.
if {[gdb_gnu_strip_debug $binfile]} {
    unsupported "cannot produce separate debug info files"
    return -1
}

set remote_python_file \
    [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]

set debug_filename ${binfile}.debug
set hidden_filename ${binfile}.hidden

# Start GDB.
clean_restart

# Some initial sanity checks; initially, we can find the debug information
# (this will use the .gnu_debuglink), then after we move the debug
# information, reload the executable, now the debug can't be found.
with_test_prefix "initial checks" {
    # Load BINFILE, we should find the separate debug information.
    gdb_file_cmd $binfile
    gdb_assert {$gdb_file_cmd_debug_info == "debug"} \
	"debug info is found"

    # Rename the debug information file, re-load BINFILE, GDB should fail
    # to find the debug information
    remote_exec build "mv $debug_filename $hidden_filename"
    gdb_file_cmd $binfile
    gdb_assert {$gdb_file_cmd_debug_info == "nodebug"} \
	"debug info no longer found"
}

# Load the Python script into GDB.
gdb_test "source $remote_python_file" "^Success" \
    "source python script"

# Setup the separate debug info directory.  This isn't actually needed until
# some of the later tests, but might as well get this done now.
set debug_directory [standard_output_file "debug-dir"]
remote_exec build "mkdir -p $debug_directory"
gdb_test_no_output "set debug-file-directory $debug_directory" \
    "set debug-file-directory"

# Initially the missing debug handler we install is in a mode where it
# returns None, indicating that it can't help locate the debug information.
# Check this works as expected.
with_test_prefix "handler returning None" {
    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(None, handler_obj)" \
	"register the initial handler"

    gdb_file_cmd $binfile
    gdb_assert {$gdb_file_cmd_debug_info == "nodebug"} \
	"debug info not found"

    # Check the handler was only called once.
    gdb_test "python print(handler_obj.call_count)" "^1" \
	"check handler was only called once"
}

# Now configure the handler to move the debug file back to the
# .gnu_debuglink location and then return True, this will cause GDB to
# recheck, at which point it should find the debug info.
with_test_prefix "handler in gnu_debuglink mode" {
    gdb_test_no_output "python handler_obj.set_mode(Mode.RETURN_TRUE, \
						    \"$hidden_filename\", \
						    \"$debug_filename\")" \
	"confirgure handler"
    gdb_file_cmd $binfile
    gdb_assert {$gdb_file_cmd_debug_info == "debug"} "debug info found"

    # Check the handler was only called once.
    gdb_test "python print(handler_obj.call_count)" "^1" \
	"check handler was only called once"
}

# Setup a directory structure based on the build-id of BINFILE, but don't
# move the debug information into place just yet.
#
# Instead, configure the handler to move the debug info into the build-id
# directory.
#
# Reload BINFILE, at which point the handler will move the debug info into
# the build-id directory and return True, GDB will then recheck for the
# debug information, and should find it.
with_test_prefix "handler in build-id mode" {
    # Move the debug file out of the way once more.
    remote_exec build "mv $debug_filename $hidden_filename"

    # Create the build-id based directory in which the debug information
    # will be placed.
    set build_id_filename \
	$debug_directory/[build_id_debug_filename_get $binfile]
    remote_exec build "mkdir -p [file dirname $build_id_filename]"

    # Configure the handler to move the debug info into the build-id dir.
    gdb_test_no_output "python handler_obj.set_mode(Mode.RETURN_TRUE, \
						    \"$hidden_filename\", \
						    \"$build_id_filename\")" \
	"confirgure handler"

    # Reload the binary and check the debug information is found.
    gdb_file_cmd $binfile
    gdb_assert {$gdb_file_cmd_debug_info == "debug"} "debug info found"

    # Check the handler was only called once.
    gdb_test "python print(handler_obj.call_count)" "^1" \
	"check handler was only called once"
}

# Move the debug information back to a hidden location and configure the
# handler to return the filename of the hidden debug info location.  GDB
# should immediately use this file as the debug information.
with_test_prefix "handler returning a string" {
    remote_exec build "mv $build_id_filename $hidden_filename"

    # Configure the handler return a filename string.
    gdb_test_no_output "python handler_obj.set_mode(Mode.RETURN_STRING, \
						    \"$hidden_filename\")" \
	"confirgure handler"

    # Reload the binary and check the debug information is found.
    gdb_file_cmd $binfile
    gdb_assert {$gdb_file_cmd_debug_info == "debug"} "debug info found"

    # Check the handler was only called once.
    gdb_test "python print(handler_obj.call_count)" "^1" \
	"check handler was only called once"
}

# Register another global handler, this one raises an exception.  Reload the
# debug information, the bad handler should be invoked first, which raises
# an excetption, at which point GDB should skip further Python handlers.
with_test_prefix "handler raises an exception" {
    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(None, rhandler)"

    foreach_with_prefix exception_type {gdb.GdbError TypeError} {
	gdb_test_no_output \
	    "python rhandler.exception_type = $exception_type"

	gdb_file_cmd $binfile
	gdb_assert {$gdb_file_cmd_debug_info == "nodebug"} \
	    "debug info not found"

	set re [string_to_regexp \
		    "Python Exception <class '$exception_type'>: message"]
	gdb_assert {[regexp $re $gdb_file_cmd_msg]} \
	    "check for exception in file command output"

	# Our original handler is still registered, but should not have been
	# called again (as the exception occurs first).
	gdb_test "python print(handler_obj.call_count)" "^1" \
	    "check good handler hasn't been called again"
    }
}

gdb_test "info missing-debug-handlers" \
    [multi_line \
	 "Global:" \
	 "  exception_handler" \
	 "  handler"] \
    "check both handlers are visible"

# Re-start GDB.
clean_restart

# Load the Python script into GDB.
gdb_test "source $remote_python_file" "^Success" \
    "source python script for bad handler name checks"

# Attempt to register a missing-debug-handler with NAME.  The expectation is
# that this should fail as NAME contains some invalid characters.
proc check_bad_name {name} {
    set name_re [string_to_regexp $name]
    set re \
	[multi_line \
	     "ValueError: invalid character '.' in handler name: $name_re" \
	     "Error while executing Python code\\."]

    gdb_test "python register(\"$name\")" $re \
	"check that '$name' is not accepted"
}

# We don't attempt to be exhaustive here, just check a few random examples
# of invalid names.
check_bad_name "!! Bad Name"
check_bad_name "Bad Name"
check_bad_name "(Bad Name)"
check_bad_name "Bad \[Name\]"
check_bad_name "Bad,Name"
check_bad_name "Bad;Name"

# Check that there are no handlers registered.
gdb_test_no_output "info missing-debug-handlers" \
    "check no handlers are registered"

# Check we can use the enable/disable commands where there are no handlers
# registered.
gdb_test "enable missing-debug-handler foo" \
    "^0 missing debug handlers enabled"
gdb_test "disable missing-debug-handler foo" \
    "^0 missing debug handlers disabled"

# Grab the current program space object, used for registering handler later.
gdb_test_no_output "python pspace = gdb.selected_inferior().progspace"

# Now register some handlers.
foreach hspec {{\"Foo\" None}
    {\"-bar\" None}
    {\"baz-\" pspace}
    {\"abc-def\" pspace}} {
    lassign $hspec name locus
    gdb_test "python register($name, $locus)"
}

with_test_prefix "all handlers enabled" {
    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Current Progspace:" \
	     "  abc-def" \
	     "  baz-" \
	     "Global:" \
	     "  -bar" \
	     "  Foo"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['abc-def', 'baz-', '-bar', 'Foo']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

with_test_prefix "disable 'baz-'" {
    gdb_test "disable missing-debug-handler progspace baz-" \
	"^1 missing debug handler disabled"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  abc-def" \
	     "  baz- \\\[disabled\\\]" \
	     "Global:" \
	     "  -bar" \
	     "  Foo"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['abc-def', '-bar', 'Foo']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

with_test_prefix "disable 'Foo'" {
    gdb_test "disable missing-debug-handler .* Foo" \
	"^1 missing debug handler disabled"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  abc-def" \
	     "  baz- \\\[disabled\\\]" \
	     "Global:" \
	     "  -bar" \
	     "  Foo \\\[disabled\\\]"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['abc-def', '-bar']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

with_test_prefix "disable everything" {
    gdb_test "disable missing-debug-handler .* .*" \
	"^2 missing debug handlers disabled"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  abc-def \\\[disabled\\\]" \
	     "  baz- \\\[disabled\\\]" \
	     "Global:" \
	     "  -bar \\\[disabled\\\]" \
	     "  Foo \\\[disabled\\\]"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {[]}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

with_test_prefix "enable 'abc-def'" {
    set re [string_to_regexp $binfile]

    gdb_test "enable missing-debug-handler \"$re\" abc-def" \
	"^1 missing debug handler enabled" \
	"enable missing-debug-handler"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  abc-def" \
	     "  baz- \\\[disabled\\\]" \
	     "Global:" \
	     "  -bar \\\[disabled\\\]" \
	     "  Foo \\\[disabled\\\]"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['abc-def']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

with_test_prefix "enable global handlers" {
    set re [string_to_regexp $binfile]

    gdb_test "enable missing-debug-handler global" \
	"^2 missing debug handlers enabled"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  abc-def" \
	     "  baz- \\\[disabled\\\]" \
	     "Global:" \
	     "  -bar" \
	     "  Foo"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['abc-def', '-bar', 'Foo']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

# Add handler_obj to the global handler list, and configure it to
# return False.  We should call all of the program space specific
# handlers (which return None), and then call handler_obj from the
# global list, which returns False, at which point we shouldn't call
# anyone else.
with_test_prefix "return False handler in progspace list" {
    gdb_test "enable missing-debug-handler progspace" \
	"^1 missing debug handler enabled"

    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(None, handler_obj)" \
	"register the initial handler"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  abc-def" \
	     "  baz-" \
	     "Global:" \
	     "  handler" \
	     "  -bar" \
	     "  Foo"]

    gdb_test_no_output "python handler_obj.set_mode(Mode.RETURN_FALSE)" \
	"confirgure handler"

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['abc-def', 'baz-', 'handler']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

# Now add handler_obj to the current program space's handler list.  We
# use the same handler object here, that's fine.  We should only see a
# call to the first handler object in the call log.
with_test_prefix "return False handler in global list" {
    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(pspace, handler_obj)" \
	"register the initial handler"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  handler" \
	     "  abc-def" \
	     "  baz-" \
	     "Global:" \
	     "  handler" \
	     "  -bar" \
	     "  Foo"]

    gdb_file_cmd $binfile
    gdb_test "python print(handler_call_log)" \
	[string_to_regexp {['handler']}]
    gdb_test_no_output "python handler_call_log = \[\]" \
	"reset call log"
}

with_test_prefix "check handler replacement" {
    # First, check we can have the same name appear in both program
    # space and global lists without giving an error.
    gdb_test_no_output "python register(\"Foo\", pspace)"

    gdb_test "info missing-debug-handlers" \
	[multi_line \
	     "Progspace \[^\r\n\]+:" \
	     "  Foo" \
	     "  handler" \
	     "  abc-def" \
	     "  baz-" \
	     "Global:" \
	     "  handler" \
	     "  -bar" \
	     "  Foo"]

    # Now check that we get an error if we try to add a handler with
    # the same name.
    gdb_test "python gdb.missing_debug.register_handler(pspace, log_handler(\"Foo\"))" \
	[multi_line \
	     "RuntimeError: Handler Foo already exists\\." \
	     "Error while executing Python code\\."]

    gdb_test "python gdb.missing_debug.register_handler(handler=log_handler(\"Foo\"), locus=pspace)" \
	[multi_line \
	     "RuntimeError: Handler Foo already exists\\." \
	     "Error while executing Python code\\."]

    # And now try again, but this time with 'replace=True', we
    # shouldn't get an error in this case.
    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(pspace, log_handler(\"Foo\"), replace=True)"

    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(handler=log_handler(\"Foo\"), locus=None, replace=True)"

    # Now disable a handler and check we still need to use 'replace=True'.
    gdb_test "disable missing-debug-handler progspace Foo" \
	"^1 missing debug handler disabled"

    gdb_test "python gdb.missing_debug.register_handler(pspace, log_handler(\"Foo\"))" \
	[multi_line \
	     "RuntimeError: Handler Foo already exists\\." \
	     "Error while executing Python code\\."] \
	"still get an error when handler is disabled"

    gdb_test_no_output \
	"python gdb.missing_debug.register_handler(pspace, log_handler(\"Foo\"), replace=True)" \
	"can replace a disabled handler"
}