symbol.c 52 KB

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