Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFC PATCH] gdb: create symbols for JITed code
@ 2022-02-08 11:56 Jan Vrany via Gdb-patches
  0 siblings, 0 replies; only message in thread
From: Jan Vrany via Gdb-patches @ 2022-02-08 11:56 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

While experimenting with JIT reader API I realized that no symbols are
created for JITed code. Running jit-reader.exp test, "info symbol" does
not show JIT symbol:

   (gdb) info symbol jit_function_add
   No symbol "jit_function_add" in current context.

Nor are JIT symbols available in Python:

   (gdb) python print(gdb.lookup_symbol("jit_function_add"))
   (None, False)

This work-in-progress patch fixes some of the issues by adding created
symbols to objfile's static block.

To make 'info symbol some_jit_symbol' work, I had to add special case to
info_symbol_command () since current implementation walks sections and
for dynamic (JIT) objfiles, there are no sections. Also, I had to change
find_symbol_at_address () to also report LOC_BLOCK symbols, not only
LOC_STATIC.

I do not pretend I understand GDB's internal symbol structure and how
it should be used and whether this is a good direction. Hence this RFC.

Later, I'd like to introduce Python API to create (no only JIT) symbols
in GDB.

TODO:
 * fix finalize_symtab to allow for more than PENDINGSIZE symbols per
   JIT objfile
 * introduce objfile::find_symbol_at_address () and use if from
   find_symbol_at_address ()
 * test (and fix) JIT PC-to-source mapping and line breakpoints.
---
 gdb/jit.c                             | 27 +++++++++++++++++++++++----
 gdb/objfiles.h                        |  6 ++++++
 gdb/printcmd.c                        | 14 +++++++++++++-
 gdb/symtab.c                          |  4 ++++
 gdb/testsuite/gdb.base/jit-reader.exp | 17 +++++++++++++++++
 5 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/gdb/jit.c b/gdb/jit.c
index 7819d763ab3..83a8489a1b5 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -42,6 +42,7 @@
 #include "readline/tilde.h"
 #include "completer.h"
 #include <forward_list>
+#include "buildsym.h"
 
 static std::string jit_reader_dir;
 
@@ -541,8 +542,10 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
 
   /* First run over all the gdb_block objects, creating a real block
      object for each.  Simultaneously, keep setting the real_block
-     fields.  */
+     fields and create a list of static symbols to put into static block 
+     below */
   int block_idx = FIRST_LOCAL_BLOCK;
+  struct pending static_symbols = { nullptr, 0 };
   for (gdb_block &gdb_block_iter : stab->blocks)
     {
       struct block *new_block = allocate_block (&objfile->objfile_obstack);
@@ -578,6 +581,14 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
 
       gdb_block_iter.real_block = new_block;
 
+      /* If the block has no parent, it will go to a static block, so
+         remember it here.  */
+      if (gdb_block_iter.parent == nullptr)
+        {
+	  gdb_assert (static_symbols.nsyms < PENDINGSIZE-1);
+	  static_symbols.symbol[static_symbols.nsyms++] = block_name;
+	}
+
       block_idx++;
     }
 
@@ -590,8 +601,16 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
       new_block = (i == GLOBAL_BLOCK
 		   ? allocate_global_block (&objfile->objfile_obstack)
 		   : allocate_block (&objfile->objfile_obstack));
-      BLOCK_MULTIDICT (new_block)
-	= mdict_create_linear (&objfile->objfile_obstack, NULL);
+      if (i == STATIC_BLOCK)
+        {
+          BLOCK_MULTIDICT (new_block)
+	    = mdict_create_linear (&objfile->objfile_obstack, &static_symbols);
+	}
+      else
+        {
+          BLOCK_MULTIDICT (new_block)
+	    = mdict_create_linear (&objfile->objfile_obstack, nullptr);		
+	}
       BLOCK_SUPERBLOCK (new_block) = block_iter;
       block_iter = new_block;
 
@@ -638,7 +657,7 @@ jit_object_close_impl (struct gdb_symbol_callbacks *cb,
 			       priv_data->entry.symfile_addr));
 
   objfile *objfile = objfile::make (nullptr, objfile_name.c_str (),
-				    OBJF_NOT_FILENAME);
+				    OBJF_NOT_FILENAME | OBJF_READNOW);
   objfile->per_bfd->gdbarch = priv_data->gdbarch;
 
   for (gdb_symtab &symtab : obj->symtabs)
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 8bd76705688..c929ebf2548 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -767,6 +767,12 @@ struct objfile
      next time.  If an objfile does not have the symbols, it will
      never have them.  */
   bool skip_jit_symbol_lookup = false;
+
+  /* Return true if this objfile is dynamic (i.e, created by JIT).  */
+  bool is_dynamic ()
+  {
+    return this->obfd == nullptr;
+  }
 };
 
 /* A deleter for objfile.  */
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 6f9be820b0c..abc6c15dff6 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -1539,6 +1539,7 @@ info_symbol_command (const char *arg, int from_tty)
 
   addr = parse_and_eval_address (arg);
   for (objfile *objfile : current_program_space->objfiles ())
+    {
     ALL_OBJFILE_OSECTIONS (objfile, osect)
       {
 	/* Only process each object file once, even if there's a separate
@@ -1610,8 +1611,19 @@ info_symbol_command (const char *arg, int from_tty)
 		else
 		  printf_filtered (_("%s in section %s\n"),
 				   loc_string, sec_name);
-	  }
+	  }	
       }
+    if (objfile->is_dynamic ())
+      {
+	symbol *sym = find_symbol_at_address (addr);
+	if (sym != nullptr)
+	  {
+	    matches = 1;	    
+	    printf_filtered (_("%s in dynamic symbol file %s\n"),
+			     sym->m_name, objfile_name(objfile));
+	  }
+      }	        
+    }
   if (matches == 0)
     printf_filtered (_("No symbol matches %s.\n"), arg);
 }
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 1a39372aad0..4692a37d12c 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3089,6 +3089,10 @@ find_symbol_at_address (CORE_ADDR address)
 	      if (sym->aclass () == LOC_STATIC
 		  && SYMBOL_VALUE_ADDRESS (sym) == addr)
 		return sym;
+	      else if (sym->aclass () == LOC_BLOCK
+	          && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= addr
+		  && BLOCK_END  (SYMBOL_BLOCK_VALUE (sym)) > addr)
+		return sym;  
 	    }
 	}
       return nullptr;
diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base/jit-reader.exp
index d94360cd7d9..e06d2daab91 100644
--- a/gdb/testsuite/gdb.base/jit-reader.exp
+++ b/gdb/testsuite/gdb.base/jit-reader.exp
@@ -124,6 +124,8 @@ proc jit_reader_test {} {
     gdb_run_cmd
     gdb_test "" "Program received signal SIGTRAP, .*" "expect SIGTRAP"
 
+    
+
     # Test the JIT reader unwinder.
     with_test_prefix "with jit-reader" {
 
@@ -236,7 +238,22 @@ proc jit_reader_test {} {
 		gdb_test "python print(list(map(lambda objf : objf.filename, gdb.objfiles())))" \
 		    "$any'<< JIT compiled code at $hex >>'$any" \
 		    "python gdb.Objfile.filename"
+
+		gdb_test "python print(gdb.lookup_symbol(\"jit_function_add\"))" \
+		    "$any<gdb.Symbol object at $hex>$any" \
+		    "python gdb.lookup_symbol"
 	    }
+
+	    gdb_test "info symbol jit_function_add" \
+		"jit_function_add in dynamic symbol file << JIT compiled code at $hex >>" \
+		"info symbol jit_function_add"
+
+	    gdb_test "b jit_function_add" \
+		"Breakpoint \[0-9]\+ at $hex" \
+		"b jit_function_add"
+
+	    gdb_test_no_output "del \$bpnum"
+
 	}
     }
 
-- 
2.30.2


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-08 11:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-08 11:56 [RFC PATCH] gdb: create symbols for JITed code Jan Vrany via Gdb-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox