symbol.c 54 KB

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