From patchwork Tue Jan 9 17:32:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83672 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 7B0CF3858029 for ; Tue, 9 Jan 2024 17:33:17 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 654203858C74 for ; Tue, 9 Jan 2024 17:32:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 654203858C74 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 654203858C74 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=1704821576; cv=none; b=AzOu0t7hJZzCyyXCqgRgLrAkql3VsB+D8xgPvVpSjpvSyEDrf61CvTvOtGlMnGMvygmC/Nxgk1e5dp5dX03SGVk/Ip0Lr4KGXnoo2RERY9qcQxKwtnvHFa/SteC/Ff+6lJmVTRLSYsYpzMRkGNEBht+X84AdasVvUwHnQAnfErU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704821576; c=relaxed/simple; bh=fbz3l9RAxgwaN3SEVOI4Z/cMNwIiN5cvaU2zAaqr6iI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=N6r5eYyZ6NeBjGDbNRXpA7+tFt0/vFdboShMUIG8HEUm6CPoj2UMfKgdPtzRzYGRSnSrj3rzLfFfS31AXL6qL4pSIuqbklQ3TVp5cnpIC+OZIYYLO7Q5f9ZIrOXgrxKdcbsJ7aeQAyq4NZIvIzZC3iXJ5x5b0xm45aDLiZQJYh4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704821571; 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; bh=1MQyXewfc1+dK5tDEyhdMcf3t0Q2B/EZimujrdf8IMU=; b=ipMz2htXYVkoJEuPYzKa6s8zQ5lBEoRACWIeD0t7GRBKudpidTN5YGCCX1UKzrAnwj8yRM YY0sX205P2Tbu3yj87glaI719VvuDB2ppwGnHl4Ig/xX9MVzA0eOeMROVQigHaYVGbbin+ lReZcG+22dfXUdthMLkN6eH06GH0O74= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-gMXPAAboMve8bHz24J63dg-1; Tue, 09 Jan 2024 12:32:50 -0500 X-MC-Unique: gMXPAAboMve8bHz24J63dg-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-3377a359fdaso102699f8f.2 for ; Tue, 09 Jan 2024 09:32:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704821568; x=1705426368; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1MQyXewfc1+dK5tDEyhdMcf3t0Q2B/EZimujrdf8IMU=; b=OmFWK4pMYA6vWBDYefeOKNd/tH2k9tYVRyKoynzKAe+I/gW7NujFzShRZGwCDdy3cf t/ZMgdziwrkJuzDTCgfwU5YMijpexNWbXwyhUAPoAYdpFpXmyaOUDUTM1cdCVrq+AyLC eiO0LT/HkB9x5yrCltcZpf4KpLERBppjyNNLmSys/r7k+ErtEzekDso4SFoK8hrhamr8 29AerEhMYtPknhiA/rsLlxD8elyKKOZLcRQbX+NBVQuDrBfOYp5CgaVY0KrV5ifWs1h1 G6I92ht5QkTB30n8xTRzeS6mqgFQBzUXq6WT/NWuEr5v8vk7b7nByH63Rl8MdMWMZoxx 6Fjw== X-Gm-Message-State: AOJu0YyvFhkIxfsBEQ2XzhoudswtE6m98cJcWUJnlHrBu1OBKgV82CEF 73Pu0mhMRB3vrEfV89CL1LRuh5U2lWfiP+S1rdStyVZIT0MOcOfBVICFTlhMjLiQZnY0jx6gjZB M1QybgjiBEhEpzLOSHs8gGfuNp5tTzYIonypNdQCFpnbbIS7l4aC81vm3Wk1fLtKY4n+8gT3+X4 Gvq8jSc3xGq2WBLg== X-Received: by 2002:adf:fb88:0:b0:336:619f:89c4 with SMTP id a8-20020adffb88000000b00336619f89c4mr748497wrr.127.1704821568648; Tue, 09 Jan 2024 09:32:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IHkxZU8yZgGYtbjDF70svDSTlvkDXAQkQUZWaiHWaClNSJ5FZPUKMXRUq8IpW16YHOFRHD3xA== X-Received: by 2002:adf:fb88:0:b0:336:619f:89c4 with SMTP id a8-20020adffb88000000b00336619f89c4mr748491wrr.127.1704821568287; Tue, 09 Jan 2024 09:32:48 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id l10-20020adfa38a000000b0033668ac65ddsm2936164wrb.25.2024.01.09.09.32.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 09:32:48 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb/python: New InferiorThread.ptid_string attribute Date: Tue, 9 Jan 2024 17:32:44 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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+patchwork=sourceware.org@sourceware.org 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 --- gdb/NEWS | 4 ++++ gdb/doc/python.texi | 7 +++++++ gdb/python/py-infthread.c | 20 ++++++++++++++++++++ gdb/testsuite/gdb.python/py-infthread.exp | 8 ++++++++ 4 files changed, 39 insertions(+) base-commit: b7a5722ebdd24a0d15d56e96d30a649ea1d7b0ee diff --git a/gdb/NEWS b/gdb/NEWS index 11cd6c0663e..60e58daa501 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -87,6 +87,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..7307b9e9da9 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -185,6 +185,23 @@ 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; + + /* 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 ()); +} + /* Getter for InferiorThread.inferior -> Inferior. */ static PyObject * @@ -388,6 +405,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"