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