]>
git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/core/standard.py
6 class ArgumentError(Exception):
7 """ Exception class for raising errors in command arguments. The lldb_command framework will catch this
8 class of exceptions and print suitable error message to user.
10 def __init__(self
, msg
):
11 self
.error_message
= msg
13 return str(self
.error_message
)
16 class RedirectStdStreams(object):
17 def __init__(self
, stdout
=None, stderr
=None):
18 self
._stdout
= stdout
or sys
.stdout
19 self
._stderr
= stderr
or sys
.stderr
22 self
.old_stdout
, self
.old_stderr
= sys
.stdout
, sys
.stderr
23 self
.old_stdout
.flush(); self
.old_stderr
.flush()
24 sys
.stdout
, sys
.stderr
= self
._stdout
, self
._stderr
26 def __exit__(self
, exc_type
, exc_value
, traceback
):
27 self
._stdout
.flush(); self
._stderr
.flush()
28 sys
.stdout
= self
.old_stdout
29 sys
.stderr
= self
.old_stderr
31 class IndentScope(object):
32 def __init__(self
, O
):
36 self
._O
._indent
+= ' '
38 def __exit__(self
, exc_type
, exc_value
, traceback
):
39 self
._O
._indent
= self
._O
._indent
[:-4]
41 class HeaderScope(object):
42 def __init__(self
, O
, hdr
, indent
= False):
48 self
._oldHeader
= self
._O
._header
49 self
._oldLastHeader
= self
._O
._lastHeader
50 self
._O
._header
= self
._header
51 self
._O
._lastHeader
= None
53 self
._O
._indent
+= ' '
55 def __exit__(self
, exc_type
, exc_value
, traceback
):
56 self
._O
._header
= self
._oldHeader
57 self
._O
._lastHeader
= self
._oldLastHeader
59 self
._O
._indent
= self
._O
._indent
[:-4]
62 Black
= "\033[38;5;0m"
63 DarkRed
= "\033[38;5;1m"
64 DarkGreen
= "\033[38;5;2m"
65 Brown
= "\033[38;5;3m"
66 DarkBlue
= "\033[38;5;4m"
67 DarkMagenta
= "\033[38;5;5m"
68 DarkCyan
= "\033[38;5;6m"
71 DarkGrey
= "\033[38;5;8m"
73 Green
= "\033[38;5;10m"
74 Yellow
= "\033[38;5;11m"
75 Blue
= "\033[38;5;12m"
76 Magenta
= "\033[38;5;13m"
77 Cyan
= "\033[38;5;14m"
78 White
= "\033[38;5;15m"
86 EndOblique
= "\033[23m"
89 EndUnderline
= "\033[24m"
94 def __getattribute__(self
, *args
):
97 class CommandOutput(object):
99 An output handler for all commands. Use Output.print to direct all output of macro via the handler.
100 These arguments are passed after a "--". eg
101 (lldb) zprint -- -o /tmp/zprint.out.txt
103 Currently this provide capabilities
106 The output of this command execution will be saved to file. Parser information or errors will
107 not be sent to file though. eg /tmp/output.txt
109 the "filter_string" param is parsed to python regex expression and each line of output
110 will be printed/saved only if it matches the expression.
111 The command header will not be filtered in any case.
113 Send the output of the command to plugin.
116 -c <always|never|auto>
119 def __init__(self
, cmd_name
, CommandResult
=None, fhandle
=None):
120 """ Create a new instance to handle command output.
122 CommandResult : SBCommandReturnObject result param from lldb's command invocation.
127 self
.pluginRequired
= False
128 self
.pluginName
= None
129 self
.cmd_name
= cmd_name
130 self
.resultObj
= CommandResult
131 self
.verbose_level
= 0
132 self
.target_cmd_args
= []
133 self
.target_cmd_options
= {}
135 self
.isatty
= os
.isatty(sys
.__stdout
__.fileno())
140 self
._lastHeader
= None
144 if self
.fhandle
!= None:
145 self
.fhandle
.write(self
._indent
+ s
+ "\n")
147 self
.resultObj
.AppendMessage(self
._indent
+ s
)
151 if self
.color
is True:
153 return self
.color
is None and self
.isatty
155 def _needsHeader(self
):
156 if self
._header
is None:
158 if self
._lastHeader
is None:
162 return self
._line
- self
._lastHeader
> 40
165 return IndentScope(self
)
167 def table(self
, header
, indent
= False):
168 return HeaderScope(self
, header
, indent
)
170 def format(self
, s
, *args
, **kwargs
):
174 kwargs
['VT'] = NOVT()
176 return s
.format(*args
, **kwargs
)
178 def error(self
, s
, *args
, **kwargs
):
179 print self
.format("{cmd.cmd_name}: {VT.Red}"+s
+"{VT.Default}", cmd
=self
, *args
, **kwargs
)
182 """ Handler for all commands output. By default just print to stdout """
186 while s
.find('\n') != -1:
187 l
, s
= s
.split("\n", 1)
189 if not self
.reg
.search(l
):
192 l
= self
.reg
.sub(VT
.Underline
+ r
"\g<0>" + VT
.EndUnderline
, l
);
194 if len(l
) and self
._needsHeader
():
195 for hdr
in self
._header
.split("\n"):
196 self
._write
(self
.format("{VT.Bold}{:s}{VT.EndBold}", hdr
))
197 self
._lastHeader
= self
._line
204 if self
.fhandle
!= None:
208 """ closes any open files. report on any errors """
209 if self
.fhandle
!= None and self
.fname
!= None:
212 def setOptions(self
, cmdargs
, cmdoptions
=''):
213 """ parse the arguments passed to the command
215 cmdargs => [] of <str> (typically args.split())
216 cmdoptions : str - string of command level options.
217 These should be CAPITAL LETTER options only.
221 cmdoptions
= cmdoptions
.upper()
223 opts
,args
= getopt
.gnu_getopt(args
,'hvo:s:p:c:'+ cmdoptions
,[])
224 self
.target_cmd_args
= args
225 except getopt
.GetoptError
,err
:
226 raise ArgumentError(str(err
))
227 #continue with processing
230 # This is misuse of exception but 'self' has no info on doc string.
231 # The caller may handle exception and display appropriate info
232 raise ArgumentError("HELP")
233 if o
== "-o" and len(a
) > 0:
234 self
.fname
=os
.path
.normpath(os
.path
.expanduser(a
.strip()))
235 self
.fhandle
=open(self
.fname
,"w")
236 print "saving results in file ",str(a
)
237 self
.fhandle
.write("(lldb)%s %s \n" % (self
.cmd_name
, " ".join(cmdargs
)))
238 self
.isatty
= os
.isatty(self
.fhandle
.fileno())
239 elif o
== "-s" and len(a
) > 0:
240 self
.reg
= re
.compile(a
.strip(),re
.MULTILINE|re
.DOTALL
)
242 print "showing results for regex:",a
.strip()
243 elif o
== "-p" and len(a
) > 0:
244 self
.pluginRequired
= True
245 self
.pluginName
= a
.strip()
246 #print "passing output to " + a.strip()
248 self
.verbose_level
+= 1
250 if a
in ["always", '1']:
252 elif a
in ["never", '0']:
258 self
.target_cmd_options
[o
] = a