xref: /reactos/sdk/lib/rossym_new/dwarfdump.c (revision 1734f297)
1 #if 0
2 #include <u.h>
3 #include <libc.h>
4 #include <bio.h>
5 #include "elf.h"
6 #endif
7 #include "dwarf.h"
8 #include "pe.h"
9 
10 void printrules(Dwarf *d, ulong pc);
11 //int exprfmt(Fmt*);
12 
13 void
14 usage(void)
15 {
16 	fprint(2, "usage: dwarfdump file\n");
17 	exits("usage");
18 }
19 
20 void
21 main(int argc, char **argv)
22 {
23 	int c;
24 	Pe *pe;
25 	Dwarf *d;
26 	DwarfSym s;
27 	char *cdir, *dir, *file;
28 	ulong line, mtime, length;
29 
30 	if(argc != 2)
31 		usage();
32 
33 #if 0
34 	fmtinstall('R', exprfmt);
35 	fmtinstall('H', encodefmt);
36 #endif
37 
38 	if((pe = peopen(argv[1])) == nil)
39 		sysfatal("elfopen %s: %r", argv[1]);
40 	if((d=dwarfopen(pe)) == nil)
41 		sysfatal("dwarfopen: %r");
42 
43 	if(dwarfenum(d, &s) < 0)
44 		sysfatal("dwarfenumall: %r");
45 
46 	while(dwarfnextsym(d, &s) == 1){
47 		switch(s.attrs.tag){
48 		case TagCompileUnit:
49 			print("compileunit %s\n", s.attrs.name);
50 			break;
51 		case TagSubprogram:
52 			c = 't';
53 			goto sym;
54 		case TagVariable:
55 			c = 'd';
56 			goto sym;
57 		case TagConstant:
58 			c = 'c';
59 			goto sym;
60 		case TagFormalParameter:
61 			if(!s.attrs.name)
62 				break;
63 			c = 'p';
64 		sym:
65 			if(s.attrs.isexternal)
66 				c += 'A' - 'a';
67 			print("%c %s", c, s.attrs.name);
68 			if(s.attrs.have.lowpc)
69 				print(" 0x%lux-0x%lux", s.attrs.lowpc, s.attrs.highpc);
70 			switch(s.attrs.have.location){
71 			case TBlock:
72 				print(" @ %.*H", s.attrs.location.b.len, s.attrs.location.b.data);
73 				break;
74 			case TConstant:
75 				print(" @ 0x%lux", s.attrs.location.c);
76 				break;
77 			}
78 			if(s.attrs.have.ranges)
79 				print(" ranges@0x%lux", s.attrs.ranges);
80 			print("\n");
81 			if(s.attrs.have.lowpc){
82 				if(dwarfpctoline(d, s.attrs.lowpc, &cdir, &dir, &file, &line, &mtime, &length) < 0)
83 					print("\tcould not find source: %r\n");
84 				else if(dir == nil)
85 					print("\t%s/%s:%lud mtime=%lud length=%lud\n",
86 						cdir, file, line, mtime, length);
87 				else
88 					print("\t%s/%s/%s:%lud mtime=%lud length=%lud\n",
89 						cdir, dir, file, line, mtime, length);
90 
91 				if(0) printrules(d, s.attrs.lowpc);
92 				if(0) printrules(d, (s.attrs.lowpc+s.attrs.highpc)/2);
93 			}
94 			break;
95 		}
96 	}
97 	exits(0);
98 }
99 
100 void
101 printrules(Dwarf *d, ulong pc)
102 {
103 	int i;
104 	DwarfExpr r[10];
105 	DwarfExpr cfa, ra;
106 
107 	if(dwarfunwind(d, pc, &cfa, &ra, r, nelem(r)) < 0)
108 		print("\tcannot unwind from pc 0x%lux: %r\n", pc);
109 
110 	print("\tpc=0x%lux cfa=%R ra=%R", pc, &cfa, &ra);
111 	for(i=0; i<nelem(r); i++)
112 		if(r[i].type != RuleSame)
113 			print(" r%d=%R", i, &r[i]);
114 	print("\n");
115 }
116 
117 #if 0
118 int
119 exprfmt(Fmt *fmt)
120 {
121 	DwarfExpr *e;
122 
123 	if((e = va_arg(fmt->args, DwarfExpr*)) == nil)
124 		return fmtstrcpy(fmt, "<nil>");
125 
126 	switch(e->type){
127 	case RuleUndef:
128 		return fmtstrcpy(fmt, "undef");
129 	case RuleSame:
130 		return fmtstrcpy(fmt, "same");
131 	case RuleCfaOffset:
132 		return fmtprint(fmt, "%ld(cfa)", e->offset);
133 	case RuleRegister:
134 		return fmtprint(fmt, "r%ld", e->reg);
135 	case RuleRegOff:
136 		return fmtprint(fmt, "%ld(r%ld)", e->offset, e->reg);
137 	case RuleLocation:
138 		return fmtprint(fmt, "l.%.*H", e->loc.len, e->loc.data);
139 	default:
140 		return fmtprint(fmt, "?%d", e->type);
141 	}
142 }
143 #endif
144