[270] | 1 | /*
|
---|
| 2 | ** mrdb.c - mruby debugger
|
---|
| 3 | **
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 | #include <string.h>
|
---|
| 8 | #include <stdio.h>
|
---|
| 9 | #include <ctype.h>
|
---|
| 10 |
|
---|
| 11 | #include "mruby.h"
|
---|
| 12 | #include "mruby/dump.h"
|
---|
| 13 | #include "mruby/debug.h"
|
---|
| 14 | #include "mruby/class.h"
|
---|
| 15 | #include "mruby/opcode.h"
|
---|
| 16 | #include "mruby/variable.h"
|
---|
| 17 |
|
---|
| 18 | #include "mrdb.h"
|
---|
| 19 | #include "apibreak.h"
|
---|
| 20 | #include "apilist.h"
|
---|
| 21 |
|
---|
| 22 | void mrdb_state_free(mrb_state *);
|
---|
| 23 |
|
---|
| 24 | static mrb_debug_context *_debug_context = NULL;
|
---|
| 25 | static mrdb_state *_mrdb_state = NULL;
|
---|
| 26 |
|
---|
| 27 | struct _args {
|
---|
| 28 | FILE *rfp;
|
---|
| 29 | char* fname;
|
---|
| 30 | char* srcpath;
|
---|
| 31 | int argc;
|
---|
| 32 | char** argv;
|
---|
| 33 | mrb_bool mrbfile : 1;
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | typedef struct debug_command {
|
---|
| 37 | const char *cmd1;
|
---|
| 38 | const char *cmd2;
|
---|
| 39 | uint8_t len1;
|
---|
| 40 | uint8_t len2;
|
---|
| 41 | uint8_t div;
|
---|
| 42 | debug_command_id id;
|
---|
| 43 | debug_command_func func;
|
---|
| 44 | } debug_command;
|
---|
| 45 |
|
---|
| 46 | static const debug_command debug_command_list[] = {
|
---|
| 47 | {"break", NULL, 1, 0, 0, DBGCMD_BREAK, dbgcmd_break}, /* b[reak] */
|
---|
| 48 | {"continue", NULL, 1, 0, 0, DBGCMD_CONTINUE, dbgcmd_continue}, /* c[ontinue] */
|
---|
| 49 | {"delete", NULL, 1, 0, 1, DBGCMD_DELETE, dbgcmd_delete}, /* d[elete] */
|
---|
| 50 | {"disable", NULL, 3, 0, 1, DBGCMD_DISABLE, dbgcmd_disable}, /* dis[able] */
|
---|
| 51 | {"enable", NULL, 2, 0, 1, DBGCMD_ENABLE, dbgcmd_enable}, /* en[able] */
|
---|
| 52 | {"eval", NULL, 2, 0, 0, DBGCMD_EVAL, dbgcmd_eval}, /* ev[al] */
|
---|
| 53 | {"help", NULL, 1, 0, 1, DBGCMD_HELP, dbgcmd_help}, /* h[elp] */
|
---|
| 54 | {"info", "breakpoints", 1, 1, 1, DBGCMD_INFO_BREAK, dbgcmd_info_break}, /* i[nfo] b[reakpoints] */
|
---|
| 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 | {NULL}
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | static void
|
---|
| 65 | usage(const char *name)
|
---|
| 66 | {
|
---|
| 67 | static const char *const usage_msg[] = {
|
---|
| 68 | "switches:",
|
---|
| 69 | "-b load and execute RiteBinary (mrb) file",
|
---|
| 70 | "-d specify source directory",
|
---|
| 71 | "--version print the version",
|
---|
| 72 | "--copyright print the copyright",
|
---|
| 73 | NULL
|
---|
| 74 | };
|
---|
| 75 | const char *const *p = usage_msg;
|
---|
| 76 |
|
---|
| 77 | printf("Usage: %s [switches] programfile\n", name);
|
---|
| 78 | while (*p) {
|
---|
| 79 | printf(" %s\n", *p++);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | static int
|
---|
| 84 | parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
|
---|
| 85 | {
|
---|
| 86 | char **origargv = argv;
|
---|
| 87 | static const struct _args args_zero = { 0 };
|
---|
| 88 |
|
---|
| 89 | *args = args_zero;
|
---|
| 90 |
|
---|
| 91 | for (argc--,argv++; argc > 0; argc--,argv++) {
|
---|
| 92 | char *item;
|
---|
| 93 | if (argv[0][0] != '-') break;
|
---|
| 94 |
|
---|
| 95 | item = argv[0] + 1;
|
---|
| 96 | switch (*item++) {
|
---|
| 97 | case 'b':
|
---|
| 98 | args->mrbfile = TRUE;
|
---|
| 99 | break;
|
---|
| 100 | case 'd':
|
---|
| 101 | if (item[0]) {
|
---|
| 102 | goto append_srcpath;
|
---|
| 103 | }
|
---|
| 104 | else if (argc > 1) {
|
---|
| 105 | argc--; argv++;
|
---|
| 106 | item = argv[0];
|
---|
| 107 | append_srcpath:
|
---|
| 108 | if (!args->srcpath) {
|
---|
| 109 | size_t buflen;
|
---|
| 110 | char *buf;
|
---|
| 111 |
|
---|
| 112 | buflen = strlen(item) + 1;
|
---|
| 113 | buf = (char *)mrb_malloc(mrb, buflen);
|
---|
| 114 | memcpy(buf, item, buflen);
|
---|
| 115 | args->srcpath = buf;
|
---|
| 116 | }
|
---|
| 117 | else {
|
---|
| 118 | size_t srcpathlen;
|
---|
| 119 | size_t itemlen;
|
---|
| 120 |
|
---|
| 121 | srcpathlen = strlen(args->srcpath);
|
---|
| 122 | itemlen = strlen(item);
|
---|
| 123 | args->srcpath =
|
---|
| 124 | (char *)mrb_realloc(mrb, args->srcpath, srcpathlen + itemlen + 2);
|
---|
| 125 | args->srcpath[srcpathlen] = '\n';
|
---|
| 126 | memcpy(args->srcpath + srcpathlen + 1, item, itemlen + 1);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | else {
|
---|
| 130 | printf("%s: No path specified for -d\n", *origargv);
|
---|
| 131 | return EXIT_SUCCESS;
|
---|
| 132 | }
|
---|
| 133 | break;
|
---|
| 134 | case '-':
|
---|
| 135 | if (strcmp((*argv) + 2, "version") == 0) {
|
---|
| 136 | mrb_show_version(mrb);
|
---|
| 137 | exit(EXIT_SUCCESS);
|
---|
| 138 | }
|
---|
| 139 | else if (strcmp((*argv) + 2, "copyright") == 0) {
|
---|
| 140 | mrb_show_copyright(mrb);
|
---|
| 141 | exit(EXIT_SUCCESS);
|
---|
| 142 | }
|
---|
| 143 | default:
|
---|
| 144 | return EXIT_FAILURE;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if (args->rfp == NULL) {
|
---|
| 149 | if (*argv == NULL) {
|
---|
| 150 | printf("%s: Program file not specified.\n", *origargv);
|
---|
| 151 | return EXIT_FAILURE;
|
---|
| 152 | }
|
---|
| 153 | else {
|
---|
| 154 | args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
|
---|
| 155 | if (args->rfp == NULL) {
|
---|
| 156 | printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
|
---|
| 157 | return EXIT_FAILURE;
|
---|
| 158 | }
|
---|
| 159 | args->fname = argv[0];
|
---|
| 160 | argc--; argv++;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
|
---|
| 164 | memcpy(args->argv, argv, (argc+1) * sizeof(char*));
|
---|
| 165 | args->argc = argc;
|
---|
| 166 |
|
---|
| 167 | return EXIT_SUCCESS;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | static void
|
---|
| 171 | cleanup(mrb_state *mrb, struct _args *args)
|
---|
| 172 | {
|
---|
| 173 | if (args->rfp)
|
---|
| 174 | fclose(args->rfp);
|
---|
| 175 | if (args->srcpath)
|
---|
| 176 | mrb_free(mrb, args->srcpath);
|
---|
| 177 | if (args->argv)
|
---|
| 178 | mrb_free(mrb, args->argv);
|
---|
| 179 | mrdb_state_free(mrb);
|
---|
| 180 | mrb_close(mrb);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | static mrb_debug_context*
|
---|
| 184 | mrb_debug_context_new(mrb_state *mrb)
|
---|
| 185 | {
|
---|
| 186 | mrb_debug_context *dbg = mrb_malloc(mrb, sizeof(mrb_debug_context));
|
---|
| 187 |
|
---|
| 188 | memset(dbg, 0, sizeof(mrb_debug_context));
|
---|
| 189 |
|
---|
| 190 | dbg->xm = DBG_INIT;
|
---|
| 191 | dbg->xphase = DBG_PHASE_BEFORE_RUN;
|
---|
| 192 | dbg->next_bpno = 1;
|
---|
| 193 |
|
---|
| 194 | return dbg;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | mrb_debug_context*
|
---|
| 198 | mrb_debug_context_get(mrb_state *mrb)
|
---|
| 199 | {
|
---|
| 200 | if (!_debug_context) {
|
---|
| 201 | _debug_context = mrb_debug_context_new(mrb);
|
---|
| 202 | }
|
---|
| 203 | return _debug_context;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | void
|
---|
| 207 | mrb_debug_context_set(mrb_debug_context *dbg)
|
---|
| 208 | {
|
---|
| 209 | _debug_context = dbg;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | void
|
---|
| 213 | mrb_debug_context_free(mrb_state *mrb)
|
---|
| 214 | {
|
---|
| 215 | if (_debug_context) {
|
---|
| 216 | mrb_debug_delete_break_all(mrb, _debug_context);
|
---|
| 217 | mrb_free(mrb, _debug_context);
|
---|
| 218 | _debug_context = NULL;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | static mrdb_state*
|
---|
| 223 | mrdb_state_new(mrb_state *mrb)
|
---|
| 224 | {
|
---|
| 225 | mrdb_state *mrdb = mrb_malloc(mrb, sizeof(mrdb_state));
|
---|
| 226 |
|
---|
| 227 | memset(mrdb, 0, sizeof(mrdb_state));
|
---|
| 228 |
|
---|
| 229 | mrdb->dbg = mrb_debug_context_get(mrb);
|
---|
| 230 | mrdb->command = mrb_malloc(mrb, MAX_COMMAND_LINE+1);
|
---|
| 231 | mrdb->print_no = 1;
|
---|
| 232 |
|
---|
| 233 | return mrdb;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | mrdb_state*
|
---|
| 237 | mrdb_state_get(mrb_state *mrb)
|
---|
| 238 | {
|
---|
| 239 | if (!_mrdb_state) {
|
---|
| 240 | _mrdb_state = mrdb_state_new(mrb);
|
---|
| 241 | }
|
---|
| 242 | return _mrdb_state;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | void
|
---|
| 246 | mrdb_state_set(mrdb_state *mrdb)
|
---|
| 247 | {
|
---|
| 248 | _mrdb_state = mrdb;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | void
|
---|
| 252 | mrdb_state_free(mrb_state *mrb)
|
---|
| 253 | {
|
---|
| 254 | mrb_debug_context_free(mrb);
|
---|
| 255 | if (_mrdb_state) {
|
---|
| 256 | mrb_free(mrb, _mrdb_state->command);
|
---|
| 257 | mrb_free(mrb, _mrdb_state);
|
---|
| 258 | _mrdb_state = NULL;
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | static char*
|
---|
| 263 | get_command(mrb_state *mrb, mrdb_state *mrdb)
|
---|
| 264 | {
|
---|
| 265 | int i;
|
---|
| 266 | int c;
|
---|
| 267 |
|
---|
| 268 | for (i=0; i<MAX_COMMAND_LINE; i++) {
|
---|
| 269 | if ((c=getchar()) == EOF || c == '\n') break;
|
---|
| 270 | mrdb->command[i] = c;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | if (i == 0 && feof(stdin)) {
|
---|
| 274 | clearerr(stdin);
|
---|
| 275 | strcpy(mrdb->command, "quit");
|
---|
| 276 | i += sizeof("quit") - 1;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | if (i == MAX_COMMAND_LINE) {
|
---|
| 280 | for ( ; (c=getchar()) != EOF && c !='\n'; i++) ;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if (i > MAX_COMMAND_LINE) {
|
---|
| 284 | printf("command line too long.\n");
|
---|
| 285 | i = 0; /* discard command data */
|
---|
| 286 | }
|
---|
| 287 | mrdb->command[i] = '\0';
|
---|
| 288 |
|
---|
| 289 | return mrdb->command;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | static char*
|
---|
| 293 | pick_out_word(mrb_state *mrb, char **pp)
|
---|
| 294 | {
|
---|
| 295 | char *ps;
|
---|
| 296 |
|
---|
| 297 | for (ps=*pp; ISBLANK(*ps); ps++) ;
|
---|
| 298 | if (*ps == '\0') {
|
---|
| 299 | return NULL;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | if (*ps == '\"' || *ps == '\'') {
|
---|
| 303 | *pp = strchr(ps+1, *ps);
|
---|
| 304 | if (*pp) (*pp)++;
|
---|
| 305 | }
|
---|
| 306 | else {
|
---|
| 307 | *pp = strpbrk(ps, " \t");
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | if (!*pp) {
|
---|
| 311 | *pp = ps + strlen(ps);
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | if (**pp != '\0') {
|
---|
| 315 | **pp = '\0';
|
---|
| 316 | (*pp)++;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | return ps;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | static debug_command*
|
---|
| 323 | parse_command(mrb_state *mrb, mrdb_state *mrdb, char *buf)
|
---|
| 324 | {
|
---|
| 325 | debug_command *cmd = NULL;
|
---|
| 326 | char *p = buf;
|
---|
| 327 | size_t wlen;
|
---|
| 328 |
|
---|
| 329 | /* get word #1 */
|
---|
| 330 | mrdb->words[0] = pick_out_word(mrb, &p);
|
---|
| 331 | if (!mrdb->words[0]) {
|
---|
| 332 | return NULL;
|
---|
| 333 | }
|
---|
| 334 | mrdb->wcnt = 1;
|
---|
| 335 | /* set remain parameter */
|
---|
| 336 | for ( ; *p && ISBLANK(*p); p++) ;
|
---|
| 337 | if (*p) {
|
---|
| 338 | mrdb->words[mrdb->wcnt++] = p;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /* check word #1 */
|
---|
| 342 | for (cmd=(debug_command*)debug_command_list; cmd->cmd1; cmd++) {
|
---|
| 343 | wlen = strlen(mrdb->words[0]);
|
---|
| 344 | if (wlen >= cmd->len1 &&
|
---|
| 345 | strncmp(mrdb->words[0], cmd->cmd1, wlen) == 0) {
|
---|
| 346 | break;
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | if (cmd->cmd2) {
|
---|
| 351 | if (mrdb->wcnt > 1) {
|
---|
| 352 | /* get word #2 */
|
---|
| 353 | mrdb->words[1] = pick_out_word(mrb, &p);
|
---|
| 354 | if (mrdb->words[1]) {
|
---|
| 355 | /* update remain parameter */
|
---|
| 356 | for ( ; *p && ISBLANK(*p); p++) ;
|
---|
| 357 | if (*p) {
|
---|
| 358 | mrdb->words[mrdb->wcnt++] = p;
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | /* check word #1,#2 */
|
---|
| 364 | for ( ; cmd->cmd1; cmd++) {
|
---|
| 365 | wlen = strlen(mrdb->words[0]);
|
---|
| 366 | if (wlen < cmd->len1 ||
|
---|
| 367 | strncmp(mrdb->words[0], cmd->cmd1, wlen)) {
|
---|
| 368 | continue;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | if (!cmd->cmd2) break; /* word #1 only */
|
---|
| 372 |
|
---|
| 373 | if (mrdb->wcnt == 1) continue; /* word #2 not specified */
|
---|
| 374 |
|
---|
| 375 | wlen = strlen(mrdb->words[1]);
|
---|
| 376 | if (wlen >= cmd->len2 &&
|
---|
| 377 | strncmp(mrdb->words[1], cmd->cmd2, wlen) == 0) {
|
---|
| 378 | break; /* word #1 and #2 */
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | /* divide remain parameters */
|
---|
| 384 | if (cmd->cmd1 && cmd->div) {
|
---|
| 385 | p = mrdb->words[--mrdb->wcnt];
|
---|
| 386 | for ( ; mrdb->wcnt<MAX_COMMAND_WORD; mrdb->wcnt++) {
|
---|
| 387 | mrdb->words[mrdb->wcnt] = pick_out_word(mrb, &p);
|
---|
| 388 | if (!mrdb->words[mrdb->wcnt]) {
|
---|
| 389 | break;
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | return cmd->cmd1 ? cmd : NULL;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | static void
|
---|
| 398 | print_info_stopped_break(mrb_state *mrb, mrdb_state *mrdb)
|
---|
| 399 | {
|
---|
| 400 | mrb_debug_breakpoint bp;
|
---|
| 401 | int32_t ret;
|
---|
| 402 | uint16_t lineno;
|
---|
| 403 | const char *file;
|
---|
| 404 | const char *method_name;
|
---|
| 405 | const char *class_name;
|
---|
| 406 |
|
---|
| 407 | ret = mrb_debug_get_break(mrb, mrdb->dbg, mrdb->dbg->stopped_bpno, &bp);
|
---|
| 408 | if(ret == 0) {
|
---|
| 409 | switch(bp.type) {
|
---|
| 410 | case MRB_DEBUG_BPTYPE_LINE:
|
---|
| 411 | file = bp.point.linepoint.file;
|
---|
| 412 | lineno = bp.point.linepoint.lineno;
|
---|
| 413 | printf("Breakpoint %d, at %s:%d\n", bp.bpno, file, lineno);
|
---|
| 414 | break;
|
---|
| 415 | case MRB_DEBUG_BPTYPE_METHOD:
|
---|
| 416 | method_name = bp.point.methodpoint.method_name;
|
---|
| 417 | class_name = bp.point.methodpoint.class_name;
|
---|
| 418 | if(class_name == NULL) {
|
---|
| 419 | printf("Breakpoint %d, %s\n", bp.bpno, method_name);
|
---|
| 420 | }
|
---|
| 421 | else {
|
---|
| 422 | printf("Breakpoint %d, %s:%s\n", bp.bpno, class_name, method_name);
|
---|
| 423 | }
|
---|
| 424 | if(mrdb->dbg->isCfunc) {
|
---|
| 425 | printf("Stopped before calling the C function.\n");
|
---|
| 426 | }
|
---|
| 427 | break;
|
---|
| 428 | default:
|
---|
| 429 | break;
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | static void
|
---|
| 435 | print_info_stopped_step_next(mrb_state *mrb, mrdb_state *mrdb)
|
---|
| 436 | {
|
---|
| 437 | const char* file = mrdb->dbg->prvfile;
|
---|
| 438 | uint16_t lineno = mrdb->dbg->prvline;
|
---|
| 439 | printf("%s:%d\n", file, lineno);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | static void
|
---|
| 443 | print_info_stopped_code(mrb_state *mrb, mrdb_state *mrdb)
|
---|
| 444 | {
|
---|
| 445 | char* file = mrb_debug_get_source(mrb, mrdb, mrdb->srcpath, mrdb->dbg->prvfile);
|
---|
| 446 | uint16_t lineno = mrdb->dbg->prvline;
|
---|
| 447 | if(file != NULL) {
|
---|
| 448 | mrb_debug_list(mrb, mrdb->dbg, file, lineno, lineno);
|
---|
| 449 | mrb_free(mrb, file);
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | static void
|
---|
| 454 | print_info_stopped(mrb_state *mrb, mrdb_state *mrdb)
|
---|
| 455 | {
|
---|
| 456 | switch(mrdb->dbg->bm) {
|
---|
| 457 | case BRK_BREAK:
|
---|
| 458 | print_info_stopped_break(mrb, mrdb);
|
---|
| 459 | print_info_stopped_code(mrb, mrdb);
|
---|
| 460 | break;
|
---|
| 461 | case BRK_STEP:
|
---|
| 462 | case BRK_NEXT:
|
---|
| 463 | print_info_stopped_step_next(mrb, mrdb);
|
---|
| 464 | print_info_stopped_code(mrb, mrdb);
|
---|
| 465 | break;
|
---|
| 466 | default:
|
---|
| 467 | break;
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | static debug_command*
|
---|
| 472 | get_and_parse_command(mrb_state *mrb, mrdb_state *mrdb)
|
---|
| 473 | {
|
---|
| 474 | debug_command *cmd = NULL;
|
---|
| 475 | char *p;
|
---|
| 476 | int i;
|
---|
| 477 |
|
---|
| 478 | while (!cmd) {
|
---|
| 479 | for (p=NULL; !p || *p=='\0'; ) {
|
---|
| 480 | printf("(%s:%d) ", mrdb->dbg->prvfile, mrdb->dbg->prvline);
|
---|
| 481 | fflush(stdout);
|
---|
| 482 | p = get_command(mrb, mrdb);
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | cmd = parse_command(mrb, mrdb, p);
|
---|
| 486 | #ifdef _DBG_MRDB_PARSER_
|
---|
| 487 | for (i=0; i<mrdb->wcnt; i++) {
|
---|
| 488 | printf("%d: %s\n", i, mrdb->words[i]);
|
---|
| 489 | }
|
---|
| 490 | #endif
|
---|
| 491 | if (!cmd) {
|
---|
| 492 | printf("invalid command (");
|
---|
| 493 | for (i=0; i<mrdb->wcnt; i++) {
|
---|
| 494 | if (i>0) {
|
---|
| 495 | printf(" ");
|
---|
| 496 | }
|
---|
| 497 | printf("%s", mrdb->words[i]);
|
---|
| 498 | }
|
---|
| 499 | puts(")");
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 | return cmd;
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | static int32_t
|
---|
| 506 | check_method_breakpoint(mrb_state *mrb, mrb_irep *irep, mrb_code *pc, mrb_value *regs)
|
---|
| 507 | {
|
---|
| 508 | struct RClass* c;
|
---|
| 509 | mrb_sym sym;
|
---|
| 510 | int32_t bpno;
|
---|
| 511 | mrb_bool isCfunc;
|
---|
| 512 |
|
---|
| 513 | mrb_debug_context *dbg = mrb_debug_context_get(mrb);
|
---|
| 514 |
|
---|
| 515 | isCfunc = FALSE;
|
---|
| 516 | bpno = dbg->method_bpno;
|
---|
| 517 | dbg->method_bpno = 0;
|
---|
| 518 |
|
---|
| 519 | switch(GET_OPCODE(*pc)) {
|
---|
| 520 | case OP_SEND:
|
---|
| 521 | case OP_SENDB:
|
---|
| 522 | c = mrb_class(mrb, regs[GETARG_A(*pc)]);
|
---|
| 523 | sym = irep->syms[GETARG_B(*pc)];
|
---|
| 524 | break;
|
---|
| 525 | case OP_SUPER:
|
---|
| 526 | c = mrb->c->ci->target_class->super;
|
---|
| 527 | sym = mrb->c->ci->mid;
|
---|
| 528 | break;
|
---|
| 529 | default:
|
---|
| 530 | sym = 0;
|
---|
| 531 | break;
|
---|
| 532 | }
|
---|
| 533 | if(sym != 0) {
|
---|
| 534 | dbg->method_bpno = mrb_debug_check_breakpoint_method(mrb, dbg, c, sym, &isCfunc);
|
---|
| 535 | if(isCfunc) {
|
---|
| 536 | bpno = dbg->method_bpno;
|
---|
| 537 | dbg->method_bpno = 0;
|
---|
| 538 | }
|
---|
| 539 | }
|
---|
| 540 | dbg->isCfunc = isCfunc;
|
---|
| 541 | return bpno;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | static void
|
---|
| 545 | mrb_code_fetch_hook(mrb_state *mrb, mrb_irep *irep, mrb_code *pc, mrb_value *regs)
|
---|
| 546 | {
|
---|
| 547 | const char *file;
|
---|
| 548 | int32_t line;
|
---|
| 549 | int32_t bpno;
|
---|
| 550 |
|
---|
| 551 | mrb_debug_context *dbg = mrb_debug_context_get(mrb);
|
---|
| 552 |
|
---|
| 553 | mrb_assert(dbg);
|
---|
| 554 |
|
---|
| 555 | dbg->irep = irep;
|
---|
| 556 | dbg->pc = pc;
|
---|
| 557 | dbg->regs = regs;
|
---|
| 558 |
|
---|
| 559 | if(dbg->xphase == DBG_PHASE_RESTART) {
|
---|
| 560 | dbg->root_irep = irep;
|
---|
| 561 | dbg->prvfile = NULL;
|
---|
| 562 | dbg->prvline = 0;
|
---|
| 563 | dbg->xm = DBG_RUN;
|
---|
| 564 | dbg->xphase = DBG_PHASE_RUNNING;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | file = mrb_debug_get_filename(irep, (uint32_t)(pc - irep->iseq));
|
---|
| 568 | line = mrb_debug_get_line(irep, (uint32_t)(pc - irep->iseq));
|
---|
| 569 |
|
---|
| 570 | switch (dbg->xm) {
|
---|
| 571 | case DBG_STEP:
|
---|
| 572 | case DBG_NEXT: // temporary
|
---|
| 573 | if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
|
---|
| 574 | return;
|
---|
| 575 | }
|
---|
| 576 | dbg->method_bpno = 0;
|
---|
| 577 | dbg->bm = BRK_STEP;
|
---|
| 578 | break;
|
---|
| 579 |
|
---|
| 580 | case DBG_RUN:
|
---|
| 581 | bpno = check_method_breakpoint(mrb, irep, pc, regs);
|
---|
| 582 | if (bpno > 0) {
|
---|
| 583 | dbg->stopped_bpno = bpno;
|
---|
| 584 | dbg->bm = BRK_BREAK;
|
---|
| 585 | break;
|
---|
| 586 | }
|
---|
| 587 | if (dbg->prvfile != file || dbg->prvline != line) {
|
---|
| 588 | bpno = mrb_debug_check_breakpoint_line(mrb, dbg, file, line);
|
---|
| 589 | if (bpno > 0) {
|
---|
| 590 | dbg->stopped_bpno = bpno;
|
---|
| 591 | dbg->bm = BRK_BREAK;
|
---|
| 592 | break;
|
---|
| 593 | }
|
---|
| 594 | }
|
---|
| 595 | dbg->prvfile = file;
|
---|
| 596 | dbg->prvline = line;
|
---|
| 597 | return;
|
---|
| 598 | case DBG_INIT:
|
---|
| 599 | dbg->root_irep = irep;
|
---|
| 600 | dbg->bm = BRK_INIT;
|
---|
| 601 | if (!file || line < 0) {
|
---|
| 602 | puts("Cannot get debugging information.");
|
---|
| 603 | }
|
---|
| 604 | break;
|
---|
| 605 |
|
---|
| 606 | default:
|
---|
| 607 | return;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | dbg->prvfile = file;
|
---|
| 611 | dbg->prvline = line;
|
---|
| 612 |
|
---|
| 613 | if(dbg->bm == BRK_BREAK && --dbg->ccnt > 0) {
|
---|
| 614 | return;
|
---|
| 615 | }
|
---|
| 616 | dbg->break_hook(mrb, dbg);
|
---|
| 617 |
|
---|
| 618 | dbg->xphase = DBG_PHASE_RUNNING;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | static mrdb_exemode
|
---|
| 622 | mrb_debug_break_hook(mrb_state *mrb, mrb_debug_context *dbg)
|
---|
| 623 | {
|
---|
| 624 | debug_command *cmd;
|
---|
| 625 | dbgcmd_state st = DBGST_CONTINUE;
|
---|
| 626 | mrdb_state *mrdb = mrdb_state_get(mrb);
|
---|
| 627 |
|
---|
| 628 | print_info_stopped(mrb, mrdb);
|
---|
| 629 |
|
---|
| 630 | while (1) {
|
---|
| 631 | cmd = get_and_parse_command(mrb, mrdb);
|
---|
| 632 | mrb_assert(cmd);
|
---|
| 633 |
|
---|
| 634 | st = cmd->func(mrb, mrdb);
|
---|
| 635 |
|
---|
| 636 | if( (st == DBGST_CONTINUE) || (st == DBGST_RESTART) ) break;
|
---|
| 637 | }
|
---|
| 638 | return dbg->xm;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | int
|
---|
| 642 | main(int argc, char **argv)
|
---|
| 643 | {
|
---|
| 644 | mrb_state *mrb = mrb_open();
|
---|
| 645 | int n = -1;
|
---|
| 646 | struct _args args;
|
---|
| 647 | mrb_value v;
|
---|
| 648 | mrdb_state *mrdb;
|
---|
| 649 | mrdb_state *mrdb_backup;
|
---|
| 650 | mrb_debug_context* dbg_backup;
|
---|
| 651 | debug_command *cmd;
|
---|
| 652 |
|
---|
| 653 | l_restart:
|
---|
| 654 |
|
---|
| 655 | if (mrb == NULL) {
|
---|
| 656 | fputs("Invalid mrb_state, exiting mruby\n", stderr);
|
---|
| 657 | return EXIT_FAILURE;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | /* parse command parameters */
|
---|
| 661 | n = parse_args(mrb, argc, argv, &args);
|
---|
| 662 | if (n == EXIT_FAILURE || args.rfp == NULL) {
|
---|
| 663 | cleanup(mrb, &args);
|
---|
| 664 | usage(argv[0]);
|
---|
| 665 | return n;
|
---|
| 666 | }
|
---|
| 667 |
|
---|
| 668 | /* initialize debugger information */
|
---|
| 669 | mrdb = mrdb_state_get(mrb);
|
---|
| 670 | mrb_assert(mrdb && mrdb->dbg);
|
---|
| 671 | mrdb->srcpath = args.srcpath;
|
---|
| 672 |
|
---|
| 673 | if(mrdb->dbg->xm == DBG_QUIT) {
|
---|
| 674 | mrdb->dbg->xphase = DBG_PHASE_RESTART;
|
---|
| 675 | }
|
---|
| 676 | else {
|
---|
| 677 | mrdb->dbg->xphase = DBG_PHASE_BEFORE_RUN;
|
---|
| 678 | }
|
---|
| 679 | mrdb->dbg->xm = DBG_INIT;
|
---|
| 680 | mrdb->dbg->ccnt = 1;
|
---|
| 681 |
|
---|
| 682 | /* setup hook functions */
|
---|
| 683 | mrb->code_fetch_hook = mrb_code_fetch_hook;
|
---|
| 684 | mrdb->dbg->break_hook = mrb_debug_break_hook;
|
---|
| 685 |
|
---|
| 686 | if (args.mrbfile) { /* .mrb */
|
---|
| 687 | v = mrb_load_irep_file(mrb, args.rfp);
|
---|
| 688 | }
|
---|
| 689 | else { /* .rb */
|
---|
| 690 | mrbc_context *cc = mrbc_context_new(mrb);
|
---|
| 691 | mrbc_filename(mrb, cc, args.fname);
|
---|
| 692 | v = mrb_load_file_cxt(mrb, args.rfp, cc);
|
---|
| 693 | mrbc_context_free(mrb, cc);
|
---|
| 694 | }
|
---|
| 695 | if (mrdb->dbg->xm == DBG_QUIT && !mrb_undef_p(v) && mrb->exc) {
|
---|
| 696 | const char *classname = mrb_obj_classname(mrb, mrb_obj_value(mrb->exc));
|
---|
| 697 | if (!strcmp(classname, "DebuggerExit")) {
|
---|
| 698 | cleanup(mrb, &args);
|
---|
| 699 | return 0;
|
---|
| 700 | }
|
---|
| 701 | if (!strcmp(classname, "DebuggerRestart")) {
|
---|
| 702 | mrdb_backup = mrdb_state_get(mrb);
|
---|
| 703 | dbg_backup = mrb_debug_context_get(mrb);
|
---|
| 704 |
|
---|
| 705 | mrdb_state_set(NULL);
|
---|
| 706 | mrb_debug_context_set(NULL);
|
---|
| 707 |
|
---|
| 708 | cleanup(mrb, &args);
|
---|
| 709 | mrb = mrb_open();
|
---|
| 710 |
|
---|
| 711 | mrdb_state_set(mrdb_backup);
|
---|
| 712 | mrb_debug_context_set(dbg_backup);
|
---|
| 713 |
|
---|
| 714 | goto l_restart;
|
---|
| 715 | }
|
---|
| 716 | }
|
---|
| 717 | puts("mruby application exited.");
|
---|
| 718 | mrdb->dbg->xphase = DBG_PHASE_AFTER_RUN;
|
---|
| 719 | if (!mrb_undef_p(v)) {
|
---|
| 720 | if (mrb->exc) {
|
---|
| 721 | mrb_print_error(mrb);
|
---|
| 722 | }
|
---|
| 723 | else {
|
---|
| 724 | printf(" => ");
|
---|
| 725 | mrb_p(mrb, v);
|
---|
| 726 | }
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | mrdb->dbg->prvfile = "-";
|
---|
| 730 | mrdb->dbg->prvline = 0;
|
---|
| 731 |
|
---|
| 732 | while (1) {
|
---|
| 733 | cmd = get_and_parse_command(mrb, mrdb);
|
---|
| 734 | mrb_assert(cmd);
|
---|
| 735 |
|
---|
| 736 | if (cmd->id == DBGCMD_QUIT) {
|
---|
| 737 | break;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | if( cmd->func(mrb, mrdb) == DBGST_RESTART ) goto l_restart;
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | cleanup(mrb, &args);
|
---|
| 744 |
|
---|
| 745 | return 0;
|
---|
| 746 | }
|
---|