From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20764 invoked by alias); 11 Aug 2011 15:07:52 -0000 Received: (qmail 20749 invoked by uid 22791); 11 Aug 2011 15:07:48 -0000 X-SWARE-Spam-Status: No, hits=-6.8 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 11 Aug 2011 15:07:28 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p7BF6lIO003509 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 11 Aug 2011 11:06:47 -0400 Received: from localhost.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p7BF6jae010544 for ; Thu, 11 Aug 2011 11:06:46 -0400 From: Phil Muldoon To: gdb-patches@sourceware.org Subject: [python] [patch] set/show extended-prompt Reply-to: pmuldoon@redhat.com X-URL: http://www.redhat.com Date: Thu, 11 Aug 2011 15:07:00 -0000 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-08/txt/msg00236.txt.bz2 This patch adds extended-prompt capabilities to GDB. In addition it adds a prompt substitution library. I made the extended-prompt lazily control the prompt_hook on setting, over controlling it unconditionally. The rest of the function remains the same other than in-Python documentation as was found in Archer. 2011-08-11 Phil Muldoon Tom Tromey * python/lib/gdb/prompt.py: New file. * python/lib/gdb/command/prompt.py: New file. 2011-08-11 Phil Muldoon * gdb.python/python.exp: Add extended-prompt tests. 2011-08-11 Phil Muldoon * gdb.texinfo (Prompt): Add set/show extended-prompt documentation. (Basic Python): Add prompt_hook anchor. (Python modules): Reword module text for reflect multiple modules. (gdb.prompt): Document gdb.prompt module. -- diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in index 11cf2e6..c04aea4 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/gdb.texinfo b/gdb/doc/gdb.texinfo index 9a0c8db..dec6933 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -19620,6 +19620,34 @@ 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 the flags that can be used for +substitution. The prompt is updated with the value of the +flags each time it 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. Each time the prompt is displayed, it is +updated with the current value of the flags passed to it with ``set +extended-prompt''. +@end table + @node Editing @section Command Editing @cindex readline @@ -21176,6 +21204,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}. @@ -23881,11 +23911,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 @@ -23953,6 +23984,54 @@ 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 the flags substituted by values. Some flags +take arguments. You can specify arguments to a flag inside ``@{@}'' +immediately following the flag. + +The flags you can pass to this function are: + +@table @code +@item @var{\\} +Substitute a backslash. +@item @var{\e} +Substitute an ESC character. +@item @var{\f} +Substitute the selected frame; an argument names a frame parameter. +@item @var{\n} +Substitute a newline. +@item @var{\p} +Substitute a parameter's value; the argument names the parameter. +@item @var{\r} +Substitute a carriage return. +@item @var{\t} +Substitute the selected thread; an argument names a thread parameter. +@item @var{\v} +Substitute the version of GDB. +@item @var{\w} +Substitute the current working directory. +@end table + +For example: + +@smallexample +substitute_prompt (``frame: \f, print arguments: \p@{print frame-arguments@}'') +@end smallexample + +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 index e69de29..4a2d35b 100644 --- a/gdb/python/lib/gdb/command/prompt.py +++ 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 index e69de29..57e43d3 100644 --- a/gdb/python/lib/gdb/prompt.py +++ b/gdb/python/lib/gdb/prompt.py @@ -0,0 +1,139 @@ +# 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) + +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 +} + +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/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp index 832afc0..c0f064f 100644 --- a/gdb/testsuite/gdb.python/python.exp +++ b/gdb/testsuite/gdb.python/python.exp @@ -275,3 +275,36 @@ 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" + } +} +