]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-symbol-cmds.c
gdb/mi: Add -symbol-info-module-{variables,functions}
[thirdparty/binutils-gdb.git] / gdb / mi / mi-symbol-cmds.c
CommitLineData
0d18235f 1/* MI Command Set - symbol commands.
42a4f53d 2 Copyright (C) 2003-2019 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 31void
9f33b8b7 32mi_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
eb822aa6 53 gdbarch = get_objfile_arch (SYMTAB_OBJFILE (s));
0d18235f 54
10f489e5 55 ui_out_emit_list list_emitter (uiout, "lines");
8435453b
DE
56 if (SYMTAB_LINETABLE (s) != NULL && SYMTAB_LINETABLE (s)->nitems > 0)
57 for (i = 0; i < SYMTAB_LINETABLE (s)->nitems; i++)
0d18235f 58 {
2e783024 59 ui_out_emit_tuple tuple_emitter (uiout, NULL);
112e8700 60 uiout->field_core_addr ("pc", gdbarch, SYMTAB_LINETABLE (s)->item[i].pc);
381befee 61 uiout->field_signed ("line", SYMTAB_LINETABLE (s)->item[i].line);
0d18235f 62 }
0d18235f 63}
7dc42066
AB
64
65/* Used by the -symbol-info-* and -symbol-info-module-* commands to print
66 information about the symbol SYM in a block of index BLOCK (either
67 GLOBAL_BLOCK or STATIC_BLOCK). KIND is the kind of symbol we searched
68 for in order to find SYM, which impact which fields are displayed in the
69 results. */
70
71static void
72output_debug_symbol (ui_out *uiout, enum search_domain kind,
73 struct symbol *sym, int block)
74{
75 ui_out_emit_tuple tuple_emitter (uiout, NULL);
76
77 if (SYMBOL_LINE (sym) != 0)
78 uiout->field_unsigned ("line", SYMBOL_LINE (sym));
79 uiout->field_string ("name", sym->print_name ());
80
81 if (kind == FUNCTIONS_DOMAIN || kind == VARIABLES_DOMAIN)
82 {
83 string_file tmp_stream;
84 type_print (SYMBOL_TYPE (sym), "", &tmp_stream, -1);
85 uiout->field_string ("type", tmp_stream.string ());
86
87 std::string str = symbol_to_info_string (sym, block, kind);
88 uiout->field_string ("description", str);
89 }
90}
91
92/* Actually output one nondebug symbol, puts a tuple emitter in place
93 and then outputs the fields for this msymbol. */
94
95static void
96output_nondebug_symbol (ui_out *uiout,
97 const struct bound_minimal_symbol &msymbol)
98{
99 struct gdbarch *gdbarch = get_objfile_arch (msymbol.objfile);
100 ui_out_emit_tuple tuple_emitter (uiout, NULL);
101
102 uiout->field_core_addr ("address", gdbarch,
103 BMSYMBOL_VALUE_ADDRESS (msymbol));
104 uiout->field_string ("name", msymbol.minsym->print_name ());
105}
106
107/* This is the guts of the commands '-symbol-info-functions',
108 '-symbol-info-variables', and '-symbol-info-types'. It searches for
109 symbols matching KING, NAME_REGEXP, TYPE_REGEXP, and EXCLUDE_MINSYMS,
110 and then prints the matching [m]symbols in an MI structured format. */
111
112static void
113mi_symbol_info (enum search_domain kind, const char *name_regexp,
114 const char *type_regexp, bool exclude_minsyms)
115{
116 global_symbol_searcher sym_search (kind, name_regexp);
117 sym_search.set_symbol_type_regexp (type_regexp);
118 sym_search.set_exclude_minsyms (exclude_minsyms);
119 std::vector<symbol_search> symbols = sym_search.search ();
120 ui_out *uiout = current_uiout;
121 int i = 0;
122
123 ui_out_emit_tuple outer_symbols_emitter (uiout, "symbols");
124
125 /* Debug symbols are placed first. */
126 if (i < symbols.size () && symbols[i].msymbol.minsym == nullptr)
127 {
128 ui_out_emit_list debug_symbols_list_emitter (uiout, "debug");
129
130 /* As long as we have debug symbols... */
131 while (i < symbols.size () && symbols[i].msymbol.minsym == nullptr)
132 {
133 symtab *symtab = symbol_symtab (symbols[i].symbol);
134 ui_out_emit_tuple symtab_tuple_emitter (uiout, nullptr);
135
136 uiout->field_string ("filename",
137 symtab_to_filename_for_display (symtab));
138 uiout->field_string ("fullname", symtab_to_fullname (symtab));
139
140 ui_out_emit_list symbols_list_emitter (uiout, "symbols");
141
142 /* As long as we have debug symbols from this symtab... */
143 for (; (i < symbols.size ()
144 && symbols[i].msymbol.minsym == nullptr
145 && symbol_symtab (symbols[i].symbol) == symtab);
146 ++i)
147 {
148 symbol_search &s = symbols[i];
149
150 output_debug_symbol (uiout, kind, s.symbol, s.block);
151 }
152 }
153 }
154
155 /* Non-debug symbols are placed after. */
156 if (i < symbols.size ())
157 {
158 ui_out_emit_list nondebug_symbols_list_emitter (uiout, "nondebug");
159
160 /* As long as we have nondebug symbols... */
161 for (; i < symbols.size (); i++)
162 {
163 gdb_assert (symbols[i].msymbol.minsym != nullptr);
164 output_nondebug_symbol (uiout, symbols[i].msymbol);
165 }
166 }
167}
168
169/* Helper for mi_cmd_symbol_info_{functions,variables} - depending on KIND.
170 Processes command line options from ARGV and ARGC. */
171
172static void
173mi_info_functions_or_variables (enum search_domain kind, char **argv, int argc)
174{
175 const char *regexp = nullptr;
176 const char *t_regexp = nullptr;
177 bool exclude_minsyms = true;
178
179 enum opt
180 {
181 INCLUDE_NONDEBUG_OPT, TYPE_REGEXP_OPT, NAME_REGEXP_OPT
182 };
183 static const struct mi_opt opts[] =
184 {
185 {"-include-nondebug" , INCLUDE_NONDEBUG_OPT, 0},
186 {"-type", TYPE_REGEXP_OPT, 1},
187 {"-name", NAME_REGEXP_OPT, 1},
188 { 0, 0, 0 }
189 };
190
191 int oind = 0;
192 char *oarg = nullptr;
193
194 while (1)
195 {
196 const char *cmd_string
197 = ((kind == FUNCTIONS_DOMAIN)
198 ? "-symbol-info-functions" : "-symbol-info-variables");
199 int opt = mi_getopt (cmd_string, argc, argv, opts, &oind, &oarg);
200 if (opt < 0)
201 break;
202 switch ((enum opt) opt)
203 {
204 case INCLUDE_NONDEBUG_OPT:
205 exclude_minsyms = false;
206 break;
207 case TYPE_REGEXP_OPT:
208 t_regexp = oarg;
209 break;
210 case NAME_REGEXP_OPT:
211 regexp = oarg;
212 break;
213 }
214 }
215
216 mi_symbol_info (kind, regexp, t_regexp, exclude_minsyms);
217}
218
293b38d6
AB
219/* Type for an iterator over a vector of module_symbol_search results. */
220typedef std::vector<module_symbol_search>::const_iterator
221 module_symbol_search_iterator;
222
223/* Helper for mi_info_module_functions_or_variables. Display the results
224 from ITER up to END or until we find a symbol that is in a different
225 module, or in a different symtab than the first symbol we print. Update
226 and return the new value for ITER. */
227static module_symbol_search_iterator
228output_module_symbols_in_single_module_and_file
229 (struct ui_out *uiout, module_symbol_search_iterator iter,
230 const module_symbol_search_iterator end, enum search_domain kind)
231{
232 /* The symbol for the module in which the first result resides. */
233 const symbol *first_module_symbol = iter->first.symbol;
234
235 /* The symbol for the first result, and the symtab in which it resides. */
236 const symbol *first_result_symbol = iter->second.symbol;
237 symtab *first_symbtab = symbol_symtab (first_result_symbol);
238
239 /* Formatted output. */
240 ui_out_emit_tuple current_file (uiout, nullptr);
241 uiout->field_string ("filename",
242 symtab_to_filename_for_display (first_symbtab));
243 uiout->field_string ("fullname", symtab_to_fullname (first_symbtab));
244 ui_out_emit_list item_list (uiout, "symbols");
245
246 /* Repeatedly output result symbols until either we run out of symbols,
247 we change module, or we change symtab. */
248 for (; (iter != end
249 && first_module_symbol == iter->first.symbol
250 && first_symbtab == symbol_symtab (iter->second.symbol));
251 ++iter)
252 output_debug_symbol (uiout, kind, iter->second.symbol,
253 iter->second.block);
254
255 return iter;
256}
257
258/* Helper for mi_info_module_functions_or_variables. Display the results
259 from ITER up to END or until we find a symbol that is in a different
260 module than the first symbol we print. Update and return the new value
261 for ITER. */
262static module_symbol_search_iterator
263output_module_symbols_in_single_module
264 (struct ui_out *uiout, module_symbol_search_iterator iter,
265 const module_symbol_search_iterator end, enum search_domain kind)
266{
267 gdb_assert (iter->first.symbol != nullptr);
268 gdb_assert (iter->second.symbol != nullptr);
269
270 /* The symbol for the module in which the first result resides. */
271 const symbol *first_module_symbol = iter->first.symbol;
272
273 /* Create output formatting. */
274 ui_out_emit_tuple module_tuple (uiout, nullptr);
275 uiout->field_string ("module", first_module_symbol->print_name ());
276 ui_out_emit_list files_list (uiout, "files");
277
278 /* The results are sorted so that symbols within the same file are next
279 to each other in the list. Calling the output function once will
280 print all results within a single file. We keep calling the output
281 function until we change module. */
282 while (iter != end && first_module_symbol == iter->first.symbol)
283 iter = output_module_symbols_in_single_module_and_file (uiout, iter,
284 end, kind);
285 return iter;
286}
287
288/* Core of -symbol-info-module-functions and -symbol-info-module-variables.
289 KIND indicates what we are searching for, and ARGV and ARGC are the
290 command line options passed to the MI command. */
291
292static void
293mi_info_module_functions_or_variables (enum search_domain kind,
294 char **argv, int argc)
295{
296 const char *module_regexp = nullptr;
297 const char *regexp = nullptr;
298 const char *type_regexp = nullptr;
299
300 /* Process the command line options. */
301
302 enum opt
303 {
304 MODULE_REGEXP_OPT, TYPE_REGEXP_OPT, NAME_REGEXP_OPT
305 };
306 static const struct mi_opt opts[] =
307 {
308 {"-module", MODULE_REGEXP_OPT, 1},
309 {"-type", TYPE_REGEXP_OPT, 1},
310 {"-name", NAME_REGEXP_OPT, 1},
311 { 0, 0, 0 }
312 };
313
314 int oind = 0;
315 char *oarg = nullptr;
316
317 while (1)
318 {
319 const char *cmd_string
320 = ((kind == FUNCTIONS_DOMAIN)
321 ? "-symbol-info-module-functions"
322 : "-symbol-info-module-variables");
323 int opt = mi_getopt (cmd_string, argc, argv, opts, &oind, &oarg);
324 if (opt < 0)
325 break;
326 switch ((enum opt) opt)
327 {
328 case MODULE_REGEXP_OPT:
329 module_regexp = oarg;
330 break;
331 case TYPE_REGEXP_OPT:
332 type_regexp = oarg;
333 break;
334 case NAME_REGEXP_OPT:
335 regexp = oarg;
336 break;
337 }
338 }
339
340 std::vector<module_symbol_search> module_symbols
341 = search_module_symbols (module_regexp, regexp, type_regexp, kind);
342
343 struct ui_out *uiout = current_uiout;
344 ui_out_emit_list all_matching_symbols (uiout, "symbols");
345
346 /* The results in the module_symbols list are ordered so symbols in the
347 same module are next to each other. Repeatedly call the output
348 function to print sequences of symbols that are in the same module
349 until we have no symbols left to print. */
350 module_symbol_search_iterator iter = module_symbols.begin ();
351 const module_symbol_search_iterator end = module_symbols.end ();
352 while (iter != end)
353 iter = output_module_symbols_in_single_module (uiout, iter, end, kind);
354}
355
7dc42066
AB
356/* Implement -symbol-info-functions command. */
357
358void
359mi_cmd_symbol_info_functions (const char *command, char **argv, int argc)
360{
361 mi_info_functions_or_variables (FUNCTIONS_DOMAIN, argv, argc);
362}
363
293b38d6
AB
364/* Implement -symbol-info-module-functions command. */
365
366void
367mi_cmd_symbol_info_module_functions (const char *command, char **argv,
368 int argc)
369{
370 mi_info_module_functions_or_variables (FUNCTIONS_DOMAIN, argv, argc);
371}
372
373/* Implement -symbol-info-module-variables command. */
374
375void
376mi_cmd_symbol_info_module_variables (const char *command, char **argv,
377 int argc)
378{
379 mi_info_module_functions_or_variables (VARIABLES_DOMAIN, argv, argc);
380}
381
db5960b4
AB
382/* Implement -symbol-inf-modules command. */
383
384void
385mi_cmd_symbol_info_modules (const char *command, char **argv, int argc)
386{
387 const char *regexp = nullptr;
388
389 enum opt
390 {
391 NAME_REGEXP_OPT
392 };
393 static const struct mi_opt opts[] =
394 {
395 {"-name", NAME_REGEXP_OPT, 1},
396 { 0, 0, 0 }
397 };
398
399 int oind = 0;
400 char *oarg = nullptr;
401
402 while (1)
403 {
404 int opt = mi_getopt ("-symbol-info-modules", argc, argv, opts,
405 &oind, &oarg);
406 if (opt < 0)
407 break;
408 switch ((enum opt) opt)
409 {
410 case NAME_REGEXP_OPT:
411 regexp = oarg;
412 break;
413 }
414 }
415
416 mi_symbol_info (MODULES_DOMAIN, regexp, nullptr, true);
417}
418
7dc42066
AB
419/* Implement -symbol-info-types command. */
420
421void
422mi_cmd_symbol_info_types (const char *command, char **argv, int argc)
423{
424 const char *regexp = nullptr;
425
426 enum opt
427 {
428 NAME_REGEXP_OPT
429 };
430 static const struct mi_opt opts[] =
431 {
432 {"-name", NAME_REGEXP_OPT, 1},
433 { 0, 0, 0 }
434 };
435
436 int oind = 0;
437 char *oarg = nullptr;
438
439 while (true)
440 {
441 int opt = mi_getopt ("-symbol-info-types", argc, argv, opts,
442 &oind, &oarg);
443 if (opt < 0)
444 break;
445 switch ((enum opt) opt)
446 {
447 case NAME_REGEXP_OPT:
448 regexp = oarg;
449 break;
450 }
451 }
452
453 mi_symbol_info (TYPES_DOMAIN, regexp, nullptr, true);
454}
455
456/* Implement -symbol-info-variables command. */
457
458void
459mi_cmd_symbol_info_variables (const char *command, char **argv, int argc)
460{
461 mi_info_functions_or_variables (VARIABLES_DOMAIN, argv, argc);
462}