Debian Codegraph @

rpython/rlib/rvmprof/src/shared/symboltable.c

raw symbols SCIP
1 #include "symboltable.h"
2
3 #include "vmprof.h"
4 #include "machine.h"
5
6 #include "khash.h"
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <assert.h>
13 #include <dlfcn.h>
14
15 #if defined(VMPROF_LINUX)
16 #include <link.h>
17 #endif
18
19 #ifdef _PY_TEST
20 #define LOG(...) printf(__VA_ARGS__)
21 #else
22 #define LOG(...)
23 #endif
24
25 #ifdef __APPLE__
26
27 #include <mach-o/loader.h>
28 #include <mach-o/nlist.h>
29 #include <mach-o/stab.h>
30 #include <mach-o/dyld.h>
31 #include <mach-o/dyld_images.h>
32 #include <mach-o/fat.h>
33
34 int dyld_index_for_hdr(const struct mach_header_64 * hdr)
35 {
36 const struct mach_header_64 * it;
37 int image_count = _dyld_image_count();
38 for (int i = 0; i < image_count; i++) {
39 it = (const struct mach_header_64*)_dyld_get_image_header(i);
40 if (it == hdr) {
41 return i;
42 }
43 }
44
45 return -1;
46 }
47
48 void lookup_vmprof_debug_info(const char * name, const void * h,
49 char * srcfile, int srcfile_len, int * lineno) {
50
51 const struct mach_header_64 * hdr = (const struct mach_header_64*)h;
52 const struct symtab_command *sc;
53 const struct load_command *lc;
54
55 int index = dyld_index_for_hdr(hdr);
56 intptr_t slide = _dyld_get_image_vmaddr_slide(index);
57 if (hdr->magic != MH_MAGIC_64) {
58 return;
59 }
60
61 if (hdr->cputype != CPU_TYPE_X86_64) {
62 return;
63 }
64
65 lc = (const struct load_command *)(hdr + 1);
66
67 struct segment_command_64 * __linkedit = NULL;
68 struct segment_command_64 * __text = NULL;
69
70 LOG(" mach-o hdr has %d commands\n", hdr->ncmds);
71 for (uint32_t j = 0; j < hdr->ncmds; j++, (lc = (const struct load_command *)((char *)lc + lc->cmdsize))) {
72 if (lc->cmd == LC_SEGMENT_64) {
73 struct segment_command_64 * sc = (struct segment_command_64*)lc;
74 //LOG("segment command %s %llx %llx foff %llx fsize %llx\n",
75 // sc->segname, sc->vmaddr, sc->vmsize, sc->fileoff, sc->filesize);
76 if (strncmp("__LINKEDIT", sc->segname, 16) == 0) {
77 //LOG("segment command %s\n", sc->segname);
78 __linkedit = sc;
79 }
80 if (strncmp("__TEXT", sc->segname, 16) == 0) {
81 __text = sc;
82 }
83 }
84 }
85
86 if (__linkedit == NULL) {
87 LOG("couldn't find __linkedit\n");
88 return;
89 } else if (__text == NULL) {
90 LOG("couldn't find __text\n");
91 return;
92 }
93
94 uint64_t fileoff = __linkedit->fileoff;
95 uint64_t vmaddr = __linkedit->vmaddr;
96 const char * baseaddr = (const char*) vmaddr + slide - fileoff;
97 const char * __text_baseaddr = (const char*) slide - __text->fileoff;
98 //LOG("%llx %llx %llx\n", slide, __text->vmaddr, __text->fileoff);
99 const char * path = NULL;
100 const char * filename = NULL;
101 uint32_t src_line = 0;
102
103 lc = (const struct load_command *)(hdr + 1);
104 for (uint32_t j = 0; j < hdr->ncmds; j++, (lc = (const struct load_command *)((char *)lc + lc->cmdsize))) {
105 if (lc->cmd == LC_SYMTAB) {
106 LOG(" cmd %d/%d is LC_SYMTAB\n", j, hdr->ncmds);
107 sc = (const struct symtab_command*) lc;
108 // skip if symtab entry is not populated
109 if (sc->symoff == 0) {
110 LOG("LC_SYMTAB.symoff == 0\n");
111 continue;
112 } else if (sc->stroff == 0) {
113 LOG("LC_SYMTAB.stroff == 0\n");
114 continue;
115 } else if (sc->nsyms == 0) {
116 LOG("LC_SYMTAB.nsym == 0\n");
117 continue;
118 } else if (sc->strsize == 0) {
119 LOG("LC_SYMTAB.strsize == 0\n");
120 continue;
121 }
122 const char * strtbl = (const char*)(baseaddr + sc->stroff);
123 struct nlist_64 * l = (struct nlist_64*)(baseaddr + sc->symoff);
124 //LOG("baseaddr %llx fileoff: %lx vmaddr %llx, symoff %llx stroff %llx slide %llx %d\n",
125 // baseaddr, fileoff, vmaddr, sc->symoff, sc->stroff, slide, sc->nsyms);
126 for (uint32_t s = 0; s < sc->nsyms; s++) {
127 struct nlist_64 * entry = &l[s];
128 uint32_t t = entry->n_type;
129 bool is_debug = (t & N_STAB) != 0;
130 if (!is_debug) {
131 continue;
132 }
133 uint32_t off = entry->n_un.n_strx;
134 if (off >= sc->strsize || off == 0) {
135 continue;
136 }
137 const char * sym = &strtbl[off];
138 if (sym[0] == '\x00') {
139 sym = NULL;
140 }
141 // switch through the different types
142 switch (t) {
143 case N_FUN: {
144 if (sym != NULL && strcmp(name, sym+1) == 0) {
145 *lineno = src_line;
146 if (src_line == 0) {
147 *lineno = entry->n_desc;
148 }
149 snprintf(srcfile, srcfile_len, "%s%s", path, filename);
150 }
151 break;
152 }
153 case N_SLINE: {
154 // does not seem to occur
155 src_line = entry->n_desc;
156 break;
157 }
158 case N_SO: {
159 // the first entry is the path, the second the filename,
160 // if a null occurs, the path and filename is reset
161 if (sym == NULL) {
162 path = NULL;
163 filename = NULL;
164 } else if (path == NULL) {
165 path = sym;
166 } else if (filename == NULL) {
167 filename = sym;
168 }
169 break;
170 }
171 }
172 }
173 }
174 }
175 }
176
177 #endif
178
179 #ifdef __unix__
180 #include "libbacktrace/backtrace.h"
181 void backtrace_error_cb(void *data, const char *msg, int errnum)
182 {
183 }
184
185 // a struct that helps to copy over data for the callbacks
186 typedef struct addr_info {
187 char * name;
188 int name_len;
189 char * srcfile;
190 int srcfile_len;
191 int * lineno;
192 } addr_info_t;
193
194 int backtrace_full_cb(void *data, uintptr_t pc, const char *filename,
195 int lineno, const char *function)
196 {
197 addr_info_t * info = (addr_info_t*)data;
198 if (function != NULL) {
199 // found the symbol name
200 (void)strncpy(info->name, function, info->name_len);
201 }
202 if (filename != NULL) {
203 (void)strncpy(info->srcfile, filename, info->srcfile_len);
204 }
205 *info->lineno = lineno;
206 return 0;
207 }
208 #endif
209
210 static
211 struct backtrace_state * bstate = NULL;
212
213 int vmp_resolve_addr(void * addr, char * name, int name_len, int * lineno, char * srcfile, int srcfile_len) {
214 #ifdef __APPLE__
215 Dl_info dlinfo;
216 if (dladdr((const void*)addr, &dlinfo) == 0) {
217 return 1;
218 }
219 if (dlinfo.dli_sname != NULL) {
220 (void)strncpy(name, dlinfo.dli_sname, name_len-1);
221 name[name_len-1] = 0;
222 }
223 lookup_vmprof_debug_info(name, dlinfo.dli_fbase, srcfile, srcfile_len, lineno);
224 // copy the shared object name to the source file name if source cannot be determined
225 if (srcfile[0] == 0 && dlinfo.dli_fname != NULL) {
226 (void)strncpy(srcfile, dlinfo.dli_fname, srcfile_len-1);
227 srcfile[srcfile_len-1] = 0;
228 }
229 #elif defined(VMPROF_LINUX)
230 if (bstate == NULL) {
231 bstate = backtrace_create_state (NULL, 1, backtrace_error_cb, NULL);
232 }
233 addr_info_t info = { .name = name, .name_len = name_len,
234 .srcfile = srcfile, .srcfile_len = srcfile_len,
235 .lineno = lineno
236 };
237 if (backtrace_pcinfo(bstate, (uintptr_t)addr, backtrace_full_cb,
238 backtrace_error_cb, (void*)&info)) {
239 // failed
240 return 1;
241 }
242
243 // nothing found, try with dladdr
244 if (info.name[0] == 0) {
245 Dl_info dlinfo;
246 dlinfo.dli_sname = NULL;
247 (void)dladdr((const void*)addr, &dlinfo);
248 if (dlinfo.dli_sname != NULL) {
249 (void)strncpy(info.name, dlinfo.dli_sname, info.name_len-1);
250 name[name_len-1] = 0;
251 }
252
253 }
254
255 // copy the shared object name to the source file name if source cannot be determined
256 if (srcfile[0] == 0) {
257 Dl_info dlinfo;
258 dlinfo.dli_fname = NULL;
259 (void)dladdr((const void*)addr, &dlinfo);
260 if (dlinfo.dli_fname != NULL) {
261 (void)strncpy(srcfile, dlinfo.dli_fname, srcfile_len-1);
262 srcfile[srcfile_len-1] = 0;
263 }
264 }
265 #endif
266 return 0;
267 }