From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by sourceware.org (Postfix) with ESMTPS id 128893858D32 for ; Tue, 14 Nov 2023 18:44:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 128893858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 128893858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699987449; cv=none; b=b36lTfuwq6dixGMXdrncYL8eTYUGOHLa5iSkdGUJrVS3bbhqIpoDudV1i1/vBv8IBcZszWG8x9B7E5UL1w8A7RsjvVx0cd7i2XHwmtAwIr8MLS29n95BydFB+PxleQ36DIDdTllnyGJ7K64ngX2/DYQqIXwL+82bGzw+AmZZfUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699987449; c=relaxed/simple; bh=m0gQyHRgHsn4kc3Fz2TT28rYoAckEwW6bG39gSxPt2I=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=ivK3WCKGnnjKGc4NwEn6/+mJhbegxUrsJikW5T3YbvVt2jRbcAAkVSVcemswOa0UHSOOQqjWeOyvG6IHHajOt7AL5eBIeMwI8cTtAhXli7X/CnTbYuBTnSCxlJb9VI5E11FO+RvLRkYpNx+ZGiMOblOodjz9TyGl7K789cKYZgg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2d.google.com with SMTP id ca18e2360f4ac-7a98fdeaac7so253483639f.1 for ; Tue, 14 Nov 2023 10:44:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1699987446; x=1700592246; 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=CpXqfyNxIcNrNief+VePvnpBCufu4X6DbrcQYCx/FwA=; b=ByVUBeACwG6xR1C0bwJl0eMxhY7EiR9YAVu3mXtOpXWaTSKlUgQ1oR9nDUTdQH1y90 bNvyOKbyICUXWiwjtExVtFvrf3AEuZAO8oruvXwLnykRVrUkxW9KaTAcwE+fSII60v8r Ks1+ophJoeZKZMCjJZ9jWaaE8v0QQEfBaDD0RlXUva25/2I6KFZ0E3QLqs3BvfDxRX/e dtvbK+GXrVeAwJLG1cV76g/ObWSwzXYZjNT9aocpq30N/vCpmNRIuG4ljJGJd0EIgkJ7 JSvt0bU+SDX4yUmhyF4QqJvECKLx9iBgILsVoND3OrJQWnQ/QE3iT8J+8TB21F6KfMUO y1CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699987446; x=1700592246; 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=CpXqfyNxIcNrNief+VePvnpBCufu4X6DbrcQYCx/FwA=; b=AGtB/JJtWLq0wen+JP5XxA2ctE5WYxt5KRFFUoXkh6kn0Pf+SdmRyP+A0LEFwRElD3 5ZdjwE8+9lHirHv41FSJLnVKFm8omLxHseTLi+RZbQ+zSf9XDbl+mROiHEZpmdc1IPAM SlylO/zP9Z65LursNoOz0ZZK9gJ9KkDEaXeWBPUoVn5iyrGO2uPshmnOujWRXs8kmO4B Jju8FrIt4Pw79Dx8r1SpvgQrvNmktTeqoLW7gd6QptW+XRYEJtvP5B20W6jOFTSjFdSG cTpvBq8A0ZJXMJuLSJk5+hSx+jiTSeV5Fru0qoeJ5ch/4s8M7ti+lwyx7riWxl2XcZVO O/hw== X-Gm-Message-State: AOJu0YyqnEgrjZrQhuxHPDBW/ci8LBxSJn+1Eq3nrGnBytza/6aqAxhQ 8hW5acZwFUv9seWq0kuik2eDjtdtD/uXCUmBKikRIw== X-Google-Smtp-Source: AGHT+IFvFJzz602VBYNiL79xTAvtJpiuk+qgDAUl9BLs0OdYcGAibSGi4iWEo+trwz3bnJPrQ0tgvw== X-Received: by 2002:a92:c24f:0:b0:358:ffc:e7ab with SMTP id k15-20020a92c24f000000b003580ffce7abmr15422698ilo.19.1699987446228; Tue, 14 Nov 2023 10:44:06 -0800 (PST) Received: from localhost.localdomain (97-122-77-73.hlrn.qwest.net. [97.122.77.73]) by smtp.gmail.com with ESMTPSA id e97-20020a02866a000000b0043194542229sm528513jai.52.2023.11.14.10.44.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 10:44:05 -0800 (PST) From: Tom Tromey Date: Tue, 14 Nov 2023 11:44:04 -0700 Subject: [PATCH 2/3] Emit stop reason details in Python stop events MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231114-py-stop-reason-v1-2-4ff440c956a9@adacore.com> References: <20231114-py-stop-reason-v1-0-4ff440c956a9@adacore.com> In-Reply-To: <20231114-py-stop-reason-v1-0-4ff440c956a9@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.8 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 List-Id: This changes Python stop events to carry a "details" dictionary, that holds any relevant information about the stop. The details are constructed using more or less the same procedure as is done for MI. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=13587 --- gdb/NEWS | 4 +++ gdb/doc/python.texi | 11 ++++++++ gdb/python/py-bpevent.c | 5 ++-- gdb/python/py-mi.c | 2 +- gdb/python/py-signalevent.c | 5 ++-- gdb/python/py-stopevent.c | 65 +++++++++++++++++++++++++++++++++++++++++---- gdb/python/py-stopevent.h | 9 ++++--- gdb/python/py-uiout.h | 4 +-- 8 files changed, 90 insertions(+), 15 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 93fbcf1c13e..12387ad9f4a 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -50,6 +50,10 @@ disable missing-debug-handler LOCUS HANDLER sub-classed to create handlers for objfiles with missing debug information. + ** Stop events now have a "details" attribute that holds a + dictionary that carries the same information as an MI "*stopped" + event. + * New commands maintenance info linux-lwps diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index b65991bbad0..ab2abcfc5b1 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3750,6 +3750,17 @@ registry extend @code{gdb.StopEvent}. As a child of thread when @value{GDBN} is running in non-stop mode. Refer to @code{gdb.ThreadEvent} above for more details. +@code{gdb.StopEvent} has the following additional attributes: + +@defvar StopEvent.details +A dictionary holding any details relevant to the stop. The exact keys +and values depend on the type of stop, but are identical to the +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. +@end defvar + Emits @code{gdb.SignalEvent}, which extends @code{gdb.StopEvent}. This event indicates that the inferior or one of its threads has diff --git a/gdb/python/py-bpevent.c b/gdb/python/py-bpevent.c index 00fb625794b..b640ec9ddd9 100644 --- a/gdb/python/py-bpevent.c +++ b/gdb/python/py-bpevent.c @@ -24,10 +24,11 @@ references to BREAKPOINT_LIST and FIRST_BP. */ gdbpy_ref<> -create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp) +create_breakpoint_event_object (const gdbpy_ref<> &dict, + PyObject *breakpoint_list, PyObject *first_bp) { gdbpy_ref<> breakpoint_event_obj - = create_stop_event_object (&breakpoint_event_object_type); + = create_stop_event_object (&breakpoint_event_object_type, dict); if (breakpoint_event_obj == NULL) return NULL; diff --git a/gdb/python/py-mi.c b/gdb/python/py-mi.c index 2b265ad80d6..04873e86dd3 100644 --- a/gdb/python/py-mi.c +++ b/gdb/python/py-mi.c @@ -173,7 +173,7 @@ gdbpy_execute_mi_command (PyObject *self, PyObject *args, PyObject *kw) return nullptr; } - return uiout.result (); + return uiout.result ().release (); } /* Convert KEY_OBJ into a string that can be used as a field name in MI diff --git a/gdb/python/py-signalevent.c b/gdb/python/py-signalevent.c index 93d06706542..edbd9b0a7ec 100644 --- a/gdb/python/py-signalevent.c +++ b/gdb/python/py-signalevent.c @@ -21,10 +21,11 @@ #include "py-stopevent.h" gdbpy_ref<> -create_signal_event_object (enum gdb_signal stop_signal) +create_signal_event_object (const gdbpy_ref<> &dict, + enum gdb_signal stop_signal) { gdbpy_ref<> signal_event_obj - = create_stop_event_object (&signal_event_object_type); + = create_stop_event_object (&signal_event_object_type, dict); if (signal_event_obj == NULL) return NULL; diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c index 0aa9d5381f8..f395e8c042e 100644 --- a/gdb/python/py-stopevent.c +++ b/gdb/python/py-stopevent.c @@ -19,12 +19,61 @@ #include "defs.h" #include "py-stopevent.h" +#include "py-uiout.h" gdbpy_ref<> -create_stop_event_object (PyTypeObject *py_type) +create_stop_event_object (PyTypeObject *py_type, const gdbpy_ref<> &dict) { gdbpy_ref<> thread = py_get_event_thread (inferior_ptid); - return create_thread_event_object (py_type, thread.get ()); + if (thread == nullptr) + return nullptr; + + gdbpy_ref<> result = create_thread_event_object (py_type, thread.get ()); + if (result == nullptr) + return nullptr; + + if (evpy_add_attribute (result.get (), "details", dict.get ()) < 0) + return nullptr; + + return result; +} + +/* Print BPSTAT to a new Python dictionary. Returns the dictionary, + or null if a Python exception occurred. */ + +static gdbpy_ref<> +py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) +{ + py_ui_out uiout; + + try + { + scoped_restore save_uiout = make_scoped_restore (¤t_uiout, &uiout); + + thread_info *tp = inferior_thread (); + if (tp->thread_fsm () != nullptr && tp->thread_fsm ()->finished_p ()) + { + async_reply_reason reason = tp->thread_fsm ()->async_reply_reason (); + uiout.field_string ("reason", async_reason_lookup (reason)); + } + + if (stop_signal != GDB_SIGNAL_0 && stop_signal != GDB_SIGNAL_TRAP) + print_signal_received_reason (&uiout, stop_signal); + else + { + struct target_waitstatus last; + get_last_target_status (nullptr, nullptr, &last); + + bpstat_print (bs, last.kind ()); + } + } + catch (const gdb_exception &except) + { + gdbpy_convert_exception (except); + return nullptr; + } + + return uiout.result (); } /* Callback observers when a stop event occurs. This function will create a @@ -45,6 +94,10 @@ emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal) if (evregpy_no_listeners_p (gdb_py_events.stop)) return 0; + gdbpy_ref<> dict = py_print_bpstat (bs, stop_signal); + if (dict == nullptr) + return -1; + /* Add any breakpoint set at this location to the list. */ for (current_bs = bs; current_bs != NULL; current_bs = current_bs->next) { @@ -71,7 +124,8 @@ emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal) if (list != NULL) { - stop_event_obj = create_breakpoint_event_object (list.get (), + stop_event_obj = create_breakpoint_event_object (dict, + list.get (), first_bp); if (stop_event_obj == NULL) return -1; @@ -81,7 +135,7 @@ emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal) if (stop_signal != GDB_SIGNAL_0 && stop_signal != GDB_SIGNAL_TRAP) { - stop_event_obj = create_signal_event_object (stop_signal); + stop_event_obj = create_signal_event_object (dict, stop_signal); if (stop_event_obj == NULL) return -1; } @@ -90,7 +144,8 @@ emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal) be known and this should eventually be unused. */ if (stop_event_obj == NULL) { - stop_event_obj = create_stop_event_object (&stop_event_object_type); + stop_event_obj = create_stop_event_object (&stop_event_object_type, + dict); if (stop_event_obj == NULL) return -1; } diff --git a/gdb/python/py-stopevent.h b/gdb/python/py-stopevent.h index 649112f0488..92282c9c413 100644 --- a/gdb/python/py-stopevent.h +++ b/gdb/python/py-stopevent.h @@ -22,14 +22,17 @@ #include "py-event.h" -extern gdbpy_ref<> create_stop_event_object (PyTypeObject *py_type); +extern gdbpy_ref<> create_stop_event_object (PyTypeObject *py_type, + const gdbpy_ref<> &dict); extern int emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal); -extern gdbpy_ref<> create_breakpoint_event_object (PyObject *breakpoint_list, +extern gdbpy_ref<> create_breakpoint_event_object (const gdbpy_ref<> &dict, + PyObject *breakpoint_list, PyObject *first_bp); -extern gdbpy_ref<> create_signal_event_object (enum gdb_signal stop_signal); +extern gdbpy_ref<> create_signal_event_object (const gdbpy_ref<> &dict, + enum gdb_signal stop_signal); #endif /* PYTHON_PY_STOPEVENT_H */ diff --git a/gdb/python/py-uiout.h b/gdb/python/py-uiout.h index e9abf8ee5be..6e43b425968 100644 --- a/gdb/python/py-uiout.h +++ b/gdb/python/py-uiout.h @@ -46,14 +46,14 @@ class py_ui_out : public ui_out /* Return the Python object that was created. If a Python error occurred during the processing, set the Python error and return nullptr. */ - PyObject *result () + gdbpy_ref<> result () { if (m_error.has_value ()) { m_error->restore (); return nullptr; } - return current ().obj.release (); + return std::move (current ().obj); } protected: -- 2.41.0