xref: /llvm-project/lldb/test/API/qemu/qemu.py (revision 249a5fb005ea27b57d12fc4425d6f1039d85c1cb)
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
26class MyResponder(MockGDBServerResponder):
27    def __init__(self, state):
28        super().__init__()
29        self._state = state
30
31    def cont(self):
32        for a in self._state["args"]:
33            action, data = a.split(":", 1)
34            if action == "dump":
35                with open(data, "w") as f:
36                    json.dump(self._state, f)
37            elif action == "stdout":
38                sys.stdout.write(data)
39                sys.stdout.flush()
40            elif action == "stderr":
41                sys.stderr.write(data)
42                sys.stderr.flush()
43            elif action == "stdin":
44                self._state[data] = sys.stdin.readline()
45            else:
46                print("Unknown action: %r\n" % a)
47                return "X01"
48        return "W47"
49
50class FakeEmulator(MockGDBServer):
51    def __init__(self, addr, state):
52        super().__init__(UnixServerSocket(addr))
53        self.responder = MyResponder(state)
54
55def main():
56    parser = argparse.ArgumentParser(description=_description,
57            formatter_class=argparse.RawDescriptionHelpFormatter)
58    parser.add_argument('-g', metavar="unix-socket", required=True)
59    parser.add_argument('-0', metavar="arg0")
60    parser.add_argument('-fake-arg', dest="fake-arg")
61    parser.add_argument('program', help="The program to 'emulate'.")
62    parser.add_argument("args", nargs=argparse.REMAINDER)
63    args = parser.parse_args()
64
65    state = vars(args)
66    state["environ"] = dict(os.environ)
67    emulator = FakeEmulator(args.g, state)
68    emulator.run()
69
70if __name__ == "__main__":
71    main()
72