symbol.c 56 KB

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