* [PATCH] gdb/python: add property ranges to gdb.Block object
@ 2026-03-11 20:18 Jan Vrany
2026-03-12 6:52 ` Eli Zaretskii
2026-03-12 12:35 ` Tom Tromey
0 siblings, 2 replies; 9+ messages in thread
From: Jan Vrany @ 2026-03-11 20:18 UTC (permalink / raw)
To: gdb-patches; +Cc: Jan Vrany
This commit adds a new property - ranges - to gdb.Block object. It holds
a tuple of ranges for that block. Each range is a tuple of (start, end)
address. For contiguous blocks it contains only one range.
---
gdb/NEWS | 4 ++++
gdb/doc/python.texi | 7 +++++++
gdb/python/py-block.c | 30 +++++++++++++++++++++++++++
gdb/testsuite/gdb.python/py-block.exp | 3 +++
4 files changed, 44 insertions(+)
diff --git a/gdb/NEWS b/gdb/NEWS
index e46a5108272..605e55f1115 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -229,6 +229,10 @@ qExecAndArgs
the appropriate user setting is enabled, and GDB knows how to
style this source file.
+ ** New gdb.Block.ranges attribute. This read only attribute contains
+ a tuple of pairs each representing a single range. Contiguous blocks
+ have only one range.
+
* Guile API
** Procedures 'memory-port-read-buffer-size',
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 2df3b7c0423..69a3b298ede 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6342,6 +6342,13 @@ One past the last address that appears in the block. This attribute
is not writable.
@end defvar
+@defvar Block.ranges
+A tuple representing address ranges of the block. Each range is represented
+as pair (two-element tuple) where first element is the start of the range
+and second element is one past the last address that appears in the range.
+The order of ranges is unspecified. Contiguous blocks have only one range.
+This attribute is not writable.
+
@defvar Block.function
The name of the block represented as a @code{gdb.Symbol}. If the
block is not named, then this attribute holds @code{None}. This
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 4d77242ca0d..3f293a2d091 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -117,6 +117,34 @@ blpy_get_end (PyObject *self, void *closure)
return gdb_py_object_from_ulongest (block->end ()).release ();
}
+/* Implementation of gdb.Block.ranges. */
+
+static PyObject *
+blpy_get_ranges (PyObject *self, void *closure)
+{
+ const struct block *block = NULL;
+
+ BLPY_REQUIRE_VALID (self, block);
+
+ auto ranges = block->ranges ();
+
+ if (ranges.size() == 0)
+ return Py_BuildValue ("((KK))", block->start (), block->end ());
+ else
+ {
+ gdbpy_ref<> ranges_obj (PyTuple_New (ranges.size ()));
+
+ for (int i = 0; i < ranges.size (); i++)
+ {
+ gdbpy_ref<> range_obj (Py_BuildValue ("(KK)", ranges[i].start (),
+ ranges[i].end ()));
+ PyTuple_SetItem (ranges_obj.get (), i, range_obj.release ());
+ }
+
+ return ranges_obj.release ();
+ }
+}
+
static PyObject *
blpy_get_function (PyObject *self, void *closure)
{
@@ -564,6 +592,8 @@ static gdb_PyGetSetDef block_object_getset[] = {
"Whether this block is a global block.", NULL },
{ "subblocks", blpy_get_subblocks, nullptr,
"List of blocks contained in this block.", nullptr },
+ { "ranges", blpy_get_ranges, nullptr,
+ "List of address ranges for this block.", nullptr },
{ NULL } /* Sentinel */
};
diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.python/py-block.exp
index b483d4b8a92..e4e309da05f 100644
--- a/gdb/testsuite/gdb.python/py-block.exp
+++ b/gdb/testsuite/gdb.python/py-block.exp
@@ -43,6 +43,9 @@ gdb_test "python print (block)" "<gdb.Block <anonymous> \{i, f, b\}>" \
gdb_test "python print (block.function)" "None" "first anonymous block"
gdb_test "python print (block.start)" "${decimal}" "check start not None"
gdb_test "python print (block.end)" "${decimal}" "check end not None"
+gdb_test "python print (block.ranges)" \
+ "\\(\\(${decimal}, ${decimal}\\)(,|(, (\\(${decimal}, ${decimal}\\))+))\\)" \
+ "check ranges contains tuple of tuples"
gdb_test "python print (block\['f'\].name == 'f')" "True" "check variable access"
gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexistent'.*" \
"check nonexistent variable"
--
2.51.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] gdb/python: add property ranges to gdb.Block object
2026-03-11 20:18 [PATCH] gdb/python: add property ranges to gdb.Block object Jan Vrany
@ 2026-03-12 6:52 ` Eli Zaretskii
2026-03-12 11:04 ` Jan Vraný
2026-03-12 12:35 ` Tom Tromey
1 sibling, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2026-03-12 6:52 UTC (permalink / raw)
To: Jan Vrany; +Cc: gdb-patches
> From: Jan Vrany <jan.vrany@labware.com>
> CC: Jan Vrany <jan.vrany@labware.com>
> Date: Wed, 11 Mar 2026 20:18:26 +0000
>
> This commit adds a new property - ranges - to gdb.Block object. It holds
> a tuple of ranges for that block. Each range is a tuple of (start, end)
> address. For contiguous blocks it contains only one range.
> ---
> gdb/NEWS | 4 ++++
> gdb/doc/python.texi | 7 +++++++
> gdb/python/py-block.c | 30 +++++++++++++++++++++++++++
> gdb/testsuite/gdb.python/py-block.exp | 3 +++
> 4 files changed, 44 insertions(+)
Thanks.
> + ** New gdb.Block.ranges attribute. This read only attribute contains
> + a tuple of pairs each representing a single range. Contiguous blocks
> + have only one range. ^^
Two spaces there, please.
> +@defvar Block.ranges
> +A tuple representing address ranges of the block. Each range is represented
> +as pair (two-element tuple) where first element is the start of the range
^^^^^^^ ^^^^^^^^^^^^^
"as a pair" and "the first element"
> +and second element is one past the last address that appears in the range.
^^^^^^^^^^^^^^
"the second element"
> +The order of ranges is unspecified. Contiguous blocks have only one range.
> +This attribute is not writable. ^^
Two spaces there.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] gdb/python: add property ranges to gdb.Block object
2026-03-12 6:52 ` Eli Zaretskii
@ 2026-03-12 11:04 ` Jan Vraný
2026-03-12 11:35 ` Eli Zaretskii
0 siblings, 1 reply; 9+ messages in thread
From: Jan Vraný @ 2026-03-12 11:04 UTC (permalink / raw)
To: eliz; +Cc: gdb-patches
On Thu, 2026-03-12 at 08:52 +0200, Eli Zaretskii wrote:
>
> > + ** New gdb.Block.ranges attribute. This read only attribute contains
> > + a tuple of pairs each representing a single range. Contiguous blocks
> > + have only one range. ^^
>
> Two spaces there, please.
>
> > +@defvar Block.ranges
> > +A tuple representing address ranges of the block. Each range is represented
> > +as pair (two-element tuple) where first element is the start of the range
> ^^^^^^^ ^^^^^^^^^^^^^
> "as a pair" and "the first element"
>
> > +and second element is one past the last address that appears in the range.
> ^^^^^^^^^^^^^^
> "the second element"
>
> > +The order of ranges is unspecified. Contiguous blocks have only one range.
> > +This attribute is not writable. ^^
>
> Two spaces there.
>
> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Thanks! Fixed below.
Jan
-- 8< --
This commit adds a new property - ranges - to gdb.Block object. It holds
a tuple of ranges for that block. Each range is a tuple of (start, end)
address. For contiguous blocks it contains only one range.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
gdb/NEWS | 4 ++++
gdb/doc/python.texi | 8 +++++++
gdb/python/py-block.c | 30 +++++++++++++++++++++++++++
gdb/testsuite/gdb.python/py-block.exp | 3 +++
4 files changed, 45 insertions(+)
diff --git a/gdb/NEWS b/gdb/NEWS
index e46a5108272..e48eecbeb7c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -229,6 +229,10 @@ qExecAndArgs
the appropriate user setting is enabled, and GDB knows how to
style this source file.
+ ** New gdb.Block.ranges attribute. This read only attribute contains
+ a tuple of pairs each representing a single range. Contiguous blocks
+ have only one range.
+
* Guile API
** Procedures 'memory-port-read-buffer-size',
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 2df3b7c0423..05e00d8370f 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6342,6 +6342,14 @@ One past the last address that appears in the block. This attribute
is not writable.
@end defvar
+@defvar Block.ranges
+A tuple representing address ranges of the block. Each range is represented
+as a pair (two-element tuple) where the first element is the start of the
+range and the second element is one past the last address that appears in
+the range. The order of ranges is unspecified. Contiguous blocks have only
+one range. This attribute is not writable.
+@end defvar
+
@defvar Block.function
The name of the block represented as a @code{gdb.Symbol}. If the
block is not named, then this attribute holds @code{None}. This
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 4d77242ca0d..3f293a2d091 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -117,6 +117,34 @@ blpy_get_end (PyObject *self, void *closure)
return gdb_py_object_from_ulongest (block->end ()).release ();
}
+/* Implementation of gdb.Block.ranges. */
+
+static PyObject *
+blpy_get_ranges (PyObject *self, void *closure)
+{
+ const struct block *block = NULL;
+
+ BLPY_REQUIRE_VALID (self, block);
+
+ auto ranges = block->ranges ();
+
+ if (ranges.size() == 0)
+ return Py_BuildValue ("((KK))", block->start (), block->end ());
+ else
+ {
+ gdbpy_ref<> ranges_obj (PyTuple_New (ranges.size ()));
+
+ for (int i = 0; i < ranges.size (); i++)
+ {
+ gdbpy_ref<> range_obj (Py_BuildValue ("(KK)", ranges[i].start (),
+ ranges[i].end ()));
+ PyTuple_SetItem (ranges_obj.get (), i, range_obj.release ());
+ }
+
+ return ranges_obj.release ();
+ }
+}
+
static PyObject *
blpy_get_function (PyObject *self, void *closure)
{
@@ -564,6 +592,8 @@ static gdb_PyGetSetDef block_object_getset[] = {
"Whether this block is a global block.", NULL },
{ "subblocks", blpy_get_subblocks, nullptr,
"List of blocks contained in this block.", nullptr },
+ { "ranges", blpy_get_ranges, nullptr,
+ "List of address ranges for this block.", nullptr },
{ NULL } /* Sentinel */
};
diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.python/py-block.exp
index b483d4b8a92..e4e309da05f 100644
--- a/gdb/testsuite/gdb.python/py-block.exp
+++ b/gdb/testsuite/gdb.python/py-block.exp
@@ -43,6 +43,9 @@ gdb_test "python print (block)" "<gdb.Block <anonymous> \{i, f, b\}>" \
gdb_test "python print (block.function)" "None" "first anonymous block"
gdb_test "python print (block.start)" "${decimal}" "check start not None"
gdb_test "python print (block.end)" "${decimal}" "check end not None"
+gdb_test "python print (block.ranges)" \
+ "\\(\\(${decimal}, ${decimal}\\)(,|(, (\\(${decimal}, ${decimal}\\))+))\\)" \
+ "check ranges contains tuple of tuples"
gdb_test "python print (block\['f'\].name == 'f')" "True" "check variable access"
gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexistent'.*" \
"check nonexistent variable"
--
2.51.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] gdb/python: add property ranges to gdb.Block object
2026-03-12 11:04 ` Jan Vraný
@ 2026-03-12 11:35 ` Eli Zaretskii
0 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2026-03-12 11:35 UTC (permalink / raw)
To: Jan Vraný; +Cc: gdb-patches
> From: Jan Vraný <Jan.Vrany@labware.com>
> CC: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
> Date: Thu, 12 Mar 2026 11:04:21 +0000
>
> On Thu, 2026-03-12 at 08:52 +0200, Eli Zaretskii wrote:
> >
> > > + ** New gdb.Block.ranges attribute. This read only attribute contains
> > > + a tuple of pairs each representing a single range. Contiguous blocks
> > > + have only one range. ^^
> >
> > Two spaces there, please.
> >
> > > +@defvar Block.ranges
> > > +A tuple representing address ranges of the block. Each range is represented
> > > +as pair (two-element tuple) where first element is the start of the range
> > ^^^^^^^ ^^^^^^^^^^^^^
> > "as a pair" and "the first element"
> >
> > > +and second element is one past the last address that appears in the range.
> > ^^^^^^^^^^^^^^
> > "the second element"
> >
> > > +The order of ranges is unspecified. Contiguous blocks have only one range.
> > > +This attribute is not writable. ^^
> >
> > Two spaces there.
> >
> > Reviewed-By: Eli Zaretskii <eliz@gnu.org>
>
> Thanks! Fixed below.
Thanks, LGTM.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] gdb/python: add property ranges to gdb.Block object
2026-03-11 20:18 [PATCH] gdb/python: add property ranges to gdb.Block object Jan Vrany
2026-03-12 6:52 ` Eli Zaretskii
@ 2026-03-12 12:35 ` Tom Tromey
2026-03-12 14:33 ` [PATCH v2] " Jan Vrany
1 sibling, 1 reply; 9+ messages in thread
From: Tom Tromey @ 2026-03-12 12:35 UTC (permalink / raw)
To: Jan Vrany; +Cc: gdb-patches
Jan> + const struct block *block = NULL;
In new code we're writing 'nullptr'
Jan> + if (ranges.size() == 0)
Space before "(".
Jan> + gdbpy_ref<> ranges_obj (PyTuple_New (ranges.size ()));
Error check.
Jan> + gdbpy_ref<> range_obj (Py_BuildValue ("(KK)", ranges[i].start (),
Jan> + ranges[i].end ()));
Another error check.
Someday maybe we'll land the "safety" approach and this stuff will be easier.
thanks,
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2] gdb/python: add property ranges to gdb.Block object
2026-03-12 12:35 ` Tom Tromey
@ 2026-03-12 14:33 ` Jan Vrany
2026-03-12 15:15 ` Tom Tromey
0 siblings, 1 reply; 9+ messages in thread
From: Jan Vrany @ 2026-03-12 14:33 UTC (permalink / raw)
To: gdb-patches, tom; +Cc: Jan Vrany, Eli Zaretskii
On Thu, 2026-03-12 at 06:35 -0600, Tom Tromey wrote:
> Jan> + gdbpy_ref<> range_obj (Py_BuildValue ("(KK)", ranges[i].start (),
> Jan> + ranges[i].end ()));
>
> Another error check.
>
> Someday maybe we'll land the "safety" approach and this stuff will be easier.
Thanks, fixed below.
Jan
-- 8< --
This commit adds a new property - ranges - to gdb.Block object. It holds
a tuple of ranges for that block. Each range is a tuple of (start, end)
address. For contiguous blocks it contains only one range.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
gdb/NEWS | 4 +++
gdb/doc/python.texi | 8 ++++++
gdb/python/py-block.c | 35 +++++++++++++++++++++++++++
gdb/testsuite/gdb.python/py-block.exp | 3 +++
4 files changed, 50 insertions(+)
diff --git a/gdb/NEWS b/gdb/NEWS
index e46a5108272..e48eecbeb7c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -229,6 +229,10 @@ qExecAndArgs
the appropriate user setting is enabled, and GDB knows how to
style this source file.
+ ** New gdb.Block.ranges attribute. This read only attribute contains
+ a tuple of pairs each representing a single range. Contiguous blocks
+ have only one range.
+
* Guile API
** Procedures 'memory-port-read-buffer-size',
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 2df3b7c0423..05e00d8370f 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6342,6 +6342,14 @@ One past the last address that appears in the block. This attribute
is not writable.
@end defvar
+@defvar Block.ranges
+A tuple representing address ranges of the block. Each range is represented
+as a pair (two-element tuple) where the first element is the start of the
+range and the second element is one past the last address that appears in
+the range. The order of ranges is unspecified. Contiguous blocks have only
+one range. This attribute is not writable.
+@end defvar
+
@defvar Block.function
The name of the block represented as a @code{gdb.Symbol}. If the
block is not named, then this attribute holds @code{None}. This
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 4d77242ca0d..6d3cb1d0bd5 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -117,6 +117,39 @@ blpy_get_end (PyObject *self, void *closure)
return gdb_py_object_from_ulongest (block->end ()).release ();
}
+/* Implementation of gdb.Block.ranges. */
+
+static PyObject *
+blpy_get_ranges (PyObject *self, void *closure)
+{
+ const struct block *block = nullptr;
+
+ BLPY_REQUIRE_VALID (self, block);
+
+ auto ranges = block->ranges ();
+
+ if (ranges.size () == 0)
+ return Py_BuildValue ("((KK))", block->start (), block->end ());
+ else
+ {
+ gdbpy_ref<> ranges_obj (PyTuple_New (ranges.size ()));
+ if (ranges_obj == nullptr)
+ return nullptr;
+
+ for (int i = 0; i < ranges.size (); i++)
+ {
+ gdbpy_ref<> range_obj (Py_BuildValue ("(KK)", ranges[i].start (),
+ ranges[i].end ()));
+ if (range_obj == nullptr)
+ return nullptr;
+
+ PyTuple_SetItem (ranges_obj.get (), i, range_obj.release ());
+ }
+
+ return ranges_obj.release ();
+ }
+}
+
static PyObject *
blpy_get_function (PyObject *self, void *closure)
{
@@ -564,6 +597,8 @@ static gdb_PyGetSetDef block_object_getset[] = {
"Whether this block is a global block.", NULL },
{ "subblocks", blpy_get_subblocks, nullptr,
"List of blocks contained in this block.", nullptr },
+ { "ranges", blpy_get_ranges, nullptr,
+ "List of address ranges for this block.", nullptr },
{ NULL } /* Sentinel */
};
diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.python/py-block.exp
index b483d4b8a92..e4e309da05f 100644
--- a/gdb/testsuite/gdb.python/py-block.exp
+++ b/gdb/testsuite/gdb.python/py-block.exp
@@ -43,6 +43,9 @@ gdb_test "python print (block)" "<gdb.Block <anonymous> \{i, f, b\}>" \
gdb_test "python print (block.function)" "None" "first anonymous block"
gdb_test "python print (block.start)" "${decimal}" "check start not None"
gdb_test "python print (block.end)" "${decimal}" "check end not None"
+gdb_test "python print (block.ranges)" \
+ "\\(\\(${decimal}, ${decimal}\\)(,|(, (\\(${decimal}, ${decimal}\\))+))\\)" \
+ "check ranges contains tuple of tuples"
gdb_test "python print (block\['f'\].name == 'f')" "True" "check variable access"
gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexistent'.*" \
"check nonexistent variable"
--
2.51.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v2] gdb/python: add property ranges to gdb.Block object
2026-03-12 14:33 ` [PATCH v2] " Jan Vrany
@ 2026-03-12 15:15 ` Tom Tromey
2026-03-13 14:40 ` [PATCH v3] " Jan Vrany
0 siblings, 1 reply; 9+ messages in thread
From: Tom Tromey @ 2026-03-12 15:15 UTC (permalink / raw)
To: Jan Vrany; +Cc: gdb-patches, tom, Eli Zaretskii
>>>>> "Jan" == Jan Vrany <jan.vrany@labware.com> writes:
Jan> + return Py_BuildValue ("((KK))", block->start (), block->end ());
Jan> + gdbpy_ref<> range_obj (Py_BuildValue ("(KK)", ranges[i].start (),
Jan> + ranges[i].end ()));
I'm sorry I didn't think of this in the last review, but when using
varargs it is up to the caller to ensure that the types are correct.
Also, we apparently still support versions of Python that don't
guarantee "long long" support. There's some text in python-internal.h
about this, look for "HAVE_LONG_LONG".
So I think this code needs to use GDB_PY_LLU_ARG and have explicit casts
to gdb_py_ulongest.
Or maybe be written in some other way.
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v3] gdb/python: add property ranges to gdb.Block object
2026-03-12 15:15 ` Tom Tromey
@ 2026-03-13 14:40 ` Jan Vrany
2026-03-19 15:12 ` Tom Tromey
0 siblings, 1 reply; 9+ messages in thread
From: Jan Vrany @ 2026-03-13 14:40 UTC (permalink / raw)
To: gdb-patches; +Cc: Jan Vrany, Eli Zaretskii
> On Thu, 2026-03-12 at 09:15 -0600, Tom Tromey wrote:
>
> Also, we apparently still support versions of Python that don't
> guarantee "long long" support. There's some text in python-internal.h
> about this, look for "HAVE_LONG_LONG".
>
> So I think this code needs to use GDB_PY_LLU_ARG and have explicit casts
> to gdb_py_ulongest.
I see. The v3 below does that.
>
> Or maybe be written in some other way.
>
I tried to use "(OO)", gdb_py_object_from_ulongest(start), gdb_py_object_from_ulongest(end)
but that did not look significantly better to me.
I do not like the need to test for block::ranges to be empty. I experimented
with making block::ranges to always return at least one range, it turns out
it simplifies the code on more places (except block::relocate).
I plan to send this in a separate series soon(ish).
Thanks,
Jan
-- 8< --
This commit adds a new property - ranges - to gdb.Block object. It holds
a tuple of ranges for that block. Each range is a tuple of (start, end)
address. For contiguous blocks it contains only one range.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
gdb/NEWS | 4 +++
gdb/doc/python.texi | 8 ++++++
gdb/python/py-block.c | 39 +++++++++++++++++++++++++++
gdb/testsuite/gdb.python/py-block.exp | 3 +++
4 files changed, 54 insertions(+)
diff --git a/gdb/NEWS b/gdb/NEWS
index e46a5108272..e48eecbeb7c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -229,6 +229,10 @@ qExecAndArgs
the appropriate user setting is enabled, and GDB knows how to
style this source file.
+ ** New gdb.Block.ranges attribute. This read only attribute contains
+ a tuple of pairs each representing a single range. Contiguous blocks
+ have only one range.
+
* Guile API
** Procedures 'memory-port-read-buffer-size',
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 2df3b7c0423..05e00d8370f 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6342,6 +6342,14 @@ One past the last address that appears in the block. This attribute
is not writable.
@end defvar
+@defvar Block.ranges
+A tuple representing address ranges of the block. Each range is represented
+as a pair (two-element tuple) where the first element is the start of the
+range and the second element is one past the last address that appears in
+the range. The order of ranges is unspecified. Contiguous blocks have only
+one range. This attribute is not writable.
+@end defvar
+
@defvar Block.function
The name of the block represented as a @code{gdb.Symbol}. If the
block is not named, then this attribute holds @code{None}. This
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 4d77242ca0d..6e4b662cc51 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -117,6 +117,43 @@ blpy_get_end (PyObject *self, void *closure)
return gdb_py_object_from_ulongest (block->end ()).release ();
}
+/* Implementation of gdb.Block.ranges. */
+
+static PyObject *
+blpy_get_ranges (PyObject *self, void *closure)
+{
+ const struct block *block = nullptr;
+
+ BLPY_REQUIRE_VALID (self, block);
+
+ auto ranges = block->ranges ();
+
+ if (ranges.size () == 0)
+ return Py_BuildValue ("((" GDB_PY_LLU_ARG GDB_PY_LLU_ARG "))",
+ (gdb_py_ulongest) block->start (),
+ (gdb_py_ulongest) block->end ());
+ else
+ {
+ gdbpy_ref<> ranges_obj (PyTuple_New (ranges.size ()));
+ if (ranges_obj == nullptr)
+ return nullptr;
+
+ for (int i = 0; i < ranges.size (); i++)
+ {
+ gdbpy_ref<> range_obj
+ (Py_BuildValue ("(" GDB_PY_LLU_ARG GDB_PY_LLU_ARG ")",
+ (gdb_py_ulongest) ranges[i].start (),
+ (gdb_py_ulongest) ranges[i].end ()));
+ if (range_obj == nullptr)
+ return nullptr;
+
+ PyTuple_SetItem (ranges_obj.get (), i, range_obj.release ());
+ }
+
+ return ranges_obj.release ();
+ }
+}
+
static PyObject *
blpy_get_function (PyObject *self, void *closure)
{
@@ -564,6 +601,8 @@ static gdb_PyGetSetDef block_object_getset[] = {
"Whether this block is a global block.", NULL },
{ "subblocks", blpy_get_subblocks, nullptr,
"List of blocks contained in this block.", nullptr },
+ { "ranges", blpy_get_ranges, nullptr,
+ "List of address ranges for this block.", nullptr },
{ NULL } /* Sentinel */
};
diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.python/py-block.exp
index b483d4b8a92..e4e309da05f 100644
--- a/gdb/testsuite/gdb.python/py-block.exp
+++ b/gdb/testsuite/gdb.python/py-block.exp
@@ -43,6 +43,9 @@ gdb_test "python print (block)" "<gdb.Block <anonymous> \{i, f, b\}>" \
gdb_test "python print (block.function)" "None" "first anonymous block"
gdb_test "python print (block.start)" "${decimal}" "check start not None"
gdb_test "python print (block.end)" "${decimal}" "check end not None"
+gdb_test "python print (block.ranges)" \
+ "\\(\\(${decimal}, ${decimal}\\)(,|(, (\\(${decimal}, ${decimal}\\))+))\\)" \
+ "check ranges contains tuple of tuples"
gdb_test "python print (block\['f'\].name == 'f')" "True" "check variable access"
gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexistent'.*" \
"check nonexistent variable"
--
2.51.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v3] gdb/python: add property ranges to gdb.Block object
2026-03-13 14:40 ` [PATCH v3] " Jan Vrany
@ 2026-03-19 15:12 ` Tom Tromey
0 siblings, 0 replies; 9+ messages in thread
From: Tom Tromey @ 2026-03-19 15:12 UTC (permalink / raw)
To: Jan Vrany; +Cc: gdb-patches, Eli Zaretskii
>>>>> "Jan" == Jan Vrany <jan.vrany@labware.com> writes:
>> Or maybe be written in some other way.
Jan> I tried to use "(OO)", gdb_py_object_from_ulongest(start),
Jan> gdb_py_object_from_ulongest(end) but that did not look
Jan> significantly better to me.
Yeah, until the safety changes, this would need extra checking as well
anyway.
Jan> This commit adds a new property - ranges - to gdb.Block object. It holds
Jan> a tuple of ranges for that block. Each range is a tuple of (start, end)
Jan> address. For contiguous blocks it contains only one range.
This version looks good to me, thank you.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-03-19 15:12 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-11 20:18 [PATCH] gdb/python: add property ranges to gdb.Block object Jan Vrany
2026-03-12 6:52 ` Eli Zaretskii
2026-03-12 11:04 ` Jan Vraný
2026-03-12 11:35 ` Eli Zaretskii
2026-03-12 12:35 ` Tom Tromey
2026-03-12 14:33 ` [PATCH v2] " Jan Vrany
2026-03-12 15:15 ` Tom Tromey
2026-03-13 14:40 ` [PATCH v3] " Jan Vrany
2026-03-19 15:12 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox