* [PATCH] gdb/python: Handle unnamed frames in caller-matching functions [not found] <b555285517bf4506b85c0421ea062784@huawei.com> @ 2026-10-13 11:35 ` Dmitry Ivanov (A) 2026-10-14 16:04 ` Tom Tromey 0 siblings, 1 reply; 5+ messages in thread From: Dmitry Ivanov (A) @ 2026-10-13 11:35 UTC (permalink / raw) To: gdb-patches [-- Attachment #1: Type: text/plain, Size: 3251 bytes --] gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError. Treat unnamed frames as non-matches. For None, keep walking older frames after encountering an unnamed frame. Add regression tests for both convenience functions. --- This is my first GDB contribution. I used ChatGPT to help investigate the issue and draft an initial version of the patch (it's fairly simple but still worth mentioning). I reviewed, simplified, and tested the final change myself. Tested with: make -C gdb check RUNTESTFLAGS='gdb.python/py-caller-is.exp' gdb/python/lib/gdb/function/caller_is.py | 6 ++++-- gdb/testsuite/gdb.python/py-caller-is.exp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/gdb/python/lib/gdb/function/caller_is.py b/gdb/python/lib/gdb/function/caller_is.py index bca09590811..a2a5798b914 100644 --- a/gdb/python/lib/gdb/function/caller_is.py +++ b/gdb/python/lib/gdb/function/caller_is.py @@ -80,7 +80,8 @@ class CallerMatches(gdb.Function): if frame is None: return False nframes = nframes - 1 - return re.match(name.string(), frame.name()) is not None + frame_name = frame.name() + return frame_name is not None and re.match(name.string(), frame_name) is not None class AnyCallerIs(gdb.Function): @@ -143,7 +144,8 @@ class AnyCallerMatches(gdb.Function): frame = gdb.selected_frame() name_re = re.compile(name.string()) while nframes >= 0: - if name_re.match(frame.name()) is not None: + frame_name = frame.name() + if frame_name is not None and name_re.match(frame_name) is not None: return True frame = frame.older() if frame is None: diff --git a/gdb/testsuite/gdb.python/py-caller-is.exp b/gdb/testsuite/gdb.python/py-caller-is.exp index d9b826cb6a2..74ee2ae645a 100644 --- a/gdb/testsuite/gdb.python/py-caller-is.exp +++ b/gdb/testsuite/gdb.python/py-caller-is.exp @@ -70,3 +70,23 @@ proc test_all_caller_is_fns { } { } test_all_caller_is_fns + +# Prepend an unnamed frame to the current stack without depending on +# missing symbols in the host's runtime libraries. +gdb_test_multiline "define unnamed frame helper" \ + "python" "" \ + "from unittest.mock import Mock, patch" "" \ + "def check_unnamed_frame(expr):" "" \ + " frame = Mock()" "" \ + " frame.name.return_value = None" "" \ + " frame.older.return_value = gdb.selected_frame()" "" \ + " with patch.object(gdb, 'selected_frame', return_value=frame):" "" \ + " print(int(gdb.parse_and_eval(expr)))" "" \ + "end" "" + +gdb_test {python check_unnamed_frame('$_caller_matches(".*", 0)')} \ + "0" "caller_matches with unnamed frame" +gdb_test {python check_unnamed_frame('$_any_caller_matches(".*", 0)')} \ + "0" "any_caller_matches with unnamed frame" +gdb_test {python check_unnamed_frame('$_any_caller_matches("^bottom_func$", 1)')} \ + "1" "any_caller_matches skips unnamed frame" -- 2.34.1 [-- Attachment #2: Type: text/html, Size: 9259 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] gdb/python: Handle unnamed frames in caller-matching functions 2026-10-13 11:35 ` [PATCH] gdb/python: Handle unnamed frames in caller-matching functions Dmitry Ivanov (A) @ 2026-10-14 16:04 ` Tom Tromey 2026-11-06 7:25 ` [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError Dmitry Ivanov 0 siblings, 1 reply; 5+ messages in thread From: Tom Tromey @ 2026-10-14 16:04 UTC (permalink / raw) To: Dmitry Ivanov (A); +Cc: gdb-patches >>>>> Dmitry Ivanov (A) <dmitry.ivanov1@huawei.com> writes: > This is my first GDB contribution. Hi. However you sent this, the mail got pretty mangled somewhere. There are a lot of extra newlines. So it can't be applied as-is. > + frame_name = frame.name() > + return frame_name is not None and re.match(name.string(), frame_name) is not None I wonder if instead the None case should be turned into an empty string here, so a user of $_caller_matches could match against "^$" to detect nameless frames. I think this would be as easy as frame_name = frame.name() or "" In this case perhaps the documentation could be updated a bit as well. > +# Prepend an unnamed frame to the current stack without depending on > +# missing symbols in the host's runtime libraries. Cool technique. Tom ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError. 2026-10-14 16:04 ` Tom Tromey @ 2026-11-06 7:25 ` Dmitry Ivanov 2026-11-06 10:26 ` Eli Zaretskii 2026-11-06 16:46 ` Tom Tromey 0 siblings, 2 replies; 5+ messages in thread From: Dmitry Ivanov @ 2026-11-06 7:25 UTC (permalink / raw) To: tom; +Cc: gdb-patches Treat unnamed frames as having empty names. Add regression tests for both convenience functions. --- Hi Tom, Thanks for the review. I've updated the patch to treat unnamed frame names as empty strings, as suggested, and adjusted the documentation and tests accordingly. Changes in v2: - Treat unnamed frame names as empty strings instead of non-matches, as suggested by Tom. - Update documentation and regression tests accordingly. gdb/doc/gdb.texinfo | 8 +++++-- gdb/python/lib/gdb/function/caller_is.py | 6 +++-- gdb/testsuite/gdb.python/py-caller-is.exp | 28 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 6d7fce4a8c2..6ea51df225e 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -13673,7 +13673,9 @@ $1 = 1 @findex $_caller_matches@r{, convenience function} @item $_caller_matches(@var{regexp}@r{[}, @var{number_of_frames}@r{]}) Returns one if the calling function's name matches the regular expression -@var{regexp}. Otherwise it returns zero. +@var{regexp}. Otherwise it returns zero. If the function's name cannot be +determined, it is treated as an empty string, which can be matched using +@code{^$}. If the optional argument @var{number_of_frames} is provided, it is the number of frames up in the stack to look. @@ -13696,7 +13698,9 @@ frame specified by @var{number_of_frames}. @findex $_any_caller_matches@r{, convenience function} @item $_any_caller_matches(@var{regexp}@r{[}, @var{number_of_frames}@r{]}) Returns one if any calling function's name matches the regular expression -@var{regexp}. Otherwise it returns zero. +@var{regexp}. Otherwise it returns zero. If the function's name cannot be +determined, it is treated as an empty string, which can be matched using +@code{^$}. If the optional argument @var{number_of_frames} is provided, it is the number of frames up in the stack to look. diff --git a/gdb/python/lib/gdb/function/caller_is.py b/gdb/python/lib/gdb/function/caller_is.py index bca09590811..65613aabdf1 100644 --- a/gdb/python/lib/gdb/function/caller_is.py +++ b/gdb/python/lib/gdb/function/caller_is.py @@ -80,7 +80,8 @@ class CallerMatches(gdb.Function): if frame is None: return False nframes = nframes - 1 - return re.match(name.string(), frame.name()) is not None + frame_name = frame.name() or "" + return re.match(name.string(), frame_name) is not None class AnyCallerIs(gdb.Function): @@ -143,7 +144,8 @@ class AnyCallerMatches(gdb.Function): frame = gdb.selected_frame() name_re = re.compile(name.string()) while nframes >= 0: - if name_re.match(frame.name()) is not None: + frame_name = frame.name() or "" + if name_re.match(frame_name) is not None: return True frame = frame.older() if frame is None: diff --git a/gdb/testsuite/gdb.python/py-caller-is.exp b/gdb/testsuite/gdb.python/py-caller-is.exp index d9b826cb6a2..09b0bfb1f8e 100644 --- a/gdb/testsuite/gdb.python/py-caller-is.exp +++ b/gdb/testsuite/gdb.python/py-caller-is.exp @@ -70,3 +70,31 @@ proc test_all_caller_is_fns { } { } test_all_caller_is_fns + +# Prepend an unnamed frame to the current stack without depending on +# missing symbols in the host's runtime libraries. +gdb_test_multiline "define unnamed frame helper" \ + "python" "" \ + "from unittest.mock import Mock, patch" "" \ + "def check_unnamed_frame(expr):" "" \ + " frame = Mock()" "" \ + " frame.name.return_value = None" "" \ + " frame.older.return_value = gdb.selected_frame()" "" \ + " with patch.object(gdb, 'selected_frame', return_value=frame):" "" \ + " print(int(gdb.parse_and_eval(expr)))" "" \ + "end" "" + +gdb_test {python check_unnamed_frame('$_caller_matches("^$", 0)')} \ + "1" "caller_matches detects unnamed frame" + +gdb_test {python check_unnamed_frame('$_caller_matches("^.+$", 0)')} \ + "0" "caller_matches rejects unnamed frame for nonempty name" + +gdb_test {python check_unnamed_frame('$_any_caller_matches("^$", 0)')} \ + "1" "any_caller_matches detects unnamed frame" + +gdb_test {python check_unnamed_frame('$_any_caller_matches("^.+$", 0)')} \ + "0" "any_caller_matches rejects unnamed frame for nonempty name" + +gdb_test {python check_unnamed_frame('$_any_caller_matches("^bottom_func$", 1)')} \ + "1" "any_caller_matches continues past unnamed frame" -- 2.34.1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError. 2026-11-06 7:25 ` [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError Dmitry Ivanov @ 2026-11-06 10:26 ` Eli Zaretskii 2026-11-06 16:46 ` Tom Tromey 1 sibling, 0 replies; 5+ messages in thread From: Eli Zaretskii @ 2026-11-06 10:26 UTC (permalink / raw) To: Dmitry Ivanov; +Cc: tom, gdb-patches > From: Dmitry Ivanov <dmitry.ivanov1@huawei.com> > CC: <gdb-patches@sourceware.org> > Date: Fri, 6 Nov 2026 07:25:53 +0000 > > Treat unnamed frames as having empty names. > > Add regression tests for both convenience functions. > --- > Hi Tom, > > Thanks for the review. I've updated the patch to treat unnamed > frame names as empty strings, as suggested, and adjusted the > documentation and tests accordingly. > > Changes in v2: > - Treat unnamed frame names as empty strings instead of non-matches, > as suggested by Tom. > - Update documentation and regression tests accordingly. > > gdb/doc/gdb.texinfo | 8 +++++-- > gdb/python/lib/gdb/function/caller_is.py | 6 +++-- > gdb/testsuite/gdb.python/py-caller-is.exp | 28 +++++++++++++++++++++++ > 3 files changed, 38 insertions(+), 4 deletions(-) Thanks. > diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo > index 6d7fce4a8c2..6ea51df225e 100644 > --- a/gdb/doc/gdb.texinfo > +++ b/gdb/doc/gdb.texinfo > @@ -13673,7 +13673,9 @@ $1 = 1 > @findex $_caller_matches@r{, convenience function} > @item $_caller_matches(@var{regexp}@r{[}, @var{number_of_frames}@r{]}) > Returns one if the calling function's name matches the regular expression > -@var{regexp}. Otherwise it returns zero. > +@var{regexp}. Otherwise it returns zero. If the function's name cannot be ^^ Two spaces between sentences there, please. > @@ -13696,7 +13698,9 @@ frame specified by @var{number_of_frames}. > @findex $_any_caller_matches@r{, convenience function} > @item $_any_caller_matches(@var{regexp}@r{[}, @var{number_of_frames}@r{]}) > Returns one if any calling function's name matches the regular expression > -@var{regexp}. Otherwise it returns zero. > +@var{regexp}. Otherwise it returns zero. If the function's name cannot be Same here. Otherwise, the documentation part is okay. Reviewed-By: Eli Zaretskii <eliz@gnu.org> ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError. 2026-11-06 7:25 ` [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError Dmitry Ivanov 2026-11-06 10:26 ` Eli Zaretskii @ 2026-11-06 16:46 ` Tom Tromey 1 sibling, 0 replies; 5+ messages in thread From: Tom Tromey @ 2026-11-06 16:46 UTC (permalink / raw) To: Dmitry Ivanov; +Cc: tom, gdb-patches >>>>> "Dmitry" == Dmitry Ivanov <dmitry.ivanov1@huawei.com> writes: Dmitry> Treat unnamed frames as having empty names. Dmitry> Add regression tests for both convenience functions. Dmitry> --- Dmitry> Hi Tom, Dmitry> Thanks for the review. I've updated the patch to treat unnamed Dmitry> frame names as empty strings, as suggested, and adjusted the Dmitry> documentation and tests accordingly. Looks good to me. Thank you. Approved-By: Tom Tromey <tom@tromey.com> I guess based on the test that we will need a copyright assignment from you. Do you have one already? I don't know. thanks Tom ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-06 16:46 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <b555285517bf4506b85c0421ea062784@huawei.com>
2026-10-13 11:35 ` [PATCH] gdb/python: Handle unnamed frames in caller-matching functions Dmitry Ivanov (A)
2026-10-14 16:04 ` Tom Tromey
2026-11-06 7:25 ` [PATCH v2] gdb/python: Handle unnamed frames in caller-matching functions git commit --amend gdb.Frame.name() can return None when GDB cannot determine a frame's function name. and currently pass that value directly to Python's regular expression functions, causing a TypeError Dmitry Ivanov
2026-11-06 10:26 ` Eli Zaretskii
2026-11-06 16:46 ` Tom Tromey
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox