[RFC,v2,01/21] gdb: update is_addr_in_objfile to support "dynamic" objfiles

Message ID 20241121124714.419946-2-jan.vrany@labware.com
State New
Headers
Series Add Python "JIT" API |

Commit Message

Jan Vraný Nov. 21, 2024, 12:46 p.m. UTC
  While working with objfiles in Python I noticed that
gdb.Progspace.objfile_for_address () does not return "dynamic" objfile
create by (for example) GDB's JIT reader API.

This is because is_addr_in_objfile() checks if given address falls into
any (mappped) section of that objfile. However objfiles created by JIT
reader API do not have sections.

To solve this issue, this commit updates is_addr_in_objfile() to also
check if address fall into any compunit if that objfile. It does so only
if objfile has no sections.
---
 gdb/objfiles.c                        | 16 ++++++++++++++++
 gdb/testsuite/gdb.base/jit-reader.exp |  9 +++++++++
 2 files changed, 25 insertions(+)
  

Patch

diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 555195dc61f..0bb578fa6a8 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -1194,6 +1194,22 @@  is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile)
       if (osect->contains (addr))
 	return true;
     }
+  /* Objfiles created dynamically by JIT reader API (and possibly by
+     other means too) do not have sections and therefore the above
+     check never succeeds.
+
+     For such "dynamic" objfiles walk over all compunits and check
+     if given ADDR falls into compunit's global block.  */
+  if (! objfile->sections_start)
+    {
+      for (compunit_symtab *cu
+	     : const_cast<struct objfile *>(objfile)->compunits ())
+	{
+	  global_block *gb = cu->blockvector ()->global_block ();
+	  if (gb->start () <= addr && addr <= gb->end ())
+	    return true;
+	}
+    }
   return false;
 }
 
diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base/jit-reader.exp
index 62f6af29ac1..9a873c77909 100644
--- a/gdb/testsuite/gdb.base/jit-reader.exp
+++ b/gdb/testsuite/gdb.base/jit-reader.exp
@@ -229,6 +229,15 @@  proc jit_reader_test {} {
 		gdb_test "python print( \[o for o in gdb.objfiles() if o.filename.startswith('<< JIT compiled code')\]\[0\].build_id )" \
 		    "None" \
 		    "python gdb.Objfile.build_id"
+
+		# Check that Progspace.objfile_for_address () finds "jitted"
+		# objfile
+		gdb_test "frame 0" \
+		    "#0  $hex in jit_function_stack_mangle ()$any" \
+		    "select frame 0"
+		gdb_test "python print( gdb.current_progspace().objfile_for_address(gdb.parse_and_eval('\$pc')) )" \
+		    "<gdb.Objfile filename=<< JIT compiled code at $hex >>>" \
+		    "python gdb.Progspace.objfile_for_address"
 	    }
 	}
     }