symbol.c 59 KB

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