diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 2f13b3bcc5..83122abc17 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2011-08-17 Phil Muldoon + Tom Tromey + Matt Rice + + * python/lib/gdb/prompt.py: New file. + * python/lib/gdb/command/prompt.py: New file. + * NEWS: Document set extended-prompt and gdb.prompt library + 2011-08-16 Yao Qi * tic6x-linux-tdep.c: Move const arrays definition from here... diff --git a/gdb/NEWS b/gdb/NEWS index 089e6ced4d..255a22edcc 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -23,6 +23,14 @@ ** A prompt subsitution hook (prompt_hook) is now available to the Python API. + ** A new command set/show extended-prompt has been added. + + ** A new Python module, gdb.prompt has been added to the GDB Python + modules library. This module provides functionality for + escape sequentions in prompts (used by set/show + extended-prompt). These escape sequences are replaced by their + corresponding value. + ** Python commands and convenience-functions located in 'data-directory'/python/gdb/command and 'data-directory'/python/gdb/function are now automatically loaded diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in index 11cf2e6736..c04aea4f20 100644 --- a/gdb/data-directory/Makefile.in +++ b/gdb/data-directory/Makefile.in @@ -55,8 +55,10 @@ PYTHON_FILES = \ gdb/__init__.py \ gdb/types.py \ gdb/printing.py \ + gdb/prompt.py \ gdb/command/__init__.py \ - gdb/command/pretty_printers.py + gdb/command/pretty_printers.py \ + gdb/command/prompt.py FLAGS_TO_PASS = \ "prefix=$(prefix)" \ diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index de65be6376..7e838cf252 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,11 @@ +2011-08-17 Phil Muldoon + + * gdb.texinfo (Prompt): Add set/show extended-prompt + documentation + (Basic Python): Add prompt_hook anchor. + (Python modules): Reword module text to reflect multiple modules. + (gdb.prompt): Document gdb.prompt module. + 2011-08-14 Yao Qi * gdb.texinfo (General Query Packets): Document qXfer:fdpic:read packet. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index b477cf383b..6e7bf52e8e 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -19620,6 +19620,35 @@ Directs @value{GDBN} to use @var{newprompt} as its prompt string henceforth. Prints a line of the form: @samp{Gdb's prompt is: @var{your-prompt}} @end table +Versions of @value{GDBN} that ship with Python scripting enabled have +prompt extensions. The commands for interacting with these extensions +are: + +@table @code +@kindex set extended-prompt +@item set extended-prompt @var{prompt} +Set an extended prompt that allows for substitutions. +@xref{gdb.prompt}, for a list of escape sequences that can be used for +substitution. Any escape sequences specified as part of the prompt +string are replaced with the corresponding strings each time the prompt +is displayed. + +For example: + +@smallexample +set extended-prompt Current working directory: \w (gdb) +@end smallexample + +Note that when an extended-prompt is set, it takes control of the +@var{prompt_hook} hook. @xref{prompt_hook}, for further information. + +@kindex show extended-prompt +@item show extended-prompt +Prints the extended prompt. Any escape sequences specified as part of +the prompt string with @code{set extended-prompt}, are replaced with the +corresponding strings each time the prompt is displayed. +@end table + @node Editing @section Command Editing @cindex readline @@ -21176,6 +21205,8 @@ provided, it is decoded the way that @value{GDBN}'s inbuilt @end defun @defop Operation {@value{GDBN}} prompt_hook current_prompt +@anchor{prompt_hook} + If @var{prompt_hook} is callable, @value{GDBN} will call the method assigned to this operation before a prompt is displayed by @value{GDBN}. @@ -23887,11 +23918,12 @@ top of the source tree to the source search path. @subsection Python modules @cindex python modules -@value{GDBN} comes with a module to assist writing Python code. +@value{GDBN} comes with several modules to assist writing Python code. @menu * gdb.printing:: Building and registering pretty-printers. * gdb.types:: Utilities for working with types. +* gdb.prompt:: Utilities for prompt value substitution. @end menu @node gdb.printing @@ -23959,6 +23991,62 @@ Return @code{True} if @var{type}, assumed to be a type with fields Return a Python @code{dictionary} type produced from @var{enum_type}. @end table +@node gdb.prompt +@subsubsection gdb.prompt +@cindex gdb.prompt + +This module provides a method for prompt value-substitution. + +@table @code +@item substitute_prompt (@var{string}) +Return @var{string} with escape sequences substituted by values. Some +escape sequences take arguments. You can specify arguments inside +``@{@}'' immediately following the escape sequence. + +The escape sequences you can pass to this function are: + +@table @code +@item \\ +Substitute a backslash. +@item \e +Substitute an ESC character. +@item \f +Substitute the selected frame; an argument names a frame parameter. +@item \n +Substitute a newline. +@item \p +Substitute a parameter's value; the argument names the parameter. +@item \r +Substitute a carriage return. +@item \t +Substitute the selected thread; an argument names a thread parameter. +@item \v +Substitute the version of GDB. +@item \w +Substitute the current working directory. +@item \[ +Begin a sequence of non-printing characters. These sequences are +typically used with the ESC character, and are not counted in the string +length. Example: ``\[\e[0;34m\](gdb)\[\e[0m\]'' will return a +blue-colored ``(gdb)'' prompt where the length is five. +@item \] +End a sequence of non-printing characters. +@end table + +For example: + +@smallexample +substitute_prompt (``frame: \f, + print arguments: \p@{print frame-arguments@}'') +@end smallexample + +@exdent will return the string: + +@smallexample +"frame: main, print arguments: scalars" +@end smallexample +@end table + @node Interpreters @chapter Command Interpreters @cindex command interpreters diff --git a/gdb/python/lib/gdb/command/prompt.py b/gdb/python/lib/gdb/command/prompt.py new file mode 100644 index 0000000000..35e522e764 --- /dev/null +++ b/gdb/python/lib/gdb/command/prompt.py @@ -0,0 +1,66 @@ +# Extended prompt. +# Copyright (C) 2011 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""GDB command for working with extended prompts.""" + +import gdb +import gdb.prompt + +class _ExtendedPrompt(gdb.Parameter): + + """Set the extended prompt. + +Usage: set extended-prompt VALUE + +Substitutions are applied to VALUE to compute the real prompt. + +The currently defined substitutions are: + +""" + # Add the prompt library's dynamically generated help to the + # __doc__ string. + __doc__ = __doc__ + gdb.prompt.prompt_help() + + set_doc = "Set the extended prompt." + show_doc = "Show the extended prompt." + + def __init__(self): + super(_ExtendedPrompt, self).__init__("extended-prompt", + gdb.COMMAND_SUPPORT, + gdb.PARAM_STRING_NOESCAPE) + self.value = '' + self.hook_set = False + + def get_show_string (self, pvalue): + if self.value is not '': + return "The extended prompt is: " + self.value + else: + return "The extended prompt is not set." + + def get_set_string (self): + if self.hook_set == False: + gdb.prompt_hook = self.before_prompt_hook + self.hook_set = True + return "" + + def before_prompt_hook(self, current): + if self.value is not '': + newprompt = gdb.prompt.substitute_prompt(self.value) + return newprompt.replace('\\', '\\\\') + else: + return None + +_ExtendedPrompt() diff --git a/gdb/python/lib/gdb/prompt.py b/gdb/python/lib/gdb/prompt.py new file mode 100644 index 0000000000..296df3ad97 --- /dev/null +++ b/gdb/python/lib/gdb/prompt.py @@ -0,0 +1,149 @@ +# Extended prompt utilities. +# Copyright (C) 2011 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" Extended prompt library functions.""" + +import gdb +import os + +def _prompt_pwd(ignore): + "The current working directory." + return os.getcwdu() + +def _prompt_object_attr(func, what, attr, nattr): + """Internal worker for fetching GDB attributes.""" + if attr is None: + attr = nattr + try: + obj = func() + except gdb.error: + return '' % what + if hasattr(obj, attr): + result = getattr(obj, attr) + if callable(result): + result = result() + return result + else: + return '' % (attr, what) + +def _prompt_frame(attr): + "The selected frame; an argument names a frame parameter." + return _prompt_object_attr(gdb.selected_frame, 'frame', attr, 'name') + +def _prompt_thread(attr): + "The selected thread; an argument names a thread parameter." + return _prompt_object_attr(gdb.selected_thread, 'thread', attr, 'num') + +def _prompt_version(attr): + "The version of GDB." + return gdb.VERSION + +def _prompt_esc(attr): + "The ESC character." + return '\033' + +def _prompt_bs(attr): + "A backslash." + return '\\' + +def _prompt_n(attr): + "A newline." + return '\n' + +def _prompt_r(attr): + "A carriage return." + return '\r' + +def _prompt_param(attr): + "A parameter's value; the argument names the parameter." + return gdb.parameter(attr) + +def _prompt_noprint_begin(attr): + "Begins a sequence of non-printing characters." + return '\001' + +def _prompt_noprint_end(attr): + "Ends a sequence of non-printing characters." + return '\002' + +prompt_substitutions = { + 'e': _prompt_esc, + '\\': _prompt_bs, + 'n': _prompt_n, + 'r': _prompt_r, + 'v': _prompt_version, + 'w': _prompt_pwd, + 'f': _prompt_frame, + 't': _prompt_thread, + 'p': _prompt_param, + '[': _prompt_noprint_begin, + ']': _prompt_noprint_end +} + +def prompt_help(): + """Generate help dynamically from the __doc__ strings of attribute + functions.""" + + result = '' + keys = prompt_substitutions.keys() + keys.sort() + for key in keys: + result += ' \\%s\t%s\n' % (key, prompt_substitutions[key].__doc__) + result += """ +A substitution can be used in a simple form, like "\\f". +An argument can also be passed to it, like "\\f{name}". +The meaning of the argument depends on the particular substitution.""" + return result + +def substitute_prompt(prompt): + "Perform substitutions on PROMPT." + + result = '' + plen = len(prompt) + i = 0 + while i < plen: + if prompt[i] == '\\': + i = i + 1 + if i >= plen: + break + cmdch = prompt[i] + + if cmdch in prompt_substitutions: + cmd = prompt_substitutions[cmdch] + + if i + 1 < plen and prompt[i + 1] == '{': + j = i + 1 + while j < plen and prompt[j] != '}': + j = j + 1 + # Just ignore formatting errors. + if j >= plen or prompt[j] != '}': + arg = None + else: + arg = prompt[i + 2 : j] + i = j + else: + arg = None + result += str(cmd(arg)) + else: + # Unrecognized escapes are turned into the escaped + # character itself. + result += prompt[i] + else: + result += prompt[i] + + i = i + 1 + + return result diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 6fed0f6b15..caf4833e49 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2011-08-17 Phil Muldoon + + * gdb.python/python.exp: Add extended-prompt tests. + 2011-08-15 Jan Kratochvil * gdb.arch/i386-dr3-watch.exp diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp index 832afc0ede..4da3c88870 100644 --- a/gdb/testsuite/gdb.python/python.exp +++ b/gdb/testsuite/gdb.python/python.exp @@ -275,3 +275,29 @@ gdb_test_multiple "end" "end programming" { pass "end programming" } } + +gdb_py_test_multiple "prompt substitution readline" \ + "python" "" \ + "import gdb.command.prompt" "" \ + "end" "" + +gdb_test_multiple "set extended-prompt one two three " \ + "set basic extended prompt" { + -re "\[\r\n\]one two three $" { + pass "set basic extended prompt" + } +} + +gdb_test_multiple "set extended-prompt \\w " \ + "set extended prompt working directory" { + -re "\[\r\n\].*gdb.*testsuite.* $" { + pass "set extended prompt working directory" + } +} + +gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \ + "set extended prompt parameter" { + -re "\[\r\n\]some param True $" { + pass "set extended prompt parameter" + } +}