|
| 1 | +import re |
| 2 | +import sys |
| 3 | +import textwrap |
| 4 | +import unittest |
| 5 | +from dataclasses import dataclass |
| 6 | +from test import support |
| 7 | +from test.support.script_helper import run_python_until_end |
| 8 | +from typing import Dict, List |
| 9 | + |
| 10 | +_strace_binary = "/usr/bin/strace" |
| 11 | +_syscall_regex = re.compile( |
| 12 | + r"(?P<syscall>[^(]*)\((?P<args>[^)]*)\)\s*[=]\s*(?P<returncode>.+)") |
| 13 | +_returncode_regex = re.compile( |
| 14 | + r"\+\+\+ exited with (?P<returncode>\d+) \+\+\+") |
| 15 | + |
| 16 | +# Cached value of whether or not there is a compatible strace binary |
| 17 | +_strace_working: bool | None = None |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class StraceEvent: |
| 22 | + syscall: str |
| 23 | + args: List[str] |
| 24 | + returncode: str |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class StraceResult: |
| 29 | + strace_returncode: int |
| 30 | + python_returncode: int |
| 31 | + _raw_events: str |
| 32 | + stdout: str |
| 33 | + stderr: str |
| 34 | + |
| 35 | + def events(self) -> List[StraceEvent]: |
| 36 | + """Extract the call list information from _raw_events""" |
| 37 | + matches = [ |
| 38 | + _syscall_regex.match(event) |
| 39 | + for event in self._raw_events.splitlines() |
| 40 | + ] |
| 41 | + return [ |
| 42 | + StraceEvent(match["syscall"], |
| 43 | + [arg.strip() for arg in (match["args"].split(","))], |
| 44 | + match["returncode"]) for match in matches if match |
| 45 | + ] |
| 46 | + |
| 47 | + def sections(self) -> Dict[str:List[StraceEvent]]: |
| 48 | + """Find all "MARK <X>" writes and use them to make groups of events. |
| 49 | +
|
| 50 | + This is useful to avoid variable / overhead strace events, like that |
| 51 | + at interpreter startup, so a test can just check does the small case |
| 52 | + under study work.""" |
| 53 | + current_section = "__startup" |
| 54 | + sections = {current_section: []} |
| 55 | + for event in self.events(): |
| 56 | + if event.syscall == 'write' and len( |
| 57 | + event.args) > 2 and event.args[1].startswith("\"MARK "): |
| 58 | + # Found a new section, don't include the write in the section |
| 59 | + # but all events until next mark should be in that section |
| 60 | + current_section = event.args[1].split( |
| 61 | + " ", 1)[1].removesuffix('\\n"') |
| 62 | + if current_section not in sections: |
| 63 | + sections[current_section] = list() |
| 64 | + else: |
| 65 | + sections[current_section].append(event) |
| 66 | + |
| 67 | + return sections |
| 68 | + |
| 69 | + |
| 70 | +@support.requires_subprocess() |
| 71 | +def strace_python(code: str, |
| 72 | + strace_flags: List[str], |
| 73 | + check: bool = True) -> StraceResult: |
| 74 | + """Run strace and return the trace. |
| 75 | +
|
| 76 | + Sets strace_returncode and python_returncode to `-1` on error |
| 77 | + """ |
| 78 | + res = None |
| 79 | + |
| 80 | + def _make_error(reason, details): |
| 81 | + return StraceResult( |
| 82 | + strace_returncode=-1, |
| 83 | + python_returncode=-1, |
| 84 | + _raw_events=f"error({reason},details={details}) = -1", |
| 85 | + stdout=res.out if res else "", |
| 86 | + stderr=res.err if res else "") |
| 87 | + |
| 88 | + # Run strace, and get out the raw text |
| 89 | + try: |
| 90 | + res, cmd_line = run_python_until_end( |
| 91 | + "-c", |
| 92 | + textwrap.dedent(code), |
| 93 | + __run_using_command=[_strace_binary] + strace_flags) |
| 94 | + except OSError as err: |
| 95 | + return _make_error("Caught OSError", err) |
| 96 | + |
| 97 | + if check and res.rc: |
| 98 | + res.fail(cmd_line) |
| 99 | + |
| 100 | + # Get out program returncode |
| 101 | + decoded = res.err.decode().strip() |
| 102 | + |
| 103 | + output = decoded.rsplit("\n", 1) |
| 104 | + if len(output) != 2: |
| 105 | + return _make_error("Expected strace events and exit code line", |
| 106 | + decoded[-50:]) |
| 107 | + |
| 108 | + returncode_match = _returncode_regex.match(output[1]) |
| 109 | + if not returncode_match: |
| 110 | + return _make_error("Expected to find returncode in last line.", |
| 111 | + output[1][:50]) |
| 112 | + |
| 113 | + python_returncode = int(returncode_match["returncode"]) |
| 114 | + if check and python_returncode: |
| 115 | + res.fail(cmd_line) |
| 116 | + |
| 117 | + return StraceResult(strace_returncode=res.rc, |
| 118 | + python_returncode=python_returncode, |
| 119 | + _raw_events=output[0], |
| 120 | + stdout=res.out, |
| 121 | + stderr=res.err) |
| 122 | + |
| 123 | + |
| 124 | +def _get_events(code: str, strace_flags: List[str], prelude: str, |
| 125 | + cleanup: str) -> List[StraceEvent]: |
| 126 | + # NOTE: The flush is currently required to prevent the prints from getting |
| 127 | + # buffered and done all at once at exit |
| 128 | + prelude = textwrap.dedent(prelude) |
| 129 | + code = textwrap.dedent(code) |
| 130 | + cleanup = textwrap.dedent(cleanup) |
| 131 | + to_run = f""" |
| 132 | +print("MARK prelude", flush=True) |
| 133 | +{prelude} |
| 134 | +print("MARK code", flush=True) |
| 135 | +{code} |
| 136 | +print("MARK cleanup", flush=True) |
| 137 | +{cleanup} |
| 138 | +print("MARK __shutdown", flush=True) |
| 139 | + """ |
| 140 | + trace = strace_python(to_run, strace_flags) |
| 141 | + all_sections = trace.sections() |
| 142 | + return all_sections['code'] |
| 143 | + |
| 144 | + |
| 145 | +def get_syscalls(code: str, |
| 146 | + strace_flags: List[str], |
| 147 | + prelude: str = "", |
| 148 | + cleanup: str = "") -> List[str]: |
| 149 | + """Get the syscalls which a given chunk of python code generates""" |
| 150 | + events = _get_events(code, strace_flags, prelude=prelude, cleanup=cleanup) |
| 151 | + return [ev.syscall for ev in events] |
| 152 | + |
| 153 | + |
| 154 | +def _can_strace(): |
| 155 | + res = strace_python("import sys; sys.exit(0)", [], check=False) |
| 156 | + assert res.events(), "Should have parsed multiple calls" |
| 157 | + |
| 158 | + global _strace_working |
| 159 | + _strace_working = res.strace_returncode == 0 and res.python_returncode == 0 |
| 160 | + |
| 161 | + |
| 162 | +def requires_strace(): |
| 163 | + if sys.platform != "linux": |
| 164 | + return unittest.skip("Linux only, requires strace.") |
| 165 | + # Moderately expensive (spawns a subprocess), so share results when possible. |
| 166 | + if _strace_working is None: |
| 167 | + _can_strace() |
| 168 | + |
| 169 | + assert _strace_working is not None, "Should have been set by _can_strace" |
| 170 | + return unittest.skipUnless(_strace_working, "Requires working strace") |
| 171 | + |
| 172 | + |
| 173 | +__all__ = ["requires_strace", "strace_python", "StraceResult", "StraceEvent"] |
0 commit comments