From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id v7FjAXNsH2GwMQAAWB0awg (envelope-from ) for ; Fri, 20 Aug 2021 04:48:51 -0400 Received: by simark.ca (Postfix, from userid 112) id E2CC81EDFB; Fri, 20 Aug 2021 04:48:50 -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.9 required=5.0 tests=DKIM_SIGNED, MAILING_LIST_MULTI,T_DKIM_INVALID,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 80F121EA7E for ; Fri, 20 Aug 2021 04:48:49 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 98C943893C42 for ; Fri, 20 Aug 2021 08:48:48 +0000 (GMT) Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id CB91A385141D for ; Fri, 20 Aug 2021 08:48:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CB91A385141D 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-x42e.google.com with SMTP id q11so13083428wrr.9 for ; Fri, 20 Aug 2021 01:48:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/1ISDWLZGRrBaOgWiX+/GXyBOQi8nNyW9OT4e/VWneM=; b=S5EkD6+6lRjPfoRylP1G+2CF/rJND59oSt/GOnBGHjnPcLfaeGFP+X465Khcu6NxJ8 cVZuRAHrdvIlHVTB/h8nrL4ZWkdG25oT2dX1CTAQtx5eeXfF8bRSb6Bg4sv6bdADq6dK tJ7L+NLxddhU6nbYNDeH1d9pJcP7SWJq0HebxjgI2DErHkm0H+QHW6OxjmKPUmsVK6an Tju+NO0xA5X9b03Do64jV9osEpTEoBctbOINx8heyuwnwkwAMRZCPEYy9f0o++ZM7B3Q yLl8uC/eKa0GFchxHDf04kpvFFCCdJt6q8E1+n58LwQGwrzeIIWxm/sldMz780cFXmbj XRdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/1ISDWLZGRrBaOgWiX+/GXyBOQi8nNyW9OT4e/VWneM=; b=dnZAqMtX/MCnzVmH3rNE0/fFGlzueG8QRzUO7ltb1UnuGWnpxwPrXJLHHaVyHbxA+1 NWFeSnfH/UJMXAdVztNAgKercLnrVsQUtxw7c9ncYN0b4sdNllQgoz4McFS5Ovi+4Lem Fz0gwAAaUUaFCeWNo1nzghol2qm3ls9Vh93VIVQRFk9TUkdyo8mO3bfI8Ch63sptXrRA /qX1dXNtAQhJQh8a8E09/e8e3C89AYMLYw6spzEqthzZw+GlxG1sqwyxI2QBmhLV6th+ 9Uj0WalZgqs50yIOpKdg+u4lG9WwSTHmjTEC9hi8bjt80EUIpjfoy90QNV0RJpwTzt9+ DL2w== X-Gm-Message-State: AOAM530qAI0izNa42M7hpA/4i0kIj3tXtz1+QNeJfAv1TOapGDFaPuO6 6hxzDjSORclNNc3dNAOF0Groiz5gNkEWQg== X-Google-Smtp-Source: ABdhPJy7uPZo7SeI5xxZunUmmTzyDcsGWrfmAivSpYyt+QarQl9Z128TmlNJVvarptvnqFmusf3slg== X-Received: by 2002:a5d:5107:: with SMTP id s7mr1212034wrt.283.1629449304745; Fri, 20 Aug 2021 01:48:24 -0700 (PDT) Received: from localhost (host86-188-49-44.range86-188.btcentralplus.com. [86.188.49.44]) by smtp.gmail.com with ESMTPSA id q17sm5163920wrr.91.2021.08.20.01.48.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 01:48:24 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Subject: [PATCH] gdb/python: add facility to add values into the history Date: Fri, 20 Aug 2021 09:48:19 +0100 Message-Id: <20210820084819.1503468-1-andrew.burgess@embecosm.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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" 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.history_add() to the Python API, this function adds into the history list, and returns the index of the new item in the history list. The can be a gdb.Value object, or anything that can be passed to the constructor of gdb.Value. --- gdb/NEWS | 6 ++++++ gdb/doc/python.texi | 14 ++++++++++++++ gdb/python/py-value.c | 27 +++++++++++++++++++++++++++ gdb/python/python-internal.h | 1 + gdb/python/python.c | 2 ++ gdb/testsuite/gdb.python/py-value.exp | 26 ++++++++++++++++++++++++++ 6 files changed, 76 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index ec3058ea118..1436e35a4d0 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -10,6 +10,12 @@ 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.history_add(), which takes a gdb.Value object + and adds the value 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..395f49229c4 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -346,6 +346,20 @@ @code{gdb.Value} (@pxref{Values From Inferior}). @end defun +@findex gdb.history_add +@defun gdb.history_add (value) +Append @var{value}, an instance of @code{gdb.Value} (@pxref{Values +From Inferior}), to @value{GDBN}'s value history (@pxref{Value +History}) and return an integer, its index in the history. 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..e636fbd4cb7 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_history_add (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..c74a60c4691 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_history_add (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..351b93069a0 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" }, + { "history_add", gdbpy_history_add, 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..ef0355c76b1 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.history_add API. +proc test_add_to_history {} { + # Add a gdb.Value to the value history list. + gdb_test_no_output "python idx = gdb.history_add(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.history_add(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.history_add(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. -- 2.25.4