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