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