diff options
author | Tom Tromey <tromey@adacore.com> | 2022-01-04 11:00:52 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2022-01-26 06:49:51 -0700 |
commit | b583c328e71369f90f4042ac7973ce9edfdb44b3 (patch) | |
tree | 3ed1cfd9763fe4984c72d204113f3b166e47d011 /gdb/python/lib | |
parent | dedb7102b3b35f789fd5c140fe01917eaeae2853 (diff) | |
download | gdb-b583c328e71369f90f4042ac7973ce9edfdb44b3.zip gdb-b583c328e71369f90f4042ac7973ce9edfdb44b3.tar.gz gdb-b583c328e71369f90f4042ac7973ce9edfdb44b3.tar.bz2 |
Add a way to temporarily set a gdb parameter from Python
It's sometimes useful to temporarily set some gdb parameter from
Python. Now that the 'endian' crash is fixed, and now that the
current language is no longer captured by the Python layer, it seems
reasonable to add a helper function for this situation.
This adds a new gdb.with_parameter function. This creates a context
manager which temporarily sets some parameter to a specified value.
The old value is restored when the context is exited. This is most
useful with the Python "with" statement:
with gdb.with_parameter('language', 'ada'):
... do Ada stuff
This also adds a simple function to set a parameter,
gdb.set_parameter, as suggested by Andrew.
This is PR python/10790.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=10790
Diffstat (limited to 'gdb/python/lib')
-rw-r--r-- | gdb/python/lib/gdb/__init__.py | 19 |
1 files changed, 19 insertions, 0 deletions
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 |