symbol.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  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 "symbol.h"
  16. #include "strlist.h"
  17. #include <elf.h>
  18. #include <limits.h>
  19. #include <sys/utsname.h>
  20. #ifndef KSYM_NAME_LEN
  21. #define KSYM_NAME_LEN 256
  22. #endif
  23. static void dso_cache__free(struct rb_root *root);
  24. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  25. symbol_filter_t filter);
  26. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  27. symbol_filter_t filter);
  28. static int vmlinux_path__nr_entries;
  29. static char **vmlinux_path;
  30. struct symbol_conf symbol_conf = {
  31. .exclude_other = true,
  32. .use_modules = true,
  33. .try_vmlinux_path = true,
  34. .annotate_src = true,
  35. .symfs = "",
  36. };
  37. static enum dso_binary_type binary_type_symtab[] = {
  38. DSO_BINARY_TYPE__KALLSYMS,
  39. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  40. DSO_BINARY_TYPE__JAVA_JIT,
  41. DSO_BINARY_TYPE__DEBUGLINK,
  42. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  43. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  44. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  45. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  46. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  47. DSO_BINARY_TYPE__GUEST_KMODULE,
  48. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  49. DSO_BINARY_TYPE__NOT_FOUND,
  50. };
  51. #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  52. static enum dso_binary_type binary_type_data[] = {
  53. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  54. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  55. DSO_BINARY_TYPE__NOT_FOUND,
  56. };
  57. #define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
  58. int dso__name_len(const struct dso *dso)
  59. {
  60. if (!dso)
  61. return strlen("[unknown]");
  62. if (verbose)
  63. return dso->long_name_len;
  64. return dso->short_name_len;
  65. }
  66. bool dso__loaded(const struct dso *dso, enum map_type type)
  67. {
  68. return dso->loaded & (1 << type);
  69. }
  70. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  71. {
  72. return dso->sorted_by_name & (1 << type);
  73. }
  74. static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  75. {
  76. dso->sorted_by_name |= (1 << type);
  77. }
  78. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  79. {
  80. symbol_type = toupper(symbol_type);
  81. switch (map_type) {
  82. case MAP__FUNCTION:
  83. return symbol_type == 'T' || symbol_type == 'W';
  84. case MAP__VARIABLE:
  85. return symbol_type == 'D';
  86. default:
  87. return false;
  88. }
  89. }
  90. static int prefix_underscores_count(const char *str)
  91. {
  92. const char *tail = str;
  93. while (*tail == '_')
  94. tail++;
  95. return tail - str;
  96. }
  97. #define SYMBOL_A 0
  98. #define SYMBOL_B 1
  99. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  100. {
  101. s64 a;
  102. s64 b;
  103. /* Prefer a symbol with non zero length */
  104. a = syma->end - syma->start;
  105. b = symb->end - symb->start;
  106. if ((b == 0) && (a > 0))
  107. return SYMBOL_A;
  108. else if ((a == 0) && (b > 0))
  109. return SYMBOL_B;
  110. /* Prefer a non weak symbol over a weak one */
  111. a = syma->binding == STB_WEAK;
  112. b = symb->binding == STB_WEAK;
  113. if (b && !a)
  114. return SYMBOL_A;
  115. if (a && !b)
  116. return SYMBOL_B;
  117. /* Prefer a global symbol over a non global one */
  118. a = syma->binding == STB_GLOBAL;
  119. b = symb->binding == STB_GLOBAL;
  120. if (a && !b)
  121. return SYMBOL_A;
  122. if (b && !a)
  123. return SYMBOL_B;
  124. /* Prefer a symbol with less underscores */
  125. a = prefix_underscores_count(syma->name);
  126. b = prefix_underscores_count(symb->name);
  127. if (b > a)
  128. return SYMBOL_A;
  129. else if (a > b)
  130. return SYMBOL_B;
  131. /* If all else fails, choose the symbol with the longest name */
  132. if (strlen(syma->name) >= strlen(symb->name))
  133. return SYMBOL_A;
  134. else
  135. return SYMBOL_B;
  136. }
  137. void symbols__fixup_duplicate(struct rb_root *symbols)
  138. {
  139. struct rb_node *nd;
  140. struct symbol *curr, *next;
  141. nd = rb_first(symbols);
  142. while (nd) {
  143. curr = rb_entry(nd, struct symbol, rb_node);
  144. again:
  145. nd = rb_next(&curr->rb_node);
  146. next = rb_entry(nd, struct symbol, rb_node);
  147. if (!nd)
  148. break;
  149. if (curr->start != next->start)
  150. continue;
  151. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  152. rb_erase(&next->rb_node, symbols);
  153. goto again;
  154. } else {
  155. nd = rb_next(&curr->rb_node);
  156. rb_erase(&curr->rb_node, symbols);
  157. }
  158. }
  159. }
  160. void symbols__fixup_end(struct rb_root *symbols)
  161. {
  162. struct rb_node *nd, *prevnd = rb_first(symbols);
  163. struct symbol *curr, *prev;
  164. if (prevnd == NULL)
  165. return;
  166. curr = rb_entry(prevnd, struct symbol, rb_node);
  167. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  168. prev = curr;
  169. curr = rb_entry(nd, struct symbol, rb_node);
  170. if (prev->end == prev->start && prev->end != curr->start)
  171. prev->end = curr->start - 1;
  172. }
  173. /* Last entry */
  174. if (curr->end == curr->start)
  175. curr->end = roundup(curr->start, 4096);
  176. }
  177. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  178. {
  179. struct map *prev, *curr;
  180. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  181. if (prevnd == NULL)
  182. return;
  183. curr = rb_entry(prevnd, struct map, rb_node);
  184. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  185. prev = curr;
  186. curr = rb_entry(nd, struct map, rb_node);
  187. prev->end = curr->start - 1;
  188. }
  189. /*
  190. * We still haven't the actual symbols, so guess the
  191. * last map final address.
  192. */
  193. curr->end = ~0ULL;
  194. }
  195. static void map_groups__fixup_end(struct map_groups *mg)
  196. {
  197. int i;
  198. for (i = 0; i < MAP__NR_TYPES; ++i)
  199. __map_groups__fixup_end(mg, i);
  200. }
  201. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
  202. {
  203. size_t namelen = strlen(name) + 1;
  204. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  205. sizeof(*sym) + namelen));
  206. if (sym == NULL)
  207. return NULL;
  208. if (symbol_conf.priv_size)
  209. sym = ((void *)sym) + symbol_conf.priv_size;
  210. sym->start = start;
  211. sym->end = len ? start + len - 1 : start;
  212. sym->binding = binding;
  213. sym->namelen = namelen - 1;
  214. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  215. __func__, name, start, sym->end);
  216. memcpy(sym->name, name, namelen);
  217. return sym;
  218. }
  219. void symbol__delete(struct symbol *sym)
  220. {
  221. free(((void *)sym) - symbol_conf.priv_size);
  222. }
  223. static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  224. {
  225. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  226. sym->start, sym->end,
  227. sym->binding == STB_GLOBAL ? 'g' :
  228. sym->binding == STB_LOCAL ? 'l' : 'w',
  229. sym->name);
  230. }
  231. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  232. const struct addr_location *al, FILE *fp)
  233. {
  234. unsigned long offset;
  235. size_t length;
  236. if (sym && sym->name) {
  237. length = fprintf(fp, "%s", sym->name);
  238. if (al) {
  239. offset = al->addr - sym->start;
  240. length += fprintf(fp, "+0x%lx", offset);
  241. }
  242. return length;
  243. } else
  244. return fprintf(fp, "[unknown]");
  245. }
  246. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
  247. {
  248. return symbol__fprintf_symname_offs(sym, NULL, fp);
  249. }
  250. void dso__set_long_name(struct dso *dso, char *name)
  251. {
  252. if (name == NULL)
  253. return;
  254. dso->long_name = name;
  255. dso->long_name_len = strlen(name);
  256. }
  257. static void dso__set_short_name(struct dso *dso, const char *name)
  258. {
  259. if (name == NULL)
  260. return;
  261. dso->short_name = name;
  262. dso->short_name_len = strlen(name);
  263. }
  264. static void dso__set_basename(struct dso *dso)
  265. {
  266. dso__set_short_name(dso, basename(dso->long_name));
  267. }
  268. struct dso *dso__new(const char *name)
  269. {
  270. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  271. if (dso != NULL) {
  272. int i;
  273. strcpy(dso->name, name);
  274. dso__set_long_name(dso, dso->name);
  275. dso__set_short_name(dso, dso->name);
  276. for (i = 0; i < MAP__NR_TYPES; ++i)
  277. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  278. dso->cache = RB_ROOT;
  279. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  280. dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
  281. dso->loaded = 0;
  282. dso->sorted_by_name = 0;
  283. dso->has_build_id = 0;
  284. dso->kernel = DSO_TYPE_USER;
  285. dso->needs_swap = DSO_SWAP__UNSET;
  286. INIT_LIST_HEAD(&dso->node);
  287. }
  288. return dso;
  289. }
  290. static void symbols__delete(struct rb_root *symbols)
  291. {
  292. struct symbol *pos;
  293. struct rb_node *next = rb_first(symbols);
  294. while (next) {
  295. pos = rb_entry(next, struct symbol, rb_node);
  296. next = rb_next(&pos->rb_node);
  297. rb_erase(&pos->rb_node, symbols);
  298. symbol__delete(pos);
  299. }
  300. }
  301. void dso__delete(struct dso *dso)
  302. {
  303. int i;
  304. for (i = 0; i < MAP__NR_TYPES; ++i)
  305. symbols__delete(&dso->symbols[i]);
  306. if (dso->sname_alloc)
  307. free((char *)dso->short_name);
  308. if (dso->lname_alloc)
  309. free(dso->long_name);
  310. dso_cache__free(&dso->cache);
  311. free(dso);
  312. }
  313. void dso__set_build_id(struct dso *dso, void *build_id)
  314. {
  315. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  316. dso->has_build_id = 1;
  317. }
  318. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  319. {
  320. struct rb_node **p = &symbols->rb_node;
  321. struct rb_node *parent = NULL;
  322. const u64 ip = sym->start;
  323. struct symbol *s;
  324. while (*p != NULL) {
  325. parent = *p;
  326. s = rb_entry(parent, struct symbol, rb_node);
  327. if (ip < s->start)
  328. p = &(*p)->rb_left;
  329. else
  330. p = &(*p)->rb_right;
  331. }
  332. rb_link_node(&sym->rb_node, parent, p);
  333. rb_insert_color(&sym->rb_node, symbols);
  334. }
  335. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  336. {
  337. struct rb_node *n;
  338. if (symbols == NULL)
  339. return NULL;
  340. n = symbols->rb_node;
  341. while (n) {
  342. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  343. if (ip < s->start)
  344. n = n->rb_left;
  345. else if (ip > s->end)
  346. n = n->rb_right;
  347. else
  348. return s;
  349. }
  350. return NULL;
  351. }
  352. struct symbol_name_rb_node {
  353. struct rb_node rb_node;
  354. struct symbol sym;
  355. };
  356. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  357. {
  358. struct rb_node **p = &symbols->rb_node;
  359. struct rb_node *parent = NULL;
  360. struct symbol_name_rb_node *symn, *s;
  361. symn = container_of(sym, struct symbol_name_rb_node, sym);
  362. while (*p != NULL) {
  363. parent = *p;
  364. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  365. if (strcmp(sym->name, s->sym.name) < 0)
  366. p = &(*p)->rb_left;
  367. else
  368. p = &(*p)->rb_right;
  369. }
  370. rb_link_node(&symn->rb_node, parent, p);
  371. rb_insert_color(&symn->rb_node, symbols);
  372. }
  373. static void symbols__sort_by_name(struct rb_root *symbols,
  374. struct rb_root *source)
  375. {
  376. struct rb_node *nd;
  377. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  378. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  379. symbols__insert_by_name(symbols, pos);
  380. }
  381. }
  382. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  383. const char *name)
  384. {
  385. struct rb_node *n;
  386. if (symbols == NULL)
  387. return NULL;
  388. n = symbols->rb_node;
  389. while (n) {
  390. struct symbol_name_rb_node *s;
  391. int cmp;
  392. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  393. cmp = strcmp(name, s->sym.name);
  394. if (cmp < 0)
  395. n = n->rb_left;
  396. else if (cmp > 0)
  397. n = n->rb_right;
  398. else
  399. return &s->sym;
  400. }
  401. return NULL;
  402. }
  403. struct symbol *dso__find_symbol(struct dso *dso,
  404. enum map_type type, u64 addr)
  405. {
  406. return symbols__find(&dso->symbols[type], addr);
  407. }
  408. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  409. const char *name)
  410. {
  411. return symbols__find_by_name(&dso->symbol_names[type], name);
  412. }
  413. void dso__sort_by_name(struct dso *dso, enum map_type type)
  414. {
  415. dso__set_sorted_by_name(dso, type);
  416. return symbols__sort_by_name(&dso->symbol_names[type],
  417. &dso->symbols[type]);
  418. }
  419. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  420. {
  421. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  422. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  423. return fprintf(fp, "%s", sbuild_id);
  424. }
  425. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  426. enum map_type type, FILE *fp)
  427. {
  428. size_t ret = 0;
  429. struct rb_node *nd;
  430. struct symbol_name_rb_node *pos;
  431. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  432. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  433. fprintf(fp, "%s\n", pos->sym.name);
  434. }
  435. return ret;
  436. }
  437. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  438. {
  439. struct rb_node *nd;
  440. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  441. if (dso->short_name != dso->long_name)
  442. ret += fprintf(fp, "%s, ", dso->long_name);
  443. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  444. dso->loaded ? "" : "NOT ");
  445. ret += dso__fprintf_buildid(dso, fp);
  446. ret += fprintf(fp, ")\n");
  447. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  448. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  449. ret += symbol__fprintf(pos, fp);
  450. }
  451. return ret;
  452. }
  453. int kallsyms__parse(const char *filename, void *arg,
  454. int (*process_symbol)(void *arg, const char *name,
  455. char type, u64 start))
  456. {
  457. char *line = NULL;
  458. size_t n;
  459. int err = -1;
  460. FILE *file = fopen(filename, "r");
  461. if (file == NULL)
  462. goto out_failure;
  463. err = 0;
  464. while (!feof(file)) {
  465. u64 start;
  466. int line_len, len;
  467. char symbol_type;
  468. char *symbol_name;
  469. line_len = getline(&line, &n, file);
  470. if (line_len < 0 || !line)
  471. break;
  472. line[--line_len] = '\0'; /* \n */
  473. len = hex2u64(line, &start);
  474. len++;
  475. if (len + 2 >= line_len)
  476. continue;
  477. symbol_type = line[len];
  478. len += 2;
  479. symbol_name = line + len;
  480. len = line_len - len;
  481. if (len >= KSYM_NAME_LEN) {
  482. err = -1;
  483. break;
  484. }
  485. err = process_symbol(arg, symbol_name,
  486. symbol_type, start);
  487. if (err)
  488. break;
  489. }
  490. free(line);
  491. fclose(file);
  492. return err;
  493. out_failure:
  494. return -1;
  495. }
  496. struct process_kallsyms_args {
  497. struct map *map;
  498. struct dso *dso;
  499. };
  500. static u8 kallsyms2elf_type(char type)
  501. {
  502. if (type == 'W')
  503. return STB_WEAK;
  504. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  505. }
  506. static int map__process_kallsym_symbol(void *arg, const char *name,
  507. char type, u64 start)
  508. {
  509. struct symbol *sym;
  510. struct process_kallsyms_args *a = arg;
  511. struct rb_root *root = &a->dso->symbols[a->map->type];
  512. if (!symbol_type__is_a(type, a->map->type))
  513. return 0;
  514. /*
  515. * module symbols are not sorted so we add all
  516. * symbols, setting length to 0, and rely on
  517. * symbols__fixup_end() to fix it up.
  518. */
  519. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  520. if (sym == NULL)
  521. return -ENOMEM;
  522. /*
  523. * We will pass the symbols to the filter later, in
  524. * map__split_kallsyms, when we have split the maps per module
  525. */
  526. symbols__insert(root, sym);
  527. return 0;
  528. }
  529. /*
  530. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  531. * so that we can in the next step set the symbol ->end address and then
  532. * call kernel_maps__split_kallsyms.
  533. */
  534. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  535. struct map *map)
  536. {
  537. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  538. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  539. }
  540. /*
  541. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  542. * kernel range is broken in several maps, named [kernel].N, as we don't have
  543. * the original ELF section names vmlinux have.
  544. */
  545. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  546. symbol_filter_t filter)
  547. {
  548. struct map_groups *kmaps = map__kmap(map)->kmaps;
  549. struct machine *machine = kmaps->machine;
  550. struct map *curr_map = map;
  551. struct symbol *pos;
  552. int count = 0, moved = 0;
  553. struct rb_root *root = &dso->symbols[map->type];
  554. struct rb_node *next = rb_first(root);
  555. int kernel_range = 0;
  556. while (next) {
  557. char *module;
  558. pos = rb_entry(next, struct symbol, rb_node);
  559. next = rb_next(&pos->rb_node);
  560. module = strchr(pos->name, '\t');
  561. if (module) {
  562. if (!symbol_conf.use_modules)
  563. goto discard_symbol;
  564. *module++ = '\0';
  565. if (strcmp(curr_map->dso->short_name, module)) {
  566. if (curr_map != map &&
  567. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  568. machine__is_default_guest(machine)) {
  569. /*
  570. * We assume all symbols of a module are
  571. * continuous in * kallsyms, so curr_map
  572. * points to a module and all its
  573. * symbols are in its kmap. Mark it as
  574. * loaded.
  575. */
  576. dso__set_loaded(curr_map->dso,
  577. curr_map->type);
  578. }
  579. curr_map = map_groups__find_by_name(kmaps,
  580. map->type, module);
  581. if (curr_map == NULL) {
  582. pr_debug("%s/proc/{kallsyms,modules} "
  583. "inconsistency while looking "
  584. "for \"%s\" module!\n",
  585. machine->root_dir, module);
  586. curr_map = map;
  587. goto discard_symbol;
  588. }
  589. if (curr_map->dso->loaded &&
  590. !machine__is_default_guest(machine))
  591. goto discard_symbol;
  592. }
  593. /*
  594. * So that we look just like we get from .ko files,
  595. * i.e. not prelinked, relative to map->start.
  596. */
  597. pos->start = curr_map->map_ip(curr_map, pos->start);
  598. pos->end = curr_map->map_ip(curr_map, pos->end);
  599. } else if (curr_map != map) {
  600. char dso_name[PATH_MAX];
  601. struct dso *ndso;
  602. if (count == 0) {
  603. curr_map = map;
  604. goto filter_symbol;
  605. }
  606. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  607. snprintf(dso_name, sizeof(dso_name),
  608. "[guest.kernel].%d",
  609. kernel_range++);
  610. else
  611. snprintf(dso_name, sizeof(dso_name),
  612. "[kernel].%d",
  613. kernel_range++);
  614. ndso = dso__new(dso_name);
  615. if (ndso == NULL)
  616. return -1;
  617. ndso->kernel = dso->kernel;
  618. curr_map = map__new2(pos->start, ndso, map->type);
  619. if (curr_map == NULL) {
  620. dso__delete(ndso);
  621. return -1;
  622. }
  623. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  624. map_groups__insert(kmaps, curr_map);
  625. ++kernel_range;
  626. }
  627. filter_symbol:
  628. if (filter && filter(curr_map, pos)) {
  629. discard_symbol: rb_erase(&pos->rb_node, root);
  630. symbol__delete(pos);
  631. } else {
  632. if (curr_map != map) {
  633. rb_erase(&pos->rb_node, root);
  634. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  635. ++moved;
  636. } else
  637. ++count;
  638. }
  639. }
  640. if (curr_map != map &&
  641. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  642. machine__is_default_guest(kmaps->machine)) {
  643. dso__set_loaded(curr_map->dso, curr_map->type);
  644. }
  645. return count + moved;
  646. }
  647. static bool symbol__restricted_filename(const char *filename,
  648. const char *restricted_filename)
  649. {
  650. bool restricted = false;
  651. if (symbol_conf.kptr_restrict) {
  652. char *r = realpath(filename, NULL);
  653. if (r != NULL) {
  654. restricted = strcmp(r, restricted_filename) == 0;
  655. free(r);
  656. return restricted;
  657. }
  658. }
  659. return restricted;
  660. }
  661. int dso__load_kallsyms(struct dso *dso, const char *filename,
  662. struct map *map, symbol_filter_t filter)
  663. {
  664. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  665. return -1;
  666. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  667. return -1;
  668. symbols__fixup_duplicate(&dso->symbols[map->type]);
  669. symbols__fixup_end(&dso->symbols[map->type]);
  670. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  671. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  672. else
  673. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  674. return dso__split_kallsyms(dso, map, filter);
  675. }
  676. static int dso__load_perf_map(struct dso *dso, struct map *map,
  677. symbol_filter_t filter)
  678. {
  679. char *line = NULL;
  680. size_t n;
  681. FILE *file;
  682. int nr_syms = 0;
  683. file = fopen(dso->long_name, "r");
  684. if (file == NULL)
  685. goto out_failure;
  686. while (!feof(file)) {
  687. u64 start, size;
  688. struct symbol *sym;
  689. int line_len, len;
  690. line_len = getline(&line, &n, file);
  691. if (line_len < 0)
  692. break;
  693. if (!line)
  694. goto out_failure;
  695. line[--line_len] = '\0'; /* \n */
  696. len = hex2u64(line, &start);
  697. len++;
  698. if (len + 2 >= line_len)
  699. continue;
  700. len += hex2u64(line + len, &size);
  701. len++;
  702. if (len + 2 >= line_len)
  703. continue;
  704. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  705. if (sym == NULL)
  706. goto out_delete_line;
  707. if (filter && filter(map, sym))
  708. symbol__delete(sym);
  709. else {
  710. symbols__insert(&dso->symbols[map->type], sym);
  711. nr_syms++;
  712. }
  713. }
  714. free(line);
  715. fclose(file);
  716. return nr_syms;
  717. out_delete_line:
  718. free(line);
  719. out_failure:
  720. return -1;
  721. }
  722. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  723. {
  724. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  725. }
  726. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  727. {
  728. bool have_build_id = false;
  729. struct dso *pos;
  730. list_for_each_entry(pos, head, node) {
  731. if (with_hits && !pos->hit)
  732. continue;
  733. if (pos->has_build_id) {
  734. have_build_id = true;
  735. continue;
  736. }
  737. if (filename__read_build_id(pos->long_name, pos->build_id,
  738. sizeof(pos->build_id)) > 0) {
  739. have_build_id = true;
  740. pos->has_build_id = true;
  741. }
  742. }
  743. return have_build_id;
  744. }
  745. char dso__symtab_origin(const struct dso *dso)
  746. {
  747. static const char origin[] = {
  748. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  749. [DSO_BINARY_TYPE__VMLINUX] = 'v',
  750. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  751. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  752. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  753. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  754. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  755. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  756. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  757. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  758. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  759. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  760. [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
  761. };
  762. if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
  763. return '!';
  764. return origin[dso->symtab_type];
  765. }
  766. int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
  767. char *root_dir, char *file, size_t size)
  768. {
  769. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  770. int ret = 0;
  771. switch (type) {
  772. case DSO_BINARY_TYPE__DEBUGLINK: {
  773. char *debuglink;
  774. strncpy(file, dso->long_name, size);
  775. debuglink = file + dso->long_name_len;
  776. while (debuglink != file && *debuglink != '/')
  777. debuglink--;
  778. if (*debuglink == '/')
  779. debuglink++;
  780. filename__read_debuglink(dso->long_name, debuglink,
  781. size - (debuglink - file));
  782. }
  783. break;
  784. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  785. /* skip the locally configured cache if a symfs is given */
  786. if (symbol_conf.symfs[0] ||
  787. (dso__build_id_filename(dso, file, size) == NULL))
  788. ret = -1;
  789. break;
  790. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  791. snprintf(file, size, "%s/usr/lib/debug%s.debug",
  792. symbol_conf.symfs, dso->long_name);
  793. break;
  794. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  795. snprintf(file, size, "%s/usr/lib/debug%s",
  796. symbol_conf.symfs, dso->long_name);
  797. break;
  798. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  799. if (!dso->has_build_id) {
  800. ret = -1;
  801. break;
  802. }
  803. build_id__sprintf(dso->build_id,
  804. sizeof(dso->build_id),
  805. build_id_hex);
  806. snprintf(file, size,
  807. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  808. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  809. break;
  810. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  811. snprintf(file, size, "%s%s",
  812. symbol_conf.symfs, dso->long_name);
  813. break;
  814. case DSO_BINARY_TYPE__GUEST_KMODULE:
  815. snprintf(file, size, "%s%s%s", symbol_conf.symfs,
  816. root_dir, dso->long_name);
  817. break;
  818. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  819. snprintf(file, size, "%s%s", symbol_conf.symfs,
  820. dso->long_name);
  821. break;
  822. default:
  823. case DSO_BINARY_TYPE__KALLSYMS:
  824. case DSO_BINARY_TYPE__VMLINUX:
  825. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  826. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  827. case DSO_BINARY_TYPE__JAVA_JIT:
  828. case DSO_BINARY_TYPE__NOT_FOUND:
  829. ret = -1;
  830. break;
  831. }
  832. return ret;
  833. }
  834. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  835. {
  836. char *name;
  837. int ret = -1;
  838. u_int i;
  839. struct machine *machine;
  840. char *root_dir = (char *) "";
  841. int ss_pos = 0;
  842. struct symsrc ss_[2];
  843. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  844. dso__set_loaded(dso, map->type);
  845. if (dso->kernel == DSO_TYPE_KERNEL)
  846. return dso__load_kernel_sym(dso, map, filter);
  847. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  848. return dso__load_guest_kernel_sym(dso, map, filter);
  849. if (map->groups && map->groups->machine)
  850. machine = map->groups->machine;
  851. else
  852. machine = NULL;
  853. name = malloc(PATH_MAX);
  854. if (!name)
  855. return -1;
  856. dso->adjust_symbols = 0;
  857. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  858. struct stat st;
  859. if (lstat(dso->name, &st) < 0)
  860. return -1;
  861. if (st.st_uid && (st.st_uid != geteuid())) {
  862. pr_warning("File %s not owned by current user or root, "
  863. "ignoring it.\n", dso->name);
  864. return -1;
  865. }
  866. ret = dso__load_perf_map(dso, map, filter);
  867. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  868. DSO_BINARY_TYPE__NOT_FOUND;
  869. return ret;
  870. }
  871. if (machine)
  872. root_dir = machine->root_dir;
  873. /* Iterate over candidate debug images.
  874. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  875. * and/or opd section) for processing.
  876. */
  877. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  878. struct symsrc *ss = &ss_[ss_pos];
  879. bool next_slot = false;
  880. enum dso_binary_type symtab_type = binary_type_symtab[i];
  881. if (dso__binary_type_file(dso, symtab_type,
  882. root_dir, name, PATH_MAX))
  883. continue;
  884. /* Name is now the name of the next image to try */
  885. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  886. continue;
  887. if (!syms_ss && symsrc__has_symtab(ss)) {
  888. syms_ss = ss;
  889. next_slot = true;
  890. }
  891. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  892. runtime_ss = ss;
  893. next_slot = true;
  894. }
  895. if (next_slot) {
  896. ss_pos++;
  897. if (syms_ss && runtime_ss)
  898. break;
  899. }
  900. }
  901. if (!runtime_ss && !syms_ss)
  902. goto out_free;
  903. if (runtime_ss && !syms_ss) {
  904. syms_ss = runtime_ss;
  905. }
  906. /* We'll have to hope for the best */
  907. if (!runtime_ss && syms_ss)
  908. runtime_ss = syms_ss;
  909. if (syms_ss)
  910. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
  911. else
  912. ret = -1;
  913. if (ret > 0) {
  914. int nr_plt;
  915. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  916. if (nr_plt > 0)
  917. ret += nr_plt;
  918. }
  919. for (; ss_pos > 0; ss_pos--)
  920. symsrc__destroy(&ss_[ss_pos - 1]);
  921. out_free:
  922. free(name);
  923. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  924. return 0;
  925. return ret;
  926. }
  927. struct map *map_groups__find_by_name(struct map_groups *mg,
  928. enum map_type type, const char *name)
  929. {
  930. struct rb_node *nd;
  931. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  932. struct map *map = rb_entry(nd, struct map, rb_node);
  933. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  934. return map;
  935. }
  936. return NULL;
  937. }
  938. static int dso__kernel_module_get_build_id(struct dso *dso,
  939. const char *root_dir)
  940. {
  941. char filename[PATH_MAX];
  942. /*
  943. * kernel module short names are of the form "[module]" and
  944. * we need just "module" here.
  945. */
  946. const char *name = dso->short_name + 1;
  947. snprintf(filename, sizeof(filename),
  948. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  949. root_dir, (int)strlen(name) - 1, name);
  950. if (sysfs__read_build_id(filename, dso->build_id,
  951. sizeof(dso->build_id)) == 0)
  952. dso->has_build_id = true;
  953. return 0;
  954. }
  955. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  956. const char *dir_name)
  957. {
  958. struct dirent *dent;
  959. DIR *dir = opendir(dir_name);
  960. int ret = 0;
  961. if (!dir) {
  962. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  963. return -1;
  964. }
  965. while ((dent = readdir(dir)) != NULL) {
  966. char path[PATH_MAX];
  967. struct stat st;
  968. /*sshfs might return bad dent->d_type, so we have to stat*/
  969. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  970. if (stat(path, &st))
  971. continue;
  972. if (S_ISDIR(st.st_mode)) {
  973. if (!strcmp(dent->d_name, ".") ||
  974. !strcmp(dent->d_name, ".."))
  975. continue;
  976. ret = map_groups__set_modules_path_dir(mg, path);
  977. if (ret < 0)
  978. goto out;
  979. } else {
  980. char *dot = strrchr(dent->d_name, '.'),
  981. dso_name[PATH_MAX];
  982. struct map *map;
  983. char *long_name;
  984. if (dot == NULL || strcmp(dot, ".ko"))
  985. continue;
  986. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  987. (int)(dot - dent->d_name), dent->d_name);
  988. strxfrchar(dso_name, '-', '_');
  989. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  990. dso_name);
  991. if (map == NULL)
  992. continue;
  993. long_name = strdup(path);
  994. if (long_name == NULL) {
  995. ret = -1;
  996. goto out;
  997. }
  998. dso__set_long_name(map->dso, long_name);
  999. map->dso->lname_alloc = 1;
  1000. dso__kernel_module_get_build_id(map->dso, "");
  1001. }
  1002. }
  1003. out:
  1004. closedir(dir);
  1005. return ret;
  1006. }
  1007. static char *get_kernel_version(const char *root_dir)
  1008. {
  1009. char version[PATH_MAX];
  1010. FILE *file;
  1011. char *name, *tmp;
  1012. const char *prefix = "Linux version ";
  1013. sprintf(version, "%s/proc/version", root_dir);
  1014. file = fopen(version, "r");
  1015. if (!file)
  1016. return NULL;
  1017. version[0] = '\0';
  1018. tmp = fgets(version, sizeof(version), file);
  1019. fclose(file);
  1020. name = strstr(version, prefix);
  1021. if (!name)
  1022. return NULL;
  1023. name += strlen(prefix);
  1024. tmp = strchr(name, ' ');
  1025. if (tmp)
  1026. *tmp = '\0';
  1027. return strdup(name);
  1028. }
  1029. static int machine__set_modules_path(struct machine *machine)
  1030. {
  1031. char *version;
  1032. char modules_path[PATH_MAX];
  1033. version = get_kernel_version(machine->root_dir);
  1034. if (!version)
  1035. return -1;
  1036. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1037. machine->root_dir, version);
  1038. free(version);
  1039. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  1040. }
  1041. struct map *machine__new_module(struct machine *machine, u64 start,
  1042. const char *filename)
  1043. {
  1044. struct map *map;
  1045. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  1046. if (dso == NULL)
  1047. return NULL;
  1048. map = map__new2(start, dso, MAP__FUNCTION);
  1049. if (map == NULL)
  1050. return NULL;
  1051. if (machine__is_host(machine))
  1052. dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
  1053. else
  1054. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
  1055. map_groups__insert(&machine->kmaps, map);
  1056. return map;
  1057. }
  1058. static int machine__create_modules(struct machine *machine)
  1059. {
  1060. char *line = NULL;
  1061. size_t n;
  1062. FILE *file;
  1063. struct map *map;
  1064. const char *modules;
  1065. char path[PATH_MAX];
  1066. if (machine__is_default_guest(machine))
  1067. modules = symbol_conf.default_guest_modules;
  1068. else {
  1069. sprintf(path, "%s/proc/modules", machine->root_dir);
  1070. modules = path;
  1071. }
  1072. if (symbol__restricted_filename(path, "/proc/modules"))
  1073. return -1;
  1074. file = fopen(modules, "r");
  1075. if (file == NULL)
  1076. return -1;
  1077. while (!feof(file)) {
  1078. char name[PATH_MAX];
  1079. u64 start;
  1080. char *sep;
  1081. int line_len;
  1082. line_len = getline(&line, &n, file);
  1083. if (line_len < 0)
  1084. break;
  1085. if (!line)
  1086. goto out_failure;
  1087. line[--line_len] = '\0'; /* \n */
  1088. sep = strrchr(line, 'x');
  1089. if (sep == NULL)
  1090. continue;
  1091. hex2u64(sep + 1, &start);
  1092. sep = strchr(line, ' ');
  1093. if (sep == NULL)
  1094. continue;
  1095. *sep = '\0';
  1096. snprintf(name, sizeof(name), "[%s]", line);
  1097. map = machine__new_module(machine, start, name);
  1098. if (map == NULL)
  1099. goto out_delete_line;
  1100. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1101. }
  1102. free(line);
  1103. fclose(file);
  1104. return machine__set_modules_path(machine);
  1105. out_delete_line:
  1106. free(line);
  1107. out_failure:
  1108. return -1;
  1109. }
  1110. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1111. const char *vmlinux, symbol_filter_t filter)
  1112. {
  1113. int err = -1;
  1114. struct symsrc ss;
  1115. char symfs_vmlinux[PATH_MAX];
  1116. enum dso_binary_type symtab_type;
  1117. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1118. symbol_conf.symfs, vmlinux);
  1119. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1120. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1121. else
  1122. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  1123. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  1124. return -1;
  1125. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  1126. symsrc__destroy(&ss);
  1127. if (err > 0) {
  1128. dso__set_long_name(dso, (char *)vmlinux);
  1129. dso__set_loaded(dso, map->type);
  1130. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1131. }
  1132. return err;
  1133. }
  1134. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1135. symbol_filter_t filter)
  1136. {
  1137. int i, err = 0;
  1138. char *filename;
  1139. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1140. vmlinux_path__nr_entries + 1);
  1141. filename = dso__build_id_filename(dso, NULL, 0);
  1142. if (filename != NULL) {
  1143. err = dso__load_vmlinux(dso, map, filename, filter);
  1144. if (err > 0)
  1145. goto out;
  1146. free(filename);
  1147. }
  1148. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1149. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1150. if (err > 0) {
  1151. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1152. break;
  1153. }
  1154. }
  1155. out:
  1156. return err;
  1157. }
  1158. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1159. symbol_filter_t filter)
  1160. {
  1161. int err;
  1162. const char *kallsyms_filename = NULL;
  1163. char *kallsyms_allocated_filename = NULL;
  1164. /*
  1165. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1166. * it and only it, reporting errors to the user if it cannot be used.
  1167. *
  1168. * For instance, try to analyse an ARM perf.data file _without_ a
  1169. * build-id, or if the user specifies the wrong path to the right
  1170. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1171. * x86_86 one, on the machine where analysis is being performed, say),
  1172. * or worse, /proc/kallsyms.
  1173. *
  1174. * If the specified file _has_ a build-id and there is a build-id
  1175. * section in the perf.data file, we will still do the expected
  1176. * validation in dso__load_vmlinux and will bail out if they don't
  1177. * match.
  1178. */
  1179. if (symbol_conf.kallsyms_name != NULL) {
  1180. kallsyms_filename = symbol_conf.kallsyms_name;
  1181. goto do_kallsyms;
  1182. }
  1183. if (symbol_conf.vmlinux_name != NULL) {
  1184. err = dso__load_vmlinux(dso, map,
  1185. symbol_conf.vmlinux_name, filter);
  1186. if (err > 0) {
  1187. dso__set_long_name(dso,
  1188. strdup(symbol_conf.vmlinux_name));
  1189. goto out_fixup;
  1190. }
  1191. return err;
  1192. }
  1193. if (vmlinux_path != NULL) {
  1194. err = dso__load_vmlinux_path(dso, map, filter);
  1195. if (err > 0)
  1196. goto out_fixup;
  1197. }
  1198. /* do not try local files if a symfs was given */
  1199. if (symbol_conf.symfs[0] != 0)
  1200. return -1;
  1201. /*
  1202. * Say the kernel DSO was created when processing the build-id header table,
  1203. * we have a build-id, so check if it is the same as the running kernel,
  1204. * using it if it is.
  1205. */
  1206. if (dso->has_build_id) {
  1207. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1208. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1209. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1210. sizeof(kallsyms_build_id)) == 0) {
  1211. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1212. kallsyms_filename = "/proc/kallsyms";
  1213. goto do_kallsyms;
  1214. }
  1215. }
  1216. /*
  1217. * Now look if we have it on the build-id cache in
  1218. * $HOME/.debug/[kernel.kallsyms].
  1219. */
  1220. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1221. sbuild_id);
  1222. if (asprintf(&kallsyms_allocated_filename,
  1223. "%s/.debug/[kernel.kallsyms]/%s",
  1224. getenv("HOME"), sbuild_id) == -1) {
  1225. pr_err("Not enough memory for kallsyms file lookup\n");
  1226. return -1;
  1227. }
  1228. kallsyms_filename = kallsyms_allocated_filename;
  1229. if (access(kallsyms_filename, F_OK)) {
  1230. pr_err("No kallsyms or vmlinux with build-id %s "
  1231. "was found\n", sbuild_id);
  1232. free(kallsyms_allocated_filename);
  1233. return -1;
  1234. }
  1235. } else {
  1236. /*
  1237. * Last resort, if we don't have a build-id and couldn't find
  1238. * any vmlinux file, try the running kernel kallsyms table.
  1239. */
  1240. kallsyms_filename = "/proc/kallsyms";
  1241. }
  1242. do_kallsyms:
  1243. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1244. if (err > 0)
  1245. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1246. free(kallsyms_allocated_filename);
  1247. if (err > 0) {
  1248. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1249. out_fixup:
  1250. map__fixup_start(map);
  1251. map__fixup_end(map);
  1252. }
  1253. return err;
  1254. }
  1255. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1256. symbol_filter_t filter)
  1257. {
  1258. int err;
  1259. const char *kallsyms_filename = NULL;
  1260. struct machine *machine;
  1261. char path[PATH_MAX];
  1262. if (!map->groups) {
  1263. pr_debug("Guest kernel map hasn't the point to groups\n");
  1264. return -1;
  1265. }
  1266. machine = map->groups->machine;
  1267. if (machine__is_default_guest(machine)) {
  1268. /*
  1269. * if the user specified a vmlinux filename, use it and only
  1270. * it, reporting errors to the user if it cannot be used.
  1271. * Or use file guest_kallsyms inputted by user on commandline
  1272. */
  1273. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1274. err = dso__load_vmlinux(dso, map,
  1275. symbol_conf.default_guest_vmlinux_name, filter);
  1276. goto out_try_fixup;
  1277. }
  1278. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1279. if (!kallsyms_filename)
  1280. return -1;
  1281. } else {
  1282. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1283. kallsyms_filename = path;
  1284. }
  1285. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1286. if (err > 0)
  1287. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1288. out_try_fixup:
  1289. if (err > 0) {
  1290. if (kallsyms_filename != NULL) {
  1291. machine__mmap_name(machine, path, sizeof(path));
  1292. dso__set_long_name(dso, strdup(path));
  1293. }
  1294. map__fixup_start(map);
  1295. map__fixup_end(map);
  1296. }
  1297. return err;
  1298. }
  1299. void dsos__add(struct list_head *head, struct dso *dso)
  1300. {
  1301. list_add_tail(&dso->node, head);
  1302. }
  1303. struct dso *dsos__find(struct list_head *head, const char *name)
  1304. {
  1305. struct dso *pos;
  1306. list_for_each_entry(pos, head, node)
  1307. if (strcmp(pos->long_name, name) == 0)
  1308. return pos;
  1309. return NULL;
  1310. }
  1311. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1312. {
  1313. struct dso *dso = dsos__find(head, name);
  1314. if (!dso) {
  1315. dso = dso__new(name);
  1316. if (dso != NULL) {
  1317. dsos__add(head, dso);
  1318. dso__set_basename(dso);
  1319. }
  1320. }
  1321. return dso;
  1322. }
  1323. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1324. {
  1325. struct dso *pos;
  1326. size_t ret = 0;
  1327. list_for_each_entry(pos, head, node) {
  1328. int i;
  1329. for (i = 0; i < MAP__NR_TYPES; ++i)
  1330. ret += dso__fprintf(pos, i, fp);
  1331. }
  1332. return ret;
  1333. }
  1334. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
  1335. {
  1336. struct rb_node *nd;
  1337. size_t ret = 0;
  1338. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1339. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1340. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1341. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1342. }
  1343. return ret;
  1344. }
  1345. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1346. bool with_hits)
  1347. {
  1348. struct dso *pos;
  1349. size_t ret = 0;
  1350. list_for_each_entry(pos, head, node) {
  1351. if (with_hits && !pos->hit)
  1352. continue;
  1353. ret += dso__fprintf_buildid(pos, fp);
  1354. ret += fprintf(fp, " %s\n", pos->long_name);
  1355. }
  1356. return ret;
  1357. }
  1358. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  1359. bool with_hits)
  1360. {
  1361. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
  1362. __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
  1363. }
  1364. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  1365. FILE *fp, bool with_hits)
  1366. {
  1367. struct rb_node *nd;
  1368. size_t ret = 0;
  1369. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1370. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1371. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1372. }
  1373. return ret;
  1374. }
  1375. static struct dso*
  1376. dso__kernel_findnew(struct machine *machine, const char *name,
  1377. const char *short_name, int dso_type)
  1378. {
  1379. /*
  1380. * The kernel dso could be created by build_id processing.
  1381. */
  1382. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  1383. /*
  1384. * We need to run this in all cases, since during the build_id
  1385. * processing we had no idea this was the kernel dso.
  1386. */
  1387. if (dso != NULL) {
  1388. dso__set_short_name(dso, short_name);
  1389. dso->kernel = dso_type;
  1390. }
  1391. return dso;
  1392. }
  1393. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1394. {
  1395. char path[PATH_MAX];
  1396. if (machine__is_default_guest(machine))
  1397. return;
  1398. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1399. if (sysfs__read_build_id(path, dso->build_id,
  1400. sizeof(dso->build_id)) == 0)
  1401. dso->has_build_id = true;
  1402. }
  1403. static struct dso *machine__get_kernel(struct machine *machine)
  1404. {
  1405. const char *vmlinux_name = NULL;
  1406. struct dso *kernel;
  1407. if (machine__is_host(machine)) {
  1408. vmlinux_name = symbol_conf.vmlinux_name;
  1409. if (!vmlinux_name)
  1410. vmlinux_name = "[kernel.kallsyms]";
  1411. kernel = dso__kernel_findnew(machine, vmlinux_name,
  1412. "[kernel]",
  1413. DSO_TYPE_KERNEL);
  1414. } else {
  1415. char bf[PATH_MAX];
  1416. if (machine__is_default_guest(machine))
  1417. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1418. if (!vmlinux_name)
  1419. vmlinux_name = machine__mmap_name(machine, bf,
  1420. sizeof(bf));
  1421. kernel = dso__kernel_findnew(machine, vmlinux_name,
  1422. "[guest.kernel]",
  1423. DSO_TYPE_GUEST_KERNEL);
  1424. }
  1425. if (kernel != NULL && (!kernel->has_build_id))
  1426. dso__read_running_kernel_build_id(kernel, machine);
  1427. return kernel;
  1428. }
  1429. struct process_args {
  1430. u64 start;
  1431. };
  1432. static int symbol__in_kernel(void *arg, const char *name,
  1433. char type __maybe_unused, u64 start)
  1434. {
  1435. struct process_args *args = arg;
  1436. if (strchr(name, '['))
  1437. return 0;
  1438. args->start = start;
  1439. return 1;
  1440. }
  1441. /* Figure out the start address of kernel map from /proc/kallsyms */
  1442. static u64 machine__get_kernel_start_addr(struct machine *machine)
  1443. {
  1444. const char *filename;
  1445. char path[PATH_MAX];
  1446. struct process_args args;
  1447. if (machine__is_host(machine)) {
  1448. filename = "/proc/kallsyms";
  1449. } else {
  1450. if (machine__is_default_guest(machine))
  1451. filename = (char *)symbol_conf.default_guest_kallsyms;
  1452. else {
  1453. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1454. filename = path;
  1455. }
  1456. }
  1457. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1458. return 0;
  1459. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  1460. return 0;
  1461. return args.start;
  1462. }
  1463. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  1464. {
  1465. enum map_type type;
  1466. u64 start = machine__get_kernel_start_addr(machine);
  1467. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1468. struct kmap *kmap;
  1469. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  1470. if (machine->vmlinux_maps[type] == NULL)
  1471. return -1;
  1472. machine->vmlinux_maps[type]->map_ip =
  1473. machine->vmlinux_maps[type]->unmap_ip =
  1474. identity__map_ip;
  1475. kmap = map__kmap(machine->vmlinux_maps[type]);
  1476. kmap->kmaps = &machine->kmaps;
  1477. map_groups__insert(&machine->kmaps,
  1478. machine->vmlinux_maps[type]);
  1479. }
  1480. return 0;
  1481. }
  1482. void machine__destroy_kernel_maps(struct machine *machine)
  1483. {
  1484. enum map_type type;
  1485. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1486. struct kmap *kmap;
  1487. if (machine->vmlinux_maps[type] == NULL)
  1488. continue;
  1489. kmap = map__kmap(machine->vmlinux_maps[type]);
  1490. map_groups__remove(&machine->kmaps,
  1491. machine->vmlinux_maps[type]);
  1492. if (kmap->ref_reloc_sym) {
  1493. /*
  1494. * ref_reloc_sym is shared among all maps, so free just
  1495. * on one of them.
  1496. */
  1497. if (type == MAP__FUNCTION) {
  1498. free((char *)kmap->ref_reloc_sym->name);
  1499. kmap->ref_reloc_sym->name = NULL;
  1500. free(kmap->ref_reloc_sym);
  1501. }
  1502. kmap->ref_reloc_sym = NULL;
  1503. }
  1504. map__delete(machine->vmlinux_maps[type]);
  1505. machine->vmlinux_maps[type] = NULL;
  1506. }
  1507. }
  1508. int machine__create_kernel_maps(struct machine *machine)
  1509. {
  1510. struct dso *kernel = machine__get_kernel(machine);
  1511. if (kernel == NULL ||
  1512. __machine__create_kernel_maps(machine, kernel) < 0)
  1513. return -1;
  1514. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  1515. if (machine__is_host(machine))
  1516. pr_debug("Problems creating module maps, "
  1517. "continuing anyway...\n");
  1518. else
  1519. pr_debug("Problems creating module maps for guest %d, "
  1520. "continuing anyway...\n", machine->pid);
  1521. }
  1522. /*
  1523. * Now that we have all the maps created, just set the ->end of them:
  1524. */
  1525. map_groups__fixup_end(&machine->kmaps);
  1526. return 0;
  1527. }
  1528. static void vmlinux_path__exit(void)
  1529. {
  1530. while (--vmlinux_path__nr_entries >= 0) {
  1531. free(vmlinux_path[vmlinux_path__nr_entries]);
  1532. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1533. }
  1534. free(vmlinux_path);
  1535. vmlinux_path = NULL;
  1536. }
  1537. static int vmlinux_path__init(void)
  1538. {
  1539. struct utsname uts;
  1540. char bf[PATH_MAX];
  1541. vmlinux_path = malloc(sizeof(char *) * 5);
  1542. if (vmlinux_path == NULL)
  1543. return -1;
  1544. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1545. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1546. goto out_fail;
  1547. ++vmlinux_path__nr_entries;
  1548. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1549. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1550. goto out_fail;
  1551. ++vmlinux_path__nr_entries;
  1552. /* only try running kernel version if no symfs was given */
  1553. if (symbol_conf.symfs[0] != 0)
  1554. return 0;
  1555. if (uname(&uts) < 0)
  1556. return -1;
  1557. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1558. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1559. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1560. goto out_fail;
  1561. ++vmlinux_path__nr_entries;
  1562. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1563. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1564. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1565. goto out_fail;
  1566. ++vmlinux_path__nr_entries;
  1567. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1568. uts.release);
  1569. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1570. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1571. goto out_fail;
  1572. ++vmlinux_path__nr_entries;
  1573. return 0;
  1574. out_fail:
  1575. vmlinux_path__exit();
  1576. return -1;
  1577. }
  1578. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  1579. {
  1580. int i;
  1581. size_t printed = 0;
  1582. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  1583. if (kdso->has_build_id) {
  1584. char filename[PATH_MAX];
  1585. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  1586. printed += fprintf(fp, "[0] %s\n", filename);
  1587. }
  1588. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  1589. printed += fprintf(fp, "[%d] %s\n",
  1590. i + kdso->has_build_id, vmlinux_path[i]);
  1591. return printed;
  1592. }
  1593. static int setup_list(struct strlist **list, const char *list_str,
  1594. const char *list_name)
  1595. {
  1596. if (list_str == NULL)
  1597. return 0;
  1598. *list = strlist__new(true, list_str);
  1599. if (!*list) {
  1600. pr_err("problems parsing %s list\n", list_name);
  1601. return -1;
  1602. }
  1603. return 0;
  1604. }
  1605. static bool symbol__read_kptr_restrict(void)
  1606. {
  1607. bool value = false;
  1608. if (geteuid() != 0) {
  1609. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1610. if (fp != NULL) {
  1611. char line[8];
  1612. if (fgets(line, sizeof(line), fp) != NULL)
  1613. value = atoi(line) != 0;
  1614. fclose(fp);
  1615. }
  1616. }
  1617. return value;
  1618. }
  1619. int symbol__init(void)
  1620. {
  1621. const char *symfs;
  1622. if (symbol_conf.initialized)
  1623. return 0;
  1624. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1625. symbol__elf_init();
  1626. if (symbol_conf.sort_by_name)
  1627. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1628. sizeof(struct symbol));
  1629. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1630. return -1;
  1631. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1632. pr_err("'.' is the only non valid --field-separator argument\n");
  1633. return -1;
  1634. }
  1635. if (setup_list(&symbol_conf.dso_list,
  1636. symbol_conf.dso_list_str, "dso") < 0)
  1637. return -1;
  1638. if (setup_list(&symbol_conf.comm_list,
  1639. symbol_conf.comm_list_str, "comm") < 0)
  1640. goto out_free_dso_list;
  1641. if (setup_list(&symbol_conf.sym_list,
  1642. symbol_conf.sym_list_str, "symbol") < 0)
  1643. goto out_free_comm_list;
  1644. /*
  1645. * A path to symbols of "/" is identical to ""
  1646. * reset here for simplicity.
  1647. */
  1648. symfs = realpath(symbol_conf.symfs, NULL);
  1649. if (symfs == NULL)
  1650. symfs = symbol_conf.symfs;
  1651. if (strcmp(symfs, "/") == 0)
  1652. symbol_conf.symfs = "";
  1653. if (symfs != symbol_conf.symfs)
  1654. free((void *)symfs);
  1655. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1656. symbol_conf.initialized = true;
  1657. return 0;
  1658. out_free_comm_list:
  1659. strlist__delete(symbol_conf.comm_list);
  1660. out_free_dso_list:
  1661. strlist__delete(symbol_conf.dso_list);
  1662. return -1;
  1663. }
  1664. void symbol__exit(void)
  1665. {
  1666. if (!symbol_conf.initialized)
  1667. return;
  1668. strlist__delete(symbol_conf.sym_list);
  1669. strlist__delete(symbol_conf.dso_list);
  1670. strlist__delete(symbol_conf.comm_list);
  1671. vmlinux_path__exit();
  1672. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1673. symbol_conf.initialized = false;
  1674. }
  1675. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
  1676. {
  1677. struct machine *machine = machines__findnew(machines, pid);
  1678. if (machine == NULL)
  1679. return -1;
  1680. return machine__create_kernel_maps(machine);
  1681. }
  1682. int machines__create_guest_kernel_maps(struct rb_root *machines)
  1683. {
  1684. int ret = 0;
  1685. struct dirent **namelist = NULL;
  1686. int i, items = 0;
  1687. char path[PATH_MAX];
  1688. pid_t pid;
  1689. char *endp;
  1690. if (symbol_conf.default_guest_vmlinux_name ||
  1691. symbol_conf.default_guest_modules ||
  1692. symbol_conf.default_guest_kallsyms) {
  1693. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  1694. }
  1695. if (symbol_conf.guestmount) {
  1696. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  1697. if (items <= 0)
  1698. return -ENOENT;
  1699. for (i = 0; i < items; i++) {
  1700. if (!isdigit(namelist[i]->d_name[0])) {
  1701. /* Filter out . and .. */
  1702. continue;
  1703. }
  1704. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  1705. if ((*endp != '\0') ||
  1706. (endp == namelist[i]->d_name) ||
  1707. (errno == ERANGE)) {
  1708. pr_debug("invalid directory (%s). Skipping.\n",
  1709. namelist[i]->d_name);
  1710. continue;
  1711. }
  1712. sprintf(path, "%s/%s/proc/kallsyms",
  1713. symbol_conf.guestmount,
  1714. namelist[i]->d_name);
  1715. ret = access(path, R_OK);
  1716. if (ret) {
  1717. pr_debug("Can't access file %s\n", path);
  1718. goto failure;
  1719. }
  1720. machines__create_kernel_maps(machines, pid);
  1721. }
  1722. failure:
  1723. free(namelist);
  1724. }
  1725. return ret;
  1726. }
  1727. void machines__destroy_guest_kernel_maps(struct rb_root *machines)
  1728. {
  1729. struct rb_node *next = rb_first(machines);
  1730. while (next) {
  1731. struct machine *pos = rb_entry(next, struct machine, rb_node);
  1732. next = rb_next(&pos->rb_node);
  1733. rb_erase(&pos->rb_node, machines);
  1734. machine__delete(pos);
  1735. }
  1736. }
  1737. int machine__load_kallsyms(struct machine *machine, const char *filename,
  1738. enum map_type type, symbol_filter_t filter)
  1739. {
  1740. struct map *map = machine->vmlinux_maps[type];
  1741. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  1742. if (ret > 0) {
  1743. dso__set_loaded(map->dso, type);
  1744. /*
  1745. * Since /proc/kallsyms will have multiple sessions for the
  1746. * kernel, with modules between them, fixup the end of all
  1747. * sections.
  1748. */
  1749. __map_groups__fixup_end(&machine->kmaps, type);
  1750. }
  1751. return ret;
  1752. }
  1753. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  1754. symbol_filter_t filter)
  1755. {
  1756. struct map *map = machine->vmlinux_maps[type];
  1757. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  1758. if (ret > 0) {
  1759. dso__set_loaded(map->dso, type);
  1760. map__reloc_vmlinux(map);
  1761. }
  1762. return ret;
  1763. }
  1764. struct map *dso__new_map(const char *name)
  1765. {
  1766. struct map *map = NULL;
  1767. struct dso *dso = dso__new(name);
  1768. if (dso)
  1769. map = map__new2(0, dso, MAP__FUNCTION);
  1770. return map;
  1771. }
  1772. static int open_dso(struct dso *dso, struct machine *machine)
  1773. {
  1774. char *root_dir = (char *) "";
  1775. char *name;
  1776. int fd;
  1777. name = malloc(PATH_MAX);
  1778. if (!name)
  1779. return -ENOMEM;
  1780. if (machine)
  1781. root_dir = machine->root_dir;
  1782. if (dso__binary_type_file(dso, dso->data_type,
  1783. root_dir, name, PATH_MAX)) {
  1784. free(name);
  1785. return -EINVAL;
  1786. }
  1787. fd = open(name, O_RDONLY);
  1788. free(name);
  1789. return fd;
  1790. }
  1791. int dso__data_fd(struct dso *dso, struct machine *machine)
  1792. {
  1793. int i = 0;
  1794. if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
  1795. return open_dso(dso, machine);
  1796. do {
  1797. int fd;
  1798. dso->data_type = binary_type_data[i++];
  1799. fd = open_dso(dso, machine);
  1800. if (fd >= 0)
  1801. return fd;
  1802. } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
  1803. return -EINVAL;
  1804. }
  1805. static void
  1806. dso_cache__free(struct rb_root *root)
  1807. {
  1808. struct rb_node *next = rb_first(root);
  1809. while (next) {
  1810. struct dso_cache *cache;
  1811. cache = rb_entry(next, struct dso_cache, rb_node);
  1812. next = rb_next(&cache->rb_node);
  1813. rb_erase(&cache->rb_node, root);
  1814. free(cache);
  1815. }
  1816. }
  1817. static struct dso_cache*
  1818. dso_cache__find(struct rb_root *root, u64 offset)
  1819. {
  1820. struct rb_node **p = &root->rb_node;
  1821. struct rb_node *parent = NULL;
  1822. struct dso_cache *cache;
  1823. while (*p != NULL) {
  1824. u64 end;
  1825. parent = *p;
  1826. cache = rb_entry(parent, struct dso_cache, rb_node);
  1827. end = cache->offset + DSO__DATA_CACHE_SIZE;
  1828. if (offset < cache->offset)
  1829. p = &(*p)->rb_left;
  1830. else if (offset >= end)
  1831. p = &(*p)->rb_right;
  1832. else
  1833. return cache;
  1834. }
  1835. return NULL;
  1836. }
  1837. static void
  1838. dso_cache__insert(struct rb_root *root, struct dso_cache *new)
  1839. {
  1840. struct rb_node **p = &root->rb_node;
  1841. struct rb_node *parent = NULL;
  1842. struct dso_cache *cache;
  1843. u64 offset = new->offset;
  1844. while (*p != NULL) {
  1845. u64 end;
  1846. parent = *p;
  1847. cache = rb_entry(parent, struct dso_cache, rb_node);
  1848. end = cache->offset + DSO__DATA_CACHE_SIZE;
  1849. if (offset < cache->offset)
  1850. p = &(*p)->rb_left;
  1851. else if (offset >= end)
  1852. p = &(*p)->rb_right;
  1853. }
  1854. rb_link_node(&new->rb_node, parent, p);
  1855. rb_insert_color(&new->rb_node, root);
  1856. }
  1857. static ssize_t
  1858. dso_cache__memcpy(struct dso_cache *cache, u64 offset,
  1859. u8 *data, u64 size)
  1860. {
  1861. u64 cache_offset = offset - cache->offset;
  1862. u64 cache_size = min(cache->size - cache_offset, size);
  1863. memcpy(data, cache->data + cache_offset, cache_size);
  1864. return cache_size;
  1865. }
  1866. static ssize_t
  1867. dso_cache__read(struct dso *dso, struct machine *machine,
  1868. u64 offset, u8 *data, ssize_t size)
  1869. {
  1870. struct dso_cache *cache;
  1871. ssize_t ret;
  1872. int fd;
  1873. fd = dso__data_fd(dso, machine);
  1874. if (fd < 0)
  1875. return -1;
  1876. do {
  1877. u64 cache_offset;
  1878. ret = -ENOMEM;
  1879. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  1880. if (!cache)
  1881. break;
  1882. cache_offset = offset & DSO__DATA_CACHE_MASK;
  1883. ret = -EINVAL;
  1884. if (-1 == lseek(fd, cache_offset, SEEK_SET))
  1885. break;
  1886. ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
  1887. if (ret <= 0)
  1888. break;
  1889. cache->offset = cache_offset;
  1890. cache->size = ret;
  1891. dso_cache__insert(&dso->cache, cache);
  1892. ret = dso_cache__memcpy(cache, offset, data, size);
  1893. } while (0);
  1894. if (ret <= 0)
  1895. free(cache);
  1896. close(fd);
  1897. return ret;
  1898. }
  1899. static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
  1900. u64 offset, u8 *data, ssize_t size)
  1901. {
  1902. struct dso_cache *cache;
  1903. cache = dso_cache__find(&dso->cache, offset);
  1904. if (cache)
  1905. return dso_cache__memcpy(cache, offset, data, size);
  1906. else
  1907. return dso_cache__read(dso, machine, offset, data, size);
  1908. }
  1909. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  1910. u64 offset, u8 *data, ssize_t size)
  1911. {
  1912. ssize_t r = 0;
  1913. u8 *p = data;
  1914. do {
  1915. ssize_t ret;
  1916. ret = dso_cache_read(dso, machine, offset, p, size);
  1917. if (ret < 0)
  1918. return ret;
  1919. /* Reached EOF, return what we have. */
  1920. if (!ret)
  1921. break;
  1922. BUG_ON(ret > size);
  1923. r += ret;
  1924. p += ret;
  1925. offset += ret;
  1926. size -= ret;
  1927. } while (size);
  1928. return r;
  1929. }
  1930. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  1931. struct machine *machine, u64 addr,
  1932. u8 *data, ssize_t size)
  1933. {
  1934. u64 offset = map->map_ip(map, addr);
  1935. return dso__data_read_offset(dso, machine, offset, data, size);
  1936. }