3 #include <mruby/irep.h>
4 #include <mruby/debug.h>
6 static mrb_irep_debug_info_file*
7 get_file(mrb_irep_debug_info *info, uint32_t pc)
9 mrb_irep_debug_info_file **ret;
12 if (pc >= info->pc_count) { return NULL; }
17 int32_t step = count / 2;
18 mrb_irep_debug_info_file **it = ret + step;
19 if (!(pc < (*it)->start_pos)) {
23 else { count = step; }
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));
38 static mrb_debug_line_type
39 select_line_type(const uint16_t *lines, size_t lines_len)
41 size_t line_count = 0;
44 for (i = 0; i < lines_len; ++i) {
45 if (lines[i] != prev_line) {
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;
54 mrb_debug_get_filename(mrb_state *mrb, mrb_irep *irep, ptrdiff_t pc)
56 if (irep && pc >= 0 && pc < irep->ilen) {
57 mrb_irep_debug_info_file* f = NULL;
58 if (!irep->debug_info) return NULL;
59 else if ((f = get_file(irep->debug_info, (uint32_t)pc))) {
60 return mrb_sym2name_len(mrb, f->filename_sym, NULL);
67 mrb_debug_get_line(mrb_state *mrb, mrb_irep *irep, ptrdiff_t pc)
69 if (irep && pc >= 0 && pc < irep->ilen) {
70 mrb_irep_debug_info_file* f = NULL;
71 if (!irep->debug_info) {
74 else if ((f = get_file(irep->debug_info, (uint32_t)pc))) {
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];
80 case mrb_debug_line_flat_map: {
82 mrb_irep_debug_info_line *ret = f->lines.flat_map;
83 uint32_t count = f->line_entry_count;
85 int32_t step = count / 2;
86 mrb_irep_debug_info_line *it = ret + step;
87 if (!(pc < it->start_pos)) {
91 else { count = step; }
96 /* check line entry pointer range */
97 mrb_assert(f->lines.flat_map <= ret && ret < (f->lines.flat_map + f->line_entry_count));
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));
111 MRB_API mrb_irep_debug_info*
112 mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep)
114 static const mrb_irep_debug_info initial = { 0, 0, NULL };
115 mrb_irep_debug_info *ret;
117 mrb_assert(!irep->debug_info);
118 ret = (mrb_irep_debug_info *)mrb_malloc(mrb, sizeof(*ret));
120 irep->debug_info = ret;
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,
127 uint32_t start_pos, uint32_t end_pos)
129 mrb_irep_debug_info_file *f;
130 uint32_t file_pc_count;
135 if (start_pos == end_pos) return NULL;
137 mrb_assert(filename);
141 const char *fn = mrb_sym2name_len(mrb, d->files[d->flen - 1]->filename_sym, NULL);
142 if (strcmp(filename, fn) == 0)
146 f = (mrb_irep_debug_info_file*)mrb_malloc(mrb, sizeof(*f));
147 d->files = (mrb_irep_debug_info_file**)(
149 ? mrb_realloc(mrb, d->files, sizeof(mrb_irep_debug_info_file*) * (d->flen + 1))
150 : mrb_malloc(mrb, sizeof(mrb_irep_debug_info_file*)));
151 d->files[d->flen++] = f;
153 file_pc_count = end_pos - start_pos;
155 f->start_pos = start_pos;
156 d->pc_count = end_pos;
158 fn_len = strlen(filename);
159 f->filename_sym = mrb_intern(mrb, filename, fn_len);
161 f->line_type = select_line_type(lines + start_pos, end_pos - start_pos);
164 switch (f->line_type) {
165 case mrb_debug_line_ary:
166 f->line_entry_count = file_pc_count;
167 f->lines.ary = (uint16_t*)mrb_malloc(mrb, sizeof(uint16_t) * file_pc_count);
168 for (i = 0; i < file_pc_count; ++i) {
169 f->lines.ary[i] = lines[start_pos + i];
173 case mrb_debug_line_flat_map: {
174 uint16_t prev_line = 0;
175 mrb_irep_debug_info_line m;
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;
178 for (i = 0; i < file_pc_count; ++i) {
179 if (lines[start_pos + i] == prev_line) { continue; }
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));
184 m.start_pos = start_pos + i;
185 m.line = lines[start_pos + i];
186 f->lines.flat_map[f->line_entry_count] = m;
189 ++f->line_entry_count;
190 prev_line = lines[start_pos + i];
194 default: mrb_assert(0); break;
201 mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d)
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]);
212 mrb_free(mrb, d->files);