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