diff options
Diffstat (limited to 'mrbgems/mruby-bin-debugger/tools/mrdb')
| -rw-r--r-- | mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c | 50 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c | 16 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-debugger/tools/mrdb/apistring.c | 34 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-debugger/tools/mrdb/apistring.h | 14 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c | 9 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.c | 10 |
6 files changed, 95 insertions, 38 deletions
diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c b/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c index 4d139aa76..55c6cd125 100644 --- a/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c @@ -14,30 +14,49 @@ #include <mruby/variable.h> #include "mrdberror.h" #include "apibreak.h" +#include "apistring.h" #define MAX_BREAKPOINTNO (MAX_BREAKPOINT * 1024) #define MRB_DEBUG_BP_FILE_OK (0x0001) #define MRB_DEBUG_BP_LINENO_OK (0x0002) +uint32_t mrb_packed_int_decode(uint8_t *p, uint8_t **newpos); + static uint16_t check_lineno(mrb_irep_debug_info_file *info_file, uint16_t lineno) { uint32_t count = info_file->line_entry_count; uint16_t l_idx; - if (info_file->line_type == mrb_debug_line_ary) { + switch (info_file->line_type) { + case mrb_debug_line_ary: for (l_idx = 0; l_idx < count; ++l_idx) { if (lineno == info_file->lines.ary[l_idx]) { return lineno; } } - } - else { + break; + + case mrb_debug_line_flat_map: for (l_idx = 0; l_idx < count; ++l_idx) { if (lineno == info_file->lines.flat_map[l_idx].line) { return lineno; } } + break; + + case mrb_debug_line_packed_map: + { + uint8_t *p = info_file->lines.packed_map; + uint8_t *pend = p + count; + uint32_t line = 0; + while (p < pend) { + mrb_packed_int_decode(p, &p); + line += mrb_packed_int_decode(p, &p); + if (line == lineno) return lineno; + } + } + break; } return 0; @@ -173,7 +192,6 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil int32_t index; char* set_file; uint16_t result; - size_t len; if ((mrb == NULL)||(dbg == NULL)||(file == NULL)) { return MRB_DEBUG_INVALID_ARGUMENT; @@ -187,7 +205,7 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil return MRB_DEBUG_BREAK_NO_OVER; } - /* file and lineno check (line type mrb_debug_line_ary only.) */ + /* file and lineno check. */ result = check_file_lineno(mrb, dbg->root_irep, file, lineno); if (result == 0) { return MRB_DEBUG_BREAK_INVALID_FILE; @@ -196,8 +214,7 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil return MRB_DEBUG_BREAK_INVALID_LINENO; } - len = strlen(file) + 1; - set_file = (char*)mrb_malloc(mrb, len); + set_file = mrdb_strdup(mrb, file); index = dbg->bpnum; dbg->bp[index].bpno = dbg->next_bpno; @@ -207,8 +224,6 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil dbg->bp[index].point.linepoint.lineno = lineno; dbg->bpnum++; - strncpy(set_file, file, len); - dbg->bp[index].point.linepoint.file = set_file; return dbg->bp[index].bpno; @@ -220,7 +235,6 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c int32_t index; char* set_class; char* set_method; - size_t len; if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) { return MRB_DEBUG_INVALID_ARGUMENT; @@ -235,18 +249,16 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c } if (class_name != NULL) { - len = strlen(class_name) + 1; - set_class = (char*)mrb_malloc(mrb, len); - strncpy(set_class, class_name, len); + set_class = mrdb_strdup(mrb, class_name); } else { set_class = NULL; } - len = strlen(method_name) + 1; - set_method = (char*)mrb_malloc(mrb, len); - - strncpy(set_method, method_name, len); + set_method = mrdb_strdup(mrb, method_name); + if (set_method == NULL) { + mrb_free(mrb, set_class); + } index = dbg->bpnum; dbg->bp[index].bpno = dbg->next_bpno; @@ -332,10 +344,10 @@ mrb_debug_delete_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno) for(i = index ; i < dbg->bpnum; i++) { if ((i + 1) == dbg->bpnum) { - memset(&dbg->bp[i], 0, sizeof(mrb_debug_breakpoint)); + dbg->bp[i] = (mrb_debug_breakpoint){0}; } else { - memcpy(&dbg->bp[i], &dbg->bp[i + 1], sizeof(mrb_debug_breakpoint)); + dbg->bp[i] = dbg->bp[i + 1]; } } diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c b/mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c index 66ddfa783..27db02b48 100644 --- a/mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c @@ -3,12 +3,12 @@ */ #include <ctype.h> -#include <stdlib.h> #include <string.h> #include "mrdb.h" #include "mrdberror.h" #include "apilist.h" +#include "apistring.h" #include <mruby/compile.h> #include <mruby/irep.h> #include <mruby/debug.h> @@ -65,7 +65,6 @@ dirname(mrb_state *mrb, const char *path) { size_t len; const char *p; - char *dir; if (path == NULL) { return NULL; @@ -74,11 +73,7 @@ dirname(mrb_state *mrb, const char *path) p = strrchr(path, '/'); len = p != NULL ? (size_t)(p - path) : strlen(path); - dir = (char*)mrb_malloc(mrb, len + 1); - strncpy(dir, path, len); - dir[len] = '\0'; - - return dir; + return mrdb_strndup(mrb, path, len); } static source_file* @@ -97,8 +92,11 @@ source_file_new(mrb_state *mrb, mrb_debug_context *dbg, char *filename) } file->lineno = 1; - file->path = (char*)mrb_malloc(mrb, strlen(filename) + 1); - strcpy(file->path, filename); + file->path = mrdb_strdup(mrb, filename); + if (file->path == NULL) { + source_file_free(mrb, file); + return NULL; + } return file; } diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.c b/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.c new file mode 100644 index 000000000..a7b320ade --- /dev/null +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.c @@ -0,0 +1,34 @@ +/* +** apistring.c +** +*/ + +#include <string.h> +#include "apistring.h" + +static size_t +mrb_debug_strnlen(const char *s, size_t maxlen) +{ + const char *p = (const char*)memchr(s, '\0', maxlen); + return p != NULL ? (size_t)(p - s) : maxlen; +} + +char* +mrdb_strndup(mrb_state *mrb, const char *s, size_t size) +{ + size_t l = mrb_debug_strnlen(s, size); + char *d = (char*)mrb_malloc_simple(mrb, l + 1); + if (d != NULL) { + memcpy(d, s, l); + d[l] = '\0'; + } + return d; +} + +char* +mrdb_strdup(mrb_state *mrb, const char *s) +{ + size_t z = strlen(s) + 1; + char *d = (char*)mrb_malloc_simple(mrb, z); + return d != NULL ? (char*)memcpy(d, s, z) : NULL; +} diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.h b/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.h new file mode 100644 index 000000000..33737e7fd --- /dev/null +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.h @@ -0,0 +1,14 @@ +/* + * apistring.h + */ + +#ifndef APISTRING_H_ +#define APISTRING_H_ + +#include "mruby.h" + +/* both functions return a null pointer on failure */ +char *mrdb_strndup(mrb_state *mrb, const char *s, size_t size); +char *mrdb_strdup(mrb_state *mrb, const char *s); + +#endif /* APISTRING_H_ */ diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c b/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c index a05ff9415..0714f3f21 100644 --- a/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c @@ -8,6 +8,7 @@ #include <string.h> #include "apilist.h" +#include "apistring.h" #include <mruby/compile.h> typedef struct help_msg { @@ -140,7 +141,8 @@ static listcmd_parser_state* listcmd_parser_state_new(mrb_state *mrb) { listcmd_parser_state *st = (listcmd_parser_state*)mrb_malloc(mrb, sizeof(listcmd_parser_state)); - memset(st, 0, sizeof(listcmd_parser_state)); + static const listcmd_parser_state st_zero = {0}; + *st = st_zero; return st; } @@ -232,10 +234,7 @@ parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st) len = strlen(*sp); } - if (len > 0) { - st->filename = (char*)mrb_malloc(mrb, len + 1); - strncpy(st->filename, *sp, len); - st->filename[len] = '\0'; + if (len > 0 && (st->filename = mrdb_strndup(mrb, *sp, len)) != NULL) { *sp += len; return TRUE; } diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.c b/mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.c index 009cd955c..ee2ae0aca 100644 --- a/mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.c +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.c @@ -186,9 +186,9 @@ static mrb_debug_context* mrb_debug_context_new(mrb_state *mrb) { mrb_debug_context *dbg = (mrb_debug_context*)mrb_malloc(mrb, sizeof(mrb_debug_context)); + static const mrb_debug_context dbg_zero = {0}; - memset(dbg, 0, sizeof(mrb_debug_context)); - + *dbg = dbg_zero; dbg->xm = DBG_INIT; dbg->xphase = DBG_PHASE_BEFORE_RUN; dbg->next_bpno = 1; @@ -225,9 +225,9 @@ static mrdb_state* mrdb_state_new(mrb_state *mrb) { mrdb_state *mrdb = (mrdb_state*)mrb_malloc(mrb, sizeof(mrdb_state)); + static const mrdb_state mrdb_zero = {0}; - memset(mrdb, 0, sizeof(mrdb_state)); - + *mrdb = mrdb_zero; mrdb->dbg = mrb_debug_context_get(mrb); mrdb->command = (char*)mrb_malloc(mrb, MAX_COMMAND_LINE+1); mrdb->print_no = 1; @@ -574,7 +574,7 @@ mrb_code_fetch_hook(mrb_state *mrb, const mrb_irep *irep, const mrb_code *pc, mr switch (dbg->xm) { case DBG_STEP: - if (!file || (dbg->prvfile == file && dbg->prvline == line)) { + if (*pc != OP_JMP && (!file || (dbg->prvfile == file && dbg->prvline == line))) { return; } dbg->method_bpno = 0; |
