symbol.c 44 KB

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