arabelatso/replay-oriented-instrumentation
Instruments programs to record execution information for deterministic replay debugging. Use when debugging hard-to-reproduce bugs (race conditions, timing issues, intermittent failures, heisenbugs), reproducing production failures, or analyzing complex execution sequences. Records non-deterministic events (I/O, threading, randomness, time) to enable exact replay of program executions. Supports Python, JavaScript, Java, and C/C++ with both custom instrumentation and existing replay tools.
npx skills add https://github.com/ArabelaTso/Skills-4-SE --skill replay-oriented-instrumentation
Instrument programs to capture execution information that enables deterministic replay, making it possible to reproduce and debug failures that are difficult to reproduce normally.
Deterministic replay works by:
Analyze the program to find sources of non-determinism. See references/non-determinism.md for comprehensive coverage.
Common sources:
Select appropriate recording level based on needs:
Function-level (recommended starting point):
Event-based (balanced approach):
Instruction-level (comprehensive):
Choose between custom instrumentation or existing tools:
Custom instrumentation (flexible):
Existing tools (easier):
Run the program in recording mode:
Reproduce the execution from the log:
Leverage replay for debugging:
For custom instrumentation, see references/python-replay.md.
Basic example:
import json
import time
import random
class ReplayRecorder:
def __init__(self, mode='record'):
self.mode = mode
self.log = []
self.index = 0
def record_call(self, func_name, result):
if self.mode == 'record':
self.log.append({'func': func_name, 'result': result})
else:
entry = self.log[self.index]
self.index += 1
return entry['result']
recorder = ReplayRecorder(mode='record')
def get_time():
if recorder.mode == 'record':
result = time.time()
recorder.record_call('time', result)
return result
else:
return recorder.record_call('time', None)
# Record mode
result = get_time()
with open('replay.log', 'w') as f:
json.dump(recorder.log, f)
# Replay mode
recorder = ReplayRecorder(mode='replay')
with open('replay.log', 'r') as f:
recorder.log = json.load(f)
result = get_time() # Returns same value
Using RR (system-level):
rr record python script.py
rr replay
Recording HTTP requests with Nock:
const nock = require('nock');
// Record mode
nock.recorder.rec();
// ... make requests ...
const fixtures = nock.recorder.play();
// Replay mode
nock('http://api.example.com')
.get('/data')
.reply(200, { data: 'recorded response' });
Using AspectJ for recording:
@Aspect
public class ReplayAspect {
private List<Event> events = new ArrayList<>();
@Around("execution(* java.io..*(..))")
public Object recordIO(ProceedingJoinPoint pjp) throws Throwable {
Object result = pjp.proceed();
events.add(new Event(pjp.getSignature(), pjp.getArgs(), result));
return result;
}
}
Using RR (recommended):
# Record
rr record ./program arg1 arg2
# Replay with GDB
rr replay -d gdb
# In GDB, use reverse execution
(gdb) reverse-continue
(gdb) reverse-step
Custom instrumentation with macros:
#define RECORD_CALL(func, ...) \
({ \
auto result = func(__VA_ARGS__); \
log_event(#func, result); \
result; \
})
// Usage
int fd = RECORD_CALL(open, "file.txt", O_RDONLY);
Problem: Test fails intermittently due to race condition
Solution:
Implementation:
import threading
class ThreadRecorder:
def __init__(self):
self.events = []
def record_lock(self, lock_id, acquired):
self.events.append({
'type': 'lock',
'lock_id': lock_id,
'acquired': acquired,
'thread': threading.current_thread().ident
})
recorder = ThreadRecorder()
class RecordingLock:
def __init__(self, lock_id):
self.lock = threading.Lock()
self.lock_id = lock_id
def acquire(self):
result = self.lock.acquire()
recorder.record_lock(self.lock_id, True)
return result
def release(self):
recorder.record_lock(self.lock_id, False)
self.lock.release()
Problem: API call fails in production, can't reproduce locally
Solution:
Implementation (JavaScript):
const nock = require('nock');
const fs = require('fs');
// Record mode (run in production)
nock.recorder.rec({ output_objects: true });
// ... application runs ...
const recordings = nock.recorder.play();
fs.writeFileSync('recordings.json', JSON.stringify(recordings));
// Replay mode (run locally)
const recordings = JSON.parse(fs.readFileSync('recordings.json'));
nock.define(recordings);
// ... application runs with recorded responses ...
Problem: Bug only occurs at specific times or after certain duration
Solution:
Implementation:
import time
class TimeRecorder:
def __init__(self, mode='record'):
self.mode = mode
self.times = []
self.index = 0
def time(self):
if self.mode == 'record':
t = time.time()
self.times.append(t)
return t
else:
t = self.times[self.index]
self.index += 1
return t
recorder = TimeRecorder(mode='record')
time.time = recorder.time
Always verify replay matches recording:
def verify_replay(original_output, replay_output):
if original_output != replay_output:
print("REPLAY MISMATCH!")
print(f"Original: {original_output}")
print(f"Replay: {replay_output}")
return False
return True
Take arabelatso/replay-oriented-instrumentation from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.