symbol.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "sort.h"
  4. #include "string.h"
  5. #include "symbol.h"
  6. #include "thread.h"
  7. #include "debug.h"
  8. #include <asm/bug.h>
  9. #include <libelf.h>
  10. #include <gelf.h>
  11. #include <elf.h>
  12. #include <limits.h>
  13. #include <sys/utsname.h>
  14. #ifndef NT_GNU_BUILD_ID
  15. #define NT_GNU_BUILD_ID 3
  16. #endif
  17. enum dso_origin {
  18. DSO__ORIG_KERNEL = 0,
  19. DSO__ORIG_JAVA_JIT,
  20. DSO__ORIG_BUILD_ID_CACHE,
  21. DSO__ORIG_FEDORA,
  22. DSO__ORIG_UBUNTU,
  23. DSO__ORIG_BUILDID,
  24. DSO__ORIG_DSO,
  25. DSO__ORIG_KMODULE,
  26. DSO__ORIG_NOT_FOUND,
  27. };
  28. static void dsos__add(struct list_head *head, struct dso *dso);
  29. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  30. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  31. symbol_filter_t filter);
  32. static int vmlinux_path__nr_entries;
  33. static char **vmlinux_path;
  34. struct symbol_conf symbol_conf = {
  35. .exclude_other = true,
  36. .use_modules = true,
  37. .try_vmlinux_path = true,
  38. };
  39. bool dso__loaded(const struct dso *self, enum map_type type)
  40. {
  41. return self->loaded & (1 << type);
  42. }
  43. bool dso__sorted_by_name(const struct dso *self, enum map_type type)
  44. {
  45. return self->sorted_by_name & (1 << type);
  46. }
  47. static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
  48. {
  49. self->sorted_by_name |= (1 << type);
  50. }
  51. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  52. {
  53. switch (map_type) {
  54. case MAP__FUNCTION:
  55. return symbol_type == 'T' || symbol_type == 'W';
  56. case MAP__VARIABLE:
  57. return symbol_type == 'D' || symbol_type == 'd';
  58. default:
  59. return false;
  60. }
  61. }
  62. static void symbols__fixup_end(struct rb_root *self)
  63. {
  64. struct rb_node *nd, *prevnd = rb_first(self);
  65. struct symbol *curr, *prev;
  66. if (prevnd == NULL)
  67. return;
  68. curr = rb_entry(prevnd, struct symbol, rb_node);
  69. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  70. prev = curr;
  71. curr = rb_entry(nd, struct symbol, rb_node);
  72. if (prev->end == prev->start)
  73. prev->end = curr->start - 1;
  74. }
  75. /* Last entry */
  76. if (curr->end == curr->start)
  77. curr->end = roundup(curr->start, 4096);
  78. }
  79. static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
  80. {
  81. struct map *prev, *curr;
  82. struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
  83. if (prevnd == NULL)
  84. return;
  85. curr = rb_entry(prevnd, struct map, rb_node);
  86. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  87. prev = curr;
  88. curr = rb_entry(nd, struct map, rb_node);
  89. prev->end = curr->start - 1;
  90. }
  91. /*
  92. * We still haven't the actual symbols, so guess the
  93. * last map final address.
  94. */
  95. curr->end = ~0UL;
  96. }
  97. static void map_groups__fixup_end(struct map_groups *self)
  98. {
  99. int i;
  100. for (i = 0; i < MAP__NR_TYPES; ++i)
  101. __map_groups__fixup_end(self, i);
  102. }
  103. static struct symbol *symbol__new(u64 start, u64 len, const char *name)
  104. {
  105. size_t namelen = strlen(name) + 1;
  106. struct symbol *self = zalloc(symbol_conf.priv_size +
  107. sizeof(*self) + namelen);
  108. if (self == NULL)
  109. return NULL;
  110. if (symbol_conf.priv_size)
  111. self = ((void *)self) + symbol_conf.priv_size;
  112. self->start = start;
  113. self->end = len ? start + len - 1 : start;
  114. pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
  115. memcpy(self->name, name, namelen);
  116. return self;
  117. }
  118. void symbol__delete(struct symbol *self)
  119. {
  120. free(((void *)self) - symbol_conf.priv_size);
  121. }
  122. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  123. {
  124. return fprintf(fp, " %llx-%llx %s\n",
  125. self->start, self->end, self->name);
  126. }
  127. void dso__set_long_name(struct dso *self, char *name)
  128. {
  129. if (name == NULL)
  130. return;
  131. self->long_name = name;
  132. self->long_name_len = strlen(name);
  133. }
  134. static void dso__set_short_name(struct dso *self, const char *name)
  135. {
  136. if (name == NULL)
  137. return;
  138. self->short_name = name;
  139. self->short_name_len = strlen(name);
  140. }
  141. static void dso__set_basename(struct dso *self)
  142. {
  143. dso__set_short_name(self, basename(self->long_name));
  144. }
  145. struct dso *dso__new(const char *name)
  146. {
  147. struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
  148. if (self != NULL) {
  149. int i;
  150. strcpy(self->name, name);
  151. dso__set_long_name(self, self->name);
  152. dso__set_short_name(self, self->name);
  153. for (i = 0; i < MAP__NR_TYPES; ++i)
  154. self->symbols[i] = self->symbol_names[i] = RB_ROOT;
  155. self->slen_calculated = 0;
  156. self->origin = DSO__ORIG_NOT_FOUND;
  157. self->loaded = 0;
  158. self->sorted_by_name = 0;
  159. self->has_build_id = 0;
  160. }
  161. return self;
  162. }
  163. static void symbols__delete(struct rb_root *self)
  164. {
  165. struct symbol *pos;
  166. struct rb_node *next = rb_first(self);
  167. while (next) {
  168. pos = rb_entry(next, struct symbol, rb_node);
  169. next = rb_next(&pos->rb_node);
  170. rb_erase(&pos->rb_node, self);
  171. symbol__delete(pos);
  172. }
  173. }
  174. void dso__delete(struct dso *self)
  175. {
  176. int i;
  177. for (i = 0; i < MAP__NR_TYPES; ++i)
  178. symbols__delete(&self->symbols[i]);
  179. if (self->long_name != self->name)
  180. free(self->long_name);
  181. free(self);
  182. }
  183. void dso__set_build_id(struct dso *self, void *build_id)
  184. {
  185. memcpy(self->build_id, build_id, sizeof(self->build_id));
  186. self->has_build_id = 1;
  187. }
  188. static void symbols__insert(struct rb_root *self, struct symbol *sym)
  189. {
  190. struct rb_node **p = &self->rb_node;
  191. struct rb_node *parent = NULL;
  192. const u64 ip = sym->start;
  193. struct symbol *s;
  194. while (*p != NULL) {
  195. parent = *p;
  196. s = rb_entry(parent, struct symbol, rb_node);
  197. if (ip < s->start)
  198. p = &(*p)->rb_left;
  199. else
  200. p = &(*p)->rb_right;
  201. }
  202. rb_link_node(&sym->rb_node, parent, p);
  203. rb_insert_color(&sym->rb_node, self);
  204. }
  205. static struct symbol *symbols__find(struct rb_root *self, u64 ip)
  206. {
  207. struct rb_node *n;
  208. if (self == NULL)
  209. return NULL;
  210. n = self->rb_node;
  211. while (n) {
  212. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  213. if (ip < s->start)
  214. n = n->rb_left;
  215. else if (ip > s->end)
  216. n = n->rb_right;
  217. else
  218. return s;
  219. }
  220. return NULL;
  221. }
  222. struct symbol_name_rb_node {
  223. struct rb_node rb_node;
  224. struct symbol sym;
  225. };
  226. static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
  227. {
  228. struct rb_node **p = &self->rb_node;
  229. struct rb_node *parent = NULL;
  230. struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
  231. while (*p != NULL) {
  232. parent = *p;
  233. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  234. if (strcmp(sym->name, s->sym.name) < 0)
  235. p = &(*p)->rb_left;
  236. else
  237. p = &(*p)->rb_right;
  238. }
  239. rb_link_node(&symn->rb_node, parent, p);
  240. rb_insert_color(&symn->rb_node, self);
  241. }
  242. static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
  243. {
  244. struct rb_node *nd;
  245. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  246. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  247. symbols__insert_by_name(self, pos);
  248. }
  249. }
  250. static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
  251. {
  252. struct rb_node *n;
  253. if (self == NULL)
  254. return NULL;
  255. n = self->rb_node;
  256. while (n) {
  257. struct symbol_name_rb_node *s;
  258. int cmp;
  259. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  260. cmp = strcmp(name, s->sym.name);
  261. if (cmp < 0)
  262. n = n->rb_left;
  263. else if (cmp > 0)
  264. n = n->rb_right;
  265. else
  266. return &s->sym;
  267. }
  268. return NULL;
  269. }
  270. struct symbol *dso__find_symbol(struct dso *self,
  271. enum map_type type, u64 addr)
  272. {
  273. return symbols__find(&self->symbols[type], addr);
  274. }
  275. struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
  276. const char *name)
  277. {
  278. return symbols__find_by_name(&self->symbol_names[type], name);
  279. }
  280. void dso__sort_by_name(struct dso *self, enum map_type type)
  281. {
  282. dso__set_sorted_by_name(self, type);
  283. return symbols__sort_by_name(&self->symbol_names[type],
  284. &self->symbols[type]);
  285. }
  286. int build_id__sprintf(const u8 *self, int len, char *bf)
  287. {
  288. char *bid = bf;
  289. const u8 *raw = self;
  290. int i;
  291. for (i = 0; i < len; ++i) {
  292. sprintf(bid, "%02x", *raw);
  293. ++raw;
  294. bid += 2;
  295. }
  296. return raw - self;
  297. }
  298. size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
  299. {
  300. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  301. build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
  302. return fprintf(fp, "%s", sbuild_id);
  303. }
  304. size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
  305. {
  306. struct rb_node *nd;
  307. size_t ret = fprintf(fp, "dso: %s (", self->short_name);
  308. if (self->short_name != self->long_name)
  309. ret += fprintf(fp, "%s, ", self->long_name);
  310. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  311. self->loaded ? "" : "NOT ");
  312. ret += dso__fprintf_buildid(self, fp);
  313. ret += fprintf(fp, ")\n");
  314. for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
  315. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  316. ret += symbol__fprintf(pos, fp);
  317. }
  318. return ret;
  319. }
  320. int kallsyms__parse(const char *filename, void *arg,
  321. int (*process_symbol)(void *arg, const char *name,
  322. char type, u64 start))
  323. {
  324. char *line = NULL;
  325. size_t n;
  326. int err = 0;
  327. FILE *file = fopen(filename, "r");
  328. if (file == NULL)
  329. goto out_failure;
  330. while (!feof(file)) {
  331. u64 start;
  332. int line_len, len;
  333. char symbol_type;
  334. char *symbol_name;
  335. line_len = getline(&line, &n, file);
  336. if (line_len < 0)
  337. break;
  338. if (!line)
  339. goto out_failure;
  340. line[--line_len] = '\0'; /* \n */
  341. len = hex2u64(line, &start);
  342. len++;
  343. if (len + 2 >= line_len)
  344. continue;
  345. symbol_type = toupper(line[len]);
  346. symbol_name = line + len + 2;
  347. err = process_symbol(arg, symbol_name, symbol_type, start);
  348. if (err)
  349. break;
  350. }
  351. free(line);
  352. fclose(file);
  353. return err;
  354. out_failure:
  355. return -1;
  356. }
  357. struct process_kallsyms_args {
  358. struct map *map;
  359. struct dso *dso;
  360. };
  361. static int map__process_kallsym_symbol(void *arg, const char *name,
  362. char type, u64 start)
  363. {
  364. struct symbol *sym;
  365. struct process_kallsyms_args *a = arg;
  366. struct rb_root *root = &a->dso->symbols[a->map->type];
  367. if (!symbol_type__is_a(type, a->map->type))
  368. return 0;
  369. /*
  370. * Will fix up the end later, when we have all symbols sorted.
  371. */
  372. sym = symbol__new(start, 0, name);
  373. if (sym == NULL)
  374. return -ENOMEM;
  375. /*
  376. * We will pass the symbols to the filter later, in
  377. * map__split_kallsyms, when we have split the maps per module
  378. */
  379. symbols__insert(root, sym);
  380. return 0;
  381. }
  382. /*
  383. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  384. * so that we can in the next step set the symbol ->end address and then
  385. * call kernel_maps__split_kallsyms.
  386. */
  387. static int dso__load_all_kallsyms(struct dso *self, const char *filename,
  388. struct map *map)
  389. {
  390. struct process_kallsyms_args args = { .map = map, .dso = self, };
  391. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  392. }
  393. /*
  394. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  395. * kernel range is broken in several maps, named [kernel].N, as we don't have
  396. * the original ELF section names vmlinux have.
  397. */
  398. static int dso__split_kallsyms(struct dso *self, struct map *map,
  399. symbol_filter_t filter)
  400. {
  401. struct map_groups *kmaps = map__kmap(map)->kmaps;
  402. struct map *curr_map = map;
  403. struct symbol *pos;
  404. int count = 0;
  405. struct rb_root *root = &self->symbols[map->type];
  406. struct rb_node *next = rb_first(root);
  407. int kernel_range = 0;
  408. while (next) {
  409. char *module;
  410. pos = rb_entry(next, struct symbol, rb_node);
  411. next = rb_next(&pos->rb_node);
  412. module = strchr(pos->name, '\t');
  413. if (module) {
  414. if (!symbol_conf.use_modules)
  415. goto discard_symbol;
  416. *module++ = '\0';
  417. if (strcmp(curr_map->dso->short_name, module)) {
  418. curr_map = map_groups__find_by_name(kmaps, map->type, module);
  419. if (curr_map == NULL) {
  420. pr_debug("/proc/{kallsyms,modules} "
  421. "inconsistency while looking "
  422. "for \"%s\" module!\n", module);
  423. return -1;
  424. }
  425. if (curr_map->dso->loaded)
  426. goto discard_symbol;
  427. }
  428. /*
  429. * So that we look just like we get from .ko files,
  430. * i.e. not prelinked, relative to map->start.
  431. */
  432. pos->start = curr_map->map_ip(curr_map, pos->start);
  433. pos->end = curr_map->map_ip(curr_map, pos->end);
  434. } else if (curr_map != map) {
  435. char dso_name[PATH_MAX];
  436. struct dso *dso;
  437. snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
  438. kernel_range++);
  439. dso = dso__new(dso_name);
  440. if (dso == NULL)
  441. return -1;
  442. curr_map = map__new2(pos->start, dso, map->type);
  443. if (curr_map == NULL) {
  444. dso__delete(dso);
  445. return -1;
  446. }
  447. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  448. map_groups__insert(kmaps, curr_map);
  449. ++kernel_range;
  450. }
  451. if (filter && filter(curr_map, pos)) {
  452. discard_symbol: rb_erase(&pos->rb_node, root);
  453. symbol__delete(pos);
  454. } else {
  455. if (curr_map != map) {
  456. rb_erase(&pos->rb_node, root);
  457. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  458. }
  459. count++;
  460. }
  461. }
  462. return count;
  463. }
  464. int dso__load_kallsyms(struct dso *self, const char *filename,
  465. struct map *map, symbol_filter_t filter)
  466. {
  467. if (dso__load_all_kallsyms(self, filename, map) < 0)
  468. return -1;
  469. symbols__fixup_end(&self->symbols[map->type]);
  470. self->origin = DSO__ORIG_KERNEL;
  471. return dso__split_kallsyms(self, map, filter);
  472. }
  473. static int dso__load_perf_map(struct dso *self, struct map *map,
  474. symbol_filter_t filter)
  475. {
  476. char *line = NULL;
  477. size_t n;
  478. FILE *file;
  479. int nr_syms = 0;
  480. file = fopen(self->long_name, "r");
  481. if (file == NULL)
  482. goto out_failure;
  483. while (!feof(file)) {
  484. u64 start, size;
  485. struct symbol *sym;
  486. int line_len, len;
  487. line_len = getline(&line, &n, file);
  488. if (line_len < 0)
  489. break;
  490. if (!line)
  491. goto out_failure;
  492. line[--line_len] = '\0'; /* \n */
  493. len = hex2u64(line, &start);
  494. len++;
  495. if (len + 2 >= line_len)
  496. continue;
  497. len += hex2u64(line + len, &size);
  498. len++;
  499. if (len + 2 >= line_len)
  500. continue;
  501. sym = symbol__new(start, size, line + len);
  502. if (sym == NULL)
  503. goto out_delete_line;
  504. if (filter && filter(map, sym))
  505. symbol__delete(sym);
  506. else {
  507. symbols__insert(&self->symbols[map->type], sym);
  508. nr_syms++;
  509. }
  510. }
  511. free(line);
  512. fclose(file);
  513. return nr_syms;
  514. out_delete_line:
  515. free(line);
  516. out_failure:
  517. return -1;
  518. }
  519. /**
  520. * elf_symtab__for_each_symbol - iterate thru all the symbols
  521. *
  522. * @self: struct elf_symtab instance to iterate
  523. * @idx: uint32_t idx
  524. * @sym: GElf_Sym iterator
  525. */
  526. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  527. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  528. idx < nr_syms; \
  529. idx++, gelf_getsym(syms, idx, &sym))
  530. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  531. {
  532. return GELF_ST_TYPE(sym->st_info);
  533. }
  534. static inline int elf_sym__is_function(const GElf_Sym *sym)
  535. {
  536. return elf_sym__type(sym) == STT_FUNC &&
  537. sym->st_name != 0 &&
  538. sym->st_shndx != SHN_UNDEF;
  539. }
  540. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  541. {
  542. return elf_sym__type(sym) == STT_OBJECT &&
  543. sym->st_name != 0 &&
  544. sym->st_shndx != SHN_UNDEF;
  545. }
  546. static inline int elf_sym__is_label(const GElf_Sym *sym)
  547. {
  548. return elf_sym__type(sym) == STT_NOTYPE &&
  549. sym->st_name != 0 &&
  550. sym->st_shndx != SHN_UNDEF &&
  551. sym->st_shndx != SHN_ABS;
  552. }
  553. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  554. const Elf_Data *secstrs)
  555. {
  556. return secstrs->d_buf + shdr->sh_name;
  557. }
  558. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  559. const Elf_Data *secstrs)
  560. {
  561. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  562. }
  563. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  564. const Elf_Data *secstrs)
  565. {
  566. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  567. }
  568. static inline const char *elf_sym__name(const GElf_Sym *sym,
  569. const Elf_Data *symstrs)
  570. {
  571. return symstrs->d_buf + sym->st_name;
  572. }
  573. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  574. GElf_Shdr *shp, const char *name,
  575. size_t *idx)
  576. {
  577. Elf_Scn *sec = NULL;
  578. size_t cnt = 1;
  579. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  580. char *str;
  581. gelf_getshdr(sec, shp);
  582. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  583. if (!strcmp(name, str)) {
  584. if (idx)
  585. *idx = cnt;
  586. break;
  587. }
  588. ++cnt;
  589. }
  590. return sec;
  591. }
  592. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  593. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  594. idx < nr_entries; \
  595. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  596. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  597. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  598. idx < nr_entries; \
  599. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  600. /*
  601. * We need to check if we have a .dynsym, so that we can handle the
  602. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  603. * .dynsym or .symtab).
  604. * And always look at the original dso, not at debuginfo packages, that
  605. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  606. */
  607. static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
  608. symbol_filter_t filter)
  609. {
  610. uint32_t nr_rel_entries, idx;
  611. GElf_Sym sym;
  612. u64 plt_offset;
  613. GElf_Shdr shdr_plt;
  614. struct symbol *f;
  615. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  616. Elf_Data *reldata, *syms, *symstrs;
  617. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  618. size_t dynsym_idx;
  619. GElf_Ehdr ehdr;
  620. char sympltname[1024];
  621. Elf *elf;
  622. int nr = 0, symidx, fd, err = 0;
  623. fd = open(self->long_name, O_RDONLY);
  624. if (fd < 0)
  625. goto out;
  626. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  627. if (elf == NULL)
  628. goto out_close;
  629. if (gelf_getehdr(elf, &ehdr) == NULL)
  630. goto out_elf_end;
  631. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  632. ".dynsym", &dynsym_idx);
  633. if (scn_dynsym == NULL)
  634. goto out_elf_end;
  635. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  636. ".rela.plt", NULL);
  637. if (scn_plt_rel == NULL) {
  638. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  639. ".rel.plt", NULL);
  640. if (scn_plt_rel == NULL)
  641. goto out_elf_end;
  642. }
  643. err = -1;
  644. if (shdr_rel_plt.sh_link != dynsym_idx)
  645. goto out_elf_end;
  646. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  647. goto out_elf_end;
  648. /*
  649. * Fetch the relocation section to find the idxes to the GOT
  650. * and the symbols in the .dynsym they refer to.
  651. */
  652. reldata = elf_getdata(scn_plt_rel, NULL);
  653. if (reldata == NULL)
  654. goto out_elf_end;
  655. syms = elf_getdata(scn_dynsym, NULL);
  656. if (syms == NULL)
  657. goto out_elf_end;
  658. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  659. if (scn_symstrs == NULL)
  660. goto out_elf_end;
  661. symstrs = elf_getdata(scn_symstrs, NULL);
  662. if (symstrs == NULL)
  663. goto out_elf_end;
  664. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  665. plt_offset = shdr_plt.sh_offset;
  666. if (shdr_rel_plt.sh_type == SHT_RELA) {
  667. GElf_Rela pos_mem, *pos;
  668. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  669. nr_rel_entries) {
  670. symidx = GELF_R_SYM(pos->r_info);
  671. plt_offset += shdr_plt.sh_entsize;
  672. gelf_getsym(syms, symidx, &sym);
  673. snprintf(sympltname, sizeof(sympltname),
  674. "%s@plt", elf_sym__name(&sym, symstrs));
  675. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  676. sympltname);
  677. if (!f)
  678. goto out_elf_end;
  679. if (filter && filter(map, f))
  680. symbol__delete(f);
  681. else {
  682. symbols__insert(&self->symbols[map->type], f);
  683. ++nr;
  684. }
  685. }
  686. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  687. GElf_Rel pos_mem, *pos;
  688. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  689. nr_rel_entries) {
  690. symidx = GELF_R_SYM(pos->r_info);
  691. plt_offset += shdr_plt.sh_entsize;
  692. gelf_getsym(syms, symidx, &sym);
  693. snprintf(sympltname, sizeof(sympltname),
  694. "%s@plt", elf_sym__name(&sym, symstrs));
  695. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  696. sympltname);
  697. if (!f)
  698. goto out_elf_end;
  699. if (filter && filter(map, f))
  700. symbol__delete(f);
  701. else {
  702. symbols__insert(&self->symbols[map->type], f);
  703. ++nr;
  704. }
  705. }
  706. }
  707. err = 0;
  708. out_elf_end:
  709. elf_end(elf);
  710. out_close:
  711. close(fd);
  712. if (err == 0)
  713. return nr;
  714. out:
  715. pr_warning("%s: problems reading %s PLT info.\n",
  716. __func__, self->long_name);
  717. return 0;
  718. }
  719. static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
  720. {
  721. switch (type) {
  722. case MAP__FUNCTION:
  723. return elf_sym__is_function(self);
  724. case MAP__VARIABLE:
  725. return elf_sym__is_object(self);
  726. default:
  727. return false;
  728. }
  729. }
  730. static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
  731. {
  732. switch (type) {
  733. case MAP__FUNCTION:
  734. return elf_sec__is_text(self, secstrs);
  735. case MAP__VARIABLE:
  736. return elf_sec__is_data(self, secstrs);
  737. default:
  738. return false;
  739. }
  740. }
  741. static int dso__load_sym(struct dso *self, struct map *map, const char *name,
  742. int fd, symbol_filter_t filter, int kmodule)
  743. {
  744. struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
  745. struct map *curr_map = map;
  746. struct dso *curr_dso = self;
  747. Elf_Data *symstrs, *secstrs;
  748. uint32_t nr_syms;
  749. int err = -1;
  750. uint32_t idx;
  751. GElf_Ehdr ehdr;
  752. GElf_Shdr shdr;
  753. Elf_Data *syms;
  754. GElf_Sym sym;
  755. Elf_Scn *sec, *sec_strndx;
  756. Elf *elf;
  757. int nr = 0;
  758. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  759. if (elf == NULL) {
  760. pr_err("%s: cannot read %s ELF file.\n", __func__, name);
  761. goto out_close;
  762. }
  763. if (gelf_getehdr(elf, &ehdr) == NULL) {
  764. pr_err("%s: cannot get elf header.\n", __func__);
  765. goto out_elf_end;
  766. }
  767. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  768. if (sec == NULL) {
  769. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  770. if (sec == NULL)
  771. goto out_elf_end;
  772. }
  773. syms = elf_getdata(sec, NULL);
  774. if (syms == NULL)
  775. goto out_elf_end;
  776. sec = elf_getscn(elf, shdr.sh_link);
  777. if (sec == NULL)
  778. goto out_elf_end;
  779. symstrs = elf_getdata(sec, NULL);
  780. if (symstrs == NULL)
  781. goto out_elf_end;
  782. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  783. if (sec_strndx == NULL)
  784. goto out_elf_end;
  785. secstrs = elf_getdata(sec_strndx, NULL);
  786. if (secstrs == NULL)
  787. goto out_elf_end;
  788. nr_syms = shdr.sh_size / shdr.sh_entsize;
  789. memset(&sym, 0, sizeof(sym));
  790. if (!self->kernel) {
  791. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  792. elf_section_by_name(elf, &ehdr, &shdr,
  793. ".gnu.prelink_undo",
  794. NULL) != NULL);
  795. } else self->adjust_symbols = 0;
  796. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  797. struct symbol *f;
  798. const char *elf_name = elf_sym__name(&sym, symstrs);
  799. char *demangled = NULL;
  800. int is_label = elf_sym__is_label(&sym);
  801. const char *section_name;
  802. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  803. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  804. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  805. if (!is_label && !elf_sym__is_a(&sym, map->type))
  806. continue;
  807. sec = elf_getscn(elf, sym.st_shndx);
  808. if (!sec)
  809. goto out_elf_end;
  810. gelf_getshdr(sec, &shdr);
  811. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  812. continue;
  813. section_name = elf_sec__name(&shdr, secstrs);
  814. if (self->kernel || kmodule) {
  815. char dso_name[PATH_MAX];
  816. if (strcmp(section_name,
  817. (curr_dso->short_name +
  818. self->short_name_len)) == 0)
  819. goto new_symbol;
  820. if (strcmp(section_name, ".text") == 0) {
  821. curr_map = map;
  822. curr_dso = self;
  823. goto new_symbol;
  824. }
  825. snprintf(dso_name, sizeof(dso_name),
  826. "%s%s", self->short_name, section_name);
  827. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  828. if (curr_map == NULL) {
  829. u64 start = sym.st_value;
  830. if (kmodule)
  831. start += map->start + shdr.sh_offset;
  832. curr_dso = dso__new(dso_name);
  833. if (curr_dso == NULL)
  834. goto out_elf_end;
  835. curr_map = map__new2(start, curr_dso,
  836. map->type);
  837. if (curr_map == NULL) {
  838. dso__delete(curr_dso);
  839. goto out_elf_end;
  840. }
  841. curr_map->map_ip = identity__map_ip;
  842. curr_map->unmap_ip = identity__map_ip;
  843. curr_dso->origin = DSO__ORIG_KERNEL;
  844. map_groups__insert(kmap->kmaps, curr_map);
  845. dsos__add(&dsos__kernel, curr_dso);
  846. dso__set_loaded(curr_dso, map->type);
  847. } else
  848. curr_dso = curr_map->dso;
  849. goto new_symbol;
  850. }
  851. if (curr_dso->adjust_symbols) {
  852. pr_debug4("%s: adjusting symbol: st_value: %#Lx "
  853. "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
  854. (u64)sym.st_value, (u64)shdr.sh_addr,
  855. (u64)shdr.sh_offset);
  856. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  857. }
  858. /*
  859. * We need to figure out if the object was created from C++ sources
  860. * DWARF DW_compile_unit has this, but we don't always have access
  861. * to it...
  862. */
  863. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  864. if (demangled != NULL)
  865. elf_name = demangled;
  866. new_symbol:
  867. f = symbol__new(sym.st_value, sym.st_size, elf_name);
  868. free(demangled);
  869. if (!f)
  870. goto out_elf_end;
  871. if (filter && filter(curr_map, f))
  872. symbol__delete(f);
  873. else {
  874. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  875. nr++;
  876. }
  877. }
  878. /*
  879. * For misannotated, zeroed, ASM function sizes.
  880. */
  881. if (nr > 0) {
  882. symbols__fixup_end(&self->symbols[map->type]);
  883. if (kmap) {
  884. /*
  885. * We need to fixup this here too because we create new
  886. * maps here, for things like vsyscall sections.
  887. */
  888. __map_groups__fixup_end(kmap->kmaps, map->type);
  889. }
  890. }
  891. err = nr;
  892. out_elf_end:
  893. elf_end(elf);
  894. out_close:
  895. return err;
  896. }
  897. static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
  898. {
  899. return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
  900. }
  901. static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  902. {
  903. bool have_build_id = false;
  904. struct dso *pos;
  905. list_for_each_entry(pos, head, node) {
  906. if (with_hits && !pos->hit)
  907. continue;
  908. if (filename__read_build_id(pos->long_name, pos->build_id,
  909. sizeof(pos->build_id)) > 0) {
  910. have_build_id = true;
  911. pos->has_build_id = true;
  912. }
  913. }
  914. return have_build_id;
  915. }
  916. bool dsos__read_build_ids(bool with_hits)
  917. {
  918. bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits),
  919. ubuildids = __dsos__read_build_ids(&dsos__user, with_hits);
  920. return kbuildids || ubuildids;
  921. }
  922. /*
  923. * Align offset to 4 bytes as needed for note name and descriptor data.
  924. */
  925. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  926. int filename__read_build_id(const char *filename, void *bf, size_t size)
  927. {
  928. int fd, err = -1;
  929. GElf_Ehdr ehdr;
  930. GElf_Shdr shdr;
  931. Elf_Data *data;
  932. Elf_Scn *sec;
  933. Elf_Kind ek;
  934. void *ptr;
  935. Elf *elf;
  936. if (size < BUILD_ID_SIZE)
  937. goto out;
  938. fd = open(filename, O_RDONLY);
  939. if (fd < 0)
  940. goto out;
  941. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  942. if (elf == NULL) {
  943. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  944. goto out_close;
  945. }
  946. ek = elf_kind(elf);
  947. if (ek != ELF_K_ELF)
  948. goto out_elf_end;
  949. if (gelf_getehdr(elf, &ehdr) == NULL) {
  950. pr_err("%s: cannot get elf header.\n", __func__);
  951. goto out_elf_end;
  952. }
  953. sec = elf_section_by_name(elf, &ehdr, &shdr,
  954. ".note.gnu.build-id", NULL);
  955. if (sec == NULL) {
  956. sec = elf_section_by_name(elf, &ehdr, &shdr,
  957. ".notes", NULL);
  958. if (sec == NULL)
  959. goto out_elf_end;
  960. }
  961. data = elf_getdata(sec, NULL);
  962. if (data == NULL)
  963. goto out_elf_end;
  964. ptr = data->d_buf;
  965. while (ptr < (data->d_buf + data->d_size)) {
  966. GElf_Nhdr *nhdr = ptr;
  967. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  968. descsz = NOTE_ALIGN(nhdr->n_descsz);
  969. const char *name;
  970. ptr += sizeof(*nhdr);
  971. name = ptr;
  972. ptr += namesz;
  973. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  974. nhdr->n_namesz == sizeof("GNU")) {
  975. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  976. memcpy(bf, ptr, BUILD_ID_SIZE);
  977. err = BUILD_ID_SIZE;
  978. break;
  979. }
  980. }
  981. ptr += descsz;
  982. }
  983. out_elf_end:
  984. elf_end(elf);
  985. out_close:
  986. close(fd);
  987. out:
  988. return err;
  989. }
  990. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  991. {
  992. int fd, err = -1;
  993. if (size < BUILD_ID_SIZE)
  994. goto out;
  995. fd = open(filename, O_RDONLY);
  996. if (fd < 0)
  997. goto out;
  998. while (1) {
  999. char bf[BUFSIZ];
  1000. GElf_Nhdr nhdr;
  1001. int namesz, descsz;
  1002. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1003. break;
  1004. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1005. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1006. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1007. nhdr.n_namesz == sizeof("GNU")) {
  1008. if (read(fd, bf, namesz) != namesz)
  1009. break;
  1010. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1011. if (read(fd, build_id,
  1012. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1013. err = 0;
  1014. break;
  1015. }
  1016. } else if (read(fd, bf, descsz) != descsz)
  1017. break;
  1018. } else {
  1019. int n = namesz + descsz;
  1020. if (read(fd, bf, n) != n)
  1021. break;
  1022. }
  1023. }
  1024. close(fd);
  1025. out:
  1026. return err;
  1027. }
  1028. char dso__symtab_origin(const struct dso *self)
  1029. {
  1030. static const char origin[] = {
  1031. [DSO__ORIG_KERNEL] = 'k',
  1032. [DSO__ORIG_JAVA_JIT] = 'j',
  1033. [DSO__ORIG_BUILD_ID_CACHE] = 'B',
  1034. [DSO__ORIG_FEDORA] = 'f',
  1035. [DSO__ORIG_UBUNTU] = 'u',
  1036. [DSO__ORIG_BUILDID] = 'b',
  1037. [DSO__ORIG_DSO] = 'd',
  1038. [DSO__ORIG_KMODULE] = 'K',
  1039. };
  1040. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  1041. return '!';
  1042. return origin[self->origin];
  1043. }
  1044. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  1045. {
  1046. int size = PATH_MAX;
  1047. char *name;
  1048. u8 build_id[BUILD_ID_SIZE];
  1049. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1050. int ret = -1;
  1051. int fd;
  1052. dso__set_loaded(self, map->type);
  1053. if (self->kernel)
  1054. return dso__load_kernel_sym(self, map, filter);
  1055. name = malloc(size);
  1056. if (!name)
  1057. return -1;
  1058. self->adjust_symbols = 0;
  1059. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  1060. ret = dso__load_perf_map(self, map, filter);
  1061. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  1062. DSO__ORIG_NOT_FOUND;
  1063. return ret;
  1064. }
  1065. self->origin = DSO__ORIG_BUILD_ID_CACHE;
  1066. if (self->has_build_id) {
  1067. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1068. build_id_hex);
  1069. snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
  1070. getenv("HOME"), DEBUG_CACHE_DIR,
  1071. build_id_hex, build_id_hex + 2);
  1072. goto open_file;
  1073. }
  1074. more:
  1075. do {
  1076. self->origin++;
  1077. switch (self->origin) {
  1078. case DSO__ORIG_FEDORA:
  1079. snprintf(name, size, "/usr/lib/debug%s.debug",
  1080. self->long_name);
  1081. break;
  1082. case DSO__ORIG_UBUNTU:
  1083. snprintf(name, size, "/usr/lib/debug%s",
  1084. self->long_name);
  1085. break;
  1086. case DSO__ORIG_BUILDID:
  1087. if (filename__read_build_id(self->long_name, build_id,
  1088. sizeof(build_id))) {
  1089. build_id__sprintf(build_id, sizeof(build_id),
  1090. build_id_hex);
  1091. snprintf(name, size,
  1092. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  1093. build_id_hex, build_id_hex + 2);
  1094. if (self->has_build_id)
  1095. goto compare_build_id;
  1096. break;
  1097. }
  1098. self->origin++;
  1099. /* Fall thru */
  1100. case DSO__ORIG_DSO:
  1101. snprintf(name, size, "%s", self->long_name);
  1102. break;
  1103. default:
  1104. goto out;
  1105. }
  1106. if (self->has_build_id) {
  1107. if (filename__read_build_id(name, build_id,
  1108. sizeof(build_id)) < 0)
  1109. goto more;
  1110. compare_build_id:
  1111. if (!dso__build_id_equal(self, build_id))
  1112. goto more;
  1113. }
  1114. open_file:
  1115. fd = open(name, O_RDONLY);
  1116. } while (fd < 0);
  1117. ret = dso__load_sym(self, map, name, fd, filter, 0);
  1118. close(fd);
  1119. /*
  1120. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  1121. */
  1122. if (!ret)
  1123. goto more;
  1124. if (ret > 0) {
  1125. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  1126. if (nr_plt > 0)
  1127. ret += nr_plt;
  1128. }
  1129. out:
  1130. free(name);
  1131. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  1132. return 0;
  1133. return ret;
  1134. }
  1135. struct map *map_groups__find_by_name(struct map_groups *self,
  1136. enum map_type type, const char *name)
  1137. {
  1138. struct rb_node *nd;
  1139. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  1140. struct map *map = rb_entry(nd, struct map, rb_node);
  1141. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1142. return map;
  1143. }
  1144. return NULL;
  1145. }
  1146. static int dso__kernel_module_get_build_id(struct dso *self)
  1147. {
  1148. char filename[PATH_MAX];
  1149. /*
  1150. * kernel module short names are of the form "[module]" and
  1151. * we need just "module" here.
  1152. */
  1153. const char *name = self->short_name + 1;
  1154. snprintf(filename, sizeof(filename),
  1155. "/sys/module/%.*s/notes/.note.gnu.build-id",
  1156. (int)strlen(name - 1), name);
  1157. if (sysfs__read_build_id(filename, self->build_id,
  1158. sizeof(self->build_id)) == 0)
  1159. self->has_build_id = true;
  1160. return 0;
  1161. }
  1162. static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
  1163. {
  1164. struct dirent *dent;
  1165. DIR *dir = opendir(dirname);
  1166. if (!dir) {
  1167. pr_debug("%s: cannot open %s dir\n", __func__, dirname);
  1168. return -1;
  1169. }
  1170. while ((dent = readdir(dir)) != NULL) {
  1171. char path[PATH_MAX];
  1172. if (dent->d_type == DT_DIR) {
  1173. if (!strcmp(dent->d_name, ".") ||
  1174. !strcmp(dent->d_name, ".."))
  1175. continue;
  1176. snprintf(path, sizeof(path), "%s/%s",
  1177. dirname, dent->d_name);
  1178. if (map_groups__set_modules_path_dir(self, path) < 0)
  1179. goto failure;
  1180. } else {
  1181. char *dot = strrchr(dent->d_name, '.'),
  1182. dso_name[PATH_MAX];
  1183. struct map *map;
  1184. char *long_name;
  1185. if (dot == NULL || strcmp(dot, ".ko"))
  1186. continue;
  1187. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1188. (int)(dot - dent->d_name), dent->d_name);
  1189. strxfrchar(dso_name, '-', '_');
  1190. map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
  1191. if (map == NULL)
  1192. continue;
  1193. snprintf(path, sizeof(path), "%s/%s",
  1194. dirname, dent->d_name);
  1195. long_name = strdup(path);
  1196. if (long_name == NULL)
  1197. goto failure;
  1198. dso__set_long_name(map->dso, long_name);
  1199. dso__kernel_module_get_build_id(map->dso);
  1200. }
  1201. }
  1202. return 0;
  1203. failure:
  1204. closedir(dir);
  1205. return -1;
  1206. }
  1207. static int map_groups__set_modules_path(struct map_groups *self)
  1208. {
  1209. struct utsname uts;
  1210. char modules_path[PATH_MAX];
  1211. if (uname(&uts) < 0)
  1212. return -1;
  1213. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  1214. uts.release);
  1215. return map_groups__set_modules_path_dir(self, modules_path);
  1216. }
  1217. /*
  1218. * Constructor variant for modules (where we know from /proc/modules where
  1219. * they are loaded) and for vmlinux, where only after we load all the
  1220. * symbols we'll know where it starts and ends.
  1221. */
  1222. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1223. {
  1224. struct map *self = zalloc(sizeof(*self) +
  1225. (dso->kernel ? sizeof(struct kmap) : 0));
  1226. if (self != NULL) {
  1227. /*
  1228. * ->end will be filled after we load all the symbols
  1229. */
  1230. map__init(self, type, start, 0, 0, dso);
  1231. }
  1232. return self;
  1233. }
  1234. struct map *map_groups__new_module(struct map_groups *self, u64 start,
  1235. const char *filename)
  1236. {
  1237. struct map *map;
  1238. struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
  1239. if (dso == NULL)
  1240. return NULL;
  1241. map = map__new2(start, dso, MAP__FUNCTION);
  1242. if (map == NULL)
  1243. return NULL;
  1244. dso->origin = DSO__ORIG_KMODULE;
  1245. map_groups__insert(self, map);
  1246. return map;
  1247. }
  1248. static int map_groups__create_modules(struct map_groups *self)
  1249. {
  1250. char *line = NULL;
  1251. size_t n;
  1252. FILE *file = fopen("/proc/modules", "r");
  1253. struct map *map;
  1254. if (file == NULL)
  1255. return -1;
  1256. while (!feof(file)) {
  1257. char name[PATH_MAX];
  1258. u64 start;
  1259. char *sep;
  1260. int line_len;
  1261. line_len = getline(&line, &n, file);
  1262. if (line_len < 0)
  1263. break;
  1264. if (!line)
  1265. goto out_failure;
  1266. line[--line_len] = '\0'; /* \n */
  1267. sep = strrchr(line, 'x');
  1268. if (sep == NULL)
  1269. continue;
  1270. hex2u64(sep + 1, &start);
  1271. sep = strchr(line, ' ');
  1272. if (sep == NULL)
  1273. continue;
  1274. *sep = '\0';
  1275. snprintf(name, sizeof(name), "[%s]", line);
  1276. map = map_groups__new_module(self, start, name);
  1277. if (map == NULL)
  1278. goto out_delete_line;
  1279. dso__kernel_module_get_build_id(map->dso);
  1280. }
  1281. free(line);
  1282. fclose(file);
  1283. return map_groups__set_modules_path(self);
  1284. out_delete_line:
  1285. free(line);
  1286. out_failure:
  1287. return -1;
  1288. }
  1289. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1290. const char *vmlinux, symbol_filter_t filter)
  1291. {
  1292. int err = -1, fd;
  1293. if (self->has_build_id) {
  1294. u8 build_id[BUILD_ID_SIZE];
  1295. if (filename__read_build_id(vmlinux, build_id,
  1296. sizeof(build_id)) < 0) {
  1297. pr_debug("No build_id in %s, ignoring it\n", vmlinux);
  1298. return -1;
  1299. }
  1300. if (!dso__build_id_equal(self, build_id)) {
  1301. char expected_build_id[BUILD_ID_SIZE * 2 + 1],
  1302. vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
  1303. build_id__sprintf(self->build_id,
  1304. sizeof(self->build_id),
  1305. expected_build_id);
  1306. build_id__sprintf(build_id, sizeof(build_id),
  1307. vmlinux_build_id);
  1308. pr_debug("build_id in %s is %s while expected is %s, "
  1309. "ignoring it\n", vmlinux, vmlinux_build_id,
  1310. expected_build_id);
  1311. return -1;
  1312. }
  1313. }
  1314. fd = open(vmlinux, O_RDONLY);
  1315. if (fd < 0)
  1316. return -1;
  1317. dso__set_loaded(self, map->type);
  1318. err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
  1319. close(fd);
  1320. if (err > 0)
  1321. pr_debug("Using %s for symbols\n", vmlinux);
  1322. return err;
  1323. }
  1324. int dso__load_vmlinux_path(struct dso *self, struct map *map,
  1325. symbol_filter_t filter)
  1326. {
  1327. int i, err = 0;
  1328. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1329. vmlinux_path__nr_entries);
  1330. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1331. err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
  1332. if (err > 0) {
  1333. dso__set_long_name(self, strdup(vmlinux_path[i]));
  1334. break;
  1335. }
  1336. }
  1337. return err;
  1338. }
  1339. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1340. symbol_filter_t filter)
  1341. {
  1342. int err;
  1343. const char *kallsyms_filename = NULL;
  1344. char *kallsyms_allocated_filename = NULL;
  1345. /*
  1346. * Step 1: if the user specified a vmlinux filename, use it and only
  1347. * it, reporting errors to the user if it cannot be used.
  1348. *
  1349. * For instance, try to analyse an ARM perf.data file _without_ a
  1350. * build-id, or if the user specifies the wrong path to the right
  1351. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1352. * x86_86 one, on the machine where analysis is being performed, say),
  1353. * or worse, /proc/kallsyms.
  1354. *
  1355. * If the specified file _has_ a build-id and there is a build-id
  1356. * section in the perf.data file, we will still do the expected
  1357. * validation in dso__load_vmlinux and will bail out if they don't
  1358. * match.
  1359. */
  1360. if (symbol_conf.vmlinux_name != NULL) {
  1361. err = dso__load_vmlinux(self, map,
  1362. symbol_conf.vmlinux_name, filter);
  1363. goto out_try_fixup;
  1364. }
  1365. if (vmlinux_path != NULL) {
  1366. err = dso__load_vmlinux_path(self, map, filter);
  1367. if (err > 0)
  1368. goto out_fixup;
  1369. }
  1370. /*
  1371. * Say the kernel DSO was created when processing the build-id header table,
  1372. * we have a build-id, so check if it is the same as the running kernel,
  1373. * using it if it is.
  1374. */
  1375. if (self->has_build_id) {
  1376. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1377. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1378. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1379. sizeof(kallsyms_build_id)) == 0) {
  1380. if (dso__build_id_equal(self, kallsyms_build_id)) {
  1381. kallsyms_filename = "/proc/kallsyms";
  1382. goto do_kallsyms;
  1383. }
  1384. }
  1385. /*
  1386. * Now look if we have it on the build-id cache in
  1387. * $HOME/.debug/[kernel.kallsyms].
  1388. */
  1389. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1390. sbuild_id);
  1391. if (asprintf(&kallsyms_allocated_filename,
  1392. "%s/.debug/[kernel.kallsyms]/%s",
  1393. getenv("HOME"), sbuild_id) == -1) {
  1394. pr_err("Not enough memory for kallsyms file lookup\n");
  1395. return -1;
  1396. }
  1397. kallsyms_filename = kallsyms_allocated_filename;
  1398. if (access(kallsyms_filename, F_OK)) {
  1399. pr_err("No kallsyms or vmlinux with build-id %s "
  1400. "was found\n", sbuild_id);
  1401. free(kallsyms_allocated_filename);
  1402. return -1;
  1403. }
  1404. } else {
  1405. /*
  1406. * Last resort, if we don't have a build-id and couldn't find
  1407. * any vmlinux file, try the running kernel kallsyms table.
  1408. */
  1409. kallsyms_filename = "/proc/kallsyms";
  1410. }
  1411. do_kallsyms:
  1412. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1413. if (err > 0)
  1414. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1415. free(kallsyms_allocated_filename);
  1416. out_try_fixup:
  1417. if (err > 0) {
  1418. out_fixup:
  1419. if (kallsyms_filename != NULL)
  1420. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1421. map__fixup_start(map);
  1422. map__fixup_end(map);
  1423. }
  1424. return err;
  1425. }
  1426. LIST_HEAD(dsos__user);
  1427. LIST_HEAD(dsos__kernel);
  1428. static void dsos__add(struct list_head *head, struct dso *dso)
  1429. {
  1430. list_add_tail(&dso->node, head);
  1431. }
  1432. static struct dso *dsos__find(struct list_head *head, const char *name)
  1433. {
  1434. struct dso *pos;
  1435. list_for_each_entry(pos, head, node)
  1436. if (strcmp(pos->long_name, name) == 0)
  1437. return pos;
  1438. return NULL;
  1439. }
  1440. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1441. {
  1442. struct dso *dso = dsos__find(head, name);
  1443. if (!dso) {
  1444. dso = dso__new(name);
  1445. if (dso != NULL) {
  1446. dsos__add(head, dso);
  1447. dso__set_basename(dso);
  1448. }
  1449. }
  1450. return dso;
  1451. }
  1452. static void __dsos__fprintf(struct list_head *head, FILE *fp)
  1453. {
  1454. struct dso *pos;
  1455. list_for_each_entry(pos, head, node) {
  1456. int i;
  1457. for (i = 0; i < MAP__NR_TYPES; ++i)
  1458. dso__fprintf(pos, i, fp);
  1459. }
  1460. }
  1461. void dsos__fprintf(FILE *fp)
  1462. {
  1463. __dsos__fprintf(&dsos__kernel, fp);
  1464. __dsos__fprintf(&dsos__user, fp);
  1465. }
  1466. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1467. bool with_hits)
  1468. {
  1469. struct dso *pos;
  1470. size_t ret = 0;
  1471. list_for_each_entry(pos, head, node) {
  1472. if (with_hits && !pos->hit)
  1473. continue;
  1474. ret += dso__fprintf_buildid(pos, fp);
  1475. ret += fprintf(fp, " %s\n", pos->long_name);
  1476. }
  1477. return ret;
  1478. }
  1479. size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
  1480. {
  1481. return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
  1482. __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
  1483. }
  1484. struct dso *dso__new_kernel(const char *name)
  1485. {
  1486. struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
  1487. if (self != NULL) {
  1488. dso__set_short_name(self, "[kernel]");
  1489. self->kernel = 1;
  1490. }
  1491. return self;
  1492. }
  1493. void dso__read_running_kernel_build_id(struct dso *self)
  1494. {
  1495. if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
  1496. sizeof(self->build_id)) == 0)
  1497. self->has_build_id = true;
  1498. }
  1499. static struct dso *dsos__create_kernel(const char *vmlinux)
  1500. {
  1501. struct dso *kernel = dso__new_kernel(vmlinux);
  1502. if (kernel != NULL) {
  1503. dso__read_running_kernel_build_id(kernel);
  1504. dsos__add(&dsos__kernel, kernel);
  1505. }
  1506. return kernel;
  1507. }
  1508. int __map_groups__create_kernel_maps(struct map_groups *self,
  1509. struct map *vmlinux_maps[MAP__NR_TYPES],
  1510. struct dso *kernel)
  1511. {
  1512. enum map_type type;
  1513. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1514. struct kmap *kmap;
  1515. vmlinux_maps[type] = map__new2(0, kernel, type);
  1516. if (vmlinux_maps[type] == NULL)
  1517. return -1;
  1518. vmlinux_maps[type]->map_ip =
  1519. vmlinux_maps[type]->unmap_ip = identity__map_ip;
  1520. kmap = map__kmap(vmlinux_maps[type]);
  1521. kmap->kmaps = self;
  1522. map_groups__insert(self, vmlinux_maps[type]);
  1523. }
  1524. return 0;
  1525. }
  1526. static void vmlinux_path__exit(void)
  1527. {
  1528. while (--vmlinux_path__nr_entries >= 0) {
  1529. free(vmlinux_path[vmlinux_path__nr_entries]);
  1530. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1531. }
  1532. free(vmlinux_path);
  1533. vmlinux_path = NULL;
  1534. }
  1535. static int vmlinux_path__init(void)
  1536. {
  1537. struct utsname uts;
  1538. char bf[PATH_MAX];
  1539. if (uname(&uts) < 0)
  1540. return -1;
  1541. vmlinux_path = malloc(sizeof(char *) * 5);
  1542. if (vmlinux_path == NULL)
  1543. return -1;
  1544. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1545. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1546. goto out_fail;
  1547. ++vmlinux_path__nr_entries;
  1548. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1549. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1550. goto out_fail;
  1551. ++vmlinux_path__nr_entries;
  1552. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1553. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1554. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1555. goto out_fail;
  1556. ++vmlinux_path__nr_entries;
  1557. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1558. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1559. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1560. goto out_fail;
  1561. ++vmlinux_path__nr_entries;
  1562. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1563. uts.release);
  1564. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1565. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1566. goto out_fail;
  1567. ++vmlinux_path__nr_entries;
  1568. return 0;
  1569. out_fail:
  1570. vmlinux_path__exit();
  1571. return -1;
  1572. }
  1573. static int setup_list(struct strlist **list, const char *list_str,
  1574. const char *list_name)
  1575. {
  1576. if (list_str == NULL)
  1577. return 0;
  1578. *list = strlist__new(true, list_str);
  1579. if (!*list) {
  1580. pr_err("problems parsing %s list\n", list_name);
  1581. return -1;
  1582. }
  1583. return 0;
  1584. }
  1585. int symbol__init(void)
  1586. {
  1587. elf_version(EV_CURRENT);
  1588. if (symbol_conf.sort_by_name)
  1589. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1590. sizeof(struct symbol));
  1591. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1592. return -1;
  1593. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1594. pr_err("'.' is the only non valid --field-separator argument\n");
  1595. return -1;
  1596. }
  1597. if (setup_list(&symbol_conf.dso_list,
  1598. symbol_conf.dso_list_str, "dso") < 0)
  1599. return -1;
  1600. if (setup_list(&symbol_conf.comm_list,
  1601. symbol_conf.comm_list_str, "comm") < 0)
  1602. goto out_free_dso_list;
  1603. if (setup_list(&symbol_conf.sym_list,
  1604. symbol_conf.sym_list_str, "symbol") < 0)
  1605. goto out_free_comm_list;
  1606. return 0;
  1607. out_free_dso_list:
  1608. strlist__delete(symbol_conf.dso_list);
  1609. out_free_comm_list:
  1610. strlist__delete(symbol_conf.comm_list);
  1611. return -1;
  1612. }
  1613. int map_groups__create_kernel_maps(struct map_groups *self,
  1614. struct map *vmlinux_maps[MAP__NR_TYPES])
  1615. {
  1616. struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
  1617. if (kernel == NULL)
  1618. return -1;
  1619. if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
  1620. return -1;
  1621. if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
  1622. pr_debug("Problems creating module maps, continuing anyway...\n");
  1623. /*
  1624. * Now that we have all the maps created, just set the ->end of them:
  1625. */
  1626. map_groups__fixup_end(self);
  1627. return 0;
  1628. }