source: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c@ 331

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

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

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