symbol.c 44 KB

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