symbol.c 43 KB

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