From patchwork Wed Jan 10 15:54:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83755 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 B2A073861033 for ; Wed, 10 Jan 2024 15:55:29 +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 D9E653858426 for ; Wed, 10 Jan 2024 15:54:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D9E653858426 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 D9E653858426 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=1704902096; cv=none; b=f39Qs57BMAplEGeoci0ukYWQpZwZYP8qowLSWCD0qT5LQlMcwkOYsz7zvfD8jRUuD/DhSuhoGWvGX56k2UlXMfMUiu3m5pwcuxd1Rqd8j5RjEYF0k4mZmcfnUza9Sae3geZM8TD0B5jIoBTCJbiY93pwWelMiixIxsoU8aRLfMg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902096; c=relaxed/simple; bh=FI7QZDgu6pNE26JlnfshnE22nDUZ6mIIM67WHnU0wjI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=cM0g8FOI39p1nHyZ2X/XJbhpnHU1dUbA9lVzRgUbOR6t/6CZxzue5sKrd1jRJzoLOwDabKy0lmj/uZLGww2yj4l1LSAl2HBRT/oHbgXHDSCwA6wZxxVCos1yOWorGtSQ+0M1EA5ijtO6s3/k3uLF7mxPnpKn9idvt0kdoKtmRN8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902093; 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=qGrc9MVuFEjeGuYDQJEFIh4PaLgb+k2juIEuvdm8Ivk=; b=BssyGlCbxhdM+eqAf3bDKPRPvUOVEOFDiv0odjKFGAufYfgGpfFdWCPbJDaifVzKc4wAHR xzyyqLkEubzkZ7nyIOHgzStvY2sPLQEvuNZd+Bk2v2ujJdSfuyPT+F5J9ChysfFX8FfuD8 2ow3UVWzH5yoMcbk9rADBfGOKlCWb6M= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-265-KzRvFAmWNVeZ5y3_Lr03NQ-1; Wed, 10 Jan 2024 10:54:52 -0500 X-MC-Unique: KzRvFAmWNVeZ5y3_Lr03NQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40e5b156692so410165e9.2 for ; Wed, 10 Jan 2024 07:54:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902091; x=1705506891; 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=qGrc9MVuFEjeGuYDQJEFIh4PaLgb+k2juIEuvdm8Ivk=; b=Qb8vNdMeBgeQv6M1JwsQp33fNVUHUeGlIl9jQNNJfXpqcracmGN6aq4aDYP5ql8dEG 1wEqP5Kxo8ObfAzxsbEpVTugf5A5kC+5SM6oMzNP6xDp6xbSyn5moJpO/z/Hu5ncoI3G DeEXhW+LfyNpj2XlSOZno6P7cuWXMmrWmHfaNdhmgU5YlD8fHuTqmYnuersl7M30QFAq gr8Bg+FC1nBI/FLgKngTYDJAgLs7oBcAMHY3sDQmOPaNnesstkGCJG28fDLhvJyS5H2y SMrKk0qBy0l5AWJxOQGRipfASJYEc6it40t2P3ycEg51Y0bdIS7LlYVs1LakCbAHPyFb A/8Q== X-Gm-Message-State: AOJu0YwyO42oR3WNeoXwKViwiM7xWEQaHM4/4Fc3svH2CGm6TheucdUD pRtd0muvsrKfR1dY0I8PAxT7Q/G5VRvYqvw6DGs6WqkbFRw8fuKH3e9esMAtNRr41ZHCdaTtS0t i5Oy1IB2HS9UqJZz3D2nB4u03pAnWND5BlblKFVbBza6uz27xJTzPvmxZWxx9K1F0mOArGA88aQ SApRnYfRu+XvWQEQ== X-Received: by 2002:a05:600c:468a:b0:40e:3d8d:4cc1 with SMTP id p10-20020a05600c468a00b0040e3d8d4cc1mr741119wmo.71.1704902090873; Wed, 10 Jan 2024 07:54:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWAfzPBYMy6WGwCzTav7can3qnApbdHdvxBtea2lIKxAefBCN6V7hn21tHEnmRl2DLobq9QA== X-Received: by 2002:a05:600c:468a:b0:40e:3d8d:4cc1 with SMTP id p10-20020a05600c468a00b0040e3d8d4cc1mr741111wmo.71.1704902090495; Wed, 10 Jan 2024 07:54:50 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id fa24-20020a05600c519800b0040e490cb666sm2551414wmb.12.2024.01.10.07.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:49 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 1/8] gdb/python: hoist common invalid object repr code into py-utils.c Date: Wed, 10 Jan 2024 15:54:37 +0000 Message-Id: <791370ed269cee0ce3dc30f0a01307c00473878a.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_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 Many object types now have a __repr__() function implementation. A common pattern is that, if an object is invalid, we print its representation as: . I thought it might be a good idea to move the formatting of this specific representation into a utility function, and then update all of our existing code to call the new function. The only place where I haven't made use of the new function is in unwind_infopy_repr, where we currently return a different string. This case is a little different as the UnwindInfo is invalid because it references a frame, and it is the frame itself which is invalid. That said, I think it would be fine to switch to using the standard format; if the UnwindInfo references an invalid frame, then the UnwindInfo is itself invalid. But changing this would be an actual change in behaviour, while all the other changes in this commit are just refactoring. --- gdb/python/py-arch.c | 2 +- gdb/python/py-block.c | 2 +- gdb/python/py-breakpoint.c | 4 ++-- gdb/python/py-connection.c | 2 +- gdb/python/py-inferior.c | 2 +- gdb/python/py-objfile.c | 2 +- gdb/python/py-symbol.c | 2 +- gdb/python/py-type.c | 3 +-- gdb/python/py-unwind.c | 2 +- gdb/python/py-utils.c | 8 ++++++++ gdb/python/python-internal.h | 9 +++++++++ 11 files changed, 27 insertions(+), 11 deletions(-) diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index ac519331f18..b7d861d27ad 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -326,7 +326,7 @@ archpy_repr (PyObject *self) { const auto gdbarch = arch_object_to_gdbarch (self); if (gdbarch == nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); + return gdb_py_invalid_object_repr (self); auto arch_info = gdbarch_bfd_arch_info (gdbarch); return PyUnicode_FromFormat ("<%s arch_name=%s printable_name=%s>", diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index dd6d6d278a0..34be4664144 100644 --- a/gdb/python/py-block.c +++ b/gdb/python/py-block.c @@ -425,7 +425,7 @@ blpy_repr (PyObject *self) { const auto block = block_object_to_block (self); if (block == nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); + return gdb_py_invalid_object_repr (self); const auto name = block->function () ? block->function ()->print_name () : ""; diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index 5155d41e675..9b5e023cb09 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -1750,8 +1750,8 @@ bplocpy_repr (PyObject *py_self) { const auto self = (gdbpy_breakpoint_location_object *) py_self; if (self->owner == nullptr || self->owner->bp == nullptr - || self->owner->bp != self->bp_loc->owner) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); + || self->owner->bp != self->bp_loc->owner) + return gdb_py_invalid_object_repr (py_self); const auto enabled = self->bp_loc->enabled ? "enabled" : "disabled"; diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c index 3df12b435bb..d288a74cb2b 100644 --- a/gdb/python/py-connection.c +++ b/gdb/python/py-connection.c @@ -204,7 +204,7 @@ connpy_repr (PyObject *obj) process_stratum_target *target = self->target; if (target == nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (obj)->tp_name); + return gdb_py_invalid_object_repr (obj); return PyUnicode_FromFormat ("<%s num=%d, what=\"%s\">", Py_TYPE (obj)->tp_name, diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index ed153d668ac..929d8bd17b5 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -800,7 +800,7 @@ infpy_repr (PyObject *obj) inferior *inf = self->inferior; if (inf == nullptr) - return PyUnicode_FromString (""); + return gdb_py_invalid_object_repr (obj); return PyUnicode_FromFormat ("", inf->num, inf->pid); diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index bb5d0d92aba..4f5e5cda5e6 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -537,7 +537,7 @@ objfpy_repr (PyObject *self_) objfile *obj = self->objfile; if (obj == nullptr) - return PyUnicode_FromString (""); + return gdb_py_invalid_object_repr (self_); return PyUnicode_FromFormat ("", objfile_name (obj)); diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 99724cfc95b..014442bf147 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -385,7 +385,7 @@ sympy_repr (PyObject *self) { const auto symbol = symbol_object_to_symbol (self); if (symbol == nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); + return gdb_py_invalid_object_repr (self); return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name, symbol->print_name ()); diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index bfaa6d24d94..27c7b78096b 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -1083,8 +1083,7 @@ typy_repr (PyObject *self) { const auto type = type_object_to_type (self); if (type == nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", - Py_TYPE (self)->tp_name); + return gdb_py_invalid_object_repr (self); const char *code = pyty_codes[type->code ()].name; string_file type_name; diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index f12485c22b7..70c33724cbc 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -425,7 +425,7 @@ pending_framepy_repr (PyObject *self) frame_info_ptr frame = pending_frame->frame_info; if (frame == nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); + return gdb_py_invalid_object_repr (self); const char *sp_str = nullptr; const char *pc_str = nullptr; diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index f1ca9ea0a5d..c29291004d2 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -597,3 +597,11 @@ gdbpy_fix_doc_string_indentation (gdb::unique_xmalloc_ptr doc) return doc; } + +/* See python-internal.h. */ + +PyObject * +gdb_py_invalid_object_repr (PyObject *self) +{ + return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); +} diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 14e15574685..8ff9af650c2 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -897,6 +897,15 @@ int gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; + +/* Return a Python string (str) object that represents SELF. SELF can be + any object type, but should be in an "invalid" state. What "invalid" + means is up to the caller. The returned string will take the form + "", without the quotes, and with TYPENAME replaced + with the type of SELF. */ + +PyObject *gdb_py_invalid_object_repr (PyObject *self); + struct varobj_iter; struct varobj; std::unique_ptr py_varobj_get_iterator From patchwork Wed Jan 10 15:54:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83758 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 C22893860765 for ; Wed, 10 Jan 2024 15:56:10 +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 2BFE23857B99 for ; Wed, 10 Jan 2024 15:54:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BFE23857B99 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 2BFE23857B99 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=SHUDBWNRIYNKCzwpFBrVfTPKAXakWkh44f9+7GrfZqsORQIZ/RmLsKt61LXhXf+nX9c+QR4jgjG3GLVJhwBzfe6VEh5cCM/3LofaP7sa6SO+t6IKo5BhB55ctjcIp1LM4ncFCBr1bRARaUQRU6TbzMdBmmeM5Gl+3hbF5I+0lms= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902101; c=relaxed/simple; bh=EuzstJi/SQudauaNeVnW6Z84FCcaipa77ZcpvAo+Ako=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vnNcLGhv1mkeLZWmm1BhLOzhoqCxDHX9c+iM+oB6xjnbCIHehnAVpoiuSbZKjN0X8A4hcdYt2JFYOwpp05uVhyxbWbij/8mWGofhFMb6H9qKFzv0mJfX4iBWSJkt2Nh+Ls3tqmS0/vKyFr/BuYUHTbSIp/tJzkQOpvGBVcUZX2w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902096; 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=fnSE3H2ACK1VEqkPatnv0AwGOzic/lZTZ0DBaMWIQQQ=; b=b0MOQC5SUyGPDDeFpVuGLqBcJZszM+TBMyx1G1B+80C5aAI17159XxCoVAGFneaZBt1f5s I8+xeXpfgXdp1eKE+PqnIOU8bP0Yi8dtT9QK7SbTD/7n+0qCj+q/AvztXlZBMYJeyt71vt 39bFl0uh+Aypn+3k1klCUHcKurnQTbo= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-74-PC03Njf0NquBKGUwZkDsdA-1; Wed, 10 Jan 2024 10:54:55 -0500 X-MC-Unique: PC03Njf0NquBKGUwZkDsdA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3368698efbdso2575270f8f.0 for ; Wed, 10 Jan 2024 07:54:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902094; x=1705506894; 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=fnSE3H2ACK1VEqkPatnv0AwGOzic/lZTZ0DBaMWIQQQ=; b=lsZIwfWJf9LgyjTh+FfXoQP/G22Q7HuM+wVjTmtwUmQYDidkSBm3phRNj0obGBJjgl YT0gQJueL87t3jyxxG7sE88DQ72cXj/H2asOn23wIFDyJMg8hCCHzJIHzLNIhIWntFyx S7IY1FSy5XrSVGtwPGl8MdtDroLxv1VP20dzNBk29J5Slr/hLzjpRUYGsXzrzf4DVDA1 yLzyb9lxaMjJsxROZzozkXqV5HudxUcWH7BnKgUEFsHyf6zOuBbwKu62vv5AAwSWLWZh tpZcH4e+YwzL44Adp4eAD3Z8Ujbg5Zc66kCCtPIiI3qRwcAYPnLpuB5M2mkTWjJd8tl0 eKhw== X-Gm-Message-State: AOJu0Yysn5XzCMuk+onA3nCcQosy2H09Z6uK+km9Rwk++Jx0sgQNje4I sqW9MwArn5VcSK8CvKOTjnptkJr4EbzITt8PX50EAPVc6QkyzOr4nyrGf4Y68SDt6QiSITexlG2 4cemUScihlWXKNsVMt3YyPcRlLa97OlDpfPfvkyNojh1DWsNU8dQYuMPMpLQ+jd4QVMhCwqoOwr SF4tzox6ZUPzj8rw== X-Received: by 2002:a5d:6e07:0:b0:336:9e8a:6fde with SMTP id h7-20020a5d6e07000000b003369e8a6fdemr789177wrz.22.1704902093829; Wed, 10 Jan 2024 07:54:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUj9T9iiZqpuXfG9eNwRp/s8sn1SAi4O1cpFMaYJXXwPVcgxTko0IIotH1xeAZ+FCvytV8ig== X-Received: by 2002:a5d:6e07:0:b0:336:9e8a:6fde with SMTP id h7-20020a5d6e07000000b003369e8a6fdemr789171wrz.22.1704902093402; Wed, 10 Jan 2024 07:54:53 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id bi6-20020a05600c3d8600b0040e3960f46asm2604545wmb.14.2024.01.10.07.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:52 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/8] gdb/python: add gdb.InferiorThread.__repr__() method Date: Wed, 10 Jan 2024 15:54:39 +0000 Message-Id: <37fbaf0a27d3938f3107be2ab79dac3e748ccff6.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 Add a gdb.InferiorThread.__repr__() method. Before this patch we would see output like this: (gdb) pi >>> gdb.selected_thread() After this patch, we now see: (gdb) pi >>> gdb.selected_thread() More verbose, but, I hope, more useful. If the gdb.InferiorThread becomes invalid, then we will see: (gdb) pi >>> invalid_thread_variable Which is inline with how other invalid objects are displayed. --- gdb/python/py-infthread.c | 19 ++++++++++++++++++- gdb/testsuite/gdb.python/py-inferior.exp | 16 +++++++++++++++- gdb/testsuite/gdb.python/py-infthread.exp | 3 ++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index 00d7171de64..2d892b10b69 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -313,6 +313,23 @@ thpy_thread_handle (PyObject *self, PyObject *args) return object; } +/* Implement repr() for gdb.InferiorThread. */ + +static PyObject * +thpy_repr (PyObject *self) +{ + thread_object *thread_obj = (thread_object *) self; + + if (thread_obj->thread == nullptr) + return gdb_py_invalid_object_repr (self); + + thread_info *thr = thread_obj->thread; + return PyUnicode_FromFormat ("<%s id=%s target-id=\"%s\">", + Py_TYPE (self)->tp_name, + print_full_thread_id (thr), + target_pid_to_str (thr->ptid).c_str ()); +} + /* Return a reference to a new Python object representing a ptid_t. The object is a tuple containing (pid, lwp, tid). */ PyObject * @@ -429,7 +446,7 @@ PyTypeObject thread_object_type = 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ - 0, /*tp_repr*/ + thpy_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 6fbcdd6822f..5a221f800c3 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -78,7 +78,9 @@ gdb_test "python print ('result = %s' % (i0.connection_num == i0.connection.num) "Inferior.connection_num equals Inferior.connection.num" gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid" gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached" -gdb_test "python print (i0.threads ())" "\\(,\\)" "test Inferior.threads" +gdb_test "python print (i0.threads ())" \ + "\\(,\\)" \ + "test Inferior.threads" gdb_test "python print (i0.progspace)" "" gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True" @@ -89,11 +91,23 @@ gdb_breakpoint check_threads gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*" gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2" +# Grab the last thread from the list. This thread object will become +# invalid when the corresponding thread exits. +gdb_test_no_output "python last_thread = i0.threads()\[-1\]" +gdb_test "python print(last_thread)" \ + "" \ + "test repr of a valid thread" + # Proceed to the next test. gdb_breakpoint [gdb_get_line_number "Break here."] gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*" +# Check the repr() for an invalid gdb.InferiorThread object. +gdb_test "python print(last_thread)" \ + "" \ + "test repr of an invalid thread" + # Test memory read and write operations. gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ diff --git a/gdb/testsuite/gdb.python/py-infthread.exp b/gdb/testsuite/gdb.python/py-infthread.exp index 0b10ce9ff77..49622c8ac28 100644 --- a/gdb/testsuite/gdb.python/py-infthread.exp +++ b/gdb/testsuite/gdb.python/py-infthread.exp @@ -57,7 +57,8 @@ gdb_py_test_silent_cmd "python gdb.selected_thread ().inferior" "test InferiorTh gdb_py_test_silent_cmd "python t0 = gdb.selected_thread ()" "test gdb.selected_thread" 1 -gdb_test "python print (t0)" "\\" "verify InferiorThread object" +gdb_test "python print (t0)" \ + "" 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" From patchwork Wed Jan 10 15:54:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83757 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 ADEEB386075A for ; Wed, 10 Jan 2024 15:56:10 +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 6DE713857C45 for ; Wed, 10 Jan 2024 15:54:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6DE713857C45 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 6DE713857C45 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=1704902102; cv=none; b=vAlZr+7BIB/5v9P1ImM0VxQLHv8rd0SLzsWNUeblbI7stDP5IE2G5GdFSKe5kAmAtHVsoHBVsWQPunxxFVUpxYsSnbi51Edp8GgNe1Q8R29AUHIp7VB8705ZZDe2ifk8KBA0L6AGZVutFenBTRSyvtFy375Yae+172d1pW36weY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902102; c=relaxed/simple; bh=VbrDf+cllGINFkyjU8HJmxaLG3f140bpf9hHU1SXWiM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=D7d4DdwkeCgAzNXKeXi7DoREtJidmqVQYSRS6Audi1pG3fqE4omdhwJPmzKHohfD7baDCzS4eLlxITO1+83wdKaKWAH2vRl6+EbzGGSDXjxk0u+w68XbvM3bmS3iuWHQitNx9YKmKQL4ff5A5iiuYVwFkgIoKbR4wUtkaGalCa8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902098; 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=GIurhLwzKPJMpUlG4tYypyzqOSUyPWdwovdKmgp5yP4=; b=Ry5hZo5zheKiP2/7QkaOeKclDsXVUiMiwDSIZcyVXX8DWc5KnyDwknlsC1m3VtJwyRu0wL AjJOu0rFIBGDbSfrARWe/8UpEr2C9BmJybXTDuE9rY2rfdrPf9wEe3lGifoRif6x8atYyJ VhuTSzSepGvM+HgynN9bjQ5xeWG5Gb8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-416-ite43Xr8OAqArdZRvTXO6Q-1; Wed, 10 Jan 2024 10:54:56 -0500 X-MC-Unique: ite43Xr8OAqArdZRvTXO6Q-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40e439092a0so29380775e9.0 for ; Wed, 10 Jan 2024 07:54:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902095; x=1705506895; 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=GIurhLwzKPJMpUlG4tYypyzqOSUyPWdwovdKmgp5yP4=; b=NdNH2kFWstk5y232D/30XoMXYSEavruwpgfdWOrn92DFqjQXlf7Nmi9Z61zLP2NaXT M7b1z8NDe+yMNHXQKlUrydVdlF6AxqAts5yHTXfcT+EBdD9fQd2VN8nNGR5Eh2WKklzA Sq/4zouxajXTiwPcGYlFE9+6T6QWj1VJ9qCpegDDRhKWU+SzRb7H6ejWu/LapYgmcubc SrntxEQ79DDnmwK5o4ygHrxZhYhwOkBd1s5ByRuuFnCj+6eu9UpZgD2SD5Y6WbXzDk0H hfdEF2dV7/IpCkR5Us9no+pvdTQR5xhVt0XwSFLoaom+v+tAjYIPeDR5tYizsH+kVqgg xB+A== X-Gm-Message-State: AOJu0Yzzg7hZ78JdM5MgA2za7ealG0OPLYqUATlXCJeKZW7dOa96oXE4 iEoSz9dqkVLo52GlKYqngWiHd0WAALjsv5WEZCYRKzsq81DeB5mocWtfuPvX0c/MGdJ44mQbC1F QKIig5C2IIFT1eC0ipCxiq4RF07J+VcIrZhwuAd19FhvkL1nsGOMnndgei9fdJGZyK4FqAMcvdF JkolVZClkt0W8Z2g== X-Received: by 2002:a05:600c:228b:b0:40e:3ad9:c139 with SMTP id 11-20020a05600c228b00b0040e3ad9c139mr706820wmf.81.1704902095390; Wed, 10 Jan 2024 07:54:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IFHksf+r3k2t93N1F/EU8Qoc03jshFg9CPlaPboW6xtlM6oShoxGitl4XbN9iW08/1hZCiF+Q== X-Received: by 2002:a05:600c:228b:b0:40e:3ad9:c139 with SMTP id 11-20020a05600c228b00b0040e3ad9c139mr706813wmf.81.1704902095127; Wed, 10 Jan 2024 07:54:55 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id bh21-20020a05600c3d1500b0040d987aae3esm2596167wmb.13.2024.01.10.07.54.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:54 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 3/8] gdb/python: add gdb.Frame.__repr__() method Date: Wed, 10 Jan 2024 15:54:40 +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 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 Add a gdb.Frame.__repr__() method. Before this patch we would see output like this: (gdb) pi >>> gdb.selected_frame() After this patch, we now see: (gdb) pi >>> gdb.selected_frame() More verbose, but, I hope, more useful. If the gdb.Frame becomes invalid, then we will see: (gdb) pi >>> invalid_frame_variable which is inline with how other invalid objects are displayed. --- gdb/python/py-frame.c | 19 ++++++++++++++++++- gdb/testsuite/gdb.python/py-frame.exp | 8 ++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index f38bf5d8237..ee465ed64a1 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -84,6 +84,23 @@ frapy_str (PyObject *self) return PyUnicode_FromString (fid.to_string ().c_str ()); } +/* Implement repr() for gdb.Frame. */ + +static PyObject * +frapy_repr (PyObject *self) +{ + frame_object *frame_obj = (frame_object *) self; + frame_info_ptr f_info = frame_find_by_id (frame_obj->frame_id); + if (f_info == nullptr) + return gdb_py_invalid_object_repr (self); + + const frame_id &fid = frame_obj->frame_id; + return PyUnicode_FromFormat ("<%s level=%d frame-id=%s>", + Py_TYPE (self)->tp_name, + frame_relative_level (f_info), + fid.to_string ().c_str ()); +} + /* Implementation of gdb.Frame.is_valid (self) -> Boolean. Returns True if the frame corresponding to the frame_id of this object still exists in the inferior. */ @@ -840,7 +857,7 @@ PyTypeObject frame_object_type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - 0, /* tp_repr */ + frapy_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp index 16177c8a5f8..6162696948f 100644 --- a/gdb/testsuite/gdb.python/py-frame.exp +++ b/gdb/testsuite/gdb.python/py-frame.exp @@ -36,6 +36,10 @@ gdb_breakpoint [gdb_get_line_number "Block break here."] gdb_continue_to_breakpoint "Block break here." gdb_py_test_silent_cmd "python bf1 = gdb.selected_frame ()" "get frame" 0 +# Test Frame.__repr__() method for a valid Frame object. +gdb_test "python print (repr(bf1))" "" \ + "test __repr__ for gdb.Frame on a valid frame" + # Test Frame.architecture() method. gdb_py_test_silent_cmd "python show_arch_str = gdb.execute(\"show architecture\", to_string=True)" "show arch" 0 gdb_test "python print (bf1.architecture().name() in show_arch_str)" "True" "test Frame.architecture()" @@ -100,6 +104,10 @@ gdb_py_test_silent_cmd "python bframe = gdb.selected_frame()" \ "get bottommost frame" 0 gdb_test "up" ".*" "" +# Test Frame.__repr__() method for an invalid Frame object. +gdb_test "python print (repr(bf1))" "" \ + "test __repr__ for gdb.Frame on an invalid frame" + gdb_py_test_silent_cmd "python f1 = gdb.selected_frame ()" "get second frame" 0 gdb_py_test_silent_cmd "python f0 = f1.newer ()" "get first frame" 0 gdb_py_test_silent_cmd "python f2 = f1.older ()" "get last frame" 0 From patchwork Wed Jan 10 15:54:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83761 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 1C598385DC01 for ; Wed, 10 Jan 2024 15:56:48 +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 8DF0438582B0 for ; Wed, 10 Jan 2024 15:55:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8DF0438582B0 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 8DF0438582B0 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=1704902102; cv=none; b=W8ilMm7rBWuDTy4H0av/8not8s3uzPAiSJ0ymGR+TZImeF8T8Yi6dyPUt4ghrC9DuyoH11J7aAZkAwSogkUcSNCHleyrC9vR111Xnqd1TbvaSLDmMNYjS6Y8YPWZ7EXG5alcejEqqPxKYph4zH45EnioDXO4DlRkPCJn2poUOwg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902102; c=relaxed/simple; bh=yoVpfo3dycYaM9L7ds6RIBN48HBNBn+AWphZVfLY/lc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=N1Mo4NyCJeuSS40eC80Xp04rC4P4FEjGQ0joWNgZv0R6K3fhmWy6yxFuDRsxJsnOTe8kDUZl80b9C3eks0/G+M6FpcJ9GpLfOi4f7cjGX4OtW/Kf5mn8Phx4x/Vvig0uCLfzyBSI/wlIa9r7jnG2OmrlvPDDQiSOw89QLGFM5Jo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902100; 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=08vr4FmuHFtNm/C6njOEFGhYSQD4wqYkXTlPYw8W5ZY=; b=dVqcGcUvVgXxLFIj3TfEwIELSZ2fSgqteVp8cmR6opijqdkyErP894dvSM93mOJMBY9CAI SNQ8zWiN8Rza9YifIs/1brd9gIF4rWiWMTqbMw9OoTEe9RYJ+vQkq3GycnqxDhN3V2I6ut YKlgT3lNv3d7FXZ356QgWhG+s12/4Po= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-463-cCgAL-AFP56qj_GI2JmIeg-1; Wed, 10 Jan 2024 10:54:58 -0500 X-MC-Unique: cCgAL-AFP56qj_GI2JmIeg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40e4caa37f5so17507435e9.0 for ; Wed, 10 Jan 2024 07:54:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902097; x=1705506897; 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=08vr4FmuHFtNm/C6njOEFGhYSQD4wqYkXTlPYw8W5ZY=; b=mqvlYj56r3HCM4mTbWYi+qmf79+p5n+eKOGFtzyZLlbq4ui+W+ku0bifWih0oLLaM+ 8upTfKodDdhbj7DIoX7/ZXAFC5urkhSUP/xB+IW1pDhG3gaX+m4jxuJCN3uA169PFWEw Mxd9Y6OdXeqVhJeSAv9bv+pnj5g+KKjPo3ADHgK8v/77xSq/08nT08BT8yWM8wdJi1M1 lMRhwRuynfcCVCjPzfYhwmcRruFgasOMMS0g1xG1/JM3xKT1ObAJScWukgfQd3j4XziK yytWHsp1+5cd26QRAt/nJSTVfVu4r/fH8v2gRYejAm1utV2avi+BAhA+dy2px8pob8ni joCQ== X-Gm-Message-State: AOJu0Yz5i917mOXIDmrm5HL+migHa2UHKWQ3xwRvgUpiDxTGJ/fS7GIi 7KNEfs3mJuzDn2F02Eh852L52OLhifk9yeii8EyPO89VjHX5fgwDVtX8YixXLmHPIz9djD3rAOR BFvuW+F47c5WhMzuVo0m3/XMgSRitxPlajZ7jXFyXaA8H74bLG4yr26VCgSWKpH2tplXYlnhSzz dzUemznFm2dM9jHg== X-Received: by 2002:a05:600c:154e:b0:40e:4617:2290 with SMTP id f14-20020a05600c154e00b0040e46172290mr675555wmg.97.1704902097029; Wed, 10 Jan 2024 07:54:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFFl22ziK6IXStyEXup8qEt6THASvtyp3USpmQlO7yMvC3nacbfHmDIR+Gu2z3IpqlZXTDK5A== X-Received: by 2002:a05:600c:154e:b0:40e:4617:2290 with SMTP id f14-20020a05600c154e00b0040e46172290mr675548wmg.97.1704902096719; Wed, 10 Jan 2024 07:54:56 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id r15-20020a05600c458f00b0040d1bd0e716sm2641853wmo.9.2024.01.10.07.54.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:55 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv2 4/8] gdb/python: remove users ability to create gdb.Progspace objects Date: Wed, 10 Jan 2024 15:54:41 +0000 Message-Id: <8990b13a57adba8f48f0aef44364bacf305c7bca.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_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 I noticed that it is possible for the user to create a new gdb.Progspace object, like this: (gdb) pi >>> p = gdb.Progspace() >>> p >>> p.is_valid() False As the new gdb.Progspace object is not associated with an actual C++ program_space object within GDB core, then the new gdb.Progspace is created invalid, and there is no way in which the new object can ever become valid. Nor do I believe there's anywhere in the Python API where it makes sense to consume an invalid gdb.Progspace created in this way, for example, the gdb.Progspace could be passed as the locus to register_type_printer, but all that would happen is that the registered printer would never be used. In this commit I propose to remove the ability to create new gdb.Progspace objects. Attempting to do so now gives an error, like this: (gdb) pi >>> gdb.Progspace() Traceback (most recent call last): File "", line 1, in TypeError: cannot create 'gdb.Progspace' instances Of course, there is a small risk here that some existing user code might break ... but if that happens I don't believe the user code can have been doing anything useful, so I see this as a small risk. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 5 +++++ gdb/python/py-progspace.c | 16 +--------------- gdb/testsuite/gdb.python/py-progspace.exp | 6 ++++++ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 11cd6c0663e..36443c38aca 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -87,6 +87,11 @@ show remote thread-options-packet ** New function gdb.interrupt(), that interrupts GDB as if the user typed control-c. + ** It is no longer possible to create new gdb.Progspace object using + 'gdb.Progspace()', this will result in a TypeError. Progspace + objects can still be obtained through calling other API + functions, for example 'gdb.current_progspace()'. + * Debugger Adapter Protocol changes ** GDB now emits the "process" event. diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 0797ef1fa6b..bfc6ff50f01 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -210,20 +210,6 @@ pspy_initialize (pspace_object *self) return 1; } -static PyObject * -pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) -{ - gdbpy_ref self ((pspace_object *) type->tp_alloc (type, 0)); - - if (self != NULL) - { - if (!pspy_initialize (self.get ())) - return NULL; - } - - return (PyObject *) self.release (); -} - PyObject * pspy_get_printers (PyObject *o, void *ignore) { @@ -859,5 +845,5 @@ PyTypeObject pspace_object_type = offsetof (pspace_object, dict), /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - pspy_new, /* tp_new */ + 0, /* tp_new */ }; diff --git a/gdb/testsuite/gdb.python/py-progspace.exp b/gdb/testsuite/gdb.python/py-progspace.exp index befd6433e47..5f6a9577f82 100644 --- a/gdb/testsuite/gdb.python/py-progspace.exp +++ b/gdb/testsuite/gdb.python/py-progspace.exp @@ -53,6 +53,12 @@ gdb_py_test_silent_cmd "python progspace.random_attribute = 42" \ gdb_test "python print (progspace.random_attribute)" "42" \ "Verify set of random attribute in progspace" +# Check that we can't create new (invalid) gdb.Progspace objects. +gdb_test "python gdb.Progspace()" \ + [multi_line "TypeError: cannot create 'gdb.Progspace' instances" \ + "Error while executing Python code\\."] \ + "check for error when calling gdb.Progspace() directly" + if {![runto_main]} { return } From patchwork Wed Jan 10 15:54:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83759 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 730DE3861810 for ; Wed, 10 Jan 2024 15:56:36 +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 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 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 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, 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]*} 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')" \ From patchwork Wed Jan 10 15:54: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: 83762 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 A31F83857C65 for ; Wed, 10 Jan 2024 15:56:58 +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 6CDB8385E838 for ; Wed, 10 Jan 2024 15:55:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6CDB8385E838 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 6CDB8385E838 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=1704902107; cv=none; b=nYqbh2Cae54NLLB5LHr7jp0t+2QvHuimVuik55u2eo72dU4hpPHGnOkkGm1B5MIXbePOeQ+2Jomdfn+ihg34VzMM91+hgxBzpa35mSBieBv7Elci5ZZc+8YtOqRisQsQJeMbPZjeOHAhAmuO8tjfAtOlBGNaIZPxVtlDdNcFbf0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902107; c=relaxed/simple; bh=qqkwLYzQ1segrGuHdfuln+pzPOl/PM6Kz75/8Sme4Cg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YOTldCo2YhOKVgXxMK+DMKpM+ZmPxGkXgMRjuXVbzO7VMmjWapJTn76NF6p68l0U+j75T5zqr5Ut6SU1VnfpTXjZCrn1xf0ALA0mVCz2EFT+g/3nxkzJ2FsqB61Q8t+kVZPcm8zpTrTuMbDJoTts5pbe10ZEwQE7qAKttwRZNcA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902105; 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=oTcBSyL9GfI7yV1i9lvEFTa9kbP4NmRLxWfmnzOZ7Ac=; b=CVW2i694352JKR8leJodgYy+ttd4FRt7lJlYKdZjuHuJysaE9yIwvFZXq660Ju1DSM4EXT KPws32F6OgxIv6UPDepxl4szcpEsp2PJ8o99Q9wKiAv4RJPXS06hrtvSsOOtnL1mUWQemX vJc9d/SptIMGLfO1lupPrs/iTitQEW8= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-656-4Y4vb8jIP7CD5YcQGRzLug-1; Wed, 10 Jan 2024 10:55:03 -0500 X-MC-Unique: 4Y4vb8jIP7CD5YcQGRzLug-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3368698f0caso2608497f8f.1 for ; Wed, 10 Jan 2024 07:55:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902101; x=1705506901; 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=oTcBSyL9GfI7yV1i9lvEFTa9kbP4NmRLxWfmnzOZ7Ac=; b=XWapxZdDT1FOeiHlh3spLZgOyQXFEvomuPipOQFqnWbv9R7RdrXXw1Rj+nIdo0PFYA QfOouaDy5V3AG99DiP9pgdAS6fLZYJSoBk3voHECaDKMUPCxDM0oQPJcaE3KlVc3sDZd QPfqRw4sBkMjaOlcAoN79P+3Zp029azxX6JlP5B3pkvSkltQ2WlZKoAso8OvFxC/cLXI Fv+/NdJJ6lYngsuAX/Y955Lr6YI1epAmtuIv1FI5RIq7W4aiGxSS1UQs/YNSIuUVjeN8 71z+luJg6UIFRUd2srFo3mJx2YPyShSNRjLgqGGQWVZSiO1XLuWHX58C6PTyiXprca16 F++g== X-Gm-Message-State: AOJu0YyDytEFO/57eIsmeHZ6ttOe8HdW/h5wPaXQKoxr4pR54B+cLD0W c+ODD6zPcCggCnc3zEd2CmzmnEx5rzfKITEVODJ191pmn420Q29/0kNcY4he50QDL9CcZVGiiac vW3gB16j2igxdvapC0p6uqyPuhbcVCtWxjKzPu79i+RMaduU8nqaZx32z9yMHlMTSBlque/VtuA NFDsMuBAqK2/+O/A== X-Received: by 2002:a5d:52c5:0:b0:337:6123:ca34 with SMTP id r5-20020a5d52c5000000b003376123ca34mr315994wrv.254.1704902101582; Wed, 10 Jan 2024 07:55:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IGwF265NSG9w5Oj4Lv1LLnuoeNW5+QPANT+pM+KF36zqn27rid3C4pStZy25NOxlgztFXUn0A== X-Received: by 2002:a5d:52c5:0:b0:337:6123:ca34 with SMTP id r5-20020a5d52c5000000b003376123ca34mr315988wrv.254.1704902101240; Wed, 10 Jan 2024 07:55:01 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id cw16-20020a056000091000b0033753a61e96sm5198077wrb.108.2024.01.10.07.55.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:55:00 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 7/8] gdb/doc: add some notes on selecting suitable attribute names Date: Wed, 10 Jan 2024 15:54:44 +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 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 In previous commits I've added Object.__dict__ support to gdb.Inferior and gdb.InferiorThread, this is similar to the existing support for gdb.Objfile and gdb.Progspace. This commit extends the documentation to offer the user some guidance on selecting good names for their custom attributes so they can (hopefully) avoid conflicting with any future attributes that GDB might add. The rules I've proposed are: 1. Don't start user attributes with a lower case letter, all the current GDB attributes start with a lower case letter, and I suspect all future attributes would also start with a lower case letter, and 2. Don't start user attributes with a double underscore, this risks conflicting with Python built in attributes (e.g. __dict__) - though clearly the user would need to start and end with a double underscore, but it seemed easier just to say no double underscores. I'm doing this as a separate commit as I've updated the docs for the existing gdb.Objfile and gdb.Progspace so they all reference a single paragraph on selecting attribute names. Reviewed-By: Eli Zaretskii --- gdb/doc/python.texi | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 2eed332fe59..40803274a27 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3671,6 +3671,13 @@ usual Python way. This is useful if, for example, one needs to do some extra record keeping associated with the inferior. +@anchor{choosing attribute names} +When selecting a name for a new attribute, avoid starting the new +attribute name with a lower case letter; future attributes added by +@value{GDBN} will start with a lower case letter. Additionally, avoid +starting attribute names with two underscore characters, as these +could clash with Python builtin attribute names. + In this contrived example we record the time when an inferior last stopped: @@ -4181,6 +4188,9 @@ in the usual Python way. This is useful if, for example, one needs to do some extra record keeping associated with the thread. +See @ref{choosing attribute names} for guidance on selecting a +suitable name for new attributes. + In this contrived example we record the time when a thread last stopped: @@ -5396,6 +5406,9 @@ This is useful if, for example, one needs to do some extra record keeping associated with the program space. +See @ref{choosing attribute names} for guidance on selecting a +suitable name for new attributes. + In this contrived example, we want to perform some processing when an objfile with a certain symbol is loaded, but we only want to do this once because it is expensive. To achieve this we record the results @@ -5556,6 +5569,9 @@ This is useful if, for example, one needs to do some extra record keeping associated with the objfile. +See @ref{choosing attribute names} for guidance on selecting a +suitable name for new attributes. + In this contrived example we record the time when @value{GDBN} loaded the objfile. From patchwork Wed Jan 10 15:54:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83763 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 DC97C385842F for ; Wed, 10 Jan 2024 15:57:13 +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 D3103385DC01 for ; Wed, 10 Jan 2024 15:55:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3103385DC01 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 D3103385DC01 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=1704902111; cv=none; b=FqVXIzdXPPXm8NgLRzvwmR9srVOTWU2vnd8qhqwC3qaI3Fh2xCV56ibNOb9qibrVB57pcInDGrrd9qhqMkjVS0D1DW3yakPwrETJJBW+hZ14wY3jiZrAWI28JoJ4GyUECUlTRzOFaQKSKJs7kGCEzn0etxmH1FkKB48JlSWE5oI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902111; c=relaxed/simple; bh=pRNYo7K/gKQMk5sNZfzmysUyTZaZTkJdphA5A7zhNz4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=KcUv95aYwlziKm59/6Y+Ly56IkP9yVn1V3syhwg6VYNQjvp9hk4nyJIsSGkRPIFTD64c2IePAZq/fXtd0x873fIIJ/t7rXBqne47c1I9CWMiHl1I/uOWyy7qpjsET3TePVI8TP5CTdHB0zVnp5hdqNszl82cZlqPTHHD8sBV08s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902107; 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=P6xHVkkzcRE7FDvy1s+fIkbme0s+3K0QO62ijO09mHk=; b=i0/BJX68jw5fvqFAFznGzCP2x4q/fWkWgDIaIRUz3yXFbmHjJud6N4zQsjlrO7w6byVRlZ Jyxd/X6aSh1zJaY9//zTSuWcj5WO0gNw/1R++VhwCya/pYfliTFFVRtIbtPi0hwveennCS 7QOVwNikKqOxe8GESDtWFC9k7VxhR+g= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-xW2t0AHIM5u8hJR_STtyOw-1; Wed, 10 Jan 2024 10:55:05 -0500 X-MC-Unique: xW2t0AHIM5u8hJR_STtyOw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40e41740b6eso36925785e9.3 for ; Wed, 10 Jan 2024 07:55:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902103; x=1705506903; 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=P6xHVkkzcRE7FDvy1s+fIkbme0s+3K0QO62ijO09mHk=; b=GycI0TTqwCgrwZiPiV5ecz2AFAOiNCFiT8TyaW45CPj5fR2orjrep/AdaeQamOpfMF hNAawI7hI7ErSooK/HQ2bktwQ2CbXLMHPPrKSCZ4oQbSGSeLBJVyFJLK07mo1Yr86xZM pjOJEXh7oUpRyrzEIJwhUZWmnSkWzybqoc3JYDGPidV8olXLKvaAgeNfob6NeXIqlSFC SUbR07+oqnwCetCfFKb84NhS53o3PpkMR5DMrpBX2LZN+xWaCzORX4poYYVa1mM12GXb EvK1KcRjBWvFbivciOxh1ECSBGh7OCRWihYqP+ZKvuHbCErFbTpkT8zHvg1FzxfWJZww vgJA== X-Gm-Message-State: AOJu0Yz7CkVSTRSy9+iUYZn0BK2keCd7or2tIg8TpL1SktqXDRWhzDXA 4O9fd2KUEMW59uXV54VPHeHPXNs4UoRh3pKRN0DiiXQBSNQnzfj6o0fPufZpWNom3m3TUDKaHki JDWvUQRk88TqFinRBuXBN2WKMX22YeXl2usi6L8o03vQ2g1JsIUyhHi7HW2JoQYAA/+CNh35sKg UQ3PqIes7Uv79Gsw== X-Received: by 2002:a7b:ca45:0:b0:40e:370e:1d89 with SMTP id m5-20020a7bca45000000b0040e370e1d89mr713323wml.36.1704902102962; Wed, 10 Jan 2024 07:55:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IG5Oe2njUgljg3pG8qNTdPb/RoLl0/+P1FyHVN+uK2+TGGjF/PQOijS6XYdDZatqw3dfUvvLg== X-Received: by 2002:a7b:ca45:0:b0:40e:370e:1d89 with SMTP id m5-20020a7bca45000000b0040e370e1d89mr713319wml.36.1704902102692; Wed, 10 Jan 2024 07:55:02 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id ek10-20020a05600c3eca00b0040d81ca11casm2546383wmb.28.2024.01.10.07.55.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:55:01 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 8/8] gdb/doc: update examples in gdb.Progspace and gdb.Objfile docs Date: Wed, 10 Jan 2024 15:54:45 +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 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 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 updates the Python example code in the gdb.Progspace and gdb.Objfile sections of the docs. Changes made: 1. Use @value{GDBP} for the GDB prompt rather than hard-coding (gdB), 2. Use @group...@end group to split the example code into unbreakable chunks, and 3. Add parenthesis to the Python print() calls in the examples. In Python 2 it was OK to drop the parenthesis, but now GDB is Python 3 only, example code should include the parenthesis. --- gdb/doc/python.texi | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 40803274a27..0b240c76dce 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -5416,13 +5416,16 @@ will be loaded. @smallexample -(gdb) python +(@value{GDBP}) python +@group def clear_objfiles_handler(event): event.progspace.expensive_computation = None def expensive(symbol): """A mock routine to perform an "expensive" computation on symbol.""" print ("Computing the answer to the ultimate question ...") return 42 +@end group +@group def new_objfile_handler(event): objfile = event.new_objfile progspace = objfile.progspace @@ -5437,15 +5440,18 @@ gdb.events.clear_objfiles.connect(clear_objfiles_handler) gdb.events.new_objfile.connect(new_objfile_handler) end -(gdb) file /tmp/hello +@end group +@group +(@value{GDBP}) file /tmp/hello Reading symbols from /tmp/hello... Computing the answer to the ultimate question ... -(gdb) python print gdb.current_progspace().expensive_computation +(@value{GDBP}) python print(gdb.current_progspace().expensive_computation) 42 -(gdb) run +(@value{GDBP}) run Starting program: /tmp/hello Hello. [Inferior 1 (process 4242) exited normally] +@end group @end smallexample @node Objfiles In Python @@ -5576,17 +5582,21 @@ loaded the objfile. @smallexample -(gdb) python +@group +(@value{GDBP}) python import datetime def new_objfile_handler(event): # Set the time_loaded attribute of the new objfile. event.new_objfile.time_loaded = datetime.datetime.today() gdb.events.new_objfile.connect(new_objfile_handler) end -(gdb) file ./hello +@end group +@group +(@value{GDBP}) file ./hello Reading symbols from ./hello... -(gdb) python print gdb.objfiles()[0].time_loaded +(@value{GDBP}) python print(gdb.objfiles()[0].time_loaded) 2014-10-09 11:41:36.770345 +@end group @end smallexample A @code{gdb.Objfile} object has the following methods: