]>
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 CommandOutput(object):
33 An output handler for all commands. Use Output.print to direct all output of macro via the handler.
34 These arguments are passed after a "--". eg
35 (lldb) zprint -- -o /tmp/zprint.out.txt
37 Currently this provide capabilities
39 The output of this command execution will be saved to file. Parser information or errors will
40 not be sent to file though. eg /tmp/output.txt
42 the "filter_string" param is parsed to python regex expression and each line of output
43 will be printed/saved only if it matches the expression.
44 The command header will not be filtered in any case.
46 def __init__(self
, cmd_name
, CommandResult
):
47 """ Create a new instance to handle command output.
49 CommandResult : SBCommandReturnObject result param from lldb's command invocation.
54 self
.pluginRequired
= False
55 self
.pluginName
= None
56 self
.cmd_name
= cmd_name
57 self
.resultObj
= CommandResult
58 self
.immediateOutput
= False
59 self
.verbose_level
= 0
60 self
.target_cmd_args
= []
61 self
.target_cmd_options
= {}
64 """ Handler for all commands output. By default just print to stdout """
65 if self
.FILTER
and not self
.reg
.search(s
):
70 if self
.fhandle
!= None:
73 if self
.immediateOutput
:
74 sys
.__stdout
__.write(s
)
79 if self
.resultObj
and len(res_str
) > 0: self
.resultObj
.AppendMessage(res_str
)
82 if self
.fhandle
!= None:
86 """ closes any open files. report on any errors """
87 if self
.fhandle
!= None :
90 def setOptions(self
, cmdargs
, cmdoptions
=''):
91 """ parse the arguments passed to the command
93 cmdargs => [] of <str> (typically args.split())
94 cmdoptions : str - string of command level options.
95 These should be CAPITAL LETTER options only.
99 cmdoptions
= cmdoptions
.upper()
101 opts
,args
= getopt
.gnu_getopt(args
,'hvo:s:p:'+ cmdoptions
,[])
102 self
.target_cmd_args
= args
103 except getopt
.GetoptError
,err
:
104 raise ArgumentError(str(err
))
105 #continue with processing
108 # This is misuse of exception but 'self' has no info on doc string.
109 # The caller may handle exception and display appropriate info
110 raise ArgumentError("HELP")
111 if o
== "-o" and len(a
) > 0:
112 self
.fname
=os
.path
.normpath(os
.path
.expanduser(a
.strip()))
113 self
.fhandle
=open(self
.fname
,"w")
114 print "saving results in file ",str(a
)
115 self
.fhandle
.write("(lldb)%s %s \n" % (self
.cmd_name
, " ".join(cmdargs
)))
116 elif o
== "-s" and len(a
) > 0:
117 self
.reg
= re
.compile(a
.strip(),re
.MULTILINE|re
.DOTALL
)
119 print "showing results for regex:",a
.strip()
120 elif o
== "-p" and len(a
) > 0:
121 self
.pluginRequired
= True
122 self
.pluginName
= a
.strip()
123 #print "passing output to " + a.strip()
125 self
.verbose_level
+= 1
128 self
.target_cmd_options
[o
] = a