source: EcnlProtoTool/trunk/mruby-1.3.0/src/debug.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 6.0 KB
Line 
1#include <string.h>
2#include <mruby.h>
3#include <mruby/irep.h>
4#include <mruby/debug.h>
5
6static mrb_irep_debug_info_file *
7get_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
38static mrb_debug_line_type
39select_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
53MRB_API char const*
54mrb_debug_get_filename(mrb_irep *irep, uint32_t pc)
55{
56 if (irep && pc < irep->ilen) {
57 mrb_irep_debug_info_file* f = NULL;
58 if (!irep->debug_info) { return irep->filename; }
59 else if ((f = get_file(irep->debug_info, pc))) {
60 return f->filename;
61 }
62 }
63 return NULL;
64}
65
66MRB_API int32_t
67mrb_debug_get_line(mrb_irep *irep, uint32_t pc)
68{
69 if (irep && pc < irep->ilen) {
70 mrb_irep_debug_info_file* f = NULL;
71 if (!irep->debug_info) {
72 return irep->lines? irep->lines[pc] : -1;
73 }
74 else if ((f = get_file(irep->debug_info, 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];
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;
90 }
91 else { count = step; }
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
111MRB_API mrb_irep_debug_info *
112mrb_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
124MRB_API mrb_irep_debug_info_file *
125mrb_debug_info_append_file(mrb_state *mrb, mrb_irep *irep,
126 uint32_t start_pos, uint32_t end_pos)
127{
128 mrb_irep_debug_info *info;
129 mrb_irep_debug_info_file *ret;
130 uint32_t file_pc_count;
131 size_t fn_len;
132 mrb_int len;
133 uint32_t i;
134
135 if (!irep->debug_info) { return NULL; }
136
137 mrb_assert(irep->filename);
138 mrb_assert(irep->lines);
139
140 info = irep->debug_info;
141
142 if (info->flen > 0 && strcmp(irep->filename, info->files[info->flen - 1]->filename) == 0) {
143 return NULL;
144 }
145
146 ret = (mrb_irep_debug_info_file *)mrb_malloc(mrb, sizeof(*ret));
147 info->files =
148 (mrb_irep_debug_info_file**)(
149 info->files
150 ? mrb_realloc(mrb, info->files, sizeof(mrb_irep_debug_info_file*) * (info->flen + 1))
151 : mrb_malloc(mrb, sizeof(mrb_irep_debug_info_file*)));
152 info->files[info->flen++] = ret;
153
154 file_pc_count = end_pos - start_pos;
155
156 ret->start_pos = start_pos;
157 info->pc_count = end_pos;
158
159 fn_len = strlen(irep->filename);
160 ret->filename_sym = mrb_intern(mrb, irep->filename, fn_len);
161 len = 0;
162 ret->filename = mrb_sym2name_len(mrb, ret->filename_sym, &len);
163
164 ret->line_type = select_line_type(irep->lines + start_pos, end_pos - start_pos);
165 ret->lines.ptr = NULL;
166
167 switch (ret->line_type) {
168 case mrb_debug_line_ary:
169 ret->line_entry_count = file_pc_count;
170 ret->lines.ary = (uint16_t*)mrb_malloc(mrb, sizeof(uint16_t) * file_pc_count);
171 for (i = 0; i < file_pc_count; ++i) {
172 ret->lines.ary[i] = irep->lines[start_pos + i];
173 }
174 break;
175
176 case mrb_debug_line_flat_map: {
177 uint16_t prev_line = 0;
178 mrb_irep_debug_info_line m;
179 ret->lines.flat_map = (mrb_irep_debug_info_line*)mrb_malloc(mrb, sizeof(mrb_irep_debug_info_line) * 1);
180 ret->line_entry_count = 0;
181 for (i = 0; i < file_pc_count; ++i) {
182 if (irep->lines[start_pos + i] == prev_line) { continue; }
183
184 ret->lines.flat_map = (mrb_irep_debug_info_line*)mrb_realloc(
185 mrb, ret->lines.flat_map,
186 sizeof(mrb_irep_debug_info_line) * (ret->line_entry_count + 1));
187 m.start_pos = start_pos + i;
188 m.line = irep->lines[start_pos + i];
189 ret->lines.flat_map[ret->line_entry_count] = m;
190
191 /* update */
192 ++ret->line_entry_count;
193 prev_line = irep->lines[start_pos + i];
194 }
195 } break;
196
197 default: mrb_assert(0); break;
198 }
199
200 return ret;
201}
202
203MRB_API void
204mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d)
205{
206 uint32_t i;
207
208 if (!d) { return; }
209
210 for (i = 0; i < d->flen; ++i) {
211 mrb_assert(d->files[i]);
212 mrb_free(mrb, d->files[i]->lines.ptr);
213 mrb_free(mrb, d->files[i]);
214 }
215 mrb_free(mrb, d->files);
216 mrb_free(mrb, d);
217}
Note: See TracBrowser for help on using the repository browser.