-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
debugger.v
320 lines (294 loc) · 8.48 KB
/
debugger.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module main
import os
import term
import time
import gx
import strings
const breakpoint_color = gx.rgb(136, 136, 97) // yellow
const debugger_name_color = gx.rgb(197, 134, 192) // pink
struct Debugger {
mut:
p os.Process
output DebuggerOutput
}
struct DebuggerOutput {
mut:
vars []DebuggerVariable
line_nr int // line at which "->" points
}
struct DebuggerVariable {
name string
typ string
mut:
value string
}
fn (mut ved Ved) run_debugger(breakpoints []int) {
if !ved.view.path.ends_with('.v') {
println('Debugger only works with V files for now')
return
}
os.system('v -w -g -o /tmp/a ${ved.view.path}')
ved.debugger = new_debugger('/tmp/a')
ved.debugger.run()
for breakpoint in breakpoints {
_ = breakpoint
// view.debugger.send_cmd('b main__foo')
}
ved.debugger.send_cmd('b main__foo')
ved.debugger.send_cmd('target stop-hook add --one-liner "frame variable"')
ved.debugger.wait_for('Breakpoint ') or { panic(err) }
ved.debugger.send_cmd('run')
for {
resp := ved.debugger.wait_for(' stop reason') or { break }
time.sleep(100 * time.millisecond)
// println('<<<<<<<<<<<<<<<<')
// println(resp)
// println('>>>>>>>>>>>>>>>>')
ved.debugger.parse_output(resp)
return
}
ved.debugger.p.close()
ved.debugger.p.wait()
dump(ved.debugger.p.code)
}
fn (mut d Debugger) send_cmd(cmd string) {
eprintln(term.bright_yellow('\n\n> sending command: ${cmd}'))
d.p.stdin_write('${cmd}\n')
}
fn (mut d Debugger) wait_for(what string) !string {
mut sb := strings.new_builder(100)
eprintln(term.bright_blue('> waiting for: ${what}'))
// now := time.now()
for d.p.is_alive() {
line := d.p.stdout_read()
// d.p.stderr_read()
sb.write_string(line)
eprint('line len: ${line.len:5} | ${line}')
if line.contains(what) {
return sb.str()
// break
}
if line.contains('exited with status') {
return error('process exited')
}
}
return ''
}
fn new_debugger(arg string) Debugger {
mut d := Debugger{
p: os.new_process(os.find_abs_path_of_executable('lldb') or { panic(err) })
}
d.p.set_args([arg])
d.p.set_work_folder(os.getwd())
d.p.set_redirect_stdio()
return d
}
fn (mut d Debugger) run() {
d.p.run()
}
fn (mut view View) add_breakpoint(line_nr int) {
view.breakpoints << line_nr
}
fn (mut d Debugger) parse_output(s string) {
// d.output = d.parse_vars(s, false)
d.add_output(d.parse_vars(s, false))
}
// merges old and new output, so that old vars are not lost and var positions are not changed (otherwise
// UI becomes jumpy)
fn (mut d Debugger) add_output(new_output DebuggerOutput) {
loop1: for new_var in new_output.vars {
for i, var in d.output.vars {
// Update value
if var.name == new_var.name {
d.output.vars[i].value = new_var.value
continue loop1
}
}
// Add a new var to the end
d.output.vars << new_var
}
d.output.line_nr = new_output.line_nr
}
fn (mut d Debugger) parse_vars(s string, is_struct bool) DebuggerOutput {
mut res := DebuggerOutput{}
lines := s.split('\n')
for line in lines {
// Get variables: "(int) a = 3"
if line.contains(' = ') && (is_struct || line.contains(') ')) { // structs don't contain (string)
var := d.parse_var(line, s, is_struct)
if var.name != '' {
res.vars << var
}
}
// Get yellow line number
else if line.starts_with('-> ') {
// "-> 2 str := 'hello'"
vals := line.fields()
res.line_nr = vals[1].int()
}
}
// println('parse_vars res=')
// println(res)
return res
}
fn (mut d Debugger) parse_var(line string, s string, is_struct bool) DebuggerVariable {
println('\n\nparse_var line=')
println('"${line}"')
par_pos := if is_struct { 0 } else { line.index(') ') or { 0 } }
typ := if is_struct { '' } else { line[1..par_pos] }
eq_pos := line.index(' = ') or { 0 }
name := line[par_pos + 2..eq_pos]
// Skip the var if it's no valid yet (not present in the code before the current line)
backtrace := s.before('->').after('stop reason = ')
println('BACKTRACE:')
println(backtrace)
println('____________________________')
if !backtrace.contains(name) {
println("SKIPPING ${name} for line '${line}'")
return DebuggerVariable{}
}
mut value := line[eq_pos + 3..]
// Get correct string value
if typ == 'string' || (is_struct && value.contains('(str =')) {
if value.contains('str = 0x0000000000000000') {
value = "''"
} else if value.contains('(str = "') {
start := value.index('(str =') or { 0 }
end := value.index(',') or { 0 }
value = value[start + 6..end]
}
// Get array contents by callign Array_xxx_str() in lldb
} else if typ.starts_with('Array_') {
elem_type := typ.replace('Array_', '')
d.send_cmd('p Array_${elem_type}_str(${name})')
resp := d.wait_for('(string)') or { return DebuggerVariable{} }
value = resp.after('(string)').after('= "').before('",')
} else if typ == 'bool' {
// Bool
// (bool) bool1 = '\x01' len=6 TRUE
//(bool) bool2 = '\0' line=4 FALSE
// println('BOOL=${value} len=${value.len}')
// println(int(value[1]))
if value.len == 6 {
value = 'true'
} else {
value = 'false'
}
} else if typ.starts_with('_option_') {
// Option
d.send_cmd('p ${typ}_str(${name})')
resp := d.wait_for('"') or { return DebuggerVariable{} }
value = resp.after('(str = "').before('", ')
} else if value == '{' {
// Struct
struct_code := s.after(line).before('\n}\n')
println('STRUCT 1st line:')
println(line)
println('STRUCT code:')
println(struct_code)
println('============')
// Sum types
if struct_code.contains('_string =') {
// value = 'sum t:${typ}'
// d.send_cmd('p v_typeof_sumtype_${typ}(${name}._typ)')
d.send_cmd('p ${typ}_str(${name})')
resp := d.wait_for('"') or { return DebuggerVariable{} }
value = resp.after('(str = "').before('", ')
} else if struct_code.contains('_object') {
// Interface
d.send_cmd('p ${typ}_str(${name})')
resp := d.wait_for('"') or { return DebuggerVariable{} }
value = resp.after('(str = "').before('", ')
} else {
// Normal struct
parsed_struct := d.parse_vars(struct_code, true)
println('PARSED STRUCT: ${parsed_struct}')
value = parsed_struct.format_struct()
}
}
return DebuggerVariable{
name: name
typ: typ.replace('main__', '').replace('Array_', '[]').replace('_option_', '?').replace('__',
'.')
value: value.trim_space()
}
}
// "next" in lldb
fn (mut debugger Debugger) step_over() {
debugger.send_cmd('next')
for {
resp := debugger.wait_for(' stop reason') or { break }
time.sleep(100 * time.millisecond)
// println('2<<<<<<<<<<<<<<<<')
// println(resp)
// println('2>>>>>>>>>>>>>>>>')
debugger.parse_output(resp)
// d.send_cmd('next')
return
}
}
fn (mut ved Ved) draw_debugger_variables() {
split_from, split_to := ved.get_splits_from_to()
split_width := ved.split_width()
// We draw debugger variables in the last split (the one with the output)
last_split_x := split_width * (split_to - 1 - split_from)
// println('split_width=${split_width}, last_split_x=${last_split_x}')
// println('DRAW D VARS x=${last_split_x} splitw=${split_width}, to=${split_to}, from=${split_from}')
ved.gg.draw_rect_filled(last_split_x, ved.cfg.line_height, split_width, 500, ved.cfg.title_color)
if ved.debugger.output.vars.len == 0 {
return
}
x := last_split_x + 3
// Calc first col width
max_name_len := 20
max_value_len := 45
/*
mut max_len := ved.debugger.output.vars[0].name.len
for var in ved.debugger.output.vars {
if var.name.len > max_len {
max_len = var.name.len
}
}
*/
col_width := (max_name_len + 1) * ved.cfg.char_width
// Draw the table
for i, var in ved.debugger.output.vars {
y := (i + 1) * ved.cfg.line_height + 3
// col_width := 80
ved.gg.draw_text(x, y, var.name.limit(max_name_len),
color: debugger_name_color
size: ved.cfg.txt_cfg.size
)
ved.gg.draw_text(x + col_width, y, var.value_fmt(max_value_len),
color: gx.white
size: ved.cfg.txt_cfg.size
)
ved.gg.draw_text(ved.win_width - col_width, y, var.typ,
color: gx.white
size: ved.cfg.txt_cfg.size
)
}
}
fn (d DebuggerOutput) format_struct() string {
mut sb := strings.new_builder(100)
sb.write_string('{ ')
for i, var in d.vars {
sb.write_string(var.name)
sb.write_string(': ')
sb.write_string(var.value)
if i < d.vars.len - 1 {
sb.write_string(', ')
}
}
sb.write_string(' }')
return sb.str()
}
fn (d DebuggerVariable) value_fmt(max_len int) string {
if d.value.len > max_len {
return d.value.limit(max_len) + '...'
}
return d.value
}