2 ** mrdb.c - mruby debugger
11 #include <mruby/dump.h>
12 #include <mruby/debug.h>
13 #include <mruby/class.h>
14 #include <mruby/opcode.h>
15 #include <mruby/variable.h>
21 void mrdb_state_free(mrb_state *);
23 static mrb_debug_context *_debug_context = NULL;
24 static mrdb_state *_mrdb_state = NULL;
35 typedef struct debug_command {
42 debug_command_func func;
45 static const debug_command debug_command_list[] = {
46 {"break", NULL, 1, 0, 0, DBGCMD_BREAK, dbgcmd_break}, /* b[reak] */
47 {"continue", NULL, 1, 0, 0, DBGCMD_CONTINUE, dbgcmd_continue}, /* c[ontinue] */
48 {"delete", NULL, 1, 0, 1, DBGCMD_DELETE, dbgcmd_delete}, /* d[elete] */
49 {"disable", NULL, 3, 0, 1, DBGCMD_DISABLE, dbgcmd_disable}, /* dis[able] */
50 {"enable", NULL, 2, 0, 1, DBGCMD_ENABLE, dbgcmd_enable}, /* en[able] */
51 {"eval", NULL, 2, 0, 0, DBGCMD_EVAL, dbgcmd_eval}, /* ev[al] */
52 {"help", NULL, 1, 0, 1, DBGCMD_HELP, dbgcmd_help}, /* h[elp] */
53 {"info", "breakpoints", 1, 1, 1, DBGCMD_INFO_BREAK, dbgcmd_info_break}, /* i[nfo] b[reakpoints] */
54 {"info", "locals", 1, 1, 0, DBGCMD_INFO_LOCAL, dbgcmd_info_local}, /* i[nfo] l[ocals] */
55 {"list", NULL, 1, 0, 1, DBGCMD_LIST, dbgcmd_list}, /* l[ist] */
56 {"print", NULL, 1, 0, 0, DBGCMD_PRINT, dbgcmd_print}, /* p[rint] */
57 {"quit", NULL, 1, 0, 0, DBGCMD_QUIT, dbgcmd_quit}, /* q[uit] */
58 {"run", NULL, 1, 0, 0, DBGCMD_RUN, dbgcmd_run}, /* r[un] */
59 {"step", NULL, 1, 0, 1, DBGCMD_STEP, dbgcmd_step}, /* s[tep] */
60 {"next", NULL, 1, 0, 1, DBGCMD_NEXT, dbgcmd_next}, /* n[ext] */
66 usage(const char *name)
68 static const char *const usage_msg[] = {
70 "-b load and execute RiteBinary (mrb) file",
71 "-d specify source directory",
72 "--version print the version",
73 "--copyright print the copyright",
76 const char *const *p = usage_msg;
78 printf("Usage: %s [switches] programfile\n", name);
80 printf(" %s\n", *p++);
85 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
87 char **origargv = argv;
88 static const struct _args args_zero = { 0 };
92 for (argc--,argv++; argc > 0; argc--,argv++) {
94 if (argv[0][0] != '-') break;
109 if (!args->srcpath) {
113 buflen = strlen(item) + 1;
114 buf = (char *)mrb_malloc(mrb, buflen);
115 memcpy(buf, item, buflen);
122 srcpathlen = strlen(args->srcpath);
123 itemlen = strlen(item);
125 (char *)mrb_realloc(mrb, args->srcpath, srcpathlen + itemlen + 2);
126 args->srcpath[srcpathlen] = '\n';
127 memcpy(args->srcpath + srcpathlen + 1, item, itemlen + 1);
131 printf("%s: No path specified for -d\n", *origargv);
136 if (strcmp((*argv) + 2, "version") == 0) {
137 mrb_show_version(mrb);
140 else if (strcmp((*argv) + 2, "copyright") == 0) {
141 mrb_show_copyright(mrb);
149 if (args->rfp == NULL) {
151 printf("%s: Program file not specified.\n", *origargv);
155 args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
156 if (args->rfp == NULL) {
157 printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
160 args->fname = argv[0];
164 args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
165 memcpy(args->argv, argv, (argc+1) * sizeof(char*));
172 cleanup(mrb_state *mrb, struct _args *args)
177 mrb_free(mrb, args->srcpath);
179 mrb_free(mrb, args->argv);
180 mrdb_state_free(mrb);
184 static mrb_debug_context*
185 mrb_debug_context_new(mrb_state *mrb)
187 mrb_debug_context *dbg = (mrb_debug_context*)mrb_malloc(mrb, sizeof(mrb_debug_context));
189 memset(dbg, 0, sizeof(mrb_debug_context));
192 dbg->xphase = DBG_PHASE_BEFORE_RUN;
199 mrb_debug_context_get(mrb_state *mrb)
201 if (!_debug_context) {
202 _debug_context = mrb_debug_context_new(mrb);
204 return _debug_context;
208 mrb_debug_context_set(mrb_debug_context *dbg)
210 _debug_context = dbg;
214 mrb_debug_context_free(mrb_state *mrb)
216 if (_debug_context) {
217 mrb_debug_delete_break_all(mrb, _debug_context);
218 mrb_free(mrb, _debug_context);
219 _debug_context = NULL;
224 mrdb_state_new(mrb_state *mrb)
226 mrdb_state *mrdb = (mrdb_state*)mrb_malloc(mrb, sizeof(mrdb_state));
228 memset(mrdb, 0, sizeof(mrdb_state));
230 mrdb->dbg = mrb_debug_context_get(mrb);
231 mrdb->command = (char*)mrb_malloc(mrb, MAX_COMMAND_LINE+1);
238 mrdb_state_get(mrb_state *mrb)
241 _mrdb_state = mrdb_state_new(mrb);
247 mrdb_state_set(mrdb_state *mrdb)
253 mrdb_state_free(mrb_state *mrb)
255 mrb_debug_context_free(mrb);
257 mrb_free(mrb, _mrdb_state->command);
258 mrb_free(mrb, _mrdb_state);
264 get_command(mrb_state *mrb, mrdb_state *mrdb)
269 for (i=0; i<MAX_COMMAND_LINE; i++) {
270 if ((c=getchar()) == EOF || c == '\n') break;
271 mrdb->command[i] = c;
274 if (i == 0 && feof(stdin)) {
276 strcpy(mrdb->command, "quit");
277 i += sizeof("quit") - 1;
280 if (i == MAX_COMMAND_LINE) {
281 for ( ; (c=getchar()) != EOF && c !='\n'; i++) ;
284 if (i > MAX_COMMAND_LINE) {
285 printf("command line too long.\n");
286 i = 0; /* discard command data */
288 mrdb->command[i] = '\0';
290 return mrdb->command;
294 pick_out_word(mrb_state *mrb, char **pp)
298 for (ps=*pp; ISBLANK(*ps); ps++) ;
303 if (*ps == '\"' || *ps == '\'') {
304 *pp = strchr(ps+1, *ps);
308 *pp = strpbrk(ps, " \t");
312 *pp = ps + strlen(ps);
323 static debug_command*
324 parse_command(mrb_state *mrb, mrdb_state *mrdb, char *buf)
326 debug_command *cmd = NULL;
331 mrdb->words[0] = pick_out_word(mrb, &p);
332 if (!mrdb->words[0]) {
336 /* set remain parameter */
337 for ( ; *p && ISBLANK(*p); p++) ;
339 mrdb->words[mrdb->wcnt++] = p;
343 for (cmd=(debug_command*)debug_command_list; cmd->cmd1; cmd++) {
344 wlen = strlen(mrdb->words[0]);
345 if (wlen >= cmd->len1 &&
346 strncmp(mrdb->words[0], cmd->cmd1, wlen) == 0) {
352 if (mrdb->wcnt > 1) {
354 mrdb->words[1] = pick_out_word(mrb, &p);
355 if (mrdb->words[1]) {
356 /* update remain parameter */
357 for ( ; *p && ISBLANK(*p); p++) ;
359 mrdb->words[mrdb->wcnt++] = p;
364 /* check word #1,#2 */
365 for ( ; cmd->cmd1; cmd++) {
366 wlen = strlen(mrdb->words[0]);
367 if (wlen < cmd->len1 ||
368 strncmp(mrdb->words[0], cmd->cmd1, wlen)) {
372 if (!cmd->cmd2) break; /* word #1 only */
374 if (mrdb->wcnt == 1) continue; /* word #2 not specified */
376 wlen = strlen(mrdb->words[1]);
377 if (wlen >= cmd->len2 &&
378 strncmp(mrdb->words[1], cmd->cmd2, wlen) == 0) {
379 break; /* word #1 and #2 */
384 /* divide remain parameters */
385 if (cmd->cmd1 && cmd->div) {
386 p = mrdb->words[--mrdb->wcnt];
387 for ( ; mrdb->wcnt<MAX_COMMAND_WORD; mrdb->wcnt++) {
388 mrdb->words[mrdb->wcnt] = pick_out_word(mrb, &p);
389 if (!mrdb->words[mrdb->wcnt]) {
395 return cmd->cmd1 ? cmd : NULL;
399 print_info_stopped_break(mrb_state *mrb, mrdb_state *mrdb)
401 mrb_debug_breakpoint bp;
405 const char *method_name;
406 const char *class_name;
408 ret = mrb_debug_get_break(mrb, mrdb->dbg, mrdb->dbg->stopped_bpno, &bp);
411 case MRB_DEBUG_BPTYPE_LINE:
412 file = bp.point.linepoint.file;
413 lineno = bp.point.linepoint.lineno;
414 printf("Breakpoint %d, at %s:%d\n", bp.bpno, file, lineno);
416 case MRB_DEBUG_BPTYPE_METHOD:
417 method_name = bp.point.methodpoint.method_name;
418 class_name = bp.point.methodpoint.class_name;
419 if (class_name == NULL) {
420 printf("Breakpoint %d, %s\n", bp.bpno, method_name);
423 printf("Breakpoint %d, %s:%s\n", bp.bpno, class_name, method_name);
425 if (mrdb->dbg->isCfunc) {
426 printf("Stopped before calling the C function.\n");
436 print_info_stopped_step_next(mrb_state *mrb, mrdb_state *mrdb)
438 const char* file = mrdb->dbg->prvfile;
439 uint16_t lineno = mrdb->dbg->prvline;
440 printf("%s:%d\n", file, lineno);
444 print_info_stopped_code(mrb_state *mrb, mrdb_state *mrdb)
446 char* file = mrb_debug_get_source(mrb, mrdb, mrdb->srcpath, mrdb->dbg->prvfile);
447 uint16_t lineno = mrdb->dbg->prvline;
449 mrb_debug_list(mrb, mrdb->dbg, file, lineno, lineno);
455 print_info_stopped(mrb_state *mrb, mrdb_state *mrdb)
457 switch(mrdb->dbg->bm) {
459 print_info_stopped_break(mrb, mrdb);
460 print_info_stopped_code(mrb, mrdb);
464 print_info_stopped_step_next(mrb, mrdb);
465 print_info_stopped_code(mrb, mrdb);
472 static debug_command*
473 get_and_parse_command(mrb_state *mrb, mrdb_state *mrdb)
475 debug_command *cmd = NULL;
480 for (p=NULL; !p || *p=='\0'; ) {
481 printf("(%s:%d) ", mrdb->dbg->prvfile, mrdb->dbg->prvline);
483 p = get_command(mrb, mrdb);
486 cmd = parse_command(mrb, mrdb, p);
487 #ifdef _DBG_MRDB_PARSER_
488 for (i=0; i<mrdb->wcnt; i++) {
489 printf("%d: %s\n", i, mrdb->words[i]);
493 printf("invalid command (");
494 for (i=0; i<mrdb->wcnt; i++) {
498 printf("%s", mrdb->words[i]);
507 check_method_breakpoint(mrb_state *mrb, mrb_irep *irep, const mrb_code *pc, mrb_value *regs)
513 struct mrb_insn_data insn;
515 mrb_debug_context *dbg = mrb_debug_context_get(mrb);
518 bpno = dbg->method_bpno;
519 dbg->method_bpno = 0;
521 insn = mrb_decode_insn(pc);
525 c = mrb_class(mrb, regs[insn.a]);
526 sym = irep->syms[insn.b];
529 c = mrb->c->ci->target_class->super;
530 sym = mrb->c->ci->mid;
537 dbg->method_bpno = mrb_debug_check_breakpoint_method(mrb, dbg, c, sym, &isCfunc);
539 bpno = dbg->method_bpno;
540 dbg->method_bpno = 0;
543 dbg->isCfunc = isCfunc;
548 mrb_code_fetch_hook(mrb_state *mrb, mrb_irep *irep, const mrb_code *pc, mrb_value *regs)
554 mrb_debug_context *dbg = mrb_debug_context_get(mrb);
562 if (dbg->xphase == DBG_PHASE_RESTART) {
563 dbg->root_irep = irep;
568 dbg->xphase = DBG_PHASE_RUNNING;
571 file = mrb_debug_get_filename(mrb, irep, pc - irep->iseq);
572 line = mrb_debug_get_line(mrb, irep, pc - irep->iseq);
576 if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
579 dbg->method_bpno = 0;
584 if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
587 if ((intptr_t)(dbg->prvci) < (intptr_t)(mrb->c->ci)) {
591 dbg->method_bpno = 0;
596 bpno = check_method_breakpoint(mrb, irep, pc, regs);
598 dbg->stopped_bpno = bpno;
602 if (dbg->prvfile != file || dbg->prvline != line) {
603 bpno = mrb_debug_check_breakpoint_line(mrb, dbg, file, line);
605 dbg->stopped_bpno = bpno;
614 dbg->root_irep = irep;
616 if (!file || line < 0) {
617 puts("Cannot get debugging information.");
628 if (dbg->bm == BRK_BREAK && --dbg->ccnt > 0) {
631 dbg->break_hook(mrb, dbg);
633 dbg->xphase = DBG_PHASE_RUNNING;
637 mrb_debug_break_hook(mrb_state *mrb, mrb_debug_context *dbg)
640 dbgcmd_state st = DBGST_CONTINUE;
641 mrdb_state *mrdb = mrdb_state_get(mrb);
643 print_info_stopped(mrb, mrdb);
646 cmd = get_and_parse_command(mrb, mrdb);
649 st = cmd->func(mrb, mrdb);
651 if ((st == DBGST_CONTINUE) || (st == DBGST_RESTART)) break;
657 main(int argc, char **argv)
659 mrb_state *mrb = mrb_open();
664 mrdb_state *mrdb_backup;
665 mrb_debug_context* dbg_backup;
671 fputs("Invalid mrb_state, exiting mruby\n", stderr);
675 /* parse command parameters */
676 n = parse_args(mrb, argc, argv, &args);
677 if (n == EXIT_FAILURE || args.rfp == NULL) {
683 /* initialize debugger information */
684 mrdb = mrdb_state_get(mrb);
685 mrb_assert(mrdb && mrdb->dbg);
686 mrdb->srcpath = args.srcpath;
688 if (mrdb->dbg->xm == DBG_QUIT) {
689 mrdb->dbg->xphase = DBG_PHASE_RESTART;
692 mrdb->dbg->xphase = DBG_PHASE_BEFORE_RUN;
694 mrdb->dbg->xm = DBG_INIT;
697 /* setup hook functions */
698 mrb->code_fetch_hook = mrb_code_fetch_hook;
699 mrdb->dbg->break_hook = mrb_debug_break_hook;
701 if (args.mrbfile) { /* .mrb */
702 v = mrb_load_irep_file(mrb, args.rfp);
705 mrbc_context *cc = mrbc_context_new(mrb);
706 mrbc_filename(mrb, cc, args.fname);
707 v = mrb_load_file_cxt(mrb, args.rfp, cc);
708 mrbc_context_free(mrb, cc);
710 if (mrdb->dbg->xm == DBG_QUIT && !mrb_undef_p(v) && mrb->exc) {
711 const char *classname = mrb_obj_classname(mrb, mrb_obj_value(mrb->exc));
712 if (!strcmp(classname, "DebuggerExit")) {
716 if (!strcmp(classname, "DebuggerRestart")) {
717 mrdb_backup = mrdb_state_get(mrb);
718 dbg_backup = mrb_debug_context_get(mrb);
720 mrdb_state_set(NULL);
721 mrb_debug_context_set(NULL);
726 mrdb_state_set(mrdb_backup);
727 mrb_debug_context_set(dbg_backup);
732 puts("mruby application exited.");
733 mrdb->dbg->xphase = DBG_PHASE_AFTER_RUN;
734 if (!mrb_undef_p(v)) {
736 mrb_print_error(mrb);
744 mrdb->dbg->prvfile = "-";
745 mrdb->dbg->prvline = 0;
748 cmd = get_and_parse_command(mrb, mrdb);
751 if (cmd->id == DBGCMD_QUIT) {
755 if ( cmd->func(mrb, mrdb) == DBGST_RESTART ) goto l_restart;