GDB (API)
|
00001 # Extended prompt utilities. 00002 # Copyright (C) 2011-2013 Free Software Foundation, Inc. 00003 00004 # This program is free software; you can redistribute it and/or modify 00005 # it under the terms of the GNU General Public License as published by 00006 # the Free Software Foundation; either version 3 of the License, or 00007 # (at your option) any later version. 00008 # 00009 # This program is distributed in the hope that it will be useful, 00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 # GNU General Public License for more details. 00013 # 00014 # You should have received a copy of the GNU General Public License 00015 # along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 """ Extended prompt library functions.""" 00018 00019 import gdb 00020 import os 00021 00022 def _prompt_pwd(ignore): 00023 "The current working directory." 00024 return os.getcwdu() 00025 00026 def _prompt_object_attr(func, what, attr, nattr): 00027 """Internal worker for fetching GDB attributes.""" 00028 if attr is None: 00029 attr = nattr 00030 try: 00031 obj = func() 00032 except gdb.error: 00033 return '<no %s>' % what 00034 if hasattr(obj, attr): 00035 result = getattr(obj, attr) 00036 if callable(result): 00037 result = result() 00038 return result 00039 else: 00040 return '<no attribute %s on current %s>' % (attr, what) 00041 00042 def _prompt_frame(attr): 00043 "The selected frame; an argument names a frame parameter." 00044 return _prompt_object_attr(gdb.selected_frame, 'frame', attr, 'name') 00045 00046 def _prompt_thread(attr): 00047 "The selected thread; an argument names a thread parameter." 00048 return _prompt_object_attr(gdb.selected_thread, 'thread', attr, 'num') 00049 00050 def _prompt_version(attr): 00051 "The version of GDB." 00052 return gdb.VERSION 00053 00054 def _prompt_esc(attr): 00055 "The ESC character." 00056 return '\033' 00057 00058 def _prompt_bs(attr): 00059 "A backslash." 00060 return '\\' 00061 00062 def _prompt_n(attr): 00063 "A newline." 00064 return '\n' 00065 00066 def _prompt_r(attr): 00067 "A carriage return." 00068 return '\r' 00069 00070 def _prompt_param(attr): 00071 "A parameter's value; the argument names the parameter." 00072 return gdb.parameter(attr) 00073 00074 def _prompt_noprint_begin(attr): 00075 "Begins a sequence of non-printing characters." 00076 return '\001' 00077 00078 def _prompt_noprint_end(attr): 00079 "Ends a sequence of non-printing characters." 00080 return '\002' 00081 00082 prompt_substitutions = { 00083 'e': _prompt_esc, 00084 '\\': _prompt_bs, 00085 'n': _prompt_n, 00086 'r': _prompt_r, 00087 'v': _prompt_version, 00088 'w': _prompt_pwd, 00089 'f': _prompt_frame, 00090 't': _prompt_thread, 00091 'p': _prompt_param, 00092 '[': _prompt_noprint_begin, 00093 ']': _prompt_noprint_end 00094 } 00095 00096 def prompt_help(): 00097 """Generate help dynamically from the __doc__ strings of attribute 00098 functions.""" 00099 00100 result = '' 00101 keys = sorted (prompt_substitutions.keys()) 00102 for key in keys: 00103 result += ' \\%s\t%s\n' % (key, prompt_substitutions[key].__doc__) 00104 result += """ 00105 A substitution can be used in a simple form, like "\\f". 00106 An argument can also be passed to it, like "\\f{name}". 00107 The meaning of the argument depends on the particular substitution.""" 00108 return result 00109 00110 def substitute_prompt(prompt): 00111 "Perform substitutions on PROMPT." 00112 00113 result = '' 00114 plen = len(prompt) 00115 i = 0 00116 while i < plen: 00117 if prompt[i] == '\\': 00118 i = i + 1 00119 if i >= plen: 00120 break 00121 cmdch = prompt[i] 00122 00123 if cmdch in prompt_substitutions: 00124 cmd = prompt_substitutions[cmdch] 00125 00126 if i + 1 < plen and prompt[i + 1] == '{': 00127 j = i + 1 00128 while j < plen and prompt[j] != '}': 00129 j = j + 1 00130 # Just ignore formatting errors. 00131 if j >= plen or prompt[j] != '}': 00132 arg = None 00133 else: 00134 arg = prompt[i + 2 : j] 00135 i = j 00136 else: 00137 arg = None 00138 result += str(cmd(arg)) 00139 else: 00140 # Unrecognized escapes are turned into the escaped 00141 # character itself. 00142 result += prompt[i] 00143 else: 00144 result += prompt[i] 00145 00146 i = i + 1 00147 00148 return result