symbol.c 46 KB

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