symbol.c 59 KB

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