symbol.c 41 KB

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