From patchwork Tue Feb 13 16:58:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 85673 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 C95C43858033 for ; Tue, 13 Feb 2024 16:59:33 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 6388C3858011 for ; Tue, 13 Feb 2024 16:58:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6388C3858011 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 6388C3858011 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::132 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707843540; cv=none; b=GLPmZs0DGYtzhJFkrMjfrLKPKUpTNs+MWupL/SjkTsDCCtISFj5p4wFLo22/7RBiJnEZlkZb6CAfXGt8eKMEkGy775mihDHtmyLWxn0/HFnJQqUMb+7P31uQ/Mr8YGIgV+BxqOH14YkTHdrEQyryVWoqm5s94KZIt5SaVlXNsrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707843540; c=relaxed/simple; bh=/U2nV7aasO1xW/CwJfSH54FRmAaCPA/q71ZzMTSyDp8=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=I/ffttj44HBVD0IJalja9vuMGypZhAexqGrkmqC/xhdJni4R1w70TGfngnsfQBdhK2f13/d7N/qlN3jboCMrwdefqqN4+e0VmKcE1Y6ADuqcHFW76qL3lNzpIr+wfeXOK1xW07Wf/61J40p83IFHepzYtw7Eg3r6cLRcKIpQ37Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-363b8429f03so13072995ab.1 for ; Tue, 13 Feb 2024 08:58:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1707843537; x=1708448337; darn=sourceware.org; h=cc: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=CVCM/3yQOmOY94rA+5MPWraRPT/APmT6Nkrfcz7BQ1Y=; b=Mjq90O8AfyYd7jYGAgiBOG1ffDGjIbTJKsWJ3ovDNUCvIV2f7bf4tDVGuCadK5AXVx 4yAgUvU6Oi217FVp0yjuNPvhHFhyRSt0jMllLpAN0f2YUJCLiJvqohXO10tKr/13J69z vrts//GRpAlapNnSz+729X8SNkfEXu5pHcB3WQJOQICSmB2uGvL6Him57aaW3TPsxWkL /bgSRT++FlTHZt+0YagsGx5/+HJZ7svzWUV1/AAZd0dLkpK0MVWhPF4nugQPD9crkByR kWVub25mulM6hVkuKph5M3tD7wOLi6xZ4TGazpf8bIz30O6IC4wVBMUD50Uh3bzcVAQG 5bxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707843537; x=1708448337; h=cc: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=CVCM/3yQOmOY94rA+5MPWraRPT/APmT6Nkrfcz7BQ1Y=; b=rniJVNaaqD+bPnMiy9KfXduj2Dm0A/xYy3ZeQtEqqE2iP9JrFjKpbcb7D3cxMOY2NN jxRXIhV/R9sUZ8SD9QZWKT2mLIkhlr7f98J6wJN0cM6Sa2aXIDfA2fjULsx1q5gQhc5b Ll3LbfIciqL4rzZY0xegFNyd3P3rTu3A2fGKF/v6a36MWi7Gnf9doY0Iji/wrCgayitj 5au4GOwMo/sHhEKMEYvV3T8EKpCWnQ01bDFmDD7OacZP9somxmkSoYIeZt7LtaYamG55 CPdYoSzBfUoRmnrWRHhJ+qBvw05Bi3gj3xfRfMA1c98uUC+AhAJUmphxQnwEBUJZW2nn FZKw== X-Gm-Message-State: AOJu0YzRg0Ca3m6kMRxcrzxOEkd3YhPLWPYnsAt5pQxwH2Op8RSkupem 7nmwAbPVLAUwmobG/UMp09AyyVUDUD9erAwz0CK6Qjhyp8vufrpJvA81by2w9gZek54kFB3s9v4 = X-Google-Smtp-Source: AGHT+IFhumvzG5ChwyCgPhWtthSL+IDe3K2/muCzVRZhSooT7LqDezqqM73IcM2eItf/aLe9MI9FTg== X-Received: by 2002:a05:6e02:cd3:b0:363:e8f4:eb4f with SMTP id c19-20020a056e020cd300b00363e8f4eb4fmr227485ilj.3.1707843537611; Tue, 13 Feb 2024 08:58:57 -0800 (PST) Received: from localhost.localdomain (71-211-170-195.hlrn.qwest.net. [71.211.170.195]) by smtp.gmail.com with ESMTPSA id bn29-20020a056e02339d00b00363c664cfeasm2622692ilb.61.2024.02.13.08.58.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 08:58:57 -0800 (PST) From: Tom Tromey Date: Tue, 13 Feb 2024 09:58:53 -0700 Subject: [PATCH v2 1/2] Export "finish" return value to Python MIME-Version: 1.0 Message-Id: <20240213-dap-finish-v2-1-d507b889d735@adacore.com> References: <20240213-dap-finish-v2-0-d507b889d735@adacore.com> In-Reply-To: <20240213-dap-finish-v2-0-d507b889d735@adacore.com> To: gdb-patches@sourceware.org Cc: Eli Zaretskii X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.0 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