gdb: add a constructor for symtab

Convert symtab to use obstack_new, and have a real constructor.  The
filename, filename_for_id and m_compunit, members should really not
change once the symtab has been created, so make these members private
(m_compunit was already private) and set them just once from the
constructor.  The set_compunit function has been deleted, and new
getter functions for filename and filename_for_id have been added.

The language is also set at construction time, but can be updated
later, so set the language in the constructor, but retain
symtab::set_language for when the language needs to be updated.

Prior to this patch the symtab was allocated with OBSTACK_ZALLOC which
would zero out the symtab object.  With the call to objstack_new
fields in the symtab would no longer be initialised, so I've added
default member initialisation for everything not set in the
constructor.

The interesting changes are in symtab.h, and symfile.c.  Everything
else is just updating to handle symfile::filename and
symfile::filename_for_id becoming methods.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Andrew Burgess
2025-10-31 10:19:41 +00:00
parent 8e9bfa66d8
commit 1ddfd4f3ea
15 changed files with 82 additions and 67 deletions

View File

@@ -3519,8 +3519,8 @@ sort_choices (std::vector<struct block_symbol> &syms)
if (!b.symbol->is_objfile_owned ()) if (!b.symbol->is_objfile_owned ())
return true; return true;
const char *fna = a.symbol->symtab ()->filename; const char *fna = a.symbol->symtab ()->filename ();
const char *fnb = b.symbol->symtab ()->filename; const char *fnb = b.symbol->symtab ()->filename ();
/* First sort by basename. This is done because, /* First sort by basename. This is done because,
depending on how GNAT was invoked, different sources depending on how GNAT was invoked, different sources
@@ -11927,7 +11927,7 @@ is_known_support_routine (const frame_info_ptr &frame)
for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1) for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
{ {
re_comp (known_runtime_file_name_patterns[i]); re_comp (known_runtime_file_name_patterns[i]);
if (re_exec (lbasename (sal.symtab->filename))) if (re_exec (lbasename (sal.symtab->filename ())))
return 1; return 1;
if (sal.symtab->compunit ()->objfile () != NULL if (sal.symtab->compunit ()->objfile () != NULL
&& re_exec (objfile_name (sal.symtab->compunit ()->objfile ()))) && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))

View File

@@ -2228,7 +2228,7 @@ cmp_symtabs (const symtab_and_line &sala, const symtab_and_line &salb)
return r; return r;
} }
r = filename_cmp (sala.symtab->filename, salb.symtab->filename); r = filename_cmp (sala.symtab->filename (), salb.symtab->filename ());
if (r) if (r)
return r; return r;

View File

@@ -56,7 +56,7 @@ convert_one_symbol (compile_c_instance *context,
int is_local) int is_local)
{ {
gcc_type sym_type; gcc_type sym_type;
const char *filename = sym.symbol->symtab ()->filename; const char *filename = sym.symbol->symtab ()->filename ();
unsigned int line = sym.symbol->line (); unsigned int line = sym.symbol->line ();
context->error_symbol_once (sym.symbol); context->error_symbol_once (sym.symbol);

View File

@@ -48,7 +48,7 @@ convert_one_symbol (compile_cplus_instance *instance,
{ {
/* Squash compiler warning. */ /* Squash compiler warning. */
gcc_type sym_type = 0; gcc_type sym_type = 0;
const char *filename = sym.symbol->symtab ()->filename; const char *filename = sym.symbol->symtab ()->filename ();
unsigned int line = sym.symbol->line (); unsigned int line = sym.symbol->line ();
instance->error_symbol_once (sym.symbol); instance->error_symbol_once (sym.symbol);

View File

@@ -624,7 +624,7 @@ compile_cplus_convert_struct_or_union_members
we can do but ignore this member. */ we can do but ignore this member. */
continue; continue;
} }
const char *filename = sym.symbol->symtab ()->filename; const char *filename = sym.symbol->symtab ()->filename ();
unsigned int line = sym.symbol->line (); unsigned int line = sym.symbol->line ();
physaddr = sym.symbol->value_address (); physaddr = sym.symbol->value_address ();
@@ -763,7 +763,7 @@ compile_cplus_convert_struct_or_union_methods (compile_cplus_instance *instance,
continue; continue;
} }
const char *filename = sym.symbol->symtab ()->filename; const char *filename = sym.symbol->symtab ()->filename ();
unsigned int line = sym.symbol->line (); unsigned int line = sym.symbol->line ();
CORE_ADDR address = sym.symbol->value_block()->start (); CORE_ADDR address = sym.symbol->value_block()->start ();
const char *kind; const char *kind;

View File

@@ -4842,7 +4842,7 @@ set_step_info (thread_info *tp, const frame_info_ptr &frame,
infrun_debug_printf infrun_debug_printf
("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s", ("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s",
tp->current_symtab != nullptr ? tp->current_symtab->filename : "<null>", tp->current_symtab != nullptr ? tp->current_symtab->filename () : "<null>",
tp->current_line, tp->current_line,
tp->control.step_frame_id.to_string ().c_str (), tp->control.step_frame_id.to_string ().c_str (),
tp->control.step_stack_frame_id.to_string ().c_str ()); tp->control.step_stack_frame_id.to_string ().c_str ());

View File

@@ -2005,7 +2005,7 @@ create_sals_line_offset (struct linespec_state *self,
set_default_source_symtab_and_line (); set_default_source_symtab_and_line ();
initialize_defaults (&self->default_symtab, &self->default_line); initialize_defaults (&self->default_symtab, &self->default_line);
ls->file_symtabs ls->file_symtabs
= collect_symtabs_from_filename (self->default_symtab->filename, = collect_symtabs_from_filename (self->default_symtab->filename (),
self->search_pspace); self->search_pspace);
use_default = true; use_default = true;
} }
@@ -2291,7 +2291,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
/* Make sure we have a filename for canonicalization. */ /* Make sure we have a filename for canonicalization. */
if (ls->explicit_loc.source_filename == NULL) if (ls->explicit_loc.source_filename == NULL)
{ {
const char *filename = state->default_symtab->filename; const char *filename = state->default_symtab->filename ();
/* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
form so that displaying SOURCE_FILENAME can follow the current form so that displaying SOURCE_FILENAME can follow the current

View File

@@ -51,7 +51,8 @@ sal_macro_scope (struct symtab_and_line sal)
macro_scope ms; macro_scope ms;
main_file = macro_main (cust->macro_table ()); main_file = macro_main (cust->macro_table ());
inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id); inclusion = macro_lookup_inclusion (main_file,
sal.symtab->filename_for_id ());
if (inclusion) if (inclusion)
{ {

View File

@@ -1644,7 +1644,7 @@ bploc_filepath (struct symtab *bploc_symtab)
{ {
} }
return host_string_to_python_string (bploc_symtab->filename); return host_string_to_python_string (bploc_symtab->filename ());
} }
/* Python function to get the source file name path and line number /* Python function to get the source file name path and line number
@@ -1763,7 +1763,7 @@ bplocpy_repr (PyObject *py_self)
paddress (self->bp_loc->owner->gdbarch, paddress (self->bp_loc->owner->gdbarch,
self->bp_loc->requested_address)); self->bp_loc->requested_address));
if (self->bp_loc->symtab != nullptr) if (self->bp_loc->symtab != nullptr)
str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename, str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename (),
self->bp_loc->line_number); self->bp_loc->line_number);
const auto fn_name = self->bp_loc->function_name.get (); const auto fn_name = self->bp_loc->function_name.get ();

View File

@@ -488,20 +488,20 @@ skiplist_entry::do_skip_file_p (const symtab_and_line &function_sal) const
if (debug_skip) if (debug_skip)
gdb_printf (gdb_stdlog, gdb_printf (gdb_stdlog,
"skip: checking if file %s matches non-glob %s...", "skip: checking if file %s matches non-glob %s...",
function_sal.symtab->filename, m_file.c_str ()); function_sal.symtab->filename (), m_file.c_str ());
bool result; bool result;
/* Check first sole SYMTAB->FILENAME. It may not be a substring of /* Check first sole SYMTAB->FILENAME. It may not be a substring of
symtab_to_fullname as it may contain "./" etc. */ symtab_to_fullname as it may contain "./" etc. */
if (compare_filenames_for_search (function_sal.symtab->filename, if (compare_filenames_for_search (function_sal.symtab->filename (),
m_file.c_str ())) m_file.c_str ()))
result = true; result = true;
/* Before we invoke realpath, which can get expensive when many /* Before we invoke realpath, which can get expensive when many
files are involved, do a quick comparison of the basenames. */ files are involved, do a quick comparison of the basenames. */
else if (!basenames_may_differ else if (!basenames_may_differ
&& filename_cmp (lbasename (function_sal.symtab->filename), && filename_cmp (lbasename (function_sal.symtab->filename ()),
lbasename (m_file.c_str ())) != 0) lbasename (m_file.c_str ())) != 0)
result = false; result = false;
else else
@@ -524,13 +524,13 @@ skiplist_entry::do_skip_gfile_p (const symtab_and_line &function_sal) const
if (debug_skip) if (debug_skip)
gdb_printf (gdb_stdlog, gdb_printf (gdb_stdlog,
"skip: checking if file %s matches glob %s...", "skip: checking if file %s matches glob %s...",
function_sal.symtab->filename, m_file.c_str ()); function_sal.symtab->filename (), m_file.c_str ());
bool result; bool result;
/* Check first sole SYMTAB->FILENAME. It may not be a substring of /* Check first sole SYMTAB->FILENAME. It may not be a substring of
symtab_to_fullname as it may contain "./" etc. */ symtab_to_fullname as it may contain "./" etc. */
if (gdb_filename_fnmatch (m_file.c_str (), function_sal.symtab->filename, if (gdb_filename_fnmatch (m_file.c_str (), function_sal.symtab->filename (),
FNM_NOESCAPE) == 0) FNM_NOESCAPE) == 0)
result = true; result = true;
@@ -541,7 +541,7 @@ skiplist_entry::do_skip_gfile_p (const symtab_and_line &function_sal) const
isn't much of a win. Oh well. */ isn't much of a win. Oh well. */
else if (!basenames_may_differ else if (!basenames_may_differ
&& gdb_filename_fnmatch (lbasename (m_file.c_str ()), && gdb_filename_fnmatch (lbasename (m_file.c_str ()),
lbasename (function_sal.symtab->filename), lbasename (function_sal.symtab->filename ()),
FNM_NOESCAPE) != 0) FNM_NOESCAPE) != 0)
result = false; result = false;
else else

View File

@@ -342,7 +342,7 @@ select_source_symtab ()
{ {
for (symtab *symtab : cu.filetabs ()) for (symtab *symtab : cu.filetabs ())
{ {
const char *name = symtab->filename; const char *name = symtab->filename ();
int len = strlen (name); int len = strlen (name);
if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
@@ -683,7 +683,7 @@ info_source_command (const char *ignore, int from_tty)
} }
cust = s->compunit (); cust = s->compunit ();
gdb_printf (_("Current source file is %s\n"), s->filename); gdb_printf (_("Current source file is %s\n"), s->filename ());
if (s->compunit ()->dirname () != NULL) if (s->compunit ()->dirname () != NULL)
gdb_printf (_("Compilation directory is %s\n"), s->compunit ()->dirname ()); gdb_printf (_("Compilation directory is %s\n"), s->compunit ()->dirname ());
if (s->fullname () != nullptr) if (s->fullname () != nullptr)
@@ -1119,7 +1119,8 @@ open_source_file (struct symtab *s)
return scoped_fd (-EINVAL); return scoped_fd (-EINVAL);
gdb::unique_xmalloc_ptr<char> fullname = s->release_fullname (); gdb::unique_xmalloc_ptr<char> fullname = s->release_fullname ();
scoped_fd fd = find_and_open_source (s->filename, s->compunit ()->dirname (), scoped_fd fd = find_and_open_source (s->filename (),
s->compunit ()->dirname (),
&fullname); &fullname);
if (fd.get () < 0) if (fd.get () < 0)
@@ -1129,13 +1130,13 @@ open_source_file (struct symtab *s)
const objfile *ofp = s->compunit ()->objfile (); const objfile *ofp = s->compunit ()->objfile ();
std::string srcpath; std::string srcpath;
if (IS_ABSOLUTE_PATH (s->filename)) if (IS_ABSOLUTE_PATH (s->filename ()))
srcpath = s->filename; srcpath = s->filename ();
else if (s->compunit ()->dirname () != nullptr) else if (s->compunit ()->dirname () != nullptr)
{ {
srcpath = s->compunit ()->dirname (); srcpath = s->compunit ()->dirname ();
srcpath += SLASH_STRING; srcpath += SLASH_STRING;
srcpath += s->filename; srcpath += s->filename ();
} }
const struct bfd_build_id *build_id const struct bfd_build_id *build_id
@@ -1220,11 +1221,11 @@ symtab_to_fullname (struct symtab *s)
should report the pathname where GDB tried to find the file. */ should report the pathname where GDB tried to find the file. */
if (s->compunit ()->dirname () == nullptr if (s->compunit ()->dirname () == nullptr
|| IS_ABSOLUTE_PATH (s->filename)) || IS_ABSOLUTE_PATH (s->filename ()))
fullname.reset (xstrdup (s->filename)); fullname.reset (xstrdup (s->filename ()));
else else
fullname.reset (concat (s->compunit ()->dirname (), SLASH_STRING, fullname.reset (concat (s->compunit ()->dirname (), SLASH_STRING,
s->filename, (char *) NULL)); s->filename (), (char *) NULL));
s->set_fullname (rewrite_source_path (fullname.get ())); s->set_fullname (rewrite_source_path (fullname.get ()));
if (s->fullname () == nullptr) if (s->fullname () == nullptr)
@@ -1241,11 +1242,11 @@ const char *
symtab_to_filename_for_display (struct symtab *symtab) symtab_to_filename_for_display (struct symtab *symtab)
{ {
if (filename_display_string == filename_display_basename) if (filename_display_string == filename_display_basename)
return lbasename (symtab->filename); return lbasename (symtab->filename ());
else if (filename_display_string == filename_display_absolute) else if (filename_display_string == filename_display_absolute)
return symtab_to_fullname (symtab); return symtab_to_fullname (symtab);
else if (filename_display_string == filename_display_relative) else if (filename_display_string == filename_display_relative)
return symtab->filename; return symtab->filename ();
else else
internal_error (_("invalid filename_display_string")); internal_error (_("invalid filename_display_string"));
} }

View File

@@ -184,7 +184,7 @@ iterate_over_one_compunit_symtab (const char *name,
for (symtab *s : cust->filetabs ()) for (symtab *s : cust->filetabs ())
{ {
if (compare_filenames_for_search (s->filename, name)) if (compare_filenames_for_search (s->filename (), name))
{ {
if (callback (s)) if (callback (s))
return true; return true;
@@ -194,7 +194,7 @@ iterate_over_one_compunit_symtab (const char *name,
/* Before we invoke realpath, which can get expensive when many /* Before we invoke realpath, which can get expensive when many
files are involved, do a quick comparison of the basenames. */ files are involved, do a quick comparison of the basenames. */
if (! basenames_may_differ if (! basenames_may_differ
&& FILENAME_CMP (base_name, lbasename (s->filename)) != 0) && FILENAME_CMP (base_name, lbasename (s->filename ())) != 0)
continue; continue;
if (compare_filenames_for_search (symtab_to_fullname (s), name)) if (compare_filenames_for_search (symtab_to_fullname (s), name))

View File

@@ -2830,11 +2830,11 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename,
{ {
struct objfile *objfile = cust->objfile (); struct objfile *objfile = cust->objfile ();
struct symtab *symtab struct symtab *symtab
= OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symtab); = obstack_new<struct symtab> (&objfile->objfile_obstack,
cust,
symtab->filename = objfile->intern (filename); objfile->intern (filename),
symtab->filename_for_id = objfile->intern (filename_for_id); objfile->intern (filename_for_id),
symtab->set_language (deduce_language_from_filename (filename)); deduce_language_from_filename (filename));
/* This can be very verbose with lots of headers. /* This can be very verbose with lots of headers.
Only print at higher debug levels. */ Only print at higher debug levels. */
@@ -2860,9 +2860,6 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename,
/* Add it to CUST's list of symtabs. */ /* Add it to CUST's list of symtabs. */
cust->add_filetab (symtab); cust->add_filetab (symtab);
/* Backlink to the containing compunit symtab. */
symtab->set_compunit (cust);
return symtab; return symtab;
} }

View File

@@ -3309,7 +3309,7 @@ find_line_symtab (symtab *sym_tab, int line, int *index)
const struct linetable *l; const struct linetable *l;
int ind; int ind;
if (FILENAME_CMP (sym_tab->filename, s->filename) != 0) if (FILENAME_CMP (sym_tab->filename (), s->filename ()) != 0)
continue; continue;
if (FILENAME_CMP (symtab_to_fullname (sym_tab), if (FILENAME_CMP (symtab_to_fullname (sym_tab),
symtab_to_fullname (s)) != 0) symtab_to_fullname (s)) != 0)
@@ -4627,8 +4627,8 @@ symbol_search::compare_search_syms (const symbol_search &sym_a,
{ {
int c; int c;
c = FILENAME_CMP (sym_a.symbol->symtab ()->filename, c = FILENAME_CMP (sym_a.symbol->symtab ()->filename (),
sym_b.symbol->symtab ()->filename); sym_b.symbol->symtab ()->filename ());
if (c != 0) if (c != 0)
return c; return c;
@@ -4802,9 +4802,9 @@ global_symbol_searcher::add_matching_symbols
/* Check first sole REAL_SYMTAB->FILENAME. It does /* Check first sole REAL_SYMTAB->FILENAME. It does
not need to be a substring of symtab_to_fullname as not need to be a substring of symtab_to_fullname as
it may contain "./" etc. */ it may contain "./" etc. */
if (!(file_matches (real_symtab->filename, m_filenames, false) if (!(file_matches (real_symtab->filename (), m_filenames, false)
|| ((basenames_may_differ || ((basenames_may_differ
|| file_matches (lbasename (real_symtab->filename), || file_matches (lbasename (real_symtab->filename ()),
m_filenames, true)) m_filenames, true))
&& file_matches (symtab_to_fullname (real_symtab), && file_matches (symtab_to_fullname (real_symtab),
m_filenames, false)))) m_filenames, false))))
@@ -6220,14 +6220,14 @@ make_source_files_completion_list (const char *text)
{ {
for (symtab *s : cu.filetabs ()) for (symtab *s : cu.filetabs ())
{ {
if (not_interesting_fname (s->filename)) if (not_interesting_fname (s->filename ()))
continue; continue;
if (!filenames_seen.seen (s->filename) if (!filenames_seen.seen (s->filename ())
&& filename_ncmp (s->filename, text, text_len) == 0) && filename_ncmp (s->filename (), text, text_len) == 0)
{ {
/* This file matches for a completion; add it to the current /* This file matches for a completion; add it to the current
list of matches. */ list of matches. */
add_filename_to_list (s->filename, text, text, &list); add_filename_to_list (s->filename (), text, text, &list);
} }
else else
{ {
@@ -6235,8 +6235,8 @@ make_source_files_completion_list (const char *text)
debug info records leading directories, but not the other debug info records leading directories, but not the other
way around. This is what subroutines of breakpoint way around. This is what subroutines of breakpoint
command do when they parse file names. */ command do when they parse file names. */
base_name = lbasename (s->filename); base_name = lbasename (s->filename ());
if (base_name != s->filename if (base_name != s->filename ()
&& !filenames_seen.seen (base_name) && !filenames_seen.seen (base_name)
&& filename_ncmp (base_name, text, text_len) == 0) && filename_ncmp (base_name, text, text_len) == 0)
add_filename_to_list (base_name, text, text, &list); add_filename_to_list (base_name, text, text, &list);

View File

@@ -1698,16 +1698,23 @@ typedef std::vector<CORE_ADDR> section_offsets;
struct symtab struct symtab
{ {
symtab (struct compunit_symtab *cust, const char *filename,
const char *filename_for_id, enum language language)
: m_filename (filename),
m_filename_for_id (filename_for_id),
m_compunit (cust),
m_language (language)
{
gdb_assert (m_filename != nullptr);
gdb_assert (m_filename_for_id != nullptr);
gdb_assert (m_compunit != nullptr);
}
struct compunit_symtab *compunit () const struct compunit_symtab *compunit () const
{ {
return m_compunit; return m_compunit;
} }
void set_compunit (struct compunit_symtab *compunit)
{
m_compunit = compunit;
}
const struct linetable *linetable () const const struct linetable *linetable () const
{ {
return m_linetable; return m_linetable;
@@ -1749,16 +1756,24 @@ struct symtab
m_fullname = name.release (); m_fullname = name.release ();
} }
const char *filename () const
{ return m_filename; }
const char *filename_for_id () const
{ return m_filename_for_id; }
/* Unordered chain of all filetabs in the compunit, with the exception /* Unordered chain of all filetabs in the compunit, with the exception
that the "main" source file is the first entry in the list. */ that the "main" source file is the first entry in the list. */
struct symtab *next; struct symtab *next = nullptr;
private:
/* Name of this source file, in a form appropriate to print to the user. /* Name of this source file, in a form appropriate to print to the user.
This pointer is never nullptr. */ This pointer is never nullptr and is set from the constructor. */
const char *filename; const char *m_filename;
/* Filename for this source file, used as an identifier to link with /* Filename for this source file, used as an identifier to link with
related objects such as associated macro_source_file objects. It must related objects such as associated macro_source_file objects. It must
@@ -1767,28 +1782,29 @@ struct symtab
follow that rule, or another form of the same file name, this is up to follow that rule, or another form of the same file name, this is up to
the specific debug info reader. the specific debug info reader.
This pointer is never nullptr.*/ This pointer is never nullptr, and is set from the constructor. */
const char *filename_for_id; const char *m_filename_for_id;
private: /* Backlink to containing compunit symtab.
/* Backlink to containing compunit symtab. */ This pointer is never nullptr, and is set from the constructor. */
struct compunit_symtab *m_compunit; struct compunit_symtab *m_compunit;
/* Table mapping core addresses to line numbers for this file. /* Table mapping core addresses to line numbers for this file.
Can be NULL if none. Never shared between different symtabs. */ Can be NULL if none. Never shared between different symtabs. */
const struct linetable *m_linetable; const struct linetable *m_linetable = nullptr;
/* Language of this source file. */ /* Language of this source file. This is set in the object
constructor. */
enum language m_language; enum language m_language;
/* Full name of file as found by searching the source path. /* Full name of file as found by searching the source path.
NULL if not yet known. */ NULL if not yet known. */
char *m_fullname; char *m_fullname = nullptr;
}; };
/* A range adapter to allowing iterating over all the file tables in a list. */ /* A range adapter to allowing iterating over all the file tables in a list. */