public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Farre <simon.farre.cx@gmail.com>
To: gdb-patches@sourceware.org
Cc: Simon Farre <simon.farre.cx@gmail.com>
Subject: [PATCH 1/2] gdb/python: add gdb.Breakpoint.source_locations property
Date: Tue, 29 Mar 2022 23:27:16 +0200	[thread overview]
Message-ID: <20220329212717.1184271-1-simon.farre.cx@gmail.com> (raw)

This attribute returns the source locations where
this breakpoint was installed. It's returned as a list
of tuples of type (file path: string, line: long).

Currently it's not possible from the Python API to know
where a breakpoint has been installed.

The rationale for this patch is for IDE or debug adapters
as they are called, for instance for VSCode, to be able
to fetch Breakpoint related data directly from Python,
but also makes it possible to get this data for plugins
without having the parse the string output of the command
`info breakpoints`.
---
 gdb/NEWS                   |  5 +++++
 gdb/doc/python.texi        |  8 ++++++++
 gdb/python/py-breakpoint.c | 31 +++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index e10062752d0..9e7e41d3bd6 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -16,6 +16,11 @@
      This is the same format that GDB uses when printing address, symbol,
      and offset information from the disassembler.
 
+  ** Added attribute Breakpoint.source_locations
+     This returns a list of (file path, line number) tuples where the
+     Breakpoint was installed. This is the information displayed
+     by the 'info breakpoints' command.
+
 *** Changes in GDB 12
 
 * DBX mode is deprecated, and will be removed in GDB 13
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 7c414b01d70..0d842af6229 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6066,6 +6066,14 @@ commands, separated by newlines.  If there are no commands, this
 attribute is @code{None}.  This attribute is writable.
 @end defvar
 
+@defvar Breakpoint.source_locations
+This attribute holds the source locations where the breakpoint installed.
+It returns a list of tuples containing the file path and the line number.
+If the breakpoint has no source location(s), either because it's a pending
+breakpoint or is of a type that doesn't have a source location related to it
+it returns @code{None}. This attribute is not writable.
+@end defvar
+
 @node Finish Breakpoints in Python
 @subsubsection Finish Breakpoints
 
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 5891c3a9735..1b9ac6957a5 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -692,6 +692,35 @@ bppy_get_ignore_count (PyObject *self, void *closure)
   return gdb_py_object_from_longest (self_bp->bp->ignore_count).release ();
 }
 
+/* Python function to get all source locations where this breakpoint is set.
+   returns a list of tuples containing the file path and the line number.  */
+
+static PyObject* bppy_get_source_locations(PyObject *self, void* closure)
+{
+  gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
+  struct breakpoint* bp = self_bp->bp;
+
+  BPPY_REQUIRE_VALID (self_bp);
+
+  if (!is_breakpoint (bp) || pending_breakpoint_p(bp))
+    Py_RETURN_NONE;
+
+  gdbpy_ref<> list (PyList_New (0));
+
+  for (const bp_location* it = bp->loc; it != nullptr; it = it->next)
+    {
+      PyObject* tuple = PyTuple_New(2);
+      if(PyTuple_SetItem(tuple, 0, host_string_to_python_string (it->symtab->filename).release ()) != 0)
+        return nullptr;
+      if(PyTuple_SetItem(tuple, 1, PyLong_FromLong(it->line_number)) != 0)
+        return nullptr;
+      if(PyList_Append(list.get(), tuple) != 0)
+        return nullptr;
+    }
+
+  return list.release();
+}
+
 /* Internal function to validate the Python parameters/keywords
    provided to bppy_init.  */
 
@@ -1264,6 +1293,8 @@ or None if no condition set."},
     "Whether this breakpoint is a temporary breakpoint."},
   { "pending", bppy_get_pending, NULL,
     "Whether this breakpoint is a pending breakpoint."},
+  { "source_locations", bppy_get_source_locations, NULL,
+    "Source file locations where this breakpoint is installed"},
   { NULL }  /* Sentinel.  */
 };
 

base-commit: b8e92c571baed4e794bd62b7bf417fa8bbaf5c95
-- 
2.32.0


             reply	other threads:[~2022-03-29 21:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-29 21:27 Simon Farre [this message]
2022-03-29 21:27 ` [PATCH 2/2] gdb/python: add gdb.Breakpoint.addresses property Simon Farre
2022-03-30  8:56   ` Andrew Burgess
2022-03-30 10:04     ` Simon Farre
2022-03-30  8:58 ` [PATCH 1/2] gdb/python: add gdb.Breakpoint.source_locations property Andrew Burgess

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=20220329212717.1184271-1-simon.farre.cx@gmail.com \
    --to=simon.farre.cx@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).