From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id En7qAkEJoWVCbzkAWB0awg (envelope-from ) for ; Fri, 12 Jan 2024 04:41:21 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KdwSMx5p; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id ECE061E0C2; Fri, 12 Jan 2024 04:41:20 -0500 (EST) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 D8C531E092 for ; Fri, 12 Jan 2024 04:41:18 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD1E63857C4E for ; Fri, 12 Jan 2024 09:41:17 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E034838582AD for ; Fri, 12 Jan 2024 09:39:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E034838582AD Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E034838582AD Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705052369; cv=none; b=D2MDzqggXSZgTimMORm/JPO+ju7YVFAccCEOiOrEEQJrNL5CNoW33IMAGaO919YvMaMtGa1C1bRHxX9bJHORFGCEVaeGl5LGIB5UVhx/sH+G+M/ImD7PvJCc5nrfb4OOAAa8mKc2PyFEd8K7OM4f3puZg9lAJrWNch9o1VBjUyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705052369; c=relaxed/simple; bh=Yd1j3gHmu4dVsGYW80w0ST0O4pM6j3rOrKWhFRw+Iw4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fRjc7m/sclNQUkzrEvT/n8osvgyk8QqxqV4wA9RfIehB5fpLhWoe7YOy7AeT012UXewfikLkNP055333rC7gyd33bxLKUkfGxmjJmnxViiBvDLEHifXWZjcqHP2AKqW+yLdsTA0UdfxBXc7DLwZ9RYp4SUN7efP358G/F6TR/Bs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705052364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QckZq/xzkkh0CaL0P+PYIqE3CwxVs60nueCcS0l9KBk=; b=KdwSMx5pkHAs09aEmk1LugPoCdmQOjJDNV1Wmd9YRbogqXPueM45xZROQnMfZ8ZkpzA2jg fwpFkMKjO3sJhtn9OdTu1lXyE+x1uaKqkru3Rgm7IVIG67uW8VgeDWUAYceTTD01iJGO00 67h83krSeiRnpcyITdkLcY/U31LxAwU= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-217-oqkipoJtMUmIxz9U13NbLg-1; Fri, 12 Jan 2024 04:39:23 -0500 X-MC-Unique: oqkipoJtMUmIxz9U13NbLg-1 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-555f8e831f2so2668871a12.1 for ; Fri, 12 Jan 2024 01:39:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705052361; x=1705657161; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QckZq/xzkkh0CaL0P+PYIqE3CwxVs60nueCcS0l9KBk=; b=Y9r+Wx0ctKM5OJeNdxocsklRDdlJQlhqipQJaJhlIHX+Olry3VzO+y9fnaUrUdBEaB 6i7i9OcWmO8dXN/yclWZbR679DrYqfbn1s3caLeQuvjPP1egvO4ZsfeMQKnA18ROzjbJ wef8NEpMa124XVKT9fRjiFc2U7NWIF1RAiR9g9bi2BBf7Q1q7v9wIODAkkrtuNHnO+eb KQmHXxyGVD5/qYItrM9NfRC1l62JT2rufGIJpUaoyRnyFDlSFPhuE7qKGF4HQGq0Fi7T edevPBkw/okyOw4DLLH+yAxTGz+07psONk2JSAOs+nFsPL6oozHfyONwanZH886uZ3Kt Ls+Q== X-Gm-Message-State: AOJu0YzA1SZKMBNOKyLW3C6pmkamNKAQb96izV9Vd2gjxsKY/4XLeecN TUNhX8uD3liBfjh5SJHBP1BRaBU8IylXCF/SmzgAS+1Ef9SbY+kY3dI97cf35Jx3EDRHwRGqwOD yQebzUrXcAS6qdZTtQzI8O3HRkdgG7dmdm9QSWn4WNbgNb+NNolHmhPcrPlp4E8pgQ0yL81qg0G G4V7JOV2ehewK0Sg== X-Received: by 2002:a05:6402:296:b0:558:d442:5f with SMTP id l22-20020a056402029600b00558d442005fmr296601edv.40.1705052361183; Fri, 12 Jan 2024 01:39:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFu31Y6PvJOP6ki1g1INbFm+NnvDiiY/miY26KqOx3v0RVQUxm7YXciKbPGmasAwt3zG+nyFQ== X-Received: by 2002:a05:6402:296:b0:558:d442:5f with SMTP id l22-20020a056402029600b00558d442005fmr296593edv.40.1705052360792; Fri, 12 Jan 2024 01:39:20 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id da22-20020a056402177600b00556a9b663c8sm1607847edb.36.2024.01.12.01.39.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 01:39:20 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii , Tom Tromey Subject: [PUSHED] gdb/python: New InferiorThread.ptid_string attribute Date: Fri, 12 Jan 2024 09:39:16 +0000 Message-Id: <76118e1675f5eaf3fc44524ec435981705572629.1705052230.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <87wmsitjad.fsf@tromey.com> References: <87wmsitjad.fsf@tromey.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+public-inbox=simark.ca@sourceware.org I realised that I've been traiding emails between myself and Tom fixing the last few nits in this patch, and somewhere along the line I managed to drop gdb-patches from the CC list. Here's the version I've now pushed. It's not massively different from the original. Thanks, Andrew --- This commit adds a new InferiorThread.ptid_string attribute. This read-only attribute contains the string returned by target_pid_to_str, which actually converts a ptid (not pid) to a string. This is the string that appears (at least in part) in the output of 'info threads' in the 'Target Id' column, but also in the thread exited message that GDB prints. Having access to this string from Python is useful for allowing extensions identify threads in a similar way to how GDB core would identify the thread. Reviewed-By: Eli Zaretskii Approved-By: Tom Tromey --- gdb/NEWS | 4 ++++ gdb/doc/python.texi | 7 ++++++ gdb/python/py-infthread.c | 27 +++++++++++++++++++++++ gdb/testsuite/gdb.python/py-infthread.exp | 8 +++++++ 4 files changed, 46 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index 96a27be0fb2..76bc131e9c9 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -91,6 +91,10 @@ show remote thread-options-packet ** New function gdb.interrupt(), that interrupts GDB as if the user typed control-c. + ** New gdb.InferiorThread.ptid_string attribute. This read-only + attribute contains the string that appears in the 'Target Id' + column of the 'info threads' command output. + * Debugger Adapter Protocol changes ** GDB now emits the "process" event. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index d74defeec0c..da37348d663 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4084,6 +4084,13 @@ does not use that identifier. @end defvar +@defvar InferiorThread.ptid_string +This read-only attribute contains a string representing +@code{InferiorThread.ptid}. This is the string that @value{GDBN} uses +in the @samp{Target Id} column in the @kbd{info threads} output +(@pxref{info_threads,,@samp{info threads}}). +@end defvar + @defvar InferiorThread.inferior The inferior this thread belongs to. This attribute is represented as a @code{gdb.Inferior} object. This attribute is not writable. diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index 00d7171de64..632984d9ce9 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -185,6 +185,30 @@ thpy_get_ptid (PyObject *self, void *closure) return gdbpy_create_ptid_object (thread_obj->thread->ptid); } +/* Implement gdb.InferiorThread.ptid_string attribute. */ + +static PyObject * +thpy_get_ptid_string (PyObject *self, void *closure) +{ + thread_object *thread_obj = (thread_object *) self; + THPY_REQUIRE_VALID (thread_obj); + ptid_t ptid = thread_obj->thread->ptid; + + try + { + /* Select the correct inferior before calling a target_* function. */ + scoped_restore_current_thread restore_thread; + switch_to_inferior_no_thread (thread_obj->thread->inf); + std::string ptid_str = target_pid_to_str (ptid); + return PyUnicode_FromString (ptid_str.c_str ()); + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + return nullptr; + } +} + /* Getter for InferiorThread.inferior -> Inferior. */ static PyObject * @@ -388,6 +412,9 @@ static gdb_PyGetSetDef thread_object_getset[] = "Global number of the thread, as assigned by GDB.", NULL }, { "ptid", thpy_get_ptid, NULL, "ID of the thread, as assigned by the OS.", NULL }, + { "ptid_string", thpy_get_ptid_string, nullptr, + "A string representing ptid, as used by, for example, 'info threads'.", + nullptr }, { "inferior", thpy_get_inferior, NULL, "The Inferior object this thread belongs to.", NULL }, diff --git a/gdb/testsuite/gdb.python/py-infthread.exp b/gdb/testsuite/gdb.python/py-infthread.exp index 0b10ce9ff77..3b07d84143f 100644 --- a/gdb/testsuite/gdb.python/py-infthread.exp +++ b/gdb/testsuite/gdb.python/py-infthread.exp @@ -62,6 +62,14 @@ gdb_test "python print ('result = %s' % t0.num)" " = 1" "test InferiorThread.num gdb_test "python print ('result = %s' % t0.global_num)" " = 1" "test InferiorThread.global_num" gdb_test "python print ('result = %s' % str (t0.ptid))" " = \\(\[0-9\]+, \[0-9\]+, \[0-9\]+\\)" "test InferiorThread.ptid" +# Test the InferiorThread.ptid_string attribute. We don't test the +# actual string contents as they vary based on target, but we check +# that we get back a non-empty string. +gdb_test "python print(type(t0.ptid_string))" "" \ + "check that InferiorThread.ptid_string is a string" +gdb_test "python print(t0.ptid_string)" ".+" \ + "check that InferiorThread.ptid_string is non-empty" + gdb_py_test_silent_cmd "python i0 = t0.inferior" "test InferiorThread.inferior" 1 gdb_test "python print ('result = %s' % i0.num)" " = 1" "test Inferior.num" base-commit: 322ffd247e4e95a0d5a1b11ad1ef91f3378e6174 -- 2.25.4