GDB (xrefs)
Loading...
Searching...
No Matches
prompt.py
Go to the documentation of this file.
1# Extended prompt utilities.
2# Copyright (C) 2011-2023 Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17""" Extended prompt library functions."""
18
19import gdb
20import os
21
22
23def _prompt_pwd(ignore):
24 "The current working directory."
25 return os.getcwd()
26
27
28def _prompt_object_attr(func, what, attr, nattr):
29 """Internal worker for fetching GDB attributes."""
30 if attr is None:
31 attr = nattr
32 try:
33 obj = func()
34 except gdb.error:
35 return "<no %s>" % what
36 if hasattr(obj, attr):
37 result = getattr(obj, attr)
38 if callable(result):
39 result = result()
40 return result
41 else:
42 return "<no attribute %s on current %s>" % (attr, what)
43
44
45def _prompt_frame(attr):
46 "The selected frame; an argument names a frame parameter."
47 return _prompt_object_attr(gdb.selected_frame, "frame", attr, "name")
48
49
51 "The selected thread; an argument names a thread parameter."
52 return _prompt_object_attr(gdb.selected_thread, "thread", attr, "num")
53
54
56 "The version of GDB."
57 return gdb.VERSION
58
59
60def _prompt_esc(attr):
61 "The ESC character."
62 return "\033"
63
64
65def _prompt_bs(attr):
66 "A backslash."
67 return "\\"
68
69
70def _prompt_n(attr):
71 "A newline."
72 return "\n"
73
74
75def _prompt_r(attr):
76 "A carriage return."
77 return "\r"
78
79
80def _prompt_param(attr):
81 "A parameter's value; the argument names the parameter."
82 return gdb.parameter(attr)
83
84
86 "Begins a sequence of non-printing characters."
87 return "\001"
88
89
91 "Ends a sequence of non-printing characters."
92 return "\002"
93
94
95prompt_substitutions = {
96 "e": _prompt_esc,
97 "\\": _prompt_bs,
98 "n": _prompt_n,
99 "r": _prompt_r,
100 "v": _prompt_version,
101 "w": _prompt_pwd,
102 "f": _prompt_frame,
103 "t": _prompt_thread,
104 "p": _prompt_param,
105 "[": _prompt_noprint_begin,
106 "]": _prompt_noprint_end,
107}
108
109
111 """Generate help dynamically from the __doc__ strings of attribute
112 functions."""
113
114 result = ""
115 keys = sorted(prompt_substitutions.keys())
116 for key in keys:
117 result += " \\%s\t%s\n" % (key, prompt_substitutions[key].__doc__)
118 result += """
119A substitution can be used in a simple form, like "\\f".
120An argument can also be passed to it, like "\\f{name}".
121The meaning of the argument depends on the particular substitution."""
122 return result
123
124
126 "Perform substitutions on PROMPT."
127
128 result = ""
129 plen = len(prompt)
130 i = 0
131 while i < plen:
132 if prompt[i] == "\\":
133 i = i + 1
134 if i >= plen:
135 break
136 cmdch = prompt[i]
137
138 if cmdch in prompt_substitutions:
139 cmd = prompt_substitutions[cmdch]
140
141 if i + 1 < plen and prompt[i + 1] == "{":
142 j = i + 1
143 while j < plen and prompt[j] != "}":
144 j = j + 1
145 # Just ignore formatting errors.
146 if j >= plen or prompt[j] != "}":
147 arg = None
148 else:
149 arg = prompt[i + 2 : j]
150 i = j
151 else:
152 arg = None
153 result += str(cmd(arg))
154 else:
155 # Unrecognized escapes are turned into the escaped
156 # character itself.
157 result += prompt[i]
158 else:
159 result += prompt[i]
160
161 i = i + 1
162
163 return result
_prompt_object_attr(func, what, attr, nattr)
Definition prompt.py:28
_prompt_esc(attr)
Definition prompt.py:60
substitute_prompt(prompt)
Definition prompt.py:125
_prompt_n(attr)
Definition prompt.py:70
_prompt_param(attr)
Definition prompt.py:80
_prompt_pwd(ignore)
Definition prompt.py:23
prompt_help()
Definition prompt.py:110
_prompt_r(attr)
Definition prompt.py:75
_prompt_noprint_end(attr)
Definition prompt.py:90
_prompt_thread(attr)
Definition prompt.py:50
_prompt_version(attr)
Definition prompt.py:55
_prompt_bs(attr)
Definition prompt.py:65
_prompt_noprint_begin(attr)
Definition prompt.py:85
_prompt_frame(attr)
Definition prompt.py:45
void(* func)(remote_target *remote, char *)