Blob


1 /*
2 * Dwarf pc to source line conversion.
3 *
4 * Maybe should do the reverse here, but what should the interface look like?
5 * One possibility is to use the Plan 9 line2addr interface:
6 *
7 * long line2addr(ulong line, ulong basepc)
8 *
9 * which returns the smallest pc > basepc with line number line (ignoring file name).
10 *
11 * The encoding may be small, but it sure isn't simple!
12 */
14 #include <u.h>
15 #include <libc.h>
16 #include <bio.h>
17 #include "elf.h"
18 #include "dwarf.h"
20 #define trace 0
22 enum
23 {
24 Isstmt = 1<<0,
25 BasicDwarfBlock = 1<<1,
26 EndSequence = 1<<2,
27 PrologueEnd = 1<<3,
28 EpilogueBegin = 1<<4
29 };
31 typedef struct State State;
32 struct State
33 {
34 ulong addr;
35 ulong file;
36 ulong line;
37 ulong column;
38 ulong flags;
39 ulong isa;
40 };
42 int
43 dwarfpctoline(Dwarf *d, ulong pc, char **cdir, char **dir, char **file, ulong *line, ulong *mtime, ulong *length)
44 {
45 uchar *prog, *opcount, *end;
46 ulong off, unit, len, vers, x, start;
47 int i, first, op, a, l, quantum, isstmt, linebase, linerange, opcodebase, nf;
48 char *files, *dirs, *s;
49 DwarfBuf b;
50 DwarfSym sym;
51 State emit, cur, reset;
52 uchar **f, **newf;
54 f = nil;
56 if(dwarfaddrtounit(d, pc, &unit) < 0
57 || dwarflookuptag(d, unit, TagCompileUnit, &sym) < 0)
58 return -1;
60 if(!sym.attrs.have.stmtlist){
61 werrstr("no line mapping information for 0x%lux", pc);
62 return -1;
63 }
64 off = sym.attrs.stmtlist;
65 if(off >= d->line.len){
66 fprint(2, "bad stmtlist\n");
67 goto bad;
68 }
70 if(trace) fprint(2, "unit 0x%lux stmtlist 0x%lux\n", unit, sym.attrs.stmtlist);
72 memset(&b, 0, sizeof b);
73 b.d = d;
74 b.p = d->line.data + off;
75 b.ep = b.p + d->line.len;
76 b.addrsize = sym.b.addrsize; /* should i get this from somewhere else? */
78 len = dwarfget4(&b);
79 if(b.p==nil || b.p+len > b.ep || b.p+len < b.p){
80 fprint(2, "bad len\n");
81 goto bad;
82 }
84 b.ep = b.p+len;
85 vers = dwarfget2(&b);
86 if(vers != 2){
87 werrstr("bad dwarf version 0x%lux", vers);
88 return -1;
89 }
91 len = dwarfget4(&b);
92 if(b.p==nil || b.p+len > b.ep || b.p+len < b.p){
93 fprint(2, "another bad len\n");
94 goto bad;
95 }
96 prog = b.p+len;
98 quantum = dwarfget1(&b);
99 isstmt = dwarfget1(&b);
100 linebase = (schar)dwarfget1(&b);
101 linerange = (schar)dwarfget1(&b);
102 opcodebase = dwarfget1(&b);
104 opcount = b.p-1;
105 dwarfgetnref(&b, opcodebase-1);
106 if(b.p == nil){
107 fprint(2, "bad opcode chart\n");
108 goto bad;
111 /* just skip the files and dirs for now; we'll come back */
112 dirs = (char*)b.p;
113 while(b.p!=nil && *b.p!=0)
114 dwarfgetstring(&b);
115 dwarfget1(&b);
117 files = (char*)b.p;
118 while(b.p!=nil && *b.p!=0){
119 dwarfgetstring(&b);
120 dwarfget128(&b);
121 dwarfget128(&b);
122 dwarfget128(&b);
124 dwarfget1(&b);
126 /* move on to the program */
127 if(b.p == nil || b.p > prog){
128 fprint(2, "bad header\n");
129 goto bad;
131 b.p = prog;
133 reset.addr = 0;
134 reset.file = 1;
135 reset.line = 1;
136 reset.column = 0;
137 reset.flags = isstmt ? Isstmt : 0;
138 reset.isa = 0;
140 cur = reset;
141 emit = reset;
142 nf = 0;
143 start = 0;
144 if(trace) fprint(2, "program @ %lud ... %.*H opbase = %d\n", b.p - d->line.data, b.ep-b.p, b.p, opcodebase);
145 first = 1;
146 while(b.p != nil){
147 op = dwarfget1(&b);
148 if(trace) fprint(2, "\tline %lud, addr 0x%lux, op %d %.10H", cur.line, cur.addr, op, b.p);
149 if(op >= opcodebase){
150 a = (op - opcodebase) / linerange;
151 l = (op - opcodebase) % linerange + linebase;
152 cur.line += l;
153 cur.addr += a * quantum;
154 if(trace) fprint(2, " +%d,%d\n", a, l);
155 emit:
156 if(first){
157 if(cur.addr > pc){
158 werrstr("found wrong line mapping 0x%lux for pc 0x%lux", cur.addr, pc);
159 goto out;
161 first = 0;
162 start = cur.addr;
164 if(cur.addr > pc)
165 break;
166 if(b.p == nil){
167 werrstr("buffer underflow in line mapping");
168 goto out;
170 emit = cur;
171 if(emit.flags & EndSequence){
172 werrstr("found wrong line mapping 0x%lux-0x%lux for pc 0x%lux", start, cur.addr, pc);
173 goto out;
175 cur.flags &= ~(BasicDwarfBlock|PrologueEnd|EpilogueBegin);
176 }else{
177 switch(op){
178 case 0: /* extended op code */
179 if(trace) fprint(2, " ext");
180 len = dwarfget128(&b);
181 end = b.p+len;
182 if(b.p == nil || end > b.ep || end < b.p || len < 1)
183 goto bad;
184 switch(dwarfget1(&b)){
185 case 1: /* end sequence */
186 if(trace) fprint(2, " end\n");
187 cur.flags |= EndSequence;
188 goto emit;
189 case 2: /* set address */
190 cur.addr = dwarfgetaddr(&b);
191 if(trace) fprint(2, " set pc 0x%lux\n", cur.addr);
192 break;
193 case 3: /* define file */
194 newf = realloc(f, (nf+1)*sizeof(f[0]));
195 if(newf == nil)
196 goto out;
197 f[nf++] = b.p;
198 s = dwarfgetstring(&b);
199 dwarfget128(&b);
200 dwarfget128(&b);
201 dwarfget128(&b);
202 if(trace) fprint(2, " def file %s\n", s);
203 break;
205 if(b.p == nil || b.p > end)
206 goto bad;
207 b.p = end;
208 break;
209 case 1: /* emit */
210 if(trace) fprint(2, " emit\n");
211 goto emit;
212 case 2: /* advance pc */
213 a = dwarfget128(&b);
214 if(trace) fprint(2, " advance pc + %lud\n", a*quantum);
215 cur.addr += a * quantum;
216 break;
217 case 3: /* advance line */
218 l = dwarfget128s(&b);
219 if(trace) fprint(2, " advance line + %ld\n", l);
220 cur.line += l;
221 break;
222 case 4: /* set file */
223 if(trace) fprint(2, " set file\n");
224 cur.file = dwarfget128s(&b);
225 break;
226 case 5: /* set column */
227 if(trace) fprint(2, " set column\n");
228 cur.column = dwarfget128(&b);
229 break;
230 case 6: /* negate stmt */
231 if(trace) fprint(2, " negate stmt\n");
232 cur.flags ^= Isstmt;
233 break;
234 case 7: /* set basic block */
235 if(trace) fprint(2, " set basic block\n");
236 cur.flags |= BasicDwarfBlock;
237 break;
238 case 8: /* const add pc */
239 a = (255 - opcodebase) / linerange * quantum;
240 if(trace) fprint(2, " const add pc + %d\n", a);
241 cur.addr += a;
242 break;
243 case 9: /* fixed advance pc */
244 a = dwarfget2(&b);
245 if(trace) fprint(2, " fixed advance pc + %d\n", a);
246 cur.addr += a;
247 break;
248 case 10: /* set prologue end */
249 if(trace) fprint(2, " set prologue end\n");
250 cur.flags |= PrologueEnd;
251 break;
252 case 11: /* set epilogue begin */
253 if(trace) fprint(2, " set epilogue begin\n");
254 cur.flags |= EpilogueBegin;
255 break;
256 case 12: /* set isa */
257 if(trace) fprint(2, " set isa\n");
258 cur.isa = dwarfget128(&b);
259 break;
260 default: /* something new - skip it */
261 if(trace) fprint(2, " unknown %d\n", opcount[op]);
262 for(i=0; i<opcount[op]; i++)
263 dwarfget128(&b);
264 break;
268 if(b.p == nil)
269 goto bad;
271 /* finally! the data we seek is in "emit" */
273 if(emit.file == 0){
274 werrstr("invalid file index in mapping data");
275 goto out;
277 if(line)
278 *line = emit.line;
280 /* skip over first emit.file-2 guys */
281 b.p = (uchar*)files;
282 for(i=emit.file-1; i > 0 && b.p!=nil && *b.p!=0; i--){
283 dwarfgetstring(&b);
284 dwarfget128(&b);
285 dwarfget128(&b);
286 dwarfget128(&b);
288 if(b.p == nil){
289 werrstr("problem parsing file data second time (cannot happen)");
290 goto bad;
292 if(*b.p == 0){
293 if(i >= nf){
294 werrstr("bad file index in mapping data");
295 goto bad;
297 b.p = f[i];
299 s = dwarfgetstring(&b);
300 if(file)
301 *file = s;
302 i = dwarfget128(&b); /* directory */
303 x = dwarfget128(&b);
304 if(mtime)
305 *mtime = x;
306 x = dwarfget128(&b);
307 if(length)
308 *length = x;
310 /* fetch dir name */
311 if(cdir)
312 *cdir = sym.attrs.compdir;
314 if(dir){
315 if(i == 0)
316 *dir = nil;
317 else{
318 b.p = (uchar*)dirs;
319 for(i--; i>0 && b.p!=nil && *b.p!=0; i--)
320 dwarfgetstring(&b);
321 if(b.p==nil || *b.p==0){
322 werrstr("bad directory reference in line mapping");
323 goto out; /* can only happen with bad dir index */
325 *dir = dwarfgetstring(&b);
329 /* free at last, free at last */
330 free(f);
331 return 0;
333 bad:
334 werrstr("corrupted line mapping for 0x%lux", pc);
335 out:
336 free(f);
337 return -1;