sensorium-trace 0.18.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sensorium/__init__.py +0 -0
- sensorium/__main__.py +4 -0
- sensorium/_c/rt/sensorium_c_rt.c +1446 -0
- sensorium/_typescript/package-lock.json +68 -0
- sensorium/_typescript/package.json +39 -0
- sensorium/_typescript/src/bindings.mjs +792 -0
- sensorium/_typescript/src/cap.mjs +227 -0
- sensorium/_typescript/src/capabilities.mjs +40 -0
- sensorium/_typescript/src/dbg.mjs +348 -0
- sensorium/_typescript/src/escape.mjs +351 -0
- sensorium/_typescript/src/focus.mjs +135 -0
- sensorium/_typescript/src/hook.mjs +127 -0
- sensorium/_typescript/src/index.mjs +1 -0
- sensorium/_typescript/src/naming.mjs +63 -0
- sensorium/_typescript/src/positions.mjs +37 -0
- sensorium/_typescript/src/probe.mjs +246 -0
- sensorium/_typescript/src/qualname.mjs +236 -0
- sensorium/_typescript/src/redact.mjs +711 -0
- sensorium/_typescript/src/register.mjs +15 -0
- sensorium/_typescript/src/resolve.mjs +260 -0
- sensorium/_typescript/src/rt.mjs +798 -0
- sensorium/_typescript/src/setup.mjs +11 -0
- sensorium/_typescript/src/tally.mjs +164 -0
- sensorium/_typescript/src/tasks.mjs +175 -0
- sensorium/_typescript/src/transform.mjs +800 -0
- sensorium/_typescript/src/vite.mjs +99 -0
- sensorium/c/__init__.py +8 -0
- sensorium/c/cc.py +612 -0
- sensorium/c/cli.py +60 -0
- sensorium/c/convert.py +669 -0
- sensorium/c/decode.py +300 -0
- sensorium/c/driver.py +365 -0
- sensorium/c/dwarf.py +706 -0
- sensorium/c/format.py +271 -0
- sensorium/c/pkg.py +187 -0
- sensorium/c/plan.py +277 -0
- sensorium/cli.py +140 -0
- sensorium/driver.py +47 -0
- sensorium/exit.py +33 -0
- sensorium/invocations.py +83 -0
- sensorium/limits.py +253 -0
- sensorium/mcp/__init__.py +0 -0
- sensorium/mcp/audit.py +162 -0
- sensorium/mcp/child.py +240 -0
- sensorium/mcp/cmd.py +101 -0
- sensorium/mcp/jsonrpc.py +172 -0
- sensorium/mcp/result.py +187 -0
- sensorium/mcp/schema.py +390 -0
- sensorium/mcp/server.py +493 -0
- sensorium/mcp/tools.py +299 -0
- sensorium/paths.py +80 -0
- sensorium/query/__init__.py +0 -0
- sensorium/query/caps.py +150 -0
- sensorium/query/dbg_dialects.py +64 -0
- sensorium/query/diff_cmd.py +748 -0
- sensorium/query/diff_notes.py +117 -0
- sensorium/query/exceptions_c.py +263 -0
- sensorium/query/exceptions_cmd.py +788 -0
- sensorium/query/exceptions_group.py +571 -0
- sensorium/query/exceptions_invocation.py +487 -0
- sensorium/query/exceptions_rust.py +683 -0
- sensorium/query/exceptions_typescript.py +688 -0
- sensorium/query/expr.py +606 -0
- sensorium/query/failures_cmd.py +306 -0
- sensorium/query/flow_cmd.py +778 -0
- sensorium/query/flow_report.py +128 -0
- sensorium/query/flow_values.py +195 -0
- sensorium/query/fmt.py +280 -0
- sensorium/query/frame_cmd.py +237 -0
- sensorium/query/grep_cmd.py +156 -0
- sensorium/query/help.py +18 -0
- sensorium/query/info_cmd.py +527 -0
- sensorium/query/info_rust.py +214 -0
- sensorium/query/info_typescript.py +261 -0
- sensorium/query/js_inspect.py +394 -0
- sensorium/query/moves.py +254 -0
- sensorium/query/redact_cmd.py +304 -0
- sensorium/query/refocus_cmd.py +661 -0
- sensorium/query/refocus_env.py +493 -0
- sensorium/query/refocus_facts.py +269 -0
- sensorium/query/refocus_licence.py +187 -0
- sensorium/query/refocus_report.py +327 -0
- sensorium/query/refocus_rust.py +537 -0
- sensorium/query/refocus_threads.py +197 -0
- sensorium/query/refocus_typescript.py +756 -0
- sensorium/query/refocus_world.py +666 -0
- sensorium/query/runs_cmd.py +213 -0
- sensorium/query/rust_debug.py +176 -0
- sensorium/query/sites.py +171 -0
- sensorium/query/tree_cmd.py +511 -0
- sensorium/query/tree_threads.py +168 -0
- sensorium/query/vocab.py +723 -0
- sensorium/query/watch_cmd.py +657 -0
- sensorium/record/__init__.py +0 -0
- sensorium/record/boot.py +797 -0
- sensorium/record/boot_io.py +141 -0
- sensorium/record/budget.py +135 -0
- sensorium/record/capture.py +433 -0
- sensorium/record/children.py +755 -0
- sensorium/record/children_merge.py +179 -0
- sensorium/record/elide.py +55 -0
- sensorium/record/fingerprint.py +26 -0
- sensorium/record/thread_starts.py +261 -0
- sensorium/record/tracer.py +798 -0
- sensorium/record/tracer_exc.py +199 -0
- sensorium/record/tracer_frames.py +618 -0
- sensorium/redact.py +304 -0
- sensorium/redact_content.py +156 -0
- sensorium/redact_key.py +317 -0
- sensorium/redact_store.py +502 -0
- sensorium/redact_values.py +433 -0
- sensorium/store/__init__.py +0 -0
- sensorium/store/db.py +284 -0
- sensorium/store/gc.py +360 -0
- sensorium/store/payloads.py +69 -0
- sensorium/store/reader.py +438 -0
- sensorium/store/writer.py +283 -0
- sensorium/ts/__init__.py +10 -0
- sensorium/ts/build.py +783 -0
- sensorium/ts/cli.py +130 -0
- sensorium/ts/driver.py +472 -0
- sensorium/ts/focus.py +146 -0
- sensorium/ts/harness.py +314 -0
- sensorium/ts/ingest.py +380 -0
- sensorium/ts/invocation.py +252 -0
- sensorium/ts/pkg.py +167 -0
- sensorium/ts/redaction.py +64 -0
- sensorium/ts/spool.py +168 -0
- sensorium/ts/truncation.py +81 -0
- sensorium/ts/wrapper.py +255 -0
- sensorium_trace-0.18.2.dist-info/METADATA +245 -0
- sensorium_trace-0.18.2.dist-info/RECORD +135 -0
- sensorium_trace-0.18.2.dist-info/WHEEL +4 -0
- sensorium_trace-0.18.2.dist-info/entry_points.txt +3 -0
- sensorium_trace-0.18.2.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Merge a child spool into the parent trace.
|
|
2
|
+
|
|
3
|
+
Split from `children.py` so that module stays under the line ceiling. The
|
|
4
|
+
parent calls `merge_children` there; these helpers only read a finished
|
|
5
|
+
spool and replay it through the parent's writer.
|
|
6
|
+
"""
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import sqlite3
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from sensorium.record.fingerprint import CAUSAL_KINDS, Fingerprint
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def merge_one(guard, path: Path) -> bool:
|
|
16
|
+
conn = sqlite3.connect(path)
|
|
17
|
+
try:
|
|
18
|
+
frame_link = meta(conn, "parent_frame", None)
|
|
19
|
+
if not frame_exists(guard, frame_link):
|
|
20
|
+
frame_link = None
|
|
21
|
+
depth_delta = link_depth(guard, frame_link)
|
|
22
|
+
strings = _strings(conn)
|
|
23
|
+
codes = {
|
|
24
|
+
row[0]: (row[1], row[2], row[3])
|
|
25
|
+
for row in conn.execute(
|
|
26
|
+
"SELECT id, file, qualname, firstlineno FROM code_objects")
|
|
27
|
+
}
|
|
28
|
+
frames = {
|
|
29
|
+
row[0]: row
|
|
30
|
+
for row in conn.execute(
|
|
31
|
+
"SELECT id, parent_id, code_id, call_event_id, depth, "
|
|
32
|
+
"thread_id, kind, return_event_id, closed_by, unwind_exc "
|
|
33
|
+
"FROM frames")
|
|
34
|
+
}
|
|
35
|
+
by_call = {row[3]: row for row in frames.values()}
|
|
36
|
+
events = list(conn.execute(
|
|
37
|
+
"SELECT id, ts_ns, thread_id, kind, frame_id, code_id, line, "
|
|
38
|
+
"payload FROM events ORDER BY id"))
|
|
39
|
+
finally:
|
|
40
|
+
conn.close()
|
|
41
|
+
if not frames:
|
|
42
|
+
return False
|
|
43
|
+
code_ids: dict[int, int] = {}
|
|
44
|
+
frame_ids: dict[int, int] = {}
|
|
45
|
+
thread_ids: dict[int, int] = {}
|
|
46
|
+
next_thread = max_thread(guard) + 1
|
|
47
|
+
fps: dict[int, Fingerprint] = {}
|
|
48
|
+
for (eid, ts, tid, kind, fid, cid, line, payload) in events:
|
|
49
|
+
if cid not in codes:
|
|
50
|
+
continue
|
|
51
|
+
file, qual, first = codes[cid]
|
|
52
|
+
if cid not in code_ids:
|
|
53
|
+
code_ids[cid] = guard.intern_code(file, qual, first)
|
|
54
|
+
if tid not in thread_ids:
|
|
55
|
+
thread_ids[tid] = next_thread
|
|
56
|
+
next_thread += 1
|
|
57
|
+
new_tid = thread_ids[tid]
|
|
58
|
+
new_fid = frame_ids.get(fid) if fid is not None else None
|
|
59
|
+
new_eid = guard.add_event(
|
|
60
|
+
ts, new_tid, kind, new_fid, code_ids[cid], line,
|
|
61
|
+
_payload(payload, strings), task_id=None)
|
|
62
|
+
if kind in CAUSAL_KINDS:
|
|
63
|
+
fps.setdefault(new_tid, Fingerprint()).update(file, qual, kind)
|
|
64
|
+
opened = by_call.get(eid)
|
|
65
|
+
if kind == "CALL" and opened is not None:
|
|
66
|
+
parent_old = opened[1]
|
|
67
|
+
if parent_old is None:
|
|
68
|
+
parent_new = frame_link
|
|
69
|
+
else:
|
|
70
|
+
parent_new = frame_ids.get(parent_old)
|
|
71
|
+
frame_ids[opened[0]] = guard.open_frame(
|
|
72
|
+
parent_new, code_ids[cid], new_eid,
|
|
73
|
+
int(opened[4]) + depth_delta, new_tid,
|
|
74
|
+
opened[6] or "function")
|
|
75
|
+
row = frames.get(fid) if fid is not None else None
|
|
76
|
+
if row is not None and row[7] == eid and fid in frame_ids:
|
|
77
|
+
exc = row[9]
|
|
78
|
+
if isinstance(exc, str):
|
|
79
|
+
try:
|
|
80
|
+
exc = json.loads(exc)
|
|
81
|
+
except json.JSONDecodeError:
|
|
82
|
+
exc = None
|
|
83
|
+
guard.close_frame(frame_ids[fid], new_eid, row[8] or "return", exc)
|
|
84
|
+
for tid, fp in fps.items():
|
|
85
|
+
guard.write_fingerprint(tid, fp.hexdigest(), fp.count)
|
|
86
|
+
return True
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def frame_exists(guard, fid) -> bool:
|
|
90
|
+
if not isinstance(fid, int) or fid <= 0:
|
|
91
|
+
return False
|
|
92
|
+
writer = guard._w
|
|
93
|
+
writer.flush()
|
|
94
|
+
row = writer._conn.execute(
|
|
95
|
+
"SELECT 1 FROM frames WHERE id = ?", (fid,)).fetchone()
|
|
96
|
+
return row is not None
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def link_depth(guard, fid) -> int:
|
|
100
|
+
"""How much to add so a child root sits one deeper than its parent."""
|
|
101
|
+
if not isinstance(fid, int) or fid <= 0:
|
|
102
|
+
return 0
|
|
103
|
+
writer = guard._w
|
|
104
|
+
row = writer._conn.execute(
|
|
105
|
+
"SELECT depth FROM frames WHERE id = ?", (fid,)).fetchone()
|
|
106
|
+
if row is None:
|
|
107
|
+
return 0
|
|
108
|
+
return int(row[0]) + 1
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def spool_status(path: Path) -> tuple[bool, int | None]:
|
|
112
|
+
conn = sqlite3.connect(path)
|
|
113
|
+
try:
|
|
114
|
+
done = meta(conn, "incomplete", True) is False
|
|
115
|
+
finally:
|
|
116
|
+
conn.close()
|
|
117
|
+
try:
|
|
118
|
+
pid = int(path.stem)
|
|
119
|
+
except ValueError:
|
|
120
|
+
pid = None
|
|
121
|
+
return done, pid
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def process_alive(pid: int | None) -> bool:
|
|
125
|
+
if not isinstance(pid, int) or pid <= 0:
|
|
126
|
+
return False
|
|
127
|
+
try:
|
|
128
|
+
os.kill(pid, 0)
|
|
129
|
+
except ProcessLookupError:
|
|
130
|
+
return False
|
|
131
|
+
except PermissionError:
|
|
132
|
+
return True
|
|
133
|
+
except OSError:
|
|
134
|
+
return False
|
|
135
|
+
return True
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def max_thread(guard) -> int:
|
|
139
|
+
writer = guard._w
|
|
140
|
+
writer.flush()
|
|
141
|
+
found = 0
|
|
142
|
+
for table in ("events", "fingerprints", "frames"):
|
|
143
|
+
row = writer._conn.execute(
|
|
144
|
+
f"SELECT COALESCE(MAX(thread_id), 0) FROM {table}").fetchone()
|
|
145
|
+
found = max(found, row[0] or 0)
|
|
146
|
+
return found
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _strings(conn) -> dict[int, str]:
|
|
150
|
+
try:
|
|
151
|
+
return {i: body for i, body in conn.execute(
|
|
152
|
+
"SELECT id, body FROM strings")}
|
|
153
|
+
except sqlite3.OperationalError:
|
|
154
|
+
return {}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _payload(raw, strings: dict[int, str]):
|
|
158
|
+
if raw is None:
|
|
159
|
+
return None
|
|
160
|
+
if isinstance(raw, str) and raw.startswith("#") and raw[1:].isdigit():
|
|
161
|
+
body = strings.get(int(raw[1:]))
|
|
162
|
+
raw = body
|
|
163
|
+
if raw is None:
|
|
164
|
+
return None
|
|
165
|
+
try:
|
|
166
|
+
return json.loads(raw)
|
|
167
|
+
except (TypeError, json.JSONDecodeError):
|
|
168
|
+
return None
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def meta(conn, key, default):
|
|
172
|
+
row = conn.execute(
|
|
173
|
+
"SELECT value FROM meta WHERE key = ?", (key,)).fetchone()
|
|
174
|
+
if row is None:
|
|
175
|
+
return default
|
|
176
|
+
try:
|
|
177
|
+
return json.loads(row[0])
|
|
178
|
+
except json.JSONDecodeError:
|
|
179
|
+
return default
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Put an elided frame back into the trace because a failure is crossing it.
|
|
2
|
+
|
|
3
|
+
The raising frame and each elided ancestor are written outermost-first, so
|
|
4
|
+
the parent row exists before the child names it. The payload is a skeleton:
|
|
5
|
+
the arguments were not read. ``note_failure`` spends one elided count, so a
|
|
6
|
+
second callback for the same activation does not write a second CALL.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
_WALK = 64
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def materialize_elided(tracer, tls, frame) -> None:
|
|
16
|
+
budget = tracer._budget
|
|
17
|
+
if budget is None or frame is None:
|
|
18
|
+
return
|
|
19
|
+
chain = []
|
|
20
|
+
seen = 0
|
|
21
|
+
current = frame
|
|
22
|
+
while current is not None and seen < _WALK:
|
|
23
|
+
seen += 1
|
|
24
|
+
key = id(current)
|
|
25
|
+
if key in tls.live or tracer._parked_has(key):
|
|
26
|
+
break
|
|
27
|
+
code = current.f_code
|
|
28
|
+
if budget.slot(id(code)) is not None:
|
|
29
|
+
chain.append(current)
|
|
30
|
+
current = current.f_back
|
|
31
|
+
for current in reversed(chain):
|
|
32
|
+
_one(tracer, tls, current)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _one(tracer, tls, frame) -> None:
|
|
36
|
+
if id(frame) in tls.live:
|
|
37
|
+
return
|
|
38
|
+
code = frame.f_code
|
|
39
|
+
traced, fp_file, qual, _focused, kind, win_key = tracer._decide(code)
|
|
40
|
+
if not traced or not tracer._budget.note_failure(id(code)):
|
|
41
|
+
return
|
|
42
|
+
tid = tls.thread_serial
|
|
43
|
+
task = tracer._task_serial(tls)
|
|
44
|
+
cid = tracer.writer.intern_code(code.co_filename, qual, code.co_firstlineno)
|
|
45
|
+
parent = tracer._parent_of(tls, frame.f_back)
|
|
46
|
+
payload = {"args": {}, "unread": ["locals"], "skeleton": "failure"}
|
|
47
|
+
eid = tracer.writer.add_event(
|
|
48
|
+
time.monotonic_ns(), tid, "CALL", None, cid, code.co_firstlineno,
|
|
49
|
+
payload, task_id=task)
|
|
50
|
+
pfid = parent[0] if parent is not None else None
|
|
51
|
+
depth = parent[4] + 1 if parent is not None else 0
|
|
52
|
+
fid = tracer.writer.open_frame(pfid, cid, eid, depth, tid, kind)
|
|
53
|
+
in_window = bool(win_key is not None or (parent is not None and parent[5]))
|
|
54
|
+
tls.live[id(frame)] = [fid, code, cid, {}, depth, in_window, False]
|
|
55
|
+
tracer._fp_for(tid, task).update(fp_file, qual, "CALL")
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Per-thread AND per-task causal fingerprints: a rolling hash over the
|
|
2
|
+
(code, kind) sequence. Values, timing, and LINE events are deliberately
|
|
3
|
+
excluded so capture depth can never alter the fingerprint (spec section 4).
|
|
4
|
+
|
|
5
|
+
One class, two uses (spec D6): a thread's fingerprint covers the causal
|
|
6
|
+
events that ran in NO asyncio task, and every task serial owns one of its
|
|
7
|
+
own. Which events go where is the tracer's decision (`_fp_for`), not this
|
|
8
|
+
file's -- what a hash covers is recorded in the trace's `fingerprint_basis`
|
|
9
|
+
so a reader is never left to infer it from the digest.
|
|
10
|
+
"""
|
|
11
|
+
import hashlib
|
|
12
|
+
|
|
13
|
+
CAUSAL_KINDS = ("CALL", "RETURN", "RAISE", "HANDLED")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Fingerprint:
|
|
17
|
+
def __init__(self) -> None:
|
|
18
|
+
self._h = hashlib.blake2b(digest_size=16)
|
|
19
|
+
self.count = 0
|
|
20
|
+
|
|
21
|
+
def update(self, file: str, qualname: str, kind: str) -> None:
|
|
22
|
+
self._h.update(f"{file}\x1f{qualname}\x1f{kind}\n".encode())
|
|
23
|
+
self.count += 1
|
|
24
|
+
|
|
25
|
+
def hexdigest(self) -> str:
|
|
26
|
+
return self._h.hexdigest()
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""Who started a thread, as a frame id the trace actually holds.
|
|
2
|
+
|
|
3
|
+
A `threading.Thread` body is a root: its caller is the stdlib bootstrap on
|
|
4
|
+
the new thread, so parentage never reaches the `start` or `submit` that
|
|
5
|
+
created it. The audit hook only counts those creations. This log records,
|
|
6
|
+
per thread the run actually started through `Thread.start`, the tracer
|
|
7
|
+
serial events use, the thread's name, and the recorded frame that asked
|
|
8
|
+
for it.
|
|
9
|
+
|
|
10
|
+
The OS ident is not stored. It is recycled, and `events.thread_id` is the
|
|
11
|
+
tracer serial minted in `_assign_thread_serial`, not `get_ident()`. The
|
|
12
|
+
serial is minted on the new thread's first monitoring callback, which is
|
|
13
|
+
`_bootstrap`, before `_bootstrap_inner` publishes the Thread. The Thread
|
|
14
|
+
object is already the `self` of that `_bootstrap` frame, so the link is
|
|
15
|
+
made there, against the object the parent registered before `start`
|
|
16
|
+
returned. Calling `current_thread()` here would insert a `_DummyThread`.
|
|
17
|
+
"""
|
|
18
|
+
import os
|
|
19
|
+
import sys
|
|
20
|
+
import threading
|
|
21
|
+
import weakref
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_SELF = os.path.realpath(__file__)
|
|
25
|
+
_THREADING = os.path.realpath(threading.__file__)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _norm(filename: str) -> str:
|
|
29
|
+
return filename.replace("\\", "/")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _same_file(filename: str, target: str) -> bool:
|
|
33
|
+
try:
|
|
34
|
+
return os.path.realpath(filename) == target
|
|
35
|
+
except OSError:
|
|
36
|
+
return False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _is_start_machinery(filename: str) -> bool:
|
|
40
|
+
"""Frames that sit between the user's call and the new thread.
|
|
41
|
+
|
|
42
|
+
`Thread.start` itself, and `Executor.submit`'s path through
|
|
43
|
+
`concurrent.futures`, are not the caller `tree` should name. This
|
|
44
|
+
module is the wrapper around `start` and is not that caller either.
|
|
45
|
+
"""
|
|
46
|
+
if _same_file(filename, _SELF) or _same_file(filename, _THREADING):
|
|
47
|
+
return True
|
|
48
|
+
norm = _norm(filename)
|
|
49
|
+
return ("/concurrent/futures/" in norm
|
|
50
|
+
or norm.endswith("/concurrent/futures.py"))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _starter_frame(tracer) -> int | None:
|
|
54
|
+
"""The recorded frame that called `start` or `submit`, or None.
|
|
55
|
+
|
|
56
|
+
The nearest frame outside the start machinery is the caller. It counts
|
|
57
|
+
only when this recorder opened it: a frame that ran before tracing, or
|
|
58
|
+
in a C extension, or in code this run did not trace, is not a frame id
|
|
59
|
+
the trace holds, and a further-out frame is a different call.
|
|
60
|
+
"""
|
|
61
|
+
tls = tracer._tls
|
|
62
|
+
frame = sys._getframe()
|
|
63
|
+
while frame is not None:
|
|
64
|
+
if not _is_start_machinery(frame.f_code.co_filename):
|
|
65
|
+
entry = tls.live.get(id(frame))
|
|
66
|
+
if entry is not None and entry[1] is frame.f_code:
|
|
67
|
+
fid = entry[0]
|
|
68
|
+
if type(fid) is int:
|
|
69
|
+
return fid
|
|
70
|
+
return None
|
|
71
|
+
frame = frame.f_back
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _plain_name(thread) -> str | None:
|
|
76
|
+
"""The thread's name as a plain str, or None if it cannot be read.
|
|
77
|
+
|
|
78
|
+
Read on the parent, before `start`, so a name the child changes later
|
|
79
|
+
is not what the starter asked for. A failing `name` must not stop the
|
|
80
|
+
thread from starting.
|
|
81
|
+
"""
|
|
82
|
+
try:
|
|
83
|
+
name = thread.name
|
|
84
|
+
except BaseException:
|
|
85
|
+
return None
|
|
86
|
+
if type(name) is not str:
|
|
87
|
+
try:
|
|
88
|
+
name = str.__str__(name)
|
|
89
|
+
except BaseException:
|
|
90
|
+
return None
|
|
91
|
+
if type(name) is not str or name == "":
|
|
92
|
+
return None
|
|
93
|
+
return name
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _is_real_thread(obj) -> bool:
|
|
97
|
+
try:
|
|
98
|
+
if isinstance(obj, threading._DummyThread):
|
|
99
|
+
return False
|
|
100
|
+
return isinstance(obj, threading.Thread)
|
|
101
|
+
except BaseException:
|
|
102
|
+
return False
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _born_thread():
|
|
106
|
+
"""The Thread object whose serial is being minted, or None.
|
|
107
|
+
|
|
108
|
+
Once the thread is in `_active`, that entry is the Thread. The mint
|
|
109
|
+
happens earlier, inside `_bootstrap`, where the same object is the
|
|
110
|
+
frame's `self` and `_active` does not have it yet. `current_thread()`
|
|
111
|
+
is not used: on a miss it inserts a `_DummyThread`, and the monitoring
|
|
112
|
+
callback that runs inside that dummy's deleter inserts another one
|
|
113
|
+
which is still alive after the worker has been joined. A thread that
|
|
114
|
+
did not come through `Thread` (a C extension, `_thread.start_new_thread`
|
|
115
|
+
with no Thread) has neither, and stays unlinked.
|
|
116
|
+
"""
|
|
117
|
+
try:
|
|
118
|
+
current = threading._active.get(threading.get_ident())
|
|
119
|
+
except BaseException:
|
|
120
|
+
current = None
|
|
121
|
+
if _is_real_thread(current):
|
|
122
|
+
return current
|
|
123
|
+
frame = sys._getframe()
|
|
124
|
+
while frame is not None:
|
|
125
|
+
code = frame.f_code
|
|
126
|
+
if (code.co_name in ("_bootstrap", "_bootstrap_inner", "run")
|
|
127
|
+
and _same_file(code.co_filename, _THREADING)):
|
|
128
|
+
try:
|
|
129
|
+
self_ = frame.f_locals.get("self")
|
|
130
|
+
except BaseException:
|
|
131
|
+
self_ = None
|
|
132
|
+
if _is_real_thread(self_):
|
|
133
|
+
return self_
|
|
134
|
+
frame = frame.f_back
|
|
135
|
+
return None
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class _Log:
|
|
139
|
+
def __init__(self) -> None:
|
|
140
|
+
self._lock = threading.Lock()
|
|
141
|
+
self._pending: weakref.WeakKeyDictionary = weakref.WeakKeyDictionary()
|
|
142
|
+
self._records: list = []
|
|
143
|
+
self._closed = False
|
|
144
|
+
self._orig = None
|
|
145
|
+
self._wrapped = None
|
|
146
|
+
self._hook = None
|
|
147
|
+
|
|
148
|
+
def arm(self, tracer) -> None:
|
|
149
|
+
if self._orig is not None:
|
|
150
|
+
return
|
|
151
|
+
self._orig = threading.Thread.start
|
|
152
|
+
self._hook = self._note
|
|
153
|
+
tracer._thread_born = self._hook
|
|
154
|
+
|
|
155
|
+
def wrapped(thread, /, *args, **kwargs):
|
|
156
|
+
return self._call_start(tracer, thread, args, kwargs)
|
|
157
|
+
|
|
158
|
+
self._wrapped = wrapped
|
|
159
|
+
threading.Thread.start = wrapped
|
|
160
|
+
|
|
161
|
+
def _call_start(self, tracer, thread, args, kwargs):
|
|
162
|
+
# Register before the real start. The child can enter `_bootstrap`
|
|
163
|
+
# and mint its serial before `start` returns, and the pending entry
|
|
164
|
+
# has to be visible by then. A failure to describe the start must
|
|
165
|
+
# not stop the thread: the heading then says the starter was not
|
|
166
|
+
# recorded, which is what happened.
|
|
167
|
+
try:
|
|
168
|
+
frame = _starter_frame(tracer)
|
|
169
|
+
name = _plain_name(thread)
|
|
170
|
+
with self._lock:
|
|
171
|
+
if not self._closed:
|
|
172
|
+
self._pending[thread] = (name, frame)
|
|
173
|
+
except BaseException:
|
|
174
|
+
pass
|
|
175
|
+
try:
|
|
176
|
+
return self._orig(thread, *args, **kwargs)
|
|
177
|
+
except BaseException:
|
|
178
|
+
with self._lock:
|
|
179
|
+
self._pending.pop(thread, None)
|
|
180
|
+
raise
|
|
181
|
+
|
|
182
|
+
def _note(self, serial: int) -> None:
|
|
183
|
+
thread = _born_thread()
|
|
184
|
+
if thread is None:
|
|
185
|
+
return
|
|
186
|
+
with self._lock:
|
|
187
|
+
if self._closed:
|
|
188
|
+
return
|
|
189
|
+
info = self._pending.pop(thread, None)
|
|
190
|
+
if info is None:
|
|
191
|
+
return
|
|
192
|
+
name, frame = info
|
|
193
|
+
self._records.append({
|
|
194
|
+
"thread": serial,
|
|
195
|
+
"name": name,
|
|
196
|
+
"frame": frame,
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
def _restore_start(self) -> None:
|
|
200
|
+
wrapped = self._wrapped
|
|
201
|
+
orig = self._orig
|
|
202
|
+
self._wrapped = None
|
|
203
|
+
self._orig = None
|
|
204
|
+
# Only if we are still the installed start. The program may have
|
|
205
|
+
# replaced it; putting ours back would hide that.
|
|
206
|
+
if (wrapped is not None and orig is not None
|
|
207
|
+
and threading.Thread.start is wrapped):
|
|
208
|
+
threading.Thread.start = orig
|
|
209
|
+
|
|
210
|
+
def finish(self, tracer) -> list:
|
|
211
|
+
# No new mint joins after this. An in-flight `_note` either holds
|
|
212
|
+
# the lock (its row is in the copy) or sees `_closed` and drops,
|
|
213
|
+
# so the list handed to meta is not half-updated.
|
|
214
|
+
if getattr(tracer, "_thread_born", None) is self._hook:
|
|
215
|
+
tracer._thread_born = None
|
|
216
|
+
with self._lock:
|
|
217
|
+
self._closed = True
|
|
218
|
+
rows = [dict(row) for row in self._records]
|
|
219
|
+
self._restore_start()
|
|
220
|
+
return rows
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def arm_thread_starts(tracer) -> None:
|
|
224
|
+
"""Wrap `Thread.start` for the duration of one recorded run."""
|
|
225
|
+
log = _Log()
|
|
226
|
+
# On the tracer before `arm`, so a failure mid-wrap is still undone by
|
|
227
|
+
# `finish` (it is the finally that puts `Thread.start` back).
|
|
228
|
+
tracer._thread_start_log = log
|
|
229
|
+
try:
|
|
230
|
+
log.arm(tracer)
|
|
231
|
+
except BaseException:
|
|
232
|
+
tracer._thread_start_log = None
|
|
233
|
+
if tracer._thread_born is log._hook:
|
|
234
|
+
tracer._thread_born = None
|
|
235
|
+
log._restore_start()
|
|
236
|
+
raise
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def finish_thread_starts(tracer) -> list:
|
|
240
|
+
"""Restore `Thread.start` and return the `thread_starts` rows.
|
|
241
|
+
|
|
242
|
+
Safe when `arm_thread_starts` never ran. Does not raise: finalizing a
|
|
243
|
+
trace must not skip `close` because the wrapper could not be put back.
|
|
244
|
+
"""
|
|
245
|
+
log = getattr(tracer, "_thread_start_log", None)
|
|
246
|
+
tracer._thread_start_log = None
|
|
247
|
+
if log is None:
|
|
248
|
+
return []
|
|
249
|
+
try:
|
|
250
|
+
return log.finish(tracer)
|
|
251
|
+
except BaseException:
|
|
252
|
+
try:
|
|
253
|
+
log._restore_start()
|
|
254
|
+
except BaseException:
|
|
255
|
+
pass
|
|
256
|
+
try:
|
|
257
|
+
if getattr(tracer, "_thread_born", None) is log._hook:
|
|
258
|
+
tracer._thread_born = None
|
|
259
|
+
except BaseException:
|
|
260
|
+
pass
|
|
261
|
+
return []
|