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.129.124]) by sourceware.org (Postfix) with ESMTPS id C1DA4385DC15 for ; Wed, 10 Jan 2024 15:55:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C1DA4385DC15 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 C1DA4385DC15 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=1704902104; cv=none; b=oN7njq/rJGXoVKQE41JaZYhKG3vJ8jj/vcVitOCkNHew4FHG6ymkOf6H55Q7fE0fuv9SVBNrSs8MxP9u8lNgIHI3uHfetVc1urfPWIRqnVcKwye9Jyg4FPq8fclC2tdded7N6/01tsXOirb15Z8rPBXCJyUy1v+9lfgag2n8XMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902104; c=relaxed/simple; bh=p6nww1CWresnRqlV/ThCjrTCtDO2N6CUzrNSkX4QFl0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=IC8yy4sLmc4WlMUzxsoG0sq3zV/g9TlnjR4urDudgiPRxy8r63LVf0EVE6e8cYERJxx6yxilZYd+ZK7pviLdAG3PyxNBpM8I+VN0zmOgOi703FWLHybe1llkoA0U3E36ZYeJPbt11WonPe+qiiyvtduCHbXb42obMMZEvouXg0I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902101; 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=VN067i5TTPJRmHdDh4i5CC/7/EPZyrtbjNp/Yx3+xBM=; b=MUJTZnl1YAsDQCQd2/4NUL8gx7e2Y0nYEV2fI9QG4jmAwoRF3P6qcT12i2raP7lNHN7+y5 C7PifTXNtcbIDmiZbKs7tS4aWT/BsW9f14uaz088nrN4JZ3y9wgY/OODijbYouIAKhmBzQ VA3dp0XSZdIv3uqsup4QMenMaBjVePE= 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-589-XC_RWuegOLWdqtWehqBUMA-1; Wed, 10 Jan 2024 10:55:00 -0500 X-MC-Unique: XC_RWuegOLWdqtWehqBUMA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40d5d0de143so34367745e9.1 for ; Wed, 10 Jan 2024 07:55:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902098; x=1705506898; 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=VN067i5TTPJRmHdDh4i5CC/7/EPZyrtbjNp/Yx3+xBM=; b=FncEQjlRqnBSbr2PFUoAqdBzQF7sLYd1iSygYbbNC1+JuUhmnck6kAs5/+v5z+7VZ6 RHxlQ7uO7ZtMoSuKPUjGOo7DnzvtnX2QLTZ/uy6ohUeeiEuFjDEY76DD7BHQepUANfmH tmMkkhPRfmji0re6pSgS7MxyeY1aYaXsDRusLGb/YS9Ll4K6Zgw1ET/+w4mE1/5JIjoD zWbq9RuAlJn1WvrOZTvrjEAVY9Hk8cjvfDdGDQ7jKjTwA+3X2d3Xze+9XnPcj/x4p2ag ON6fvKN9MwKGXsOpK4bRSP/plodZwv7kywsLRKillodgVOZnJ4EUfA8ppJiA7PPKpNbZ AVcQ== X-Gm-Message-State: AOJu0YyG5fDYngT0akyeAL8xwgCQ5UInUFUZkLuLsN6F16caIqvkn8d+ U6m/b2kUoe98x0riPZrjRQmo6rXwiQEZX45O1oCdbR8MNIOZe9SUN9l7gFV2KvAUsJ091zq41YW qJwmUVdye6BePq6V+1/wDMsaH4opDTw/OR0e4FG2gGIP/Nm4/ZecfWpsY4w/HI1baR0/GHv0oMl kzXDuSVnDbfMuozA== X-Received: by 2002:a05:600c:4d12:b0:40d:aa43:c718 with SMTP id u18-20020a05600c4d1200b0040daa43c718mr448198wmp.28.1704902098588; Wed, 10 Jan 2024 07:54:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IHB4OXWtrPCPksaCI9cdWZ4ZsC78CHYhQWum82pDiemQBjKw7YdWRfbjKm/6cpUZya9SWhwZg== X-Received: by 2002:a05:600c:4d12:b0:40d:aa43:c718 with SMTP id u18-20020a05600c4d1200b0040daa43c718mr448191wmp.28.1704902098181; Wed, 10 Jan 2024 07:54:58 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id u13-20020a05600c19cd00b0040e34ca648bsm2634902wmq.0.2024.01.10.07.54.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:57 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv2 5/8] gdb/python: Add gdb.Inferior.__dict__ attribute Date: Wed, 10 Jan 2024 15:54:42 +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_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 List-Id: The gdb.Objfile, gdb.Progspace, and gdb.Type 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.Inferior type. After this commit there is a new gdb.Inferior.__dict__ attribute, which is a dictionary. A user can, for example, do this: (gdb) pi >>> i = gdb.selected_inferior() >>> i._user_attribute = 123 >>> i._user_attribute 123 >>> There's a new test included. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 4 +++ gdb/doc/python.texi | 36 ++++++++++++++++++++++++ gdb/python/py-inferior.c | 13 ++++++++- gdb/testsuite/gdb.python/py-inferior.exp | 19 +++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/gdb/NEWS b/gdb/NEWS index 36443c38aca..500d5ab7160 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -92,6 +92,10 @@ show remote thread-options-packet objects can still be obtained through calling other API functions, for example 'gdb.current_progspace()'. + ** User defined attributes can be added to a gdb.Inferior object, + these will be stored in the object's new Inferior.__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 d74defeec0c..0e6eeeeb15d 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3667,6 +3667,42 @@ string. @end defun +One may add arbitrary attributes to @code{gdb.Inferior} objects in the +usual Python way. This is useful if, for example, one needs to do +some extra record keeping associated with the inferior. + +In this contrived example we record the time when an inferior 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() + inferior = thread.inferior + inferior._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_inferior()._last_stop_time) +2024-01-04 14:48:41.347036 +@end group +@end smallexample + @node Events In Python @subsubsection Events In Python @cindex inferior events in Python diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 929d8bd17b5..3f14bc31459 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -46,6 +46,10 @@ struct inferior_object /* thread_object instances under this inferior. This owns a reference to each object it contains. */ thread_map_t *threads; + + /* Dictionary holding user-added attributes. + This is the __dict__ attribute of the object. */ + PyObject *dict; }; extern PyTypeObject inferior_object_type @@ -241,6 +245,9 @@ inferior_to_inferior_object (struct inferior *inferior) inf_obj->inferior = inferior; inf_obj->threads = new thread_map_t (); + inf_obj->dict = PyDict_New (); + if (inf_obj->dict == nullptr) + return nullptr; /* PyObject_New initializes the new object with a refcount of 1. This counts for the reference we are keeping in the inferior data. */ @@ -981,6 +988,8 @@ infpy_dealloc (PyObject *obj) function is called. */ gdb_assert (inf_obj->inferior == nullptr); + Py_XDECREF (inf_obj->dict); + Py_TYPE (obj)->tp_free (obj); } @@ -1038,6 +1047,8 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_inferior); static gdb_PyGetSetDef inferior_object_getset[] = { + { "__dict__", gdb_py_generic_dict, nullptr, + "The __dict__ for this inferior.", &inferior_object_type }, { "arguments", infpy_get_args, infpy_set_args, "Arguments to this program.", nullptr }, { "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL }, @@ -1135,7 +1146,7 @@ PyTypeObject inferior_object_type = 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ + offsetof (inferior_object, dict), /* tp_dictoffset */ 0, /* tp_init */ 0 /* tp_alloc */ }; diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 5a221f800c3..0e00636fa1c 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -85,6 +85,15 @@ gdb_test "python print (i0.threads ())" \ gdb_test "python print (i0.progspace)" "" gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True" +# Add a user defined attribute to the inferior, and check the +# attribute can be read back. +gdb_test_no_output "python i0._user_attr = 123" \ + "add a user defined attribute to the inferior object" +gdb_test "python print(i0._user_attr)" \ + "123" "read back user defined attribute from i0" +gdb_test "python print(gdb.inferiors()\[0\]._user_attr)" \ + "123" "read back user defined attribute from gdb.inferiors" + # Test the number of inferior threads. gdb_breakpoint check_threads @@ -127,6 +136,16 @@ gdb_test "print str" " = \"hallo, testsuite\"" \ # correct inferior. set num [add_inferior] +# Confirm the new inferior doesn't have the user defined attribute, +# but that the first inferior does still have the attribute. +gdb_test "python print(gdb.inferiors()\[1\]._user_attr)" \ + [multi_line \ + "AttributeError: 'gdb\\.Inferior' object has no attribute '_user_attr'" \ + "Error while executing Python code\\."] \ + "check new inferior doesn't have user defined attribute" +gdb_test "python print(gdb.inferiors()\[0\]._user_attr)" \ + "123" "read back user defined attribute again" + # Test memory search. set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*} -- 2.25.4