From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id 907F03858D28; Tue, 12 Apr 2022 18:21:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 907F03858D28 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Simon Marchi To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: change subfile::line_vector to an std::vector X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: b08c778be92ec638f4f8e6d8d7153c1456b460c8 X-Git-Newrev: 558802e4d1c5dcbd0df7d2c6ef62a6deac247a2f Message-Id: <20220412182111.907F03858D28@sourceware.org> Date: Tue, 12 Apr 2022 18:21:11 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 18:21:11 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D558802e4d1c5= dcbd0df7d2c6ef62a6deac247a2f commit 558802e4d1c5dcbd0df7d2c6ef62a6deac247a2f Author: Simon Marchi Date: Thu Apr 7 08:55:16 2022 -0400 gdb: change subfile::line_vector to an std::vector =20 Change this field to an std::vector to facilitate memory management. Since the linetable_entry array is copied into the symtab resulting from the subfile, it is possible to change it without changing how symtab stores the linetable entries (which would be a much larger change). =20 There is a small change in buildsym_compunit::record_line to avoid accessing a now invalid linetable_entry. Before this patch, we keep a pointer to the last linetable entry, pop it from the vector, and then read last->line. It works with the manually-maintained array, but since we now use std::vector::pop_back, I am afraid that it could be flagged as an invalid access by the various static / dynamic analysis tools to access the linetable_entry object after popping it from the vector. Instead, record just the line number in an optional and use it. =20 There are substantial changes in xcoffread.c that simplify the code, but I can't test them. I was hesitant to do this change because of that, but I decided to send it anyway. I don't think that an almost dead platform should hold back improving the code in the common parts of GDB. =20 The changes in xcoffread.c are: =20 - Make arrange_linetable "arrange" the linetable passed as a parameter, instead of returning possibly a new one, possibly the same one. - In the "Process main file's line numbers.", I'm not too sure what happens. We get the lintable from "main_subfile", "arrange" it, but then assign the result to the current subfile, obtained with get_current_subfile. I assume that the current subfile is also the main one, so now I just call arrange_linetable on the main subfile's line table. - Remove that weird "Useless if!!!" FIXME comment. It's been there forever, but the "if" is still there, so I guess the "if" can stay there. =20 Change-Id: I11799006fd85189e8cf5bd3a168f8f38c2c27a80 Diff: --- gdb/buildsym.c | 94 ++++++++++++++++---------------------------- gdb/buildsym.h | 6 +-- gdb/xcoffread.c | 118 +++++++++++++++-------------------------------------= ---- 3 files changed, 67 insertions(+), 151 deletions(-) diff --git a/gdb/buildsym.c b/gdb/buildsym.c index 034db598735..628903d674f 100644 --- a/gdb/buildsym.c +++ b/gdb/buildsym.c @@ -49,13 +49,6 @@ struct pending_block struct block *block; }; =20 -/* Initial sizes of data structures. These are realloc'd larger if - needed, and realloc'd down to the size actually used, when - completed. */ - -#define INITIAL_LINE_VECTOR_LENGTH 1000 -=0C - buildsym_compunit::buildsym_compunit (struct objfile *objfile_, const char *name, const char *comp_dir_, @@ -95,7 +88,6 @@ buildsym_compunit::~buildsym_compunit () subfile =3D nextsub) { nextsub =3D subfile->next; - xfree (subfile->line_vector); delete subfile; } =20 @@ -536,9 +528,6 @@ buildsym_compunit::start_subfile (const char *name) =20 m_current_subfile =3D subfile.get (); =20 - /* Initialize line-number recording for this subfile. */ - subfile->line_vector =3D NULL; - /* Default the source language to whatever can be deduced from the filename. If nothing can be deduced (such as for a C/C++ include file with a ".h" extension), then inherit whatever language the @@ -657,28 +646,7 @@ void buildsym_compunit::record_line (struct subfile *subfile, int line, CORE_ADDR pc, linetable_entry_flags flags) { - struct linetable_entry *e; - - /* Make sure line vector exists and is big enough. */ - if (!subfile->line_vector) - { - subfile->line_vector_length =3D INITIAL_LINE_VECTOR_LENGTH; - subfile->line_vector =3D (struct linetable *) - xmalloc (sizeof (struct linetable) - + subfile->line_vector_length * sizeof (struct linetable_entry)); - subfile->line_vector->nitems =3D 0; - m_have_line_numbers =3D true; - } - - if (subfile->line_vector->nitems >=3D subfile->line_vector_length) - { - subfile->line_vector_length *=3D 2; - subfile->line_vector =3D (struct linetable *) - xrealloc ((char *) subfile->line_vector, - (sizeof (struct linetable) - + (subfile->line_vector_length - * sizeof (struct linetable_entry)))); - } + m_have_line_numbers =3D true; =20 /* Normally, we treat lines as unsorted. But the end of sequence marker is special. We sort line markers at the same PC by line @@ -695,25 +663,30 @@ buildsym_compunit::record_line (struct subfile *subfi= le, int line, anyway. */ if (line =3D=3D 0) { - struct linetable_entry *last =3D nullptr; - while (subfile->line_vector->nitems > 0) + gdb::optional last_line; + + while (!subfile->line_vector_entries.empty ()) { - last =3D subfile->line_vector->item + subfile->line_vector->nitems - 1; + linetable_entry *last =3D &subfile->line_vector_entries.back (); + last_line =3D last->line; + if (last->pc !=3D pc) break; - subfile->line_vector->nitems--; + + subfile->line_vector_entries.pop_back (); } =20 /* Ignore an end-of-sequence marker marking an empty sequence. */ - if (last =3D=3D nullptr || last->line =3D=3D 0) + if (!last_line.has_value () || *last_line =3D=3D 0) return; } =20 - e =3D subfile->line_vector->item + subfile->line_vector->nitems++; - e->line =3D line; - e->is_stmt =3D (flags & LEF_IS_STMT) !=3D 0; - e->pc =3D pc; - e->prologue_end =3D (flags & LEF_PROLOGUE_END) !=3D 0; + subfile->line_vector_entries.emplace_back (); + linetable_entry &e =3D subfile->line_vector_entries.back (); + e.line =3D line; + e.is_stmt =3D (flags & LEF_IS_STMT) !=3D 0; + e.pc =3D pc; + e.prologue_end =3D (flags & LEF_PROLOGUE_END) !=3D 0; } =20 =0C @@ -738,7 +711,7 @@ buildsym_compunit::watch_main_source_file_lossage () /* If the main source file doesn't have any line number or symbol info, look for an alias in another subfile. */ =20 - if (mainsub->line_vector =3D=3D NULL + if (mainsub->line_vector_entries.empty () && mainsub->symtab =3D=3D NULL) { const char *mainbase =3D lbasename (mainsub->name.c_str ()); @@ -771,8 +744,8 @@ buildsym_compunit::watch_main_source_file_lossage () Copy its line_vector and symtab to the main subfile and then discard it. */ =20 - mainsub->line_vector =3D mainsub_alias->line_vector; - mainsub->line_vector_length =3D mainsub_alias->line_vector_length; + mainsub->line_vector_entries + =3D std::move (mainsub_alias->line_vector_entries); mainsub->symtab =3D mainsub_alias->symtab; =20 if (prev_mainsub_alias =3D=3D NULL) @@ -929,13 +902,8 @@ buildsym_compunit::end_compunit_symtab_with_blockvector subfile !=3D NULL; subfile =3D subfile->next) { - int linetablesize =3D 0; - - if (subfile->line_vector) + if (!subfile->line_vector_entries.empty ()) { - linetablesize =3D sizeof (struct linetable) + - subfile->line_vector->nitems * sizeof (struct linetable_entry); - const auto lte_is_less_than =3D [] (const linetable_entry &ln1, const linetable_entry &ln2) -> bool @@ -953,9 +921,8 @@ buildsym_compunit::end_compunit_symtab_with_blockvector address, as this maintains the inline function caller/callee relationships, this is why std::stable_sort is used. */ if (m_objfile->flags & OBJF_REORDERED) - std::stable_sort (subfile->line_vector->item, - subfile->line_vector->item - + subfile->line_vector->nitems, + std::stable_sort (subfile->line_vector_entries.begin (), + subfile->line_vector_entries.end (), lte_is_less_than); } =20 @@ -967,13 +934,20 @@ buildsym_compunit::end_compunit_symtab_with_blockvect= or =20 /* Fill in its components. */ =20 - if (subfile->line_vector) + if (!subfile->line_vector_entries.empty ()) { - /* Reallocate the line table on the symbol obstack. */ + /* Reallocate the line table on the objfile obstack. */ + size_t n_entries =3D subfile->line_vector_entries.size (); + size_t entry_array_size =3D n_entries * sizeof (struct linetable_entry); + int linetablesize =3D sizeof (struct linetable) + entry_array_size; + symtab->set_linetable - ((struct linetable *) - obstack_alloc (&m_objfile->objfile_obstack, linetablesize)); - memcpy (symtab->linetable (), subfile->line_vector, linetablesize); + (XOBNEWVAR (&m_objfile->objfile_obstack, struct linetable, + linetablesize)); + + symtab->linetable ()->nitems =3D n_entries; + memcpy (symtab->linetable ()->item, + subfile->line_vector_entries.data (), entry_array_size); } else symtab->set_linetable (nullptr); diff --git a/gdb/buildsym.h b/gdb/buildsym.h index 6284aafc878..ee75e6fd95d 100644 --- a/gdb/buildsym.h +++ b/gdb/buildsym.h @@ -20,6 +20,7 @@ #define BUILDSYM_H 1 =20 #include "gdbsupport/gdb_obstack.h" +#include "symtab.h" =20 struct objfile; struct symbol; @@ -53,10 +54,7 @@ struct subfile =20 struct subfile *next =3D nullptr; std::string name; - - /* Space for this is malloc'd. */ - struct linetable *line_vector =3D nullptr; - int line_vector_length =3D 0; + std::vector line_vector_entries; enum language language =3D language_unknown; struct symtab *symtab =3D nullptr; }; diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c index b89e98ae05f..566c0824cd5 100644 --- a/gdb/xcoffread.c +++ b/gdb/xcoffread.c @@ -239,8 +239,6 @@ static void read_xcoff_symtab (struct objfile *, legacy= _psymtab *); static void add_stab_to_list (char *, struct pending_stabs **); #endif =20 -static struct linetable *arrange_linetable (struct linetable *); - static void record_include_end (struct coff_symbol *); =20 static void process_linenos (CORE_ADDR, CORE_ADDR); @@ -412,89 +410,77 @@ add_stab_to_list (char *stabname, struct pending_stab= s **stabvector) =20 /* Given a line table with function entries are marked, arrange its functions in ascending order and strip off function entry markers - and return it in a newly created table. If the old one is good - enough, return the old one. */ + and return it in a newly created table. */ + /* FIXME: I think all this stuff can be replaced by just passing sort_linevec =3D 1 to end_compunit_symtab. */ =20 -static struct linetable * -arrange_linetable (struct linetable *oldLineTb) +static void +arrange_linetable (std::vector &old_linetable) { int extra_lines =3D 0; =20 std::vector fentries; =20 - for (int ii =3D 0; ii < oldLineTb->nitems; ++ii) + for (int ii =3D 0; ii < old_linetable.size (); ++ii) { - if (oldLineTb->item[ii].is_stmt =3D=3D 0) + if (old_linetable[ii].is_stmt =3D=3D 0) continue; =20 - if (oldLineTb->item[ii].line =3D=3D 0) + if (old_linetable[ii].line =3D=3D 0) { /* Function entry found. */ fentries.emplace_back (); linetable_entry &e =3D fentries.back (); e.line =3D ii; e.is_stmt =3D 1; - e.pc =3D oldLineTb->item[ii].pc; + e.pc =3D old_linetable[ii].pc; =20 /* If the function was compiled with XLC, we may have to add an extra line entry later. Reserve space for that. */ - if (ii + 1 < oldLineTb->nitems - && oldLineTb->item[ii].pc !=3D oldLineTb->item[ii + 1].pc) + if (ii + 1 < old_linetable.size () + && old_linetable[ii].pc !=3D old_linetable[ii + 1].pc) extra_lines++; } } =20 if (fentries.empty ()) - return oldLineTb; + return; =20 std::sort (fentries.begin (), fentries.end (), [] (const linetable_entry <e1, const linetable_entry& lte2) { return lte1.pc < lte2.pc; }); =20 /* Allocate a new line table. */ - int new_linetable_nitems =3D oldLineTb->nitems - fentries.size () + extr= a_lines; - linetable *new_linetable - =3D XNEWVAR (linetable, - (sizeof (struct linetable) - + (new_linetable_nitems * sizeof (struct linetable_entry)))); + std::vector new_linetable; + new_linetable.reserve (old_linetable.size ()); =20 /* If line table does not start with a function beginning, copy up until a function begin. */ - - int newline =3D 0; - if (oldLineTb->item[0].line !=3D 0) - for (newline =3D 0; - newline < oldLineTb->nitems && oldLineTb->item[newline].line; - ++newline) - new_linetable->item[newline] =3D oldLineTb->item[newline]; + for (int i =3D 0; i < old_linetable.size () && old_linetable[i].line != =3D 0; ++i) + new_linetable.push_back (old_linetable[i]); =20 /* Now copy function lines one by one. */ - for (const linetable_entry &entry : fentries) { /* If the function was compiled with XLC, we may have to add an extra line to cover the function prologue. */ int jj =3D entry.line; - if (jj + 1 < oldLineTb->nitems - && oldLineTb->item[jj].pc !=3D oldLineTb->item[jj + 1].pc) + if (jj + 1 < old_linetable.size () + && old_linetable[jj].pc !=3D old_linetable[jj + 1].pc) { - new_linetable->item[newline] =3D oldLineTb->item[jj]; - new_linetable->item[newline].line =3D oldLineTb->item[jj + 1].line; - newline++; + new_linetable.push_back (old_linetable[jj]); + new_linetable.back ().line =3D old_linetable[jj + 1].line; } =20 for (jj =3D entry.line + 1; - jj < oldLineTb->nitems && oldLineTb->item[jj].line !=3D 0; - ++jj, ++newline) - new_linetable->item[newline] =3D oldLineTb->item[jj]; + jj < old_linetable.size () && old_linetable[jj].line !=3D 0; + ++jj) + new_linetable.push_back (old_linetable[jj]); } =20 - /* The number of items in the line table must include these - extra lines which were added in case of XLC compiled functions. */ - new_linetable->nitems =3D new_linetable_nitems; - return new_linetable; + new_linetable.shrink_to_fit (); + old_linetable =3D std::move (new_linetable); } =20 /* include file support: C_BINCL/C_EINCL pairs will be kept in the=20 @@ -595,7 +581,7 @@ static struct objfile *this_symtab_objfile; static void process_linenos (CORE_ADDR start, CORE_ADDR end) { - int offset, ii; + int offset; file_ptr max_offset =3D XCOFF_DATA (this_symtab_objfile)->max_lineno_offset; =20 @@ -631,7 +617,7 @@ process_linenos (CORE_ADDR start, CORE_ADDR end) coff_data (this_symtab_objfile->obfd)->local_linesz; main_source_baseline =3D 0; =20 - for (ii =3D 0; ii < inclIndx; ++ii) + for (int ii =3D 0; ii < inclIndx; ++ii) { /* If there is main file source before include file, enter it. */ if (offset < inclTable[ii].begin) @@ -678,49 +664,23 @@ process_linenos (CORE_ADDR start, CORE_ADDR end) } =20 /* Process main file's line numbers. */ - if (main_subfile.line_vector) + if (!main_subfile.line_vector_entries.empty ()) { - struct linetable *lineTb, *lv; - - lv =3D main_subfile.line_vector; - /* Line numbers are not necessarily ordered. xlc compilation will put static function to the end. */ - - struct subfile *current_subfile =3D get_current_subfile (); - lineTb =3D arrange_linetable (lv); - if (lv =3D=3D lineTb) - { - current_subfile->line_vector =3D (struct linetable *) - xrealloc (lv, (sizeof (struct linetable) - + lv->nitems * sizeof (struct linetable_entry))); - } - else - { - xfree (lv); - current_subfile->line_vector =3D lineTb; - } - - current_subfile->line_vector_length =3D - current_subfile->line_vector->nitems; + arrange_linetable (main_subfile.line_vector_entries); } =20 /* Now, process included files' line numbers. */ =20 - for (ii =3D 0; ii < inclIndx; ++ii) + for (int ii =3D 0; ii < inclIndx; ++ii) { if (inclTable[ii].subfile !=3D ((struct subfile *) &main_subfile) - && (inclTable[ii].subfile)->line_vector) /* Useless if!!! - FIXMEmgo */ + && !inclTable[ii].subfile->line_vector_entries.empty ()) { - struct linetable *lineTb, *lv; - - lv =3D (inclTable[ii].subfile)->line_vector; - /* Line numbers are not necessarily ordered. xlc compilation will put static function to the end. */ - - lineTb =3D arrange_linetable (lv); + arrange_linetable (inclTable[ii].subfile->line_vector_entries); =20 push_subfile (); =20 @@ -755,22 +715,6 @@ process_linenos (CORE_ADDR start, CORE_ADDR end) current_subfile->name =3D inclTable[ii].name; #endif =20 - if (lv =3D=3D lineTb) - { - current_subfile->line_vector =3D - (struct linetable *) xrealloc - (lv, (sizeof (struct linetable) - + lv->nitems * sizeof (struct linetable_entry))); - - } - else - { - xfree (lv); - current_subfile->line_vector =3D lineTb; - } - - current_subfile->line_vector_length =3D - current_subfile->line_vector->nitems; start_subfile (pop_subfile ()); } }