From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [PATCH] gdb/python: add property ranges to gdb.Block object
Date: Wed, 11 Mar 2026 20:18:26 +0000 [thread overview]
Message-ID: <20260311201826.2244014-1-jan.vrany@labware.com> (raw)
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
next reply other threads:[~2026-03-11 20:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-11 20:18 Jan Vrany [this message]
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
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=20260311201826.2244014-1-jan.vrany@labware.com \
--to=jan.vrany@labware.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