From patchwork Wed Jan 10 15:54:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83760 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 E9314386100B for ; Wed, 10 Jan 2024 15:56:47 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 579393857C4C for ; Wed, 10 Jan 2024 15:55:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 579393857C4C 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 579393857C4C 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=1704902105; cv=none; b=mEt0+PwPSUFCPrUSMHDzV4hIqDzsujWsFA+Fbys6LoBJ0wR9mczSWi1hPHV4vpm3fjGgsFxxq0pSqIMZErGUasuVaPAQWSYJ8KZa6LPW6TEhwrbowlx9Cg3S5QtZDuD1/v0CsufVwLfnbOe5Fk0u6f+ibnsQRM7xB6YEpYguZHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902105; c=relaxed/simple; bh=UzhwPREI9SsonURE9lHlX4Voz78+4q3uQzHdbQ4YscM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Lk0WyO3VfewroIAUYnRFXS9Mbe65QCwH3fRZTWvx0Cfvm9OQlE/zhE9I1eaLVdlMdxnBuHyaerRRSdJ2IzawF68otlAFh4Z/5GHgGqCBMhQOegdj/UAfOq5z5qT1crP925gRl7Wqji4Ph/z/OtH00cq6WvA4Rm844+v1bcb/Ysk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902103; 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=MYiRThvoVxp6tNO3OtuEJWx45kUOFq6I9ZjXt9nBTAI=; b=RutFhG+kD81DBMa9XJmgvKLsFUj7mSzbvYniRy2jPTnvt0rmetBTAeqDh1H8jbS9bYLjAb US5G0y7jvzfp5KQvg/7pPOO/w5PEIzsv8uMq7gAdqbzZMf9V7CSz/wuFEuRtwCh/Y5YguZ p++h7QK9mkK+bozrFBLP1T1TvQtKnD8= 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-258-4XRCtijLNAOF71YejRRZow-1; Wed, 10 Jan 2024 10:55:01 -0500 X-MC-Unique: 4XRCtijLNAOF71YejRRZow-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40e5317c7a5so8604685e9.0 for ; Wed, 10 Jan 2024 07:55:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902100; x=1705506900; 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=MYiRThvoVxp6tNO3OtuEJWx45kUOFq6I9ZjXt9nBTAI=; b=PESl1TeR4e9lbATv2aMgP3ZhjcmF/oW5lLdUz4/+Awg014N38w+PW1hRZjuLRs63FO kfXGzQ4a5YTvhzZss07knI5F5xI3O0Vyb3u2YTSmHEnJJd6LYEDb61KJNSwTFKR1ibju wRbnm02fbgNu0kqyl7uHxz+fGuY5xLHSCQ1GnieM2yLOXbvJsw/B3oHjaJ/6Y4LSTOlW 03+mdboOUWKpQIPq/RYyOOaLJILQ/vanQe9yUbgN4stYUjX6wcGYbFQQ8jCbHBxEJgLZ 9My5ng3KCl22cl8LSpOGpEVXYIXo4c6p8cFc0bW8B58K+NGIj4PuCHc1WhcGNAuVCLgh JmUA== X-Gm-Message-State: AOJu0Yzm1HwSCgcz6HsN/LuA8tLDJftkc5cEMPnOFityPv8u387TC5Nk fTivml0WbMZ417aZvTYMVt/L5l2IIireTEEgcJTCYDZc4RTSXYgHT69YL0ui/GhNVjecq0c06yX VNfqrEZXALePiQs8RggWYTtNjaLNzb4aIzvFqaT/VSbvZyJkar8jA3dlZ6ik7CmnibdHOqO4ZTT tq1JwsgOKyJdgY+w== X-Received: by 2002:a05:600c:982:b0:40e:48e4:bf85 with SMTP id w2-20020a05600c098200b0040e48e4bf85mr730781wmp.48.1704902099909; Wed, 10 Jan 2024 07:54:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHV3KxBzOU+L5PPYsOerpLJQQb/SqVGWoYHqupGsyOP68sd38N60Qw6P41paxi1ud3evhOt5A== X-Received: by 2002:a05:600c:982:b0:40e:48e4:bf85 with SMTP id w2-20020a05600c098200b0040e48e4bf85mr730773wmp.48.1704902099597; Wed, 10 Jan 2024 07:54:59 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id i8-20020a05600c354800b0040e482b1925sm2614942wmq.30.2024.01.10.07.54.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:58 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv2 6/8] gdb/python: Add gdb.InferiorThread.__dict__ attribute Date: Wed, 10 Jan 2024 15:54:43 +0000 Message-Id: <6c237c5f72197b63c723d91123ae11ef9e9ffed9.1704901918.git.aburgess@redhat.com> 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 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 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 The gdb.Objfile, gdb.Progspace, gdb.Type, and gdb.Inferior Python types already have a __dict__ attribute, which allows users to create user defined attributes within the objects. This is useful if the user wants to cache information within an object. This commit adds the same functionality to the gdb.InferiorThread type. After this commit there is a new gdb.InferiorThread.__dict__ attribute, which is a dictionary. A user can, for example, do this: (gdb) pi >>> t = gdb.selected_thread() >>> t._user_attribute = 123 >>> t._user_attribute 123 >>> There's a new test included. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 4 +++ gdb/doc/python.texi | 35 ++++++++++++++++++++++++ gdb/python/py-infthread.c | 15 ++++++++-- gdb/python/python-internal.h | 4 +++ gdb/testsuite/gdb.python/py-inferior.exp | 17 ++++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 500d5ab7160..c4862a8beb6 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -96,6 +96,10 @@ show remote thread-options-packet these will be stored in the object's new Inferior.__dict__ attribute. + ** User defined attributes can be added to a gdb.InferiorThread + object, these will be stored in the object's new + InferiorThread.__dict__ attribute. + * Debugger Adapter Protocol changes ** GDB now emits the "process" event. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 0e6eeeeb15d..2eed332fe59 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4177,6 +4177,41 @@ a @code{gdb.Type} for the handle type. @end defun +One may add arbitrary attributes to @code{gdb.InferiorThread} objects +in the usual Python way. This is useful if, for example, one needs to +do some extra record keeping associated with the thread. + +In this contrived example we record the time when a thread last +stopped: + +@smallexample +@group +(@value{GDBP}) python +import datetime + +def thread_stopped(event): + if event.inferior_thread is not None: + thread = event.inferior_thread + else: + thread = gdb.selected_thread() + thread._last_stop_time = datetime.datetime.today() + +gdb.events.stop.connect(thread_stopped) +@end group +@group +(@value{GDBP}) file /tmp/hello +Reading symbols from /tmp/hello... +(@value{GDBP}) start +Temporary breakpoint 1 at 0x401198: file /tmp/hello.c, line 18. +Starting program: /tmp/hello + +Temporary breakpoint 1, main () at /tmp/hello.c:18 +18 printf ("Hello World\n"); +(@value{GDBP}) python print(gdb.selected_thread()._last_stop_time) +2024-01-04 14:48:41.347036 +@end group +@end smallexample + @node Recordings In Python @subsubsection Recordings In Python @cindex recordings in python diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index 2d892b10b69..d1fb5bd4160 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -51,6 +51,9 @@ create_thread_object (struct thread_info *tp) thread_obj->thread = tp; thread_obj->inf_obj = (PyObject *) inf_obj.release (); + thread_obj->dict = PyDict_New (); + if (thread_obj->dict == nullptr) + return nullptr; return thread_obj; } @@ -58,7 +61,13 @@ create_thread_object (struct thread_info *tp) static void thpy_dealloc (PyObject *self) { - Py_DECREF (((thread_object *) self)->inf_obj); + thread_object *thr_obj = (thread_object *) self; + + gdb_assert (thr_obj->inf_obj != nullptr); + + Py_DECREF (thr_obj->inf_obj); + Py_XDECREF (thr_obj->dict); + Py_TYPE (self)->tp_free (self); } @@ -394,6 +403,8 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread); static gdb_PyGetSetDef thread_object_getset[] = { + { "__dict__", gdb_py_generic_dict, nullptr, + "The __dict__ for this thread.", &thread_object_type }, { "name", thpy_get_name, thpy_set_name, "The name of the thread, as set by the user or the OS.", NULL }, { "details", thpy_get_details, NULL, @@ -471,7 +482,7 @@ PyTypeObject thread_object_type = 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ + offsetof (thread_object, dict), /* tp_dictoffset */ 0, /* tp_init */ 0 /* tp_alloc */ }; diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 8ff9af650c2..e01557edeb7 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -356,6 +356,10 @@ struct thread_object /* The Inferior object to which this thread belongs. */ PyObject *inf_obj; + + /* Dictionary holding user-added attributes. This is the __dict__ + attribute of the object. */ + PyObject *dict; }; struct inferior_object; diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 0e00636fa1c..d1cd29f734b 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -107,6 +107,19 @@ gdb_test "python print(last_thread)" \ "" \ "test repr of a valid thread" +# Add a user defined attribute to this thread, check the attribute can +# be read back, and check the attribute is not present on other +# threads. +gdb_test_no_output "python last_thread._user_attribute = 123" \ + "add user defined attribute to InferiorThread object" +gdb_test "python print(last_thread._user_attribute)" "123" \ + "read back user defined attribute" +gdb_test "python print(i0.threads ()\[0\]._user_attribute)" \ + [multi_line \ + "AttributeError: 'gdb\\.InferiorThread' object has no attribute '_user_attribute'" \ + "Error while executing Python code\\."] \ + "attempt to read non-existent user defined attribute" + # Proceed to the next test. gdb_breakpoint [gdb_get_line_number "Break here."] @@ -117,6 +130,10 @@ gdb_test "python print(last_thread)" \ "" \ "test repr of an invalid thread" +# Check the user defined attribute is still present on the invalid thread object. +gdb_test "python print(last_thread._user_attribute)" "123" \ + "check user defined attribute on an invalid InferiorThread object" + # Test memory read and write operations. gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \