symbol.c 55 KB

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