1# Copyright 1998-2023 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 file was written by Elena Zannoni (ezannoni@cygnus.com)
17
18# this file tests maintenance commands and help on those.
19
20# source file used is break.c
21
22
23#maintenance check-psymtabs -- Check consistency of psymtabs vs symtabs
24#maintenance check-symtabs -- Check consistency of symtabs
25#maintenance expand-symtabs -- Expand symtabs matching a file regexp
26#maintenance set -- Set GDB internal variables used by the GDB maintainer
27#maintenance show -- Show GDB internal variables used by the GDB maintainer
28#maintenance dump-me -- Get fatal error; make debugger dump its core
29#maintenance print -- Maintenance command for printing GDB internal state
30#maintenance info -- Commands for showing internal info about the program being debugged
31#maintenance internal-error -- Give GDB an internal error.
32#
33#maintenance print dummy-frames -- Print the dummy frame stack
34#maintenance print statistics -- Print statistics about internal gdb state
35#maintenance print objfiles -- Print dump of current object file definitions
36#maintenance print psymbols -- Print dump of current partial symbol definitions
37#maintenance print msymbols -- Print dump of current minimal symbol definitions
38#maintenance print symbols -- Print dump of current symbol definitions
39#maintenance print type -- Print a type chain for a given symbol
40#maintenance print unwind -- Print unwind table entry at given address
41#
42#
43#maintenance info breakpoints -- Status of all breakpoints
44#
45
46
47
48standard_testfile break.c break1.c
49
50if {[prepare_for_testing "failed to prepare" $testfile \
51	 [list $srcfile $srcfile2] {debug nowarnings}]} {
52    return -1
53}
54
55set readnow_p [readnow]
56
57# The commands we test here produce many lines of output; disable "press
58# <return> to continue" prompts.
59gdb_test_no_output "set height 0"
60
61# Tests that require that no program is running
62
63gdb_file_cmd ${binfile}
64
65# Test for a regression where this command would internal-error if the
66# program wasn't running.  If there's a lot of registers then this
67# might overflow expect's buffers, so process the output line at a
68# time.
69set saw_registers 0
70set saw_headers 0
71set test "maint print registers"
72gdb_test_multiple $test $test {
73    -re "\[^\r\n\]+Name\[^\r\n\]+Nr\[^\r\n\]+Rel\[^\r\n\]+Offset\[^\r\n\]+Size\[^\r\n\]+Type\[^\r\n\]+\r\n" {
74	set saw_headers 1
75	exp_continue
76    }
77    -re "^\[^\r\n\]+\[0-9\]+\[^\r\n\]+\[0-9\]+\[^\r\n\]+\[0-9\]+\[^\r\n\]+\[0-9\]+\[^\r\n\]+\r\n" {
78        set saw_registers 1
79        exp_continue
80    }
81    -re "^\\*\[0-9\]+\[^\r\n\]+\r\n" {
82        exp_continue
83    }
84    -re "$gdb_prompt $" {
85        gdb_assert { $saw_registers && $saw_headers } $test
86    }
87}
88
89# Test "mt expand-symtabs" here as it's easier to verify before we
90# run the program.
91gdb_test_no_output "mt set per on" "mt set per on for expand-symtabs"
92gdb_test_multiple "mt expand-symtabs $subdir/break\[.\]c$" \
93    "mt expand-symtabs" {
94	-re "#compunits: (1|2) \\(\[+\](0|1|2)\\),.*$gdb_prompt $" {
95	    # This should expand at most two primary symtabs.
96	    # "Normally" it will not expand any, because the symtab
97	    # holding "main" will already have been expanded, but if the
98	    # file is compiled with -fdebug-types-section then a second primary
99	    # symtab for break.c will be created for any types.
100	    pass "$gdb_test_name"
101	}
102	-re "#compunits: ($decimal) \\(\[+\]0\\),.*$gdb_prompt $" {
103	    if { $readnow_p } {
104		pass "$gdb_test_name"
105	    } else {
106		fail "$gdb_test_name"
107	    }
108	}
109    }
110gdb_test "mt set per off" ".*" "mt set per off for expand-symtabs"
111
112# Tests that can or should be done with a running program
113
114gdb_load ${binfile}
115
116if {![runto_main]} {
117    return
118}
119
120# If we're using .gdb_index or .debug_names there will be no psymtabs.
121set have_gdb_index [ exec_has_index_section ${binfile} ]
122
123# There also won't be any psymtabs if we read the index from the index cache.
124# We can detect this by looking if the index-cache is enabled and if the number
125# of cache misses is 0.
126set index_cache_misses -1
127gdb_test_multiple "show index-cache stats" "check index cache stats" {
128    -re ".*Cache misses \\(this session\\): (\\d+)\r\n.*$gdb_prompt $" {
129	set index_cache_misses $expect_out(1,string)
130    }
131}
132
133set using_index_cache 0
134gdb_test_multiple "show index-cache enabled" "check index cache status" {
135    -re ".*is off.\r\n$gdb_prompt $" {
136	set using_index_cache 0
137    }
138    -re ".*is on.\r\n$gdb_prompt $" {
139	set using_index_cache 1
140    }
141}
142
143if { $index_cache_misses == 0 && $using_index_cache } {
144    set have_gdb_index 1
145}
146
147set have_psyms [expr ! ( $have_gdb_index || $readnow_p )]
148
149#
150# this command does not produce any output
151# unless there is some problem with the symtabs and psymtabs
152# so that branch will really never be covered in this tests here!!
153#
154# When there is a problem, there may be loads of output, which can
155# overwhelm the expect buffer. Splitting it seems to fix those
156# issues.
157
158set seen_command false
159gdb_test_multiple "maint check-psymtabs" "" {
160    -re "^maint check-psymtabs\r\n" {
161	set seen_command true
162	exp_continue
163    }
164
165    -re "^$gdb_prompt $" {
166	gdb_assert { $seen_command } $gdb_test_name
167    }
168
169    -re "^\[^\r\n\]+\r\n" {
170	exp_continue
171    }
172}
173
174# This command does not produce any output unless there is some problem
175# with the symtabs, so that branch will really never be covered in the
176# tests here!!
177gdb_test_no_output "maint check-symtabs"
178
179# Test per-command stats.
180gdb_test_no_output "maint set per-command on"
181set decimal "\[0-9\]+"
182set time_fmt "${decimal}-${decimal}-${decimal} ${decimal}:${decimal}:${decimal}\\.${decimal}"
183gdb_test "pwd" \
184    "${time_fmt} - command started\r\n.*\r\n${time_fmt} - command finished\r\nCommand execution time: \[0-9.\]+ \\(cpu\\), \[0-9.\]+ \\(wall\\)\[\r\n\]+Space used: $decimal \\(\\+$decimal for this command\\)\[\r\n\]+#symtabs: $decimal \\(\\+$decimal\\), #compunits: $decimal \\(\\+$decimal\\), #blocks: $decimal \\(\\+$decimal\\)"
185gdb_test "maint set per-command off" \
186    "${time_fmt} - command started"
187
188# The timeout value is raised, because printing all the symbols and
189# statistical information about Cygwin and Windows libraries takes a lot
190# of time.
191if [istarget "*-*-cygwin*"] {
192	set oldtimeout $timeout
193	set timeout [expr $timeout + 500]
194}
195
196set re \
197    [list \
198	 "Statistics for\[^\n\r\]*maint\[^\n\r\]*:" \
199	 "  Number of \"minimal\" symbols read: $decimal" \
200	 "  Number of \"full\" symbols read: $decimal" \
201	 "  Number of \"types\" defined: $decimal" \
202	 "  Number of symbol tables: $decimal" \
203	 "  Number of symbol tables with line tables: $decimal" \
204	 "  Number of symbol tables with blockvectors: $decimal" \
205	 "(  Number of \"partial\" symbols read: $decimal" \
206	 ")?(  Number of psym tables \\(not yet expanded\\): $decimal" \
207	 ")?(  Total memory used for psymbol cache: $decimal" \
208	 ")?(  Number of read CUs: $decimal" \
209	 "  Number of unread CUs: $decimal" \
210	 ")?  Total memory used for objfile obstack: $decimal" \
211	 "  Total memory used for BFD obstack: $decimal" \
212	 "  Total memory used for string cache: $decimal" \
213	 ""]
214
215set re [multi_line {*}$re]
216gdb_test_lines "maint print statistics" "" $re
217
218# There aren't any ...
219gdb_test_no_output "maint print dummy-frames"
220
221
222
223# To avoid timeouts, we avoid expects with many .* patterns that match
224# many lines.  Instead, we keep track of which milestones we've seen
225# in the output, and stop when we've seen all of them.
226
227set header 0
228set psymtabs 0
229set cooked_index 0
230set symtabs 0
231set cmd "maint print objfiles"
232set re "maint"
233gdb_test_multiple "$cmd $re" "$cmd" -lbl {
234    -re "\r\nObject file.*maint($EXEEXT)?:  Objfile at ${hex}" {
235	set header 1
236	exp_continue
237    }
238    -re "\r\nCooked index" {
239	set cooked_index 1
240	exp_continue
241    }
242    -re "\r\nPsymtabs:\[\r\t \]+" {
243	set psymtabs 1
244	exp_continue
245    }
246    -re "\r\nSymtabs:\[\r\t \]+\n" {
247	set symtabs 1
248	exp_continue
249    }
250    -re -wrap "" {
251	pass $gdb_test_name
252    }
253}
254
255proc maint_pass_if {val name} {
256    if $val { pass $name } else { fail $name }
257}
258
259maint_pass_if $header   "maint print objfiles: header"
260if {$cooked_index} {
261    set have_psyms 0
262}
263if { $have_psyms } {
264    maint_pass_if $psymtabs "maint print objfiles: psymtabs"
265}
266maint_pass_if $symtabs  "maint print objfiles: symtabs"
267
268if { $have_psyms } {
269    set psymbols_output [standard_output_file psymbols_output]
270    set psymbols_output_re [string_to_regexp $psymbols_output]
271    set test_list [list \
272		       "maint print psymbols -source" \
273		       "maint print psymbols -source ${srcdir}/${subdir}/${srcfile} $psymbols_output" \
274		       "maint print psymbols -pc" \
275		       "maint print psymbols -pc main $psymbols_output"]
276    foreach { test_name command } $test_list {
277	gdb_test_no_output "$command" "collecting data for $test_name"
278	gdb_test_multiple "shell grep 'main.*function' $psymbols_output" "" {
279		-re -wrap ".main., function, $hex.*" {
280		    pass "$test_name (pattern 1)"
281		}
282		-re -wrap ".*main.  .., function, $hex.*" {
283		    pass "$test_name (pattern 2)"
284		}
285	}
286	gdb_test "shell rm -f $psymbols_output" ".*" \
287	    "${test_name}: shell rm -f psymbols_output"
288    }
289}
290
291
292set msymbols_output [standard_output_file msymbols_output]
293set msymbols_output_re [string_to_regexp $msymbols_output]
294gdb_test_no_output "maint print msymbols -objfile ${binfile} $msymbols_output" \
295    "print msymbols to file, with absolute path"
296gdb_test "shell grep factorial $msymbols_output" \
297    "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*" \
298    "maint print msymbols, absolute pathname"
299gdb_test "shell rm -f $msymbols_output" ".*" "remove absolute path msymbols"
300
301# Check that maint print msymbols allows relative pathnames
302set mydir [pwd]
303gdb_test "cd [standard_output_file {}]" \
304    "Working directory .*\..*" \
305    "cd to objdir"
306
307gdb_test_no_output "maint print msymbols -objfile ${testfile} $msymbols_output"\
308    "print msymbols to file, with relative path"
309gdb_test "shell grep factorial $msymbols_output" \
310    "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*" \
311    "maint print msymbols, relative pathname"
312gdb_test "shell rm -f msymbols_output" ".*" "remove relative path msymbols"
313
314gdb_test "cd ${mydir}" \
315    "Working directory [string_to_regexp ${mydir}]\..*" \
316    "cd to mydir"
317
318
319# Request symbols for one particular source file so that we don't try to
320# dump the symbol information for the entire C library - over 500MB nowadays
321# for GNU libc.
322
323set symbols_output [standard_output_file symbols_output]
324set symbols_output_re [string_to_regexp $symbols_output]
325set test_list [list \
326		   "maint print symbols -source" \
327		   "maint print symbols -source ${srcdir}/${subdir}/${srcfile} $symbols_output" \
328		   "maint print symbols -pc" \
329		   "maint print symbols -pc main $symbols_output"]
330foreach { test_name command } $test_list {
331    gdb_test_no_output "$command" "$test_name generate"
332    gdb_test "shell grep 'main(.*block' $symbols_output"\
333	"int main\\(int, char \\*\\*, char \\*\\*\\); block.*"\
334	"$test_name read"
335    gdb_test "shell rm -f $symbols_output" ".*" \
336	"$test_name: shell rm -f symbols_output"
337}
338
339set msg "maint print type"
340gdb_test_multiple "maint print type argc" $msg {
341    -re "type node $hex\r\nname .int. \\($hex\\)\r\ncode $hex \\(TYPE_CODE_INT\\)\r\nlength \[24\]\r\nobjfile $hex\r\ntarget_type $hex\r\npointer_type $hex\r\nreference_type $hex\r\ntype_chain $hex\r\ninstance_flags $hex\r\nflags\r\nnfields 0 $hex\r\n$gdb_prompt $" {
342        pass $msg
343    }
344}
345
346if [istarget "hppa*-*-11*"] {
347    setup_xfail hppa*-*-*11* CLLbs14860
348    gdb_test_multiple "maint print unwind &main" "maint print unwind" {
349        -re ".*unwind_table_entry \\($hex\\):\r\n\tregion_start = $hex <main>\r\n\tregion_end = $hex <main\\+\[0-9\]*>\r\n\tflags = Args_stored Save_RP\r\n\tRegion_description = $hex\r\n\tEntry_FR = $hex\r\n\tEntry_GR = $hex\r\n\tTotal_frame_size = $hex\r\n$gdb_prompt $" {
350	    pass "maint print unwind"
351	}
352        -re ".*unwind_table_entry \\($hex\\):\r\n\tregion_start = $hex <main>\r\n\tregion_end = $hex <main\\+\[0-9\]*>\r\n\tflags = Args_stored Save_RP\r\n\tFLD = $hex\r\n\tFLD = $hex\r\n\tFLD = $hex\r\n\tFLD = $hex\r\n$gdb_prompt $" {
353	    xfail "maint print unwind"
354	}
355    }
356}
357
358set oldtimeout $timeout
359set timeout [expr $timeout + 300]
360
361set bp_location6 [gdb_get_line_number "set breakpoint 6 here"]
362
363gdb_test_multiple "maint info breakpoints" "maint info breakpoints" {
364    -re "Num\[ \t\]+Type\[ \t\]+Disp\[ \t\]+Enb\[ \t\]+Address\[ \t\]+What\r\n1\[ \t\]+breakpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+$hex\[ \t\]+in main at.*break.c:$bp_location6 inf 1\r\n\[ \t\]+breakpoint already hit 1 time\r\n.*$gdb_prompt $" {
365	pass "maint info breakpoints"
366    }
367    -re "Num\[ \t\]+Type\[ \t\]+Disp\[ \t\]+Enb\[ \t\]+Address\[ \t\]+What\r\n1\[ \t\]+breakpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+$hex in main at.*break.c:$bp_location6 sspace 1\r\n\[ \t\]+breakpoint already hit 1 time\r\n-1\[ \t\]+shlib events\[ \t\]+keep\[ \t\]+y\[ \t\]+$hex.*breakpoint already hit.*$gdb_prompt $" {
368	pass "maint info breakpoints (with shlib events)"
369    }
370}
371
372gdb_test "maint print" \
373    "List.*unambiguous\\..*" \
374    "maint print w/o args"
375
376gdb_test "maint info" \
377    "List.*unambiguous\\..*" \
378    "maint info w/o args"
379
380gdb_test "maint" \
381    "List.*unambiguous\\..*" \
382    "maint w/o args"
383
384# Test that "main info line-table" w/o a file name shows the symtab for
385# $srcfile.
386set saw_srcfile 0
387gdb_test_multiple "maint info line-table" \
388    "maint info line-table w/o a file name" {
389	-re "symtab: \[^\n\r\]+${srcfile} \\(\\(struct symtab \\*\\) $hex\\)\r\nlinetable: \\(\\(struct linetable \\*\\) $hex\\):\r\nINDEX\[ \t\]+LINE\[ \t\]+ADDRESS\[^\r\n\]*" {
390	set saw_srcfile 1
391	exp_continue
392    }
393    -re "symtab: \[^\n\r\]+ \\(\\(struct symtab \\*\\) $hex\\)\r\nlinetable: \\(\\(struct linetable \\*\\) $hex\\):\r\nINDEX\[ \t\]+LINE\[ \t\]+ADDRESS\[^\r\n\]*" {
394	# Match each symtab to avoid overflowing expect's buffer.
395	exp_continue
396    }
397    -re "symtab: \[^\n\r\]+ \\(\\(struct symtab \\*\\) $hex\\)\r\nlinetable: \\(\\(struct linetable \\*\\) 0x0\\):\r\nNo line table.\r\n" {
398	# For symtabs with no linetable.
399	exp_continue
400    }
401    -re "^$decimal\[ \t\]+$decimal\[ \t\]+$hex\[^\r\n\]*\r\n" {
402	# Line table entries can be long too:
403	#
404	#  INDEX    LINE ADDRESS
405	#  0          29 0x00000000004006f6
406	#  1          30 0x00000000004006fa
407	#  2          31 0x0000000000400704
408	#  3          42 0x0000000000400706
409	#  4          43 0x0000000000400719
410	#  5          44 0x0000000000400722
411	#  6          45 0x0000000000400740
412	#  (...)
413	#  454       129 0x00007ffff7df1d28
414	#  455       END 0x00007ffff7df1d3f
415	#
416	# Match each line to avoid overflowing expect's buffer.
417	exp_continue
418    }
419    -re "^$decimal\[ \t\]+END\[ \t\]+$hex\[^\r\n\]*\r\n" {
420	# Matches an end marker in the above.
421	exp_continue
422    }
423    -re "^\r\n" {
424	# Empty line between tables.
425	exp_continue
426    }
427    -re "^$gdb_prompt $" {
428	gdb_assert $saw_srcfile $gdb_test_name
429    }
430}
431
432gdb_test "maint info line-table ${srcfile}" \
433    "symtab: \[^\n\r\]+${srcfile}.*INDEX.*LINE.*ADDRESS.*" \
434    "maint info line-table with filename of current symtab"
435
436if { ! $readnow_p } {
437    gdb_test_no_output "maint info line-table ${srcfile2}" \
438	[join \
439	     "maint info line-table with filename of symtab that is not" \
440	     " currently expanded"]
441}
442
443gdb_test_no_output "maint expand-symtabs"
444
445gdb_test "maint info line-table ${srcfile2}" \
446    "symtab: \[^\n\r\]+${srcfile2}.*INDEX.*LINE.*ADDRESS.*" \
447    "maint info line-table with filename of symtab that is not current"
448
449gdb_test_no_output "maint info line-table xxx.c" \
450    "maint info line-table with invalid filename"
451
452set timeout $oldtimeout
453
454# Just check that the DWARF unwinders control flag is visible.
455gdb_test "maint show dwarf unwinders" \
456    "The DWARF stack unwinders are currently (on|off)\\."
457
458#============test help on maint commands
459
460test_prefix_command_help {"maint info" "maintenance info"} {
461    "Commands for showing internal info about the program being debugged\\.\[\r\n\]+"
462}
463
464test_prefix_command_help {"maint print" "maintenance print"} {
465    "Maintenance command for printing GDB internal state\\.\[\r\n\]+"
466}
467
468test_prefix_command_help {"maint" "maintenance"} {
469    "Commands for use by GDB maintainers\\.\[\r\n\]+"
470    "Includes commands to dump specific internal GDB structures in\[\r\n\]+"
471    "a human readable form, to cause GDB to deliberately dump core, etc\\.\[\r\n\]+"
472}
473
474#set oldtimeout $timeout
475#set timeout [expr $timeout + 300]
476
477gdb_test_multiple "maint dump-me" "maint dump-me" {
478    -re "Should GDB dump core.*\\(y or n\\) $" {
479	gdb_test "n" ".*" "maint dump-me"
480    }
481    -re "Undefined maintenance command: .*$gdb_prompt $" {
482	# Command 'maint dump-me' is registered on non-win32 host.
483	unsupported "maint dump-me"
484    }
485}
486
487send_gdb "maint internal-error\n"
488gdb_expect {
489    -re "A problem internal to GDB has been detected" {
490	pass "maint internal-error"
491	if [gdb_internal_error_resync] {
492	    pass "internal-error resync"
493	} else {
494	    fail "internal-error resync"
495	}
496    }
497    -re ".*$gdb_prompt $" {
498	fail "maint internal-error"
499	untested "internal-error resync"
500    }
501    timeout {
502	fail "maint internal-error (timeout)"
503	untested "internal-error resync"
504    }
505}
506
507#set timeout $oldtimeout
508
509# Test that the commands work without an argument.  For this test, we
510# don't need an inferior loaded/running.  See PR gdb/21164.
511gdb_exit
512gdb_start
513gdb_test_no_output "maint print symbols"
514gdb_test_no_output "maint print msymbols"
515gdb_test_no_output "maint print psymbols"
516
517gdb_exit
518return 0
519