source: EcnlProtoTool/trunk/prototool/src/apibreak.c@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

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