[Workflow] Try to fix code-formatter failing to find changes in some cases.
[llvm-project.git] / lldb / utils / lldb-repro / lldb-repro.py
blob30788d6a815e1aafea347d4d70d212171f6c410b
1 #!/usr/bin/env python
2 """lldb-repro
4 lldb-repro is a utility to transparently capture and replay debugger sessions
5 through the command line driver. Its used to test the reproducers by running
6 the test suite twice.
8 During the first run, with 'capture' as its first argument, it captures a
9 reproducer for every lldb invocation and saves it to a well-know location
10 derived from the arguments and current working directory.
12 During the second run, with 'replay' as its first argument, the test suite is
13 run again but this time every invocation of lldb replays the previously
14 recorded session.
15 """
17 import hashlib
18 import os
19 import shutil
20 import subprocess
21 import sys
22 import tempfile
25 def help():
26 print("usage: {} capture|replay [args]".format(sys.argv[0]))
29 def main():
30 if len(sys.argv) < 2:
31 help()
32 return 1
34 # Compute an MD5 hash based on the input arguments and the current working
35 # directory.
36 h = hashlib.md5()
37 h.update(" ".join(sys.argv[2:]).encode("utf-8"))
38 h.update(os.getcwd().encode("utf-8"))
39 input_hash = h.hexdigest()
41 # Use the hash to "uniquely" identify a reproducer path.
42 reproducer_path = os.path.join(tempfile.gettempdir(), input_hash)
44 # Create a new lldb invocation with capture or replay enabled.
45 lldb = os.path.join(os.path.dirname(sys.argv[0]), "lldb")
46 new_args = [lldb]
47 if sys.argv[1] == "replay":
48 new_args.extend(["--replay", reproducer_path])
49 elif sys.argv[1] == "capture":
50 new_args.extend(
52 "--capture",
53 "--capture-path",
54 reproducer_path,
55 "--reproducer-generate-on-exit",
58 new_args.extend(sys.argv[2:])
59 else:
60 help()
61 return 1
63 exit_code = subprocess.call(new_args)
65 # The driver always exists with a zero exit code during replay. Store the
66 # exit code and return that for tests that expect a non-zero exit code.
67 exit_code_path = os.path.join(reproducer_path, "exit_code.txt")
68 if sys.argv[1] == "replay":
69 replay_exit_code = exit_code
70 with open(exit_code_path, "r") as f:
71 exit_code = int(f.read())
72 if replay_exit_code != 0:
73 print("error: replay failed with exit code {}".format(replay_exit_code))
74 print("invocation: " + " ".join(new_args))
75 # Return 1 if the expected exit code is 0 or vice versa.
76 return 1 if (exit_code == 0) else 0
77 shutil.rmtree(reproducer_path, True)
78 elif sys.argv[1] == "capture":
79 with open(exit_code_path, "w") as f:
80 f.write("%d" % exit_code)
82 return exit_code
85 if __name__ == "__main__":
86 exit(main())