From ea4bacd354c1eb02c2e4faaa8096e69be6f0177f Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 16 Apr 2026 16:16:13 -0400 Subject: gdb: introduce iteration_status enum, use it for search callbacks There are a bunch of iteration functions that take a callback returning true or false to indicate whether to continue or stop iterating. These functions then return the same value, indicate whether the iteration was done until the end of interrupted. I think this is confusing and error-prone, as I never know which value means what. It is especially confusing when two opposite conventions collide, such as in objfile::map_symtabs_matching_filename. I propose to make that more obvious by introducing a new iteration_status enum with self-documenting values. I started to change the callback type compunit_symtab_iteration_callback, taken by quick_symbol_functions::search, and then followed that path to update a bunch of other functions. I chose the name to be kind of generic, so that it can be used for other similar iteration patterns. I also put it in gdbsupport, in case we want to use it in gdbserver too. Change-Id: I55d84d0c1af8ac0b82cc9f49ccf0d6b60e1769e0 Approved-By: Andrew Burgess --- gdb/python/py-symbol.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'gdb/python') diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index e74c8e4b368..6293b658ea1 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -599,7 +599,7 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw) /* Skip included compunits to prevent including compunits from being searched twice. */ if (cust->user != nullptr) - return true; + return iteration_status::keep_going; const struct blockvector *bv = cust->blockvector (); const struct block *block = bv->static_block (); @@ -615,16 +615,22 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw) if (sym_obj == nullptr || PyList_Append (return_list.get (), sym_obj.get ()) == -1) - return false; + return iteration_status::stop; } } - return true; + return iteration_status::keep_going; }; - if (!objfile.search (nullptr, &lookup_name, nullptr, callback, - SEARCH_STATIC_BLOCK, flags)) - return nullptr; + /* The only reason why the iteration would stop is if an error was + encountered during the callback execution. */ + if (objfile.search (nullptr, &lookup_name, nullptr, callback, + SEARCH_STATIC_BLOCK, flags) + == iteration_status::stop) + { + gdb_assert (PyErr_Occurred ()); + return nullptr; + } } } catch (const gdb_exception &except) -- cgit v1.2.3