From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 0F0723858D33; Fri, 8 Mar 2024 17:56:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F0723858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1709920587; bh=69D23jXG8whBXJltlP2OofjOtbVx51AnqwrGMlnNSi0=; h=From:To:Subject:Date:From; b=Xs7vUsSodx215cYBV3pxrEdPDlufdfksbEFOzHX5udXOsbodnfOOC7vOgECho8N8X mynnl50N8PJkLX7GEcQ82TiwZtsd2nlL2vB+z5mJsLaZaloMLbjufkQM2T3zQ6qxqb fdP91TynYJ16RzrMEB8B57eqSNzLMJZytDtc3pMM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Export "finish" return value to Python X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 03fa0c63d3a5944afcf031ecf0b433b2985e6eeb X-Git-Newrev: 99761c5ab53e11105b6067bc4314e74bb066006c Message-Id: <20240308175627.0F0723858D33@sourceware.org> Date: Fri, 8 Mar 2024 17:56:26 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D99761c5ab53e= 11105b6067bc4314e74bb066006c commit 99761c5ab53e11105b6067bc4314e74bb066006c Author: Tom Tromey Date: Mon Feb 12 08:12:02 2024 -0700 Export "finish" return value to Python =20 This patch changes the Python "stop" event emission code to also add the function return value, if it is known. This happens when the stop comes from a "finish" command and when the value can be fetched. =20 The test is in the next patch. =20 Reviewed-By: Eli Zaretskii Diff: --- gdb/doc/python.texi | 6 ++++++ gdb/infcmd.c | 17 ----------------- gdb/python/py-stopevent.c | 23 ++++++++++++++++++++++- gdb/thread-fsm.h | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 4ae72716f56..4ca3ae4eca4 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3813,6 +3813,12 @@ corresponding MI output (@pxref{GDB/MI Async Records= }). =20 A dictionary was used for this (rather than adding attributes directly to the event object) so that the MI keys could be used unchanged. + +When a @code{StopEvent} results from a @code{finish} command, it will +also hold the return value from the function, if that is available. +This will be an entry named @samp{return-value} in the @code{details} +dictionary. The value of this entry will be a @code{gdb.Value} +object. @end defvar =20 Emits @code{gdb.SignalEvent}, which extends @code{gdb.StopEvent}. diff --git a/gdb/infcmd.c b/gdb/infcmd.c index c1fdbb300c6..ac41ebf11b4 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -1484,23 +1484,6 @@ get_return_value (struct symbol *func_symbol, struct= value *function) return value; } =20 -/* The captured function return value/type and its position in the - value history. */ - -struct return_value_info -{ - /* The captured return value. May be NULL if we weren't able to - retrieve it. See get_return_value. */ - struct value *value; - - /* The return type. In some cases, we'll not be able extract the - return value, but we always know the type. */ - struct type *type; - - /* If we captured a value, this is the value history index. */ - int value_history_index; -}; - /* Helper for print_return_value. */ =20 static void diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c index 61d93727b31..fcaebe26f13 100644 --- a/gdb/python/py-stopevent.c +++ b/gdb/python/py-stopevent.c @@ -20,6 +20,7 @@ #include "defs.h" #include "py-stopevent.h" #include "py-uiout.h" +#include "thread-fsm.h" =20 gdbpy_ref<> create_stop_event_object (PyTypeObject *py_type, const gdbpy_ref<> &dict) @@ -45,6 +46,7 @@ static gdbpy_ref<> py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) { py_ui_out uiout; + struct value *return_value =3D nullptr; =20 try { @@ -55,6 +57,10 @@ py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) { async_reply_reason reason =3D tp->thread_fsm ()->async_reply_reason (); uiout.field_string ("reason", async_reason_lookup (reason)); + + return_value_info *rvinfo =3D tp->thread_fsm ()->return_value (); + if (rvinfo !=3D nullptr && rvinfo->value !=3D nullptr) + return_value =3D rvinfo->value; } =20 if (stop_signal !=3D GDB_SIGNAL_0 && stop_signal !=3D GDB_SIGNAL_TRA= P) @@ -73,7 +79,22 @@ py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) return nullptr; } =20 - return uiout.result (); + gdbpy_ref<> dict =3D uiout.result (); + if (dict =3D=3D nullptr) + return nullptr; + + /* This has to be done separately to avoid error issues, and because + there's no API to add generic Python objects to a py_ui_out. */ + if (return_value !=3D nullptr) + { + gdbpy_ref<> val (value_to_value_object (return_value)); + if (val =3D=3D nullptr) + return nullptr; + if (PyDict_SetItemString (dict.get (), "finish-value", val.get ()) <= 0) + return nullptr; + } + + return dict; } =20 /* Callback observers when a stop event occurs. This function will create= a diff --git a/gdb/thread-fsm.h b/gdb/thread-fsm.h index 90abb5c27f6..ed117719c0d 100644 --- a/gdb/thread-fsm.h +++ b/gdb/thread-fsm.h @@ -23,6 +23,25 @@ =20 struct return_value_info; struct thread_fsm_ops; +struct type; +struct value; + +/* The captured function return value/type and its position in the + value history. */ + +struct return_value_info +{ + /* The captured return value. May be NULL if we weren't able to + retrieve it. See get_return_value. */ + struct value *value; + + /* The return type. In some cases, we'll not be able extract the + return value, but we always know the type. */ + struct type *type; + + /* If we captured a value, this is the value history index. */ + int value_history_index; +}; =20 /* A thread finite-state machine structure contains the necessary info and callbacks to manage the state machine protocol of a thread's