From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id +bheHaVChmpFXTEAWB0awg (envelope-from ) for ; Wed, 19 Aug 2026 19:56:21 -0400 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (768-bit key; unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=U9onVAEF; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 722BA1E0A3; Wed, 19 Aug 2026 19:56:21 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id BAF241E09B for ; Wed, 19 Aug 2026 19:56:20 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5BEF64B99F7C for ; Wed, 19 Aug 2026 23:56:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5BEF64B99F7C Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=U9onVAEF Received: from omta40.uswest2.a.cloudfilter.net (omta40.uswest2.a.cloudfilter.net [35.89.44.39]) by sourceware.org (Postfix) with ESMTPS id 895B74BAE7EE for ; Wed, 19 Aug 2026 23:54:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 895B74BAE7EE Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 895B74BAE7EE Authentication-Results: sourceware.org; arc=none smtp.remote-ip=35.89.44.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1787183684; cv=none; b=Rpft8JM3jU7o8WAE4T8MEVBb1zSocBDV0EkwE2mEE/xvxufVW/AkvLXUlo0v5SclMzoxHHnFfaacBNQJC3YljbGrKTuXqL+HuvtT6sCpt1Rvo2P6Gkav+F+IEp1ilONEeZbHXMHanxzyGl02QIfdOdOk4OM81L/lpGhDyUQXdcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1787183684; c=relaxed/simple; bh=47kCjmcmF5lhPJxaf2JF/QTZ7Z4baTK9ic0I9w1G0bU=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=ftfcs6x0PgC0BVM+CpXK1xGO0A3syfQFC2097C2pP8JlIkBfvu5s2Yj8xSuZrnPiSfeLbCk6czbKSlvkpmqK3NWFWG5G/mgqrlzgRxQLnICVbeo0tf4g3ZG9GNOwCaCapkpaCI01Fak7dGHJhxxRQ9Gr3LhzCtabvhZnatyY8VE= ARC-Authentication-Results: i=1; sourceware.org; dkim=policy (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=U9onVAEF reason="signing key too small" DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 895B74BAE7EE Received: from eig-obgw-6006b.ext.cloudfilter.net ([10.0.30.211]) by cmsmtp with ESMTPS id wpHywWv8IhJKMwq7Hw60EW; Wed, 19 Aug 2026 23:54:43 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id wq7GwYJqXxg8Ywq7GwPrwL; Wed, 19 Aug 2026 23:54:42 +0000 X-Authority-Analysis: v=2.4 cv=R44DGcRX c=1 sm=1 tr=0 ts=6a864242 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=Sv0fKeRqtYgA:10 a=ItBw4LHWJt0A:10 a=TYJEEQ40o1Y8WCiuVxwA:9 a=QEXdDO2ut3YA:10 a=O8hF6Hzn-FEA:10 a=DCx65vhANUyCzuf5D8fC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=mFZgawUaRRmLYXoIVNOlK6cTGUlTN1sb5UXQx2rFyyM=; b=U9onVAEF6Ha84t3gAUsqSeZSWz tLxcXpatxBypDxATAW2w/pMv8wUaNSB+2J+9+pI9y5heMu38/hqh/ncDLXc+OHWAmzAopvHToZfJA 1i522HrHL2q14LgBhDcsjvgAD; Received: from 75-166-225-82.hlrn.qwest.net ([75.166.225.82]:36824 helo=[192.168.122.1]) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1wwq7G-000000020o1-019j; Wed, 19 Aug 2026 17:54:42 -0600 From: Tom Tromey Date: Wed, 19 Aug 2026 17:54:40 -0600 Subject: [PATCH 2/4] Convert gdbpy_newest_frame and gdbpy_selected_frame to safety API MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260819-python-safety-frame-v1-2-563cb6b9e7a6@tromey.com> References: <20260819-python-safety-frame-v1-0-563cb6b9e7a6@tromey.com> In-Reply-To: <20260819-python-safety-frame-v1-0-563cb6b9e7a6@tromey.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.14.3 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.225.82 X-Source-L: No X-Exim-ID: 1wwq7G-000000020o1-019j X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-225-82.hlrn.qwest.net ([192.168.122.1]) [75.166.225.82]:36824 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfM9YK4NCf36uRuCzm6As2rIV4UeI5GzvE32iF52w3BiX5WU49m+NNsx8v4xB647H72jXqyVDFMWUrS5eqZqXfbwp5y+FKHwYNcQSR8C8G3W8WwxiB8lR SEJiwDBgYU9XnHoFooxyjviprBpNRkHaX6+zPfRs5cKMvTsBfclAOpkaHwNvU+k+viTIHTVuIQW/gcgVLEPW3v5J4ggv5lhsPnI= X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This convert gdbpy_newest_frame and gdbpy_selected_frame to the Python safety API. Some extra work was needed in py-inferior.c; note that the code there is "temporary" -- once event emission is converted, the try/catch can be removed. Also, a new noargs_function wrapper was needed. --- gdb/python/py-frame.c | 44 ++++++++++++++++---------------------------- gdb/python/py-inferior.c | 24 ++++++++++++++++++------ gdb/python/py-safety.h | 21 +++++++++++++++++++++ gdb/python/python-internal.h | 4 ++-- gdb/python/python.c | 8 ++++---- 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index 5c5ff8cf0de..3910f19ef83 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -438,41 +438,29 @@ frame_object::static_link () /* Implementation of gdb.newest_frame () -> gdb.Frame. Returns the newest frame object. */ -PyObject * -gdbpy_newest_frame (PyObject *self, PyObject *args) +gdbpy_ref<> +gdbpy_newest_frame () { - frame_info_ptr frame = NULL; - - try - { - frame = get_current_frame (); - } - catch (const gdb_exception &except) - { - return gdbpy_handle_gdb_exception (nullptr, except); - } - - return frame_info_to_frame_object (frame).release (); + /* FIXME: Python safety. Convert frame_info_to_frame_object. */ + gdbpy_ref<> result = frame_info_to_frame_object (get_current_frame ()); + if (result == nullptr) + throw gdb_python_exception (); + return result; } /* Implementation of gdb.selected_frame () -> gdb.Frame. Returns the selected frame object. */ -PyObject * -gdbpy_selected_frame (PyObject *self, PyObject *args) +gdbpy_ref<> +gdbpy_selected_frame () { - frame_info_ptr frame = NULL; - - try - { - frame = get_selected_frame ("No frame is currently selected."); - } - catch (const gdb_exception &except) - { - return gdbpy_handle_gdb_exception (nullptr, except); - } - - return frame_info_to_frame_object (frame).release (); + frame_info_ptr frame + = get_selected_frame ("No frame is currently selected."); + /* FIXME: Python safety. Convert frame_info_to_frame_object. */ + gdbpy_ref<> result = frame_info_to_frame_object (frame); + if (result == nullptr) + throw gdb_python_exception (); + return result; } /* Implementation of gdb.stop_reason_string (Integer) -> String. diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 426aec31e9e..780f7271c09 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -1023,13 +1023,25 @@ python_context_changed (user_selected_what selection) } gdbpy_ref<> frame_obj; - if (has_stack_frames ()) - frame_obj = gdbpy_ref<> (gdbpy_selected_frame (nullptr, nullptr)); - else - frame_obj = py_none (); - - if (frame_obj == nullptr) + /* FIXME: Python safety. Eventually this function will be converted + and this try/catch can be removed. */ + try + { + if (has_stack_frames ()) + frame_obj = gdbpy_selected_frame (); + else + frame_obj = py_none (); + } + catch (const gdb_python_exception &e) + { + gdbpy_print_stack (); + return; + } + catch (const gdb_exception &exc) { + /* This is a bit roundabout but we're going to be deleting this + code someday anyway. */ + (void) gdbpy_handle_gdb_exception (nullptr, exc); gdbpy_print_stack (); return; } diff --git a/gdb/python/py-safety.h b/gdb/python/py-safety.h index 3294f38c8b6..06324868817 100644 --- a/gdb/python/py-safety.h +++ b/gdb/python/py-safety.h @@ -233,6 +233,27 @@ varargs_wrapper (PyObject *self, PyObject *args, PyObject *kw) } /* namespace safety_details */ +/* Create a PyMethodDef for a no-argument function. It takes the + underlying function F as template parameters, and the name and + documentation as arguments. The function F is wrapped to call + to_python and to catch exceptions per the safety protocol. F + should not accept any arguments. */ +template +constexpr PyMethodDef +noargs_function (const char *name, const char *doc) +{ + using namespace safety_details; + return { + name, + [] (PyObject *self, PyObject *args) -> PyObject * + { + return wrapped_function (); + }, + METH_NOARGS, + doc, + }; +} + /* Create a PyMethodDef for a no-argument method. It takes the underlying class C and a pointer-to-method M as template parameters, and the name and documentation as arguments. The diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 5529e9fd4d7..76ccdc6b0c6 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -477,8 +477,8 @@ gdbpy_ref<> gdbpy_lookup_static_symbols (gdbpy_borrowed_ref<> args, PyObject *gdbpy_start_recording (PyObject *self, PyObject *args); PyObject *gdbpy_current_recording (PyObject *self, PyObject *args); PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args); -PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args); -PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args); +gdbpy_ref<> gdbpy_newest_frame (); +gdbpy_ref<> gdbpy_selected_frame (); PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw); int gdbpy_is_field (PyObject *obj); PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length, diff --git a/gdb/python/python.c b/gdb/python/python.c index 14c243b135e..72c2d7bb10d 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -3159,12 +3159,12 @@ Arguments (also strings) are passed to the command." }, { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, "Return the current Objfile being loaded, or None." }, - { "newest_frame", gdbpy_newest_frame, METH_NOARGS, + noargs_function ("newest_frame", "newest_frame () -> gdb.Frame.\n\ -Return the newest frame object." }, - { "selected_frame", gdbpy_selected_frame, METH_NOARGS, +Return the newest frame object."), + noargs_function ("selected_frame", "selected_frame () -> gdb.Frame.\n\ -Return the selected frame object." }, +Return the selected frame object."), { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS, "stop_reason_string (Integer) -> String.\n\ Return a string explaining unwind stop reason." }, -- 2.49.0