1import argparse 2import socket 3import json 4import os 5import sys 6 7import use_lldb_suite 8from lldbsuite.test.gdbclientutils import * 9 10_description = """\ 11Implements a fake qemu for testing purposes. The executable program 12is not actually run. Instead a very basic mock process is presented 13to lldb. This allows us to check the invocation parameters. 14 15The behavior of the emulated "process" is controlled via its command line 16arguments, which should take the form of key:value pairs. Currently supported 17actions are: 18- dump: Dump the state of the emulator as a json dictionary. <value> specifies 19 the target filename. 20- stdout: Write <value> to program stdout. 21- stderr: Write <value> to program stderr. 22- stdin: Read a line from stdin and store it in the emulator state. <value> 23 specifies the dictionary key. 24""" 25 26 27class MyResponder(MockGDBServerResponder): 28 def __init__(self, state): 29 super().__init__() 30 self._state = state 31 32 def cont(self): 33 for a in self._state["args"]: 34 action, data = a.split(":", 1) 35 if action == "dump": 36 with open(data, "w") as f: 37 json.dump(self._state, f) 38 elif action == "stdout": 39 sys.stdout.write(data) 40 sys.stdout.flush() 41 elif action == "stderr": 42 sys.stderr.write(data) 43 sys.stderr.flush() 44 elif action == "stdin": 45 self._state[data] = sys.stdin.readline() 46 else: 47 print("Unknown action: %r\n" % a) 48 return "X01" 49 return "W47" 50 51 52class FakeEmulator(MockGDBServer): 53 def __init__(self, addr, state): 54 super().__init__(UnixServerSocket(addr)) 55 self.responder = MyResponder(state) 56 57 58def main(): 59 parser = argparse.ArgumentParser( 60 description=_description, formatter_class=argparse.RawDescriptionHelpFormatter 61 ) 62 parser.add_argument("-g", metavar="unix-socket", required=True) 63 parser.add_argument("-0", metavar="arg0") 64 parser.add_argument("-fake-arg", dest="fake-arg") 65 parser.add_argument("program", help="The program to 'emulate'.") 66 parser.add_argument("args", nargs=argparse.REMAINDER) 67 args = parser.parse_args() 68 69 state = vars(args) 70 state["environ"] = dict(os.environ) 71 emulator = FakeEmulator(args.g, state) 72 emulator.run() 73 74 75if __name__ == "__main__": 76 main() 77