1*27b03b36SApple OSS Distributionsfrom __future__ import absolute_import, print_function 2*27b03b36SApple OSS Distributions 3*27b03b36SApple OSS Distributionsfrom builtins import object 4*27b03b36SApple OSS Distributions 5*27b03b36SApple OSS Distributionsimport getopt 6*27b03b36SApple OSS Distributionsimport os 7*27b03b36SApple OSS Distributionsimport sys 8*27b03b36SApple OSS Distributionsimport re 9*27b03b36SApple OSS Distributions 10*27b03b36SApple OSS Distributionsclass ArgumentError(Exception): 11*27b03b36SApple OSS Distributions """ Exception class for raising errors in command arguments. The lldb_command framework will catch this 12*27b03b36SApple OSS Distributions class of exceptions and print suitable error message to user. 13*27b03b36SApple OSS Distributions """ 14*27b03b36SApple OSS Distributions def __init__(self, msg): 15*27b03b36SApple OSS Distributions self.error_message = msg 16*27b03b36SApple OSS Distributions def __str__(self): 17*27b03b36SApple OSS Distributions return str(self.error_message) 18*27b03b36SApple OSS Distributions 19*27b03b36SApple OSS Distributions 20*27b03b36SApple OSS Distributionsclass RedirectStdStreams(object): 21*27b03b36SApple OSS Distributions def __init__(self, stdout=None, stderr=None): 22*27b03b36SApple OSS Distributions self._stdout = stdout or sys.stdout 23*27b03b36SApple OSS Distributions self._stderr = stderr or sys.stderr 24*27b03b36SApple OSS Distributions 25*27b03b36SApple OSS Distributions def __enter__(self): 26*27b03b36SApple OSS Distributions self.old_stdout, self.old_stderr = sys.stdout, sys.stderr 27*27b03b36SApple OSS Distributions self.old_stdout.flush(); self.old_stderr.flush() 28*27b03b36SApple OSS Distributions sys.stdout, sys.stderr = self._stdout, self._stderr 29*27b03b36SApple OSS Distributions 30*27b03b36SApple OSS Distributions def __exit__(self, exc_type, exc_value, traceback): 31*27b03b36SApple OSS Distributions self._stdout.flush(); self._stderr.flush() 32*27b03b36SApple OSS Distributions sys.stdout = self.old_stdout 33*27b03b36SApple OSS Distributions sys.stderr = self.old_stderr 34*27b03b36SApple OSS Distributions 35*27b03b36SApple OSS Distributionsclass IndentScope(object): 36*27b03b36SApple OSS Distributions def __init__(self, O): 37*27b03b36SApple OSS Distributions self._O = O 38*27b03b36SApple OSS Distributions 39*27b03b36SApple OSS Distributions def __enter__(self): 40*27b03b36SApple OSS Distributions self._O._indent += ' ' 41*27b03b36SApple OSS Distributions 42*27b03b36SApple OSS Distributions def __exit__(self, exc_type, exc_value, traceback): 43*27b03b36SApple OSS Distributions self._O._indent = self._O._indent[:-4] 44*27b03b36SApple OSS Distributions 45*27b03b36SApple OSS Distributionsclass HeaderScope(object): 46*27b03b36SApple OSS Distributions def __init__(self, O, hdr, indent = False): 47*27b03b36SApple OSS Distributions self._O = O 48*27b03b36SApple OSS Distributions self._header = hdr 49*27b03b36SApple OSS Distributions self._indent = indent 50*27b03b36SApple OSS Distributions 51*27b03b36SApple OSS Distributions def __enter__(self): 52*27b03b36SApple OSS Distributions self._oldHeader = self._O._header 53*27b03b36SApple OSS Distributions self._oldLastHeader = self._O._lastHeader 54*27b03b36SApple OSS Distributions self._O._header = self._header 55*27b03b36SApple OSS Distributions self._O._lastHeader = None 56*27b03b36SApple OSS Distributions if self._indent: 57*27b03b36SApple OSS Distributions self._O._indent += ' ' 58*27b03b36SApple OSS Distributions 59*27b03b36SApple OSS Distributions def __exit__(self, exc_type, exc_value, traceback): 60*27b03b36SApple OSS Distributions self._O._header = self._oldHeader 61*27b03b36SApple OSS Distributions self._O._lastHeader = self._oldLastHeader 62*27b03b36SApple OSS Distributions if self._indent: 63*27b03b36SApple OSS Distributions self._O._indent = self._O._indent[:-4] 64*27b03b36SApple OSS Distributions 65*27b03b36SApple OSS Distributionsclass VT(object): 66*27b03b36SApple OSS Distributions Black = "\033[38;5;0m" 67*27b03b36SApple OSS Distributions DarkRed = "\033[38;5;1m" 68*27b03b36SApple OSS Distributions DarkGreen = "\033[38;5;2m" 69*27b03b36SApple OSS Distributions Brown = "\033[38;5;3m" 70*27b03b36SApple OSS Distributions DarkBlue = "\033[38;5;4m" 71*27b03b36SApple OSS Distributions DarkMagenta = "\033[38;5;5m" 72*27b03b36SApple OSS Distributions DarkCyan = "\033[38;5;6m" 73*27b03b36SApple OSS Distributions Grey = "\033[38;5;7m" 74*27b03b36SApple OSS Distributions 75*27b03b36SApple OSS Distributions DarkGrey = "\033[38;5;8m" 76*27b03b36SApple OSS Distributions Red = "\033[38;5;9m" 77*27b03b36SApple OSS Distributions Green = "\033[38;5;10m" 78*27b03b36SApple OSS Distributions Yellow = "\033[38;5;11m" 79*27b03b36SApple OSS Distributions Blue = "\033[38;5;12m" 80*27b03b36SApple OSS Distributions Magenta = "\033[38;5;13m" 81*27b03b36SApple OSS Distributions Cyan = "\033[38;5;14m" 82*27b03b36SApple OSS Distributions White = "\033[38;5;15m" 83*27b03b36SApple OSS Distributions 84*27b03b36SApple OSS Distributions Default = "\033[39m" 85*27b03b36SApple OSS Distributions 86*27b03b36SApple OSS Distributions Bold = "\033[1m" 87*27b03b36SApple OSS Distributions EndBold = "\033[22m" 88*27b03b36SApple OSS Distributions 89*27b03b36SApple OSS Distributions Oblique = "\033[3m" 90*27b03b36SApple OSS Distributions EndOblique = "\033[23m" 91*27b03b36SApple OSS Distributions 92*27b03b36SApple OSS Distributions Underline = "\033[4m" 93*27b03b36SApple OSS Distributions EndUnderline = "\033[24m" 94*27b03b36SApple OSS Distributions 95*27b03b36SApple OSS Distributions Reset = "\033[0m" 96*27b03b36SApple OSS Distributions 97*27b03b36SApple OSS Distributionsclass NOVT(object): 98*27b03b36SApple OSS Distributions def __getattribute__(self, *args): 99*27b03b36SApple OSS Distributions return "" 100*27b03b36SApple OSS Distributions 101*27b03b36SApple OSS Distributionsclass CommandOutput(object): 102*27b03b36SApple OSS Distributions """ 103*27b03b36SApple OSS Distributions An output handler for all commands. Use Output.print to direct all output of macro via the handler. 104*27b03b36SApple OSS Distributions These arguments are passed after a "--". eg 105*27b03b36SApple OSS Distributions (lldb) zprint -- -o /tmp/zprint.out.txt 106*27b03b36SApple OSS Distributions 107*27b03b36SApple OSS Distributions Currently this provide capabilities 108*27b03b36SApple OSS Distributions -h show help 109*27b03b36SApple OSS Distributions -o path/to/filename 110*27b03b36SApple OSS Distributions The output of this command execution will be saved to file. Parser information or errors will 111*27b03b36SApple OSS Distributions not be sent to file though. eg /tmp/output.txt 112*27b03b36SApple OSS Distributions -s filter_string 113*27b03b36SApple OSS Distributions the "filter_string" param is parsed to python regex expression and each line of output 114*27b03b36SApple OSS Distributions will be printed/saved only if it matches the expression. 115*27b03b36SApple OSS Distributions The command header will not be filtered in any case. 116*27b03b36SApple OSS Distributions -p <plugin_name> 117*27b03b36SApple OSS Distributions Send the output of the command to plugin. 118*27b03b36SApple OSS Distributions -v ... 119*27b03b36SApple OSS Distributions Up verbosity 120*27b03b36SApple OSS Distributions -c <always|never|auto> 121*27b03b36SApple OSS Distributions configure color 122*27b03b36SApple OSS Distributions """ 123*27b03b36SApple OSS Distributions def __init__(self, cmd_name, CommandResult=None, fhandle=None): 124*27b03b36SApple OSS Distributions """ Create a new instance to handle command output. 125*27b03b36SApple OSS Distributions params: 126*27b03b36SApple OSS Distributions CommandResult : SBCommandReturnObject result param from lldb's command invocation. 127*27b03b36SApple OSS Distributions """ 128*27b03b36SApple OSS Distributions self.fname=None 129*27b03b36SApple OSS Distributions self.fhandle=fhandle 130*27b03b36SApple OSS Distributions self.FILTER=False 131*27b03b36SApple OSS Distributions self.pluginRequired = False 132*27b03b36SApple OSS Distributions self.pluginName = None 133*27b03b36SApple OSS Distributions self.cmd_name = cmd_name 134*27b03b36SApple OSS Distributions self.resultObj = CommandResult 135*27b03b36SApple OSS Distributions self.verbose_level = 0 136*27b03b36SApple OSS Distributions self.target_cmd_args = [] 137*27b03b36SApple OSS Distributions self.target_cmd_options = {} 138*27b03b36SApple OSS Distributions self.color = None 139*27b03b36SApple OSS Distributions self.isatty = os.isatty(sys.__stdout__.fileno()) 140*27b03b36SApple OSS Distributions self._indent = '' 141*27b03b36SApple OSS Distributions self._buffer = '' 142*27b03b36SApple OSS Distributions 143*27b03b36SApple OSS Distributions self._header = None 144*27b03b36SApple OSS Distributions self._lastHeader = None 145*27b03b36SApple OSS Distributions self._line = 0 146*27b03b36SApple OSS Distributions 147*27b03b36SApple OSS Distributions def _write(self, s): 148*27b03b36SApple OSS Distributions if self.fhandle != None: 149*27b03b36SApple OSS Distributions self.fhandle.write(self._indent + s + "\n") 150*27b03b36SApple OSS Distributions else: 151*27b03b36SApple OSS Distributions self.resultObj.AppendMessage(self._indent + s) 152*27b03b36SApple OSS Distributions self._line += 1 153*27b03b36SApple OSS Distributions 154*27b03b36SApple OSS Distributions def _doColor(self): 155*27b03b36SApple OSS Distributions if self.color is True: 156*27b03b36SApple OSS Distributions return True; 157*27b03b36SApple OSS Distributions return self.color is None and self.isatty 158*27b03b36SApple OSS Distributions 159*27b03b36SApple OSS Distributions def _needsHeader(self): 160*27b03b36SApple OSS Distributions if self._header is None: 161*27b03b36SApple OSS Distributions return False 162*27b03b36SApple OSS Distributions if self._lastHeader is None: 163*27b03b36SApple OSS Distributions return True 164*27b03b36SApple OSS Distributions if not self.isatty: 165*27b03b36SApple OSS Distributions return False 166*27b03b36SApple OSS Distributions return self._line - self._lastHeader > 40 167*27b03b36SApple OSS Distributions 168*27b03b36SApple OSS Distributions def indent(self): 169*27b03b36SApple OSS Distributions return IndentScope(self) 170*27b03b36SApple OSS Distributions 171*27b03b36SApple OSS Distributions def table(self, header, indent = False): 172*27b03b36SApple OSS Distributions return HeaderScope(self, header, indent) 173*27b03b36SApple OSS Distributions 174*27b03b36SApple OSS Distributions def format(self, s, *args, **kwargs): 175*27b03b36SApple OSS Distributions if self._doColor(): 176*27b03b36SApple OSS Distributions kwargs['VT'] = VT 177*27b03b36SApple OSS Distributions else: 178*27b03b36SApple OSS Distributions kwargs['VT'] = NOVT() 179*27b03b36SApple OSS Distributions 180*27b03b36SApple OSS Distributions return s.format(*args, **kwargs) 181*27b03b36SApple OSS Distributions 182*27b03b36SApple OSS Distributions def error(self, s, *args, **kwargs): 183*27b03b36SApple OSS Distributions print(self.format("{cmd.cmd_name}: {VT.Red}"+s+"{VT.Default}", cmd=self, *args, **kwargs)) 184*27b03b36SApple OSS Distributions 185*27b03b36SApple OSS Distributions def write(self, s): 186*27b03b36SApple OSS Distributions """ Handler for all commands output. By default just print to stdout """ 187*27b03b36SApple OSS Distributions 188*27b03b36SApple OSS Distributions s = self._buffer + s 189*27b03b36SApple OSS Distributions 190*27b03b36SApple OSS Distributions while s.find('\n') != -1: 191*27b03b36SApple OSS Distributions l, s = s.split("\n", 1) 192*27b03b36SApple OSS Distributions if self.FILTER: 193*27b03b36SApple OSS Distributions if not self.reg.search(l): 194*27b03b36SApple OSS Distributions continue 195*27b03b36SApple OSS Distributions if self._doColor(): 196*27b03b36SApple OSS Distributions l = self.reg.sub(VT.Underline + r"\g<0>" + VT.EndUnderline, l); 197*27b03b36SApple OSS Distributions 198*27b03b36SApple OSS Distributions if len(l) and self._needsHeader(): 199*27b03b36SApple OSS Distributions for hdr in self._header.split("\n"): 200*27b03b36SApple OSS Distributions self._write(self.format("{VT.Bold}{:s}{VT.EndBold}", hdr)) 201*27b03b36SApple OSS Distributions self._lastHeader = self._line 202*27b03b36SApple OSS Distributions 203*27b03b36SApple OSS Distributions self._write(l) 204*27b03b36SApple OSS Distributions 205*27b03b36SApple OSS Distributions self._buffer = s 206*27b03b36SApple OSS Distributions 207*27b03b36SApple OSS Distributions def flush(self): 208*27b03b36SApple OSS Distributions if self.fhandle != None: 209*27b03b36SApple OSS Distributions self.fhandle.flush() 210*27b03b36SApple OSS Distributions 211*27b03b36SApple OSS Distributions def __del__(self): 212*27b03b36SApple OSS Distributions """ closes any open files. report on any errors """ 213*27b03b36SApple OSS Distributions if self.fhandle != None and self.fname != None: 214*27b03b36SApple OSS Distributions self.fhandle.close() 215*27b03b36SApple OSS Distributions 216*27b03b36SApple OSS Distributions def setOptions(self, cmdargs, cmdoptions =''): 217*27b03b36SApple OSS Distributions """ parse the arguments passed to the command 218*27b03b36SApple OSS Distributions param : 219*27b03b36SApple OSS Distributions cmdargs => [] of <str> (typically args.split()) 220*27b03b36SApple OSS Distributions cmdoptions : str - string of command level options. 221*27b03b36SApple OSS Distributions These should be CAPITAL LETTER options only. 222*27b03b36SApple OSS Distributions """ 223*27b03b36SApple OSS Distributions opts=() 224*27b03b36SApple OSS Distributions args = cmdargs 225*27b03b36SApple OSS Distributions cmdoptions = cmdoptions.upper() 226*27b03b36SApple OSS Distributions try: 227*27b03b36SApple OSS Distributions opts,args = getopt.gnu_getopt(args,'hvo:s:p:c:'+ cmdoptions,[]) 228*27b03b36SApple OSS Distributions self.target_cmd_args = args 229*27b03b36SApple OSS Distributions except getopt.GetoptError as err: 230*27b03b36SApple OSS Distributions raise ArgumentError(str(err)) 231*27b03b36SApple OSS Distributions #continue with processing 232*27b03b36SApple OSS Distributions for o,a in opts : 233*27b03b36SApple OSS Distributions if o == "-h": 234*27b03b36SApple OSS Distributions # This is misuse of exception but 'self' has no info on doc string. 235*27b03b36SApple OSS Distributions # The caller may handle exception and display appropriate info 236*27b03b36SApple OSS Distributions raise ArgumentError("HELP") 237*27b03b36SApple OSS Distributions if o == "-o" and len(a) > 0: 238*27b03b36SApple OSS Distributions self.fname=os.path.normpath(os.path.expanduser(a.strip())) 239*27b03b36SApple OSS Distributions self.fhandle=open(self.fname,"w") 240*27b03b36SApple OSS Distributions print("saving results in file ",str(a)) 241*27b03b36SApple OSS Distributions self.fhandle.write("(lldb)%s %s \n" % (self.cmd_name, " ".join(cmdargs))) 242*27b03b36SApple OSS Distributions self.isatty = os.isatty(self.fhandle.fileno()) 243*27b03b36SApple OSS Distributions elif o == "-s" and len(a) > 0: 244*27b03b36SApple OSS Distributions self.reg = re.compile(a.strip(),re.MULTILINE|re.DOTALL) 245*27b03b36SApple OSS Distributions self.FILTER=True 246*27b03b36SApple OSS Distributions print("showing results for regex:",a.strip()) 247*27b03b36SApple OSS Distributions elif o == "-p" and len(a) > 0: 248*27b03b36SApple OSS Distributions self.pluginRequired = True 249*27b03b36SApple OSS Distributions self.pluginName = a.strip() 250*27b03b36SApple OSS Distributions #print "passing output to " + a.strip() 251*27b03b36SApple OSS Distributions elif o == "-v": 252*27b03b36SApple OSS Distributions self.verbose_level += 1 253*27b03b36SApple OSS Distributions elif o == "-c": 254*27b03b36SApple OSS Distributions if a in ["always", '1']: 255*27b03b36SApple OSS Distributions self.color = True 256*27b03b36SApple OSS Distributions elif a in ["never", '0']: 257*27b03b36SApple OSS Distributions self.color = False 258*27b03b36SApple OSS Distributions else: 259*27b03b36SApple OSS Distributions self.color = None 260*27b03b36SApple OSS Distributions else: 261*27b03b36SApple OSS Distributions o = o.strip() 262*27b03b36SApple OSS Distributions self.target_cmd_options[o] = a 263*27b03b36SApple OSS Distributions 264*27b03b36SApple OSS Distributions 265