symbol.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "sort.h"
  4. #include "string.h"
  5. #include "symbol.h"
  6. #include "thread.h"
  7. #include "debug.h"
  8. #include <asm/bug.h>
  9. #include <libelf.h>
  10. #include <gelf.h>
  11. #include <elf.h>
  12. #include <limits.h>
  13. #include <sys/utsname.h>
  14. #ifndef NT_GNU_BUILD_ID
  15. #define NT_GNU_BUILD_ID 3
  16. #endif
  17. enum dso_origin {
  18. DSO__ORIG_KERNEL = 0,
  19. DSO__ORIG_JAVA_JIT,
  20. DSO__ORIG_BUILD_ID_CACHE,
  21. DSO__ORIG_FEDORA,
  22. DSO__ORIG_UBUNTU,
  23. DSO__ORIG_BUILDID,
  24. DSO__ORIG_DSO,
  25. DSO__ORIG_KMODULE,
  26. DSO__ORIG_NOT_FOUND,
  27. };
  28. static void dsos__add(struct list_head *head, struct dso *dso);
  29. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  30. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  31. symbol_filter_t filter);
  32. static int vmlinux_path__nr_entries;
  33. static char **vmlinux_path;
  34. struct symbol_conf symbol_conf = {
  35. .exclude_other = true,
  36. .use_modules = true,
  37. .try_vmlinux_path = true,
  38. };
  39. bool dso__loaded(const struct dso *self, enum map_type type)
  40. {
  41. return self->loaded & (1 << type);
  42. }
  43. bool dso__sorted_by_name(const struct dso *self, enum map_type type)
  44. {
  45. return self->sorted_by_name & (1 << type);
  46. }
  47. static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
  48. {
  49. self->sorted_by_name |= (1 << type);
  50. }
  51. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  52. {
  53. switch (map_type) {
  54. case MAP__FUNCTION:
  55. return symbol_type == 'T' || symbol_type == 'W';
  56. case MAP__VARIABLE:
  57. return symbol_type == 'D' || symbol_type == 'd';
  58. default:
  59. return false;
  60. }
  61. }
  62. static void symbols__fixup_end(struct rb_root *self)
  63. {
  64. struct rb_node *nd, *prevnd = rb_first(self);
  65. struct symbol *curr, *prev;
  66. if (prevnd == NULL)
  67. return;
  68. curr = rb_entry(prevnd, struct symbol, rb_node);
  69. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  70. prev = curr;
  71. curr = rb_entry(nd, struct symbol, rb_node);
  72. if (prev->end == prev->start)
  73. prev->end = curr->start - 1;
  74. }
  75. /* Last entry */
  76. if (curr->end == curr->start)
  77. curr->end = roundup(curr->start, 4096);
  78. }
  79. static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
  80. {
  81. struct map *prev, *curr;
  82. struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
  83. if (prevnd == NULL)
  84. return;
  85. curr = rb_entry(prevnd, struct map, rb_node);
  86. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  87. prev = curr;
  88. curr = rb_entry(nd, struct map, rb_node);
  89. prev->end = curr->start - 1;
  90. }
  91. /*
  92. * We still haven't the actual symbols, so guess the
  93. * last map final address.
  94. */
  95. curr->end = ~0UL;
  96. }
  97. static void map_groups__fixup_end(struct map_groups *self)
  98. {
  99. int i;
  100. for (i = 0; i < MAP__NR_TYPES; ++i)
  101. __map_groups__fixup_end(self, i);
  102. }
  103. static struct symbol *symbol__new(u64 start, u64 len, const char *name)
  104. {
  105. size_t namelen = strlen(name) + 1;
  106. struct symbol *self = zalloc(symbol_conf.priv_size +
  107. sizeof(*self) + namelen);
  108. if (self == NULL)
  109. return NULL;
  110. if (symbol_conf.priv_size)
  111. self = ((void *)self) + symbol_conf.priv_size;
  112. self->start = start;
  113. self->end = len ? start + len - 1 : start;
  114. pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
  115. memcpy(self->name, name, namelen);
  116. return self;
  117. }
  118. void symbol__delete(struct symbol *self)
  119. {
  120. free(((void *)self) - symbol_conf.priv_size);
  121. }
  122. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  123. {
  124. return fprintf(fp, " %llx-%llx %s\n",
  125. self->start, self->end, self->name);
  126. }
  127. void dso__set_long_name(struct dso *self, char *name)
  128. {
  129. if (name == NULL)
  130. return;
  131. self->long_name = name;
  132. self->long_name_len = strlen(name);
  133. }
  134. static void dso__set_basename(struct dso *self)
  135. {
  136. self->short_name = basename(self->long_name);
  137. }
  138. struct dso *dso__new(const char *name)
  139. {
  140. struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
  141. if (self != NULL) {
  142. int i;
  143. strcpy(self->name, name);
  144. dso__set_long_name(self, self->name);
  145. self->short_name = self->name;
  146. for (i = 0; i < MAP__NR_TYPES; ++i)
  147. self->symbols[i] = self->symbol_names[i] = RB_ROOT;
  148. self->slen_calculated = 0;
  149. self->origin = DSO__ORIG_NOT_FOUND;
  150. self->loaded = 0;
  151. self->sorted_by_name = 0;
  152. self->has_build_id = 0;
  153. }
  154. return self;
  155. }
  156. static void symbols__delete(struct rb_root *self)
  157. {
  158. struct symbol *pos;
  159. struct rb_node *next = rb_first(self);
  160. while (next) {
  161. pos = rb_entry(next, struct symbol, rb_node);
  162. next = rb_next(&pos->rb_node);
  163. rb_erase(&pos->rb_node, self);
  164. symbol__delete(pos);
  165. }
  166. }
  167. void dso__delete(struct dso *self)
  168. {
  169. int i;
  170. for (i = 0; i < MAP__NR_TYPES; ++i)
  171. symbols__delete(&self->symbols[i]);
  172. if (self->long_name != self->name)
  173. free(self->long_name);
  174. free(self);
  175. }
  176. void dso__set_build_id(struct dso *self, void *build_id)
  177. {
  178. memcpy(self->build_id, build_id, sizeof(self->build_id));
  179. self->has_build_id = 1;
  180. }
  181. static void symbols__insert(struct rb_root *self, struct symbol *sym)
  182. {
  183. struct rb_node **p = &self->rb_node;
  184. struct rb_node *parent = NULL;
  185. const u64 ip = sym->start;
  186. struct symbol *s;
  187. while (*p != NULL) {
  188. parent = *p;
  189. s = rb_entry(parent, struct symbol, rb_node);
  190. if (ip < s->start)
  191. p = &(*p)->rb_left;
  192. else
  193. p = &(*p)->rb_right;
  194. }
  195. rb_link_node(&sym->rb_node, parent, p);
  196. rb_insert_color(&sym->rb_node, self);
  197. }
  198. static struct symbol *symbols__find(struct rb_root *self, u64 ip)
  199. {
  200. struct rb_node *n;
  201. if (self == NULL)
  202. return NULL;
  203. n = self->rb_node;
  204. while (n) {
  205. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  206. if (ip < s->start)
  207. n = n->rb_left;
  208. else if (ip > s->end)
  209. n = n->rb_right;
  210. else
  211. return s;
  212. }
  213. return NULL;
  214. }
  215. struct symbol_name_rb_node {
  216. struct rb_node rb_node;
  217. struct symbol sym;
  218. };
  219. static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
  220. {
  221. struct rb_node **p = &self->rb_node;
  222. struct rb_node *parent = NULL;
  223. struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
  224. while (*p != NULL) {
  225. parent = *p;
  226. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  227. if (strcmp(sym->name, s->sym.name) < 0)
  228. p = &(*p)->rb_left;
  229. else
  230. p = &(*p)->rb_right;
  231. }
  232. rb_link_node(&symn->rb_node, parent, p);
  233. rb_insert_color(&symn->rb_node, self);
  234. }
  235. static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
  236. {
  237. struct rb_node *nd;
  238. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  239. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  240. symbols__insert_by_name(self, pos);
  241. }
  242. }
  243. static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
  244. {
  245. struct rb_node *n;
  246. if (self == NULL)
  247. return NULL;
  248. n = self->rb_node;
  249. while (n) {
  250. struct symbol_name_rb_node *s;
  251. int cmp;
  252. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  253. cmp = strcmp(name, s->sym.name);
  254. if (cmp < 0)
  255. n = n->rb_left;
  256. else if (cmp > 0)
  257. n = n->rb_right;
  258. else
  259. return &s->sym;
  260. }
  261. return NULL;
  262. }
  263. struct symbol *dso__find_symbol(struct dso *self,
  264. enum map_type type, u64 addr)
  265. {
  266. return symbols__find(&self->symbols[type], addr);
  267. }
  268. struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
  269. const char *name)
  270. {
  271. return symbols__find_by_name(&self->symbol_names[type], name);
  272. }
  273. void dso__sort_by_name(struct dso *self, enum map_type type)
  274. {
  275. dso__set_sorted_by_name(self, type);
  276. return symbols__sort_by_name(&self->symbol_names[type],
  277. &self->symbols[type]);
  278. }
  279. int build_id__sprintf(const u8 *self, int len, char *bf)
  280. {
  281. char *bid = bf;
  282. const u8 *raw = self;
  283. int i;
  284. for (i = 0; i < len; ++i) {
  285. sprintf(bid, "%02x", *raw);
  286. ++raw;
  287. bid += 2;
  288. }
  289. return raw - self;
  290. }
  291. size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
  292. {
  293. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  294. build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
  295. return fprintf(fp, "%s", sbuild_id);
  296. }
  297. size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
  298. {
  299. struct rb_node *nd;
  300. size_t ret = fprintf(fp, "dso: %s (", self->short_name);
  301. if (self->short_name != self->long_name)
  302. ret += fprintf(fp, "%s, ", self->long_name);
  303. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  304. self->loaded ? "" : "NOT ");
  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 (curr_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->type);
  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. dso__set_loaded(curr_dso, map->type);
  840. } else
  841. curr_dso = curr_map->dso;
  842. goto new_symbol;
  843. }
  844. if (curr_dso->adjust_symbols) {
  845. pr_debug4("%s: adjusting symbol: st_value: %#Lx "
  846. "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
  847. (u64)sym.st_value, (u64)shdr.sh_addr,
  848. (u64)shdr.sh_offset);
  849. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  850. }
  851. /*
  852. * We need to figure out if the object was created from C++ sources
  853. * DWARF DW_compile_unit has this, but we don't always have access
  854. * to it...
  855. */
  856. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  857. if (demangled != NULL)
  858. elf_name = demangled;
  859. new_symbol:
  860. f = symbol__new(sym.st_value, sym.st_size, elf_name);
  861. free(demangled);
  862. if (!f)
  863. goto out_elf_end;
  864. if (filter && filter(curr_map, f))
  865. symbol__delete(f);
  866. else {
  867. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  868. nr++;
  869. }
  870. }
  871. /*
  872. * For misannotated, zeroed, ASM function sizes.
  873. */
  874. if (nr > 0) {
  875. symbols__fixup_end(&self->symbols[map->type]);
  876. if (kmap) {
  877. /*
  878. * We need to fixup this here too because we create new
  879. * maps here, for things like vsyscall sections.
  880. */
  881. __map_groups__fixup_end(kmap->kmaps, map->type);
  882. }
  883. }
  884. err = nr;
  885. out_elf_end:
  886. elf_end(elf);
  887. out_close:
  888. return err;
  889. }
  890. static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
  891. {
  892. return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
  893. }
  894. static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  895. {
  896. bool have_build_id = false;
  897. struct dso *pos;
  898. list_for_each_entry(pos, head, node) {
  899. if (with_hits && !pos->hit)
  900. continue;
  901. if (filename__read_build_id(pos->long_name, pos->build_id,
  902. sizeof(pos->build_id)) > 0) {
  903. have_build_id = true;
  904. pos->has_build_id = true;
  905. }
  906. }
  907. return have_build_id;
  908. }
  909. bool dsos__read_build_ids(bool with_hits)
  910. {
  911. bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits),
  912. ubuildids = __dsos__read_build_ids(&dsos__user, with_hits);
  913. return kbuildids || ubuildids;
  914. }
  915. /*
  916. * Align offset to 4 bytes as needed for note name and descriptor data.
  917. */
  918. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  919. int filename__read_build_id(const char *filename, void *bf, size_t size)
  920. {
  921. int fd, err = -1;
  922. GElf_Ehdr ehdr;
  923. GElf_Shdr shdr;
  924. Elf_Data *data;
  925. Elf_Scn *sec;
  926. Elf_Kind ek;
  927. void *ptr;
  928. Elf *elf;
  929. if (size < BUILD_ID_SIZE)
  930. goto out;
  931. fd = open(filename, O_RDONLY);
  932. if (fd < 0)
  933. goto out;
  934. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  935. if (elf == NULL) {
  936. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  937. goto out_close;
  938. }
  939. ek = elf_kind(elf);
  940. if (ek != ELF_K_ELF)
  941. goto out_elf_end;
  942. if (gelf_getehdr(elf, &ehdr) == NULL) {
  943. pr_err("%s: cannot get elf header.\n", __func__);
  944. goto out_elf_end;
  945. }
  946. sec = elf_section_by_name(elf, &ehdr, &shdr,
  947. ".note.gnu.build-id", NULL);
  948. if (sec == NULL) {
  949. sec = elf_section_by_name(elf, &ehdr, &shdr,
  950. ".notes", NULL);
  951. if (sec == NULL)
  952. goto out_elf_end;
  953. }
  954. data = elf_getdata(sec, NULL);
  955. if (data == NULL)
  956. goto out_elf_end;
  957. ptr = data->d_buf;
  958. while (ptr < (data->d_buf + data->d_size)) {
  959. GElf_Nhdr *nhdr = ptr;
  960. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  961. descsz = NOTE_ALIGN(nhdr->n_descsz);
  962. const char *name;
  963. ptr += sizeof(*nhdr);
  964. name = ptr;
  965. ptr += namesz;
  966. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  967. nhdr->n_namesz == sizeof("GNU")) {
  968. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  969. memcpy(bf, ptr, BUILD_ID_SIZE);
  970. err = BUILD_ID_SIZE;
  971. break;
  972. }
  973. }
  974. ptr += descsz;
  975. }
  976. out_elf_end:
  977. elf_end(elf);
  978. out_close:
  979. close(fd);
  980. out:
  981. return err;
  982. }
  983. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  984. {
  985. int fd, err = -1;
  986. if (size < BUILD_ID_SIZE)
  987. goto out;
  988. fd = open(filename, O_RDONLY);
  989. if (fd < 0)
  990. goto out;
  991. while (1) {
  992. char bf[BUFSIZ];
  993. GElf_Nhdr nhdr;
  994. int namesz, descsz;
  995. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  996. break;
  997. namesz = NOTE_ALIGN(nhdr.n_namesz);
  998. descsz = NOTE_ALIGN(nhdr.n_descsz);
  999. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1000. nhdr.n_namesz == sizeof("GNU")) {
  1001. if (read(fd, bf, namesz) != namesz)
  1002. break;
  1003. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1004. if (read(fd, build_id,
  1005. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1006. err = 0;
  1007. break;
  1008. }
  1009. } else if (read(fd, bf, descsz) != descsz)
  1010. break;
  1011. } else {
  1012. int n = namesz + descsz;
  1013. if (read(fd, bf, n) != n)
  1014. break;
  1015. }
  1016. }
  1017. close(fd);
  1018. out:
  1019. return err;
  1020. }
  1021. char dso__symtab_origin(const struct dso *self)
  1022. {
  1023. static const char origin[] = {
  1024. [DSO__ORIG_KERNEL] = 'k',
  1025. [DSO__ORIG_JAVA_JIT] = 'j',
  1026. [DSO__ORIG_BUILD_ID_CACHE] = 'B',
  1027. [DSO__ORIG_FEDORA] = 'f',
  1028. [DSO__ORIG_UBUNTU] = 'u',
  1029. [DSO__ORIG_BUILDID] = 'b',
  1030. [DSO__ORIG_DSO] = 'd',
  1031. [DSO__ORIG_KMODULE] = 'K',
  1032. };
  1033. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  1034. return '!';
  1035. return origin[self->origin];
  1036. }
  1037. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  1038. {
  1039. int size = PATH_MAX;
  1040. char *name;
  1041. u8 build_id[BUILD_ID_SIZE];
  1042. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1043. int ret = -1;
  1044. int fd;
  1045. dso__set_loaded(self, map->type);
  1046. if (self->kernel)
  1047. return dso__load_kernel_sym(self, map, filter);
  1048. name = malloc(size);
  1049. if (!name)
  1050. return -1;
  1051. self->adjust_symbols = 0;
  1052. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  1053. ret = dso__load_perf_map(self, map, filter);
  1054. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  1055. DSO__ORIG_NOT_FOUND;
  1056. return ret;
  1057. }
  1058. self->origin = DSO__ORIG_BUILD_ID_CACHE;
  1059. if (self->has_build_id) {
  1060. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1061. build_id_hex);
  1062. snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
  1063. getenv("HOME"), DEBUG_CACHE_DIR,
  1064. build_id_hex, build_id_hex + 2);
  1065. goto open_file;
  1066. }
  1067. more:
  1068. do {
  1069. self->origin++;
  1070. switch (self->origin) {
  1071. case DSO__ORIG_FEDORA:
  1072. snprintf(name, size, "/usr/lib/debug%s.debug",
  1073. self->long_name);
  1074. break;
  1075. case DSO__ORIG_UBUNTU:
  1076. snprintf(name, size, "/usr/lib/debug%s",
  1077. self->long_name);
  1078. break;
  1079. case DSO__ORIG_BUILDID:
  1080. if (filename__read_build_id(self->long_name, build_id,
  1081. sizeof(build_id))) {
  1082. build_id__sprintf(build_id, sizeof(build_id),
  1083. build_id_hex);
  1084. snprintf(name, size,
  1085. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  1086. build_id_hex, build_id_hex + 2);
  1087. if (self->has_build_id)
  1088. goto compare_build_id;
  1089. break;
  1090. }
  1091. self->origin++;
  1092. /* Fall thru */
  1093. case DSO__ORIG_DSO:
  1094. snprintf(name, size, "%s", self->long_name);
  1095. break;
  1096. default:
  1097. goto out;
  1098. }
  1099. if (self->has_build_id) {
  1100. if (filename__read_build_id(name, build_id,
  1101. sizeof(build_id)) < 0)
  1102. goto more;
  1103. compare_build_id:
  1104. if (!dso__build_id_equal(self, build_id))
  1105. goto more;
  1106. }
  1107. open_file:
  1108. fd = open(name, O_RDONLY);
  1109. } while (fd < 0);
  1110. ret = dso__load_sym(self, map, name, fd, filter, 0);
  1111. close(fd);
  1112. /*
  1113. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  1114. */
  1115. if (!ret)
  1116. goto more;
  1117. if (ret > 0) {
  1118. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  1119. if (nr_plt > 0)
  1120. ret += nr_plt;
  1121. }
  1122. out:
  1123. free(name);
  1124. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  1125. return 0;
  1126. return ret;
  1127. }
  1128. struct map *map_groups__find_by_name(struct map_groups *self,
  1129. enum map_type type, const char *name)
  1130. {
  1131. struct rb_node *nd;
  1132. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  1133. struct map *map = rb_entry(nd, struct map, rb_node);
  1134. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1135. return map;
  1136. }
  1137. return NULL;
  1138. }
  1139. static int dso__kernel_module_get_build_id(struct dso *self)
  1140. {
  1141. char filename[PATH_MAX];
  1142. /*
  1143. * kernel module short names are of the form "[module]" and
  1144. * we need just "module" here.
  1145. */
  1146. const char *name = self->short_name + 1;
  1147. snprintf(filename, sizeof(filename),
  1148. "/sys/module/%.*s/notes/.note.gnu.build-id",
  1149. (int)strlen(name - 1), name);
  1150. if (sysfs__read_build_id(filename, self->build_id,
  1151. sizeof(self->build_id)) == 0)
  1152. self->has_build_id = true;
  1153. return 0;
  1154. }
  1155. static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
  1156. {
  1157. struct dirent *dent;
  1158. DIR *dir = opendir(dirname);
  1159. if (!dir) {
  1160. pr_debug("%s: cannot open %s dir\n", __func__, dirname);
  1161. return -1;
  1162. }
  1163. while ((dent = readdir(dir)) != NULL) {
  1164. char path[PATH_MAX];
  1165. if (dent->d_type == DT_DIR) {
  1166. if (!strcmp(dent->d_name, ".") ||
  1167. !strcmp(dent->d_name, ".."))
  1168. continue;
  1169. snprintf(path, sizeof(path), "%s/%s",
  1170. dirname, dent->d_name);
  1171. if (map_groups__set_modules_path_dir(self, path) < 0)
  1172. goto failure;
  1173. } else {
  1174. char *dot = strrchr(dent->d_name, '.'),
  1175. dso_name[PATH_MAX];
  1176. struct map *map;
  1177. char *long_name;
  1178. if (dot == NULL || strcmp(dot, ".ko"))
  1179. continue;
  1180. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1181. (int)(dot - dent->d_name), dent->d_name);
  1182. strxfrchar(dso_name, '-', '_');
  1183. map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
  1184. if (map == NULL)
  1185. continue;
  1186. snprintf(path, sizeof(path), "%s/%s",
  1187. dirname, dent->d_name);
  1188. long_name = strdup(path);
  1189. if (long_name == NULL)
  1190. goto failure;
  1191. dso__set_long_name(map->dso, long_name);
  1192. dso__kernel_module_get_build_id(map->dso);
  1193. }
  1194. }
  1195. return 0;
  1196. failure:
  1197. closedir(dir);
  1198. return -1;
  1199. }
  1200. static int map_groups__set_modules_path(struct map_groups *self)
  1201. {
  1202. struct utsname uts;
  1203. char modules_path[PATH_MAX];
  1204. if (uname(&uts) < 0)
  1205. return -1;
  1206. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  1207. uts.release);
  1208. return map_groups__set_modules_path_dir(self, modules_path);
  1209. }
  1210. /*
  1211. * Constructor variant for modules (where we know from /proc/modules where
  1212. * they are loaded) and for vmlinux, where only after we load all the
  1213. * symbols we'll know where it starts and ends.
  1214. */
  1215. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1216. {
  1217. struct map *self = zalloc(sizeof(*self) +
  1218. (dso->kernel ? sizeof(struct kmap) : 0));
  1219. if (self != NULL) {
  1220. /*
  1221. * ->end will be filled after we load all the symbols
  1222. */
  1223. map__init(self, type, start, 0, 0, dso);
  1224. }
  1225. return self;
  1226. }
  1227. struct map *map_groups__new_module(struct map_groups *self, u64 start,
  1228. const char *filename)
  1229. {
  1230. struct map *map;
  1231. struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
  1232. if (dso == NULL)
  1233. return NULL;
  1234. map = map__new2(start, dso, MAP__FUNCTION);
  1235. if (map == NULL)
  1236. return NULL;
  1237. dso->origin = DSO__ORIG_KMODULE;
  1238. map_groups__insert(self, map);
  1239. return map;
  1240. }
  1241. static int map_groups__create_modules(struct map_groups *self)
  1242. {
  1243. char *line = NULL;
  1244. size_t n;
  1245. FILE *file = fopen("/proc/modules", "r");
  1246. struct map *map;
  1247. if (file == NULL)
  1248. return -1;
  1249. while (!feof(file)) {
  1250. char name[PATH_MAX];
  1251. u64 start;
  1252. char *sep;
  1253. int line_len;
  1254. line_len = getline(&line, &n, file);
  1255. if (line_len < 0)
  1256. break;
  1257. if (!line)
  1258. goto out_failure;
  1259. line[--line_len] = '\0'; /* \n */
  1260. sep = strrchr(line, 'x');
  1261. if (sep == NULL)
  1262. continue;
  1263. hex2u64(sep + 1, &start);
  1264. sep = strchr(line, ' ');
  1265. if (sep == NULL)
  1266. continue;
  1267. *sep = '\0';
  1268. snprintf(name, sizeof(name), "[%s]", line);
  1269. map = map_groups__new_module(self, start, name);
  1270. if (map == NULL)
  1271. goto out_delete_line;
  1272. dso__kernel_module_get_build_id(map->dso);
  1273. }
  1274. free(line);
  1275. fclose(file);
  1276. return map_groups__set_modules_path(self);
  1277. out_delete_line:
  1278. free(line);
  1279. out_failure:
  1280. return -1;
  1281. }
  1282. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1283. const char *vmlinux, symbol_filter_t filter)
  1284. {
  1285. int err = -1, fd;
  1286. if (self->has_build_id) {
  1287. u8 build_id[BUILD_ID_SIZE];
  1288. if (filename__read_build_id(vmlinux, build_id,
  1289. sizeof(build_id)) < 0) {
  1290. pr_debug("No build_id in %s, ignoring it\n", vmlinux);
  1291. return -1;
  1292. }
  1293. if (!dso__build_id_equal(self, build_id)) {
  1294. char expected_build_id[BUILD_ID_SIZE * 2 + 1],
  1295. vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
  1296. build_id__sprintf(self->build_id,
  1297. sizeof(self->build_id),
  1298. expected_build_id);
  1299. build_id__sprintf(build_id, sizeof(build_id),
  1300. vmlinux_build_id);
  1301. pr_debug("build_id in %s is %s while expected is %s, "
  1302. "ignoring it\n", vmlinux, vmlinux_build_id,
  1303. expected_build_id);
  1304. return -1;
  1305. }
  1306. }
  1307. fd = open(vmlinux, O_RDONLY);
  1308. if (fd < 0)
  1309. return -1;
  1310. dso__set_loaded(self, map->type);
  1311. err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
  1312. close(fd);
  1313. if (err > 0)
  1314. pr_debug("Using %s for symbols\n", vmlinux);
  1315. return err;
  1316. }
  1317. int dso__load_vmlinux_path(struct dso *self, struct map *map,
  1318. symbol_filter_t filter)
  1319. {
  1320. int i, err = 0;
  1321. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1322. vmlinux_path__nr_entries);
  1323. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1324. err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
  1325. if (err > 0) {
  1326. dso__set_long_name(self, strdup(vmlinux_path[i]));
  1327. break;
  1328. }
  1329. }
  1330. return err;
  1331. }
  1332. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1333. symbol_filter_t filter)
  1334. {
  1335. int err;
  1336. const char *kallsyms_filename = NULL;
  1337. char *kallsyms_allocated_filename = NULL;
  1338. /*
  1339. * Step 1: if the user specified a vmlinux filename, use it and only
  1340. * it, reporting errors to the user if it cannot be used.
  1341. *
  1342. * For instance, try to analyse an ARM perf.data file _without_ a
  1343. * build-id, or if the user specifies the wrong path to the right
  1344. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1345. * x86_86 one, on the machine where analysis is being performed, say),
  1346. * or worse, /proc/kallsyms.
  1347. *
  1348. * If the specified file _has_ a build-id and there is a build-id
  1349. * section in the perf.data file, we will still do the expected
  1350. * validation in dso__load_vmlinux and will bail out if they don't
  1351. * match.
  1352. */
  1353. if (symbol_conf.vmlinux_name != NULL) {
  1354. err = dso__load_vmlinux(self, map,
  1355. symbol_conf.vmlinux_name, filter);
  1356. goto out_try_fixup;
  1357. }
  1358. if (vmlinux_path != NULL) {
  1359. err = dso__load_vmlinux_path(self, map, filter);
  1360. if (err > 0)
  1361. goto out_fixup;
  1362. }
  1363. /*
  1364. * Say the kernel DSO was created when processing the build-id header table,
  1365. * we have a build-id, so check if it is the same as the running kernel,
  1366. * using it if it is.
  1367. */
  1368. if (self->has_build_id) {
  1369. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1370. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1371. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1372. sizeof(kallsyms_build_id)) == 0) {
  1373. if (dso__build_id_equal(self, kallsyms_build_id)) {
  1374. kallsyms_filename = "/proc/kallsyms";
  1375. goto do_kallsyms;
  1376. }
  1377. }
  1378. /*
  1379. * Now look if we have it on the build-id cache in
  1380. * $HOME/.debug/[kernel.kallsyms].
  1381. */
  1382. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1383. sbuild_id);
  1384. if (asprintf(&kallsyms_allocated_filename,
  1385. "%s/.debug/[kernel.kallsyms]/%s",
  1386. getenv("HOME"), sbuild_id) == -1) {
  1387. pr_err("Not enough memory for kallsyms file lookup\n");
  1388. return -1;
  1389. }
  1390. kallsyms_filename = kallsyms_allocated_filename;
  1391. if (access(kallsyms_filename, F_OK)) {
  1392. pr_err("No kallsyms or vmlinux with build-id %s "
  1393. "was found\n", sbuild_id);
  1394. free(kallsyms_allocated_filename);
  1395. return -1;
  1396. }
  1397. } else {
  1398. /*
  1399. * Last resort, if we don't have a build-id and couldn't find
  1400. * any vmlinux file, try the running kernel kallsyms table.
  1401. */
  1402. kallsyms_filename = "/proc/kallsyms";
  1403. }
  1404. do_kallsyms:
  1405. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1406. if (err > 0)
  1407. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1408. free(kallsyms_allocated_filename);
  1409. out_try_fixup:
  1410. if (err > 0) {
  1411. out_fixup:
  1412. if (kallsyms_filename != NULL)
  1413. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1414. map__fixup_start(map);
  1415. map__fixup_end(map);
  1416. }
  1417. return err;
  1418. }
  1419. LIST_HEAD(dsos__user);
  1420. LIST_HEAD(dsos__kernel);
  1421. static void dsos__add(struct list_head *head, struct dso *dso)
  1422. {
  1423. list_add_tail(&dso->node, head);
  1424. }
  1425. static struct dso *dsos__find(struct list_head *head, const char *name)
  1426. {
  1427. struct dso *pos;
  1428. list_for_each_entry(pos, head, node)
  1429. if (strcmp(pos->long_name, name) == 0)
  1430. return pos;
  1431. return NULL;
  1432. }
  1433. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1434. {
  1435. struct dso *dso = dsos__find(head, name);
  1436. if (!dso) {
  1437. dso = dso__new(name);
  1438. if (dso != NULL) {
  1439. dsos__add(head, dso);
  1440. dso__set_basename(dso);
  1441. }
  1442. }
  1443. return dso;
  1444. }
  1445. static void __dsos__fprintf(struct list_head *head, FILE *fp)
  1446. {
  1447. struct dso *pos;
  1448. list_for_each_entry(pos, head, node) {
  1449. int i;
  1450. for (i = 0; i < MAP__NR_TYPES; ++i)
  1451. dso__fprintf(pos, i, fp);
  1452. }
  1453. }
  1454. void dsos__fprintf(FILE *fp)
  1455. {
  1456. __dsos__fprintf(&dsos__kernel, fp);
  1457. __dsos__fprintf(&dsos__user, fp);
  1458. }
  1459. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1460. bool with_hits)
  1461. {
  1462. struct dso *pos;
  1463. size_t ret = 0;
  1464. list_for_each_entry(pos, head, node) {
  1465. if (with_hits && !pos->hit)
  1466. continue;
  1467. ret += dso__fprintf_buildid(pos, fp);
  1468. ret += fprintf(fp, " %s\n", pos->long_name);
  1469. }
  1470. return ret;
  1471. }
  1472. size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
  1473. {
  1474. return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
  1475. __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
  1476. }
  1477. struct dso *dso__new_kernel(const char *name)
  1478. {
  1479. struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
  1480. if (self != NULL) {
  1481. self->short_name = "[kernel]";
  1482. self->kernel = 1;
  1483. }
  1484. return self;
  1485. }
  1486. void dso__read_running_kernel_build_id(struct dso *self)
  1487. {
  1488. if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
  1489. sizeof(self->build_id)) == 0)
  1490. self->has_build_id = true;
  1491. }
  1492. static struct dso *dsos__create_kernel(const char *vmlinux)
  1493. {
  1494. struct dso *kernel = dso__new_kernel(vmlinux);
  1495. if (kernel != NULL) {
  1496. dso__read_running_kernel_build_id(kernel);
  1497. dsos__add(&dsos__kernel, kernel);
  1498. }
  1499. return kernel;
  1500. }
  1501. int __map_groups__create_kernel_maps(struct map_groups *self,
  1502. struct map *vmlinux_maps[MAP__NR_TYPES],
  1503. struct dso *kernel)
  1504. {
  1505. enum map_type type;
  1506. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1507. struct kmap *kmap;
  1508. vmlinux_maps[type] = map__new2(0, kernel, type);
  1509. if (vmlinux_maps[type] == NULL)
  1510. return -1;
  1511. vmlinux_maps[type]->map_ip =
  1512. vmlinux_maps[type]->unmap_ip = identity__map_ip;
  1513. kmap = map__kmap(vmlinux_maps[type]);
  1514. kmap->kmaps = self;
  1515. map_groups__insert(self, vmlinux_maps[type]);
  1516. }
  1517. return 0;
  1518. }
  1519. static void vmlinux_path__exit(void)
  1520. {
  1521. while (--vmlinux_path__nr_entries >= 0) {
  1522. free(vmlinux_path[vmlinux_path__nr_entries]);
  1523. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1524. }
  1525. free(vmlinux_path);
  1526. vmlinux_path = NULL;
  1527. }
  1528. static int vmlinux_path__init(void)
  1529. {
  1530. struct utsname uts;
  1531. char bf[PATH_MAX];
  1532. if (uname(&uts) < 0)
  1533. return -1;
  1534. vmlinux_path = malloc(sizeof(char *) * 5);
  1535. if (vmlinux_path == NULL)
  1536. return -1;
  1537. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1538. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1539. goto out_fail;
  1540. ++vmlinux_path__nr_entries;
  1541. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1542. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1543. goto out_fail;
  1544. ++vmlinux_path__nr_entries;
  1545. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", 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. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1551. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1552. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1553. goto out_fail;
  1554. ++vmlinux_path__nr_entries;
  1555. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1556. uts.release);
  1557. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1558. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1559. goto out_fail;
  1560. ++vmlinux_path__nr_entries;
  1561. return 0;
  1562. out_fail:
  1563. vmlinux_path__exit();
  1564. return -1;
  1565. }
  1566. static int setup_list(struct strlist **list, const char *list_str,
  1567. const char *list_name)
  1568. {
  1569. if (list_str == NULL)
  1570. return 0;
  1571. *list = strlist__new(true, list_str);
  1572. if (!*list) {
  1573. pr_err("problems parsing %s list\n", list_name);
  1574. return -1;
  1575. }
  1576. return 0;
  1577. }
  1578. int symbol__init(void)
  1579. {
  1580. elf_version(EV_CURRENT);
  1581. if (symbol_conf.sort_by_name)
  1582. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1583. sizeof(struct symbol));
  1584. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1585. return -1;
  1586. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1587. pr_err("'.' is the only non valid --field-separator argument\n");
  1588. return -1;
  1589. }
  1590. if (setup_list(&symbol_conf.dso_list,
  1591. symbol_conf.dso_list_str, "dso") < 0)
  1592. return -1;
  1593. if (setup_list(&symbol_conf.comm_list,
  1594. symbol_conf.comm_list_str, "comm") < 0)
  1595. goto out_free_dso_list;
  1596. if (setup_list(&symbol_conf.sym_list,
  1597. symbol_conf.sym_list_str, "symbol") < 0)
  1598. goto out_free_comm_list;
  1599. return 0;
  1600. out_free_dso_list:
  1601. strlist__delete(symbol_conf.dso_list);
  1602. out_free_comm_list:
  1603. strlist__delete(symbol_conf.comm_list);
  1604. return -1;
  1605. }
  1606. int map_groups__create_kernel_maps(struct map_groups *self,
  1607. struct map *vmlinux_maps[MAP__NR_TYPES])
  1608. {
  1609. struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
  1610. if (kernel == NULL)
  1611. return -1;
  1612. if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
  1613. return -1;
  1614. if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
  1615. pr_debug("Problems creating module maps, continuing anyway...\n");
  1616. /*
  1617. * Now that we have all the maps created, just set the ->end of them:
  1618. */
  1619. map_groups__fixup_end(self);
  1620. return 0;
  1621. }