1 /* MI Command Set - symbol commands.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "mi-getopt.h"
26 /* Print the list of all pc addresses and lines of code for the
27 provided (full or base) source file name. The entries are sorted
28 in ascending PC order. */
31 mi_cmd_symbol_list_lines (const char *command
, const char *const *argv
,
34 struct gdbarch
*gdbarch
;
38 struct ui_out
*uiout
= current_uiout
;
41 error (_("-symbol-list-lines: Usage: SOURCE_FILENAME"));
44 s
= lookup_symtab (current_program_space
, filename
);
47 error (_("-symbol-list-lines: Unknown source file name."));
49 /* Now, dump the associated line table. The pc addresses are
50 already sorted by increasing values in the symbol table, so no
51 need to perform any other sorting. */
53 struct objfile
*objfile
= s
->compunit ()->objfile ();
54 gdbarch
= objfile
->arch ();
56 ui_out_emit_list
list_emitter (uiout
, "lines");
57 if (s
->linetable () != NULL
&& s
->linetable ()->nitems
> 0)
58 for (i
= 0; i
< s
->linetable ()->nitems
; i
++)
60 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
61 uiout
->field_core_addr ("pc", gdbarch
,
62 s
->linetable ()->item
[i
].pc (objfile
));
63 uiout
->field_signed ("line", s
->linetable ()->item
[i
].line
);
67 /* Used by the -symbol-info-* and -symbol-info-module-* commands to print
68 information about the symbol SYM in a block of index BLOCK (either
69 GLOBAL_BLOCK or STATIC_BLOCK). KIND is the kind of symbol we searched
70 for in order to find SYM, which impact which fields are displayed in the
74 output_debug_symbol (ui_out
*uiout
, domain_search_flags kind
,
75 struct symbol
*sym
, int block
)
77 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
79 if (sym
->line () != 0)
80 uiout
->field_unsigned ("line", sym
->line ());
81 uiout
->field_string ("name", sym
->print_name ());
83 if ((kind
& (SEARCH_FUNCTION_DOMAIN
| SEARCH_VAR_DOMAIN
)) != 0)
85 string_file tmp_stream
;
86 type_print (sym
->type (), "", &tmp_stream
, -1);
87 uiout
->field_string ("type", tmp_stream
.string ());
89 std::string str
= symbol_to_info_string (sym
, block
);
90 uiout
->field_string ("description", str
);
94 /* Actually output one nondebug symbol, puts a tuple emitter in place
95 and then outputs the fields for this msymbol. */
98 output_nondebug_symbol (ui_out
*uiout
, const bound_minimal_symbol
&msymbol
)
100 struct gdbarch
*gdbarch
= msymbol
.objfile
->arch ();
101 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
103 uiout
->field_core_addr ("address", gdbarch
,
104 msymbol
.value_address ());
105 uiout
->field_string ("name", msymbol
.minsym
->print_name ());
108 /* This is the guts of the commands '-symbol-info-functions',
109 '-symbol-info-variables', and '-symbol-info-types'. It searches for
110 symbols matching KING, NAME_REGEXP, TYPE_REGEXP, and EXCLUDE_MINSYMS,
111 and then prints the matching [m]symbols in an MI structured format. */
114 mi_symbol_info (domain_search_flags kind
, const char *name_regexp
,
115 const char *type_regexp
, bool exclude_minsyms
,
118 global_symbol_searcher
sym_search (kind
, name_regexp
);
119 sym_search
.set_symbol_type_regexp (type_regexp
);
120 sym_search
.set_exclude_minsyms (exclude_minsyms
);
121 sym_search
.set_max_search_results (max_results
);
122 std::vector
<symbol_search
> symbols
= sym_search
.search ();
123 ui_out
*uiout
= current_uiout
;
126 ui_out_emit_tuple
outer_symbols_emitter (uiout
, "symbols");
128 /* Debug symbols are placed first. */
129 if (i
< symbols
.size () && symbols
[i
].msymbol
.minsym
== nullptr)
131 ui_out_emit_list
debug_symbols_list_emitter (uiout
, "debug");
133 /* As long as we have debug symbols... */
134 while (i
< symbols
.size () && symbols
[i
].msymbol
.minsym
== nullptr)
136 symtab
*symtab
= symbols
[i
].symbol
->symtab ();
137 ui_out_emit_tuple
symtab_tuple_emitter (uiout
, nullptr);
139 uiout
->field_string ("filename",
140 symtab_to_filename_for_display (symtab
));
141 uiout
->field_string ("fullname", symtab_to_fullname (symtab
));
143 ui_out_emit_list
symbols_list_emitter (uiout
, "symbols");
145 /* As long as we have debug symbols from this symtab... */
146 for (; (i
< symbols
.size ()
147 && symbols
[i
].msymbol
.minsym
== nullptr
148 && symbols
[i
].symbol
->symtab () == symtab
);
151 symbol_search
&s
= symbols
[i
];
153 output_debug_symbol (uiout
, kind
, s
.symbol
, s
.block
);
158 /* Non-debug symbols are placed after. */
159 if (i
< symbols
.size ())
161 ui_out_emit_list
nondebug_symbols_list_emitter (uiout
, "nondebug");
163 /* As long as we have nondebug symbols... */
164 for (; i
< symbols
.size (); i
++)
166 gdb_assert (symbols
[i
].msymbol
.minsym
!= nullptr);
167 output_nondebug_symbol (uiout
, symbols
[i
].msymbol
);
172 /* Helper to parse the option text from an -max-results argument and return
173 the parsed value. If the text can't be parsed then an error is thrown. */
176 parse_max_results_option (const char *arg
)
179 long long val
= strtoll (arg
, &ptr
, 10);
180 if (arg
== ptr
|| *ptr
!= '\0' || val
> SIZE_MAX
|| val
< 0)
181 error (_("invalid value for --max-results argument"));
182 size_t max_results
= (size_t) val
;
187 /* Helper for mi_cmd_symbol_info_{functions,variables} - depending on KIND.
188 Processes command line options from ARGV and ARGC. */
191 mi_info_functions_or_variables (domain_search_flags kind
,
192 const char *const *argv
, int argc
)
194 size_t max_results
= SIZE_MAX
;
195 const char *regexp
= nullptr;
196 const char *t_regexp
= nullptr;
197 bool exclude_minsyms
= true;
201 INCLUDE_NONDEBUG_OPT
, TYPE_REGEXP_OPT
, NAME_REGEXP_OPT
, MAX_RESULTS_OPT
203 static const struct mi_opt opts
[] =
205 {"-include-nondebug" , INCLUDE_NONDEBUG_OPT
, 0},
206 {"-type", TYPE_REGEXP_OPT
, 1},
207 {"-name", NAME_REGEXP_OPT
, 1},
208 {"-max-results", MAX_RESULTS_OPT
, 1},
213 const char *oarg
= nullptr;
217 const char *cmd_string
218 = ((kind
== SEARCH_FUNCTION_DOMAIN
)
219 ? "-symbol-info-functions" : "-symbol-info-variables");
220 int opt
= mi_getopt (cmd_string
, argc
, argv
, opts
, &oind
, &oarg
);
223 switch ((enum opt
) opt
)
225 case INCLUDE_NONDEBUG_OPT
:
226 exclude_minsyms
= false;
228 case TYPE_REGEXP_OPT
:
231 case NAME_REGEXP_OPT
:
234 case MAX_RESULTS_OPT
:
235 max_results
= parse_max_results_option (oarg
);
240 mi_symbol_info (kind
, regexp
, t_regexp
, exclude_minsyms
, max_results
);
243 /* Type for an iterator over a vector of module_symbol_search results. */
244 typedef std::vector
<module_symbol_search
>::const_iterator
245 module_symbol_search_iterator
;
247 /* Helper for mi_info_module_functions_or_variables. Display the results
248 from ITER up to END or until we find a symbol that is in a different
249 module, or in a different symtab than the first symbol we print. Update
250 and return the new value for ITER. */
251 static module_symbol_search_iterator
252 output_module_symbols_in_single_module_and_file
253 (struct ui_out
*uiout
, module_symbol_search_iterator iter
,
254 const module_symbol_search_iterator end
, domain_search_flags kind
)
256 /* The symbol for the module in which the first result resides. */
257 const symbol
*first_module_symbol
= iter
->first
.symbol
;
259 /* The symbol for the first result, and the symtab in which it resides. */
260 const symbol
*first_result_symbol
= iter
->second
.symbol
;
261 symtab
*first_symbtab
= first_result_symbol
->symtab ();
263 /* Formatted output. */
264 ui_out_emit_tuple
current_file (uiout
, nullptr);
265 uiout
->field_string ("filename",
266 symtab_to_filename_for_display (first_symbtab
));
267 uiout
->field_string ("fullname", symtab_to_fullname (first_symbtab
));
268 ui_out_emit_list
item_list (uiout
, "symbols");
270 /* Repeatedly output result symbols until either we run out of symbols,
271 we change module, or we change symtab. */
273 && first_module_symbol
== iter
->first
.symbol
274 && first_symbtab
== iter
->second
.symbol
->symtab ());
276 output_debug_symbol (uiout
, kind
, iter
->second
.symbol
,
282 /* Helper for mi_info_module_functions_or_variables. Display the results
283 from ITER up to END or until we find a symbol that is in a different
284 module than the first symbol we print. Update and return the new value
286 static module_symbol_search_iterator
287 output_module_symbols_in_single_module
288 (struct ui_out
*uiout
, module_symbol_search_iterator iter
,
289 const module_symbol_search_iterator end
, domain_search_flags kind
)
291 gdb_assert (iter
->first
.symbol
!= nullptr);
292 gdb_assert (iter
->second
.symbol
!= nullptr);
294 /* The symbol for the module in which the first result resides. */
295 const symbol
*first_module_symbol
= iter
->first
.symbol
;
297 /* Create output formatting. */
298 ui_out_emit_tuple
module_tuple (uiout
, nullptr);
299 uiout
->field_string ("module", first_module_symbol
->print_name ());
300 ui_out_emit_list
files_list (uiout
, "files");
302 /* The results are sorted so that symbols within the same file are next
303 to each other in the list. Calling the output function once will
304 print all results within a single file. We keep calling the output
305 function until we change module. */
306 while (iter
!= end
&& first_module_symbol
== iter
->first
.symbol
)
307 iter
= output_module_symbols_in_single_module_and_file (uiout
, iter
,
312 /* Core of -symbol-info-module-functions and -symbol-info-module-variables.
313 KIND indicates what we are searching for, and ARGV and ARGC are the
314 command line options passed to the MI command. */
317 mi_info_module_functions_or_variables (domain_search_flags kind
,
318 const char *const *argv
, int argc
)
320 const char *module_regexp
= nullptr;
321 const char *regexp
= nullptr;
322 const char *type_regexp
= nullptr;
324 /* Process the command line options. */
328 MODULE_REGEXP_OPT
, TYPE_REGEXP_OPT
, NAME_REGEXP_OPT
330 static const struct mi_opt opts
[] =
332 {"-module", MODULE_REGEXP_OPT
, 1},
333 {"-type", TYPE_REGEXP_OPT
, 1},
334 {"-name", NAME_REGEXP_OPT
, 1},
339 const char *oarg
= nullptr;
343 const char *cmd_string
344 = ((kind
== SEARCH_FUNCTION_DOMAIN
)
345 ? "-symbol-info-module-functions"
346 : "-symbol-info-module-variables");
347 int opt
= mi_getopt (cmd_string
, argc
, argv
, opts
, &oind
, &oarg
);
350 switch ((enum opt
) opt
)
352 case MODULE_REGEXP_OPT
:
353 module_regexp
= oarg
;
355 case TYPE_REGEXP_OPT
:
358 case NAME_REGEXP_OPT
:
364 std::vector
<module_symbol_search
> module_symbols
365 = search_module_symbols (module_regexp
, regexp
, type_regexp
, kind
);
367 struct ui_out
*uiout
= current_uiout
;
368 ui_out_emit_list
all_matching_symbols (uiout
, "symbols");
370 /* The results in the module_symbols list are ordered so symbols in the
371 same module are next to each other. Repeatedly call the output
372 function to print sequences of symbols that are in the same module
373 until we have no symbols left to print. */
374 module_symbol_search_iterator iter
= module_symbols
.begin ();
375 const module_symbol_search_iterator end
= module_symbols
.end ();
377 iter
= output_module_symbols_in_single_module (uiout
, iter
, end
, kind
);
380 /* Implement -symbol-info-functions command. */
383 mi_cmd_symbol_info_functions (const char *command
, const char *const *argv
,
386 mi_info_functions_or_variables (SEARCH_FUNCTION_DOMAIN
, argv
, argc
);
389 /* Implement -symbol-info-module-functions command. */
392 mi_cmd_symbol_info_module_functions (const char *command
,
393 const char *const *argv
, int argc
)
395 mi_info_module_functions_or_variables (SEARCH_FUNCTION_DOMAIN
, argv
, argc
);
398 /* Implement -symbol-info-module-variables command. */
401 mi_cmd_symbol_info_module_variables (const char *command
,
402 const char *const *argv
, int argc
)
404 mi_info_module_functions_or_variables (SEARCH_VAR_DOMAIN
, argv
, argc
);
407 /* Implement -symbol-inf-modules command. */
410 mi_cmd_symbol_info_modules (const char *command
, const char *const *argv
,
413 size_t max_results
= SIZE_MAX
;
414 const char *regexp
= nullptr;
418 NAME_REGEXP_OPT
, MAX_RESULTS_OPT
420 static const struct mi_opt opts
[] =
422 {"-name", NAME_REGEXP_OPT
, 1},
423 {"-max-results", MAX_RESULTS_OPT
, 1},
428 const char *oarg
= nullptr;
432 int opt
= mi_getopt ("-symbol-info-modules", argc
, argv
, opts
,
436 switch ((enum opt
) opt
)
438 case NAME_REGEXP_OPT
:
441 case MAX_RESULTS_OPT
:
442 max_results
= parse_max_results_option (oarg
);
447 mi_symbol_info (SEARCH_MODULE_DOMAIN
, regexp
, nullptr, true, max_results
);
450 /* Implement -symbol-info-types command. */
453 mi_cmd_symbol_info_types (const char *command
, const char *const *argv
,
456 size_t max_results
= SIZE_MAX
;
457 const char *regexp
= nullptr;
461 NAME_REGEXP_OPT
, MAX_RESULTS_OPT
463 static const struct mi_opt opts
[] =
465 {"-name", NAME_REGEXP_OPT
, 1},
466 {"-max-results", MAX_RESULTS_OPT
, 1},
471 const char *oarg
= nullptr;
475 int opt
= mi_getopt ("-symbol-info-types", argc
, argv
, opts
,
479 switch ((enum opt
) opt
)
481 case NAME_REGEXP_OPT
:
484 case MAX_RESULTS_OPT
:
485 max_results
= parse_max_results_option (oarg
);
490 mi_symbol_info (SEARCH_TYPE_DOMAIN
| SEARCH_STRUCT_DOMAIN
, regexp
, nullptr,
494 /* Implement -symbol-info-variables command. */
497 mi_cmd_symbol_info_variables (const char *command
, const char *const *argv
,
500 mi_info_functions_or_variables (SEARCH_VAR_DOMAIN
, argv
, argc
);