Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c

raw
1 /*
2 ** cmdmisc.c - mruby debugger miscellaneous command functions
3 **
4 */
5
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "apilist.h"
11 #include "apistring.h"
12 #include <mruby/compile.h>
13
14 typedef struct help_msg {
15 const char *cmd1;
16 const char *cmd2;
17 const char *short_msg;
18 const char *long_msg;
19 } help_msg;
20
21 static help_msg help_msg_list[] = {
22 {
23 "b[reak]", NULL, "Set breakpoint",
24 "Usage: break [file:]line\n"
25 " break [class:]method\n"
26 "\n"
27 "Set breakpoint at specified line or method.\n"
28 "If \'[file:]line\' is specified, break at start of code for that line (in a file).\n"
29 "If \'[class:]method\' is specified, break at start of code for that method (of the class).\n"
30 },
31 {
32 "c[ontinue]", NULL, "Continue program being debugged",
33 "Usage: continue [N]\n"
34 "\n"
35 "Continue program stopped by a breakpoint.\n"
36 "If N, which is non negative value, is passed,\n"
37 "proceed program until the N-th breakpoint is coming.\n"
38 "If N is not passed, N is assumed 1.\n"
39 },
40 {
41 "d[elete]", NULL, "Delete some breakpoints",
42 "Usage: delete [bpno1 [bpno2 [... [bpnoN]]]]\n"
43 "\n"
44 "Delete some breakpoints.\n"
45 "Arguments are breakpoint numbers with spaces in between.\n"
46 "To delete all breakpoints, give no argument.\n"
47 },
48 {
49 "dis[able]", NULL, "Disable some breakpoints",
50 "Usage: disable [bpno1 [bpno2 [... [bpnoN]]]]\n"
51 "\n"
52 "Disable some breakpoints.\n"
53 "Arguments are breakpoint numbers with spaces in between.\n"
54 "To disable all breakpoints, give no argument.\n"
55 },
56 {
57 "en[able]", NULL, "Enable some breakpoints",
58 "Usage: enable [bpno1 [bpno2 [... [bpnoN]]]]\n"
59 "\n"
60 "Enable some breakpoints.\n"
61 "Arguments are breakpoint numbers with spaces in between.\n"
62 "To enable all breakpoints, give no argument.\n"
63 },
64 {
65 "ev[al]", NULL, "Evaluate expression",
66 "Usage: eval expr\n"
67 "\n"
68 "It evaluates and prints the value of the mruby expression.\n"
69 "This is equivalent to the \'print\' command.\n"
70 },
71 {
72 "h[elp]", NULL, "Print this help",
73 "Usage: help [command]\n"
74 "\n"
75 "With no arguments, help displays a short list of commands.\n"
76 "With a command name as help argument, help displays how to use that command.\n"
77 },
78 {
79 "i[nfo]", "b[reakpoints]", "Status of breakpoints",
80 "Usage: info breakpoints [bpno1 [bpno2 [... [bpnoN]]]]\n"
81 "\n"
82 "Status of specified breakpoints (all user-settable breakpoints if no argument).\n"
83 "Arguments are breakpoint numbers with spaces in between.\n"
84 },
85 {
86 "i[nfo]", "l[ocals]", "Print name of local variables",
87 "Usage: info locals\n"
88 "\n"
89 "Print name of local variables.\n"
90 },
91 {
92 "l[ist]", NULL, "List specified line",
93 "Usage: list\n"
94 " list first[,last]\n"
95 " list filename:first[,last]\n"
96 "\n"
97 "Print lines from a source file.\n"
98 "\n"
99 "With first and last, list prints lines from first to last.\n"
100 "When last is empty, it stands for ten lines away from first.\n"
101 "With filename, list prints lines in the specified source file.\n"
102 },
103 {
104 "p[rint]", NULL, "Print value of expression",
105 "Usage: print expr\n"
106 "\n"
107 "It evaluates and prints the value of the mruby expression.\n"
108 "This is equivalent to the \'eval\' command.\n"
109 },
110 {
111 "q[uit]", NULL, "Exit mrdb",
112 "Usage: quit\n"
113 "\n"
114 "Exit mrdb.\n"
115 },
116 {
117 "r[un]", NULL, "Start debugged program",
118 "Usage: run\n"
119 "\n"
120 "Start debugged program.\n"
121 },
122 {
123 "s[tep]", NULL, "Step program until it reaches a different source line",
124 "Usage: step\n"
125 "\n"
126 "Step program until it reaches a different source line.\n"
127 },
128 { NULL, NULL, NULL, NULL }
129 };
130
131 typedef struct listcmd_parser_state {
132 mrb_bool parse_error;
133 mrb_bool has_line_min;
134 mrb_bool has_line_max;
135 char *filename;
136 uint16_t line_min;
137 uint16_t line_max;
138 } listcmd_parser_state;
139
140 static listcmd_parser_state*
141 listcmd_parser_state_new(mrb_state *mrb)
142 {
143 listcmd_parser_state *st = (listcmd_parser_state*)mrb_malloc(mrb, sizeof(listcmd_parser_state));
144 static const listcmd_parser_state st_zero = {0};
145 *st = st_zero;
146 return st;
147 }
148
149 static void
150 listcmd_parser_state_free(mrb_state *mrb, listcmd_parser_state *st)
151 {
152 if (st != NULL) {
153 if (st->filename != NULL) {
154 mrb_free(mrb, st->filename);
155 }
156 mrb_free(mrb, st);
157 }
158 }
159
160 static mrb_bool
161 parse_uint(char **sp, uint16_t *n)
162 {
163 char *p;
164 int i;
165
166 if (*sp == NULL || **sp == '\0') {
167 return FALSE;
168 }
169
170 for (p = *sp; *p != '\0' && ISDIGIT(*p); p++)
171 ;
172
173 if (p != *sp && (i = atoi(*sp)) >= 0) {
174 *n = (uint16_t)i;
175 *sp = p;
176 return TRUE;
177 }
178 return FALSE;
179 }
180
181 static mrb_bool
182 skip_char(char **sp, char c)
183 {
184 if (*sp != NULL && **sp == c) {
185 ++*sp;
186 return TRUE;
187 }
188 return FALSE;
189 }
190
191 static mrb_bool
192 parse_lineno(mrb_state *mrb, char **sp, listcmd_parser_state *st)
193 {
194 if (*sp == NULL || **sp == '\0') {
195 return FALSE;
196 }
197
198 st->has_line_min = FALSE;
199 st->has_line_max = FALSE;
200
201 if (parse_uint(sp, &st->line_min)) {
202 st->has_line_min = TRUE;
203 }
204 else {
205 return FALSE;
206 }
207
208 if (skip_char(sp, ',')) {
209 if (parse_uint(sp, &st->line_max)) {
210 st->has_line_max = TRUE;
211 }
212 else {
213 st->parse_error = TRUE;
214 return FALSE;
215 }
216 }
217 return TRUE;
218 }
219
220 static mrb_bool
221 parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st)
222 {
223 char *p;
224 int len;
225
226 if (st->filename != NULL) {
227 mrb_free(mrb, st->filename);
228 st->filename = NULL;
229 }
230
231 if ((p = strchr(*sp, ':')) != NULL) {
232 len = p - *sp;
233 }
234 else {
235 len = strlen(*sp);
236 }
237
238 if (len > 0 && (st->filename = mrdb_strndup(mrb, *sp, len)) != NULL) {
239 *sp += len;
240 return TRUE;
241 }
242 else {
243 return FALSE;
244 }
245 }
246
247 char*
248 replace_ext(mrb_state *mrb, const char *filename, const char *ext)
249 {
250 size_t len;
251 const char *p;
252 char *s;
253
254 if (filename == NULL) {
255 return NULL;
256 }
257
258 if ((p = strrchr(filename, '.')) != NULL && strchr(p, '/') == NULL) {
259 len = p - filename;
260 }
261 else {
262 len = strlen(filename);
263 }
264
265 s = (char*)mrb_malloc(mrb, len + strlen(ext) + 1);
266 memset(s, '\0', len + strlen(ext) + 1);
267 strncpy(s, filename, len);
268 strcat(s, ext);
269
270 return s;
271 }
272
273 static mrb_bool
274 parse_listcmd_args(mrb_state *mrb, mrdb_state *mrdb, listcmd_parser_state *st)
275 {
276 char *p;
277
278 switch (mrdb->wcnt) {
279 case 2:
280 p = mrdb->words[1];
281
282 /* mrdb->words[1] ::= <lineno> | <filename> ':' <lineno> | <filename> */
283 if (!parse_lineno(mrb, &p, st)) {
284 if (parse_filename(mrb, &p, st)) {
285 if (skip_char(&p, ':')) {
286 if (!parse_lineno(mrb, &p, st)) {
287 st->parse_error = TRUE;
288 }
289 }
290 }
291 else {
292 st->parse_error = TRUE;
293 }
294 }
295 if (*p != '\0') {
296 st->parse_error = TRUE;
297 }
298 break;
299 case 1:
300 case 0:
301 /* do nothing */
302 break;
303 default:
304 st->parse_error = TRUE;
305 printf("too many arguments\n");
306 break;
307 }
308
309 if (!st->parse_error) {
310 if (!st->has_line_min) {
311 st->line_min = (!st->filename && mrdb->dbg->prvline > 0) ? mrdb->dbg->prvline : 1;
312 }
313
314 if (!st->has_line_max) {
315 st->line_max = st->line_min + 9;
316 }
317
318 if (st->filename == NULL) {
319 if (mrdb->dbg->prvfile && strcmp(mrdb->dbg->prvfile, "-")) {
320 st->filename = replace_ext(mrb, mrdb->dbg->prvfile, ".rb");
321 }
322 }
323 }
324
325 if (st->parse_error || st->filename == NULL) {
326 return FALSE;
327 }
328
329 return TRUE;
330 }
331
332 static mrb_bool
333 check_cmd_pattern(const char *pattern, const char *cmd)
334 {
335 const char *lbracket, *rbracket, *p, *q;
336
337 if (pattern == NULL && cmd == NULL) {
338 return TRUE;
339 }
340 if (pattern == NULL || cmd == NULL) {
341 return FALSE;
342 }
343 if ((lbracket = strchr(pattern, '[')) == NULL) {
344 return !strcmp(pattern, cmd);
345 }
346 if ((rbracket = strchr(pattern, ']')) == NULL) {
347 return FALSE;
348 }
349 if (strncmp(pattern, cmd, lbracket - pattern)) {
350 return FALSE;
351 }
352
353 p = lbracket + 1;
354 q = (char*)cmd + (lbracket - pattern);
355
356 for (; p < rbracket && *q != '\0'; p++, q++) {
357 if (*p != *q) {
358 break;
359 }
360 }
361 return *q == '\0';
362 }
363
364 static help_msg*
365 get_help_msg(char *cmd1, char *cmd2)
366 {
367 help_msg *p;
368
369 if (cmd1 == NULL) {
370 return NULL;
371 }
372 for (p = help_msg_list; p->cmd1 != NULL; p++) {
373 if (check_cmd_pattern(p->cmd1, cmd1) && check_cmd_pattern(p->cmd2, cmd2)) {
374 return p;
375 }
376 }
377 return NULL;
378 }
379
380 static mrb_bool
381 show_short_help(void)
382 {
383 help_msg *p;
384
385 printf("Commands\n");
386
387 for (p = help_msg_list; p->cmd1 != NULL; p++) {
388 if (p->cmd2 == NULL) {
389 printf(" %s -- %s\n", p->cmd1, p->short_msg);
390 }
391 else {
392 printf(" %s %s -- %s\n", p->cmd1, p->cmd2, p->short_msg);
393 }
394 }
395 return TRUE;
396 }
397
398 static mrb_bool
399 show_long_help(char *cmd1, char *cmd2)
400 {
401 help_msg *help;
402
403 if ((help = get_help_msg(cmd1, cmd2)) == NULL) {
404 return FALSE;
405 }
406 printf("%s", help->long_msg);
407 return TRUE;
408 }
409
410 dbgcmd_state
411 dbgcmd_list(mrb_state *mrb, mrdb_state *mrdb)
412 {
413 char *filename;
414 listcmd_parser_state *st = listcmd_parser_state_new(mrb);
415
416 if (parse_listcmd_args(mrb, mrdb, st)) {
417 if ((filename = mrb_debug_get_source(mrb, mrdb, mrdb->srcpath, st->filename)) == NULL) {
418 filename = st->filename;
419 }
420 mrb_debug_list(mrb, mrdb->dbg, filename, st->line_min, st->line_max);
421
422 if (filename != NULL && filename != st->filename) {
423 mrb_free(mrb, filename);
424 }
425 listcmd_parser_state_free(mrb, st);
426 }
427
428 return DBGST_PROMPT;
429 }
430
431 dbgcmd_state
432 dbgcmd_help(mrb_state *mrb, mrdb_state *mrdb)
433 {
434 mrb_bool is_valid;
435 int i;
436
437 switch (mrdb->wcnt) {
438 case 0:
439 case 1:
440 is_valid = show_short_help();
441 break;
442 case 2:
443 is_valid = show_long_help(mrdb->words[1], NULL);
444 break;
445 case 3:
446 is_valid = show_long_help(mrdb->words[1], mrdb->words[2]);
447 break;
448 default:
449 is_valid = FALSE;
450 break;
451 }
452
453 if (!is_valid) {
454 printf("Invalid command \"");
455 for (i = 1; i < mrdb->wcnt; i++) {
456 printf("%s%s", i == 1 ? "" : " ", mrdb->words[i]);
457 }
458 printf("\". Try \"help\".\n");
459 }
460
461 return DBGST_PROMPT;
462 }
463
464 dbgcmd_state
465 dbgcmd_quit(mrb_state *mrb, mrdb_state *mrdb)
466 {
467 switch (mrdb->dbg->xm) {
468 case DBG_RUN:
469 case DBG_STEP:
470 case DBG_NEXT:
471 while (1) {
472 char c;
473 int buf;
474
475 printf("The program is running. Exit anyway? (y or n) ");
476 fflush(stdout);
477
478 if ((buf = getchar()) == EOF) {
479 mrdb->dbg->xm = DBG_QUIT;
480 break;
481 }
482 c = buf;
483 while (buf != '\n' && (buf = getchar()) != EOF)
484 ;
485
486 if (c == 'y' || c == 'Y') {
487 mrdb->dbg->xm = DBG_QUIT;
488 break;
489 }
490 else if (c == 'n' || c == 'N') {
491 break;
492 }
493 else {
494 printf("Please answer y or n.\n");
495 }
496 }
497 break;
498 default:
499 mrdb->dbg->xm = DBG_QUIT;
500 break;
501 }
502
503 if (mrdb->dbg->xm == DBG_QUIT) {
504 struct RClass *exc;
505 exc = mrb_define_class(mrb, "DebuggerExit", E_EXCEPTION);
506 mrb_raise(mrb, exc, "Exit mrdb");
507 }
508 return DBGST_PROMPT;
509 }