arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / mi / mi-symbol-cmds.c
blobe4d890f3f36f02c65ff351ce794bb58398b4c34e
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/>. */
19 #include "mi-cmds.h"
20 #include "symtab.h"
21 #include "objfiles.h"
22 #include "ui-out.h"
23 #include "source.h"
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. */
30 void
31 mi_cmd_symbol_list_lines (const char *command, const char *const *argv,
32 int argc)
34 struct gdbarch *gdbarch;
35 const char *filename;
36 struct symtab *s;
37 int i;
38 struct ui_out *uiout = current_uiout;
40 if (argc != 1)
41 error (_("-symbol-list-lines: Usage: SOURCE_FILENAME"));
43 filename = argv[0];
44 s = lookup_symtab (current_program_space, filename);
46 if (s == NULL)
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
71 results. */
73 static void
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. */
97 static void
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. */
113 static void
114 mi_symbol_info (domain_search_flags kind, const char *name_regexp,
115 const char *type_regexp, bool exclude_minsyms,
116 size_t max_results)
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;
124 int i = 0;
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);
149 ++i)
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. */
175 static size_t
176 parse_max_results_option (const char *arg)
178 char *ptr;
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;
184 return max_results;
187 /* Helper for mi_cmd_symbol_info_{functions,variables} - depending on KIND.
188 Processes command line options from ARGV and ARGC. */
190 static void
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;
199 enum opt
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},
209 { 0, 0, 0 }
212 int oind = 0;
213 const char *oarg = nullptr;
215 while (1)
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);
221 if (opt < 0)
222 break;
223 switch ((enum opt) opt)
225 case INCLUDE_NONDEBUG_OPT:
226 exclude_minsyms = false;
227 break;
228 case TYPE_REGEXP_OPT:
229 t_regexp = oarg;
230 break;
231 case NAME_REGEXP_OPT:
232 regexp = oarg;
233 break;
234 case MAX_RESULTS_OPT:
235 max_results = parse_max_results_option (oarg);
236 break;
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. */
272 for (; (iter != end
273 && first_module_symbol == iter->first.symbol
274 && first_symbtab == iter->second.symbol->symtab ());
275 ++iter)
276 output_debug_symbol (uiout, kind, iter->second.symbol,
277 iter->second.block);
279 return iter;
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
285 for ITER. */
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,
308 end, kind);
309 return 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. */
316 static void
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. */
326 enum opt
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},
335 { 0, 0, 0 }
338 int oind = 0;
339 const char *oarg = nullptr;
341 while (1)
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);
348 if (opt < 0)
349 break;
350 switch ((enum opt) opt)
352 case MODULE_REGEXP_OPT:
353 module_regexp = oarg;
354 break;
355 case TYPE_REGEXP_OPT:
356 type_regexp = oarg;
357 break;
358 case NAME_REGEXP_OPT:
359 regexp = oarg;
360 break;
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 ();
376 while (iter != end)
377 iter = output_module_symbols_in_single_module (uiout, iter, end, kind);
380 /* Implement -symbol-info-functions command. */
382 void
383 mi_cmd_symbol_info_functions (const char *command, const char *const *argv,
384 int argc)
386 mi_info_functions_or_variables (SEARCH_FUNCTION_DOMAIN, argv, argc);
389 /* Implement -symbol-info-module-functions command. */
391 void
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. */
400 void
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. */
409 void
410 mi_cmd_symbol_info_modules (const char *command, const char *const *argv,
411 int argc)
413 size_t max_results = SIZE_MAX;
414 const char *regexp = nullptr;
416 enum opt
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},
424 { 0, 0, 0 }
427 int oind = 0;
428 const char *oarg = nullptr;
430 while (1)
432 int opt = mi_getopt ("-symbol-info-modules", argc, argv, opts,
433 &oind, &oarg);
434 if (opt < 0)
435 break;
436 switch ((enum opt) opt)
438 case NAME_REGEXP_OPT:
439 regexp = oarg;
440 break;
441 case MAX_RESULTS_OPT:
442 max_results = parse_max_results_option (oarg);
443 break;
447 mi_symbol_info (SEARCH_MODULE_DOMAIN, regexp, nullptr, true, max_results);
450 /* Implement -symbol-info-types command. */
452 void
453 mi_cmd_symbol_info_types (const char *command, const char *const *argv,
454 int argc)
456 size_t max_results = SIZE_MAX;
457 const char *regexp = nullptr;
459 enum opt
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},
467 { 0, 0, 0 }
470 int oind = 0;
471 const char *oarg = nullptr;
473 while (true)
475 int opt = mi_getopt ("-symbol-info-types", argc, argv, opts,
476 &oind, &oarg);
477 if (opt < 0)
478 break;
479 switch ((enum opt) opt)
481 case NAME_REGEXP_OPT:
482 regexp = oarg;
483 break;
484 case MAX_RESULTS_OPT:
485 max_results = parse_max_results_option (oarg);
486 break;
490 mi_symbol_info (SEARCH_TYPE_DOMAIN | SEARCH_STRUCT_DOMAIN, regexp, nullptr,
491 true, max_results);
494 /* Implement -symbol-info-variables command. */
496 void
497 mi_cmd_symbol_info_variables (const char *command, const char *const *argv,
498 int argc)
500 mi_info_functions_or_variables (SEARCH_VAR_DOMAIN, argv, argc);