binutils-gdb/gdb/mi/mi-symbol-cmds.c
Tom Tromey 08feed99cb Change get_objfile_arch to a method on objfile
This changes get_objfile_arch to be a new inline method,
objfile::arch.

To my surprise, this function came up while profiling DWARF psymbol
reading.  Making this change improved performance from 1.986 seconds
to 1.869 seconds.  Both measurements were done by taking the mean of
10 runs on a fixed copy of the gdb executable.

gdb/ChangeLog
2020-04-18  Tom Tromey  <tom@tromey.com>

	* xcoffread.c (enter_line_range, scan_xcoff_symtab): Update.
	* value.c (value_fn_field): Update.
	* valops.c (find_function_in_inferior)
	(value_allocate_space_in_inferior): Update.
	* tui/tui-winsource.c (tui_update_source_windows_with_line):
	Update.
	* tui/tui-source.c (tui_source_window::set_contents): Update.
	* symtab.c (lookup_global_or_static_symbol)
	(find_function_start_sal_1, skip_prologue_sal)
	(print_msymbol_info, find_gnu_ifunc, symbol_arch): Update.
	* symmisc.c (dump_msymbols, dump_symtab_1)
	(maintenance_print_one_line_table): Update.
	* symfile.c (init_entry_point_info, section_is_mapped)
	(list_overlays_command, simple_read_overlay_table)
	(simple_overlay_update_1): Update.
	* stap-probe.c (handle_stap_probe): Update.
	* stabsread.c (dbx_init_float_type, define_symbol)
	(read_one_struct_field, read_enum_type, read_range_type): Update.
	* source.c (info_line_command): Update.
	* python/python.c (gdbpy_source_objfile_script)
	(gdbpy_execute_objfile_script): Update.
	* python/py-type.c (save_objfile_types): Update.
	* python/py-objfile.c (py_free_objfile): Update.
	* python/py-inferior.c (python_new_objfile): Update.
	* psymtab.c (psym_find_pc_sect_compunit_symtab, dump_psymtab)
	(dump_psymtab_addrmap_1, maintenance_info_psymtabs)
	(maintenance_check_psymtabs): Update.
	* printcmd.c (info_address_command): Update.
	* objfiles.h (struct objfile) <arch>: New method, from
	get_objfile_arch.
	(get_objfile_arch): Don't declare.
	* objfiles.c (get_objfile_arch): Remove.
	(filter_overlapping_sections): Update.
	* minsyms.c (msymbol_is_function): Update.
	* mi/mi-symbol-cmds.c (mi_cmd_symbol_list_lines)
	(output_nondebug_symbol): Update.
	* mdebugread.c (parse_symbol, basic_type, parse_partial_symbols)
	(mdebug_expand_psymtab): Update.
	* machoread.c (macho_add_oso_symfile): Update.
	* linux-tdep.c (linux_infcall_mmap, linux_infcall_munmap):
	Update.
	* linux-fork.c (checkpoint_command): Update.
	* linespec.c (convert_linespec_to_sals): Update.
	* jit.c (finalize_symtab): Update.
	* infrun.c (insert_exception_resume_from_probe): Update.
	* ia64-tdep.c (ia64_find_unwind_table): Update.
	* hppa-tdep.c (internalize_unwinds): Update.
	* gdbtypes.c (get_type_arch, init_float_type, objfile_type):
	Update.
	* gcore.c (call_target_sbrk): Update.
	* elfread.c (record_minimal_symbol, elf_symtab_read)
	(elf_rel_plt_read, elf_gnu_ifunc_record_cache)
	(elf_gnu_ifunc_resolve_by_got): Update.
	* dwarf2/read.c (create_addrmap_from_index)
	(create_addrmap_from_aranges, dw2_find_pc_sect_compunit_symtab)
	(read_debug_names_from_section)
	(process_psymtab_comp_unit_reader, add_partial_symbol)
	(add_partial_subprogram, process_full_comp_unit)
	(read_file_scope, read_func_scope, read_lexical_block_scope)
	(read_call_site_scope, dwarf2_ranges_read)
	(dwarf2_record_block_ranges, dwarf2_add_field)
	(mark_common_block_symbol_computed, read_tag_pointer_type)
	(read_tag_string_type, dwarf2_init_float_type)
	(dwarf2_init_complex_target_type, read_base_type)
	(partial_die_info::read, partial_die_info::read)
	(read_attribute_value, dwarf_decode_lines_1, new_symbol)
	(dwarf2_fetch_die_loc_sect_off): Update.
	* dwarf2/loc.c (dwarf2_find_location_expression)
	(class dwarf_evaluate_loc_desc, rw_pieced_value)
	(dwarf2_evaluate_loc_desc_full, dwarf2_locexpr_baton_eval)
	(dwarf2_loc_desc_get_symbol_read_needs)
	(locexpr_describe_location_piece, locexpr_describe_location_1)
	(loclist_describe_location): Update.
	* dwarf2/index-write.c (write_debug_names): Update.
	* dwarf2/frame.c (dwarf2_build_frame_info): Update.
	* dtrace-probe.c (dtrace_process_dof): Update.
	* dbxread.c (read_dbx_symtab, dbx_end_psymtab)
	(process_one_symbol): Update.
	* ctfread.c (ctf_init_float_type, read_base_type): Update.
	* coffread.c (coff_symtab_read, enter_linenos, decode_base_type)
	(coff_read_enum_type): Update.
	* cli/cli-cmds.c (edit_command, list_command): Update.
	* buildsym.c (buildsym_compunit::finish_block_internal): Update.
	* breakpoint.c (create_overlay_event_breakpoint)
	(create_longjmp_master_breakpoint)
	(create_std_terminate_master_breakpoint)
	(create_exception_master_breakpoint, get_sal_arch): Update.
	* block.c (block_gdbarch): Update.
	* annotate.c (annotate_source_line): Update.
2020-04-18 08:35:04 -06:00

495 lines
14 KiB
C

/* MI Command Set - symbol commands.
Copyright (C) 2003-2020 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "mi-cmds.h"
#include "symtab.h"
#include "objfiles.h"
#include "ui-out.h"
#include "source.h"
#include "mi-getopt.h"
/* Print the list of all pc addresses and lines of code for the
provided (full or base) source file name. The entries are sorted
in ascending PC order. */
void
mi_cmd_symbol_list_lines (const char *command, char **argv, int argc)
{
struct gdbarch *gdbarch;
char *filename;
struct symtab *s;
int i;
struct ui_out *uiout = current_uiout;
if (argc != 1)
error (_("-symbol-list-lines: Usage: SOURCE_FILENAME"));
filename = argv[0];
s = lookup_symtab (filename);
if (s == NULL)
error (_("-symbol-list-lines: Unknown source file name."));
/* Now, dump the associated line table. The pc addresses are
already sorted by increasing values in the symbol table, so no
need to perform any other sorting. */
gdbarch = SYMTAB_OBJFILE (s)->arch ();
ui_out_emit_list list_emitter (uiout, "lines");
if (SYMTAB_LINETABLE (s) != NULL && SYMTAB_LINETABLE (s)->nitems > 0)
for (i = 0; i < SYMTAB_LINETABLE (s)->nitems; i++)
{
ui_out_emit_tuple tuple_emitter (uiout, NULL);
uiout->field_core_addr ("pc", gdbarch, SYMTAB_LINETABLE (s)->item[i].pc);
uiout->field_signed ("line", SYMTAB_LINETABLE (s)->item[i].line);
}
}
/* Used by the -symbol-info-* and -symbol-info-module-* commands to print
information about the symbol SYM in a block of index BLOCK (either
GLOBAL_BLOCK or STATIC_BLOCK). KIND is the kind of symbol we searched
for in order to find SYM, which impact which fields are displayed in the
results. */
static void
output_debug_symbol (ui_out *uiout, enum search_domain kind,
struct symbol *sym, int block)
{
ui_out_emit_tuple tuple_emitter (uiout, NULL);
if (SYMBOL_LINE (sym) != 0)
uiout->field_unsigned ("line", SYMBOL_LINE (sym));
uiout->field_string ("name", sym->print_name ());
if (kind == FUNCTIONS_DOMAIN || kind == VARIABLES_DOMAIN)
{
string_file tmp_stream;
type_print (SYMBOL_TYPE (sym), "", &tmp_stream, -1);
uiout->field_string ("type", tmp_stream.string ());
std::string str = symbol_to_info_string (sym, block, kind);
uiout->field_string ("description", str);
}
}
/* Actually output one nondebug symbol, puts a tuple emitter in place
and then outputs the fields for this msymbol. */
static void
output_nondebug_symbol (ui_out *uiout,
const struct bound_minimal_symbol &msymbol)
{
struct gdbarch *gdbarch = msymbol.objfile->arch ();
ui_out_emit_tuple tuple_emitter (uiout, NULL);
uiout->field_core_addr ("address", gdbarch,
BMSYMBOL_VALUE_ADDRESS (msymbol));
uiout->field_string ("name", msymbol.minsym->print_name ());
}
/* This is the guts of the commands '-symbol-info-functions',
'-symbol-info-variables', and '-symbol-info-types'. It searches for
symbols matching KING, NAME_REGEXP, TYPE_REGEXP, and EXCLUDE_MINSYMS,
and then prints the matching [m]symbols in an MI structured format. */
static void
mi_symbol_info (enum search_domain kind, const char *name_regexp,
const char *type_regexp, bool exclude_minsyms,
size_t max_results)
{
global_symbol_searcher sym_search (kind, name_regexp);
sym_search.set_symbol_type_regexp (type_regexp);
sym_search.set_exclude_minsyms (exclude_minsyms);
sym_search.set_max_search_results (max_results);
std::vector<symbol_search> symbols = sym_search.search ();
ui_out *uiout = current_uiout;
int i = 0;
ui_out_emit_tuple outer_symbols_emitter (uiout, "symbols");
/* Debug symbols are placed first. */
if (i < symbols.size () && symbols[i].msymbol.minsym == nullptr)
{
ui_out_emit_list debug_symbols_list_emitter (uiout, "debug");
/* As long as we have debug symbols... */
while (i < symbols.size () && symbols[i].msymbol.minsym == nullptr)
{
symtab *symtab = symbol_symtab (symbols[i].symbol);
ui_out_emit_tuple symtab_tuple_emitter (uiout, nullptr);
uiout->field_string ("filename",
symtab_to_filename_for_display (symtab));
uiout->field_string ("fullname", symtab_to_fullname (symtab));
ui_out_emit_list symbols_list_emitter (uiout, "symbols");
/* As long as we have debug symbols from this symtab... */
for (; (i < symbols.size ()
&& symbols[i].msymbol.minsym == nullptr
&& symbol_symtab (symbols[i].symbol) == symtab);
++i)
{
symbol_search &s = symbols[i];
output_debug_symbol (uiout, kind, s.symbol, s.block);
}
}
}
/* Non-debug symbols are placed after. */
if (i < symbols.size ())
{
ui_out_emit_list nondebug_symbols_list_emitter (uiout, "nondebug");
/* As long as we have nondebug symbols... */
for (; i < symbols.size (); i++)
{
gdb_assert (symbols[i].msymbol.minsym != nullptr);
output_nondebug_symbol (uiout, symbols[i].msymbol);
}
}
}
/* Helper to parse the option text from an -max-results argument and return
the parsed value. If the text can't be parsed then an error is thrown. */
static size_t
parse_max_results_option (char *arg)
{
char *ptr = arg;
long long val = strtoll (arg, &ptr, 10);
if (arg == ptr || *ptr != '\0' || val > SIZE_MAX || val < 0)
error (_("invalid value for --max-results argument"));
size_t max_results = (size_t) val;
return max_results;
}
/* Helper for mi_cmd_symbol_info_{functions,variables} - depending on KIND.
Processes command line options from ARGV and ARGC. */
static void
mi_info_functions_or_variables (enum search_domain kind, char **argv, int argc)
{
size_t max_results = SIZE_MAX;
const char *regexp = nullptr;
const char *t_regexp = nullptr;
bool exclude_minsyms = true;
enum opt
{
INCLUDE_NONDEBUG_OPT, TYPE_REGEXP_OPT, NAME_REGEXP_OPT, MAX_RESULTS_OPT
};
static const struct mi_opt opts[] =
{
{"-include-nondebug" , INCLUDE_NONDEBUG_OPT, 0},
{"-type", TYPE_REGEXP_OPT, 1},
{"-name", NAME_REGEXP_OPT, 1},
{"-max-results", MAX_RESULTS_OPT, 1},
{ 0, 0, 0 }
};
int oind = 0;
char *oarg = nullptr;
while (1)
{
const char *cmd_string
= ((kind == FUNCTIONS_DOMAIN)
? "-symbol-info-functions" : "-symbol-info-variables");
int opt = mi_getopt (cmd_string, argc, argv, opts, &oind, &oarg);
if (opt < 0)
break;
switch ((enum opt) opt)
{
case INCLUDE_NONDEBUG_OPT:
exclude_minsyms = false;
break;
case TYPE_REGEXP_OPT:
t_regexp = oarg;
break;
case NAME_REGEXP_OPT:
regexp = oarg;
break;
case MAX_RESULTS_OPT:
max_results = parse_max_results_option (oarg);
break;
}
}
mi_symbol_info (kind, regexp, t_regexp, exclude_minsyms, max_results);
}
/* Type for an iterator over a vector of module_symbol_search results. */
typedef std::vector<module_symbol_search>::const_iterator
module_symbol_search_iterator;
/* Helper for mi_info_module_functions_or_variables. Display the results
from ITER up to END or until we find a symbol that is in a different
module, or in a different symtab than the first symbol we print. Update
and return the new value for ITER. */
static module_symbol_search_iterator
output_module_symbols_in_single_module_and_file
(struct ui_out *uiout, module_symbol_search_iterator iter,
const module_symbol_search_iterator end, enum search_domain kind)
{
/* The symbol for the module in which the first result resides. */
const symbol *first_module_symbol = iter->first.symbol;
/* The symbol for the first result, and the symtab in which it resides. */
const symbol *first_result_symbol = iter->second.symbol;
symtab *first_symbtab = symbol_symtab (first_result_symbol);
/* Formatted output. */
ui_out_emit_tuple current_file (uiout, nullptr);
uiout->field_string ("filename",
symtab_to_filename_for_display (first_symbtab));
uiout->field_string ("fullname", symtab_to_fullname (first_symbtab));
ui_out_emit_list item_list (uiout, "symbols");
/* Repeatedly output result symbols until either we run out of symbols,
we change module, or we change symtab. */
for (; (iter != end
&& first_module_symbol == iter->first.symbol
&& first_symbtab == symbol_symtab (iter->second.symbol));
++iter)
output_debug_symbol (uiout, kind, iter->second.symbol,
iter->second.block);
return iter;
}
/* Helper for mi_info_module_functions_or_variables. Display the results
from ITER up to END or until we find a symbol that is in a different
module than the first symbol we print. Update and return the new value
for ITER. */
static module_symbol_search_iterator
output_module_symbols_in_single_module
(struct ui_out *uiout, module_symbol_search_iterator iter,
const module_symbol_search_iterator end, enum search_domain kind)
{
gdb_assert (iter->first.symbol != nullptr);
gdb_assert (iter->second.symbol != nullptr);
/* The symbol for the module in which the first result resides. */
const symbol *first_module_symbol = iter->first.symbol;
/* Create output formatting. */
ui_out_emit_tuple module_tuple (uiout, nullptr);
uiout->field_string ("module", first_module_symbol->print_name ());
ui_out_emit_list files_list (uiout, "files");
/* The results are sorted so that symbols within the same file are next
to each other in the list. Calling the output function once will
print all results within a single file. We keep calling the output
function until we change module. */
while (iter != end && first_module_symbol == iter->first.symbol)
iter = output_module_symbols_in_single_module_and_file (uiout, iter,
end, kind);
return iter;
}
/* Core of -symbol-info-module-functions and -symbol-info-module-variables.
KIND indicates what we are searching for, and ARGV and ARGC are the
command line options passed to the MI command. */
static void
mi_info_module_functions_or_variables (enum search_domain kind,
char **argv, int argc)
{
const char *module_regexp = nullptr;
const char *regexp = nullptr;
const char *type_regexp = nullptr;
/* Process the command line options. */
enum opt
{
MODULE_REGEXP_OPT, TYPE_REGEXP_OPT, NAME_REGEXP_OPT
};
static const struct mi_opt opts[] =
{
{"-module", MODULE_REGEXP_OPT, 1},
{"-type", TYPE_REGEXP_OPT, 1},
{"-name", NAME_REGEXP_OPT, 1},
{ 0, 0, 0 }
};
int oind = 0;
char *oarg = nullptr;
while (1)
{
const char *cmd_string
= ((kind == FUNCTIONS_DOMAIN)
? "-symbol-info-module-functions"
: "-symbol-info-module-variables");
int opt = mi_getopt (cmd_string, argc, argv, opts, &oind, &oarg);
if (opt < 0)
break;
switch ((enum opt) opt)
{
case MODULE_REGEXP_OPT:
module_regexp = oarg;
break;
case TYPE_REGEXP_OPT:
type_regexp = oarg;
break;
case NAME_REGEXP_OPT:
regexp = oarg;
break;
}
}
std::vector<module_symbol_search> module_symbols
= search_module_symbols (module_regexp, regexp, type_regexp, kind);
struct ui_out *uiout = current_uiout;
ui_out_emit_list all_matching_symbols (uiout, "symbols");
/* The results in the module_symbols list are ordered so symbols in the
same module are next to each other. Repeatedly call the output
function to print sequences of symbols that are in the same module
until we have no symbols left to print. */
module_symbol_search_iterator iter = module_symbols.begin ();
const module_symbol_search_iterator end = module_symbols.end ();
while (iter != end)
iter = output_module_symbols_in_single_module (uiout, iter, end, kind);
}
/* Implement -symbol-info-functions command. */
void
mi_cmd_symbol_info_functions (const char *command, char **argv, int argc)
{
mi_info_functions_or_variables (FUNCTIONS_DOMAIN, argv, argc);
}
/* Implement -symbol-info-module-functions command. */
void
mi_cmd_symbol_info_module_functions (const char *command, char **argv,
int argc)
{
mi_info_module_functions_or_variables (FUNCTIONS_DOMAIN, argv, argc);
}
/* Implement -symbol-info-module-variables command. */
void
mi_cmd_symbol_info_module_variables (const char *command, char **argv,
int argc)
{
mi_info_module_functions_or_variables (VARIABLES_DOMAIN, argv, argc);
}
/* Implement -symbol-inf-modules command. */
void
mi_cmd_symbol_info_modules (const char *command, char **argv, int argc)
{
size_t max_results = SIZE_MAX;
const char *regexp = nullptr;
enum opt
{
NAME_REGEXP_OPT, MAX_RESULTS_OPT
};
static const struct mi_opt opts[] =
{
{"-name", NAME_REGEXP_OPT, 1},
{"-max-results", MAX_RESULTS_OPT, 1},
{ 0, 0, 0 }
};
int oind = 0;
char *oarg = nullptr;
while (1)
{
int opt = mi_getopt ("-symbol-info-modules", argc, argv, opts,
&oind, &oarg);
if (opt < 0)
break;
switch ((enum opt) opt)
{
case NAME_REGEXP_OPT:
regexp = oarg;
break;
case MAX_RESULTS_OPT:
max_results = parse_max_results_option (oarg);
break;
}
}
mi_symbol_info (MODULES_DOMAIN, regexp, nullptr, true, max_results);
}
/* Implement -symbol-info-types command. */
void
mi_cmd_symbol_info_types (const char *command, char **argv, int argc)
{
size_t max_results = SIZE_MAX;
const char *regexp = nullptr;
enum opt
{
NAME_REGEXP_OPT, MAX_RESULTS_OPT
};
static const struct mi_opt opts[] =
{
{"-name", NAME_REGEXP_OPT, 1},
{"-max-results", MAX_RESULTS_OPT, 1},
{ 0, 0, 0 }
};
int oind = 0;
char *oarg = nullptr;
while (true)
{
int opt = mi_getopt ("-symbol-info-types", argc, argv, opts,
&oind, &oarg);
if (opt < 0)
break;
switch ((enum opt) opt)
{
case NAME_REGEXP_OPT:
regexp = oarg;
break;
case MAX_RESULTS_OPT:
max_results = parse_max_results_option (oarg);
break;
}
}
mi_symbol_info (TYPES_DOMAIN, regexp, nullptr, true, max_results);
}
/* Implement -symbol-info-variables command. */
void
mi_cmd_symbol_info_variables (const char *command, char **argv, int argc)
{
mi_info_functions_or_variables (VARIABLES_DOMAIN, argv, argc);
}