From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5FC5A385C422 for ; Wed, 10 Jan 2024 15:54:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5FC5A385C422 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 5FC5A385C422 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902101; cv=none; b=cV14Wg5nPQhZlCv1j+yfolwzN9fBX4R5FTrbkV/M/CG6fgiLTT+AVU1asrQQYxZ4ATm2KCq8jt9oRFXEcXoJNMvHVb/qnubP297BYnWxrFmQI7QjJEmmY6TSuwG+Qa5c0EdVyItVyygLD+KkECwo/RETFg0vN7wqo16DTDXqQhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902101; c=relaxed/simple; bh=fbz3l9RAxgwaN3SEVOI4Z/cMNwIiN5cvaU2zAaqr6iI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ehH9phfht76loiudYTLiXdF3jKAdKsEqjwRxRm4iYvd1e8KR2JEWFZ/I8xVbsF/mv9EnMIhWAZp70DndzgltJg1zswM/h87+sgJBq+58ShZwKo2pWZJFseSuU8LiOpvDz8gAPstW64ZKsEsx6vlUj0QYPTXZKlFEdRWZa44UBMM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902097; 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=1MQyXewfc1+dK5tDEyhdMcf3t0Q2B/EZimujrdf8IMU=; b=ZH4IiOGWvkWA889/t6evX5Oyrc7/fw3222aREJpfg4HHmDlsGDqhN8pQebbZSQRcvvp2QN czdKSZAZDwBu6e2p4Jl5ZqUrD9sL4blokzvqixURVP8Qa63BVuhKWBRGavTz+bQ41tgBZY 2jfFfTckLpXtHi5uU1kaPronXNENUho= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-482-kDMj4rUDMUO6_7l1zKJY2Q-1; Wed, 10 Jan 2024 10:54:53 -0500 X-MC-Unique: kDMj4rUDMUO6_7l1zKJY2Q-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40e40126031so36333385e9.0 for ; Wed, 10 Jan 2024 07:54:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902092; x=1705506892; 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=1MQyXewfc1+dK5tDEyhdMcf3t0Q2B/EZimujrdf8IMU=; b=YKpR93RIA9+oWbfsbj/5MFmE0EUQZIJFAmhHZiswUx65zVT83gv5MGOfxEUIy5kaqM X1I0IiNnkTN9nXsQL7stO8Kmzi1PgxtjN49TTissnf2IpKwHMfDGOFLWtEPszhtHLOeX nZgEBB6Hq3dClKEtnI1ReU0a4OCRaWBdwj62gq8GhO6fp2lMKmjXolQu6E9MIJhOqQM8 byer2QrGuCHmBj1jaiYaxFcbbuzupROtlG8FrkmwwnRXz88s5Ao0Rx5kM7t/lYRx07tW bhdr4khXrnWK4abYVhDxCL3dJqvbHeKniRhM9VWED4rqM2/THIb5HdXE0oWrxOY2cCfH kWBQ== X-Gm-Message-State: AOJu0YwpC2FJiYUArlm3GdnBmYM/FaO5lng+0itjYeJC2gxswWAuMfgy X2k0bArDWODmN1HyBtGCaY8AMq+Lz3CJHIIVWoYHr4qOXdM+VwIGLtEP9HI4wromO/ackX3vzos vC3JtbR6+uUCjWTR3zI30CZDkwFyrgmG6eh4zSuohEhwhdKKHKRwFdMeLHHa1tD8jespWzd88zf xQ5Vj7qUoUVu2krg== X-Received: by 2002:a05:600c:500f:b0:40d:5c4c:a4ea with SMTP id n15-20020a05600c500f00b0040d5c4ca4eamr690104wmr.162.1704902091905; Wed, 10 Jan 2024 07:54:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IFAyr71mbUwe53d7hL3hzrctj9ZBk/7O4aoZ87Q9XFPjvudGiD0/cR8wyB5J8tPqn6vUkzJYA== X-Received: by 2002:a05:600c:500f:b0:40d:5c4c:a4ea with SMTP id n15-20020a05600c500f00b0040d5c4ca4eamr690099wmr.162.1704902091630; Wed, 10 Jan 2024 07:54:51 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id d7-20020adffd87000000b003374555d88esm5191289wrr.56.2024.01.10.07.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:51 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb/python: New InferiorThread.ptid_string attribute Date: Wed, 10 Jan 2024 15:54:38 +0000 Message-ID: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: 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.0 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_H3,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 List-Id: Message-ID: <20240110155438.45mxVODUrQSrBi5RsNx64XyjuEUzSJd9Bg4cCtu4CzM@z> 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. --- 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(+) 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" base-commit: b7a5722ebdd24a0d15d56e96d30a649ea1d7b0ee -- 2.25.4