[270] | 1 | #include <string.h>
|
---|
[331] | 2 | #include <mruby.h>
|
---|
| 3 | #include <mruby/irep.h>
|
---|
| 4 | #include <mruby/debug.h>
|
---|
[270] | 5 |
|
---|
[439] | 6 | static mrb_irep_debug_info_file*
|
---|
[270] | 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;
|
---|
[331] | 22 | }
|
---|
| 23 | else { count = step; }
|
---|
[270] | 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 | static mrb_debug_line_type
|
---|
| 39 | select_line_type(const uint16_t *lines, size_t lines_len)
|
---|
| 40 | {
|
---|
| 41 | size_t line_count = 0;
|
---|
| 42 | int prev_line = -1;
|
---|
| 43 | size_t i;
|
---|
| 44 | for (i = 0; i < lines_len; ++i) {
|
---|
| 45 | if (lines[i] != prev_line) {
|
---|
| 46 | ++line_count;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | return (sizeof(uint16_t) * lines_len) <= (sizeof(mrb_irep_debug_info_line) * line_count)
|
---|
| 50 | ? mrb_debug_line_ary : mrb_debug_line_flat_map;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | MRB_API char const*
|
---|
[439] | 54 | mrb_debug_get_filename(mrb_state *mrb, mrb_irep *irep, ptrdiff_t pc)
|
---|
[270] | 55 | {
|
---|
[439] | 56 | if (irep && pc >= 0 && pc < irep->ilen) {
|
---|
[270] | 57 | mrb_irep_debug_info_file* f = NULL;
|
---|
[439] | 58 | if (!irep->debug_info) return NULL;
|
---|
| 59 | else if ((f = get_file(irep->debug_info, (uint32_t)pc))) {
|
---|
| 60 | return mrb_sym_name_len(mrb, f->filename_sym, NULL);
|
---|
[270] | 61 | }
|
---|
| 62 | }
|
---|
| 63 | return NULL;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | MRB_API int32_t
|
---|
[439] | 67 | mrb_debug_get_line(mrb_state *mrb, mrb_irep *irep, ptrdiff_t pc)
|
---|
[270] | 68 | {
|
---|
[439] | 69 | if (irep && pc >= 0 && pc < irep->ilen) {
|
---|
[270] | 70 | mrb_irep_debug_info_file* f = NULL;
|
---|
| 71 | if (!irep->debug_info) {
|
---|
[439] | 72 | return -1;
|
---|
[270] | 73 | }
|
---|
[439] | 74 | else if ((f = get_file(irep->debug_info, (uint32_t)pc))) {
|
---|
[270] | 75 | switch (f->line_type) {
|
---|
| 76 | case mrb_debug_line_ary:
|
---|
| 77 | mrb_assert(f->start_pos <= pc && pc < (f->start_pos + f->line_entry_count));
|
---|
| 78 | return f->lines.ary[pc - f->start_pos];
|
---|
| 79 |
|
---|
| 80 | case mrb_debug_line_flat_map: {
|
---|
| 81 | /* get upper bound */
|
---|
| 82 | mrb_irep_debug_info_line *ret = f->lines.flat_map;
|
---|
| 83 | uint32_t count = f->line_entry_count;
|
---|
| 84 | while (count > 0) {
|
---|
| 85 | int32_t step = count / 2;
|
---|
| 86 | mrb_irep_debug_info_line *it = ret + step;
|
---|
| 87 | if (!(pc < it->start_pos)) {
|
---|
| 88 | ret = it + 1;
|
---|
| 89 | count -= step + 1;
|
---|
[331] | 90 | }
|
---|
| 91 | else { count = step; }
|
---|
[270] | 92 | }
|
---|
| 93 |
|
---|
| 94 | --ret;
|
---|
| 95 |
|
---|
| 96 | /* check line entry pointer range */
|
---|
| 97 | mrb_assert(f->lines.flat_map <= ret && ret < (f->lines.flat_map + f->line_entry_count));
|
---|
| 98 | /* check pc range */
|
---|
| 99 | mrb_assert(ret->start_pos <= pc &&
|
---|
| 100 | pc < (((uint32_t)(ret + 1 - f->lines.flat_map) < f->line_entry_count)
|
---|
| 101 | ? (ret+1)->start_pos : irep->debug_info->pc_count));
|
---|
| 102 |
|
---|
| 103 | return ret->line;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | return -1;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[439] | 111 | MRB_API mrb_irep_debug_info*
|
---|
[270] | 112 | mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep)
|
---|
| 113 | {
|
---|
| 114 | static const mrb_irep_debug_info initial = { 0, 0, NULL };
|
---|
| 115 | mrb_irep_debug_info *ret;
|
---|
| 116 |
|
---|
| 117 | mrb_assert(!irep->debug_info);
|
---|
| 118 | ret = (mrb_irep_debug_info *)mrb_malloc(mrb, sizeof(*ret));
|
---|
| 119 | *ret = initial;
|
---|
| 120 | irep->debug_info = ret;
|
---|
| 121 | return ret;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[439] | 124 | MRB_API mrb_irep_debug_info_file*
|
---|
| 125 | mrb_debug_info_append_file(mrb_state *mrb, mrb_irep_debug_info *d,
|
---|
| 126 | const char *filename, uint16_t *lines,
|
---|
[270] | 127 | uint32_t start_pos, uint32_t end_pos)
|
---|
| 128 | {
|
---|
[439] | 129 | mrb_irep_debug_info_file *f;
|
---|
[270] | 130 | uint32_t file_pc_count;
|
---|
| 131 | size_t fn_len;
|
---|
| 132 | uint32_t i;
|
---|
| 133 |
|
---|
[439] | 134 | if (!d) return NULL;
|
---|
| 135 | if (start_pos == end_pos) return NULL;
|
---|
[270] | 136 |
|
---|
[439] | 137 | mrb_assert(filename);
|
---|
| 138 | mrb_assert(lines);
|
---|
[270] | 139 |
|
---|
[439] | 140 | if (d->flen > 0) {
|
---|
| 141 | const char *fn = mrb_sym_name_len(mrb, d->files[d->flen - 1]->filename_sym, NULL);
|
---|
| 142 | if (strcmp(filename, fn) == 0)
|
---|
| 143 | return NULL;
|
---|
[270] | 144 | }
|
---|
| 145 |
|
---|
[439] | 146 | f = (mrb_irep_debug_info_file*)mrb_malloc(mrb, sizeof(*f));
|
---|
| 147 | d->files = (mrb_irep_debug_info_file**)(
|
---|
| 148 | d->files
|
---|
| 149 | ? mrb_realloc(mrb, d->files, sizeof(mrb_irep_debug_info_file*) * (d->flen + 1))
|
---|
[270] | 150 | : mrb_malloc(mrb, sizeof(mrb_irep_debug_info_file*)));
|
---|
[439] | 151 | d->files[d->flen++] = f;
|
---|
[270] | 152 |
|
---|
| 153 | file_pc_count = end_pos - start_pos;
|
---|
| 154 |
|
---|
[439] | 155 | f->start_pos = start_pos;
|
---|
| 156 | d->pc_count = end_pos;
|
---|
[270] | 157 |
|
---|
[439] | 158 | fn_len = strlen(filename);
|
---|
| 159 | f->filename_sym = mrb_intern(mrb, filename, fn_len);
|
---|
[270] | 160 |
|
---|
[439] | 161 | f->line_type = select_line_type(lines + start_pos, end_pos - start_pos);
|
---|
| 162 | f->lines.ptr = NULL;
|
---|
[270] | 163 |
|
---|
[439] | 164 | switch (f->line_type) {
|
---|
[270] | 165 | case mrb_debug_line_ary:
|
---|
[439] | 166 | f->line_entry_count = file_pc_count;
|
---|
| 167 | f->lines.ary = (uint16_t*)mrb_malloc(mrb, sizeof(uint16_t) * file_pc_count);
|
---|
[270] | 168 | for (i = 0; i < file_pc_count; ++i) {
|
---|
[439] | 169 | f->lines.ary[i] = lines[start_pos + i];
|
---|
[270] | 170 | }
|
---|
| 171 | break;
|
---|
| 172 |
|
---|
| 173 | case mrb_debug_line_flat_map: {
|
---|
| 174 | uint16_t prev_line = 0;
|
---|
| 175 | mrb_irep_debug_info_line m;
|
---|
[439] | 176 | f->lines.flat_map = (mrb_irep_debug_info_line*)mrb_malloc(mrb, sizeof(mrb_irep_debug_info_line) * 1);
|
---|
| 177 | f->line_entry_count = 0;
|
---|
[270] | 178 | for (i = 0; i < file_pc_count; ++i) {
|
---|
[439] | 179 | if (lines[start_pos + i] == prev_line) { continue; }
|
---|
[270] | 180 |
|
---|
[439] | 181 | f->lines.flat_map = (mrb_irep_debug_info_line*)mrb_realloc(
|
---|
| 182 | mrb, f->lines.flat_map,
|
---|
| 183 | sizeof(mrb_irep_debug_info_line) * (f->line_entry_count + 1));
|
---|
[270] | 184 | m.start_pos = start_pos + i;
|
---|
[439] | 185 | m.line = lines[start_pos + i];
|
---|
| 186 | f->lines.flat_map[f->line_entry_count] = m;
|
---|
[270] | 187 |
|
---|
| 188 | /* update */
|
---|
[439] | 189 | ++f->line_entry_count;
|
---|
| 190 | prev_line = lines[start_pos + i];
|
---|
[270] | 191 | }
|
---|
| 192 | } break;
|
---|
| 193 |
|
---|
| 194 | default: mrb_assert(0); break;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[439] | 197 | return f;
|
---|
[270] | 198 | }
|
---|
| 199 |
|
---|
| 200 | MRB_API void
|
---|
| 201 | mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d)
|
---|
| 202 | {
|
---|
| 203 | uint32_t i;
|
---|
| 204 |
|
---|
| 205 | if (!d) { return; }
|
---|
| 206 |
|
---|
| 207 | for (i = 0; i < d->flen; ++i) {
|
---|
| 208 | mrb_assert(d->files[i]);
|
---|
| 209 | mrb_free(mrb, d->files[i]->lines.ptr);
|
---|
| 210 | mrb_free(mrb, d->files[i]);
|
---|
| 211 | }
|
---|
| 212 | mrb_free(mrb, d->files);
|
---|
| 213 | mrb_free(mrb, d);
|
---|
| 214 | }
|
---|