Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c

raw
1 /*
2 ** apibreak.c
3 **
4 */
5
6 #include <string.h>
7 #include <mruby.h>
8 #include <mruby/irep.h>
9 #include "mrdb.h"
10 #include <mruby/debug.h>
11 #include <mruby/opcode.h>
12 #include <mruby/class.h>
13 #include <mruby/proc.h>
14 #include <mruby/variable.h>
15 #include <mruby/internal.h>
16 #include "mrdberror.h"
17 #include "apibreak.h"
18 #include "apistring.h"
19
20 #define MAX_BREAKPOINTNO (MAX_BREAKPOINT * 1024)
21 #define MRB_DEBUG_BP_FILE_OK (0x0001)
22 #define MRB_DEBUG_BP_LINENO_OK (0x0002)
23
24 static uint16_t
25 check_lineno(mrb_irep_debug_info_file *info_file, uint16_t lineno)
26 {
27 uint32_t count = info_file->line_entry_count;
28 uint16_t l_idx;
29
30 switch (info_file->line_type) {
31 case mrb_debug_line_ary:
32 for (l_idx = 0; l_idx < count; ++l_idx) {
33 if (lineno == info_file->lines.ary[l_idx]) {
34 return lineno;
35 }
36 }
37 break;
38
39 case mrb_debug_line_flat_map:
40 for (l_idx = 0; l_idx < count; ++l_idx) {
41 if (lineno == info_file->lines.flat_map[l_idx].line) {
42 return lineno;
43 }
44 }
45 break;
46
47 case mrb_debug_line_packed_map:
48 {
49 const uint8_t *p = info_file->lines.packed_map;
50 const uint8_t *pend = p + count;
51 uint32_t line = 0;
52 while (p < pend) {
53 mrb_packed_int_decode(p, &p);
54 line += mrb_packed_int_decode(p, &p);
55 if (line == lineno) return lineno;
56 }
57 }
58 break;
59 }
60
61 return 0;
62 }
63
64 static int32_t
65 get_break_index(mrb_debug_context *dbg, uint32_t bpno)
66 {
67 uint32_t i;
68 int32_t index;
69 char hit = FALSE;
70
71 for (i = 0; i < dbg->bpnum; i++) {
72 if (dbg->bp[i].bpno == bpno) {
73 hit = TRUE;
74 index = i;
75 break;
76 }
77 }
78
79 if (hit == FALSE) {
80 return MRB_DEBUG_BREAK_INVALID_NO;
81 }
82
83 return index;
84 }
85
86 static void
87 free_breakpoint(mrb_state *mrb, mrb_debug_breakpoint *bp)
88 {
89 switch(bp->type) {
90 case MRB_DEBUG_BPTYPE_LINE:
91 mrb_free(mrb, (void*)bp->point.linepoint.file);
92 break;
93 case MRB_DEBUG_BPTYPE_METHOD:
94 mrb_free(mrb, (void*)bp->point.methodpoint.method_name);
95 if (bp->point.methodpoint.class_name != NULL) {
96 mrb_free(mrb, (void*)bp->point.methodpoint.class_name);
97 }
98 break;
99 default:
100 break;
101 }
102 }
103
104 static uint16_t
105 check_file_lineno(mrb_state *mrb, const struct mrb_irep *irep, const char *file, uint16_t lineno)
106 {
107 mrb_irep_debug_info_file *info_file;
108 uint16_t result = 0;
109 uint16_t f_idx;
110 uint16_t fix_lineno;
111 uint16_t i;
112
113 for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
114 const char *filename;
115 info_file = irep->debug_info->files[f_idx];
116 filename = mrb_sym_name_len(mrb, info_file->filename_sym, NULL);
117 if (!strcmp(filename, file)) {
118 result = MRB_DEBUG_BP_FILE_OK;
119
120 fix_lineno = check_lineno(info_file, lineno);
121 if (fix_lineno != 0) {
122 return result | MRB_DEBUG_BP_LINENO_OK;
123 }
124 }
125 for (i=0; i < irep->rlen; ++i) {
126 result |= check_file_lineno(mrb, irep->reps[i], file, lineno);
127 if (result == (MRB_DEBUG_BP_FILE_OK | MRB_DEBUG_BP_LINENO_OK)) {
128 return result;
129 }
130 }
131 }
132 return result;
133 }
134
135 static int32_t
136 compare_break_method(mrb_state *mrb, mrb_debug_breakpoint *bp, struct RClass *class_obj, mrb_sym method_sym, mrb_bool* isCfunc)
137 {
138 const char* class_name;
139 const char* method_name;
140 mrb_method_t m;
141 struct RClass* sc;
142 const char* sn;
143 mrb_sym ssym;
144 mrb_debug_methodpoint *method_p;
145 mrb_bool is_defined;
146
147 method_name = mrb_sym_name(mrb, method_sym);
148
149 method_p = &bp->point.methodpoint;
150 if (strcmp(method_p->method_name, method_name) == 0) {
151 class_name = mrb_class_name(mrb, class_obj);
152 if (class_name == NULL) {
153 if (method_p->class_name == NULL) {
154 return bp->bpno;
155 }
156 }
157 else if (method_p->class_name != NULL) {
158 m = mrb_method_search_vm(mrb, &class_obj, method_sym);
159 if (MRB_METHOD_UNDEF_P(m)) {
160 return MRB_DEBUG_OK;
161 }
162 if (MRB_METHOD_CFUNC_P(m)) {
163 *isCfunc = TRUE;
164 }
165
166 is_defined = mrb_class_defined(mrb, method_p->class_name);
167 if (is_defined == FALSE) {
168 return MRB_DEBUG_OK;
169 }
170
171 sc = mrb_class_get(mrb, method_p->class_name);
172 ssym = mrb_intern_check_cstr(mrb, method_p->method_name);
173 m = mrb_method_search_vm(mrb, &sc, ssym);
174 if (MRB_METHOD_UNDEF_P(m)) {
175 return MRB_DEBUG_OK;
176 }
177
178 class_name = mrb_class_name(mrb, class_obj);
179 sn = mrb_class_name(mrb, sc);
180 if (strcmp(sn, class_name) == 0) {
181 return bp->bpno;
182 }
183 }
184 }
185 return MRB_DEBUG_OK;
186 }
187
188 int32_t
189 mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *file, uint16_t lineno)
190 {
191 int32_t index;
192 char* set_file;
193 uint16_t result;
194
195 if ((mrb == NULL)||(dbg == NULL)||(file == NULL)) {
196 return MRB_DEBUG_INVALID_ARGUMENT;
197 }
198
199 if (dbg->bpnum >= MAX_BREAKPOINT) {
200 return MRB_DEBUG_BREAK_NUM_OVER;
201 }
202
203 if (dbg->next_bpno > MAX_BREAKPOINTNO) {
204 return MRB_DEBUG_BREAK_NO_OVER;
205 }
206
207 /* file and lineno check. */
208 result = check_file_lineno(mrb, dbg->root_irep, file, lineno);
209 if (result == 0) {
210 return MRB_DEBUG_BREAK_INVALID_FILE;
211 }
212 else if (result == MRB_DEBUG_BP_FILE_OK) {
213 return MRB_DEBUG_BREAK_INVALID_LINENO;
214 }
215
216 set_file = mrdb_strdup(mrb, file);
217
218 index = dbg->bpnum;
219 dbg->bp[index].bpno = dbg->next_bpno;
220 dbg->next_bpno++;
221 dbg->bp[index].enable = TRUE;
222 dbg->bp[index].type = MRB_DEBUG_BPTYPE_LINE;
223 dbg->bp[index].point.linepoint.lineno = lineno;
224 dbg->bpnum++;
225
226 dbg->bp[index].point.linepoint.file = set_file;
227
228 return dbg->bp[index].bpno;
229 }
230
231 int32_t
232 mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *class_name, const char *method_name)
233 {
234 int32_t index;
235 char* set_class;
236 char* set_method;
237
238 if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) {
239 return MRB_DEBUG_INVALID_ARGUMENT;
240 }
241
242 if (dbg->bpnum >= MAX_BREAKPOINT) {
243 return MRB_DEBUG_BREAK_NUM_OVER;
244 }
245
246 if (dbg->next_bpno > MAX_BREAKPOINTNO) {
247 return MRB_DEBUG_BREAK_NO_OVER;
248 }
249
250 if (class_name != NULL) {
251 set_class = mrdb_strdup(mrb, class_name);
252 }
253 else {
254 set_class = NULL;
255 }
256
257 set_method = mrdb_strdup(mrb, method_name);
258 if (set_method == NULL) {
259 mrb_free(mrb, set_class);
260 }
261
262 index = dbg->bpnum;
263 dbg->bp[index].bpno = dbg->next_bpno;
264 dbg->next_bpno++;
265 dbg->bp[index].enable = TRUE;
266 dbg->bp[index].type = MRB_DEBUG_BPTYPE_METHOD;
267 dbg->bp[index].point.methodpoint.method_name = set_method;
268 dbg->bp[index].point.methodpoint.class_name = set_class;
269 dbg->bpnum++;
270
271 return dbg->bp[index].bpno;
272 }
273
274 int32_t
275 mrb_debug_get_breaknum(mrb_state *mrb, mrb_debug_context *dbg)
276 {
277 if ((mrb == NULL) || (dbg == NULL)) {
278 return MRB_DEBUG_INVALID_ARGUMENT;
279 }
280
281 return dbg->bpnum;
282 }
283
284 int32_t
285 mrb_debug_get_break_all(mrb_state *mrb, mrb_debug_context *dbg, uint32_t size, mrb_debug_breakpoint *bp)
286 {
287 uint32_t get_size = 0;
288
289 if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
290 return MRB_DEBUG_INVALID_ARGUMENT;
291 }
292
293 if (dbg->bpnum >= size) {
294 get_size = size;
295 }
296 else {
297 get_size = dbg->bpnum;
298 }
299
300 memcpy(bp, dbg->bp, sizeof(mrb_debug_breakpoint) * get_size);
301
302 return get_size;
303 }
304
305 int32_t
306 mrb_debug_get_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno, mrb_debug_breakpoint *bp)
307 {
308 int32_t index;
309
310 if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
311 return MRB_DEBUG_INVALID_ARGUMENT;
312 }
313
314 index = get_break_index(dbg, bpno);
315 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
316 return MRB_DEBUG_BREAK_INVALID_NO;
317 }
318
319 bp->bpno = dbg->bp[index].bpno;
320 bp->enable = dbg->bp[index].enable;
321 bp->point = dbg->bp[index].point;
322 bp->type = dbg->bp[index].type;
323
324 return 0;
325 }
326
327 int32_t
328 mrb_debug_delete_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
329 {
330 uint32_t i;
331 int32_t index;
332
333 if ((mrb == NULL) ||(dbg == NULL)) {
334 return MRB_DEBUG_INVALID_ARGUMENT;
335 }
336
337 index = get_break_index(dbg, bpno);
338 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
339 return MRB_DEBUG_BREAK_INVALID_NO;
340 }
341
342 free_breakpoint(mrb, &dbg->bp[index]);
343
344 for (i = index; i < dbg->bpnum; i++) {
345 if ((i + 1) == dbg->bpnum) {
346 dbg->bp[i] = (mrb_debug_breakpoint){0};
347 }
348 else {
349 dbg->bp[i] = dbg->bp[i + 1];
350 }
351 }
352
353 dbg->bpnum--;
354
355 return MRB_DEBUG_OK;
356 }
357
358 int32_t
359 mrb_debug_delete_break_all(mrb_state *mrb, mrb_debug_context *dbg)
360 {
361 uint32_t i;
362
363 if ((mrb == NULL) || (dbg == NULL)) {
364 return MRB_DEBUG_INVALID_ARGUMENT;
365 }
366
367 for (i = 0; i < dbg->bpnum; i++) {
368 free_breakpoint(mrb, &dbg->bp[i]);
369 }
370
371 dbg->bpnum = 0;
372
373 return MRB_DEBUG_OK;
374 }
375
376 int32_t
377 mrb_debug_enable_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
378 {
379 int32_t index = 0;
380
381 if ((mrb == NULL) || (dbg == NULL)) {
382 return MRB_DEBUG_INVALID_ARGUMENT;
383 }
384
385 index = get_break_index(dbg, bpno);
386 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
387 return MRB_DEBUG_BREAK_INVALID_NO;
388 }
389
390 dbg->bp[index].enable = TRUE;
391
392 return MRB_DEBUG_OK;
393 }
394
395 int32_t
396 mrb_debug_enable_break_all(mrb_state *mrb, mrb_debug_context *dbg)
397 {
398 uint32_t i;
399
400 if ((mrb == NULL) || (dbg == NULL)) {
401 return MRB_DEBUG_INVALID_ARGUMENT;
402 }
403
404 for (i = 0; i < dbg->bpnum; i++) {
405 dbg->bp[i].enable = TRUE;
406 }
407
408 return MRB_DEBUG_OK;
409 }
410
411 int32_t
412 mrb_debug_disable_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
413 {
414 int32_t index = 0;
415
416 if ((mrb == NULL) || (dbg == NULL)) {
417 return MRB_DEBUG_INVALID_ARGUMENT;
418 }
419
420 index = get_break_index(dbg, bpno);
421 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
422 return MRB_DEBUG_BREAK_INVALID_NO;
423 }
424
425 dbg->bp[index].enable = FALSE;
426
427 return MRB_DEBUG_OK;
428 }
429
430 int32_t
431 mrb_debug_disable_break_all(mrb_state *mrb, mrb_debug_context *dbg)
432 {
433 uint32_t i;
434
435 if ((mrb == NULL) || (dbg == NULL)) {
436 return MRB_DEBUG_INVALID_ARGUMENT;
437 }
438
439 for (i = 0; i < dbg->bpnum; i++) {
440 dbg->bp[i].enable = FALSE;
441 }
442
443 return MRB_DEBUG_OK;
444 }
445
446 static mrb_bool
447 check_start_pc_for_line(mrb_state *mrb, const mrb_irep *irep, const mrb_code *pc, uint16_t line)
448 {
449 if (pc > irep->iseq) {
450 if (line == mrb_debug_get_line(mrb, irep, pc - irep->iseq - 1)) {
451 return FALSE;
452 }
453 }
454 return TRUE;
455 }
456
457 int32_t
458 mrb_debug_check_breakpoint_line(mrb_state *mrb, mrb_debug_context *dbg, const char *file, uint16_t line)
459 {
460 mrb_debug_breakpoint *bp;
461 mrb_debug_linepoint *line_p;
462 uint32_t i;
463
464 if ((mrb == NULL) || (dbg == NULL) || (file == NULL) || (line <= 0)) {
465 return MRB_DEBUG_INVALID_ARGUMENT;
466 }
467
468 if (!check_start_pc_for_line(mrb, dbg->irep, dbg->pc, line)) {
469 return MRB_DEBUG_OK;
470 }
471
472 bp = dbg->bp;
473 for (i=0; i<dbg->bpnum; i++) {
474 switch (bp->type) {
475 case MRB_DEBUG_BPTYPE_LINE:
476 if (bp->enable == TRUE) {
477 line_p = &bp->point.linepoint;
478 if ((strcmp(line_p->file, file) == 0) && (line_p->lineno == line)) {
479 return bp->bpno;
480 }
481 }
482 break;
483 case MRB_DEBUG_BPTYPE_METHOD:
484 break;
485 case MRB_DEBUG_BPTYPE_NONE:
486 default:
487 return MRB_DEBUG_OK;
488 }
489 bp++;
490 }
491 return MRB_DEBUG_OK;
492 }
493
494
495 int32_t
496 mrb_debug_check_breakpoint_method(mrb_state *mrb, mrb_debug_context *dbg, struct RClass *class_obj, mrb_sym method_sym, mrb_bool* isCfunc)
497 {
498 mrb_debug_breakpoint *bp;
499 int32_t bpno;
500 uint32_t i;
501
502 if ((mrb == NULL) || (dbg == NULL) || (class_obj == NULL)) {
503 return MRB_DEBUG_INVALID_ARGUMENT;
504 }
505
506 bp = dbg->bp;
507 for (i=0; i<dbg->bpnum; i++) {
508 if (bp->type == MRB_DEBUG_BPTYPE_METHOD) {
509 if (bp->enable == TRUE) {
510 bpno = compare_break_method(mrb, bp, class_obj, method_sym, isCfunc);
511 if (bpno > 0) {
512 return bpno;
513 }
514 }
515 }
516 else if (bp->type == MRB_DEBUG_BPTYPE_NONE) {
517 break;
518 }
519 bp++;
520 }
521
522 return 0;
523 }