Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Jan Vraný" <Jan.Vrany@labware.com>
To: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>,
	"aburgess@redhat.com" <aburgess@redhat.com>
Subject: Re: [RFC v5 15/18] gdb/python: add more attributes to gdb.LinetableEntry objects
Date: Tue, 2 Sep 2025 11:03:56 +0000	[thread overview]
Message-ID: <b3111d5d248bb66d138fc654a1feb8232b1d21cf.camel@labware.com> (raw)
In-Reply-To: <87h5xqqlbs.fsf@redhat.com>

On Fri, 2025-08-29 at 15:00 +0100, Andrew Burgess wrote:
> Jan Vrany <jan.vrany@labware.com> writes:
...
> > +			entry->line, entry->pc (objfile), entry->is_stmt,
> > +			entry->prologue_end, entry->epilogue_begin));
> >  
> >        if (obj == NULL)
> >  	return NULL;
> > @@ -155,24 +169,35 @@ ltpy_get_pcs_for_line (PyObject *self, PyObject *args)
> >  {
> >    struct symtab *symtab;
> >    gdb_py_longest py_line;
> > -  const linetable_entry *best_entry = nullptr;
> > -  std::vector<CORE_ADDR> pcs;
> > +  std::vector<const linetable_entry*> entries;
> >  
> >    LTPY_REQUIRE_VALID (self, symtab);
> >  
> >    if (! PyArg_ParseTuple (args, GDB_PY_LL_ARG, &py_line))
> >      return NULL;
> >  
> > +  if (! symtab->linetable ())
> > +    Py_RETURN_NONE;
> > +
> >    try
> >      {
> > -      pcs = find_pcs_for_symtab_line (symtab, py_line, &best_entry);
> 
> Did you consider updating find_pcs_for_symtab_line to return
> 'std::vector<linetable_entry *>' ?
> 
> find_pcs_for_symtab_line already returns a linetable_entry* in the
> best_entry argument, so there's clearly no problem with the callers
> accessing that type.
> 
> There are only two users of find_pcs_for_symtab_line (one of which is
> this one), so updating the function wouldn't be a huge amount of work.
> 
> And this function uses a binary search to find the line table entry
> we're looking for, which will be better than the linear search you are
> using.

I did not actually, but it makes perfect sense. Below patch does that,
I'll include it in the next version. 

Thanks!

Jan

-- >8 --
From 0cf3ea738b0f9358660cd8d292b99033a9780e54 Mon Sep 17 00:00:00 2001
From: Jan Vrany <jan.vrany@labware.com>
Date: Mon, 1 Sep 2025 18:18:38 +0100
Subject: [PATCH 1/5] gdb: change find_pcs_for_symtab_line() to return entries
 instead of PCs

This commit changes find_pcs_for_symtab_line() to return complete
linetable entries instead of just PCs.  This is a preparation for adding
more attributes to gdb.LinetableEntry objects.

I also renamed the function to find_linetable_entries_for_symtab_line()
to better reflect what it does.
---
 gdb/linespec.c            | 11 ++++++-----
 gdb/python/py-linetable.c | 23 ++++++++++++++---------
 gdb/symtab.c              | 11 +++++------
 gdb/symtab.h              |  2 +-
 4 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/gdb/linespec.c b/gdb/linespec.c
index cefee026d92..9eca52a8e62 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -3984,23 +3984,24 @@ decode_digits_ordinary (struct linespec_state *self,
   std::vector<symtab_and_line> sals;
   for (const auto &elt : ls->file_symtabs)
     {
-      std::vector<CORE_ADDR> pcs;
+      std::vector<const linetable_entry *> pcs;
 
       /* The logic above should ensure this.  */
       gdb_assert (elt != NULL);
 
-      program_space *pspace = elt->compunit ()->objfile ()->pspace ();
+      objfile *objfile = elt->compunit ()->objfile ();
+      program_space *pspace = objfile->pspace ();
       set_current_program_space (pspace);
 
-      pcs = find_pcs_for_symtab_line (elt, line, best_entry);
-      for (CORE_ADDR pc : pcs)
+      pcs = find_linetable_entries_for_symtab_line (elt, line, best_entry);
+      for (auto linetable_entry : pcs)
 	{
 	  symtab_and_line sal;
 	  sal.pspace = pspace;
 	  sal.symtab = elt;
 	  sal.line = line;
 	  sal.explicit_line = true;
-	  sal.pc = pc;
+	  sal.pc = linetable_entry->pc (objfile);
 	  sals.push_back (std::move (sal));
 	}
     }
diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
index 90cba09c88e..882ae5fe8a8 100644
--- a/gdb/python/py-linetable.c
+++ b/gdb/python/py-linetable.c
@@ -120,22 +120,25 @@ build_linetable_entry (int line, CORE_ADDR address)
    address.  */
 
 static PyObject *
-build_line_table_tuple_from_pcs (int line, const std::vector<CORE_ADDR> &pcs)
+build_line_table_tuple_from_entries (
+	const struct objfile *objfile,
+	const std::vector<const linetable_entry *> &entries)
 {
   int i;
 
-  if (pcs.size () < 1)
+  if (entries.size () < 1)
     Py_RETURN_NONE;
 
-  gdbpy_ref<> tuple (PyTuple_New (pcs.size ()));
+  gdbpy_ref<> tuple (PyTuple_New (entries.size ()));
 
   if (tuple == NULL)
     return NULL;
 
-  for (i = 0; i < pcs.size (); ++i)
+  for (i = 0; i < entries.size (); ++i)
     {
-      CORE_ADDR pc = pcs[i];
-      gdbpy_ref<> obj (build_linetable_entry (line, pc));
+      auto entry = entries[i];
+      gdbpy_ref<> obj (build_linetable_entry
+			(entry->line, entry->pc (objfile)));
 
       if (obj == NULL)
 	return NULL;
@@ -156,7 +159,7 @@ ltpy_get_pcs_for_line (PyObject *self, PyObject *args)
   struct symtab *symtab;
   gdb_py_longest py_line;
   const linetable_entry *best_entry = nullptr;
-  std::vector<CORE_ADDR> pcs;
+  std::vector<const linetable_entry*> entries;
 
   LTPY_REQUIRE_VALID (self, symtab);
 
@@ -165,14 +168,16 @@ ltpy_get_pcs_for_line (PyObject *self, PyObject *args)
 
   try
     {
-      pcs = find_pcs_for_symtab_line (symtab, py_line, &best_entry);
+      entries = find_linetable_entries_for_symtab_line (symtab, py_line,
+							&best_entry);
     }
   catch (const gdb_exception &except)
     {
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return build_line_table_tuple_from_pcs (py_line, pcs);
+  struct objfile *objfile = symtab->compunit ()->objfile ();
+  return build_line_table_tuple_from_entries (objfile, entries);
 }
 
 /* Implementation of gdb.LineTable.has_line (self, line) -> Boolean.
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 33475fbbb9e..5c8eb357689 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3604,13 +3604,12 @@ find_line_symtab (symtab *sym_tab, int line, int *index)
    exactly match LINE.  Returns an empty vector if there are no exact
    matches, but updates BEST_ITEM in this case.  */
 
-std::vector<CORE_ADDR>
-find_pcs_for_symtab_line (struct symtab *symtab, int line,
-			  const linetable_entry **best_item)
+std::vector<const linetable_entry *>
+find_linetable_entries_for_symtab_line (struct symtab *symtab, int line,
+					const linetable_entry **best_item)
 {
   int start = 0;
-  std::vector<CORE_ADDR> result;
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  std::vector<const linetable_entry *> result;
 
   /* First, collect all the PCs that are at this line.  */
   while (1)
@@ -3634,7 +3633,7 @@ find_pcs_for_symtab_line (struct symtab *symtab, int line,
 	  break;
 	}
 
-      result.push_back (symtab->linetable ()->item[idx].pc (objfile));
+      result.push_back (&symtab->linetable ()->item[idx]);
       start = idx + 1;
     }
 
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 7ae7451dcfa..91fe763ce43 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -2831,7 +2831,7 @@ bool iterate_over_some_symtabs (const char *name,
 void iterate_over_symtabs (program_space *pspace, const char *name,
 			   gdb::function_view<bool (symtab *)> callback);
 
-std::vector<CORE_ADDR> find_pcs_for_symtab_line
+std::vector<const linetable_entry *> find_linetable_entries_for_symtab_line
     (struct symtab *symtab, int line, const linetable_entry **best_entry);
 
 /* Prototype for callbacks for LA_ITERATE_OVER_SYMBOLS.  The callback
-- 
2.50.1



  reply	other threads:[~2025-09-02 11:08 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23 16:09 [RFC v5 00/19] Add Python "JIT" API Jan Vrany
2025-06-23 16:09 ` [RFC v5 01/18] gdb: introduce expand_symtabs_maybe_overlapping Jan Vrany
2025-06-24 15:22   ` Tom Tromey
2025-06-26 15:05     ` Jan Vraný
2025-06-23 16:09 ` [RFC v5 02/18] gdb: introduce compunit_symtab::maybe_contains Jan Vrany
2025-06-23 16:09 ` [RFC v5 03/18] gdb: update is_addr_in_objfile to support "dynamic" objfiles Jan Vrany
2025-06-23 16:09 ` [RFC v5 04/18] gdb: introduce new function create_function_type Jan Vrany
2025-06-24 15:29   ` Tom Tromey
2025-06-26 11:12     ` Jan Vraný
2025-06-27 14:21       ` Tom Tromey
2025-06-27 14:30         ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 05/18] gdb/python: add function () method to gdb.Type object Jan Vrany
2025-06-24 16:11   ` Tom Tromey
2025-06-26 11:13     ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 06/18] gdb: use std::vector<> to hold on blocks in struct blockvector Jan Vrany
2025-06-23 16:10 ` [RFC v5 07/18] gdb/python: add gdb.Compunit Jan Vrany
2025-06-23 16:10 ` [RFC v5 08/18] gdb/python: allow instantiation of gdb.Objfile from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 09/18] gdb/python: add unlink () method to gdb.Objfile object Jan Vrany
2025-06-23 16:10 ` [RFC v5 10/18] gdb/python: allow instantiation of gdb.Compunit from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 11/18] gdb/python: allow instantiation of gdb.Symtab " Jan Vrany
2025-06-23 16:10 ` [RFC v5 12/18] gdb/python: allow instantiation of gdb.Block " Jan Vrany
2025-06-23 16:10 ` [RFC v5 13/18] gdb/python: allow instantiation of gdb.Symbol " Jan Vrany
2025-06-23 16:10 ` [RFC v5 14/18] gdb/python: add add_symbol () method to gdb.Block Jan Vrany
2025-08-29 14:10   ` Andrew Burgess
2025-08-29 14:14     ` Andrew Burgess
2025-06-23 16:10 ` [RFC v5 15/18] gdb/python: add more attributes to gdb.LinetableEntry objects Jan Vrany
2025-08-29 14:00   ` Andrew Burgess
2025-09-02 11:03     ` Jan Vraný [this message]
2025-06-23 16:10 ` [RFC v5 16/18] gdb/python: allow instantiation of gdb.LineTableEntry objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 17/18] gdb/python: allow instantiation of gdb.LineTable objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 18/18] gdb/python: add section in documentation on implementing JIT interface Jan Vrany

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=b3111d5d248bb66d138fc654a1feb8232b1d21cf.camel@labware.com \
    --to=jan.vrany@labware.com \
    --cc=aburgess@redhat.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