public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Matheus Branco Borella <dark.ryu.550@gmail.com>
To: gdb-patches@sourceware.org
Cc: Matheus Branco Borella <dark.ryu.550@gmail.com>
Subject: [PATCH] Add name_of_main and language_of_main to the DWARF index
Date: Thu,  8 Jun 2023 18:40:13 -0300	[thread overview]
Message-ID: <20230608214012.1561-1-dark.ryu.550@gmail.com> (raw)

This patch adds a new section to the DWARF index containing the name
and the language of the main function symbol, gathered from
`cooked_index::get_main`, if available. Currently, for lack of a better name,
this section is called the "shortcut table" (suggestions for a better name are
appreciated). The way this name is both saved and applied upon an index being
loaded in mirrors how it is done in `cooked_index_functions`, more specifically,
the full name of the main function symbol is saved and `set_objfile_main_name`
is used to apply it after it is loaded.

The main use case for this patch is in improving startup times when dealing with
large binaries. Currently, when an index is used, GDB has to expand symtabs
until it finds out what the language of the main function symbol is. For some
large executables, this may take a considerable amount of time to complete,
slowing down startup. This patch bypasses that operation by having both the name
and language of the main function symbol be provided ahead of time by the index.

In my testing (a binary with about 1.8GB worth of DWARF data) this change brings
startup time down from about 34 seconds to about 1.5 seconds.

(I feel like I might've changed too much about the index format by adding a 
breaking change. If there's a better way to do this it'd be glad to hear about 
it.)

---
 gdb/dwarf2/index-write.c    | 47 +++++++++++++++++++++++++++++++++----
 gdb/dwarf2/read-gdb-index.c | 44 +++++++++++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 6 deletions(-)

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index 62c2cc6ac7..4554b5bc49 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -1080,14 +1080,15 @@ write_gdbindex_1 (FILE *out_file,
 		  const data_buf &types_cu_list,
 		  const data_buf &addr_vec,
 		  const data_buf &symtab_vec,
-		  const data_buf &constant_pool)
+		  const data_buf &constant_pool,
+                  const data_buf &shortcuts)
 {
   data_buf contents;
-  const offset_type size_of_header = 6 * sizeof (offset_type);
+  const offset_type size_of_header = 7 * sizeof (offset_type);
   offset_type total_len = size_of_header;
 
   /* The version number.  */
-  contents.append_offset (8);
+  contents.append_offset (9);
 
   /* The offset of the CU list from the start of the file.  */
   contents.append_offset (total_len);
@@ -1105,6 +1106,10 @@ write_gdbindex_1 (FILE *out_file,
   contents.append_offset (total_len);
   total_len += symtab_vec.size ();
 
+  /* The offset of the shortcut table from the start of the file.  */
+  contents.append_offset (total_len);
+  total_len += shortcuts.size ();
+
   /* The offset of the constant pool from the start of the file.  */
   contents.append_offset (total_len);
   total_len += constant_pool.size ();
@@ -1116,6 +1121,7 @@ write_gdbindex_1 (FILE *out_file,
   types_cu_list.file_write (out_file);
   addr_vec.file_write (out_file);
   symtab_vec.file_write (out_file);
+  shortcuts.file_write (out_file);
   constant_pool.file_write (out_file);
 
   assert_file_size (out_file, total_len);
@@ -1193,6 +1199,34 @@ write_cooked_index (cooked_index *table,
     }
 }
 
+/* Write shortcut information. */
+
+static void
+write_shortcuts_table (cooked_index *table, data_buf& shortcuts,
+                       data_buf& cpool)
+{
+  const auto main_info = table->get_main ();
+  size_t main_name_offset = 0;
+  language lang = language_unknown;
+
+  if (main_info != nullptr)
+    {
+      lang = main_info->per_cu->lang ();
+
+      if (lang != language_unknown)
+        {
+          auto_obstack obstack;
+          const auto main_name = main_info->full_name (&obstack, true);
+
+          main_name_offset = cpool.size ();
+          cpool.append_cstr0 (main_name);
+        }
+    }
+
+  shortcuts.append_uint (4, BFD_ENDIAN_LITTLE, lang);
+  shortcuts.append_offset (main_name_offset);
+}
+
 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
    If OBJFILE has an associated dwz file, write contents of a .gdb_index
    section for that dwz file into DWZ_OUT_FILE.  If OBJFILE does not have an
@@ -1270,11 +1304,14 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table,
 
   write_hash_table (&symtab, symtab_vec, constant_pool);
 
+  data_buf shortcuts;
+  write_shortcuts_table (table, shortcuts, constant_pool);
+
   write_gdbindex_1(out_file, objfile_cu_list, types_cu_list, addr_vec,
-		   symtab_vec, constant_pool);
+		   symtab_vec, constant_pool, shortcuts);
 
   if (dwz_out_file != NULL)
-    write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {});
+    write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {}, {});
   else
     gdb_assert (dwz_cu_list.empty ());
 }
diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
index 1006386cb2..f590c7fb99 100644
--- a/gdb/dwarf2/read-gdb-index.c
+++ b/gdb/dwarf2/read-gdb-index.c
@@ -88,6 +88,9 @@ struct mapped_gdb_index final : public mapped_index_base
   /* A pointer to the constant pool.  */
   gdb::array_view<const gdb_byte> constant_pool;
 
+  /* The shortcut table data. */
+  gdb::array_view<const gdb_byte> shortcut_table;
+
   /* Return the index into the constant pool of the name of the IDXth
      symbol in the symbol table.  */
   offset_type symbol_name_index (offset_type idx) const
@@ -166,6 +169,7 @@ dwarf2_gdb_index::dump (struct objfile *objfile)
 
   mapped_gdb_index *index = (gdb::checked_static_cast<mapped_gdb_index *>
 			     (per_objfile->per_bfd->index_table.get ()));
+
   gdb_printf (".gdb_index: version %d\n", index->version);
   gdb_printf ("\n");
 }
@@ -583,7 +587,7 @@ to use the section anyway."),
 
   /* Indexes with higher version than the one supported by GDB may be no
      longer backward compatible.  */
-  if (version > 8)
+  if (version > 9)
     return 0;
 
   map->version = version;
@@ -608,6 +612,12 @@ to use the section anyway."),
   map->symbol_table
     = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
 						    symbol_table_end));
+  ++i;
+
+  const gdb_byte *shortcut_table = addr + metadata[i];
+  const gdb_byte *shortcut_table_end = addr + metadata[i + 1];
+  map->shortcut_table
+    = gdb::array_view<const gdb_byte> (shortcut_table, shortcut_table_end);
 
   ++i;
   map->constant_pool = buffer.slice (metadata[i]);
@@ -763,6 +773,36 @@ create_addrmap_from_gdb_index (dwarf2_per_objfile *per_objfile,
     = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
 }
 
+/* Sets the name and language of the main function from the shortcut table. */
+
+static void
+set_main_name_from_gdb_index (dwarf2_per_objfile *per_objfile, 
+                              mapped_gdb_index *index)
+{
+  auto ptr = index->shortcut_table.data ();
+  const auto lang = extract_unsigned_integer (ptr, 4, BFD_ENDIAN_LITTLE);
+  if (lang >= nr_languages)
+    {
+      complaint (_(".gdb_index shortcut table has invalid main language %u"),
+                   (unsigned) lang);
+      return;
+    }
+  if (lang == language_unknown)
+    {
+      /* Don't bother if the language for the main symbol was not known or if
+       * there was no main symbol at all when the index was built. */
+      return;
+    }
+  ptr += 4;
+
+  const auto name_offset = extract_unsigned_integer (ptr, 
+                                                     sizeof (offset_type), 
+                                                     BFD_ENDIAN_LITTLE);
+  const auto name = (const char*) (index->constant_pool.data () + name_offset);
+
+  set_objfile_main_name (per_objfile->objfile, name, (enum language) lang);
+}
+
 /* See read-gdb-index.h.  */
 
 int
@@ -848,6 +888,8 @@ dwarf2_read_gdb_index
 
   create_addrmap_from_gdb_index (per_objfile, map.get ());
 
+  set_main_name_from_gdb_index (per_objfile, map.get ());
+
   per_bfd->index_table = std::move (map);
   per_bfd->quick_file_names_table =
     create_quick_file_names_table (per_bfd->all_units.size ());
-- 
2.40.1


             reply	other threads:[~2023-06-08 21:44 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 21:40 Matheus Branco Borella [this message]
2023-06-09 16:56 ` Tom Tromey
2023-06-30 20:36   ` Matheus Branco Borella
2023-07-01  5:47     ` Eli Zaretskii
2023-07-07 15:00       ` Matheus Branco Borella
2023-07-07 18:00         ` Eli Zaretskii
2023-08-04 20:55           ` Tom de Vries
2023-08-05  5:36             ` Eli Zaretskii
2023-08-03  7:12         ` Tom de Vries
2023-08-03  7:29         ` Tom de Vries
2023-08-04 18:09           ` [PATCH v2] " Matheus Branco Borella
2023-08-11 18:21 ` [PATCH v3] " Matheus Branco Borella
2023-08-14  7:31   ` Tom de Vries
2023-08-14  7:36     ` Tom de Vries
2023-09-13  7:09     ` Tom de Vries
2023-09-25 18:47       ` Matheus Branco Borella (DarkRyu550)
2023-09-26 14:07         ` Tom de Vries
2023-10-04 22:30           ` Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230608214012.1561-1-dark.ryu.550@gmail.com \
    --to=dark.ryu.550@gmail.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).