| 1 |
#include <string.h> |
| 2 |
#include <mruby.h> |
| 3 |
#include <mruby/irep.h> |
| 4 |
#include <mruby/debug.h> |
| 5 |
|
| 6 |
static mrb_irep_debug_info_file* |
| 7 |
get_file(mrb_irep_debug_info *info, uint32_t pc) |
| 8 |
{ |
| 9 |
mrb_irep_debug_info_file **ret; |
| 10 |
int32_t count; |
| 11 |
|
| 12 |
if (pc >= info->pc_count) { return NULL; } |
| 13 |
/* get upper bound */ |
| 14 |
ret = info->files; |
| 15 |
count = info->flen; |
| 16 |
while (count > 0) { |
| 17 |
int32_t step = count / 2; |
| 18 |
mrb_irep_debug_info_file **it = ret + step; |
| 19 |
if (!(pc < (*it)->start_pos)) { |
| 20 |
ret = it + 1; |
| 21 |
count -= step + 1; |
| 22 |
} |
| 23 |
else { count = step; } |
| 24 |
} |
| 25 |
|
| 26 |
--ret; |
| 27 |
|
| 28 |
/* check returning file exists inside debug info */ |
| 29 |
mrb_assert(info->files <= ret && ret < (info->files + info->flen)); |
| 30 |
/* check pc is within the range of returning file */ |
| 31 |
mrb_assert((*ret)->start_pos <= pc && |
| 32 |
pc < (((ret + 1 - info->files) < info->flen) |
| 33 |
? (*(ret+1))->start_pos : info->pc_count)); |
| 34 |
|
| 35 |
return *ret; |
| 36 |
} |
| 37 |
|
| 38 |
size_t |
| 39 |
mrb_packed_int_len(uint32_t num) |
| 40 |
{ |
| 41 |
size_t llen = 0; |
| 42 |
|
| 43 |
do { |
| 44 |
llen++; |
| 45 |
} while (num >>= 7); |
| 46 |
return llen; |
| 47 |
} |
| 48 |
|
| 49 |
size_t |
| 50 |
mrb_packed_int_encode(uint32_t num, uint8_t *p) |
| 51 |
{ |
| 52 |
size_t llen = 0; |
| 53 |
|
| 54 |
do { |
| 55 |
uint8_t byte = num & 0x7f; |
| 56 |
num >>= 7; |
| 57 |
if (num != 0) byte |= 0x80; |
| 58 |
*p++ = byte; |
| 59 |
llen++; |
| 60 |
} while (num != 0); |
| 61 |
|
| 62 |
return llen; |
| 63 |
} |
| 64 |
|
| 65 |
uint32_t |
| 66 |
mrb_packed_int_decode(const uint8_t *p, const uint8_t **newpos) |
| 67 |
{ |
| 68 |
size_t i = 0, shift = 0; |
| 69 |
uint32_t n = 0; |
| 70 |
|
| 71 |
do { |
| 72 |
n |= ((uint32_t)(p[i] & 0x7f)) << shift; |
| 73 |
i++; |
| 74 |
shift += 7; |
| 75 |
} while (shift < sizeof(uint32_t) * 8 && (p[i - 1] & 0x80)); |
| 76 |
if (newpos) *newpos = p + i; |
| 77 |
return n; |
| 78 |
} |
| 79 |
|
| 80 |
static char const* |
| 81 |
debug_get_filename(mrb_state *mrb, mrb_irep_debug_info_file* f) |
| 82 |
{ |
| 83 |
if (f == NULL) return NULL; |
| 84 |
return mrb_sym_name_len(mrb, f->filename_sym, NULL); |
| 85 |
} |
| 86 |
|
| 87 |
static int32_t |
| 88 |
debug_get_line(mrb_state *mrb, mrb_irep_debug_info_file* f, uint32_t pc) |
| 89 |
{ |
| 90 |
if (f == NULL) return -1; |
| 91 |
switch (f->line_type) { |
| 92 |
case mrb_debug_line_ary: |
| 93 |
case mrb_debug_line_flat_map: |
| 94 |
default: |
| 95 |
break; |
| 96 |
|
| 97 |
case mrb_debug_line_packed_map: |
| 98 |
{ |
| 99 |
const uint8_t *p = f->lines.packed_map; |
| 100 |
const uint8_t *pend = p + f->line_entry_count; |
| 101 |
uint32_t pos = 0, line = 0, line_diff; |
| 102 |
while (p < pend) { |
| 103 |
pos += mrb_packed_int_decode(p, &p); |
| 104 |
line_diff = mrb_packed_int_decode(p, &p); |
| 105 |
if (pc < pos) break; |
| 106 |
line += line_diff; |
| 107 |
} |
| 108 |
return line; |
| 109 |
} |
| 110 |
} |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
MRB_API char const* |
| 115 |
mrb_debug_get_filename(mrb_state *mrb, const mrb_irep *irep, uint32_t pc) |
| 116 |
{ |
| 117 |
if (irep && pc < irep->ilen) { |
| 118 |
if (!irep->debug_info) return NULL; |
| 119 |
return debug_get_filename(mrb, get_file(irep->debug_info, pc)); |
| 120 |
} |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
MRB_API int32_t |
| 125 |
mrb_debug_get_line(mrb_state *mrb, const mrb_irep *irep, uint32_t pc) |
| 126 |
{ |
| 127 |
if (irep && pc < irep->ilen) { |
| 128 |
if (!irep->debug_info) return -1; |
| 129 |
return debug_get_line(mrb, get_file(irep->debug_info, pc), pc); |
| 130 |
} |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
MRB_API mrb_bool |
| 135 |
mrb_debug_get_position(mrb_state *mrb, const mrb_irep *irep, uint32_t pc, int32_t *lp, const char **fp) |
| 136 |
{ |
| 137 |
if (irep && pc < irep->ilen && irep->debug_info) { |
| 138 |
mrb_irep_debug_info_file *f = get_file(irep->debug_info, pc); |
| 139 |
*lp = debug_get_line(mrb, f, pc); |
| 140 |
if (*lp > 0) { |
| 141 |
*fp = debug_get_filename(mrb, f); |
| 142 |
if (*fp) return TRUE; |
| 143 |
} |
| 144 |
} |
| 145 |
*lp = -1; *fp = NULL; |
| 146 |
return FALSE; |
| 147 |
} |
| 148 |
|
| 149 |
MRB_API mrb_irep_debug_info* |
| 150 |
mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep) |
| 151 |
{ |
| 152 |
static const mrb_irep_debug_info initial = { 0, 0, NULL }; |
| 153 |
mrb_irep_debug_info *ret; |
| 154 |
|
| 155 |
mrb_assert(!irep->debug_info); |
| 156 |
ret = (mrb_irep_debug_info*)mrb_malloc(mrb, sizeof(*ret)); |
| 157 |
*ret = initial; |
| 158 |
irep->debug_info = ret; |
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
MRB_API mrb_irep_debug_info_file* |
| 163 |
mrb_debug_info_append_file(mrb_state *mrb, mrb_irep_debug_info *d, |
| 164 |
const char *filename, uint16_t *lines, |
| 165 |
uint32_t start_pos, uint32_t end_pos) |
| 166 |
{ |
| 167 |
mrb_irep_debug_info_file *f; |
| 168 |
uint32_t file_pc_count; |
| 169 |
size_t fn_len; |
| 170 |
uint32_t i; |
| 171 |
|
| 172 |
if (!d) return NULL; |
| 173 |
if (start_pos == end_pos) return NULL; |
| 174 |
|
| 175 |
mrb_assert(filename); |
| 176 |
mrb_assert(lines); |
| 177 |
|
| 178 |
if (d->flen > 0) { |
| 179 |
const char *fn = mrb_sym_name_len(mrb, d->files[d->flen - 1]->filename_sym, NULL); |
| 180 |
if (strcmp(filename, fn) == 0) |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
f = (mrb_irep_debug_info_file*)mrb_malloc(mrb, sizeof(*f)); |
| 185 |
d->files = (mrb_irep_debug_info_file**)mrb_realloc(mrb, d->files, sizeof(mrb_irep_debug_info_file*) * (d->flen + 1)); |
| 186 |
d->files[d->flen++] = f; |
| 187 |
|
| 188 |
file_pc_count = end_pos - start_pos; |
| 189 |
|
| 190 |
f->start_pos = start_pos; |
| 191 |
d->pc_count = end_pos; |
| 192 |
|
| 193 |
fn_len = strlen(filename); |
| 194 |
f->filename_sym = mrb_intern(mrb, filename, fn_len); |
| 195 |
f->line_type = mrb_debug_line_packed_map; |
| 196 |
f->lines.ptr = NULL; |
| 197 |
|
| 198 |
uint16_t prev_line = 0; |
| 199 |
uint32_t prev_pc = 0; |
| 200 |
size_t packed_size = 0; |
| 201 |
uint8_t *p; |
| 202 |
|
| 203 |
for (i = 0; i < file_pc_count; i++) { |
| 204 |
if (lines[start_pos + i] == prev_line) continue; |
| 205 |
packed_size += mrb_packed_int_len(start_pos+i-prev_pc); |
| 206 |
prev_pc = start_pos+i; |
| 207 |
packed_size += mrb_packed_int_len(lines[start_pos+i]-prev_line); |
| 208 |
prev_line = lines[start_pos + i]; |
| 209 |
} |
| 210 |
f->lines.packed_map = p = (uint8_t*)mrb_malloc(mrb, packed_size); |
| 211 |
prev_line = 0; prev_pc = 0; |
| 212 |
for (i = 0; i < file_pc_count; i++) { |
| 213 |
if (lines[start_pos + i] == prev_line) continue; |
| 214 |
p += mrb_packed_int_encode(start_pos+i-prev_pc, p); |
| 215 |
prev_pc = start_pos + i; |
| 216 |
p += mrb_packed_int_encode(lines[start_pos + i]-prev_line, p); |
| 217 |
prev_line = lines[start_pos + i]; |
| 218 |
} |
| 219 |
f->line_entry_count = (uint32_t)packed_size; |
| 220 |
|
| 221 |
return f; |
| 222 |
} |
| 223 |
|
| 224 |
MRB_API void |
| 225 |
mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d) |
| 226 |
{ |
| 227 |
uint32_t i; |
| 228 |
|
| 229 |
if (!d) { return; } |
| 230 |
|
| 231 |
if (d->files) { |
| 232 |
for (i = 0; i < d->flen; i++) { |
| 233 |
if (d->files[i]) { |
| 234 |
mrb_free(mrb, d->files[i]->lines.ptr); |
| 235 |
mrb_free(mrb, d->files[i]); |
| 236 |
} |
| 237 |
} |
| 238 |
mrb_free(mrb, d->files); |
| 239 |
} |
| 240 |
mrb_free(mrb, d); |
| 241 |
} |