symbol.c 40 KB

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