symbol.c 46 KB

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