retracesoftware-proxy 0.1.5__py3-none-any.whl → 0.2.4__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.
Files changed (35) hide show
  1. retracesoftware/__main__.py +285 -0
  2. retracesoftware/autoenable.py +53 -0
  3. retracesoftware/config.json +19 -233
  4. retracesoftware/config.yaml +0 -0
  5. retracesoftware/install/config.py +6 -0
  6. retracesoftware/install/edgecases.py +23 -1
  7. retracesoftware/install/patcher.py +98 -513
  8. retracesoftware/install/patchfindspec.py +117 -0
  9. retracesoftware/install/phases.py +338 -0
  10. retracesoftware/install/record.py +111 -40
  11. retracesoftware/install/replace.py +28 -0
  12. retracesoftware/install/replay.py +59 -11
  13. retracesoftware/install/tracer.py +171 -33
  14. retracesoftware/install/typeutils.py +20 -0
  15. retracesoftware/modules.toml +384 -0
  16. retracesoftware/preload.txt +216 -0
  17. retracesoftware/proxy/__init__.py +1 -1
  18. retracesoftware/proxy/globalref.py +31 -0
  19. retracesoftware/proxy/messagestream.py +204 -0
  20. retracesoftware/proxy/proxysystem.py +328 -71
  21. retracesoftware/proxy/proxytype.py +90 -38
  22. retracesoftware/proxy/record.py +109 -119
  23. retracesoftware/proxy/replay.py +94 -188
  24. retracesoftware/proxy/serializer.py +28 -0
  25. retracesoftware/proxy/startthread.py +40 -0
  26. retracesoftware/proxy/stubfactory.py +82 -27
  27. retracesoftware/proxy/thread.py +64 -4
  28. retracesoftware/replay.py +104 -0
  29. retracesoftware/run.py +378 -0
  30. retracesoftware/stackdifference.py +133 -0
  31. {retracesoftware_proxy-0.1.5.dist-info → retracesoftware_proxy-0.2.4.dist-info}/METADATA +2 -1
  32. retracesoftware_proxy-0.2.4.dist-info/RECORD +42 -0
  33. retracesoftware_proxy-0.1.5.dist-info/RECORD +0 -27
  34. {retracesoftware_proxy-0.1.5.dist-info → retracesoftware_proxy-0.2.4.dist-info}/WHEEL +0 -0
  35. {retracesoftware_proxy-0.1.5.dist-info → retracesoftware_proxy-0.2.4.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,9 @@
1
1
  import retracesoftware.functional as functional
2
2
  import retracesoftware_utils as utils
3
3
 
4
+ import os
5
+ import _thread
6
+
4
7
  # def thread_aware_writer(writer):
5
8
  # on_thread_switch = functional.sequence(utils.thread_id(), writer.handle('THREAD_SWITCH'))
6
9
  # return utils.threadawareproxy(on_thread_switch = on_thread_switch, target = writer)
@@ -11,23 +14,35 @@ class ThreadSwitch:
11
14
  def __init__(self, id):
12
15
  self.id = id
13
16
 
14
- def set_thread_id(writer, id):
15
- utils.set_thread_id(writer.handle(ThreadSwitch(id)))
17
+ def __repr__(self):
18
+ return f'ThreadSwitch<{self.id}>'
19
+
20
+ def __str__(self):
21
+ return f'ThreadSwitch<{self.id}>'
22
+
23
+ # def set_thread_id(writer, id):
24
+ # utils.sigtrap(id)
25
+ # utils.set_thread_id(writer.handle(ThreadSwitch(id)))
16
26
 
17
27
  def write_thread_switch(writer):
18
28
  on_thread_switch = functional.repeatedly(functional.sequence(utils.thread_id, writer))
19
29
 
20
30
  return lambda f: utils.thread_aware_proxy(target = f, on_thread_switch = on_thread_switch, sticky = False)
21
31
 
22
- def prefix_with_thread_id(f, current):
32
+ def prefix_with_thread_id(f, thread_id):
33
+ current = None
34
+
23
35
  def next():
24
36
  nonlocal current, f
37
+ if current is None: current = thread_id()
38
+
25
39
  obj = f()
26
40
 
27
41
  while issubclass(type(obj), ThreadSwitch):
28
42
  current = obj.id
29
43
  obj = f()
30
44
 
45
+ # print(f'prefix_with_thread_id: {(current, obj)}')
31
46
  return (current, obj)
32
47
 
33
48
  return next
@@ -36,7 +51,15 @@ def per_thread_messages(messages):
36
51
  thread_id = utils.thread_id
37
52
  # thread_id = lambda: 'FOOOOO!!!'
38
53
 
39
- demux = utils.demux(source = prefix_with_thread_id(messages, thread_id()), key_function = lambda obj: obj[0])
54
+ def on_timeout(demux, key):
55
+ print(f'ON TIMEOUT!!!! {key} pending: {demux.pending} {demux.pending_keys}')
56
+ utils.sigtrap(demux)
57
+ os._exit(1)
58
+
59
+ demux = utils.demux(source = prefix_with_thread_id(messages, thread_id),
60
+ key_function = lambda obj: obj[0],
61
+ timeout_seconds = 60,
62
+ on_timeout = on_timeout)
40
63
 
41
64
  # def next():
42
65
  # thread,message = demux(thread_id())
@@ -44,3 +67,40 @@ def per_thread_messages(messages):
44
67
 
45
68
  # return next
46
69
  return functional.repeatedly(lambda: demux(thread_id())[1])
70
+
71
+
72
+ # _thread.start_new_thread(function, args[, kwargs])
73
+ counters = _thread._local()
74
+ counters.id = ()
75
+ counters.counter = 0
76
+
77
+ def with_thread_id(thread_id, on_exit, function):
78
+ def on_call(*args, **kwargs):
79
+ counters.id = thread_id
80
+ counters.counter = 0
81
+
82
+ def on_result(res):
83
+ on_exit(thread_id)
84
+
85
+ def on_error(*args):
86
+ on_exit(thread_id)
87
+
88
+ return utils.observer(on_call = on_call, on_result = on_result, on_error = on_error, function = function)
89
+
90
+ thread_id = functional.lazy(getattr, counters, 'id')
91
+
92
+ def start_new_thread_wrapper(thread_state, on_exit, start_new_thread):
93
+
94
+ def wrapper(function, *args):
95
+
96
+ next_id = counters.id + (counters.counter,)
97
+ counters.counter += 1
98
+
99
+ wrapped_function = with_thread_id(thread_id = next_id,
100
+ on_exit = on_exit,
101
+ function = thread_state.wrap('internal', function))
102
+
103
+ return start_new_thread(wrapped_function, *args)
104
+
105
+ return thread_state.dispatch(start_new_thread, internal = wrapper)
106
+
@@ -0,0 +1,104 @@
1
+ import retracesoftware.stream as stream
2
+ from retracesoftware.run import install, run_with_retrace, ImmutableTypes, thread_states
3
+ import argparse
4
+ import sys
5
+ from pathlib import Path
6
+ from retracesoftware.proxy.thread import thread_id
7
+ from retracesoftware.proxy.replay import ReplayProxySystem
8
+ import retracesoftware.utils as utils
9
+ import json
10
+ from retracesoftware.stackdifference import on_stack_difference
11
+
12
+ def parse_args():
13
+ parser = argparse.ArgumentParser(
14
+ prog=f"python -m {sys.argv[0]}",
15
+ description="Run a Python module with debugging, logging, etc."
16
+ )
17
+
18
+ parser.add_argument(
19
+ '--verbose',
20
+ action='store_true',
21
+ help='Enable verbose output'
22
+ )
23
+
24
+ parser.add_argument(
25
+ '--timeout', # or '-r'
26
+ type = int, # ensures it's a string (optional, but safe)
27
+ default = 60, # default value if not provided
28
+ help = 'the directory to place the recording files'
29
+ )
30
+
31
+ parser.add_argument(
32
+ '--recording', # or '-r'
33
+ type = str, # ensures it's a string (optional, but safe)
34
+ default = '.', # default value if not provided
35
+ help = 'the directory to place the recording files'
36
+ )
37
+
38
+ return parser.parse_args()
39
+
40
+ def load_json(file):
41
+ with open(file, "r", encoding="utf-8") as f:
42
+ return json.load(f)
43
+
44
+ def main():
45
+ # import debugpy
46
+
47
+ # port = 1977
48
+ # debugpy.listen(("127.0.0.1", port))
49
+ # debugpy.wait_for_client()
50
+
51
+ # self.reader = stream.reader(path,
52
+ # thread = thread_id,
53
+ # timeout_seconds = 60,
54
+ # verbose = verbose,
55
+ # on_stack_difference = thread_state.wrap('disabled', on_stack_difference),
56
+ # magic_markers = magic_markers)
57
+
58
+ # return ReplayProxySystem(thread_state = thread_state,
59
+ # immutable_types = immutable_types,
60
+ # tracing_config = tracing_config,
61
+ # mainscript = mainscript,
62
+ # path = recording_path / 'trace.bin',
63
+ # tracecalls = env_truthy('RETRACE_ALL', False),
64
+ # verbose = verbose,
65
+ # magic_markers = env_truthy('RETRACE_MAGIC_MARKERS', False))
66
+
67
+ args = parse_args()
68
+ path = Path(args.recording)
69
+
70
+ if not path.exists():
71
+ raise Exception(f"Recording path: {path} does not exist")
72
+
73
+ settings = load_json(path / "settings.json")
74
+
75
+ thread_state = utils.ThreadState(*thread_states)
76
+
77
+ with stream.reader(path = path / 'trace.bin',
78
+ thread = thread_id,
79
+ timeout_seconds = args.timeout,
80
+ verbose = args.verbose,
81
+ on_stack_difference = thread_state.wrap('disabled', on_stack_difference),
82
+ magic_markers = settings['magic_markers']) as reader:
83
+
84
+ tracing_config = {}
85
+
86
+ system = ReplayProxySystem(
87
+ reader = reader,
88
+ thread_state = thread_state,
89
+ immutable_types = ImmutableTypes(),
90
+ tracing_config = tracing_config,
91
+ tracecalls = settings['trace_inputs'])
92
+
93
+ install(system)
94
+
95
+ run_with_retrace(system, settings['argv'])
96
+
97
+ # install(system)
98
+
99
+ # run_with_retrace(system, args.rest[1:])
100
+
101
+ # runpy.run_module('foo', run_name="__main__", alter_sys=False)
102
+
103
+ if __name__ == "__main__":
104
+ main()
retracesoftware/run.py ADDED
@@ -0,0 +1,378 @@
1
+ import sys
2
+ import os
3
+ import runpy
4
+ from pathlib import Path
5
+ # from retracesoftware.install.phases import *
6
+ import pkgutil
7
+ import tomllib
8
+ import retracesoftware.functional as functional
9
+ import builtins
10
+ import importlib
11
+ import _imp
12
+ import importlib._bootstrap_external as _bootstrap_external
13
+ import atexit
14
+ import threading
15
+ import _signal
16
+ import retracesoftware.utils as utils
17
+ from retracesoftware.install.patcher import patch_module, create_patcher, patch_imported_module
18
+ from retracesoftware.proxy.startthread import patch_thread_start
19
+ from retracesoftware.install.replace import update
20
+
21
+ thread_states = [
22
+ "disabled", # Default state when retrace is disabled for a thread
23
+ "internal", # Default state when retrace is disabled for a thread
24
+ "external", # When target thread is running outside the python system to be recorded
25
+ "retrace", # When target thread is running outside the retrace system
26
+ "importing", # When target thread is running outside the retrace system
27
+ "gc", # When the target thread is running inside the pyton garbage collector
28
+ ]
29
+
30
+ class ImmutableTypes(set):
31
+ def __init__(self, *args, **kwargs):
32
+ super().__init__(*args, **kwargs)
33
+
34
+ def __contains__(self, item):
35
+ assert isinstance(item, type)
36
+
37
+ if super().__contains__(item):
38
+ return True
39
+
40
+ for elem in self:
41
+ if issubclass(item, elem):
42
+ self.add(item)
43
+ return True
44
+
45
+ return False
46
+
47
+ def load_module_config(filename):
48
+ data = pkgutil.get_data("retracesoftware", filename)
49
+ assert data is not None
50
+ return tomllib.loads(data.decode("utf-8"))
51
+
52
+ def wait_for_non_daemon_threads(timeout=None):
53
+ """
54
+ Wait for all non-daemon threads to finish, just like Python does on exit.
55
+
56
+ Args:
57
+ timeout (float, optional): Max seconds to wait. None = wait forever.
58
+
59
+ Returns:
60
+ bool: True if all threads finished, False if timeout exceeded.
61
+ """
62
+ import threading
63
+ import time
64
+
65
+ start_time = time.time()
66
+ main_thread = threading.main_thread()
67
+
68
+ while True:
69
+ # Get all active threads
70
+ active = threading.enumerate()
71
+
72
+ # Filter: non-daemon and not the main thread
73
+ non_daemon_threads = [
74
+ t for t in active
75
+ if t is not main_thread and not t.daemon
76
+ ]
77
+
78
+ if not non_daemon_threads:
79
+ return True # All done!
80
+
81
+ # Check timeout
82
+ if timeout is not None:
83
+ elapsed = time.time() - start_time
84
+ if elapsed >= timeout:
85
+ print(f"Timeout: {len(non_daemon_threads)} thread(s) still alive")
86
+ return False
87
+
88
+ # Sleep briefly to avoid busy-wait
89
+ time.sleep(0.1)
90
+
91
+ def run_python_command(argv):
92
+ """
93
+ Run a Python app from a command list using runpy.
94
+
95
+ Supports:
96
+ ['-m', 'module', 'arg1', ...] → like `python -m module arg1 ...`
97
+ ['script.py', 'arg1', ...] → like `python script.py arg1 ...`
98
+
99
+ Args:
100
+ argv: List of command-line arguments (first item is either '-m' or script path)
101
+
102
+ Returns:
103
+ Exit code (0 on success, 1+ on error)
104
+ """
105
+ if not argv:
106
+ print("Error: No command provided", file=sys.stderr)
107
+ return 1
108
+
109
+ original_argv = sys.argv[:]
110
+ original_cwd = os.getcwd()
111
+
112
+ try:
113
+ if argv[0] == '-m':
114
+ if len(argv) < 2:
115
+ print("Error: -m requires a module name", file=sys.stderr)
116
+ return 1
117
+ module_name = argv[1]
118
+ module_args = argv[2:]
119
+ sys.argv = ['-m', module_name] + module_args
120
+ runpy.run_module(module_name, run_name="__main__")
121
+ return 0
122
+
123
+ else:
124
+ script_path = argv[0]
125
+ script_args = argv[1:]
126
+ path = Path(script_path)
127
+
128
+ if not path.exists():
129
+ print(f"Error: Script not found: {script_path}", file=sys.stderr)
130
+ return 1
131
+
132
+ if path.suffix != ".py":
133
+ print(f"Error: Not a Python script: {script_path}", file=sys.stderr)
134
+ return 1
135
+
136
+ full_path = str(path.resolve())
137
+ sys.argv = [full_path] + script_args
138
+
139
+ # Change to script's directory
140
+ os.chdir(path.parent)
141
+
142
+ runpy.run_path(full_path, run_name="__main__")
143
+ return 0
144
+
145
+ except ModuleNotFoundError:
146
+ print(f"Error: No module named '{argv[1]}'", file=sys.stderr)
147
+ return 1
148
+ except Exception as e:
149
+ print(f"Error: {e}", file=sys.stderr)
150
+ return 1
151
+ finally:
152
+ sys.argv = original_argv
153
+ os.chdir(original_cwd)
154
+
155
+ def patch_import(thread_state, patcher, sync, checkpoint):
156
+ # builtins.__import__ = thread_state.dispatch(builtins.__import__, internal = sync(thread_state.wrap('importing', builtins.__import__)))
157
+ # bi = builtins.__import__
158
+ # def foo(*args, **kwargs):
159
+ # print(f'in patched __import__: {thread_state.value} {args[0]}')
160
+ # if thread_state.value == 'internal':
161
+ # with thread_state.select('importing'):
162
+ # return bi(*args, **kwargs)
163
+ # else:
164
+ # return bi(*args, **kwargs)
165
+
166
+ # builtins.__import__ = foo
167
+
168
+ builtins.__import__ = thread_state.dispatch(builtins.__import__, internal = thread_state.wrap('importing', builtins.__import__))
169
+
170
+ def exec(source, globals = None, locals = None):
171
+ checkpoint(f"exec module: {globals.get('__name__', 'unknown')}")
172
+ res = builtins.exec(source, globals, locals)
173
+ patcher(globals, False)
174
+ return res
175
+
176
+ def patch(module):
177
+ patcher(module.__dict__, False)
178
+ return module
179
+
180
+ _imp.exec_dynamic = thread_state.dispatch(_imp.exec_dynamic,
181
+ importing = functional.juxt(
182
+ thread_state.wrap('internal', _imp.exec_dynamic),
183
+ patch))
184
+ # _imp.exec_dynamic = lambda mod: utils.sigtrap(f'{thread_state.value} - {mod}')
185
+ # _imp.exec_builtin = lambda mod: utils.sigtrap(f'{thread_state.value} - {mod}')
186
+
187
+ _imp.exec_builtin = thread_state.dispatch(_imp.exec_builtin,
188
+ importing = functional.juxt(
189
+ thread_state.wrap('internal', _imp.exec_builtin),
190
+ patch))
191
+
192
+ # def runpy_exec(source, globals = None, locals = None):
193
+ # print(f'In runpy exec!!!!!')
194
+ # return builtins.exec(source, globals, locals)
195
+
196
+ # utils.update(runpy, "_run_code",
197
+ # utils.wrap_func_with_overrides,
198
+ # exec = runpy_exec)
199
+
200
+ utils.update(_bootstrap_external._LoaderBasics, "exec_module",
201
+ utils.wrap_func_with_overrides,
202
+ exec = thread_state.dispatch(builtins.exec, importing = thread_state.wrap('internal', sync(exec))))
203
+
204
+ preload = [
205
+ "logging",
206
+ "pathlib",
207
+ "_signal",
208
+ "_posixsubprocess",
209
+ "socket",
210
+ "select",
211
+ "ssl",
212
+ "random",
213
+ "email",
214
+ "email.errors",
215
+ "http.client",
216
+
217
+ "json",
218
+ "typing",
219
+ "queue",
220
+ "mimetypes",
221
+ "tempfile",
222
+ "zipfile",
223
+ "importlib.resources",
224
+ "importlib.metadata",
225
+ "encodings.idna"
226
+
227
+ # "http.client",
228
+ # "queue",
229
+ # "mimetypes",
230
+ # "encodings.idna",
231
+ # "hmac",
232
+ # "ipaddress",
233
+ # "tempfile",
234
+ # "zipfile",
235
+ # "importlib.resources",
236
+ # "importlib.metadata",
237
+ # "atexit",
238
+ # "weakref"
239
+ ]
240
+
241
+ # def debugger_is_active():
242
+ # return sys.gettrace() and 'debugpy' in sys.modules
243
+
244
+ def init_weakref():
245
+ import weakref
246
+ def dummy_callback():
247
+ pass
248
+
249
+ class DummyTarget:
250
+ pass
251
+
252
+ f = weakref.finalize(DummyTarget(), dummy_callback)
253
+ f.detach()
254
+
255
+ def wrapped_weakref(ref, thread_state, wrap_callback):
256
+ orig_new = ref.__new__
257
+
258
+ def __new__(cls, ob, callback=None, **kwargs):
259
+ return orig_new(cls, ob, wrap_callback(callback) if callback else None)
260
+
261
+ return type('ref', (ref, ), {'__new__': thread_state.dispatch(orig_new, internal = __new__)})
262
+
263
+ def patch_weakref(thread_state, wrap_callback):
264
+ import _weakref
265
+
266
+ update(_weakref.ref, wrapped_weakref(_weakref.ref, thread_state, wrap_callback))
267
+ # _weakref.ref = wrapped_weakref(_weakref.ref)
268
+
269
+ # def patch_signal(thread_state, wrap_callback):
270
+
271
+ # def wrap_handler(handler):
272
+ # return utils.observer(on_call = ..., function = handler)
273
+
274
+ # _signal.signal =
275
+ # update(_signal.signal, thread_state.dispatch(_signal.signal, internal = thread_state.wrap('internal', _signal.signal)))
276
+
277
+
278
+
279
+ def install(system):
280
+
281
+ patch_weakref(thread_state = system.thread_state, wrap_callback = system.wrap_weakref_callback)
282
+
283
+ init_weakref()
284
+
285
+ preload = pkgutil.get_data("retracesoftware", "preload.txt")
286
+
287
+ for name in preload.decode("utf-8").splitlines():
288
+ try:
289
+ importlib.import_module(name.strip())
290
+ except ModuleNotFoundError:
291
+ pass
292
+
293
+ # if 'pydevd' in sys.modules:
294
+ # utils.update(sys.modules['pydevd'].PyDB, 'enable_tracing', system.disable_for)
295
+ # utils.update(sys.modules['pydevd'].PyDB, 'set_suspend', system.disable_for)
296
+ # utils.update(sys.modules['pydevd'].PyDB, 'do_wait_suspend', system.disable_for)
297
+
298
+ # if '_pydevd_bundle.pydevd_trace_dispatch_regular' in sys.modules:
299
+ # mod = sys.modules['_pydevd_bundle.pydevd_trace_dispatch_regular']
300
+ # utils.update(mod.ThreadTracer, '__call__', system.disable_for)
301
+
302
+ for function in utils.stack_functions():
303
+ system.exclude_from_stacktrace(function)
304
+
305
+ def recursive_disable(func):
306
+ if not callable(func):
307
+ return func
308
+
309
+ def wrapped(*args, **kwargs):
310
+ with system.thread_state.select('disabled'):
311
+ return recursive_disable(func(*args, **kwargs))
312
+
313
+ return wrapped
314
+
315
+ sys.settrace = functional.sequence(recursive_disable, sys.settrace)
316
+ sys.setprofile = functional.sequence(recursive_disable, sys.setprofile)
317
+ threading.settrace = functional.sequence(recursive_disable, threading.settrace)
318
+
319
+ sys.settrace(sys.gettrace())
320
+ sys.setprofile(sys.getprofile())
321
+
322
+ system.checkpoint('About to install retrace system')
323
+
324
+ module_config = load_module_config('modules.toml')
325
+
326
+ patch_loaded = functional.partial(patch_module, create_patcher(system), module_config)
327
+ patch_imported = functional.partial(patch_imported_module, create_patcher(system), system.checkpoint, module_config)
328
+
329
+ system.checkpoint('Started installing system 1')
330
+
331
+ for modname in module_config.keys():
332
+ if modname in sys.modules:
333
+ patch_loaded(sys.modules[modname].__dict__, True)
334
+
335
+ system.checkpoint('About to patch threading')
336
+
337
+ patch_thread_start(system.thread_state)
338
+ threading.current_thread().__retrace__ = system
339
+
340
+ system.checkpoint('About to patch import')
341
+ patch_import(thread_state = system.thread_state,
342
+ patcher = patch_imported,
343
+ sync = system.sync,
344
+ checkpoint = system.checkpoint)
345
+
346
+ # print(f'MODULES: {list(sys.modules.keys())}')
347
+
348
+ importlib.import_module = \
349
+ system.thread_state.dispatch(system.disable_for(importlib.import_module),
350
+ internal = system.thread_state.wrap('importing', importlib.import_module))
351
+
352
+
353
+ system.checkpoint('About to patch preload libraries')
354
+
355
+ system.checkpoint('system patched...')
356
+
357
+ def run_with_retrace(system, argv, trace_shutdown = False):
358
+
359
+ def runpy_exec(source, globals = None, locals = None):
360
+ with system.thread_state.select('internal'):
361
+ return builtins.exec(source, globals, locals)
362
+
363
+ utils.update(runpy, "_run_code",
364
+ utils.wrap_func_with_overrides,
365
+ exec = runpy_exec)
366
+
367
+ try:
368
+ run_python_command(argv)
369
+ finally:
370
+ wait_for_non_daemon_threads()
371
+ try:
372
+ if trace_shutdown:
373
+ with system.thread_state.select('internal'):
374
+ atexit._run_exitfuncs()
375
+ else:
376
+ atexit._run_exitfuncs()
377
+ except Exception as e:
378
+ print(f"Error in atexit hook: {e}", file=sys.stderr)