From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id amXfE203N2EgLgAAWB0awg (envelope-from ) for ; Tue, 07 Sep 2021 05:57:01 -0400 Received: by simark.ca (Postfix, from userid 112) id 368291EE22; Tue, 7 Sep 2021 05:57:01 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 6C8081EDDB for ; Tue, 7 Sep 2021 05:56:59 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 63E84385EC52 for ; Tue, 7 Sep 2021 09:56:58 +0000 (GMT) Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 02C633858415 for ; Tue, 7 Sep 2021 09:56:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 02C633858415 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x434.google.com with SMTP id u9so13488358wrg.8 for ; Tue, 07 Sep 2021 02:56:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=PK1/fAe+/sAeFheruGEjQ4ETlms1C/ZLv4lel0A9HHY=; b=GLFHCR23Xi0sHbU9E9h7dq1CoqSnG/hGu7SFhn4ls+zOZAb3LJySLZxgSm79lBfYZo X2ITKY7nEh4RFqK8FIyDjfDoOXHKyNG5Kz5wt2rNqspAtCJQcS4uHfMGcX1kX3ni/kRD jI9oI9iwK+yc0GuRooX+mKo+5Z757GW7lz68G+r/+Zojm9hv9MpWALNU7QLBhivFUrrC b6aMdrd1KaH20iVPlE6QQ1Ju+zt+f98Qd+WPEPVWgC03t/QXryy8IiDx6Eo8txIGIPkH 6cMESXb/cx84fME6cnzIrDdtDQ3WWi9d5vsZXOtFTo3CazoZvFY+JeWikBFklEa29tgg q2gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=PK1/fAe+/sAeFheruGEjQ4ETlms1C/ZLv4lel0A9HHY=; b=HAYSs53l1p69yacuUfW1tZg9bymx4M5HqKgMtv7NLqGz3+Kqohhgfm330XdvgHR8bf f3aR7qZyjRrbcv+lU+5iKiYnO7fZIEYtjU/NFjX/OujG7kFkuKZJ0aHrnscg+SSR2z/k yrGjr/x39zoS2ynJiyg6YBjUiKRHB81ln0zYzWDBGUU1riSNbF6UkT0/eCsm7Osd8aQ7 n+mf8Jkl2q0Wu8mx6qjjhsQoY5yq+6UfCO1caPXZa1eDxC5kUrlWnVoMVoG+xGawd9KJ REOrXcxZRUmgXT7iokRAugHSKU8s+dXE0fboP+IZ4iMmckYMFiU0+9eMACWK8kdUY4Ax +NJQ== X-Gm-Message-State: AOAM532BNCemjI7gcvIqntKz4T+jNRApZUDMqz2+SrngKclMvj9qcz45 UR7V6x3EXWdu8atx5vgvOp4EBScJ7GNMkQ== X-Google-Smtp-Source: ABdhPJzH2jWmwEpP4EdRRP4jyMoXt8yPfj2rtXDBmvSXDaoM+oKMNpquyN9+pmH2Tj7+RnE186vAiQ== X-Received: by 2002:adf:f3c4:: with SMTP id g4mr17835483wrp.409.1631008605857; Tue, 07 Sep 2021 02:56:45 -0700 (PDT) Received: from localhost (host86-191-239-77.range86-191.btcentralplus.com. [86.191.239.77]) by smtp.gmail.com with ESMTPSA id k16sm10437821wrh.24.2021.09.07.02.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Sep 2021 02:56:45 -0700 (PDT) Date: Tue, 7 Sep 2021 10:56:44 +0100 From: Andrew Burgess To: gdb-patches@sourceware.org Subject: Re: [PATCHv2] gdb/python: add facility to add values into the history Message-ID: <20210907095644.GO2581@embecosm.com> References: <874kbfaon0.fsf@tromey.com> <20210825172719.3814152-1-andrew.burgess@embecosm.com> <83y28ptnch.fsf@gnu.org> <20210826092002.GI2581@embecosm.com> <83eeagtugt.fsf@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <83eeagtugt.fsf@gnu.org> X-Operating-System: Linux/5.8.18-100.fc31.x86_64 (x86_64) X-Uptime: 10:55:33 up 20 days, 22:51, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" * Eli Zaretskii [2021-08-26 12:40:18 +0300]: > > Date: Thu, 26 Aug 2021 10:20:02 +0100 > > From: Andrew Burgess > > Cc: gdb-patches@sourceware.org > > > > * Eli Zaretskii [2021-08-25 21:01:50 +0300]: > > > > > > From: Andrew Burgess > > > > Date: Wed, 25 Aug 2021 18:27:19 +0100 > > > > > > > > +* Python API > > > > + > > > > + ** New function gdb.add_history(), which takes a gdb.Value object > > > > + and adds the value to GDB's history list. An integer, the index > > > ^^^^^^^^^ > > > "its value", right? > > > > I don't know, that doesn't sound right to me. 'its value' seems to > > imply a value that belongs to someone/something, but I don't think > > that's the case here. > > > > Maybe, '...takes a gdb.Value object and adds it to GDB's history list.' ? > > No, that's worse, because you don't add the Python object to the > history. > > How about if you use the same wording you suggested for the manual: > "the value it represents"? Excellent, I'll do that. The final version of the patch I pushed is below. Thanks for the review. Andrew --- commit 10effdd0d48947b41eb89426d0a408b2421c3d27 Author: Andrew Burgess Date: Fri Jul 30 12:56:34 2021 +0100 gdb/python: new function to add values into GDB's history The guile API has (history-append! ) to add values into GDB's history list. There is currently no equivalent in the Python API. This commit adds gdb.add_history() to the Python API, this function takes a gdb.Value (or anything that can be passed to the constructor of gdb.Value), and adds the value it represents to GDB's history list. The index of the newly added value is returned. diff --git a/gdb/NEWS b/gdb/NEWS index ec3058ea118..f9485520438 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -10,6 +10,13 @@ maint show backtrace-on-fatal-signal fatal signal. This only supported on some platforms where the backtrace and backtrace_symbols_fd functions are available. +* Python API + + ** New function gdb.add_history(), which takes a gdb.Value object + and adds the value it represents to GDB's history list. An + integer, the index of the new item in the history list, is + returned. + *** Changes in GDB 11 * The 'set disassembler-options' command now supports specifying options diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index f4865b3d6a6..d8f682a091c 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -346,6 +346,20 @@ @code{gdb.Value} (@pxref{Values From Inferior}). @end defun +@defun gdb.add_history (value) +Takes @var{value}, an instance of @code{gdb.Value} (@pxref{Values From +Inferior}), and appends the value this object represents to +@value{GDBN}'s value history (@pxref{Value History}), and return an +integer, its history number. If @var{value} is not a +@code{gdb.Value}, it is is converted using the @code{gdb.Value} +constructor. If @var{value} can't be converted to a @code{gdb.Value} +then a @code{TypeError} is raised. + +When a command implemented in Python prints a single @code{gdb.Value} +as its result, then placing the value into the history will allow the +user convenient access to those values via CLI history facilities. +@end defun + @findex gdb.convenience_variable @defun gdb.convenience_variable (name) Return the value of the convenience variable (@pxref{Convenience diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 8df8a15f8d6..26d5940f842 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1960,6 +1960,33 @@ gdbpy_history (PyObject *self, PyObject *args) return value_to_value_object (res_val); } +/* Add a gdb.Value into GDB's history, and return (as an integer) the + position of the newly added value. */ +PyObject * +gdbpy_add_history (PyObject *self, PyObject *args) +{ + PyObject *value_obj; + + if (!PyArg_ParseTuple (args, "O", &value_obj)) + return nullptr; + + struct value *value = convert_value_from_python (value_obj); + if (value == nullptr) + return nullptr; + + try + { + int idx = record_latest_value (value); + return gdb_py_object_from_longest (idx).release (); + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + return nullptr; +} + /* Return the value of a convenience variable. */ PyObject * gdbpy_convenience_variable (PyObject *self, PyObject *args) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 690d2fb43c0..0e140f1af61 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -412,6 +412,7 @@ extern enum ext_lang_rc gdbpy_get_matching_xmethod_workers PyObject *gdbpy_history (PyObject *self, PyObject *args); +PyObject *gdbpy_add_history (PyObject *self, PyObject *args); PyObject *gdbpy_convenience_variable (PyObject *self, PyObject *args); PyObject *gdbpy_set_convenience_variable (PyObject *self, PyObject *args); PyObject *gdbpy_breakpoints (PyObject *, PyObject *); diff --git a/gdb/python/python.c b/gdb/python/python.c index e42cbc4fd5e..6af9c722e7b 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2076,6 +2076,8 @@ PyMethodDef python_GdbMethods[] = { { "history", gdbpy_history, METH_VARARGS, "Get a value from history" }, + { "add_history", gdbpy_add_history, METH_VARARGS, + "Add a value to the value history list" }, { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS, "execute (command [, from_tty] [, to_string]) -> [String]\n\ Evaluate command, a string, as a gdb CLI command. Optionally returns\n\ diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index eca4a474727..d9f1a76a388 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -575,6 +575,31 @@ proc test_value_from_buffer {} { "attempt to construct value with string as type" } +# Test the gdb.add_history API. +proc test_add_to_history {} { + # Add a gdb.Value to the value history list. + gdb_test_no_output "python idx = gdb.add_history(gdb.Value(42))" \ + "add value 42 to the history list" + gdb_test "python print (\"$%d = %s\" % (idx, gdb.history (idx)))" \ + " = 42" "print value 42 from the history list" + set idx [get_python_valueof "idx" "**DEFAULT**" "get idx for value 42"] + gdb_test "print \$${idx}" " = 42" + + # Add something to the history list that can be converted into a + # gdb.Value. + gdb_test_no_output "python idx = gdb.add_history(84)" \ + "add value to 84 to the history list" + gdb_test "python print (\"$%d = %s\" % (idx, gdb.history (idx)))" \ + " = 84" "print value 84 from the history list" + set idx [get_python_valueof "idx" "**DEFAULT**" "get idx for value 84"] + gdb_test "print \$${idx}" " = 84" + + # Try adding something that can't be converted to a gdb.Value, + # this should give an error. + gdb_test "python idx = gdb.add_history(gdb.GdbError(\"an error\"))" \ + "TypeError: Could not convert Python object: .*" +} + # Build C version of executable. C++ is built later. if { [build_inferior "${binfile}" "c"] < 0 } { return -1 @@ -594,6 +619,7 @@ test_objfiles test_parse_and_eval test_value_hash test_float_conversion +test_add_to_history # The following tests require execution.