Skip to content

Commit 180f1de

Browse files
committed
fix: commit lldbinit.py
1 parent 62cb3cd commit 180f1de

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

src/db-backend/lldbinit.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# GENERATED BY `RR`: rr lldbinit
2+
# supposedly we assume MIT License as RR
3+
# TODO: maybe autogenerate in replay
4+
5+
# This is a Python script. Save it to a file and run it from LLDB using
6+
# script exec(open('<filename>').read())
7+
# or similar.
8+
9+
def hex_unescape(string):
10+
str_len = len(string)
11+
if str_len % 2: # check for unexpected string length
12+
return ""
13+
result = bytearray()
14+
try:
15+
pos = 0
16+
while pos < str_len:
17+
hex_char = string[pos:pos+2]
18+
result.append(int(hex_char, 16))
19+
pos += 2
20+
except: # check for unexpected string value
21+
return ""
22+
return result.decode('utf-8')
23+
24+
def hex_escape(string):
25+
result = ""
26+
for curr_char in string.encode('utf-8'):
27+
if isinstance(curr_char, str):
28+
curr_char = ord(curr_char)
29+
result += format(curr_char, '02x')
30+
return result
31+
32+
33+
import lldb
34+
import re
35+
import shlex
36+
37+
def run_command_and_get_output(debugger, command):
38+
result = lldb.SBCommandReturnObject()
39+
debugger.GetCommandInterpreter().HandleCommand(command, result)
40+
assert result.Succeeded()
41+
return result.GetOutput()
42+
43+
def command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args):
44+
interpreter = debugger.GetCommandInterpreter()
45+
args = shlex.split(command)
46+
# Ensure lldb tells rr its current thread
47+
curr_thread = exe_ctx.thread
48+
cmd_prefix = ("process plugin packet send qRRCmd:%s:%d"%
49+
(cmd_name, -1 if curr_thread is None else curr_thread.GetThreadID()))
50+
arg_strs = []
51+
for auto_arg in auto_args:
52+
arg_strs.append(":" + hex_escape(run_command_and_get_output(debugger, auto_arg)))
53+
for arg in args:
54+
arg_strs.append(":" + hex_escape(arg))
55+
rv = run_command_and_get_output(debugger, cmd_prefix + ''.join(arg_strs));
56+
rv_match = re.search('response: (.*)$', rv, re.MULTILINE);
57+
if not rv_match:
58+
result.SetError(None, "Invalid response: %s" % rv)
59+
return
60+
response = hex_unescape(rv_match.group(1))
61+
result.Print(response.strip())
62+
63+
def rr_command_elapsed_time(debugger, command, exe_ctx, result, internal_dict):
64+
"""Print elapsed time (in seconds) since the start of the trace, in the 'record' timeline."""
65+
cmd_name = 'elapsed-time'
66+
auto_args = []
67+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
68+
69+
lldb.debugger.HandleCommand('command script add -f rr_command_elapsed_time elapsed-time')
70+
71+
def rr_command_when(debugger, command, exe_ctx, result, internal_dict):
72+
"""Print the number of the last completely replayed rr event."""
73+
cmd_name = 'when'
74+
auto_args = []
75+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
76+
77+
lldb.debugger.HandleCommand('command script add -f rr_command_when when')
78+
79+
def rr_command_when_ticks(debugger, command, exe_ctx, result, internal_dict):
80+
"""Print the current rr tick count for the current thread."""
81+
cmd_name = 'when-ticks'
82+
auto_args = []
83+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
84+
85+
lldb.debugger.HandleCommand('command script add -f rr_command_when_ticks when-ticks')
86+
87+
def rr_command_when_tid(debugger, command, exe_ctx, result, internal_dict):
88+
"""Print the real tid for the current thread."""
89+
cmd_name = 'when-tid'
90+
auto_args = []
91+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
92+
93+
lldb.debugger.HandleCommand('command script add -f rr_command_when_tid when-tid')
94+
95+
def rr_command_seek_ticks(debugger, command, exe_ctx, result, internal_dict):
96+
"""Print the current rr tick count for the current thread."""
97+
cmd_name = 'seek-ticks'
98+
auto_args = []
99+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
100+
101+
lldb.debugger.HandleCommand('command script add -f rr_command_seek_ticks seek-ticks')
102+
103+
def rr_command_reverse_finish(debugger, command, exe_ctx, result, internal_dict):
104+
"""Print the current rr tick count for the current thread."""
105+
cmd_name = 'reverse-finish'
106+
auto_args = []
107+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
108+
109+
lldb.debugger.HandleCommand('command script add -f rr_command_reverse_finish reverse-finish')
110+
111+
def rr_command_rr_history_push(debugger, command, exe_ctx, result, internal_dict):
112+
"""Push an entry into the rr history."""
113+
cmd_name = 'rr-history-push'
114+
auto_args = []
115+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
116+
117+
lldb.debugger.HandleCommand('command script add -f rr_command_rr_history_push rr-history-push')
118+
119+
def rr_command_back(debugger, command, exe_ctx, result, internal_dict):
120+
"""Go back one entry in the rr history."""
121+
cmd_name = 'back'
122+
auto_args = []
123+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
124+
125+
lldb.debugger.HandleCommand('command script add -f rr_command_back back')
126+
127+
def rr_command_forward(debugger, command, exe_ctx, result, internal_dict):
128+
"""Go forward one entry in the rr history."""
129+
cmd_name = 'forward'
130+
auto_args = []
131+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
132+
133+
lldb.debugger.HandleCommand('command script add -f rr_command_forward forward')
134+
135+
def rr_command_checkpoint(debugger, command, exe_ctx, result, internal_dict):
136+
"""create a checkpoint representing a point in the execution
137+
use the 'restart' command to return to the checkpoint"""
138+
cmd_name = 'checkpoint'
139+
auto_args = ['rr-where']
140+
command_impl(debugger, command, exe_ctx, result, cmd_name, auto_args)
141+
142+
lldb.debugger.HandleCommand('command script add -f rr_command_checkpoint checkpoint')
143+
144+
lldb.debugger.HandleCommand('set set prompt "(rr) "')

0 commit comments

Comments
 (0)