From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23099 invoked by alias); 12 Sep 2018 22:50:29 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 22394 invoked by uid 89); 12 Sep 2018 22:50:29 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: sesbmg23.ericsson.net Received: from sesbmg23.ericsson.net (HELO sesbmg23.ericsson.net) (193.180.251.37) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 12 Sep 2018 22:50:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/simple; q=dns/txt; i=@ericsson.com; t=1536792624; h=From:Sender:Reply-To:Subject:Date:Message-Id:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=lRn71NtGNApF+R49AkSu8QnHyhWoAkkYg4ai040Kc7U=; b=Cz9NijU4WVa8c0zCCgHYtyAeD6leHyfQa2Wj7lRzkqc5Tq03pG6sb6jHB+z9nTS8 jzL0i7tvRhh6jZf74cLZAu9a+KEpjM1n/DE9VU3ErU9U4ll7elktIguJEir1J5VM HyjLG7iXsd3jVHtE43zKzYBCkFW0Q8zzmM1CQ2dUGU4=; Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sesbmg23.ericsson.net (Symantec Mail Security) with SMTP id 43.BA.05037.F28999B5; Thu, 13 Sep 2018 00:50:23 +0200 (CEST) Received: from ESESSMB505.ericsson.se (153.88.183.166) by ESESSMB501.ericsson.se (153.88.183.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Thu, 13 Sep 2018 00:50:23 +0200 Received: from NAM01-BN3-obe.outbound.protection.outlook.com (153.88.183.157) by ESESSMB505.ericsson.se (153.88.183.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3 via Frontend Transport; Thu, 13 Sep 2018 00:50:23 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=l2R5A7qLdiQ48qnZEgd/zx8FVJxV5TW3I3J9Fpr7d80=; b=GfnGVJqDjM3ZnzowBeYSi62X/ZIpLmHO8RvUhaYV4We9yggKwaMhgO4pnhv1CvZCKW9xmeC+ZTcN6Ntoo+JixByxt8GaQ1ZqyIH/lj4BFr3ClK3L7w6l+0eH5fCvBhUDQVfioZlOTrS4IoyOJ4iLltwunbwITgHG/gSnLvGaIB8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; Received: from elxacz23q12.ericsson.se (192.176.1.81) by BN7PR15MB2386.namprd15.prod.outlook.com (2603:10b6:406:8c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1122.17; Wed, 12 Sep 2018 22:50:19 +0000 From: Simon Marchi To: CC: Simon Marchi Subject: [PATCH v2, doc] python: Provide textual representation for Inferior and Objfile Date: Wed, 12 Sep 2018 22:50:00 -0000 Message-Id: <20180912225001.8701-1-simon.marchi@ericsson.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain Return-Path: simon.marchi@ericsson.com Received-SPF: None (protection.outlook.com: ericsson.com does not designate permitted sender hosts) X-IsSubscribed: yes X-SW-Source: 2018-09/txt/msg00385.txt.bz2 [New in v2: the doc part] Printing a GDB Python object is notoriously not helpful: >>> print(gdb.selected_inferior()) >>> print(gdb.objfiles()) [] This makes printing debug traces more difficult than it should be. This patch provides some repr() implementation for these two types (more to come if people agree with the idea, but I want to test the water first). Here's the same example as above, but with this patch: >>> print(gdb.selected_inferior()) >>> print(gdb.objfiles()) [] I implemented repr rather than str, because when printing a list (or another container I suppose), Python calls the repr method of the elements. This is useful when printing a list of inferiors or objfiles. The print(gdb.objfiles()) above would not have worked if I had implemented str. I found this post useful to understand the difference between repr and str: https://stackoverflow.com/questions/1436703/difference-between-str-and-repr gdb/ChangeLog: * python/py-inferior.c (infpy_repr): New. (inferior_object_type): Register infpy_repr. * python/py-objfile.c (objfpy_repr): New. (objfile_object_type): Register objfpy_repr. gdb/testsuite/ChangeLog: * gdb.python/py-inferior.exp: Test repr() of gdb.Inferior. * gdb.python/py-objfile.exp: Test repr() of gdb.Objfile. * gdb.python/py-symtab.exp: Update test printing an objfile. gdb/doc/ChangeLog: * python.texi (Basic Python): Mention the string representation of GDB Python objects. --- gdb/doc/python.texi | 4 ++++ gdb/python/py-inferior.c | 17 ++++++++++++++++- gdb/python/py-objfile.c | 17 ++++++++++++++++- gdb/testsuite/gdb.python/py-inferior.exp | 14 +++++++++++++- gdb/testsuite/gdb.python/py-objfile.exp | 7 +++++++ gdb/testsuite/gdb.python/py-symtab.exp | 2 +- 6 files changed, 57 insertions(+), 4 deletions(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 5e2ab76d2bc..1c70088acea 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -207,6 +207,10 @@ methods and classes added by @value{GDBN} are placed in this module. @value{GDBN} automatically @code{import}s the @code{gdb} module for use in all scripts evaluated by the @code{python} command. +Some types of the @code{gdb} module come with a textual representation +(accessible through @code{repr()} or @code{str()}). These are offered for +debugging purposes only, expect them to change over time. + @findex gdb.PYTHONDIR @defvar gdb.PYTHONDIR A string containing the python directory (@pxref{Python}). diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 1cf37296973..56019bf9e05 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -860,6 +860,21 @@ infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw) return result; } +/* Implement repr() for gdb.Inferior. */ + +static PyObject * +infpy_repr (PyObject *obj) +{ + inferior_object *self = (inferior_object *) obj; + inferior *inf = self->inferior; + + if (inf == nullptr) + return PyString_FromString (""); + + return PyString_FromFormat ("", + inf->num, inf->pid); +} + static void infpy_dealloc (PyObject *obj) @@ -991,7 +1006,7 @@ PyTypeObject inferior_object_type = 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - 0, /* tp_repr */ + infpy_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index c2b40ff5352..61d3a158198 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -456,6 +456,21 @@ objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } +/* Implement repr() for gdb.Objfile. */ + +static PyObject * +objfpy_repr (PyObject *self_) +{ + objfile_object *self = (objfile_object *) self_; + objfile *obj = self->objfile; + + if (obj == nullptr) + return PyString_FromString (""); + + return PyString_FromFormat ("", + objfile_filename (obj)); +} + /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it. Return non-zero if STRING is a potentially valid build id. */ @@ -709,7 +724,7 @@ PyTypeObject objfile_object_type = 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ - 0, /*tp_repr*/ + objfpy_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 055dd87c6d8..077279f2f54 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -45,7 +45,7 @@ if ![runto_main] then { # Test basic gdb.Inferior attributes and methods. gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1 -gdb_test "python print (inferiors)" "\\(,\\)" "verify inferiors list" +gdb_test "python print (inferiors)" "\\(,\\)" "verify inferiors list" gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0 gdb_test "python print ('result = %s' % (i0 == inferiors\[0\]))" " = True" "test equality comparison (true)" @@ -279,3 +279,15 @@ with_test_prefix "selected_inferior" { gdb_test "inferior 1" ".*" "switch back to first inferior" gdb_test_no_output "remove-inferiors 3" "remove second inferior" } + +# Test repr()/str() +with_test_prefix "__repr__" { + gdb_test "add-inferior" "Added inferior 4" "add inferior 4" + gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1 + gdb_test "python print (infs\[0\])" "" + gdb_test "python print (infs)" "\\\(, \\\)" \ + "print all inferiors 1" + gdb_test_no_output "remove-inferiors 4" + gdb_test "python print (infs)" "\\\(, \\\)" \ + "print all inferiors 2" +} diff --git a/gdb/testsuite/gdb.python/py-objfile.exp b/gdb/testsuite/gdb.python/py-objfile.exp index 6e81750cfe9..240de29efd1 100644 --- a/gdb/testsuite/gdb.python/py-objfile.exp +++ b/gdb/testsuite/gdb.python/py-objfile.exp @@ -45,6 +45,8 @@ gdb_test "python print (objfile.filename)" "${testfile}" \ gdb_test "python print (objfile.username)" "${testfile}" \ "Get objfile user name" +gdb_test "python print (objfile)" "" + gdb_test_no_output "python dir(objfile)" gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").filename)" \ @@ -149,3 +151,8 @@ if [remote_file host exists "${symlink_binary}"] { gdb_test "python print (gdb.lookup_objfile (\"${symlink_binary}\").filename)" \ "${testfile}" "gdb.lookup_objfile of symlinked binary" } + +# Test printing an Objfile object that is no longer valid. +gdb_py_test_silent_cmd "python objfile = gdb.objfiles()\[0\]" "get first objfile" 1 +gdb_file_cmd ${binfile} +gdb_test "python print(objfile)" "" \ No newline at end of file diff --git a/gdb/testsuite/gdb.python/py-symtab.exp b/gdb/testsuite/gdb.python/py-symtab.exp index f504362526f..4083b3b4475 100644 --- a/gdb/testsuite/gdb.python/py-symtab.exp +++ b/gdb/testsuite/gdb.python/py-symtab.exp @@ -66,7 +66,7 @@ gdb_test "python print (sal.is_valid())" "True" "test sal.is_valid" # Test symbol table. gdb_test "python print (symtab.filename)" ".*${py_symbol_c}" "test symtab.filename" -gdb_test "python print (symtab.objfile)" "" "test symtab.objfile" +gdb_test "python print (symtab.objfile)" "" "test symtab.objfile" gdb_test "python print (symtab.fullname())" ".*${full_py_symbol_c}" "test symtab.fullname" gdb_test "python print (symtab.is_valid())" "True" "test symtab.is_valid()" gdb_test "python print (\"qq\" in global_symbols)" "True" "test qq in global symbols" -- 2.19.0