]>
Commit | Line | Data |
---|---|---|
0d18235f | 1 | /* MI Command Set - symbol commands. |
213516ef | 2 | Copyright (C) 2003-2023 Free Software Foundation, Inc. |
0d18235f JB |
3 | |
4 | This file is part of GDB. | |
5 | ||
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 | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
0d18235f JB |
9 | (at your option) any later version. |
10 | ||
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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
0d18235f JB |
18 | |
19 | #include "defs.h" | |
20 | #include "mi-cmds.h" | |
21 | #include "symtab.h" | |
5af949e3 | 22 | #include "objfiles.h" |
0d18235f | 23 | #include "ui-out.h" |
7dc42066 AB |
24 | #include "source.h" |
25 | #include "mi-getopt.h" | |
0d18235f | 26 | |
2b03b41d SS |
27 | /* Print the list of all pc addresses and lines of code for the |
28 | provided (full or base) source file name. The entries are sorted | |
29 | in ascending PC order. */ | |
0d18235f | 30 | |
ce8f13f8 | 31 | void |
9f33b8b7 | 32 | mi_cmd_symbol_list_lines (const char *command, char **argv, int argc) |
0d18235f | 33 | { |
5af949e3 | 34 | struct gdbarch *gdbarch; |
0d18235f JB |
35 | char *filename; |
36 | struct symtab *s; | |
37 | int i; | |
79a45e25 | 38 | struct ui_out *uiout = current_uiout; |
0d18235f JB |
39 | |
40 | if (argc != 1) | |
1b05df00 | 41 | error (_("-symbol-list-lines: Usage: SOURCE_FILENAME")); |
0d18235f JB |
42 | |
43 | filename = argv[0]; | |
44 | s = lookup_symtab (filename); | |
45 | ||
46 | if (s == NULL) | |
1b05df00 | 47 | error (_("-symbol-list-lines: Unknown source file name.")); |
0d18235f | 48 | |
2b03b41d SS |
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. */ | |
0d18235f | 52 | |
1acc9dca TT |
53 | struct objfile *objfile = s->compunit ()->objfile (); |
54 | gdbarch = objfile->arch (); | |
0d18235f | 55 | |
10f489e5 | 56 | ui_out_emit_list list_emitter (uiout, "lines"); |
5b607461 SM |
57 | if (s->linetable () != NULL && s->linetable ()->nitems > 0) |
58 | for (i = 0; i < s->linetable ()->nitems; i++) | |
01add95b SM |
59 | { |
60 | ui_out_emit_tuple tuple_emitter (uiout, NULL); | |
1acc9dca TT |
61 | uiout->field_core_addr ("pc", gdbarch, |
62 | s->linetable ()->item[i].pc (objfile)); | |
5b607461 | 63 | uiout->field_signed ("line", s->linetable ()->item[i].line); |
01add95b | 64 | } |
0d18235f | 65 | } |
7dc42066 AB |
66 | |
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. */ | |
72 | ||
73 | static void | |
74 | output_debug_symbol (ui_out *uiout, enum search_domain kind, | |
75 | struct symbol *sym, int block) | |
76 | { | |
77 | ui_out_emit_tuple tuple_emitter (uiout, NULL); | |
78 | ||
5d0027b9 SM |
79 | if (sym->line () != 0) |
80 | uiout->field_unsigned ("line", sym->line ()); | |
7dc42066 AB |
81 | uiout->field_string ("name", sym->print_name ()); |
82 | ||
83 | if (kind == FUNCTIONS_DOMAIN || kind == VARIABLES_DOMAIN) | |
84 | { | |
85 | string_file tmp_stream; | |
5f9c5a63 | 86 | type_print (sym->type (), "", &tmp_stream, -1); |
7dc42066 AB |
87 | uiout->field_string ("type", tmp_stream.string ()); |
88 | ||
89 | std::string str = symbol_to_info_string (sym, block, kind); | |
90 | uiout->field_string ("description", str); | |
91 | } | |
92 | } | |
93 | ||
94 | /* Actually output one nondebug symbol, puts a tuple emitter in place | |
95 | and then outputs the fields for this msymbol. */ | |
96 | ||
97 | static void | |
98 | output_nondebug_symbol (ui_out *uiout, | |
99 | const struct bound_minimal_symbol &msymbol) | |
100 | { | |
08feed99 | 101 | struct gdbarch *gdbarch = msymbol.objfile->arch (); |
7dc42066 AB |
102 | ui_out_emit_tuple tuple_emitter (uiout, NULL); |
103 | ||
104 | uiout->field_core_addr ("address", gdbarch, | |
4aeddc50 | 105 | msymbol.value_address ()); |
7dc42066 AB |
106 | uiout->field_string ("name", msymbol.minsym->print_name ()); |
107 | } | |
108 | ||
109 | /* This is the guts of the commands '-symbol-info-functions', | |
110 | '-symbol-info-variables', and '-symbol-info-types'. It searches for | |
111 | symbols matching KING, NAME_REGEXP, TYPE_REGEXP, and EXCLUDE_MINSYMS, | |
112 | and then prints the matching [m]symbols in an MI structured format. */ | |
113 | ||
114 | static void | |
115 | mi_symbol_info (enum search_domain kind, const char *name_regexp, | |
c2512106 AB |
116 | const char *type_regexp, bool exclude_minsyms, |
117 | size_t max_results) | |
7dc42066 AB |
118 | { |
119 | global_symbol_searcher sym_search (kind, name_regexp); | |
120 | sym_search.set_symbol_type_regexp (type_regexp); | |
121 | sym_search.set_exclude_minsyms (exclude_minsyms); | |
c2512106 | 122 | sym_search.set_max_search_results (max_results); |
7dc42066 AB |
123 | std::vector<symbol_search> symbols = sym_search.search (); |
124 | ui_out *uiout = current_uiout; | |
125 | int i = 0; | |
126 | ||
127 | ui_out_emit_tuple outer_symbols_emitter (uiout, "symbols"); | |
128 | ||
129 | /* Debug symbols are placed first. */ | |
130 | if (i < symbols.size () && symbols[i].msymbol.minsym == nullptr) | |
131 | { | |
132 | ui_out_emit_list debug_symbols_list_emitter (uiout, "debug"); | |
133 | ||
134 | /* As long as we have debug symbols... */ | |
135 | while (i < symbols.size () && symbols[i].msymbol.minsym == nullptr) | |
136 | { | |
4206d69e | 137 | symtab *symtab = symbols[i].symbol->symtab (); |
7dc42066 AB |
138 | ui_out_emit_tuple symtab_tuple_emitter (uiout, nullptr); |
139 | ||
140 | uiout->field_string ("filename", | |
141 | symtab_to_filename_for_display (symtab)); | |
142 | uiout->field_string ("fullname", symtab_to_fullname (symtab)); | |
143 | ||
144 | ui_out_emit_list symbols_list_emitter (uiout, "symbols"); | |
145 | ||
146 | /* As long as we have debug symbols from this symtab... */ | |
147 | for (; (i < symbols.size () | |
148 | && symbols[i].msymbol.minsym == nullptr | |
4206d69e | 149 | && symbols[i].symbol->symtab () == symtab); |
7dc42066 AB |
150 | ++i) |
151 | { | |
152 | symbol_search &s = symbols[i]; | |
153 | ||
154 | output_debug_symbol (uiout, kind, s.symbol, s.block); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | /* Non-debug symbols are placed after. */ | |
160 | if (i < symbols.size ()) | |
161 | { | |
162 | ui_out_emit_list nondebug_symbols_list_emitter (uiout, "nondebug"); | |
163 | ||
164 | /* As long as we have nondebug symbols... */ | |
165 | for (; i < symbols.size (); i++) | |
166 | { | |
167 | gdb_assert (symbols[i].msymbol.minsym != nullptr); | |
168 | output_nondebug_symbol (uiout, symbols[i].msymbol); | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
c2512106 AB |
173 | /* Helper to parse the option text from an -max-results argument and return |
174 | the parsed value. If the text can't be parsed then an error is thrown. */ | |
175 | ||
176 | static size_t | |
177 | parse_max_results_option (char *arg) | |
178 | { | |
179 | char *ptr = arg; | |
180 | long long val = strtoll (arg, &ptr, 10); | |
181 | if (arg == ptr || *ptr != '\0' || val > SIZE_MAX || val < 0) | |
182 | error (_("invalid value for --max-results argument")); | |
183 | size_t max_results = (size_t) val; | |
184 | ||
185 | return max_results; | |
186 | } | |
187 | ||
7dc42066 AB |
188 | /* Helper for mi_cmd_symbol_info_{functions,variables} - depending on KIND. |
189 | Processes command line options from ARGV and ARGC. */ | |
190 | ||
191 | static void | |
192 | mi_info_functions_or_variables (enum search_domain kind, char **argv, int argc) | |
193 | { | |
c2512106 | 194 | size_t max_results = SIZE_MAX; |
7dc42066 AB |
195 | const char *regexp = nullptr; |
196 | const char *t_regexp = nullptr; | |
197 | bool exclude_minsyms = true; | |
198 | ||
199 | enum opt | |
200 | { | |
c2512106 | 201 | INCLUDE_NONDEBUG_OPT, TYPE_REGEXP_OPT, NAME_REGEXP_OPT, MAX_RESULTS_OPT |
7dc42066 AB |
202 | }; |
203 | static const struct mi_opt opts[] = | |
204 | { | |
205 | {"-include-nondebug" , INCLUDE_NONDEBUG_OPT, 0}, | |
206 | {"-type", TYPE_REGEXP_OPT, 1}, | |
207 | {"-name", NAME_REGEXP_OPT, 1}, | |
c2512106 | 208 | {"-max-results", MAX_RESULTS_OPT, 1}, |
7dc42066 AB |
209 | { 0, 0, 0 } |
210 | }; | |
211 | ||
212 | int oind = 0; | |
213 | char *oarg = nullptr; | |
214 | ||
215 | while (1) | |
216 | { | |
217 | const char *cmd_string | |
218 | = ((kind == FUNCTIONS_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) | |
224 | { | |
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; | |
c2512106 AB |
234 | case MAX_RESULTS_OPT: |
235 | max_results = parse_max_results_option (oarg); | |
236 | break; | |
7dc42066 AB |
237 | } |
238 | } | |
239 | ||
c2512106 | 240 | mi_symbol_info (kind, regexp, t_regexp, exclude_minsyms, max_results); |
7dc42066 AB |
241 | } |
242 | ||
293b38d6 AB |
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; | |
246 | ||
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, enum search_domain kind) | |
255 | { | |
256 | /* The symbol for the module in which the first result resides. */ | |
257 | const symbol *first_module_symbol = iter->first.symbol; | |
258 | ||
259 | /* The symbol for the first result, and the symtab in which it resides. */ | |
260 | const symbol *first_result_symbol = iter->second.symbol; | |
4206d69e | 261 | symtab *first_symbtab = first_result_symbol->symtab (); |
293b38d6 AB |
262 | |
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"); | |
269 | ||
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 | |
4206d69e | 274 | && first_symbtab == iter->second.symbol->symtab ()); |
293b38d6 AB |
275 | ++iter) |
276 | output_debug_symbol (uiout, kind, iter->second.symbol, | |
277 | iter->second.block); | |
278 | ||
279 | return iter; | |
280 | } | |
281 | ||
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, enum search_domain kind) | |
290 | { | |
291 | gdb_assert (iter->first.symbol != nullptr); | |
292 | gdb_assert (iter->second.symbol != nullptr); | |
293 | ||
294 | /* The symbol for the module in which the first result resides. */ | |
295 | const symbol *first_module_symbol = iter->first.symbol; | |
296 | ||
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"); | |
301 | ||
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; | |
310 | } | |
311 | ||
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. */ | |
315 | ||
316 | static void | |
317 | mi_info_module_functions_or_variables (enum search_domain kind, | |
318 | char **argv, int argc) | |
319 | { | |
320 | const char *module_regexp = nullptr; | |
321 | const char *regexp = nullptr; | |
322 | const char *type_regexp = nullptr; | |
323 | ||
324 | /* Process the command line options. */ | |
325 | ||
326 | enum opt | |
327 | { | |
328 | MODULE_REGEXP_OPT, TYPE_REGEXP_OPT, NAME_REGEXP_OPT | |
329 | }; | |
330 | static const struct mi_opt opts[] = | |
331 | { | |
332 | {"-module", MODULE_REGEXP_OPT, 1}, | |
333 | {"-type", TYPE_REGEXP_OPT, 1}, | |
334 | {"-name", NAME_REGEXP_OPT, 1}, | |
335 | { 0, 0, 0 } | |
336 | }; | |
337 | ||
338 | int oind = 0; | |
339 | char *oarg = nullptr; | |
340 | ||
341 | while (1) | |
342 | { | |
343 | const char *cmd_string | |
344 | = ((kind == FUNCTIONS_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) | |
351 | { | |
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; | |
361 | } | |
362 | } | |
363 | ||
364 | std::vector<module_symbol_search> module_symbols | |
365 | = search_module_symbols (module_regexp, regexp, type_regexp, kind); | |
366 | ||
367 | struct ui_out *uiout = current_uiout; | |
368 | ui_out_emit_list all_matching_symbols (uiout, "symbols"); | |
369 | ||
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); | |
378 | } | |
379 | ||
7dc42066 AB |
380 | /* Implement -symbol-info-functions command. */ |
381 | ||
382 | void | |
383 | mi_cmd_symbol_info_functions (const char *command, char **argv, int argc) | |
384 | { | |
385 | mi_info_functions_or_variables (FUNCTIONS_DOMAIN, argv, argc); | |
386 | } | |
387 | ||
293b38d6 AB |
388 | /* Implement -symbol-info-module-functions command. */ |
389 | ||
390 | void | |
391 | mi_cmd_symbol_info_module_functions (const char *command, char **argv, | |
392 | int argc) | |
393 | { | |
394 | mi_info_module_functions_or_variables (FUNCTIONS_DOMAIN, argv, argc); | |
395 | } | |
396 | ||
397 | /* Implement -symbol-info-module-variables command. */ | |
398 | ||
399 | void | |
400 | mi_cmd_symbol_info_module_variables (const char *command, char **argv, | |
401 | int argc) | |
402 | { | |
403 | mi_info_module_functions_or_variables (VARIABLES_DOMAIN, argv, argc); | |
404 | } | |
405 | ||
db5960b4 AB |
406 | /* Implement -symbol-inf-modules command. */ |
407 | ||
408 | void | |
409 | mi_cmd_symbol_info_modules (const char *command, char **argv, int argc) | |
410 | { | |
c2512106 | 411 | size_t max_results = SIZE_MAX; |
db5960b4 AB |
412 | const char *regexp = nullptr; |
413 | ||
414 | enum opt | |
415 | { | |
c2512106 | 416 | NAME_REGEXP_OPT, MAX_RESULTS_OPT |
db5960b4 AB |
417 | }; |
418 | static const struct mi_opt opts[] = | |
419 | { | |
420 | {"-name", NAME_REGEXP_OPT, 1}, | |
c2512106 | 421 | {"-max-results", MAX_RESULTS_OPT, 1}, |
db5960b4 AB |
422 | { 0, 0, 0 } |
423 | }; | |
424 | ||
425 | int oind = 0; | |
426 | char *oarg = nullptr; | |
427 | ||
428 | while (1) | |
429 | { | |
430 | int opt = mi_getopt ("-symbol-info-modules", argc, argv, opts, | |
431 | &oind, &oarg); | |
432 | if (opt < 0) | |
433 | break; | |
434 | switch ((enum opt) opt) | |
435 | { | |
436 | case NAME_REGEXP_OPT: | |
437 | regexp = oarg; | |
438 | break; | |
c2512106 AB |
439 | case MAX_RESULTS_OPT: |
440 | max_results = parse_max_results_option (oarg); | |
441 | break; | |
db5960b4 AB |
442 | } |
443 | } | |
444 | ||
c2512106 | 445 | mi_symbol_info (MODULES_DOMAIN, regexp, nullptr, true, max_results); |
db5960b4 AB |
446 | } |
447 | ||
7dc42066 AB |
448 | /* Implement -symbol-info-types command. */ |
449 | ||
450 | void | |
451 | mi_cmd_symbol_info_types (const char *command, char **argv, int argc) | |
452 | { | |
c2512106 | 453 | size_t max_results = SIZE_MAX; |
7dc42066 AB |
454 | const char *regexp = nullptr; |
455 | ||
456 | enum opt | |
457 | { | |
c2512106 | 458 | NAME_REGEXP_OPT, MAX_RESULTS_OPT |
7dc42066 AB |
459 | }; |
460 | static const struct mi_opt opts[] = | |
461 | { | |
462 | {"-name", NAME_REGEXP_OPT, 1}, | |
c2512106 | 463 | {"-max-results", MAX_RESULTS_OPT, 1}, |
7dc42066 AB |
464 | { 0, 0, 0 } |
465 | }; | |
466 | ||
467 | int oind = 0; | |
468 | char *oarg = nullptr; | |
469 | ||
470 | while (true) | |
471 | { | |
472 | int opt = mi_getopt ("-symbol-info-types", argc, argv, opts, | |
473 | &oind, &oarg); | |
474 | if (opt < 0) | |
475 | break; | |
476 | switch ((enum opt) opt) | |
477 | { | |
478 | case NAME_REGEXP_OPT: | |
479 | regexp = oarg; | |
480 | break; | |
c2512106 AB |
481 | case MAX_RESULTS_OPT: |
482 | max_results = parse_max_results_option (oarg); | |
483 | break; | |
7dc42066 AB |
484 | } |
485 | } | |
486 | ||
c2512106 | 487 | mi_symbol_info (TYPES_DOMAIN, regexp, nullptr, true, max_results); |
7dc42066 AB |
488 | } |
489 | ||
490 | /* Implement -symbol-info-variables command. */ | |
491 | ||
492 | void | |
493 | mi_cmd_symbol_info_variables (const char *command, char **argv, int argc) | |
494 | { | |
495 | mi_info_functions_or_variables (VARIABLES_DOMAIN, argv, argc); | |
496 | } |