-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm_debug.c
306 lines (251 loc) · 7.44 KB
/
vm_debug.c
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
#include "shared/shared.h"
#include "vm.h"
#include "vm_game.h"
#include "vm_debug.h"
#include "vm_string.h"
static void QC_stacktrace(qcvm_t *vm)
{
qcvm_return_string(vm, qcvm_stack_trace(vm, qcvm_argv_int32(vm, 0)));
}
static void QC_debugbreak(qcvm_t *vm)
{
#if ALLOW_DEBUGGING
qcvm_break_on_current_statement(vm);
#endif
}
static void QC_dumpentity(qcvm_t *vm)
{
FILE *fp = fopen(qcvm_temp_format(vm, "%sdumpentity.text", vm->path), "a");
edict_t *ent = qcvm_argv_entity(vm, 0);
for (qcvm_definition_t *f = vm->fields; f < vm->fields + vm->fields_size; f++)
{
fprintf(fp, "%s: ", qcvm_get_string(vm, f->name_index));
ptrdiff_t val;
if (!qcvm_resolve_pointer(vm, qcvm_get_entity_field_pointer(vm, ent, (int32_t)f->global_index), true, qcvm_type_size(f->id), (void **)&val))
{
fclose(fp);
qcvm_error(vm, "invalid pointer");
}
switch (f->id)
{
case TYPE_FLOAT:
fprintf(fp, "%f", *(vec_t *)val);
break;
case TYPE_VECTOR: {
vec_t *v = (vec_t *)val;
fprintf(fp, "%f %f %f", v[0], v[1], v[2]);
break; }
default:
fprintf(fp, "%i", *(int32_t *)val);
break;
}
fprintf(fp, "\r\n");
}
fclose(fp);
}
void qcvm_init_debug_builtins(qcvm_t *vm)
{
qcvm_register_builtin(stacktrace);
qcvm_register_builtin(debugbreak);
qcvm_register_builtin(dumpentity);
}
#if ALLOW_DEBUGGING
static size_t debuggerwnd;
static char *debugger_command;
static qcvm_mutex_t input_mutex;
static bool awaiting_steal = false;
static qcvm_thread_t input_thread;
static bool running_thread = false;
static qcvm_t *thread_vm;
static void qcvm_debugger_thread(void)
{
static char debug_string[MAX_INFO_STRING * 4];
static size_t debug_pos = 0;
debugger_command = debug_string;
while (true)
{
while (true)
{
char c = getc(stdin);
if (c == 0)
return;
if (debug_pos >= sizeof(debug_string))
{
debug_pos = 0;
debug_string[0] = 0;
thread_vm->debug_print("DEBUGGER STRING OVERFLOW :(");
}
debug_string[debug_pos] = c;
if (debug_string[debug_pos] == '\n')
{
debug_string[debug_pos] = 0;
break;
}
debug_pos++;
}
thread_vm->debug.lock_mutex(input_mutex);
debug_pos = 0;
awaiting_steal = true;
thread_vm->debug.unlock_mutex(input_mutex);
while (true)
{
thread_vm->debug.thread_sleep(10);
thread_vm->debug.lock_mutex(input_mutex);
bool can_break = !awaiting_steal;
thread_vm->debug.unlock_mutex(input_mutex);
if (can_break)
break;
}
}
}
void qcvm_wait_for_debugger_commands(qcvm_t *vm);
void qcvm_init_debugger(qcvm_t *vm)
{
thread_vm = vm;
input_mutex = thread_vm->debug.create_mutex();
thread_vm->debug.thread_sleep(5000);
input_thread = thread_vm->debug.create_thread(qcvm_debugger_thread);
running_thread = true;
vm->debug.state = DEBUG_BROKE;
qcvm_wait_for_debugger_commands(vm);
}
void qcvm_send_debugger_command(const qcvm_t *vm, const char *cmd)
{
printf("%s\n", cmd);
fflush(stdout);
}
static const char *strtok_emulate(qcvm_t *vm, qcvm_function_t *func, const char *string, int *start)
{
qcvm_set_global_str(vm, GLOBAL_PARM0, string, strlen(string), true);
qcvm_set_global_typed_ptr(int32_t, vm, GLOBAL_PARM1, start);
qcvm_execute(vm, func);
*start = *qcvm_get_global_typed(int32_t, vm, GLOBAL_PARM1);
return qcvm_get_string(vm, *qcvm_get_global_typed(qcvm_string_t, vm, GLOBAL_RETURN));
}
void qcvm_check_debugger_commands(qcvm_t *vm)
{
if (!thread_vm)
return;
thread_vm->debug.lock_mutex(input_mutex);
if (!awaiting_steal)
{
thread_vm->debug.unlock_mutex(input_mutex);
return;
}
while (*debugger_command && debugger_command[strlen(debugger_command) - 1] == '\n')
debugger_command[strlen(debugger_command) - 1] = 0;
qcvm_function_t *qc_strtok = qcvm_find_function(vm, "strtok");
if (!qc_strtok)
qcvm_error(vm, "Can't find strtok :(");
else if (strncmp(debugger_command, "debuggerwnd ", 12) == 0)
{
vm->debug.attached = true;
debuggerwnd = strtoul(debugger_command + 12, NULL, 0);
qcvm_send_debugger_command(vm, qcvm_temp_format(vm, "qcreloaded \"%s\" \"%s\"\n", vm->engine_name, vm->path));
}
else if (strncmp(debugger_command, "qcbreakpoint ", 13) == 0)
{
int start = 13;
int mode = strtol(strtok_emulate(vm, qc_strtok, debugger_command, &start), NULL, 10);
const char *file = strtok_emulate(vm, qc_strtok, debugger_command, &start);
int line = strtol(strtok_emulate(vm, qc_strtok, debugger_command, &start), NULL, 10);
qcvm_set_breakpoint(vm, mode, file, line);
}
else if (strcmp(debugger_command, "qcresume") == 0)
{
vm->debug.state = DEBUG_NONE;
}
else if (strncmp(debugger_command, "qcstep ", 7) == 0)
{
int start = 7;
const char *mode = strtok_emulate(vm, qc_strtok, debugger_command, &start);
if (!strcmp(mode, "into"))
vm->debug.state = DEBUG_STEP_INTO;
else if (!strcmp(mode, "out"))
vm->debug.state = DEBUG_STEP_OUT;
else
vm->debug.state = DEBUG_STEP_OVER;
}
else if (strncmp(debugger_command, "qcinspect ", 10) == 0)
{
int start = 10;
const char *variable = strtok_emulate(vm, qc_strtok, debugger_command, &start);
qcvm_evaluated_t result = qcvm_evaluate(vm, variable);
const char *value;
char *slashed = NULL;
switch (result.variant.type)
{
case TYPE_INTEGER:
value = qcvm_temp_format(vm, "[%i]_%i", result.global, result.variant.value.itg);
break;
case TYPE_FLOAT:
value = qcvm_temp_format(vm, "[%i]_%g", result.global, result.variant.value.flt);
break;
case TYPE_VECTOR:
value = qcvm_temp_format(vm, "[%i]_%f_%f_%f", result.global, result.variant.value.vec.x, result.variant.value.vec.y, result.variant.value.vec.z);
break;
case TYPE_STRING:
value = qcvm_get_string(vm, result.variant.value.str);
if (strchr(value, '"') || strchr(value, '\\'))
{
size_t newlen = strlen(value);
for (const char *c = value; *c; c++)
if (*c == '"' || *c == '\\')
newlen++;
slashed = (char *)qcvm_alloc(vm, newlen + 2 + 1);
*slashed = '"';
strcpy(slashed + 1, value);
const char *end = slashed + newlen + 1;
char *c;
for (c = slashed + 1; *c; c++)
{
if (*c != '"' && *c != '\\')
continue;
memmove(c + 1, c, end - c);
*c = '\\';
c++;
}
*c++ = '"';
*c = 0;
value = slashed;
}
value = qcvm_temp_format(vm, "[%i]_%s", result.global, value);
break;
case TYPE_ENTITY:
if (result.variant.value.ent == ENT_INVALID)
value = qcvm_temp_format(vm, "[%i]_invalid/null_entity", result.global);
else
value = qcvm_temp_format(vm, "[%i]_entity_%i", result.global, qcvm_ent_to_entity(vm, result.variant.value.ent, false)->s.number);
break;
case TYPE_FUNCTION:
if (result.variant.value.fnc == FUNC_VOID)
value = qcvm_temp_format(vm, "[%i]_invalid/null_function", result.global);
else
{
qcvm_function_t *func = qcvm_get_function(vm, result.variant.value.fnc);
if (!func || func->name_index == STRING_EMPTY)
value = qcvm_temp_format(vm, "[%i]_can't_resolve_function_%i", result.global, result.variant.value.fnc);
else
value = qcvm_temp_format(vm, "[%i]_%s", result.global, qcvm_get_string(vm, func->name_index));
}
break;
default:
value = "unable_to_evaluate";
break;
}
qcvm_send_debugger_command(vm, qcvm_temp_format(vm, "qcvalue \"%s\" %s\n", variable, value));
if (slashed)
qcvm_mem_free(vm, slashed);
}
awaiting_steal = false;
thread_vm->debug.unlock_mutex(input_mutex);
}
void qcvm_wait_for_debugger_commands(qcvm_t *vm)
{
while (vm->debug.state == DEBUG_BROKE)
{
qcvm_check_debugger_commands(vm);
vm->debug.thread_sleep(1);
}
}
#endif