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
RevLine 
[279]1/*
2** apibreak.c
3**
4*/
5
6#include <string.h>
[439]7#include <mruby.h>
8#include <mruby/irep.h>
[279]9#include "mrdb.h"
[439]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>
[279]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{
[439]25 uint32_t count = info_file->line_entry_count;
26 uint16_t l_idx;
[279]27
[439]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 }
[279]42
[439]43 return 0;
[279]44}
45
46static int32_t
47get_break_index(mrb_debug_context *dbg, uint32_t bpno)
48{
[439]49 uint32_t i;
50 int32_t index;
51 char hit = FALSE;
[279]52
[439]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 }
[279]60
[439]61 if (hit == FALSE) {
62 return MRB_DEBUG_BREAK_INVALID_NO;
63 }
[279]64
[439]65 return index;
[279]66}
67
68static void
69free_breakpoint(mrb_state *mrb, mrb_debug_breakpoint *bp)
70{
[439]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 }
[279]84}
85
86static uint16_t
[439]87check_file_lineno(mrb_state *mrb, struct mrb_irep *irep, const char *file, uint16_t lineno)
[279]88{
[439]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;
[279]94
[439]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;
[279]101
[439]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;
[279]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{
[439]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;
[279]128
[439]129 method_name = mrb_sym_name(mrb, method_sym);
[279]130
[439]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 }
[279]147
[439]148 is_defined = mrb_class_defined(mrb, method_p->class_name);
149 if (is_defined == FALSE) {
150 return MRB_DEBUG_OK;
151 }
[279]152
[439]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 }
[279]159
[439]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;
[279]168}
169
170int32_t
171mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *file, uint16_t lineno)
172{
[439]173 int32_t index;
174 char* set_file;
175 uint16_t result;
[279]176
[439]177 if ((mrb == NULL)||(dbg == NULL)||(file == NULL)) {
178 return MRB_DEBUG_INVALID_ARGUMENT;
179 }
[279]180
[439]181 if (dbg->bpnum >= MAX_BREAKPOINT) {
182 return MRB_DEBUG_BREAK_NUM_OVER;
183 }
[279]184
[439]185 if (dbg->next_bpno > MAX_BREAKPOINTNO) {
186 return MRB_DEBUG_BREAK_NO_OVER;
187 }
[279]188
[439]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 }
[279]197
[439]198 set_file = (char*)mrb_malloc(mrb, strlen(file) + 1);
[279]199
[439]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++;
[279]207
[439]208 strncpy(set_file, file, strlen(file) + 1);
[279]209
[439]210 dbg->bp[index].point.linepoint.file = set_file;
[279]211
[439]212 return dbg->bp[index].bpno;
[279]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{
[439]218 int32_t index;
219 char* set_class;
220 char* set_method;
[279]221
[439]222 if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) {
223 return MRB_DEBUG_INVALID_ARGUMENT;
224 }
[279]225
[439]226 if (dbg->bpnum >= MAX_BREAKPOINT) {
227 return MRB_DEBUG_BREAK_NUM_OVER;
228 }
[279]229
[439]230 if (dbg->next_bpno > MAX_BREAKPOINTNO) {
231 return MRB_DEBUG_BREAK_NO_OVER;
232 }
[279]233
[439]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 }
[279]241
[439]242 set_method = (char*)mrb_malloc(mrb, strlen(method_name) + 1);
[279]243
[439]244 strncpy(set_method, method_name, strlen(method_name) + 1);
[279]245
[439]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++;
[279]254
[439]255 return dbg->bp[index].bpno;
[279]256}
257
258int32_t
259mrb_debug_get_breaknum(mrb_state *mrb, mrb_debug_context *dbg)
260{
[439]261 if ((mrb == NULL) || (dbg == NULL)) {
262 return MRB_DEBUG_INVALID_ARGUMENT;
263 }
[279]264
[439]265 return dbg->bpnum;
[279]266}
267
268int32_t
[439]269mrb_debug_get_break_all(mrb_state *mrb, mrb_debug_context *dbg, uint32_t size, mrb_debug_breakpoint *bp)
[279]270{
[439]271 uint32_t get_size = 0;
[279]272
[439]273 if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
274 return MRB_DEBUG_INVALID_ARGUMENT;
275 }
[279]276
[439]277 if (dbg->bpnum >= size) {
278 get_size = size;
279 }
280 else {
281 get_size = dbg->bpnum;
282 }
[279]283
[439]284 memcpy(bp, dbg->bp, sizeof(mrb_debug_breakpoint) * get_size);
[279]285
[439]286 return get_size;
[279]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
[439]294 if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
295 return MRB_DEBUG_INVALID_ARGUMENT;
296 }
[279]297
[439]298 index = get_break_index(dbg, bpno);
299 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
300 return MRB_DEBUG_BREAK_INVALID_NO;
301 }
[279]302
[439]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;
[279]307
[439]308 return 0;
[279]309}
310
311int32_t
312mrb_debug_delete_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
313{
[439]314 uint32_t i;
315 int32_t index;
[279]316
[439]317 if ((mrb == NULL) ||(dbg == NULL)) {
318 return MRB_DEBUG_INVALID_ARGUMENT;
319 }
[279]320
[439]321 index = get_break_index(dbg, bpno);
322 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
323 return MRB_DEBUG_BREAK_INVALID_NO;
324 }
[279]325
[439]326 free_breakpoint(mrb, &dbg->bp[index]);
[279]327
[439]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 }
[279]336
[439]337 dbg->bpnum--;
[279]338
[439]339 return MRB_DEBUG_OK;
[279]340}
341
342int32_t
343mrb_debug_delete_break_all(mrb_state *mrb, mrb_debug_context *dbg)
344{
[439]345 uint32_t i;
[279]346
[439]347 if ((mrb == NULL) || (dbg == NULL)) {
348 return MRB_DEBUG_INVALID_ARGUMENT;
349 }
[279]350
[439]351 for(i = 0 ; i < dbg->bpnum ; i++) {
352 free_breakpoint(mrb, &dbg->bp[i]);
353 }
[279]354
[439]355 dbg->bpnum = 0;
[279]356
[439]357 return MRB_DEBUG_OK;
[279]358}
359
360int32_t
361mrb_debug_enable_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
362{
[439]363 int32_t index = 0;
[279]364
[439]365 if ((mrb == NULL) || (dbg == NULL)) {
366 return MRB_DEBUG_INVALID_ARGUMENT;
367 }
[279]368
[439]369 index = get_break_index(dbg, bpno);
370 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
371 return MRB_DEBUG_BREAK_INVALID_NO;
372 }
[279]373
[439]374 dbg->bp[index].enable = TRUE;
[279]375
[439]376 return MRB_DEBUG_OK;
[279]377}
378
379int32_t
380mrb_debug_enable_break_all(mrb_state *mrb, mrb_debug_context *dbg)
381{
[439]382 uint32_t i;
[279]383
[439]384 if ((mrb == NULL) || (dbg == NULL)) {
385 return MRB_DEBUG_INVALID_ARGUMENT;
386 }
[279]387
[439]388 for(i = 0 ; i < dbg->bpnum; i++) {
389 dbg->bp[i].enable = TRUE;
390 }
[279]391
[439]392 return MRB_DEBUG_OK;
[279]393}
394
395int32_t
396mrb_debug_disable_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
397{
[439]398 int32_t index = 0;
[279]399
[439]400 if ((mrb == NULL) || (dbg == NULL)) {
401 return MRB_DEBUG_INVALID_ARGUMENT;
402 }
[279]403
[439]404 index = get_break_index(dbg, bpno);
405 if (index == MRB_DEBUG_BREAK_INVALID_NO) {
406 return MRB_DEBUG_BREAK_INVALID_NO;
407 }
[279]408
[439]409 dbg->bp[index].enable = FALSE;
[279]410
[439]411 return MRB_DEBUG_OK;
[279]412}
413
414int32_t
415mrb_debug_disable_break_all(mrb_state *mrb, mrb_debug_context *dbg)
416{
[439]417 uint32_t i;
[279]418
[439]419 if ((mrb == NULL) || (dbg == NULL)) {
420 return MRB_DEBUG_INVALID_ARGUMENT;
421 }
[279]422
[439]423 for(i = 0 ; i < dbg->bpnum; i++) {
424 dbg->bp[i].enable = FALSE;
425 }
[279]426
[439]427 return MRB_DEBUG_OK;
[279]428}
429
430static mrb_bool
[439]431check_start_pc_for_line(mrb_state *mrb, mrb_irep *irep, const mrb_code *pc, uint16_t line)
[279]432{
[439]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;
[279]439}
440
441int32_t
442mrb_debug_check_breakpoint_line(mrb_state *mrb, mrb_debug_context *dbg, const char *file, uint16_t line)
443{
[439]444 mrb_debug_breakpoint *bp;
445 mrb_debug_linepoint *line_p;
[279]446 uint32_t i;
447
[439]448 if ((mrb == NULL) || (dbg == NULL) || (file == NULL) || (line <= 0)) {
449 return MRB_DEBUG_INVALID_ARGUMENT;
450 }
[279]451
[439]452 if (!check_start_pc_for_line(mrb, dbg->irep, dbg->pc, line)) {
453 return MRB_DEBUG_OK;
454 }
[279]455
[439]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;
[279]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{
[439]482 mrb_debug_breakpoint *bp;
483 int32_t bpno;
[279]484 uint32_t i;
485
[439]486 if ((mrb == NULL) || (dbg == NULL) || (class_obj == NULL)) {
487 return MRB_DEBUG_INVALID_ARGUMENT;
488 }
[279]489
[439]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 }
[279]505
[439]506 return 0;
[279]507}
Note: See TracBrowser for help on using the repository browser.