symbol.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/param.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <inttypes.h>
  12. #include "build-id.h"
  13. #include "util.h"
  14. #include "debug.h"
  15. #include "machine.h"
  16. #include "symbol.h"
  17. #include "strlist.h"
  18. #include <elf.h>
  19. #include <limits.h>
  20. #include <sys/utsname.h>
  21. #ifndef KSYM_NAME_LEN
  22. #define KSYM_NAME_LEN 256
  23. #endif
  24. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  25. symbol_filter_t filter);
  26. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  27. symbol_filter_t filter);
  28. int vmlinux_path__nr_entries;
  29. char **vmlinux_path;
  30. struct symbol_conf symbol_conf = {
  31. .use_modules = true,
  32. .try_vmlinux_path = true,
  33. .annotate_src = true,
  34. .demangle = true,
  35. .symfs = "",
  36. };
  37. static enum dso_binary_type binary_type_symtab[] = {
  38. DSO_BINARY_TYPE__KALLSYMS,
  39. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  40. DSO_BINARY_TYPE__JAVA_JIT,
  41. DSO_BINARY_TYPE__DEBUGLINK,
  42. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  43. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  44. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  45. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  46. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  47. DSO_BINARY_TYPE__GUEST_KMODULE,
  48. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  49. DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  50. DSO_BINARY_TYPE__NOT_FOUND,
  51. };
  52. #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  53. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  54. {
  55. symbol_type = toupper(symbol_type);
  56. switch (map_type) {
  57. case MAP__FUNCTION:
  58. return symbol_type == 'T' || symbol_type == 'W';
  59. case MAP__VARIABLE:
  60. return symbol_type == 'D';
  61. default:
  62. return false;
  63. }
  64. }
  65. static int prefix_underscores_count(const char *str)
  66. {
  67. const char *tail = str;
  68. while (*tail == '_')
  69. tail++;
  70. return tail - str;
  71. }
  72. #define SYMBOL_A 0
  73. #define SYMBOL_B 1
  74. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  75. {
  76. s64 a;
  77. s64 b;
  78. size_t na, nb;
  79. /* Prefer a symbol with non zero length */
  80. a = syma->end - syma->start;
  81. b = symb->end - symb->start;
  82. if ((b == 0) && (a > 0))
  83. return SYMBOL_A;
  84. else if ((a == 0) && (b > 0))
  85. return SYMBOL_B;
  86. /* Prefer a non weak symbol over a weak one */
  87. a = syma->binding == STB_WEAK;
  88. b = symb->binding == STB_WEAK;
  89. if (b && !a)
  90. return SYMBOL_A;
  91. if (a && !b)
  92. return SYMBOL_B;
  93. /* Prefer a global symbol over a non global one */
  94. a = syma->binding == STB_GLOBAL;
  95. b = symb->binding == STB_GLOBAL;
  96. if (a && !b)
  97. return SYMBOL_A;
  98. if (b && !a)
  99. return SYMBOL_B;
  100. /* Prefer a symbol with less underscores */
  101. a = prefix_underscores_count(syma->name);
  102. b = prefix_underscores_count(symb->name);
  103. if (b > a)
  104. return SYMBOL_A;
  105. else if (a > b)
  106. return SYMBOL_B;
  107. /* Choose the symbol with the longest name */
  108. na = strlen(syma->name);
  109. nb = strlen(symb->name);
  110. if (na > nb)
  111. return SYMBOL_A;
  112. else if (na < nb)
  113. return SYMBOL_B;
  114. /* Avoid "SyS" kernel syscall aliases */
  115. if (na >= 3 && !strncmp(syma->name, "SyS", 3))
  116. return SYMBOL_B;
  117. if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
  118. return SYMBOL_B;
  119. return SYMBOL_A;
  120. }
  121. void symbols__fixup_duplicate(struct rb_root *symbols)
  122. {
  123. struct rb_node *nd;
  124. struct symbol *curr, *next;
  125. nd = rb_first(symbols);
  126. while (nd) {
  127. curr = rb_entry(nd, struct symbol, rb_node);
  128. again:
  129. nd = rb_next(&curr->rb_node);
  130. next = rb_entry(nd, struct symbol, rb_node);
  131. if (!nd)
  132. break;
  133. if (curr->start != next->start)
  134. continue;
  135. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  136. rb_erase(&next->rb_node, symbols);
  137. goto again;
  138. } else {
  139. nd = rb_next(&curr->rb_node);
  140. rb_erase(&curr->rb_node, symbols);
  141. }
  142. }
  143. }
  144. void symbols__fixup_end(struct rb_root *symbols)
  145. {
  146. struct rb_node *nd, *prevnd = rb_first(symbols);
  147. struct symbol *curr, *prev;
  148. if (prevnd == NULL)
  149. return;
  150. curr = rb_entry(prevnd, struct symbol, rb_node);
  151. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  152. prev = curr;
  153. curr = rb_entry(nd, struct symbol, rb_node);
  154. if (prev->end == prev->start && prev->end != curr->start)
  155. prev->end = curr->start - 1;
  156. }
  157. /* Last entry */
  158. if (curr->end == curr->start)
  159. curr->end = roundup(curr->start, 4096);
  160. }
  161. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  162. {
  163. struct map *prev, *curr;
  164. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  165. if (prevnd == NULL)
  166. return;
  167. curr = rb_entry(prevnd, struct map, rb_node);
  168. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  169. prev = curr;
  170. curr = rb_entry(nd, struct map, rb_node);
  171. prev->end = curr->start - 1;
  172. }
  173. /*
  174. * We still haven't the actual symbols, so guess the
  175. * last map final address.
  176. */
  177. curr->end = ~0ULL;
  178. }
  179. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
  180. {
  181. size_t namelen = strlen(name) + 1;
  182. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  183. sizeof(*sym) + namelen));
  184. if (sym == NULL)
  185. return NULL;
  186. if (symbol_conf.priv_size)
  187. sym = ((void *)sym) + symbol_conf.priv_size;
  188. sym->start = start;
  189. sym->end = len ? start + len - 1 : start;
  190. sym->binding = binding;
  191. sym->namelen = namelen - 1;
  192. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  193. __func__, name, start, sym->end);
  194. memcpy(sym->name, name, namelen);
  195. return sym;
  196. }
  197. void symbol__delete(struct symbol *sym)
  198. {
  199. free(((void *)sym) - symbol_conf.priv_size);
  200. }
  201. size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  202. {
  203. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  204. sym->start, sym->end,
  205. sym->binding == STB_GLOBAL ? 'g' :
  206. sym->binding == STB_LOCAL ? 'l' : 'w',
  207. sym->name);
  208. }
  209. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  210. const struct addr_location *al, FILE *fp)
  211. {
  212. unsigned long offset;
  213. size_t length;
  214. if (sym && sym->name) {
  215. length = fprintf(fp, "%s", sym->name);
  216. if (al) {
  217. if (al->addr < sym->end)
  218. offset = al->addr - sym->start;
  219. else
  220. offset = al->addr - al->map->start - sym->start;
  221. length += fprintf(fp, "+0x%lx", offset);
  222. }
  223. return length;
  224. } else
  225. return fprintf(fp, "[unknown]");
  226. }
  227. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
  228. {
  229. return symbol__fprintf_symname_offs(sym, NULL, fp);
  230. }
  231. void symbols__delete(struct rb_root *symbols)
  232. {
  233. struct symbol *pos;
  234. struct rb_node *next = rb_first(symbols);
  235. while (next) {
  236. pos = rb_entry(next, struct symbol, rb_node);
  237. next = rb_next(&pos->rb_node);
  238. rb_erase(&pos->rb_node, symbols);
  239. symbol__delete(pos);
  240. }
  241. }
  242. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  243. {
  244. struct rb_node **p = &symbols->rb_node;
  245. struct rb_node *parent = NULL;
  246. const u64 ip = sym->start;
  247. struct symbol *s;
  248. while (*p != NULL) {
  249. parent = *p;
  250. s = rb_entry(parent, struct symbol, rb_node);
  251. if (ip < s->start)
  252. p = &(*p)->rb_left;
  253. else
  254. p = &(*p)->rb_right;
  255. }
  256. rb_link_node(&sym->rb_node, parent, p);
  257. rb_insert_color(&sym->rb_node, symbols);
  258. }
  259. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  260. {
  261. struct rb_node *n;
  262. if (symbols == NULL)
  263. return NULL;
  264. n = symbols->rb_node;
  265. while (n) {
  266. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  267. if (ip < s->start)
  268. n = n->rb_left;
  269. else if (ip > s->end)
  270. n = n->rb_right;
  271. else
  272. return s;
  273. }
  274. return NULL;
  275. }
  276. static struct symbol *symbols__first(struct rb_root *symbols)
  277. {
  278. struct rb_node *n = rb_first(symbols);
  279. if (n)
  280. return rb_entry(n, struct symbol, rb_node);
  281. return NULL;
  282. }
  283. struct symbol_name_rb_node {
  284. struct rb_node rb_node;
  285. struct symbol sym;
  286. };
  287. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  288. {
  289. struct rb_node **p = &symbols->rb_node;
  290. struct rb_node *parent = NULL;
  291. struct symbol_name_rb_node *symn, *s;
  292. symn = container_of(sym, struct symbol_name_rb_node, sym);
  293. while (*p != NULL) {
  294. parent = *p;
  295. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  296. if (strcmp(sym->name, s->sym.name) < 0)
  297. p = &(*p)->rb_left;
  298. else
  299. p = &(*p)->rb_right;
  300. }
  301. rb_link_node(&symn->rb_node, parent, p);
  302. rb_insert_color(&symn->rb_node, symbols);
  303. }
  304. static void symbols__sort_by_name(struct rb_root *symbols,
  305. struct rb_root *source)
  306. {
  307. struct rb_node *nd;
  308. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  309. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  310. symbols__insert_by_name(symbols, pos);
  311. }
  312. }
  313. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  314. const char *name)
  315. {
  316. struct rb_node *n;
  317. if (symbols == NULL)
  318. return NULL;
  319. n = symbols->rb_node;
  320. while (n) {
  321. struct symbol_name_rb_node *s;
  322. int cmp;
  323. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  324. cmp = strcmp(name, s->sym.name);
  325. if (cmp < 0)
  326. n = n->rb_left;
  327. else if (cmp > 0)
  328. n = n->rb_right;
  329. else
  330. return &s->sym;
  331. }
  332. return NULL;
  333. }
  334. struct symbol *dso__find_symbol(struct dso *dso,
  335. enum map_type type, u64 addr)
  336. {
  337. return symbols__find(&dso->symbols[type], addr);
  338. }
  339. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
  340. {
  341. return symbols__first(&dso->symbols[type]);
  342. }
  343. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  344. const char *name)
  345. {
  346. return symbols__find_by_name(&dso->symbol_names[type], name);
  347. }
  348. void dso__sort_by_name(struct dso *dso, enum map_type type)
  349. {
  350. dso__set_sorted_by_name(dso, type);
  351. return symbols__sort_by_name(&dso->symbol_names[type],
  352. &dso->symbols[type]);
  353. }
  354. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  355. enum map_type type, FILE *fp)
  356. {
  357. size_t ret = 0;
  358. struct rb_node *nd;
  359. struct symbol_name_rb_node *pos;
  360. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  361. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  362. fprintf(fp, "%s\n", pos->sym.name);
  363. }
  364. return ret;
  365. }
  366. int kallsyms__parse(const char *filename, void *arg,
  367. int (*process_symbol)(void *arg, const char *name,
  368. char type, u64 start))
  369. {
  370. char *line = NULL;
  371. size_t n;
  372. int err = -1;
  373. FILE *file = fopen(filename, "r");
  374. if (file == NULL)
  375. goto out_failure;
  376. err = 0;
  377. while (!feof(file)) {
  378. u64 start;
  379. int line_len, len;
  380. char symbol_type;
  381. char *symbol_name;
  382. line_len = getline(&line, &n, file);
  383. if (line_len < 0 || !line)
  384. break;
  385. line[--line_len] = '\0'; /* \n */
  386. len = hex2u64(line, &start);
  387. len++;
  388. if (len + 2 >= line_len)
  389. continue;
  390. symbol_type = line[len];
  391. len += 2;
  392. symbol_name = line + len;
  393. len = line_len - len;
  394. if (len >= KSYM_NAME_LEN) {
  395. err = -1;
  396. break;
  397. }
  398. err = process_symbol(arg, symbol_name,
  399. symbol_type, start);
  400. if (err)
  401. break;
  402. }
  403. free(line);
  404. fclose(file);
  405. return err;
  406. out_failure:
  407. return -1;
  408. }
  409. int modules__parse(const char *filename, void *arg,
  410. int (*process_module)(void *arg, const char *name,
  411. u64 start))
  412. {
  413. char *line = NULL;
  414. size_t n;
  415. FILE *file;
  416. int err = 0;
  417. file = fopen(filename, "r");
  418. if (file == NULL)
  419. return -1;
  420. while (1) {
  421. char name[PATH_MAX];
  422. u64 start;
  423. char *sep;
  424. ssize_t line_len;
  425. line_len = getline(&line, &n, file);
  426. if (line_len < 0) {
  427. if (feof(file))
  428. break;
  429. err = -1;
  430. goto out;
  431. }
  432. if (!line) {
  433. err = -1;
  434. goto out;
  435. }
  436. line[--line_len] = '\0'; /* \n */
  437. sep = strrchr(line, 'x');
  438. if (sep == NULL)
  439. continue;
  440. hex2u64(sep + 1, &start);
  441. sep = strchr(line, ' ');
  442. if (sep == NULL)
  443. continue;
  444. *sep = '\0';
  445. scnprintf(name, sizeof(name), "[%s]", line);
  446. err = process_module(arg, name, start);
  447. if (err)
  448. break;
  449. }
  450. out:
  451. free(line);
  452. fclose(file);
  453. return err;
  454. }
  455. struct process_kallsyms_args {
  456. struct map *map;
  457. struct dso *dso;
  458. };
  459. static u8 kallsyms2elf_type(char type)
  460. {
  461. if (type == 'W')
  462. return STB_WEAK;
  463. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  464. }
  465. static int map__process_kallsym_symbol(void *arg, const char *name,
  466. char type, u64 start)
  467. {
  468. struct symbol *sym;
  469. struct process_kallsyms_args *a = arg;
  470. struct rb_root *root = &a->dso->symbols[a->map->type];
  471. if (!symbol_type__is_a(type, a->map->type))
  472. return 0;
  473. /*
  474. * module symbols are not sorted so we add all
  475. * symbols, setting length to 0, and rely on
  476. * symbols__fixup_end() to fix it up.
  477. */
  478. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  479. if (sym == NULL)
  480. return -ENOMEM;
  481. /*
  482. * We will pass the symbols to the filter later, in
  483. * map__split_kallsyms, when we have split the maps per module
  484. */
  485. symbols__insert(root, sym);
  486. return 0;
  487. }
  488. /*
  489. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  490. * so that we can in the next step set the symbol ->end address and then
  491. * call kernel_maps__split_kallsyms.
  492. */
  493. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  494. struct map *map)
  495. {
  496. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  497. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  498. }
  499. static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
  500. symbol_filter_t filter)
  501. {
  502. struct map_groups *kmaps = map__kmap(map)->kmaps;
  503. struct map *curr_map;
  504. struct symbol *pos;
  505. int count = 0, moved = 0;
  506. struct rb_root *root = &dso->symbols[map->type];
  507. struct rb_node *next = rb_first(root);
  508. while (next) {
  509. char *module;
  510. pos = rb_entry(next, struct symbol, rb_node);
  511. next = rb_next(&pos->rb_node);
  512. module = strchr(pos->name, '\t');
  513. if (module)
  514. *module = '\0';
  515. curr_map = map_groups__find(kmaps, map->type, pos->start);
  516. if (!curr_map || (filter && filter(curr_map, pos))) {
  517. rb_erase(&pos->rb_node, root);
  518. symbol__delete(pos);
  519. } else {
  520. pos->start -= curr_map->start - curr_map->pgoff;
  521. if (pos->end)
  522. pos->end -= curr_map->start - curr_map->pgoff;
  523. if (curr_map != map) {
  524. rb_erase(&pos->rb_node, root);
  525. symbols__insert(
  526. &curr_map->dso->symbols[curr_map->type],
  527. pos);
  528. ++moved;
  529. } else {
  530. ++count;
  531. }
  532. }
  533. }
  534. /* Symbols have been adjusted */
  535. dso->adjust_symbols = 1;
  536. return count + moved;
  537. }
  538. /*
  539. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  540. * kernel range is broken in several maps, named [kernel].N, as we don't have
  541. * the original ELF section names vmlinux have.
  542. */
  543. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  544. symbol_filter_t filter)
  545. {
  546. struct map_groups *kmaps = map__kmap(map)->kmaps;
  547. struct machine *machine = kmaps->machine;
  548. struct map *curr_map = map;
  549. struct symbol *pos;
  550. int count = 0, moved = 0;
  551. struct rb_root *root = &dso->symbols[map->type];
  552. struct rb_node *next = rb_first(root);
  553. int kernel_range = 0;
  554. while (next) {
  555. char *module;
  556. pos = rb_entry(next, struct symbol, rb_node);
  557. next = rb_next(&pos->rb_node);
  558. module = strchr(pos->name, '\t');
  559. if (module) {
  560. if (!symbol_conf.use_modules)
  561. goto discard_symbol;
  562. *module++ = '\0';
  563. if (strcmp(curr_map->dso->short_name, module)) {
  564. if (curr_map != map &&
  565. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  566. machine__is_default_guest(machine)) {
  567. /*
  568. * We assume all symbols of a module are
  569. * continuous in * kallsyms, so curr_map
  570. * points to a module and all its
  571. * symbols are in its kmap. Mark it as
  572. * loaded.
  573. */
  574. dso__set_loaded(curr_map->dso,
  575. curr_map->type);
  576. }
  577. curr_map = map_groups__find_by_name(kmaps,
  578. map->type, module);
  579. if (curr_map == NULL) {
  580. pr_debug("%s/proc/{kallsyms,modules} "
  581. "inconsistency while looking "
  582. "for \"%s\" module!\n",
  583. machine->root_dir, module);
  584. curr_map = map;
  585. goto discard_symbol;
  586. }
  587. if (curr_map->dso->loaded &&
  588. !machine__is_default_guest(machine))
  589. goto discard_symbol;
  590. }
  591. /*
  592. * So that we look just like we get from .ko files,
  593. * i.e. not prelinked, relative to map->start.
  594. */
  595. pos->start = curr_map->map_ip(curr_map, pos->start);
  596. pos->end = curr_map->map_ip(curr_map, pos->end);
  597. } else if (curr_map != map) {
  598. char dso_name[PATH_MAX];
  599. struct dso *ndso;
  600. if (count == 0) {
  601. curr_map = map;
  602. goto filter_symbol;
  603. }
  604. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  605. snprintf(dso_name, sizeof(dso_name),
  606. "[guest.kernel].%d",
  607. kernel_range++);
  608. else
  609. snprintf(dso_name, sizeof(dso_name),
  610. "[kernel].%d",
  611. kernel_range++);
  612. ndso = dso__new(dso_name);
  613. if (ndso == NULL)
  614. return -1;
  615. ndso->kernel = dso->kernel;
  616. curr_map = map__new2(pos->start, ndso, map->type);
  617. if (curr_map == NULL) {
  618. dso__delete(ndso);
  619. return -1;
  620. }
  621. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  622. map_groups__insert(kmaps, curr_map);
  623. ++kernel_range;
  624. }
  625. filter_symbol:
  626. if (filter && filter(curr_map, pos)) {
  627. discard_symbol: rb_erase(&pos->rb_node, root);
  628. symbol__delete(pos);
  629. } else {
  630. if (curr_map != map) {
  631. rb_erase(&pos->rb_node, root);
  632. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  633. ++moved;
  634. } else
  635. ++count;
  636. }
  637. }
  638. if (curr_map != map &&
  639. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  640. machine__is_default_guest(kmaps->machine)) {
  641. dso__set_loaded(curr_map->dso, curr_map->type);
  642. }
  643. return count + moved;
  644. }
  645. bool symbol__restricted_filename(const char *filename,
  646. const char *restricted_filename)
  647. {
  648. bool restricted = false;
  649. if (symbol_conf.kptr_restrict) {
  650. char *r = realpath(filename, NULL);
  651. if (r != NULL) {
  652. restricted = strcmp(r, restricted_filename) == 0;
  653. free(r);
  654. return restricted;
  655. }
  656. }
  657. return restricted;
  658. }
  659. struct kcore_mapfn_data {
  660. struct dso *dso;
  661. enum map_type type;
  662. struct list_head maps;
  663. };
  664. static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
  665. {
  666. struct kcore_mapfn_data *md = data;
  667. struct map *map;
  668. map = map__new2(start, md->dso, md->type);
  669. if (map == NULL)
  670. return -ENOMEM;
  671. map->end = map->start + len;
  672. map->pgoff = pgoff;
  673. list_add(&map->node, &md->maps);
  674. return 0;
  675. }
  676. /*
  677. * If kallsyms is referenced by name then we look for kcore in the same
  678. * directory.
  679. */
  680. static bool kcore_filename_from_kallsyms_filename(char *kcore_filename,
  681. const char *kallsyms_filename)
  682. {
  683. char *name;
  684. strcpy(kcore_filename, kallsyms_filename);
  685. name = strrchr(kcore_filename, '/');
  686. if (!name)
  687. return false;
  688. if (!strcmp(name, "/kallsyms")) {
  689. strcpy(name, "/kcore");
  690. return true;
  691. }
  692. return false;
  693. }
  694. static int dso__load_kcore(struct dso *dso, struct map *map,
  695. const char *kallsyms_filename)
  696. {
  697. struct map_groups *kmaps = map__kmap(map)->kmaps;
  698. struct machine *machine = kmaps->machine;
  699. struct kcore_mapfn_data md;
  700. struct map *old_map, *new_map, *replacement_map = NULL;
  701. bool is_64_bit;
  702. int err, fd;
  703. char kcore_filename[PATH_MAX];
  704. struct symbol *sym;
  705. /* This function requires that the map is the kernel map */
  706. if (map != machine->vmlinux_maps[map->type])
  707. return -EINVAL;
  708. if (!kcore_filename_from_kallsyms_filename(kcore_filename,
  709. kallsyms_filename))
  710. return -EINVAL;
  711. md.dso = dso;
  712. md.type = map->type;
  713. INIT_LIST_HEAD(&md.maps);
  714. fd = open(kcore_filename, O_RDONLY);
  715. if (fd < 0)
  716. return -EINVAL;
  717. /* Read new maps into temporary lists */
  718. err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
  719. &is_64_bit);
  720. if (err)
  721. goto out_err;
  722. if (list_empty(&md.maps)) {
  723. err = -EINVAL;
  724. goto out_err;
  725. }
  726. /* Remove old maps */
  727. old_map = map_groups__first(kmaps, map->type);
  728. while (old_map) {
  729. struct map *next = map_groups__next(old_map);
  730. if (old_map != map)
  731. map_groups__remove(kmaps, old_map);
  732. old_map = next;
  733. }
  734. /* Find the kernel map using the first symbol */
  735. sym = dso__first_symbol(dso, map->type);
  736. list_for_each_entry(new_map, &md.maps, node) {
  737. if (sym && sym->start >= new_map->start &&
  738. sym->start < new_map->end) {
  739. replacement_map = new_map;
  740. break;
  741. }
  742. }
  743. if (!replacement_map)
  744. replacement_map = list_entry(md.maps.next, struct map, node);
  745. /* Add new maps */
  746. while (!list_empty(&md.maps)) {
  747. new_map = list_entry(md.maps.next, struct map, node);
  748. list_del(&new_map->node);
  749. if (new_map == replacement_map) {
  750. map->start = new_map->start;
  751. map->end = new_map->end;
  752. map->pgoff = new_map->pgoff;
  753. map->map_ip = new_map->map_ip;
  754. map->unmap_ip = new_map->unmap_ip;
  755. map__delete(new_map);
  756. /* Ensure maps are correctly ordered */
  757. map_groups__remove(kmaps, map);
  758. map_groups__insert(kmaps, map);
  759. } else {
  760. map_groups__insert(kmaps, new_map);
  761. }
  762. }
  763. /*
  764. * Set the data type and long name so that kcore can be read via
  765. * dso__data_read_addr().
  766. */
  767. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  768. dso->data_type = DSO_BINARY_TYPE__GUEST_KCORE;
  769. else
  770. dso->data_type = DSO_BINARY_TYPE__KCORE;
  771. dso__set_long_name(dso, strdup(kcore_filename));
  772. close(fd);
  773. if (map->type == MAP__FUNCTION)
  774. pr_debug("Using %s for kernel object code\n", kcore_filename);
  775. else
  776. pr_debug("Using %s for kernel data\n", kcore_filename);
  777. return 0;
  778. out_err:
  779. while (!list_empty(&md.maps)) {
  780. map = list_entry(md.maps.next, struct map, node);
  781. list_del(&map->node);
  782. map__delete(map);
  783. }
  784. close(fd);
  785. return -EINVAL;
  786. }
  787. int dso__load_kallsyms(struct dso *dso, const char *filename,
  788. struct map *map, symbol_filter_t filter)
  789. {
  790. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  791. return -1;
  792. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  793. return -1;
  794. symbols__fixup_duplicate(&dso->symbols[map->type]);
  795. symbols__fixup_end(&dso->symbols[map->type]);
  796. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  797. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  798. else
  799. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  800. if (!dso__load_kcore(dso, map, filename))
  801. return dso__split_kallsyms_for_kcore(dso, map, filter);
  802. else
  803. return dso__split_kallsyms(dso, map, filter);
  804. }
  805. static int dso__load_perf_map(struct dso *dso, struct map *map,
  806. symbol_filter_t filter)
  807. {
  808. char *line = NULL;
  809. size_t n;
  810. FILE *file;
  811. int nr_syms = 0;
  812. file = fopen(dso->long_name, "r");
  813. if (file == NULL)
  814. goto out_failure;
  815. while (!feof(file)) {
  816. u64 start, size;
  817. struct symbol *sym;
  818. int line_len, len;
  819. line_len = getline(&line, &n, file);
  820. if (line_len < 0)
  821. break;
  822. if (!line)
  823. goto out_failure;
  824. line[--line_len] = '\0'; /* \n */
  825. len = hex2u64(line, &start);
  826. len++;
  827. if (len + 2 >= line_len)
  828. continue;
  829. len += hex2u64(line + len, &size);
  830. len++;
  831. if (len + 2 >= line_len)
  832. continue;
  833. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  834. if (sym == NULL)
  835. goto out_delete_line;
  836. if (filter && filter(map, sym))
  837. symbol__delete(sym);
  838. else {
  839. symbols__insert(&dso->symbols[map->type], sym);
  840. nr_syms++;
  841. }
  842. }
  843. free(line);
  844. fclose(file);
  845. return nr_syms;
  846. out_delete_line:
  847. free(line);
  848. out_failure:
  849. return -1;
  850. }
  851. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  852. {
  853. char *name;
  854. int ret = -1;
  855. u_int i;
  856. struct machine *machine;
  857. char *root_dir = (char *) "";
  858. int ss_pos = 0;
  859. struct symsrc ss_[2];
  860. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  861. dso__set_loaded(dso, map->type);
  862. if (dso->kernel == DSO_TYPE_KERNEL)
  863. return dso__load_kernel_sym(dso, map, filter);
  864. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  865. return dso__load_guest_kernel_sym(dso, map, filter);
  866. if (map->groups && map->groups->machine)
  867. machine = map->groups->machine;
  868. else
  869. machine = NULL;
  870. dso->adjust_symbols = 0;
  871. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  872. struct stat st;
  873. if (lstat(dso->name, &st) < 0)
  874. return -1;
  875. if (st.st_uid && (st.st_uid != geteuid())) {
  876. pr_warning("File %s not owned by current user or root, "
  877. "ignoring it.\n", dso->name);
  878. return -1;
  879. }
  880. ret = dso__load_perf_map(dso, map, filter);
  881. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  882. DSO_BINARY_TYPE__NOT_FOUND;
  883. return ret;
  884. }
  885. if (machine)
  886. root_dir = machine->root_dir;
  887. name = malloc(PATH_MAX);
  888. if (!name)
  889. return -1;
  890. /* Iterate over candidate debug images.
  891. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  892. * and/or opd section) for processing.
  893. */
  894. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  895. struct symsrc *ss = &ss_[ss_pos];
  896. bool next_slot = false;
  897. enum dso_binary_type symtab_type = binary_type_symtab[i];
  898. if (dso__binary_type_file(dso, symtab_type,
  899. root_dir, name, PATH_MAX))
  900. continue;
  901. /* Name is now the name of the next image to try */
  902. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  903. continue;
  904. if (!syms_ss && symsrc__has_symtab(ss)) {
  905. syms_ss = ss;
  906. next_slot = true;
  907. }
  908. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  909. runtime_ss = ss;
  910. next_slot = true;
  911. }
  912. if (next_slot) {
  913. ss_pos++;
  914. if (syms_ss && runtime_ss)
  915. break;
  916. }
  917. }
  918. if (!runtime_ss && !syms_ss)
  919. goto out_free;
  920. if (runtime_ss && !syms_ss) {
  921. syms_ss = runtime_ss;
  922. }
  923. /* We'll have to hope for the best */
  924. if (!runtime_ss && syms_ss)
  925. runtime_ss = syms_ss;
  926. if (syms_ss) {
  927. int km;
  928. km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  929. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
  930. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
  931. } else {
  932. ret = -1;
  933. }
  934. if (ret > 0) {
  935. int nr_plt;
  936. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  937. if (nr_plt > 0)
  938. ret += nr_plt;
  939. }
  940. for (; ss_pos > 0; ss_pos--)
  941. symsrc__destroy(&ss_[ss_pos - 1]);
  942. out_free:
  943. free(name);
  944. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  945. return 0;
  946. return ret;
  947. }
  948. struct map *map_groups__find_by_name(struct map_groups *mg,
  949. enum map_type type, const char *name)
  950. {
  951. struct rb_node *nd;
  952. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  953. struct map *map = rb_entry(nd, struct map, rb_node);
  954. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  955. return map;
  956. }
  957. return NULL;
  958. }
  959. int dso__load_vmlinux(struct dso *dso, struct map *map,
  960. const char *vmlinux, symbol_filter_t filter)
  961. {
  962. int err = -1;
  963. struct symsrc ss;
  964. char symfs_vmlinux[PATH_MAX];
  965. enum dso_binary_type symtab_type;
  966. if (vmlinux[0] == '/')
  967. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  968. else
  969. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  970. symbol_conf.symfs, vmlinux);
  971. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  972. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  973. else
  974. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  975. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  976. return -1;
  977. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  978. symsrc__destroy(&ss);
  979. if (err > 0) {
  980. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  981. dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  982. else
  983. dso->data_type = DSO_BINARY_TYPE__VMLINUX;
  984. dso__set_long_name(dso, (char *)vmlinux);
  985. dso__set_loaded(dso, map->type);
  986. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  987. }
  988. return err;
  989. }
  990. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  991. symbol_filter_t filter)
  992. {
  993. int i, err = 0;
  994. char *filename;
  995. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  996. vmlinux_path__nr_entries + 1);
  997. filename = dso__build_id_filename(dso, NULL, 0);
  998. if (filename != NULL) {
  999. err = dso__load_vmlinux(dso, map, filename, filter);
  1000. if (err > 0) {
  1001. dso->lname_alloc = 1;
  1002. goto out;
  1003. }
  1004. free(filename);
  1005. }
  1006. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1007. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1008. if (err > 0) {
  1009. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1010. dso->lname_alloc = 1;
  1011. break;
  1012. }
  1013. }
  1014. out:
  1015. return err;
  1016. }
  1017. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1018. symbol_filter_t filter)
  1019. {
  1020. int err;
  1021. const char *kallsyms_filename = NULL;
  1022. char *kallsyms_allocated_filename = NULL;
  1023. /*
  1024. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1025. * it and only it, reporting errors to the user if it cannot be used.
  1026. *
  1027. * For instance, try to analyse an ARM perf.data file _without_ a
  1028. * build-id, or if the user specifies the wrong path to the right
  1029. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1030. * x86_86 one, on the machine where analysis is being performed, say),
  1031. * or worse, /proc/kallsyms.
  1032. *
  1033. * If the specified file _has_ a build-id and there is a build-id
  1034. * section in the perf.data file, we will still do the expected
  1035. * validation in dso__load_vmlinux and will bail out if they don't
  1036. * match.
  1037. */
  1038. if (symbol_conf.kallsyms_name != NULL) {
  1039. kallsyms_filename = symbol_conf.kallsyms_name;
  1040. goto do_kallsyms;
  1041. }
  1042. if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
  1043. err = dso__load_vmlinux(dso, map,
  1044. symbol_conf.vmlinux_name, filter);
  1045. if (err > 0) {
  1046. dso__set_long_name(dso,
  1047. strdup(symbol_conf.vmlinux_name));
  1048. dso->lname_alloc = 1;
  1049. return err;
  1050. }
  1051. return err;
  1052. }
  1053. if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
  1054. err = dso__load_vmlinux_path(dso, map, filter);
  1055. if (err > 0)
  1056. return err;
  1057. }
  1058. /* do not try local files if a symfs was given */
  1059. if (symbol_conf.symfs[0] != 0)
  1060. return -1;
  1061. /*
  1062. * Say the kernel DSO was created when processing the build-id header table,
  1063. * we have a build-id, so check if it is the same as the running kernel,
  1064. * using it if it is.
  1065. */
  1066. if (dso->has_build_id) {
  1067. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1068. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1069. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1070. sizeof(kallsyms_build_id)) == 0) {
  1071. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1072. kallsyms_filename = "/proc/kallsyms";
  1073. goto do_kallsyms;
  1074. }
  1075. }
  1076. /*
  1077. * Now look if we have it on the build-id cache in
  1078. * $HOME/.debug/[kernel.kallsyms].
  1079. */
  1080. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1081. sbuild_id);
  1082. if (asprintf(&kallsyms_allocated_filename,
  1083. "%s/.debug/[kernel.kallsyms]/%s",
  1084. getenv("HOME"), sbuild_id) == -1) {
  1085. pr_err("Not enough memory for kallsyms file lookup\n");
  1086. return -1;
  1087. }
  1088. kallsyms_filename = kallsyms_allocated_filename;
  1089. if (access(kallsyms_filename, F_OK)) {
  1090. pr_err("No kallsyms or vmlinux with build-id %s "
  1091. "was found\n", sbuild_id);
  1092. free(kallsyms_allocated_filename);
  1093. return -1;
  1094. }
  1095. } else {
  1096. /*
  1097. * Last resort, if we don't have a build-id and couldn't find
  1098. * any vmlinux file, try the running kernel kallsyms table.
  1099. */
  1100. kallsyms_filename = "/proc/kallsyms";
  1101. }
  1102. do_kallsyms:
  1103. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1104. if (err > 0)
  1105. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1106. free(kallsyms_allocated_filename);
  1107. if (err > 0 && !dso__is_kcore(dso)) {
  1108. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1109. map__fixup_start(map);
  1110. map__fixup_end(map);
  1111. }
  1112. return err;
  1113. }
  1114. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1115. symbol_filter_t filter)
  1116. {
  1117. int err;
  1118. const char *kallsyms_filename = NULL;
  1119. struct machine *machine;
  1120. char path[PATH_MAX];
  1121. if (!map->groups) {
  1122. pr_debug("Guest kernel map hasn't the point to groups\n");
  1123. return -1;
  1124. }
  1125. machine = map->groups->machine;
  1126. if (machine__is_default_guest(machine)) {
  1127. /*
  1128. * if the user specified a vmlinux filename, use it and only
  1129. * it, reporting errors to the user if it cannot be used.
  1130. * Or use file guest_kallsyms inputted by user on commandline
  1131. */
  1132. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1133. err = dso__load_vmlinux(dso, map,
  1134. symbol_conf.default_guest_vmlinux_name, filter);
  1135. return err;
  1136. }
  1137. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1138. if (!kallsyms_filename)
  1139. return -1;
  1140. } else {
  1141. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1142. kallsyms_filename = path;
  1143. }
  1144. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1145. if (err > 0)
  1146. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1147. if (err > 0 && !dso__is_kcore(dso)) {
  1148. machine__mmap_name(machine, path, sizeof(path));
  1149. dso__set_long_name(dso, strdup(path));
  1150. map__fixup_start(map);
  1151. map__fixup_end(map);
  1152. }
  1153. return err;
  1154. }
  1155. static void vmlinux_path__exit(void)
  1156. {
  1157. while (--vmlinux_path__nr_entries >= 0) {
  1158. free(vmlinux_path[vmlinux_path__nr_entries]);
  1159. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1160. }
  1161. free(vmlinux_path);
  1162. vmlinux_path = NULL;
  1163. }
  1164. static int vmlinux_path__init(void)
  1165. {
  1166. struct utsname uts;
  1167. char bf[PATH_MAX];
  1168. vmlinux_path = malloc(sizeof(char *) * 5);
  1169. if (vmlinux_path == NULL)
  1170. return -1;
  1171. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1172. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1173. goto out_fail;
  1174. ++vmlinux_path__nr_entries;
  1175. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1176. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1177. goto out_fail;
  1178. ++vmlinux_path__nr_entries;
  1179. /* only try running kernel version if no symfs was given */
  1180. if (symbol_conf.symfs[0] != 0)
  1181. return 0;
  1182. if (uname(&uts) < 0)
  1183. return -1;
  1184. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1185. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1186. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1187. goto out_fail;
  1188. ++vmlinux_path__nr_entries;
  1189. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1190. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1191. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1192. goto out_fail;
  1193. ++vmlinux_path__nr_entries;
  1194. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1195. uts.release);
  1196. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1197. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1198. goto out_fail;
  1199. ++vmlinux_path__nr_entries;
  1200. return 0;
  1201. out_fail:
  1202. vmlinux_path__exit();
  1203. return -1;
  1204. }
  1205. static int setup_list(struct strlist **list, const char *list_str,
  1206. const char *list_name)
  1207. {
  1208. if (list_str == NULL)
  1209. return 0;
  1210. *list = strlist__new(true, list_str);
  1211. if (!*list) {
  1212. pr_err("problems parsing %s list\n", list_name);
  1213. return -1;
  1214. }
  1215. return 0;
  1216. }
  1217. static bool symbol__read_kptr_restrict(void)
  1218. {
  1219. bool value = false;
  1220. if (geteuid() != 0) {
  1221. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1222. if (fp != NULL) {
  1223. char line[8];
  1224. if (fgets(line, sizeof(line), fp) != NULL)
  1225. value = atoi(line) != 0;
  1226. fclose(fp);
  1227. }
  1228. }
  1229. return value;
  1230. }
  1231. int symbol__init(void)
  1232. {
  1233. const char *symfs;
  1234. if (symbol_conf.initialized)
  1235. return 0;
  1236. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1237. symbol__elf_init();
  1238. if (symbol_conf.sort_by_name)
  1239. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1240. sizeof(struct symbol));
  1241. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1242. return -1;
  1243. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1244. pr_err("'.' is the only non valid --field-separator argument\n");
  1245. return -1;
  1246. }
  1247. if (setup_list(&symbol_conf.dso_list,
  1248. symbol_conf.dso_list_str, "dso") < 0)
  1249. return -1;
  1250. if (setup_list(&symbol_conf.comm_list,
  1251. symbol_conf.comm_list_str, "comm") < 0)
  1252. goto out_free_dso_list;
  1253. if (setup_list(&symbol_conf.sym_list,
  1254. symbol_conf.sym_list_str, "symbol") < 0)
  1255. goto out_free_comm_list;
  1256. /*
  1257. * A path to symbols of "/" is identical to ""
  1258. * reset here for simplicity.
  1259. */
  1260. symfs = realpath(symbol_conf.symfs, NULL);
  1261. if (symfs == NULL)
  1262. symfs = symbol_conf.symfs;
  1263. if (strcmp(symfs, "/") == 0)
  1264. symbol_conf.symfs = "";
  1265. if (symfs != symbol_conf.symfs)
  1266. free((void *)symfs);
  1267. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1268. symbol_conf.initialized = true;
  1269. return 0;
  1270. out_free_comm_list:
  1271. strlist__delete(symbol_conf.comm_list);
  1272. out_free_dso_list:
  1273. strlist__delete(symbol_conf.dso_list);
  1274. return -1;
  1275. }
  1276. void symbol__exit(void)
  1277. {
  1278. if (!symbol_conf.initialized)
  1279. return;
  1280. strlist__delete(symbol_conf.sym_list);
  1281. strlist__delete(symbol_conf.dso_list);
  1282. strlist__delete(symbol_conf.comm_list);
  1283. vmlinux_path__exit();
  1284. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1285. symbol_conf.initialized = false;
  1286. }