symbol.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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 "intlist.h"
  19. #include "header.h"
  20. #include <elf.h>
  21. #include <limits.h>
  22. #include <symbol/kallsyms.h>
  23. #include <sys/utsname.h>
  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. .demangle_kernel = false,
  36. .cumulate_callchain = true,
  37. .show_hist_headers = true,
  38. .symfs = "",
  39. .event_group = true,
  40. };
  41. static enum dso_binary_type binary_type_symtab[] = {
  42. DSO_BINARY_TYPE__KALLSYMS,
  43. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  44. DSO_BINARY_TYPE__JAVA_JIT,
  45. DSO_BINARY_TYPE__DEBUGLINK,
  46. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  47. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  48. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  49. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  50. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  51. DSO_BINARY_TYPE__GUEST_KMODULE,
  52. DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
  53. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  54. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
  55. DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  56. DSO_BINARY_TYPE__NOT_FOUND,
  57. };
  58. #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  59. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  60. {
  61. symbol_type = toupper(symbol_type);
  62. switch (map_type) {
  63. case MAP__FUNCTION:
  64. return symbol_type == 'T' || symbol_type == 'W';
  65. case MAP__VARIABLE:
  66. return symbol_type == 'D';
  67. default:
  68. return false;
  69. }
  70. }
  71. static int prefix_underscores_count(const char *str)
  72. {
  73. const char *tail = str;
  74. while (*tail == '_')
  75. tail++;
  76. return tail - str;
  77. }
  78. int __weak arch__choose_best_symbol(struct symbol *syma,
  79. struct symbol *symb __maybe_unused)
  80. {
  81. /* Avoid "SyS" kernel syscall aliases */
  82. if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
  83. return SYMBOL_B;
  84. if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
  85. return SYMBOL_B;
  86. return SYMBOL_A;
  87. }
  88. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  89. {
  90. s64 a;
  91. s64 b;
  92. size_t na, nb;
  93. /* Prefer a symbol with non zero length */
  94. a = syma->end - syma->start;
  95. b = symb->end - symb->start;
  96. if ((b == 0) && (a > 0))
  97. return SYMBOL_A;
  98. else if ((a == 0) && (b > 0))
  99. return SYMBOL_B;
  100. /* Prefer a non weak symbol over a weak one */
  101. a = syma->binding == STB_WEAK;
  102. b = symb->binding == STB_WEAK;
  103. if (b && !a)
  104. return SYMBOL_A;
  105. if (a && !b)
  106. return SYMBOL_B;
  107. /* Prefer a global symbol over a non global one */
  108. a = syma->binding == STB_GLOBAL;
  109. b = symb->binding == STB_GLOBAL;
  110. if (a && !b)
  111. return SYMBOL_A;
  112. if (b && !a)
  113. return SYMBOL_B;
  114. /* Prefer a symbol with less underscores */
  115. a = prefix_underscores_count(syma->name);
  116. b = prefix_underscores_count(symb->name);
  117. if (b > a)
  118. return SYMBOL_A;
  119. else if (a > b)
  120. return SYMBOL_B;
  121. /* Choose the symbol with the longest name */
  122. na = strlen(syma->name);
  123. nb = strlen(symb->name);
  124. if (na > nb)
  125. return SYMBOL_A;
  126. else if (na < nb)
  127. return SYMBOL_B;
  128. return arch__choose_best_symbol(syma, symb);
  129. }
  130. void symbols__fixup_duplicate(struct rb_root *symbols)
  131. {
  132. struct rb_node *nd;
  133. struct symbol *curr, *next;
  134. nd = rb_first(symbols);
  135. while (nd) {
  136. curr = rb_entry(nd, struct symbol, rb_node);
  137. again:
  138. nd = rb_next(&curr->rb_node);
  139. next = rb_entry(nd, struct symbol, rb_node);
  140. if (!nd)
  141. break;
  142. if (curr->start != next->start)
  143. continue;
  144. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  145. rb_erase(&next->rb_node, symbols);
  146. symbol__delete(next);
  147. goto again;
  148. } else {
  149. nd = rb_next(&curr->rb_node);
  150. rb_erase(&curr->rb_node, symbols);
  151. symbol__delete(curr);
  152. }
  153. }
  154. }
  155. void symbols__fixup_end(struct rb_root *symbols)
  156. {
  157. struct rb_node *nd, *prevnd = rb_first(symbols);
  158. struct symbol *curr, *prev;
  159. if (prevnd == NULL)
  160. return;
  161. curr = rb_entry(prevnd, struct symbol, rb_node);
  162. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  163. prev = curr;
  164. curr = rb_entry(nd, struct symbol, rb_node);
  165. if (prev->end == prev->start && prev->end != curr->start)
  166. prev->end = curr->start;
  167. }
  168. /* Last entry */
  169. if (curr->end == curr->start)
  170. curr->end = roundup(curr->start, 4096);
  171. }
  172. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  173. {
  174. struct maps *maps = &mg->maps[type];
  175. struct map *next, *curr;
  176. pthread_rwlock_wrlock(&maps->lock);
  177. curr = maps__first(maps);
  178. if (curr == NULL)
  179. goto out_unlock;
  180. for (next = map__next(curr); next; next = map__next(curr)) {
  181. curr->end = next->start;
  182. curr = next;
  183. }
  184. /*
  185. * We still haven't the actual symbols, so guess the
  186. * last map final address.
  187. */
  188. curr->end = ~0ULL;
  189. out_unlock:
  190. pthread_rwlock_unlock(&maps->lock);
  191. }
  192. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
  193. {
  194. size_t namelen = strlen(name) + 1;
  195. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  196. sizeof(*sym) + namelen));
  197. if (sym == NULL)
  198. return NULL;
  199. if (symbol_conf.priv_size)
  200. sym = ((void *)sym) + symbol_conf.priv_size;
  201. sym->start = start;
  202. sym->end = len ? start + len : start;
  203. sym->binding = binding;
  204. sym->namelen = namelen - 1;
  205. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  206. __func__, name, start, sym->end);
  207. memcpy(sym->name, name, namelen);
  208. return sym;
  209. }
  210. void symbol__delete(struct symbol *sym)
  211. {
  212. free(((void *)sym) - symbol_conf.priv_size);
  213. }
  214. void symbols__delete(struct rb_root *symbols)
  215. {
  216. struct symbol *pos;
  217. struct rb_node *next = rb_first(symbols);
  218. while (next) {
  219. pos = rb_entry(next, struct symbol, rb_node);
  220. next = rb_next(&pos->rb_node);
  221. rb_erase(&pos->rb_node, symbols);
  222. symbol__delete(pos);
  223. }
  224. }
  225. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  226. {
  227. struct rb_node **p = &symbols->rb_node;
  228. struct rb_node *parent = NULL;
  229. const u64 ip = sym->start;
  230. struct symbol *s;
  231. while (*p != NULL) {
  232. parent = *p;
  233. s = rb_entry(parent, struct symbol, rb_node);
  234. if (ip < s->start)
  235. p = &(*p)->rb_left;
  236. else
  237. p = &(*p)->rb_right;
  238. }
  239. rb_link_node(&sym->rb_node, parent, p);
  240. rb_insert_color(&sym->rb_node, symbols);
  241. }
  242. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  243. {
  244. struct rb_node *n;
  245. if (symbols == NULL)
  246. return NULL;
  247. n = symbols->rb_node;
  248. while (n) {
  249. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  250. if (ip < s->start)
  251. n = n->rb_left;
  252. else if (ip > s->end || (ip == s->end && ip != s->start))
  253. n = n->rb_right;
  254. else
  255. return s;
  256. }
  257. return NULL;
  258. }
  259. static struct symbol *symbols__first(struct rb_root *symbols)
  260. {
  261. struct rb_node *n = rb_first(symbols);
  262. if (n)
  263. return rb_entry(n, struct symbol, rb_node);
  264. return NULL;
  265. }
  266. static struct symbol *symbols__next(struct symbol *sym)
  267. {
  268. struct rb_node *n = rb_next(&sym->rb_node);
  269. if (n)
  270. return rb_entry(n, struct symbol, rb_node);
  271. return NULL;
  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. struct symbol_name_rb_node *s = NULL;
  304. if (symbols == NULL)
  305. return NULL;
  306. n = symbols->rb_node;
  307. while (n) {
  308. int cmp;
  309. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  310. cmp = arch__compare_symbol_names(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. break;
  317. }
  318. if (n == NULL)
  319. return NULL;
  320. /* return first symbol that has same name (if any) */
  321. for (n = rb_prev(n); n; n = rb_prev(n)) {
  322. struct symbol_name_rb_node *tmp;
  323. tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
  324. if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
  325. break;
  326. s = tmp;
  327. }
  328. return &s->sym;
  329. }
  330. void dso__reset_find_symbol_cache(struct dso *dso)
  331. {
  332. enum map_type type;
  333. for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
  334. dso->last_find_result[type].addr = 0;
  335. dso->last_find_result[type].symbol = NULL;
  336. }
  337. }
  338. void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
  339. {
  340. symbols__insert(&dso->symbols[type], sym);
  341. /* update the symbol cache if necessary */
  342. if (dso->last_find_result[type].addr >= sym->start &&
  343. (dso->last_find_result[type].addr < sym->end ||
  344. sym->start == sym->end)) {
  345. dso->last_find_result[type].symbol = sym;
  346. }
  347. }
  348. struct symbol *dso__find_symbol(struct dso *dso,
  349. enum map_type type, u64 addr)
  350. {
  351. if (dso->last_find_result[type].addr != addr) {
  352. dso->last_find_result[type].addr = addr;
  353. dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
  354. }
  355. return dso->last_find_result[type].symbol;
  356. }
  357. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
  358. {
  359. return symbols__first(&dso->symbols[type]);
  360. }
  361. struct symbol *dso__next_symbol(struct symbol *sym)
  362. {
  363. return symbols__next(sym);
  364. }
  365. struct symbol *symbol__next_by_name(struct symbol *sym)
  366. {
  367. struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
  368. struct rb_node *n = rb_next(&s->rb_node);
  369. return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
  370. }
  371. /*
  372. * Teturns first symbol that matched with @name.
  373. */
  374. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  375. const char *name)
  376. {
  377. return symbols__find_by_name(&dso->symbol_names[type], name);
  378. }
  379. void dso__sort_by_name(struct dso *dso, enum map_type type)
  380. {
  381. dso__set_sorted_by_name(dso, type);
  382. return symbols__sort_by_name(&dso->symbol_names[type],
  383. &dso->symbols[type]);
  384. }
  385. int modules__parse(const char *filename, void *arg,
  386. int (*process_module)(void *arg, const char *name,
  387. u64 start))
  388. {
  389. char *line = NULL;
  390. size_t n;
  391. FILE *file;
  392. int err = 0;
  393. file = fopen(filename, "r");
  394. if (file == NULL)
  395. return -1;
  396. while (1) {
  397. char name[PATH_MAX];
  398. u64 start;
  399. char *sep;
  400. ssize_t line_len;
  401. line_len = getline(&line, &n, file);
  402. if (line_len < 0) {
  403. if (feof(file))
  404. break;
  405. err = -1;
  406. goto out;
  407. }
  408. if (!line) {
  409. err = -1;
  410. goto out;
  411. }
  412. line[--line_len] = '\0'; /* \n */
  413. sep = strrchr(line, 'x');
  414. if (sep == NULL)
  415. continue;
  416. hex2u64(sep + 1, &start);
  417. sep = strchr(line, ' ');
  418. if (sep == NULL)
  419. continue;
  420. *sep = '\0';
  421. scnprintf(name, sizeof(name), "[%s]", line);
  422. err = process_module(arg, name, start);
  423. if (err)
  424. break;
  425. }
  426. out:
  427. free(line);
  428. fclose(file);
  429. return err;
  430. }
  431. struct process_kallsyms_args {
  432. struct map *map;
  433. struct dso *dso;
  434. };
  435. /*
  436. * These are symbols in the kernel image, so make sure that
  437. * sym is from a kernel DSO.
  438. */
  439. bool symbol__is_idle(struct symbol *sym)
  440. {
  441. const char * const idle_symbols[] = {
  442. "cpu_idle",
  443. "cpu_startup_entry",
  444. "intel_idle",
  445. "default_idle",
  446. "native_safe_halt",
  447. "enter_idle",
  448. "exit_idle",
  449. "mwait_idle",
  450. "mwait_idle_with_hints",
  451. "poll_idle",
  452. "ppc64_runlatch_off",
  453. "pseries_dedicated_idle_sleep",
  454. NULL
  455. };
  456. int i;
  457. if (!sym)
  458. return false;
  459. for (i = 0; idle_symbols[i]; i++) {
  460. if (!strcmp(idle_symbols[i], sym->name))
  461. return true;
  462. }
  463. return false;
  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_binding(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__kmaps(map);
  503. struct map *curr_map;
  504. struct symbol *pos;
  505. int count = 0;
  506. struct rb_root old_root = dso->symbols[map->type];
  507. struct rb_root *root = &dso->symbols[map->type];
  508. struct rb_node *next = rb_first(root);
  509. if (!kmaps)
  510. return -1;
  511. *root = RB_ROOT;
  512. while (next) {
  513. char *module;
  514. pos = rb_entry(next, struct symbol, rb_node);
  515. next = rb_next(&pos->rb_node);
  516. rb_erase_init(&pos->rb_node, &old_root);
  517. module = strchr(pos->name, '\t');
  518. if (module)
  519. *module = '\0';
  520. curr_map = map_groups__find(kmaps, map->type, pos->start);
  521. if (!curr_map || (filter && filter(curr_map, pos))) {
  522. symbol__delete(pos);
  523. continue;
  524. }
  525. pos->start -= curr_map->start - curr_map->pgoff;
  526. if (pos->end)
  527. pos->end -= curr_map->start - curr_map->pgoff;
  528. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  529. ++count;
  530. }
  531. /* Symbols have been adjusted */
  532. dso->adjust_symbols = 1;
  533. return count;
  534. }
  535. /*
  536. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  537. * kernel range is broken in several maps, named [kernel].N, as we don't have
  538. * the original ELF section names vmlinux have.
  539. */
  540. static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
  541. symbol_filter_t filter)
  542. {
  543. struct map_groups *kmaps = map__kmaps(map);
  544. struct machine *machine;
  545. struct map *curr_map = map;
  546. struct symbol *pos;
  547. int count = 0, moved = 0;
  548. struct rb_root *root = &dso->symbols[map->type];
  549. struct rb_node *next = rb_first(root);
  550. int kernel_range = 0;
  551. if (!kmaps)
  552. return -1;
  553. machine = kmaps->machine;
  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 (delta) {
  601. /* Kernel was relocated at boot time */
  602. pos->start -= delta;
  603. pos->end -= delta;
  604. }
  605. if (count == 0) {
  606. curr_map = map;
  607. goto filter_symbol;
  608. }
  609. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  610. snprintf(dso_name, sizeof(dso_name),
  611. "[guest.kernel].%d",
  612. kernel_range++);
  613. else
  614. snprintf(dso_name, sizeof(dso_name),
  615. "[kernel].%d",
  616. kernel_range++);
  617. ndso = dso__new(dso_name);
  618. if (ndso == NULL)
  619. return -1;
  620. ndso->kernel = dso->kernel;
  621. curr_map = map__new2(pos->start, ndso, map->type);
  622. if (curr_map == NULL) {
  623. dso__put(ndso);
  624. return -1;
  625. }
  626. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  627. map_groups__insert(kmaps, curr_map);
  628. ++kernel_range;
  629. } else if (delta) {
  630. /* Kernel was relocated at boot time */
  631. pos->start -= delta;
  632. pos->end -= delta;
  633. }
  634. filter_symbol:
  635. if (filter && filter(curr_map, pos)) {
  636. discard_symbol: rb_erase(&pos->rb_node, root);
  637. symbol__delete(pos);
  638. } else {
  639. if (curr_map != map) {
  640. rb_erase(&pos->rb_node, root);
  641. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  642. ++moved;
  643. } else
  644. ++count;
  645. }
  646. }
  647. if (curr_map != map &&
  648. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  649. machine__is_default_guest(kmaps->machine)) {
  650. dso__set_loaded(curr_map->dso, curr_map->type);
  651. }
  652. return count + moved;
  653. }
  654. bool symbol__restricted_filename(const char *filename,
  655. const char *restricted_filename)
  656. {
  657. bool restricted = false;
  658. if (symbol_conf.kptr_restrict) {
  659. char *r = realpath(filename, NULL);
  660. if (r != NULL) {
  661. restricted = strcmp(r, restricted_filename) == 0;
  662. free(r);
  663. return restricted;
  664. }
  665. }
  666. return restricted;
  667. }
  668. struct module_info {
  669. struct rb_node rb_node;
  670. char *name;
  671. u64 start;
  672. };
  673. static void add_module(struct module_info *mi, struct rb_root *modules)
  674. {
  675. struct rb_node **p = &modules->rb_node;
  676. struct rb_node *parent = NULL;
  677. struct module_info *m;
  678. while (*p != NULL) {
  679. parent = *p;
  680. m = rb_entry(parent, struct module_info, rb_node);
  681. if (strcmp(mi->name, m->name) < 0)
  682. p = &(*p)->rb_left;
  683. else
  684. p = &(*p)->rb_right;
  685. }
  686. rb_link_node(&mi->rb_node, parent, p);
  687. rb_insert_color(&mi->rb_node, modules);
  688. }
  689. static void delete_modules(struct rb_root *modules)
  690. {
  691. struct module_info *mi;
  692. struct rb_node *next = rb_first(modules);
  693. while (next) {
  694. mi = rb_entry(next, struct module_info, rb_node);
  695. next = rb_next(&mi->rb_node);
  696. rb_erase(&mi->rb_node, modules);
  697. zfree(&mi->name);
  698. free(mi);
  699. }
  700. }
  701. static struct module_info *find_module(const char *name,
  702. struct rb_root *modules)
  703. {
  704. struct rb_node *n = modules->rb_node;
  705. while (n) {
  706. struct module_info *m;
  707. int cmp;
  708. m = rb_entry(n, struct module_info, rb_node);
  709. cmp = strcmp(name, m->name);
  710. if (cmp < 0)
  711. n = n->rb_left;
  712. else if (cmp > 0)
  713. n = n->rb_right;
  714. else
  715. return m;
  716. }
  717. return NULL;
  718. }
  719. static int __read_proc_modules(void *arg, const char *name, u64 start)
  720. {
  721. struct rb_root *modules = arg;
  722. struct module_info *mi;
  723. mi = zalloc(sizeof(struct module_info));
  724. if (!mi)
  725. return -ENOMEM;
  726. mi->name = strdup(name);
  727. mi->start = start;
  728. if (!mi->name) {
  729. free(mi);
  730. return -ENOMEM;
  731. }
  732. add_module(mi, modules);
  733. return 0;
  734. }
  735. static int read_proc_modules(const char *filename, struct rb_root *modules)
  736. {
  737. if (symbol__restricted_filename(filename, "/proc/modules"))
  738. return -1;
  739. if (modules__parse(filename, modules, __read_proc_modules)) {
  740. delete_modules(modules);
  741. return -1;
  742. }
  743. return 0;
  744. }
  745. int compare_proc_modules(const char *from, const char *to)
  746. {
  747. struct rb_root from_modules = RB_ROOT;
  748. struct rb_root to_modules = RB_ROOT;
  749. struct rb_node *from_node, *to_node;
  750. struct module_info *from_m, *to_m;
  751. int ret = -1;
  752. if (read_proc_modules(from, &from_modules))
  753. return -1;
  754. if (read_proc_modules(to, &to_modules))
  755. goto out_delete_from;
  756. from_node = rb_first(&from_modules);
  757. to_node = rb_first(&to_modules);
  758. while (from_node) {
  759. if (!to_node)
  760. break;
  761. from_m = rb_entry(from_node, struct module_info, rb_node);
  762. to_m = rb_entry(to_node, struct module_info, rb_node);
  763. if (from_m->start != to_m->start ||
  764. strcmp(from_m->name, to_m->name))
  765. break;
  766. from_node = rb_next(from_node);
  767. to_node = rb_next(to_node);
  768. }
  769. if (!from_node && !to_node)
  770. ret = 0;
  771. delete_modules(&to_modules);
  772. out_delete_from:
  773. delete_modules(&from_modules);
  774. return ret;
  775. }
  776. static int do_validate_kcore_modules(const char *filename, struct map *map,
  777. struct map_groups *kmaps)
  778. {
  779. struct rb_root modules = RB_ROOT;
  780. struct map *old_map;
  781. int err;
  782. err = read_proc_modules(filename, &modules);
  783. if (err)
  784. return err;
  785. old_map = map_groups__first(kmaps, map->type);
  786. while (old_map) {
  787. struct map *next = map_groups__next(old_map);
  788. struct module_info *mi;
  789. if (old_map == map || old_map->start == map->start) {
  790. /* The kernel map */
  791. old_map = next;
  792. continue;
  793. }
  794. /* Module must be in memory at the same address */
  795. mi = find_module(old_map->dso->short_name, &modules);
  796. if (!mi || mi->start != old_map->start) {
  797. err = -EINVAL;
  798. goto out;
  799. }
  800. old_map = next;
  801. }
  802. out:
  803. delete_modules(&modules);
  804. return err;
  805. }
  806. /*
  807. * If kallsyms is referenced by name then we look for filename in the same
  808. * directory.
  809. */
  810. static bool filename_from_kallsyms_filename(char *filename,
  811. const char *base_name,
  812. const char *kallsyms_filename)
  813. {
  814. char *name;
  815. strcpy(filename, kallsyms_filename);
  816. name = strrchr(filename, '/');
  817. if (!name)
  818. return false;
  819. name += 1;
  820. if (!strcmp(name, "kallsyms")) {
  821. strcpy(name, base_name);
  822. return true;
  823. }
  824. return false;
  825. }
  826. static int validate_kcore_modules(const char *kallsyms_filename,
  827. struct map *map)
  828. {
  829. struct map_groups *kmaps = map__kmaps(map);
  830. char modules_filename[PATH_MAX];
  831. if (!kmaps)
  832. return -EINVAL;
  833. if (!filename_from_kallsyms_filename(modules_filename, "modules",
  834. kallsyms_filename))
  835. return -EINVAL;
  836. if (do_validate_kcore_modules(modules_filename, map, kmaps))
  837. return -EINVAL;
  838. return 0;
  839. }
  840. static int validate_kcore_addresses(const char *kallsyms_filename,
  841. struct map *map)
  842. {
  843. struct kmap *kmap = map__kmap(map);
  844. if (!kmap)
  845. return -EINVAL;
  846. if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
  847. u64 start;
  848. start = kallsyms__get_function_start(kallsyms_filename,
  849. kmap->ref_reloc_sym->name);
  850. if (start != kmap->ref_reloc_sym->addr)
  851. return -EINVAL;
  852. }
  853. return validate_kcore_modules(kallsyms_filename, map);
  854. }
  855. struct kcore_mapfn_data {
  856. struct dso *dso;
  857. enum map_type type;
  858. struct list_head maps;
  859. };
  860. static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
  861. {
  862. struct kcore_mapfn_data *md = data;
  863. struct map *map;
  864. map = map__new2(start, md->dso, md->type);
  865. if (map == NULL)
  866. return -ENOMEM;
  867. map->end = map->start + len;
  868. map->pgoff = pgoff;
  869. list_add(&map->node, &md->maps);
  870. return 0;
  871. }
  872. static int dso__load_kcore(struct dso *dso, struct map *map,
  873. const char *kallsyms_filename)
  874. {
  875. struct map_groups *kmaps = map__kmaps(map);
  876. struct machine *machine;
  877. struct kcore_mapfn_data md;
  878. struct map *old_map, *new_map, *replacement_map = NULL;
  879. bool is_64_bit;
  880. int err, fd;
  881. char kcore_filename[PATH_MAX];
  882. struct symbol *sym;
  883. if (!kmaps)
  884. return -EINVAL;
  885. machine = kmaps->machine;
  886. /* This function requires that the map is the kernel map */
  887. if (map != machine->vmlinux_maps[map->type])
  888. return -EINVAL;
  889. if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
  890. kallsyms_filename))
  891. return -EINVAL;
  892. /* Modules and kernel must be present at their original addresses */
  893. if (validate_kcore_addresses(kallsyms_filename, map))
  894. return -EINVAL;
  895. md.dso = dso;
  896. md.type = map->type;
  897. INIT_LIST_HEAD(&md.maps);
  898. fd = open(kcore_filename, O_RDONLY);
  899. if (fd < 0) {
  900. pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
  901. kcore_filename);
  902. return -EINVAL;
  903. }
  904. /* Read new maps into temporary lists */
  905. err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
  906. &is_64_bit);
  907. if (err)
  908. goto out_err;
  909. dso->is_64_bit = is_64_bit;
  910. if (list_empty(&md.maps)) {
  911. err = -EINVAL;
  912. goto out_err;
  913. }
  914. /* Remove old maps */
  915. old_map = map_groups__first(kmaps, map->type);
  916. while (old_map) {
  917. struct map *next = map_groups__next(old_map);
  918. if (old_map != map)
  919. map_groups__remove(kmaps, old_map);
  920. old_map = next;
  921. }
  922. /* Find the kernel map using the first symbol */
  923. sym = dso__first_symbol(dso, map->type);
  924. list_for_each_entry(new_map, &md.maps, node) {
  925. if (sym && sym->start >= new_map->start &&
  926. sym->start < new_map->end) {
  927. replacement_map = new_map;
  928. break;
  929. }
  930. }
  931. if (!replacement_map)
  932. replacement_map = list_entry(md.maps.next, struct map, node);
  933. /* Add new maps */
  934. while (!list_empty(&md.maps)) {
  935. new_map = list_entry(md.maps.next, struct map, node);
  936. list_del_init(&new_map->node);
  937. if (new_map == replacement_map) {
  938. map->start = new_map->start;
  939. map->end = new_map->end;
  940. map->pgoff = new_map->pgoff;
  941. map->map_ip = new_map->map_ip;
  942. map->unmap_ip = new_map->unmap_ip;
  943. /* Ensure maps are correctly ordered */
  944. map__get(map);
  945. map_groups__remove(kmaps, map);
  946. map_groups__insert(kmaps, map);
  947. map__put(map);
  948. } else {
  949. map_groups__insert(kmaps, new_map);
  950. }
  951. map__put(new_map);
  952. }
  953. /*
  954. * Set the data type and long name so that kcore can be read via
  955. * dso__data_read_addr().
  956. */
  957. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  958. dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
  959. else
  960. dso->binary_type = DSO_BINARY_TYPE__KCORE;
  961. dso__set_long_name(dso, strdup(kcore_filename), true);
  962. close(fd);
  963. if (map->type == MAP__FUNCTION)
  964. pr_debug("Using %s for kernel object code\n", kcore_filename);
  965. else
  966. pr_debug("Using %s for kernel data\n", kcore_filename);
  967. return 0;
  968. out_err:
  969. while (!list_empty(&md.maps)) {
  970. map = list_entry(md.maps.next, struct map, node);
  971. list_del_init(&map->node);
  972. map__put(map);
  973. }
  974. close(fd);
  975. return -EINVAL;
  976. }
  977. /*
  978. * If the kernel is relocated at boot time, kallsyms won't match. Compute the
  979. * delta based on the relocation reference symbol.
  980. */
  981. static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
  982. {
  983. struct kmap *kmap = map__kmap(map);
  984. u64 addr;
  985. if (!kmap)
  986. return -1;
  987. if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
  988. return 0;
  989. addr = kallsyms__get_function_start(filename,
  990. kmap->ref_reloc_sym->name);
  991. if (!addr)
  992. return -1;
  993. *delta = addr - kmap->ref_reloc_sym->addr;
  994. return 0;
  995. }
  996. int __dso__load_kallsyms(struct dso *dso, const char *filename,
  997. struct map *map, bool no_kcore, symbol_filter_t filter)
  998. {
  999. u64 delta = 0;
  1000. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1001. return -1;
  1002. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  1003. return -1;
  1004. if (kallsyms__delta(map, filename, &delta))
  1005. return -1;
  1006. symbols__fixup_duplicate(&dso->symbols[map->type]);
  1007. symbols__fixup_end(&dso->symbols[map->type]);
  1008. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1009. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  1010. else
  1011. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  1012. if (!no_kcore && !dso__load_kcore(dso, map, filename))
  1013. return dso__split_kallsyms_for_kcore(dso, map, filter);
  1014. else
  1015. return dso__split_kallsyms(dso, map, delta, filter);
  1016. }
  1017. int dso__load_kallsyms(struct dso *dso, const char *filename,
  1018. struct map *map, symbol_filter_t filter)
  1019. {
  1020. return __dso__load_kallsyms(dso, filename, map, false, filter);
  1021. }
  1022. static int dso__load_perf_map(struct dso *dso, struct map *map,
  1023. symbol_filter_t filter)
  1024. {
  1025. char *line = NULL;
  1026. size_t n;
  1027. FILE *file;
  1028. int nr_syms = 0;
  1029. file = fopen(dso->long_name, "r");
  1030. if (file == NULL)
  1031. goto out_failure;
  1032. while (!feof(file)) {
  1033. u64 start, size;
  1034. struct symbol *sym;
  1035. int line_len, len;
  1036. line_len = getline(&line, &n, file);
  1037. if (line_len < 0)
  1038. break;
  1039. if (!line)
  1040. goto out_failure;
  1041. line[--line_len] = '\0'; /* \n */
  1042. len = hex2u64(line, &start);
  1043. len++;
  1044. if (len + 2 >= line_len)
  1045. continue;
  1046. len += hex2u64(line + len, &size);
  1047. len++;
  1048. if (len + 2 >= line_len)
  1049. continue;
  1050. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  1051. if (sym == NULL)
  1052. goto out_delete_line;
  1053. if (filter && filter(map, sym))
  1054. symbol__delete(sym);
  1055. else {
  1056. symbols__insert(&dso->symbols[map->type], sym);
  1057. nr_syms++;
  1058. }
  1059. }
  1060. free(line);
  1061. fclose(file);
  1062. return nr_syms;
  1063. out_delete_line:
  1064. free(line);
  1065. out_failure:
  1066. return -1;
  1067. }
  1068. static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
  1069. enum dso_binary_type type)
  1070. {
  1071. switch (type) {
  1072. case DSO_BINARY_TYPE__JAVA_JIT:
  1073. case DSO_BINARY_TYPE__DEBUGLINK:
  1074. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  1075. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  1076. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  1077. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  1078. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  1079. return !kmod && dso->kernel == DSO_TYPE_USER;
  1080. case DSO_BINARY_TYPE__KALLSYMS:
  1081. case DSO_BINARY_TYPE__VMLINUX:
  1082. case DSO_BINARY_TYPE__KCORE:
  1083. return dso->kernel == DSO_TYPE_KERNEL;
  1084. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  1085. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  1086. case DSO_BINARY_TYPE__GUEST_KCORE:
  1087. return dso->kernel == DSO_TYPE_GUEST_KERNEL;
  1088. case DSO_BINARY_TYPE__GUEST_KMODULE:
  1089. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  1090. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  1091. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  1092. /*
  1093. * kernel modules know their symtab type - it's set when
  1094. * creating a module dso in machine__findnew_module_map().
  1095. */
  1096. return kmod && dso->symtab_type == type;
  1097. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  1098. return true;
  1099. case DSO_BINARY_TYPE__NOT_FOUND:
  1100. default:
  1101. return false;
  1102. }
  1103. }
  1104. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1105. {
  1106. char *name;
  1107. int ret = -1;
  1108. u_int i;
  1109. struct machine *machine;
  1110. char *root_dir = (char *) "";
  1111. int ss_pos = 0;
  1112. struct symsrc ss_[2];
  1113. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  1114. bool kmod;
  1115. unsigned char build_id[BUILD_ID_SIZE];
  1116. pthread_mutex_lock(&dso->lock);
  1117. /* check again under the dso->lock */
  1118. if (dso__loaded(dso, map->type)) {
  1119. ret = 1;
  1120. goto out;
  1121. }
  1122. if (dso->kernel) {
  1123. if (dso->kernel == DSO_TYPE_KERNEL)
  1124. ret = dso__load_kernel_sym(dso, map, filter);
  1125. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1126. ret = dso__load_guest_kernel_sym(dso, map, filter);
  1127. goto out;
  1128. }
  1129. if (map->groups && map->groups->machine)
  1130. machine = map->groups->machine;
  1131. else
  1132. machine = NULL;
  1133. dso->adjust_symbols = 0;
  1134. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1135. struct stat st;
  1136. if (lstat(dso->name, &st) < 0)
  1137. goto out;
  1138. if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
  1139. pr_warning("File %s not owned by current user or root, "
  1140. "ignoring it (use -f to override).\n", dso->name);
  1141. goto out;
  1142. }
  1143. ret = dso__load_perf_map(dso, map, filter);
  1144. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  1145. DSO_BINARY_TYPE__NOT_FOUND;
  1146. goto out;
  1147. }
  1148. if (machine)
  1149. root_dir = machine->root_dir;
  1150. name = malloc(PATH_MAX);
  1151. if (!name)
  1152. goto out;
  1153. kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  1154. dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
  1155. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
  1156. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
  1157. /*
  1158. * Read the build id if possible. This is required for
  1159. * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
  1160. */
  1161. if (is_regular_file(dso->long_name) &&
  1162. filename__read_build_id(dso->long_name, build_id, BUILD_ID_SIZE) > 0)
  1163. dso__set_build_id(dso, build_id);
  1164. /*
  1165. * Iterate over candidate debug images.
  1166. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  1167. * and/or opd section) for processing.
  1168. */
  1169. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  1170. struct symsrc *ss = &ss_[ss_pos];
  1171. bool next_slot = false;
  1172. enum dso_binary_type symtab_type = binary_type_symtab[i];
  1173. if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
  1174. continue;
  1175. if (dso__read_binary_type_filename(dso, symtab_type,
  1176. root_dir, name, PATH_MAX))
  1177. continue;
  1178. if (!is_regular_file(name))
  1179. continue;
  1180. /* Name is now the name of the next image to try */
  1181. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  1182. continue;
  1183. if (!syms_ss && symsrc__has_symtab(ss)) {
  1184. syms_ss = ss;
  1185. next_slot = true;
  1186. if (!dso->symsrc_filename)
  1187. dso->symsrc_filename = strdup(name);
  1188. }
  1189. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  1190. runtime_ss = ss;
  1191. next_slot = true;
  1192. }
  1193. if (next_slot) {
  1194. ss_pos++;
  1195. if (syms_ss && runtime_ss)
  1196. break;
  1197. } else {
  1198. symsrc__destroy(ss);
  1199. }
  1200. }
  1201. if (!runtime_ss && !syms_ss)
  1202. goto out_free;
  1203. if (runtime_ss && !syms_ss) {
  1204. syms_ss = runtime_ss;
  1205. }
  1206. /* We'll have to hope for the best */
  1207. if (!runtime_ss && syms_ss)
  1208. runtime_ss = syms_ss;
  1209. if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
  1210. if (dso__build_id_is_kmod(dso, name, PATH_MAX))
  1211. kmod = true;
  1212. if (syms_ss)
  1213. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
  1214. else
  1215. ret = -1;
  1216. if (ret > 0) {
  1217. int nr_plt;
  1218. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  1219. if (nr_plt > 0)
  1220. ret += nr_plt;
  1221. }
  1222. for (; ss_pos > 0; ss_pos--)
  1223. symsrc__destroy(&ss_[ss_pos - 1]);
  1224. out_free:
  1225. free(name);
  1226. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1227. ret = 0;
  1228. out:
  1229. dso__set_loaded(dso, map->type);
  1230. pthread_mutex_unlock(&dso->lock);
  1231. return ret;
  1232. }
  1233. struct map *map_groups__find_by_name(struct map_groups *mg,
  1234. enum map_type type, const char *name)
  1235. {
  1236. struct maps *maps = &mg->maps[type];
  1237. struct map *map;
  1238. pthread_rwlock_rdlock(&maps->lock);
  1239. for (map = maps__first(maps); map; map = map__next(map)) {
  1240. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1241. goto out_unlock;
  1242. }
  1243. map = NULL;
  1244. out_unlock:
  1245. pthread_rwlock_unlock(&maps->lock);
  1246. return map;
  1247. }
  1248. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1249. const char *vmlinux, bool vmlinux_allocated,
  1250. symbol_filter_t filter)
  1251. {
  1252. int err = -1;
  1253. struct symsrc ss;
  1254. char symfs_vmlinux[PATH_MAX];
  1255. enum dso_binary_type symtab_type;
  1256. if (vmlinux[0] == '/')
  1257. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  1258. else
  1259. symbol__join_symfs(symfs_vmlinux, vmlinux);
  1260. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1261. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1262. else
  1263. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  1264. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  1265. return -1;
  1266. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  1267. symsrc__destroy(&ss);
  1268. if (err > 0) {
  1269. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1270. dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1271. else
  1272. dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
  1273. dso__set_long_name(dso, vmlinux, vmlinux_allocated);
  1274. dso__set_loaded(dso, map->type);
  1275. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1276. }
  1277. return err;
  1278. }
  1279. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1280. symbol_filter_t filter)
  1281. {
  1282. int i, err = 0;
  1283. char *filename = NULL;
  1284. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1285. vmlinux_path__nr_entries + 1);
  1286. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1287. err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
  1288. if (err > 0)
  1289. goto out;
  1290. }
  1291. if (!symbol_conf.ignore_vmlinux_buildid)
  1292. filename = dso__build_id_filename(dso, NULL, 0);
  1293. if (filename != NULL) {
  1294. err = dso__load_vmlinux(dso, map, filename, true, filter);
  1295. if (err > 0)
  1296. goto out;
  1297. free(filename);
  1298. }
  1299. out:
  1300. return err;
  1301. }
  1302. static bool visible_dir_filter(const char *name, struct dirent *d)
  1303. {
  1304. if (d->d_type != DT_DIR)
  1305. return false;
  1306. return lsdir_no_dot_filter(name, d);
  1307. }
  1308. static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
  1309. {
  1310. char kallsyms_filename[PATH_MAX];
  1311. int ret = -1;
  1312. struct strlist *dirs;
  1313. struct str_node *nd;
  1314. dirs = lsdir(dir, visible_dir_filter);
  1315. if (!dirs)
  1316. return -1;
  1317. strlist__for_each_entry(nd, dirs) {
  1318. scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
  1319. "%s/%s/kallsyms", dir, nd->s);
  1320. if (!validate_kcore_addresses(kallsyms_filename, map)) {
  1321. strlcpy(dir, kallsyms_filename, dir_sz);
  1322. ret = 0;
  1323. break;
  1324. }
  1325. }
  1326. strlist__delete(dirs);
  1327. return ret;
  1328. }
  1329. /*
  1330. * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
  1331. * since access(R_OK) only checks with real UID/GID but open() use effective
  1332. * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
  1333. */
  1334. static bool filename__readable(const char *file)
  1335. {
  1336. int fd = open(file, O_RDONLY);
  1337. if (fd < 0)
  1338. return false;
  1339. close(fd);
  1340. return true;
  1341. }
  1342. static char *dso__find_kallsyms(struct dso *dso, struct map *map)
  1343. {
  1344. u8 host_build_id[BUILD_ID_SIZE];
  1345. char sbuild_id[SBUILD_ID_SIZE];
  1346. bool is_host = false;
  1347. char path[PATH_MAX];
  1348. if (!dso->has_build_id) {
  1349. /*
  1350. * Last resort, if we don't have a build-id and couldn't find
  1351. * any vmlinux file, try the running kernel kallsyms table.
  1352. */
  1353. goto proc_kallsyms;
  1354. }
  1355. if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
  1356. sizeof(host_build_id)) == 0)
  1357. is_host = dso__build_id_equal(dso, host_build_id);
  1358. /* Try a fast path for /proc/kallsyms if possible */
  1359. if (is_host) {
  1360. /*
  1361. * Do not check the build-id cache, unless we know we cannot use
  1362. * /proc/kcore or module maps don't match to /proc/kallsyms.
  1363. * To check readability of /proc/kcore, do not use access(R_OK)
  1364. * since /proc/kcore requires CAP_SYS_RAWIO to read and access
  1365. * can't check it.
  1366. */
  1367. if (filename__readable("/proc/kcore") &&
  1368. !validate_kcore_addresses("/proc/kallsyms", map))
  1369. goto proc_kallsyms;
  1370. }
  1371. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  1372. /* Find kallsyms in build-id cache with kcore */
  1373. scnprintf(path, sizeof(path), "%s/%s/%s",
  1374. buildid_dir, DSO__NAME_KCORE, sbuild_id);
  1375. if (!find_matching_kcore(map, path, sizeof(path)))
  1376. return strdup(path);
  1377. /* Use current /proc/kallsyms if possible */
  1378. if (is_host) {
  1379. proc_kallsyms:
  1380. return strdup("/proc/kallsyms");
  1381. }
  1382. /* Finally, find a cache of kallsyms */
  1383. if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
  1384. pr_err("No kallsyms or vmlinux with build-id %s was found\n",
  1385. sbuild_id);
  1386. return NULL;
  1387. }
  1388. return strdup(path);
  1389. }
  1390. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1391. symbol_filter_t filter)
  1392. {
  1393. int err;
  1394. const char *kallsyms_filename = NULL;
  1395. char *kallsyms_allocated_filename = NULL;
  1396. /*
  1397. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1398. * it and only it, reporting errors to the user if it cannot be used.
  1399. *
  1400. * For instance, try to analyse an ARM perf.data file _without_ a
  1401. * build-id, or if the user specifies the wrong path to the right
  1402. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1403. * x86_86 one, on the machine where analysis is being performed, say),
  1404. * or worse, /proc/kallsyms.
  1405. *
  1406. * If the specified file _has_ a build-id and there is a build-id
  1407. * section in the perf.data file, we will still do the expected
  1408. * validation in dso__load_vmlinux and will bail out if they don't
  1409. * match.
  1410. */
  1411. if (symbol_conf.kallsyms_name != NULL) {
  1412. kallsyms_filename = symbol_conf.kallsyms_name;
  1413. goto do_kallsyms;
  1414. }
  1415. if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
  1416. return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
  1417. false, filter);
  1418. }
  1419. if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
  1420. err = dso__load_vmlinux_path(dso, map, filter);
  1421. if (err > 0)
  1422. return err;
  1423. }
  1424. /* do not try local files if a symfs was given */
  1425. if (symbol_conf.symfs[0] != 0)
  1426. return -1;
  1427. kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
  1428. if (!kallsyms_allocated_filename)
  1429. return -1;
  1430. kallsyms_filename = kallsyms_allocated_filename;
  1431. do_kallsyms:
  1432. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1433. if (err > 0)
  1434. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1435. free(kallsyms_allocated_filename);
  1436. if (err > 0 && !dso__is_kcore(dso)) {
  1437. dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
  1438. dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
  1439. map__fixup_start(map);
  1440. map__fixup_end(map);
  1441. }
  1442. return err;
  1443. }
  1444. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1445. symbol_filter_t filter)
  1446. {
  1447. int err;
  1448. const char *kallsyms_filename = NULL;
  1449. struct machine *machine;
  1450. char path[PATH_MAX];
  1451. if (!map->groups) {
  1452. pr_debug("Guest kernel map hasn't the point to groups\n");
  1453. return -1;
  1454. }
  1455. machine = map->groups->machine;
  1456. if (machine__is_default_guest(machine)) {
  1457. /*
  1458. * if the user specified a vmlinux filename, use it and only
  1459. * it, reporting errors to the user if it cannot be used.
  1460. * Or use file guest_kallsyms inputted by user on commandline
  1461. */
  1462. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1463. err = dso__load_vmlinux(dso, map,
  1464. symbol_conf.default_guest_vmlinux_name,
  1465. false, filter);
  1466. return err;
  1467. }
  1468. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1469. if (!kallsyms_filename)
  1470. return -1;
  1471. } else {
  1472. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1473. kallsyms_filename = path;
  1474. }
  1475. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1476. if (err > 0)
  1477. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1478. if (err > 0 && !dso__is_kcore(dso)) {
  1479. dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  1480. machine__mmap_name(machine, path, sizeof(path));
  1481. dso__set_long_name(dso, strdup(path), true);
  1482. map__fixup_start(map);
  1483. map__fixup_end(map);
  1484. }
  1485. return err;
  1486. }
  1487. static void vmlinux_path__exit(void)
  1488. {
  1489. while (--vmlinux_path__nr_entries >= 0)
  1490. zfree(&vmlinux_path[vmlinux_path__nr_entries]);
  1491. vmlinux_path__nr_entries = 0;
  1492. zfree(&vmlinux_path);
  1493. }
  1494. static const char * const vmlinux_paths[] = {
  1495. "vmlinux",
  1496. "/boot/vmlinux"
  1497. };
  1498. static const char * const vmlinux_paths_upd[] = {
  1499. "/boot/vmlinux-%s",
  1500. "/usr/lib/debug/boot/vmlinux-%s",
  1501. "/lib/modules/%s/build/vmlinux",
  1502. "/usr/lib/debug/lib/modules/%s/vmlinux",
  1503. "/usr/lib/debug/boot/vmlinux-%s.debug"
  1504. };
  1505. static int vmlinux_path__add(const char *new_entry)
  1506. {
  1507. vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
  1508. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1509. return -1;
  1510. ++vmlinux_path__nr_entries;
  1511. return 0;
  1512. }
  1513. static int vmlinux_path__init(struct perf_env *env)
  1514. {
  1515. struct utsname uts;
  1516. char bf[PATH_MAX];
  1517. char *kernel_version;
  1518. unsigned int i;
  1519. vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
  1520. ARRAY_SIZE(vmlinux_paths_upd)));
  1521. if (vmlinux_path == NULL)
  1522. return -1;
  1523. for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
  1524. if (vmlinux_path__add(vmlinux_paths[i]) < 0)
  1525. goto out_fail;
  1526. /* only try kernel version if no symfs was given */
  1527. if (symbol_conf.symfs[0] != 0)
  1528. return 0;
  1529. if (env) {
  1530. kernel_version = env->os_release;
  1531. } else {
  1532. if (uname(&uts) < 0)
  1533. goto out_fail;
  1534. kernel_version = uts.release;
  1535. }
  1536. for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
  1537. snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
  1538. if (vmlinux_path__add(bf) < 0)
  1539. goto out_fail;
  1540. }
  1541. return 0;
  1542. out_fail:
  1543. vmlinux_path__exit();
  1544. return -1;
  1545. }
  1546. int setup_list(struct strlist **list, const char *list_str,
  1547. const char *list_name)
  1548. {
  1549. if (list_str == NULL)
  1550. return 0;
  1551. *list = strlist__new(list_str, NULL);
  1552. if (!*list) {
  1553. pr_err("problems parsing %s list\n", list_name);
  1554. return -1;
  1555. }
  1556. symbol_conf.has_filter = true;
  1557. return 0;
  1558. }
  1559. int setup_intlist(struct intlist **list, const char *list_str,
  1560. const char *list_name)
  1561. {
  1562. if (list_str == NULL)
  1563. return 0;
  1564. *list = intlist__new(list_str);
  1565. if (!*list) {
  1566. pr_err("problems parsing %s list\n", list_name);
  1567. return -1;
  1568. }
  1569. return 0;
  1570. }
  1571. static bool symbol__read_kptr_restrict(void)
  1572. {
  1573. bool value = false;
  1574. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1575. if (fp != NULL) {
  1576. char line[8];
  1577. if (fgets(line, sizeof(line), fp) != NULL)
  1578. value = (geteuid() != 0) ?
  1579. (atoi(line) != 0) :
  1580. (atoi(line) == 2);
  1581. fclose(fp);
  1582. }
  1583. return value;
  1584. }
  1585. int symbol__init(struct perf_env *env)
  1586. {
  1587. const char *symfs;
  1588. if (symbol_conf.initialized)
  1589. return 0;
  1590. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1591. symbol__elf_init();
  1592. if (symbol_conf.sort_by_name)
  1593. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1594. sizeof(struct symbol));
  1595. if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
  1596. return -1;
  1597. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1598. pr_err("'.' is the only non valid --field-separator argument\n");
  1599. return -1;
  1600. }
  1601. if (setup_list(&symbol_conf.dso_list,
  1602. symbol_conf.dso_list_str, "dso") < 0)
  1603. return -1;
  1604. if (setup_list(&symbol_conf.comm_list,
  1605. symbol_conf.comm_list_str, "comm") < 0)
  1606. goto out_free_dso_list;
  1607. if (setup_intlist(&symbol_conf.pid_list,
  1608. symbol_conf.pid_list_str, "pid") < 0)
  1609. goto out_free_comm_list;
  1610. if (setup_intlist(&symbol_conf.tid_list,
  1611. symbol_conf.tid_list_str, "tid") < 0)
  1612. goto out_free_pid_list;
  1613. if (setup_list(&symbol_conf.sym_list,
  1614. symbol_conf.sym_list_str, "symbol") < 0)
  1615. goto out_free_tid_list;
  1616. /*
  1617. * A path to symbols of "/" is identical to ""
  1618. * reset here for simplicity.
  1619. */
  1620. symfs = realpath(symbol_conf.symfs, NULL);
  1621. if (symfs == NULL)
  1622. symfs = symbol_conf.symfs;
  1623. if (strcmp(symfs, "/") == 0)
  1624. symbol_conf.symfs = "";
  1625. if (symfs != symbol_conf.symfs)
  1626. free((void *)symfs);
  1627. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1628. symbol_conf.initialized = true;
  1629. return 0;
  1630. out_free_tid_list:
  1631. intlist__delete(symbol_conf.tid_list);
  1632. out_free_pid_list:
  1633. intlist__delete(symbol_conf.pid_list);
  1634. out_free_comm_list:
  1635. strlist__delete(symbol_conf.comm_list);
  1636. out_free_dso_list:
  1637. strlist__delete(symbol_conf.dso_list);
  1638. return -1;
  1639. }
  1640. void symbol__exit(void)
  1641. {
  1642. if (!symbol_conf.initialized)
  1643. return;
  1644. strlist__delete(symbol_conf.sym_list);
  1645. strlist__delete(symbol_conf.dso_list);
  1646. strlist__delete(symbol_conf.comm_list);
  1647. intlist__delete(symbol_conf.tid_list);
  1648. intlist__delete(symbol_conf.pid_list);
  1649. vmlinux_path__exit();
  1650. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1651. symbol_conf.initialized = false;
  1652. }
  1653. int symbol__config_symfs(const struct option *opt __maybe_unused,
  1654. const char *dir, int unset __maybe_unused)
  1655. {
  1656. char *bf = NULL;
  1657. int ret;
  1658. symbol_conf.symfs = strdup(dir);
  1659. if (symbol_conf.symfs == NULL)
  1660. return -ENOMEM;
  1661. /* skip the locally configured cache if a symfs is given, and
  1662. * config buildid dir to symfs/.debug
  1663. */
  1664. ret = asprintf(&bf, "%s/%s", dir, ".debug");
  1665. if (ret < 0)
  1666. return -ENOMEM;
  1667. set_buildid_dir(bf);
  1668. free(bf);
  1669. return 0;
  1670. }