From patchwork Mon Feb 12 18:47:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 85621 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28F5B3858C2A for ; Mon, 12 Feb 2024 18:47:39 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id 8BC683858CDB for ; Mon, 12 Feb 2024 18:47:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8BC683858CDB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8BC683858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707763626; cv=none; b=AgYF//ENNXqpbOll3ETezGVsQ2ZoZr8o92LsJaFxHfzZYm8FRKuK01qeYI4kMrsGXh0s0NRaMI58Gj0V/CQFZk60RzLFLcKIdVx6m5zv466jgZ+n+wYOSV6yQOF/62D/SGSlicMmzASs6qVXW523JJ3Kugz8pdZB5o65eyogc/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707763626; c=relaxed/simple; bh=Akqb4yGTESGxlNQlUggq3p4bS0fQKkwIax/unK6AkIE=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=x0LkmCx6i8ChY9rbDJncNfciocvNmV/e6Z8abB8UUUP33kJyVFox2XIEeijJegnErH6zsE4hdMIOA9mCChU65iG2y5yQDCQQQKlfcUv5wemPzFPGYvgHpCvCr+M3hhMlpX5Fxinq4OmsozC15ISBGyAVEulvztnGMefCEVLGdqY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-7c3e4290f5fso159879539f.2 for ; Mon, 12 Feb 2024 10:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1707763623; x=1708368423; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=khzeEUKi97Gq7oDmq0PjyiuUwSgZxlS4iywBwjdRO20=; b=XUH8hVH+T+e70Ov8zaNHajlOO/jygNWmhBRHI5sFSfSt7RyS5iblwtriQqTWZ5oAby Xb1RrH03D3O3FlqViaABD8C0EpWzlNF9Yw+Y0zfbMnxYE/TAL/aCtI6Hbjg1GrQ0GylR Mv/HrxE21gmQdCcrj1XtIkXQEJPo41XVFQPhGnOE9K6oXaYrE4Jl7ZChlANS1Jb5YjW/ uuBdGnRvNim2VdaoJZgz8h5jNbwNPJlwW0ntbWA6oPyZ+P8U/EuEKNM1OpHjEXX9Vsz3 ToG0rBtOyIJY6t+dnt3J0nK1iphTYwJgLlGUrgYepYFmpcsTnU59wzbmyliK9ckP17R1 XaCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707763623; x=1708368423; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=khzeEUKi97Gq7oDmq0PjyiuUwSgZxlS4iywBwjdRO20=; b=eL5oSgrTfpRL0ropJO6QhLG0XsQMKVDkY0vcRN9jfiVguBgN5hp4mp9WtTXn67X76x IsHLrlPS/vUk857PJB07sXYE176Ig305Njfa3Zawu0fTnXDtKZD5Xh/HWzKC1B+GTsd9 sM7LulxeseEES329fxH+im4mKxvfX6vPhJ1SfycwHGFV36VXm4xsXPAtV9nl9zhAzDzt HaBKjQ0cI0yvqn48GRY1Nxzdpaz0v7U2YtTg5CLuy6ACXcVNA4K1cj+QR3cW6Ibf7m5b 5dyv+k0HBePw4jVIOd/Hg2QV6HKNP4DXlqN63cYbn4vWhpQkFRMq6MHbfwDOZDYDbIVa oI1Q== X-Gm-Message-State: AOJu0YwE+okoQrA4/Wu6r07+8a2TitPhpg0MNMCnD/MmXEkjgfzK0UlH hKOihbqKP2p8ue72ARuAPGuU+OXRa7VWKLEP4ekVd1mwBF6yigQqj2dvhwLhSIrwMs5PuOaq4uI = X-Google-Smtp-Source: AGHT+IG5X7Zc+bNNLLubugcHsEtTvGsXCJDyIuztVhvrwzWETGLlHGq16fIMcEKk/n0sPsx17x0Fig== X-Received: by 2002:a6b:2a88:0:b0:7c4:6efa:486b with SMTP id q130-20020a6b2a88000000b007c46efa486bmr1000131ioq.15.1707763623689; Mon, 12 Feb 2024 10:47:03 -0800 (PST) Received: from localhost.localdomain (97-122-68-157.hlrn.qwest.net. [97.122.68.157]) by smtp.gmail.com with ESMTPSA id ep15-20020a0566384e0f00b0047132ed4890sm1523547jab.105.2024.02.12.10.47.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 10:47:03 -0800 (PST) From: Tom Tromey Date: Mon, 12 Feb 2024 11:47:02 -0700 Subject: [PATCH 1/2] Export "finish" return value to Python MIME-Version: 1.0 Message-Id: <20240212-dap-finish-v1-1-8e30870e8b25@adacore.com> References: <20240212-dap-finish-v1-0-8e30870e8b25@adacore.com> In-Reply-To: <20240212-dap-finish-v1-0-8e30870e8b25@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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+patchwork=sourceware.org@sourceware.org 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. The test is in the next patch. Reviewed-By: Eli Zaretskii --- 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 ece9038844c..21a84fabb34 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3813,6 +3813,12 @@ corresponding MI output (@pxref{GDB/MI Async Records}). 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 Emits @code{gdb.SignalEvent}, which extends @code{gdb.StopEvent}. diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 4e17a6b0629..68f64f0d2b3 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; } -/* 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. */ 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" 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 = nullptr; try { @@ -55,6 +57,10 @@ py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) { async_reply_reason reason = tp->thread_fsm ()->async_reply_reason (); uiout.field_string ("reason", async_reason_lookup (reason)); + + return_value_info *rvinfo = tp->thread_fsm ()->return_value (); + if (rvinfo != nullptr && rvinfo->value != nullptr) + return_value = rvinfo->value; } if (stop_signal != GDB_SIGNAL_0 && stop_signal != GDB_SIGNAL_TRAP) @@ -73,7 +79,22 @@ py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) return nullptr; } - return uiout.result (); + gdbpy_ref<> dict = uiout.result (); + if (dict == 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 != nullptr) + { + gdbpy_ref<> val (value_to_value_object (return_value)); + if (val == nullptr) + return nullptr; + if (PyDict_SetItemString (dict.get (), "finish-value", val.get ()) < 0) + return nullptr; + } + + return dict; } /* 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 @@ 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; +}; /* A thread finite-state machine structure contains the necessary info and callbacks to manage the state machine protocol of a thread's