aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gdb/NEWS7
-rw-r--r--gdb/doc/python.texi26
-rw-r--r--gdb/python/lib/gdb/__init__.py19
-rw-r--r--gdb/testsuite/gdb.python/py-lookup-type.exp18
4 files changed, 70 insertions, 0 deletions
diff --git a/gdb/NEWS b/gdb/NEWS
index 8c13cef..31dff78 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -146,6 +146,13 @@ show debug lin-lwp
** New function gdb.host_charset(), returns a string, which is the
name of the current host charset.
+ ** New gdb.set_parameter(NAME, VALUE). This sets the gdb parameter
+ NAME to VALUE.
+
+ ** New gdb.with_parameter(NAME, VALUE). This returns a context
+ manager that temporarily sets the gdb parameter NAME to VALUE,
+ then resets it when the context is exited.
+
* New features in the GDB remote stub, GDBserver
** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index f02380a..39bf6e8 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -332,6 +332,32 @@ parameter's value is converted to a Python value of the appropriate
type, and returned.
@end defun
+@findex gdb.set_parameter
+@defun gdb.set_parameter (name, value)
+Sets the gdb parameter @var{name} to @var{value}. As with
+@code{gdb.parameter}, the parameter name string may contain spaces if
+the parameter has a multi-part name.
+@end defun
+
+@findex gdb.with_parameter
+@defun gdb.with_parameter (name, value)
+Create a Python context manager (for use with the Python
+@command{with} statement) that temporarily sets the gdb parameter
+@var{name} to @var{value}. On exit from the context, the previous
+value will be restored.
+
+This uses @code{gdb.parameter} in its implementation, so it can throw
+the same exceptions as that function.
+
+For example, it's sometimes useful to evaluate some Python code with a
+particular gdb language:
+
+@smallexample
+with gdb.with_parameter('language', 'pascal'):
+ ... language-specific operations
+@end smallexample
+@end defun
+
@findex gdb.history
@defun gdb.history (number)
Return a value from @value{GDBN}'s value history (@pxref{Value
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 11a1b44..d5e7eac 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -17,6 +17,7 @@ import traceback
import os
import sys
import _gdb
+from contextlib import contextmanager
# Python 3 moved "reload"
if sys.version_info >= (3, 4):
@@ -231,6 +232,24 @@ def find_pc_line(pc):
return current_progspace().find_pc_line(pc)
+def set_parameter(name, value):
+ """Set the GDB parameter NAME to VALUE."""
+ execute('set ' + name + ' ' + str(value), to_string=True)
+
+
+@contextmanager
+def with_parameter(name, value):
+ """Temporarily set the GDB parameter NAME to VALUE.
+ Note that this is a context manager."""
+ old_value = parameter(name)
+ set_parameter(name, value)
+ try:
+ # Nothing that useful to return.
+ yield None
+ finally:
+ set_parameter(name, old_value)
+
+
try:
from pygments import formatters, lexers, highlight
diff --git a/gdb/testsuite/gdb.python/py-lookup-type.exp b/gdb/testsuite/gdb.python/py-lookup-type.exp
index 534a5fd..596ab9a 100644
--- a/gdb/testsuite/gdb.python/py-lookup-type.exp
+++ b/gdb/testsuite/gdb.python/py-lookup-type.exp
@@ -55,6 +55,19 @@ test_lookup_type "objective-c" "char"
test_lookup_type "pascal" "char"
+gdb_test "show language" \
+ "The current source language is .pascal.." \
+ "show language before 'with'"
+gdb_test_multiline "look up type using set_parameter" \
+ "python" "" \
+ "with gdb.with_parameter('language', 'ada'):" "" \
+ " print(gdb.lookup_type('character'))" "" \
+ "end" "character"
+gdb_test "show language" \
+ "The current source language is .pascal.." \
+ "show language after 'with'"
+
+
# Ensure that the language can be changed from within Python and still
# affect the results.
gdb_test_multiline "look up ada type from another language" \
@@ -62,3 +75,8 @@ gdb_test_multiline "look up ada type from another language" \
"gdb.execute('set language ada')" "" \
"print(gdb.lookup_type('character'))" "" \
"end" "character"
+
+gdb_test_no_output "python gdb.set_parameter('language', 'rust')"
+gdb_test "show language" \
+ "The current source language is .rust.." \
+ "show language after 'set_parameter'"