From patchwork Fri Jan 5 11:48:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83391 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 60474385E00B for ; Fri, 5 Jan 2024 11:49:16 +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 539A33857BB8 for ; Fri, 5 Jan 2024 11:48:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 539A33857BB8 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 539A33857BB8 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=1704455326; cv=none; b=BGrkvmfQw5Rr9D5AuzFkVz8D5mzOx6szUtZTNcvaPT/Lit8hQIjtqZXVs+hKW2o3oFEPa9SHJIiuXE+tmpflKhJumK3SKSyJzKYhcD6JM6C14o+fmKTEqB7AVxxusTKIUxrn4S3RjwS4+FyzmuSO1b+0RNoMQUji9f88kEYVpYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455326; c=relaxed/simple; bh=FI7QZDgu6pNE26JlnfshnE22nDUZ6mIIM67WHnU0wjI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Pr73Rw7mY6VrzfDGu7U5WiNMRbMmdJMQUJkJsD2F+VwNEjsIBYiNWVKTT8HC+fMoxtD4F8GFg5qoa9aGTNcWWhs2HTxhZnxMzNQnwrwL5bKGuSL9td7zma2A6vwO23cZZUtJd4elQoj9CmZ1zvyjMaCVjRE0hTXI6bk0g1zxbII= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455324; 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=Ff8QhmoKBj5+cp0RiaU13q/rdCcuQnFv2lKe/cvU9577OZRkQ78fczQ22sQ3aXgy33DCmz XtIUU6cZ9ta25ghhB9pNnOZ8POAPwNMKdqA3JRWdHg3/kAcai/3EN/h/Zmsd7CdO9z5zsY 5EytAN3ij1p3V40fp+w4P9mJsnwewbA= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-g98brvMqMZmjOVQzOH7Q1w-1; Fri, 05 Jan 2024 06:48:42 -0500 X-MC-Unique: g98brvMqMZmjOVQzOH7Q1w-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a28b9b87013so51027566b.0 for ; Fri, 05 Jan 2024 03:48:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455321; x=1705060121; 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=FCeqGtV8lLua+kaz//K9uAC0lWo8QRctTuZKHWucu3XoAKOpAOXoedCvgarLl9DdxX 9n5qh5Iymm7cC/8QURxk5WdUoSD4jvnXJ4iaTMTEszReX3RviS4WPRNk0UrAgCkuPCJ9 fY/kr087ZAE1Vyvvzuv5tq0P9mGlHnwH9vY8jbDddjREyEGQ40uFVfgYvvuZdwpAt/o9 4UniiMWJ72KWXk1gCmXOvfUt45p/qF4S50fm7frjWRyb5o/pjbW4goNpD/IT6O8Ktuvg UGtmnIMoEf+LwFtOBgKkW9cnK23gdWKpIz48HnI8J7YLkss5qbSW97xSvH+NWkD8YTas hFkw== X-Gm-Message-State: AOJu0YxW85G1pFqGhxagUCHAKOmMGrudt7nVxNTHUV+W0krugiFcGFw+ j50PM6wGUAxe5PO54cmrflmungAdmgHc7/1EgRiOIbYKOBfX8Qr08sYTnuWzoDoXGE/XWMlfTgI BHyWoW88LaIKVPow2Yed8qTm/21vN/roOReRfasZ2LewpVTKRwBYodR211991dWdvdBrHvVQ9Ig PXBpQvOuVaRFAtQQ== X-Received: by 2002:a17:907:9714:b0:a1e:3a14:e84b with SMTP id jg20-20020a170907971400b00a1e3a14e84bmr2214767ejc.44.1704455320664; Fri, 05 Jan 2024 03:48:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnLWJGHPP3W/eAk7mHlacUc1PFhQrvKSyh6liNAFP4qMfTdb6yQEO/YlWyriBhC8pCP0K2xg== X-Received: by 2002:a17:907:9714:b0:a1e:3a14:e84b with SMTP id jg20-20020a170907971400b00a1e3a14e84bmr2214758ejc.44.1704455320165; Fri, 05 Jan 2024 03:48:40 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id b7-20020a1709063f8700b00a290920c78csm791795ejj.187.2024.01.05.03.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:39 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/6] gdb/python: hoist common invalid object repr code into py-utils.c Date: Fri, 5 Jan 2024 11:48:30 +0000 Message-Id: <791370ed269cee0ce3dc30f0a01307c00473878a.1704455158.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.4 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 Fri Jan 5 11:48:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83392 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 2182D385E005 for ; Fri, 5 Jan 2024 11:49:43 +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 69FD83857C49 for ; Fri, 5 Jan 2024 11:48:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 69FD83857C49 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 69FD83857C49 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=1704455328; cv=none; b=KmkS2INSduDHz/scutWFSYOyZ8KoKV3UfkD3+ErRjMss1YBEVZj89YODpUgsLwFlvvdLvaameK71taA6oXrbB8bzTLlrEug8uxeyQgR4k48iZ61sdR21UAFCymIPze40s2T64ePNwHvQt9Grt7NQ8Zg3qbdiKcPG1ibIuCI2iSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455328; c=relaxed/simple; bh=EuzstJi/SQudauaNeVnW6Z84FCcaipa77ZcpvAo+Ako=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=iBzLAoY8/mGLKYBK8GGv8IXndJt6/eiq/CmaFnldhKD5aytf9mW0IgdFZxYhRSJoyS4zRZNbF6kUIhvR5tkaPNqlxOeoyp3OGrc08gRy3oyg1NwGsC3Doitl/g+j1IDmn9qL4k4YxSFimKENsh5dfe8uFPE+zDyIG/vHwHjiwGs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455326; 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=jHP0C4zJXHWPwCnGB4umdwPazHNtW0tq+/3MPZcjzKKyQNZnTqp7QqwX7Eznd+1zCja19p YhgR6oR20LDBMvn8LtoZjc70FxekMRxQmskQtIM6RGPpZmpSvq5CPuVWlmAXfeIAsZUs5p D7iaaZRi3IsYxnf0GLsAgCXPUlJ7b1E= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-630-9YDXxYVXNIqx6EkpvpWpPg-1; Fri, 05 Jan 2024 06:48:45 -0500 X-MC-Unique: 9YDXxYVXNIqx6EkpvpWpPg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a28f6a78b83so52390666b.1 for ; Fri, 05 Jan 2024 03:48:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455323; x=1705060123; 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=v/o/Vg41Mh9FYYGPxsBW2DbtWtGw3UN+t3V5Kvc22HzWNl7Hr3IJ3hsbpT442KrCC1 U/C3/7/Bh3SDHkzufXcBwob/mGT4Kxtqr1BpBp//hu/gkCsV9UvHBfZLw+/zxaZwqsuc ZLgj//jgU9gZedoqBM+GLim+aQKkC5B3DeiQ1wcBe7HrrDXlJB8/SEgVkOpWWQO/4rKu tQA6UkIGLMM7IKobDCP8LoL+umdHP+2Sbo+0jCYH+Dl8UOqqbucQRYvS0Sw+u2B9NkMj OhIoIITML7mcaOYrYKMPJD2ngAOWuvROamo1fpWTGqRIBi9BubCSb46sZBLcY6+jNGAj GMMw== X-Gm-Message-State: AOJu0YxRlghoSWoBwhnKnzwgGk+0YSFnI+cFkGfKNRQeLmtkwD53/LIk 5poRhMAn3rrVF/r/EP8bMjOSlQUjp5H4GAp1eTQg5rh9/230U4iXIq9cB2OGUj/v9/Mx4jGwV5r AVx93LIja/aO02k/hh5IRam+pt1e78hQsfVeP7zG9GD7/iw7+uPnbKx4G3bVq0DFa9sDA2KT4ij n/L0lu9D38zamlEg== X-Received: by 2002:a17:906:1dd:b0:9cc:450c:b0d5 with SMTP id 29-20020a17090601dd00b009cc450cb0d5mr970281ejj.4.1704455322864; Fri, 05 Jan 2024 03:48:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUbYnxbtxUY7MdKCIfotZrb3frNRn6GdA+UuS8bvYCmjeykOOv6W0aPb8pzJ2ACXNhWGH1tA== X-Received: by 2002:a17:906:1dd:b0:9cc:450c:b0d5 with SMTP id 29-20020a17090601dd00b009cc450cb0d5mr970275ejj.4.1704455322369; Fri, 05 Jan 2024 03:48:42 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id q13-20020a170906770d00b00a2697984204sm797206ejm.72.2024.01.05.03.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:40 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/6] gdb/python: add gdb.InferiorThread.__repr__() method Date: Fri, 5 Jan 2024 11:48:31 +0000 Message-Id: <37fbaf0a27d3938f3107be2ab79dac3e748ccff6.1704455158.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.4 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 Fri Jan 5 11:48:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83397 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 DC9FD3857C64 for ; Fri, 5 Jan 2024 11:51:34 +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 D56DA3857C60 for ; Fri, 5 Jan 2024 11:48:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D56DA3857C60 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 D56DA3857C60 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=1704455336; cv=none; b=DpQfSF2IXKfroo3vWDqdqpGRGgJMDc5ddW878rq7Ip1mcaLdfh6Z1uie+Flzwef/Z9aItzLgQgwueYqwQqwgI7nbYQr+TPhpUtKIYaorfDA6tJSy8DKW83EhsUw2NyaqOSGS6JF+ghx4lggkmrK/inVz5ilE/OBCdmsPk9MHwys= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455336; c=relaxed/simple; bh=VbrDf+cllGINFkyjU8HJmxaLG3f140bpf9hHU1SXWiM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=p+tSNVgwoDC0yIliAf1ynpW/Z22cfCt/OKFek6A3FczLbikRkudz9Snh+kQb8OwDDxNBXZ6K2HW1p51AG8L4q/6KRO/2DFrrTd8mAfwoIL/35Y0NE3cOLRkK6eNRRYF194EzXU2WJi7418icXjo6CzlxBm19ra5epDg7QXRGecs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455333; 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=Z+lWJTfd7tuuMBozuhrLSRmdLuJOIYjy3KT9lAdGCyChQXn9eFO8r6wsu+16fbqMDE7mcB 1wdoNr9EO0Eiz3YYYl5+KbmDqD9cO1QDvt6Wb/2kIiH9jZnq4N7C5tmAX+D8ihB4C5cgoM HvodsmkqK1bdWDkSNzqwMBCRALw2C3g= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-655-0bV64f_dO9ifnW-9kAYL5Q-1; Fri, 05 Jan 2024 06:48:47 -0500 X-MC-Unique: 0bV64f_dO9ifnW-9kAYL5Q-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-54554ea191bso787303a12.2 for ; Fri, 05 Jan 2024 03:48:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455325; x=1705060125; 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=JmRS9XtElGoeI90kB0apCe2KxdnhDTaQwdJrTpseGf9yXYE95cmEj4QYCNtfdtGJHC pDdXe7/9G/kwXPnILdbAaLvOW0o3XGLI/SlenrryC3egB+M1nk9iPGrKY/TtmBOKGkZA YZ+rDy1ANk+Ll5A1q4OsTo/5M1taMZvK1TCb7D/T80e7xsrh4N8mnNFUjXm/9x//T4BD Agn/4Gz9DzZZp8rhpN6T7JK6PCZX8KgNA8LUQo70V9HcjHqAfU+1rrd1ZPBDUTmog7ia LuV+xSSKcQPDzLXSPfq2mvRhKY7vH1Cz082mm+BWplPLVMixmHZllLPhnr8ILq5MGp65 BGLA== X-Gm-Message-State: AOJu0YxWeLpbDma+1cLduwJYsEW4AdGIC0Xv/vHs9mSyo12ij9jGtHVc VrHSYLnXJa992W2XyDYNezCyTVzXbOxnmNSb61F670ezPxOb69ji+KjDt+0Nw5pYRc8z7saneiz m0PZ5HvbDT/2AmhIUEMSR9Va5cK9KWWGqp0xV1GoWFFb8T9EEJKi+4+2/MxqUpmhTS25S/3bDmv 3SOjYkPxmBr6MHDQ== X-Received: by 2002:a50:fb98:0:b0:54f:5017:e54b with SMTP id e24-20020a50fb98000000b0054f5017e54bmr1156820edq.34.1704455325152; Fri, 05 Jan 2024 03:48:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCaW3IjZW0dy4r1KBksRuUthR0LKZsjLqywl3CLmIciqfY+suX42Jky+Z6P44lgQinZ3ciiw== X-Received: by 2002:a50:fb98:0:b0:54f:5017:e54b with SMTP id e24-20020a50fb98000000b0054f5017e54bmr1156814edq.34.1704455324779; Fri, 05 Jan 2024 03:48:44 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id dy17-20020a05640231f100b005528a899fccsm881684edb.37.2024.01.05.03.48.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:43 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/6] gdb/python: add gdb.Frame.__repr__() method Date: Fri, 5 Jan 2024 11:48:32 +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.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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 Fri Jan 5 11:48:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83394 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 18E8B385DC05 for ; Fri, 5 Jan 2024 11:49:57 +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 1810E385DC03 for ; Fri, 5 Jan 2024 11:48:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1810E385DC03 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 1810E385DC03 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=1704455331; cv=none; b=tzo9H8TDIseywr4l87VSz7CRihrgfnrCJcFAjLuyMAEz3B5NBC/2WRL6Zgf7IuCeRy731n+Y+fAqe387eteZnIIDQQFT0mbBdrwl4bZN2n28+SHSTQpjoLcuQ3b0dL7D/wWHVS7kc2wrZNN+cz7+9/KTCXZgQvdzyNGbU8noj9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455331; c=relaxed/simple; bh=kd5HBSqji9ep0geepKuVhxdGsHzfBPAQZIn6knz6zdM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gtbL1anaJuKsoIgn85dBCkPYX3vLMdtd8A8ZJahbdVI+vSB/LdmYfi14K/XqFpOQkBN86Sf+mB1HtgQDEWP00CSfWu5n5ImaOJNt1aw7aZdkT466mGadCu3sUpnG5kww2jRsGUTB94hKlXEHewxxTuv/Lvo8PStRQDUVsYyYyB4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455329; 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=OvgdxGaYyhQU5eeq0gi3nBgC4K0280jDESofvQbbR8o=; b=VF+i0BqTcJOftfIbqhNRjBCdAkkS70Xkqdj+JlVpBzRaECxa8btfrOKnhwjFOwHMpFKB3U 1Xk1VAjF/c0EuH1xrerNvJS3SrTfYVgIxLHjiJaKv1fDKYfMvZwTJfGSyxzv4171qJltGE lMhudv/RzX+VtUAKQCvcfrpEPcM8bak= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-28-UCPj6DpvONOo1m2eVSlVDg-1; Fri, 05 Jan 2024 06:48:48 -0500 X-MC-Unique: UCPj6DpvONOo1m2eVSlVDg-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a27eddc1c27so83289966b.3 for ; Fri, 05 Jan 2024 03:48:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455327; x=1705060127; 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=OvgdxGaYyhQU5eeq0gi3nBgC4K0280jDESofvQbbR8o=; b=SLmGY/3kTbNMW5A/KD7PdVHCgFo789sCnr4EIlS/9RAVEWj8Ira9l5x/f/jtfyrQdd l21OCCDRrTx1vYLYrs+ZSNXQOR+kJvcTh/9UkR6wory+58WmyuUtzdASYgZheVVeBQv7 /Q12jCj0n12+X5yZzsStb8kffWT/1fejyiqYAvUn2E3kBm4HmlnI17BO+lg5I8EMTjnT vmK0HS+HkplP3im/gOOUUKDiN6xzlAOL9hoDd80G6JYMuJw3NQKTk+cHb4vdvk7rs8LW TuBU/lqWeL7upjyJHWRnCzOMnQtXmrqS2rOPDXR8Aea46WiIPLoKM9kYv9/94K39NRUN LEbw== X-Gm-Message-State: AOJu0YzMZDFD2Lax+SXfAmD6hVtv5NN7m3L3jp9U8FrkTou+N7tanEqS 3rCFpOD7BZVyYJCjvxNjrlh5CUUWLFdSjuoSvwXr/DBwa6/PhtnJZqlEVJaVXMbYOVzCvu/wysE h4q2Vc3UcAo738GOJkIFVuCOynhO8cG/ubiz7bCipCtOj4hWVmCMjmQ6XGoHXRsRHqtLAYjXqCZ ojPfnme1qNh0pIQQ== X-Received: by 2002:a17:906:3851:b0:a29:9225:8e4b with SMTP id w17-20020a170906385100b00a2992258e4bmr54919ejc.106.1704455326932; Fri, 05 Jan 2024 03:48:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFoSzpoyHiSbvwXsHkhLY5MUIVvLggi+ffgOc1rft6g+vQO0g0zg3fhTvUsfqfcAvdJaSvPWA== X-Received: by 2002:a17:906:3851:b0:a29:9225:8e4b with SMTP id w17-20020a170906385100b00a2992258e4bmr54913ejc.106.1704455326701; Fri, 05 Jan 2024 03:48:46 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id x7-20020a170906440700b00a1d2b0d4500sm786000ejo.168.2024.01.05.03.48.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:45 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 4/6] gdb/python: remove users ability to create gdb.Progspace objects Date: Fri, 5 Jan 2024 11:48:33 +0000 Message-Id: <5d718380fd0499359cbdd909ae8459b314e4c6b8.1704455158.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.4 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 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 Fri Jan 5 11:48:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83396 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 1D1F6385B535 for ; Fri, 5 Jan 2024 11:51:06 +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 999E13857013 for ; Fri, 5 Jan 2024 11:48:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 999E13857013 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 999E13857013 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=1704455335; cv=none; b=oGgbcfZYo7llf5UbsKW3jP6N8szwb/Z0ZFq/5X8gRIJMrEJOCd1+/EhfccLdsXc0hBi9VhgyhTDKAnXKipHDFwD5Q2ex8glPQNlayM8uKadqnbhvwtyGz1DWVyA09oOybztWiZGFtQ7YLR1sjnDC5zhe8UvIabVp+7nNSLweWFA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455335; c=relaxed/simple; bh=DV4Dq0y6kSMhBLBc2Y5pmg1oKIQPFbFphHIszjqiRHM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=wf7uQ8wi09Y/Dxe0a/GtGZNH/6afjb1D7yKP/GMq1SXL39LxeWrZ4IRtBl7HHWq9ytc6oQk1a8+KddED30x5Bqlcu0Beqrfq7sTIrns3ptbWnqDCqHaFGSb55cOM6p94ejA6d6zo21zfNX/FtoDel3nVp1HCMdVNw5slViPXIUQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455332; 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=XxSGyzS/240RUKKdO2ui4cR3VRu5V8Wrb2zAJsnBtvE=; b=ErN45NB4YS2kDS1QUUvVNkB+liZpVrWE024XAzn1jecy6TcVPiHv+ET+Xof4TIWdzNh196 /UYydLvfcwNY2dFVo3lMkvUMmzv08knFtbklgznV4YDzGX0aKr9T7TSPnZ/gTgMVg8F7Mh pt1eKy4f4z8v4BynU9QHIm7VMpsZb9M= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-619-zips7KKEMBWnHWDPikhZWg-1; Fri, 05 Jan 2024 06:48:51 -0500 X-MC-Unique: zips7KKEMBWnHWDPikhZWg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a28f6b75283so42333266b.1 for ; Fri, 05 Jan 2024 03:48:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455329; x=1705060129; 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=XxSGyzS/240RUKKdO2ui4cR3VRu5V8Wrb2zAJsnBtvE=; b=EihOSB1JMbSbcA7b0Wzmi5tg0ihSdQTtMcpv70xwI5IB6AdZ3NYZWYNL179wOIbwnc Z54FxlYYcZrMsV0rCj3GzFgnMTO+hrpcOyXu4iWaK5kQCj3fNjFj1YpO2XJiIu2EhNZZ pU1W2G7WJkRKGbjFjiTw6+3pJH8mlh7pa7d3/HMAyX2ENlsjCt8UdWOgO3vOpAfrobX2 /6L3qJXvrUzxfbe2Vh1fezWM230NUMdSUIBWJfl5PQZC3qzBA3JNgeLrU2wuO9GZ3TeD bbUAj4fctzvtDNIYFyq3nci8lbg6j9f5oboCoIrj9s1fV6xNlrwBdilbsuhMxiq+nUuF lIWA== X-Gm-Message-State: AOJu0YxBTaZpZAmorJo8Q3Jm04NuM1Gn3PJ5/d7ju0XJjiquitSQRRNE gocK1M1rSvc6e9wJmKLrwFF706BcNp+eBYQ+hKY54y1AArEq9K3ParunlGEbOv2ifp7VmKPVVC0 s+lZZzzFaql1kOCyHAR+CwOca62khwqkMLjQz7ZdZtj7cL2pbjk044Vk4DgU05A7LNPbbNTtdH0 Z0pEW/5S0FO7BGvQ== X-Received: by 2002:a17:906:f80a:b0:a19:a19b:78a2 with SMTP id kh10-20020a170906f80a00b00a19a19b78a2mr1025868ejb.101.1704455329207; Fri, 05 Jan 2024 03:48:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFCh4bUpUtOeQiOUYk6fOYBBcgKu5FbI8O6rBlObZ+wilFnq2sHVuIMdCiCXfzh5npSqy/U9Q== X-Received: by 2002:a17:906:f80a:b0:a19:a19b:78a2 with SMTP id kh10-20020a170906f80a00b00a19a19b78a2mr1025863ejb.101.1704455328835; Fri, 05 Jan 2024 03:48:48 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id vr3-20020a170906bfe300b00a26e53be089sm799390ejb.44.2024.01.05.03.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:47 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 5/6] gdb/python: Add gdb.Inferior.__dict__ attribute Date: Fri, 5 Jan 2024 11:48:34 +0000 Message-Id: <6eb215cd5252a70326d1ec5bd9c7d96e9a6d20e5.1704455158.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.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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 | 32 ++++++++++++++++++++++++ gdb/python/py-inferior.c | 13 +++++++++- gdb/testsuite/gdb.python/py-inferior.exp | 19 ++++++++++++++ 4 files changed, 67 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..721f0100178 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3667,6 +3667,38 @@ 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 +(gdb) 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) +(gdb) file /tmp/hello +Reading symbols from /tmp/hello... +(gdb) 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"); +(gdb) python print(gdb.selected_inferior()._last_stop_time) +2024-01-04 14:48:41.347036 +@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 Fri Jan 5 11:48:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83395 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 6CE12385C413 for ; Fri, 5 Jan 2024 11:50:49 +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 82848385773F for ; Fri, 5 Jan 2024 11:48:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 82848385773F 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 82848385773F 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=1704455342; cv=none; b=X8BUACbElu8oiINb/WKbs9my9Z3o5QmRBkLtG+58AqjCQOHS3qmyyQ2RHqcpOPPDP80AkRtFs3UBgowVtY3dCLTtp668VxdQGq+IEb/pVzGZ0g+poau+4b/3eSwpzNHolQfbZOmWKrOrZbZp+OteXY3SQIfT95QrVK7R7L2DNsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455342; c=relaxed/simple; bh=I3NiweyDJ6ujHWyYvR5zFJsVtvEo9RsuhoI+KZ+cdfM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Uuh1OG0gdITg0jVcxhySDAEtIF8Hv2toUOpFPxEEFsHTzRlFb5rAiKndOHd4l3AxQ2BcMGuL0vIZ3OuOI8pYzHPnzlNbExpUXl7GFnN4t06/l2MnhrwH199M2ox5gz/AAmQ/NaOR+8uvmydXoKMg+kIS9Em8Zh8MsbCFkWgCBx4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455339; 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=w6nzeiYdwUWe8jFBRaSup9KdOjCmvB104h73GOz+nWA=; b=YTUy/igZNBLZyoP0bd4x4vZp9cyRey77Z/Rdqh/U2zSrA0fo6jdyH/vWQZHmBEeMbey4U8 2eSlaWw+JKbSBlnXLIhOSMe6yNRVKm7d6RsCvWERvR/faD7+z/9EaCsBQLM6kdAxfVO5k9 LYFIU9SSlxtHf6Ps5weRmkKEdNAFGdQ= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-490-99LsQJOrOlexOUANjhkDgA-1; Fri, 05 Jan 2024 06:48:52 -0500 X-MC-Unique: 99LsQJOrOlexOUANjhkDgA-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a28d6f071b2so57213566b.0 for ; Fri, 05 Jan 2024 03:48:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455331; x=1705060131; 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=w6nzeiYdwUWe8jFBRaSup9KdOjCmvB104h73GOz+nWA=; b=i9bp487AGyu8JuwqlEORfgfg3Qp89mW1hmqxqwAhoHB0MNED5j/lhcojVslPobPa7z D9p5iUN6kxrwEsIZlRcUlKrd1qfBKqrRnef9NKK8gc4plONpT4siXf8HJCxyRag7SL/+ EsW6aabnrgTq07dWifNYlVsNMm+rLzZ7gznGBoC622MJFSappvwAAjfua2KuCvCApC+C 5R0PSeA+g0OiX/70pDbR4rKCESyJEsaFg98/kZoZSQF9aXLOp+Z8zUua9KvJzfY21l39 sU4TZMC7msExYd74SkypU45br6a+PsI3AiJEb9lsd5WvPWKYWbM5y8YswlAoJrb6wzlF RaaQ== X-Gm-Message-State: AOJu0YxQFokNXv15L+gSMRc18d5d1kpMhL2VFg3kBJtU/OQ5YVuBoGhn rcgQI3nSkfVjGH/PCxUl9wHg6KIwZvrfXxlAHLg8K2GVh5Z+qfKPLIjsbF0T6lfet5wtwfjMqxQ WA780xrHU9xp9MZpoJRBziL34h0owUWQS3Dgmktm3cUs8UIFQDlMDk2MfUdNsipBjXzcnj1LshD 7EawETKM6dUGHXrw== X-Received: by 2002:a17:906:185:b0:a28:fdc0:e704 with SMTP id 5-20020a170906018500b00a28fdc0e704mr755913ejb.53.1704455330889; Fri, 05 Jan 2024 03:48:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IFtFB0jGwk/DnewKAw+B8E1572RF2wOg1OZnaEUZYJ9ldAoUIe62TDWSylci9caoTEIFjQxRQ== X-Received: by 2002:a17:906:185:b0:a28:fdc0:e704 with SMTP id 5-20020a170906018500b00a28fdc0e704mr755905ejb.53.1704455330458; Fri, 05 Jan 2024 03:48:50 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id v14-20020a1709060b4e00b00a27ac0895ecsm798273ejg.9.2024.01.05.03.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:49 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 6/6] gdb/python: Add gdb.InferiorThread.__dict__ attribute Date: Fri, 5 Jan 2024 11:48:35 +0000 Message-Id: <6fdf97c5f147a7e71fc0ca4de9e49961c067bae1.1704455158.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.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_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 | 31 ++++++++++++++++++++++++ gdb/python/py-infthread.c | 17 +++++++++++-- gdb/python/python-internal.h | 4 +++ gdb/testsuite/gdb.python/py-inferior.exp | 17 +++++++++++++ 5 files changed, 71 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 721f0100178..ecba7cfa89c 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4173,6 +4173,37 @@ 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 +(gdb) 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) +(gdb) file /tmp/hello +Reading symbols from /tmp/hello... +(gdb) 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"); +(gdb) python print(gdb.selected_thread()._last_stop_time) +2024-01-04 14:48:41.347036 +@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..fa24ef3b9ca 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -49,6 +49,10 @@ create_thread_object (struct thread_info *tp) if (thread_obj == NULL) return NULL; + thread_obj->dict = PyDict_New (); + if (thread_obj->dict == nullptr) + return nullptr; + thread_obj->thread = tp; thread_obj->inf_obj = (PyObject *) inf_obj.release (); @@ -58,7 +62,14 @@ 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); + gdb_assert (thr_obj->dict != nullptr); + + Py_DECREF (thr_obj->inf_obj); + Py_DECREF (thr_obj->dict); + Py_TYPE (self)->tp_free (self); } @@ -394,6 +405,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 +484,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')" \