]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: add subblocks property to gdb.Block
authorJan Vrany <jan.vrany@labware.com>
Tue, 4 Feb 2025 13:56:49 +0000 (13:56 +0000)
committerJan Vrany <jan.vrany@labware.com>
Tue, 4 Feb 2025 13:56:49 +0000 (13:56 +0000)
This commit adds new propery "subblocks" to gdb.Block objects. This
allows Python to traverse block tree starting with global block.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Andrew Burgess <aburgess@redhat.com>
gdb/NEWS
gdb/doc/python.texi
gdb/python/py-block.c
gdb/testsuite/gdb.python/py-block.exp

index e678de55ce64771cb53adda2be58ac032abed6cd..1d8f2c5bbe7161718b08f0df281d47edf3b11507 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -152,6 +152,9 @@ binary-upload in qSupported reply
 
   ** Added the gdb.Symbol.is_artificial attribute.
 
+  ** Added gdb.Block.subblocks.  Returns a list of blocks contained in that
+     block.
+
 * Debugger Adapter Protocol changes
 
   ** The "scopes" request will now return a scope holding global
index c0bd9e7992f3214cd388fa90b170cfff52979463..d3c77d6a54a9403a06bf8ef7be3c34d61d394df5 100644 (file)
@@ -1,4 +1,4 @@
-@c Copyright (C) 2008--2024 Free Software Foundation, Inc.
+@c Copyright (C) 2008--2025 Free Software Foundation, Inc.
 @c Permission is granted to copy, distribute and/or modify this document
 @c under the terms of the GNU Free Documentation License, Version 1.3 or
 @c any later version published by the Free Software Foundation; with the
@@ -6046,6 +6046,11 @@ The block containing this block.  If this parent block does not exist,
 this attribute holds @code{None}.  This attribute is not writable.
 @end defvar
 
+@defvar Block.subblocks
+A list of blocks nested in this block.  If there are no blocks nested,
+this attribute holds an empty list.  This attribute is not writable.
+@end defvar
+
 @defvar Block.global_block
 The global block associated with this block.  This attribute is not
 writable.
index aeb9acb7260526b24b65adee3ff4b8f5131bbe95..fa7dd19228061bd9cfa802b81801b273053e0061 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to blocks.
 
-   Copyright (C) 2008-2024 Free Software Foundation, Inc.
+   Copyright (C) 2008-2025 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -149,6 +149,37 @@ blpy_get_superblock (PyObject *self, void *closure)
   Py_RETURN_NONE;
 }
 
+/* Implement gdb.Block.subblocks attribute.  Return a list of gdb.Block
+   objects that are direct children of this block.  */
+
+static PyObject *
+blpy_get_subblocks (PyObject *self, void *closure)
+{
+  const struct block *block;
+
+  BLPY_REQUIRE_VALID (self, block);
+
+  gdbpy_ref<> list (PyList_New (0));
+  if (list == nullptr)
+    return nullptr;
+
+  compunit_symtab *cu = block->global_block ()->compunit ();
+
+  for (const struct block *each : cu->blockvector ()->blocks ())
+    {
+      if (each->superblock () == block)
+       {
+         gdbpy_ref<> item (block_to_block_object (each, cu->objfile ()));
+
+         if (item.get () == nullptr
+             || PyList_Append (list.get (), item.get ()) == -1)
+           return nullptr;
+       }
+    }
+
+  return list.release ();
+}
+
 /* Return the global block associated to this block.  */
 
 static PyObject *
@@ -529,6 +560,8 @@ static gdb_PyGetSetDef block_object_getset[] = {
     "Whether this block is a static block.", NULL },
   { "is_global", blpy_is_global, NULL,
     "Whether this block is a global block.", NULL },
+  { "subblocks", blpy_get_subblocks, nullptr,
+    "List of blocks contained in this block.", nullptr },
   { NULL }  /* Sentinel */
 };
 
index 0e6851ddf8bf7b85cf5549c3fc7a29595f4ce3d8..ce3f7ce04c4b4c807437b86d1854157688f30bcf 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2010-2024 Free Software Foundation, Inc.
+# Copyright (C) 2010-2025 Free Software Foundation, Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -61,6 +61,10 @@ gdb_py_test_silent_cmd "python sblock = block.static_block" \
     "Get block, static_block" 1
 gdb_test "python print (gblock.is_global)" "True" "is the global block"
 gdb_test "python print (sblock.is_static)" "True" "is the static block"
+gdb_test "python print (len(gblock.subblocks) > 0)" "True" \
+       "global block contains at least one block"
+gdb_test "python print (sblock in gblock.subblocks)" "True" \
+       "global block contains at static block"
 
 # Move up superblock(s) until we reach function block_func.
 gdb_test_no_output "python block = block.superblock" "get superblock"