From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id E75A33858404 for ; Mon, 11 Sep 2023 17:28:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E75A33858404 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-34e25ad38cbso15626255ab.1 for ; Mon, 11 Sep 2023 10:28:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1694453308; x=1695058108; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=UICWBjxFkQ8cZ0WB1vopxrCTTywecT93VfzUuvbIR9M=; b=AMpGQ7Vu3WL2wYNJr4A9L9JOpjhPpoZy+PQlBuvPEioDcYKCSP/BQ035CkzVUz8WA/ QWtoTOve8Kcxa47rk1NKFwtz8kgMdP7mi4jDTY0VxZQFL7tc3qgZxp9YrtvoNy+Lv97g UQlGoCOGWX4hmYb7A45mOb8eA4E8DkEcIvHiC+JsXvzI4wm17JiK0dPOZXJ0cMBZyNpJ TGxFKYQyMs9ml1tlU4C8vsXMQ4bkqe5w04W07tGjloHCjwmMEmYp558A9ZpqdEbnlvJ1 Y15femaKE7C2SicXdMqMlNK3e6LpD5s6n0N/yyLAr3tZt4dzVH6gav8/gCQpVoOkFV4M B3Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694453308; x=1695058108; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UICWBjxFkQ8cZ0WB1vopxrCTTywecT93VfzUuvbIR9M=; b=wJXR7epmowuCLUD8aEZVuvsYXJgm4TvMDetTehNhMXlgydPPnzxquWO/7/Th4EClFE Tby5Q7X+KDSl0e/l+Fh9FbqFBwIn66E1aJ/C5vPBbvbRcbmYJC5nQpEbzMVA11OTQBMi CgAR4lYK1x/bj4iDJFazFONdDsZzHpNpFNbmUgBiNos9yNfO2QZOZUn4NRcTyjUim8Ql G0dP6ds2A3FQL0sDAzVCdD/DSVoSUTXA3YpC5gOSLXxEdAzmmGTbnI34bS2dGhwOvWR8 TXLsczjACtJ++gLVfrXiF5y2uEULE320eC0B/pL5ZOHYaUiY7oJriWR1dnQN+BIxHhUn XCvA== X-Gm-Message-State: AOJu0YwUO7htwFpk0x+vIWzXhViVYg8oefBz01mUj5C60bk93YAB+tG8 MVGN7QurSyA2mXvVJzs0HuWkBw8ST9c/IzSypR8bww== X-Google-Smtp-Source: AGHT+IEDGBWLfN/m9hVXwx7opcGEb50k9t/GcwO4R6g8qZIqZf1S5mOJvqQJ8crLPZyXK71rdWw3yA== X-Received: by 2002:a05:6e02:1186:b0:34b:ae9d:9b38 with SMTP id y6-20020a056e02118600b0034bae9d9b38mr9863481ili.30.1694453308187; Mon, 11 Sep 2023 10:28:28 -0700 (PDT) Received: from localhost.localdomain (71-211-130-31.hlrn.qwest.net. [71.211.130.31]) by smtp.gmail.com with ESMTPSA id j18-20020a056e02015200b0034989674ad7sm2455324ilr.52.2023.09.11.10.28.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 10:28:27 -0700 (PDT) From: Tom Tromey Date: Mon, 11 Sep 2023 11:28:26 -0600 Subject: [PATCH 2/2] Add two new pretty-printer methods MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230911-pp-v2-v1-2-c4e0d40c8b63@adacore.com> References: <20230911-pp-v2-v1-0-c4e0d40c8b63@adacore.com> In-Reply-To: <20230911-pp-v2-v1-0-c4e0d40c8b63@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This adds two new pretty-printer methods, to support random access to children. The methods are implemented for the no-op array printer, and DAP is updated to use this. --- gdb/doc/python.texi | 16 ++++++++++++++++ gdb/python/lib/gdb/dap/varref.py | 22 +++++++++++++++------- gdb/python/lib/gdb/printing.py | 9 ++++++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index d09f5e03997..79438baf477 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -1828,6 +1828,22 @@ are peformed in this method and nothing is printed. If the result is not one of these types, an exception is raised. @end defun +@defun pretty_printer.num_children () +This is not a basic method, so @value{GDBN} will only ever call it for +objects derived from @code{gdb.ValuePrinter}. + +If available, this method should return the number of children. +@code{None} may be returned if the number can't readily be computed. +@end defun + +@defun pretty_printer.child (n) +This is not a basic method, so @value{GDBN} will only ever call it for +objects derived from @code{gdb.ValuePrinter}. + +If available, this method should return the child value indicated by +@var{n}. Indices start at zero. +@end defun + @value{GDBN} provides a function which can be used to look up the default pretty-printer for a @code{gdb.Value}: diff --git a/gdb/python/lib/gdb/dap/varref.py b/gdb/python/lib/gdb/dap/varref.py index c1e5ba8686d..768d768c7b3 100644 --- a/gdb/python/lib/gdb/dap/varref.py +++ b/gdb/python/lib/gdb/dap/varref.py @@ -151,9 +151,9 @@ class VariableReference(BaseReference): # This discards all laziness. This could be improved # slightly by lazily evaluating children, but because this # code also generally needs to know the number of - # children, it probably wouldn't help much. A real fix - # would require an update to gdb's pretty-printer protocol - # (though of course that is probably also inadvisable). + # children, it probably wouldn't help much. Note that + # this is only needed with legacy (non-ValuePrinter) + # printers. self.child_cache = list(self.printer.children()) return self.child_cache @@ -161,9 +161,12 @@ class VariableReference(BaseReference): if self.count is None: return None if self.count == -1: - if hasattr(self.printer, "num_children"): - num_children = self.printer.num_children - else: + num_children = None + if isinstance(self.printer, gdb.ValuePrinter) and hasattr( + self.printer, "num_children" + ): + num_children = self.printer.num_children() + if num_children is None: num_children = len(self.cache_children()) self.count = num_children return self.count @@ -193,7 +196,12 @@ class VariableReference(BaseReference): @in_gdb_thread def fetch_one_child(self, idx): - return self.cache_children()[idx] + if isinstance(self.printer, gdb.ValuePrinter) and hasattr( + self.printer, "child" + ): + return self.printer.child(idx) + else: + return self.cache_children()[idx] @in_gdb_thread diff --git a/gdb/python/lib/gdb/printing.py b/gdb/python/lib/gdb/printing.py index 80851590134..4982abc3f77 100644 --- a/gdb/python/lib/gdb/printing.py +++ b/gdb/python/lib/gdb/printing.py @@ -299,9 +299,6 @@ class NoOpArrayPrinter(gdb.ValuePrinter): e_values = itertools.takewhile(lambda x: x.enumval <= high, e_values) low = 0 high = len(list(e_values)) - 1 - # This is a convenience to the DAP code and perhaps other - # users. - self.num_children = high - low + 1 self.__low = low self.__high = high @@ -311,6 +308,12 @@ class NoOpArrayPrinter(gdb.ValuePrinter): def display_hint(self): return "array" + def num_children(self): + return self.__high - self.__low + 1 + + def child(self, i): + return (self.__low + i, self.__value[self.__low + i]) + def children(self): for i in range(self.__low, self.__high + 1): yield (i, self.__value[i]) -- 2.40.1