symbol.c 52 KB

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