[PATCH 1/2] gdb/python: add gdb.Breakpoint.source_locations property

Andrew Burgess aburgess@redhat.com
Wed Mar 30 08:58:08 GMT 2022


Simon Farre via Gdb-patches <gdb-patches@sourceware.org> writes:

> 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`.

See my comments on patch 2/2.  I think the same applies for this patch
too.

Thanks,
Andrew

> ---
>  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



More information about the Gdb-patches mailing list