symbol.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  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. static bool symbol__restricted_filename(const char *filename,
  565. const char *restricted_filename)
  566. {
  567. bool restricted = false;
  568. if (symbol_conf.kptr_restrict) {
  569. char *r = realpath(filename, NULL);
  570. if (r != NULL) {
  571. restricted = strcmp(r, restricted_filename) == 0;
  572. free(r);
  573. return restricted;
  574. }
  575. }
  576. return restricted;
  577. }
  578. int dso__load_kallsyms(struct dso *dso, const char *filename,
  579. struct map *map, symbol_filter_t filter)
  580. {
  581. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  582. return -1;
  583. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  584. return -1;
  585. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  586. dso->symtab_type = SYMTAB__GUEST_KALLSYMS;
  587. else
  588. dso->symtab_type = SYMTAB__KALLSYMS;
  589. return dso__split_kallsyms(dso, map, filter);
  590. }
  591. static int dso__load_perf_map(struct dso *dso, struct map *map,
  592. symbol_filter_t filter)
  593. {
  594. char *line = NULL;
  595. size_t n;
  596. FILE *file;
  597. int nr_syms = 0;
  598. file = fopen(dso->long_name, "r");
  599. if (file == NULL)
  600. goto out_failure;
  601. while (!feof(file)) {
  602. u64 start, size;
  603. struct symbol *sym;
  604. int line_len, len;
  605. line_len = getline(&line, &n, file);
  606. if (line_len < 0)
  607. break;
  608. if (!line)
  609. goto out_failure;
  610. line[--line_len] = '\0'; /* \n */
  611. len = hex2u64(line, &start);
  612. len++;
  613. if (len + 2 >= line_len)
  614. continue;
  615. len += hex2u64(line + len, &size);
  616. len++;
  617. if (len + 2 >= line_len)
  618. continue;
  619. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  620. if (sym == NULL)
  621. goto out_delete_line;
  622. if (filter && filter(map, sym))
  623. symbol__delete(sym);
  624. else {
  625. symbols__insert(&dso->symbols[map->type], sym);
  626. nr_syms++;
  627. }
  628. }
  629. free(line);
  630. fclose(file);
  631. return nr_syms;
  632. out_delete_line:
  633. free(line);
  634. out_failure:
  635. return -1;
  636. }
  637. /**
  638. * elf_symtab__for_each_symbol - iterate thru all the symbols
  639. *
  640. * @syms: struct elf_symtab instance to iterate
  641. * @idx: uint32_t idx
  642. * @sym: GElf_Sym iterator
  643. */
  644. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  645. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  646. idx < nr_syms; \
  647. idx++, gelf_getsym(syms, idx, &sym))
  648. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  649. {
  650. return GELF_ST_TYPE(sym->st_info);
  651. }
  652. static inline int elf_sym__is_function(const GElf_Sym *sym)
  653. {
  654. return elf_sym__type(sym) == STT_FUNC &&
  655. sym->st_name != 0 &&
  656. sym->st_shndx != SHN_UNDEF;
  657. }
  658. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  659. {
  660. return elf_sym__type(sym) == STT_OBJECT &&
  661. sym->st_name != 0 &&
  662. sym->st_shndx != SHN_UNDEF;
  663. }
  664. static inline int elf_sym__is_label(const GElf_Sym *sym)
  665. {
  666. return elf_sym__type(sym) == STT_NOTYPE &&
  667. sym->st_name != 0 &&
  668. sym->st_shndx != SHN_UNDEF &&
  669. sym->st_shndx != SHN_ABS;
  670. }
  671. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  672. const Elf_Data *secstrs)
  673. {
  674. return secstrs->d_buf + shdr->sh_name;
  675. }
  676. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  677. const Elf_Data *secstrs)
  678. {
  679. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  680. }
  681. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  682. const Elf_Data *secstrs)
  683. {
  684. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  685. }
  686. static inline const char *elf_sym__name(const GElf_Sym *sym,
  687. const Elf_Data *symstrs)
  688. {
  689. return symstrs->d_buf + sym->st_name;
  690. }
  691. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  692. GElf_Shdr *shp, const char *name,
  693. size_t *idx)
  694. {
  695. Elf_Scn *sec = NULL;
  696. size_t cnt = 1;
  697. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  698. char *str;
  699. gelf_getshdr(sec, shp);
  700. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  701. if (!strcmp(name, str)) {
  702. if (idx)
  703. *idx = cnt;
  704. break;
  705. }
  706. ++cnt;
  707. }
  708. return sec;
  709. }
  710. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  711. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  712. idx < nr_entries; \
  713. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  714. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  715. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  716. idx < nr_entries; \
  717. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  718. /*
  719. * We need to check if we have a .dynsym, so that we can handle the
  720. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  721. * .dynsym or .symtab).
  722. * And always look at the original dso, not at debuginfo packages, that
  723. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  724. */
  725. static int dso__synthesize_plt_symbols(struct dso *dso, struct map *map,
  726. symbol_filter_t filter)
  727. {
  728. uint32_t nr_rel_entries, idx;
  729. GElf_Sym sym;
  730. u64 plt_offset;
  731. GElf_Shdr shdr_plt;
  732. struct symbol *f;
  733. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  734. Elf_Data *reldata, *syms, *symstrs;
  735. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  736. size_t dynsym_idx;
  737. GElf_Ehdr ehdr;
  738. char sympltname[1024];
  739. Elf *elf;
  740. int nr = 0, symidx, fd, err = 0;
  741. char name[PATH_MAX];
  742. snprintf(name, sizeof(name), "%s%s",
  743. symbol_conf.symfs, dso->long_name);
  744. fd = open(name, O_RDONLY);
  745. if (fd < 0)
  746. goto out;
  747. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  748. if (elf == NULL)
  749. goto out_close;
  750. if (gelf_getehdr(elf, &ehdr) == NULL)
  751. goto out_elf_end;
  752. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  753. ".dynsym", &dynsym_idx);
  754. if (scn_dynsym == NULL)
  755. goto out_elf_end;
  756. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  757. ".rela.plt", NULL);
  758. if (scn_plt_rel == NULL) {
  759. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  760. ".rel.plt", NULL);
  761. if (scn_plt_rel == NULL)
  762. goto out_elf_end;
  763. }
  764. err = -1;
  765. if (shdr_rel_plt.sh_link != dynsym_idx)
  766. goto out_elf_end;
  767. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  768. goto out_elf_end;
  769. /*
  770. * Fetch the relocation section to find the idxes to the GOT
  771. * and the symbols in the .dynsym they refer to.
  772. */
  773. reldata = elf_getdata(scn_plt_rel, NULL);
  774. if (reldata == NULL)
  775. goto out_elf_end;
  776. syms = elf_getdata(scn_dynsym, NULL);
  777. if (syms == NULL)
  778. goto out_elf_end;
  779. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  780. if (scn_symstrs == NULL)
  781. goto out_elf_end;
  782. symstrs = elf_getdata(scn_symstrs, NULL);
  783. if (symstrs == NULL)
  784. goto out_elf_end;
  785. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  786. plt_offset = shdr_plt.sh_offset;
  787. if (shdr_rel_plt.sh_type == SHT_RELA) {
  788. GElf_Rela pos_mem, *pos;
  789. elf_section__for_each_rela(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(&dso->symbols[map->type], f);
  804. ++nr;
  805. }
  806. }
  807. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  808. GElf_Rel pos_mem, *pos;
  809. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  810. nr_rel_entries) {
  811. symidx = GELF_R_SYM(pos->r_info);
  812. plt_offset += shdr_plt.sh_entsize;
  813. gelf_getsym(syms, symidx, &sym);
  814. snprintf(sympltname, sizeof(sympltname),
  815. "%s@plt", elf_sym__name(&sym, symstrs));
  816. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  817. STB_GLOBAL, sympltname);
  818. if (!f)
  819. goto out_elf_end;
  820. if (filter && filter(map, f))
  821. symbol__delete(f);
  822. else {
  823. symbols__insert(&dso->symbols[map->type], f);
  824. ++nr;
  825. }
  826. }
  827. }
  828. err = 0;
  829. out_elf_end:
  830. elf_end(elf);
  831. out_close:
  832. close(fd);
  833. if (err == 0)
  834. return nr;
  835. out:
  836. pr_debug("%s: problems reading %s PLT info.\n",
  837. __func__, dso->long_name);
  838. return 0;
  839. }
  840. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  841. {
  842. switch (type) {
  843. case MAP__FUNCTION:
  844. return elf_sym__is_function(sym);
  845. case MAP__VARIABLE:
  846. return elf_sym__is_object(sym);
  847. default:
  848. return false;
  849. }
  850. }
  851. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  852. enum map_type type)
  853. {
  854. switch (type) {
  855. case MAP__FUNCTION:
  856. return elf_sec__is_text(shdr, secstrs);
  857. case MAP__VARIABLE:
  858. return elf_sec__is_data(shdr, secstrs);
  859. default:
  860. return false;
  861. }
  862. }
  863. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  864. {
  865. Elf_Scn *sec = NULL;
  866. GElf_Shdr shdr;
  867. size_t cnt = 1;
  868. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  869. gelf_getshdr(sec, &shdr);
  870. if ((addr >= shdr.sh_addr) &&
  871. (addr < (shdr.sh_addr + shdr.sh_size)))
  872. return cnt;
  873. ++cnt;
  874. }
  875. return -1;
  876. }
  877. static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
  878. int fd, symbol_filter_t filter, int kmodule,
  879. int want_symtab)
  880. {
  881. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  882. struct map *curr_map = map;
  883. struct dso *curr_dso = dso;
  884. Elf_Data *symstrs, *secstrs;
  885. uint32_t nr_syms;
  886. int err = -1;
  887. uint32_t idx;
  888. GElf_Ehdr ehdr;
  889. GElf_Shdr shdr, opdshdr;
  890. Elf_Data *syms, *opddata = NULL;
  891. GElf_Sym sym;
  892. Elf_Scn *sec, *sec_strndx, *opdsec;
  893. Elf *elf;
  894. int nr = 0;
  895. size_t opdidx = 0;
  896. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  897. if (elf == NULL) {
  898. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  899. goto out_close;
  900. }
  901. if (gelf_getehdr(elf, &ehdr) == NULL) {
  902. pr_debug("%s: cannot get elf header.\n", __func__);
  903. goto out_elf_end;
  904. }
  905. /* Always reject images with a mismatched build-id: */
  906. if (dso->has_build_id) {
  907. u8 build_id[BUILD_ID_SIZE];
  908. if (elf_read_build_id(elf, build_id,
  909. BUILD_ID_SIZE) != BUILD_ID_SIZE)
  910. goto out_elf_end;
  911. if (!dso__build_id_equal(dso, build_id))
  912. goto out_elf_end;
  913. }
  914. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  915. if (sec == NULL) {
  916. if (want_symtab)
  917. goto out_elf_end;
  918. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  919. if (sec == NULL)
  920. goto out_elf_end;
  921. }
  922. opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
  923. if (opdshdr.sh_type != SHT_PROGBITS)
  924. opdsec = NULL;
  925. if (opdsec)
  926. opddata = elf_rawdata(opdsec, NULL);
  927. syms = elf_getdata(sec, NULL);
  928. if (syms == NULL)
  929. goto out_elf_end;
  930. sec = elf_getscn(elf, shdr.sh_link);
  931. if (sec == NULL)
  932. goto out_elf_end;
  933. symstrs = elf_getdata(sec, NULL);
  934. if (symstrs == NULL)
  935. goto out_elf_end;
  936. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  937. if (sec_strndx == NULL)
  938. goto out_elf_end;
  939. secstrs = elf_getdata(sec_strndx, NULL);
  940. if (secstrs == NULL)
  941. goto out_elf_end;
  942. nr_syms = shdr.sh_size / shdr.sh_entsize;
  943. memset(&sym, 0, sizeof(sym));
  944. if (dso->kernel == DSO_TYPE_USER) {
  945. dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  946. elf_section_by_name(elf, &ehdr, &shdr,
  947. ".gnu.prelink_undo",
  948. NULL) != NULL);
  949. } else {
  950. dso->adjust_symbols = 0;
  951. }
  952. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  953. struct symbol *f;
  954. const char *elf_name = elf_sym__name(&sym, symstrs);
  955. char *demangled = NULL;
  956. int is_label = elf_sym__is_label(&sym);
  957. const char *section_name;
  958. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  959. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  960. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  961. if (!is_label && !elf_sym__is_a(&sym, map->type))
  962. continue;
  963. /* Reject ARM ELF "mapping symbols": these aren't unique and
  964. * don't identify functions, so will confuse the profile
  965. * output: */
  966. if (ehdr.e_machine == EM_ARM) {
  967. if (!strcmp(elf_name, "$a") ||
  968. !strcmp(elf_name, "$d") ||
  969. !strcmp(elf_name, "$t"))
  970. continue;
  971. }
  972. if (opdsec && sym.st_shndx == opdidx) {
  973. u32 offset = sym.st_value - opdshdr.sh_addr;
  974. u64 *opd = opddata->d_buf + offset;
  975. sym.st_value = *opd;
  976. sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
  977. }
  978. sec = elf_getscn(elf, sym.st_shndx);
  979. if (!sec)
  980. goto out_elf_end;
  981. gelf_getshdr(sec, &shdr);
  982. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  983. continue;
  984. section_name = elf_sec__name(&shdr, secstrs);
  985. /* On ARM, symbols for thumb functions have 1 added to
  986. * the symbol address as a flag - remove it */
  987. if ((ehdr.e_machine == EM_ARM) &&
  988. (map->type == MAP__FUNCTION) &&
  989. (sym.st_value & 1))
  990. --sym.st_value;
  991. if (dso->kernel != DSO_TYPE_USER || kmodule) {
  992. char dso_name[PATH_MAX];
  993. if (strcmp(section_name,
  994. (curr_dso->short_name +
  995. dso->short_name_len)) == 0)
  996. goto new_symbol;
  997. if (strcmp(section_name, ".text") == 0) {
  998. curr_map = map;
  999. curr_dso = dso;
  1000. goto new_symbol;
  1001. }
  1002. snprintf(dso_name, sizeof(dso_name),
  1003. "%s%s", dso->short_name, section_name);
  1004. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  1005. if (curr_map == NULL) {
  1006. u64 start = sym.st_value;
  1007. if (kmodule)
  1008. start += map->start + shdr.sh_offset;
  1009. curr_dso = dso__new(dso_name);
  1010. if (curr_dso == NULL)
  1011. goto out_elf_end;
  1012. curr_dso->kernel = dso->kernel;
  1013. curr_dso->long_name = dso->long_name;
  1014. curr_dso->long_name_len = dso->long_name_len;
  1015. curr_map = map__new2(start, curr_dso,
  1016. map->type);
  1017. if (curr_map == NULL) {
  1018. dso__delete(curr_dso);
  1019. goto out_elf_end;
  1020. }
  1021. curr_map->map_ip = identity__map_ip;
  1022. curr_map->unmap_ip = identity__map_ip;
  1023. curr_dso->symtab_type = dso->symtab_type;
  1024. map_groups__insert(kmap->kmaps, curr_map);
  1025. dsos__add(&dso->node, curr_dso);
  1026. dso__set_loaded(curr_dso, map->type);
  1027. } else
  1028. curr_dso = curr_map->dso;
  1029. goto new_symbol;
  1030. }
  1031. if (curr_dso->adjust_symbols) {
  1032. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  1033. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  1034. (u64)sym.st_value, (u64)shdr.sh_addr,
  1035. (u64)shdr.sh_offset);
  1036. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  1037. }
  1038. /*
  1039. * We need to figure out if the object was created from C++ sources
  1040. * DWARF DW_compile_unit has this, but we don't always have access
  1041. * to it...
  1042. */
  1043. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  1044. if (demangled != NULL)
  1045. elf_name = demangled;
  1046. new_symbol:
  1047. f = symbol__new(sym.st_value, sym.st_size,
  1048. GELF_ST_BIND(sym.st_info), elf_name);
  1049. free(demangled);
  1050. if (!f)
  1051. goto out_elf_end;
  1052. if (filter && filter(curr_map, f))
  1053. symbol__delete(f);
  1054. else {
  1055. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  1056. nr++;
  1057. }
  1058. }
  1059. /*
  1060. * For misannotated, zeroed, ASM function sizes.
  1061. */
  1062. if (nr > 0) {
  1063. symbols__fixup_end(&dso->symbols[map->type]);
  1064. if (kmap) {
  1065. /*
  1066. * We need to fixup this here too because we create new
  1067. * maps here, for things like vsyscall sections.
  1068. */
  1069. __map_groups__fixup_end(kmap->kmaps, map->type);
  1070. }
  1071. }
  1072. err = nr;
  1073. out_elf_end:
  1074. elf_end(elf);
  1075. out_close:
  1076. return err;
  1077. }
  1078. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  1079. {
  1080. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  1081. }
  1082. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  1083. {
  1084. bool have_build_id = false;
  1085. struct dso *pos;
  1086. list_for_each_entry(pos, head, node) {
  1087. if (with_hits && !pos->hit)
  1088. continue;
  1089. if (pos->has_build_id) {
  1090. have_build_id = true;
  1091. continue;
  1092. }
  1093. if (filename__read_build_id(pos->long_name, pos->build_id,
  1094. sizeof(pos->build_id)) > 0) {
  1095. have_build_id = true;
  1096. pos->has_build_id = true;
  1097. }
  1098. }
  1099. return have_build_id;
  1100. }
  1101. /*
  1102. * Align offset to 4 bytes as needed for note name and descriptor data.
  1103. */
  1104. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  1105. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  1106. {
  1107. int err = -1;
  1108. GElf_Ehdr ehdr;
  1109. GElf_Shdr shdr;
  1110. Elf_Data *data;
  1111. Elf_Scn *sec;
  1112. Elf_Kind ek;
  1113. void *ptr;
  1114. if (size < BUILD_ID_SIZE)
  1115. goto out;
  1116. ek = elf_kind(elf);
  1117. if (ek != ELF_K_ELF)
  1118. goto out;
  1119. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1120. pr_err("%s: cannot get elf header.\n", __func__);
  1121. goto out;
  1122. }
  1123. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1124. ".note.gnu.build-id", NULL);
  1125. if (sec == NULL) {
  1126. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1127. ".notes", NULL);
  1128. if (sec == NULL)
  1129. goto out;
  1130. }
  1131. data = elf_getdata(sec, NULL);
  1132. if (data == NULL)
  1133. goto out;
  1134. ptr = data->d_buf;
  1135. while (ptr < (data->d_buf + data->d_size)) {
  1136. GElf_Nhdr *nhdr = ptr;
  1137. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  1138. descsz = NOTE_ALIGN(nhdr->n_descsz);
  1139. const char *name;
  1140. ptr += sizeof(*nhdr);
  1141. name = ptr;
  1142. ptr += namesz;
  1143. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1144. nhdr->n_namesz == sizeof("GNU")) {
  1145. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1146. memcpy(bf, ptr, BUILD_ID_SIZE);
  1147. err = BUILD_ID_SIZE;
  1148. break;
  1149. }
  1150. }
  1151. ptr += descsz;
  1152. }
  1153. out:
  1154. return err;
  1155. }
  1156. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1157. {
  1158. int fd, err = -1;
  1159. Elf *elf;
  1160. if (size < BUILD_ID_SIZE)
  1161. goto out;
  1162. fd = open(filename, O_RDONLY);
  1163. if (fd < 0)
  1164. goto out;
  1165. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1166. if (elf == NULL) {
  1167. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1168. goto out_close;
  1169. }
  1170. err = elf_read_build_id(elf, bf, size);
  1171. elf_end(elf);
  1172. out_close:
  1173. close(fd);
  1174. out:
  1175. return err;
  1176. }
  1177. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1178. {
  1179. int fd, err = -1;
  1180. if (size < BUILD_ID_SIZE)
  1181. goto out;
  1182. fd = open(filename, O_RDONLY);
  1183. if (fd < 0)
  1184. goto out;
  1185. while (1) {
  1186. char bf[BUFSIZ];
  1187. GElf_Nhdr nhdr;
  1188. int namesz, descsz;
  1189. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1190. break;
  1191. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1192. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1193. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1194. nhdr.n_namesz == sizeof("GNU")) {
  1195. if (read(fd, bf, namesz) != namesz)
  1196. break;
  1197. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1198. if (read(fd, build_id,
  1199. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1200. err = 0;
  1201. break;
  1202. }
  1203. } else if (read(fd, bf, descsz) != descsz)
  1204. break;
  1205. } else {
  1206. int n = namesz + descsz;
  1207. if (read(fd, bf, n) != n)
  1208. break;
  1209. }
  1210. }
  1211. close(fd);
  1212. out:
  1213. return err;
  1214. }
  1215. char dso__symtab_origin(const struct dso *dso)
  1216. {
  1217. static const char origin[] = {
  1218. [SYMTAB__KALLSYMS] = 'k',
  1219. [SYMTAB__JAVA_JIT] = 'j',
  1220. [SYMTAB__BUILD_ID_CACHE] = 'B',
  1221. [SYMTAB__FEDORA_DEBUGINFO] = 'f',
  1222. [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
  1223. [SYMTAB__BUILDID_DEBUGINFO] = 'b',
  1224. [SYMTAB__SYSTEM_PATH_DSO] = 'd',
  1225. [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
  1226. [SYMTAB__GUEST_KALLSYMS] = 'g',
  1227. [SYMTAB__GUEST_KMODULE] = 'G',
  1228. };
  1229. if (dso == NULL || dso->symtab_type == SYMTAB__NOT_FOUND)
  1230. return '!';
  1231. return origin[dso->symtab_type];
  1232. }
  1233. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1234. {
  1235. int size = PATH_MAX;
  1236. char *name;
  1237. int ret = -1;
  1238. int fd;
  1239. struct machine *machine;
  1240. const char *root_dir;
  1241. int want_symtab;
  1242. dso__set_loaded(dso, map->type);
  1243. if (dso->kernel == DSO_TYPE_KERNEL)
  1244. return dso__load_kernel_sym(dso, map, filter);
  1245. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1246. return dso__load_guest_kernel_sym(dso, map, filter);
  1247. if (map->groups && map->groups->machine)
  1248. machine = map->groups->machine;
  1249. else
  1250. machine = NULL;
  1251. name = malloc(size);
  1252. if (!name)
  1253. return -1;
  1254. dso->adjust_symbols = 0;
  1255. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1256. ret = dso__load_perf_map(dso, map, filter);
  1257. dso->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
  1258. SYMTAB__NOT_FOUND;
  1259. return ret;
  1260. }
  1261. /* Iterate over candidate debug images.
  1262. * On the first pass, only load images if they have a full symtab.
  1263. * Failing that, do a second pass where we accept .dynsym also
  1264. */
  1265. want_symtab = 1;
  1266. restart:
  1267. for (dso->symtab_type = SYMTAB__BUILD_ID_CACHE;
  1268. dso->symtab_type != SYMTAB__NOT_FOUND;
  1269. dso->symtab_type++) {
  1270. switch (dso->symtab_type) {
  1271. case SYMTAB__BUILD_ID_CACHE:
  1272. /* skip the locally configured cache if a symfs is given */
  1273. if (symbol_conf.symfs[0] ||
  1274. (dso__build_id_filename(dso, name, size) == NULL)) {
  1275. continue;
  1276. }
  1277. break;
  1278. case SYMTAB__FEDORA_DEBUGINFO:
  1279. snprintf(name, size, "%s/usr/lib/debug%s.debug",
  1280. symbol_conf.symfs, dso->long_name);
  1281. break;
  1282. case SYMTAB__UBUNTU_DEBUGINFO:
  1283. snprintf(name, size, "%s/usr/lib/debug%s",
  1284. symbol_conf.symfs, dso->long_name);
  1285. break;
  1286. case SYMTAB__BUILDID_DEBUGINFO: {
  1287. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1288. if (!dso->has_build_id)
  1289. continue;
  1290. build_id__sprintf(dso->build_id,
  1291. sizeof(dso->build_id),
  1292. build_id_hex);
  1293. snprintf(name, size,
  1294. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  1295. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  1296. }
  1297. break;
  1298. case SYMTAB__SYSTEM_PATH_DSO:
  1299. snprintf(name, size, "%s%s",
  1300. symbol_conf.symfs, dso->long_name);
  1301. break;
  1302. case SYMTAB__GUEST_KMODULE:
  1303. if (map->groups && machine)
  1304. root_dir = machine->root_dir;
  1305. else
  1306. root_dir = "";
  1307. snprintf(name, size, "%s%s%s", symbol_conf.symfs,
  1308. root_dir, dso->long_name);
  1309. break;
  1310. case SYMTAB__SYSTEM_PATH_KMODULE:
  1311. snprintf(name, size, "%s%s", symbol_conf.symfs,
  1312. dso->long_name);
  1313. break;
  1314. default:;
  1315. }
  1316. /* Name is now the name of the next image to try */
  1317. fd = open(name, O_RDONLY);
  1318. if (fd < 0)
  1319. continue;
  1320. ret = dso__load_sym(dso, map, name, fd, filter, 0,
  1321. want_symtab);
  1322. close(fd);
  1323. /*
  1324. * Some people seem to have debuginfo files _WITHOUT_ debug
  1325. * info!?!?
  1326. */
  1327. if (!ret)
  1328. continue;
  1329. if (ret > 0) {
  1330. int nr_plt = dso__synthesize_plt_symbols(dso, map,
  1331. filter);
  1332. if (nr_plt > 0)
  1333. ret += nr_plt;
  1334. break;
  1335. }
  1336. }
  1337. /*
  1338. * If we wanted a full symtab but no image had one,
  1339. * relax our requirements and repeat the search.
  1340. */
  1341. if (ret <= 0 && want_symtab) {
  1342. want_symtab = 0;
  1343. goto restart;
  1344. }
  1345. free(name);
  1346. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1347. return 0;
  1348. return ret;
  1349. }
  1350. struct map *map_groups__find_by_name(struct map_groups *mg,
  1351. enum map_type type, const char *name)
  1352. {
  1353. struct rb_node *nd;
  1354. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1355. struct map *map = rb_entry(nd, struct map, rb_node);
  1356. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1357. return map;
  1358. }
  1359. return NULL;
  1360. }
  1361. static int dso__kernel_module_get_build_id(struct dso *dso,
  1362. const char *root_dir)
  1363. {
  1364. char filename[PATH_MAX];
  1365. /*
  1366. * kernel module short names are of the form "[module]" and
  1367. * we need just "module" here.
  1368. */
  1369. const char *name = dso->short_name + 1;
  1370. snprintf(filename, sizeof(filename),
  1371. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1372. root_dir, (int)strlen(name) - 1, name);
  1373. if (sysfs__read_build_id(filename, dso->build_id,
  1374. sizeof(dso->build_id)) == 0)
  1375. dso->has_build_id = true;
  1376. return 0;
  1377. }
  1378. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  1379. const char *dir_name)
  1380. {
  1381. struct dirent *dent;
  1382. DIR *dir = opendir(dir_name);
  1383. int ret = 0;
  1384. if (!dir) {
  1385. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1386. return -1;
  1387. }
  1388. while ((dent = readdir(dir)) != NULL) {
  1389. char path[PATH_MAX];
  1390. struct stat st;
  1391. /*sshfs might return bad dent->d_type, so we have to stat*/
  1392. sprintf(path, "%s/%s", dir_name, dent->d_name);
  1393. if (stat(path, &st))
  1394. continue;
  1395. if (S_ISDIR(st.st_mode)) {
  1396. if (!strcmp(dent->d_name, ".") ||
  1397. !strcmp(dent->d_name, ".."))
  1398. continue;
  1399. snprintf(path, sizeof(path), "%s/%s",
  1400. dir_name, dent->d_name);
  1401. ret = map_groups__set_modules_path_dir(mg, path);
  1402. if (ret < 0)
  1403. goto out;
  1404. } else {
  1405. char *dot = strrchr(dent->d_name, '.'),
  1406. dso_name[PATH_MAX];
  1407. struct map *map;
  1408. char *long_name;
  1409. if (dot == NULL || strcmp(dot, ".ko"))
  1410. continue;
  1411. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1412. (int)(dot - dent->d_name), dent->d_name);
  1413. strxfrchar(dso_name, '-', '_');
  1414. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  1415. dso_name);
  1416. if (map == NULL)
  1417. continue;
  1418. snprintf(path, sizeof(path), "%s/%s",
  1419. dir_name, dent->d_name);
  1420. long_name = strdup(path);
  1421. if (long_name == NULL) {
  1422. ret = -1;
  1423. goto out;
  1424. }
  1425. dso__set_long_name(map->dso, long_name);
  1426. map->dso->lname_alloc = 1;
  1427. dso__kernel_module_get_build_id(map->dso, "");
  1428. }
  1429. }
  1430. out:
  1431. closedir(dir);
  1432. return ret;
  1433. }
  1434. static char *get_kernel_version(const char *root_dir)
  1435. {
  1436. char version[PATH_MAX];
  1437. FILE *file;
  1438. char *name, *tmp;
  1439. const char *prefix = "Linux version ";
  1440. sprintf(version, "%s/proc/version", root_dir);
  1441. file = fopen(version, "r");
  1442. if (!file)
  1443. return NULL;
  1444. version[0] = '\0';
  1445. tmp = fgets(version, sizeof(version), file);
  1446. fclose(file);
  1447. name = strstr(version, prefix);
  1448. if (!name)
  1449. return NULL;
  1450. name += strlen(prefix);
  1451. tmp = strchr(name, ' ');
  1452. if (tmp)
  1453. *tmp = '\0';
  1454. return strdup(name);
  1455. }
  1456. static int machine__set_modules_path(struct machine *machine)
  1457. {
  1458. char *version;
  1459. char modules_path[PATH_MAX];
  1460. version = get_kernel_version(machine->root_dir);
  1461. if (!version)
  1462. return -1;
  1463. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1464. machine->root_dir, version);
  1465. free(version);
  1466. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  1467. }
  1468. /*
  1469. * Constructor variant for modules (where we know from /proc/modules where
  1470. * they are loaded) and for vmlinux, where only after we load all the
  1471. * symbols we'll know where it starts and ends.
  1472. */
  1473. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1474. {
  1475. struct map *map = calloc(1, (sizeof(*map) +
  1476. (dso->kernel ? sizeof(struct kmap) : 0)));
  1477. if (map != NULL) {
  1478. /*
  1479. * ->end will be filled after we load all the symbols
  1480. */
  1481. map__init(map, type, start, 0, 0, dso);
  1482. }
  1483. return map;
  1484. }
  1485. struct map *machine__new_module(struct machine *machine, u64 start,
  1486. const char *filename)
  1487. {
  1488. struct map *map;
  1489. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  1490. if (dso == NULL)
  1491. return NULL;
  1492. map = map__new2(start, dso, MAP__FUNCTION);
  1493. if (map == NULL)
  1494. return NULL;
  1495. if (machine__is_host(machine))
  1496. dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
  1497. else
  1498. dso->symtab_type = SYMTAB__GUEST_KMODULE;
  1499. map_groups__insert(&machine->kmaps, map);
  1500. return map;
  1501. }
  1502. static int machine__create_modules(struct machine *machine)
  1503. {
  1504. char *line = NULL;
  1505. size_t n;
  1506. FILE *file;
  1507. struct map *map;
  1508. const char *modules;
  1509. char path[PATH_MAX];
  1510. if (machine__is_default_guest(machine))
  1511. modules = symbol_conf.default_guest_modules;
  1512. else {
  1513. sprintf(path, "%s/proc/modules", machine->root_dir);
  1514. modules = path;
  1515. }
  1516. if (symbol__restricted_filename(path, "/proc/modules"))
  1517. return -1;
  1518. file = fopen(modules, "r");
  1519. if (file == NULL)
  1520. return -1;
  1521. while (!feof(file)) {
  1522. char name[PATH_MAX];
  1523. u64 start;
  1524. char *sep;
  1525. int line_len;
  1526. line_len = getline(&line, &n, file);
  1527. if (line_len < 0)
  1528. break;
  1529. if (!line)
  1530. goto out_failure;
  1531. line[--line_len] = '\0'; /* \n */
  1532. sep = strrchr(line, 'x');
  1533. if (sep == NULL)
  1534. continue;
  1535. hex2u64(sep + 1, &start);
  1536. sep = strchr(line, ' ');
  1537. if (sep == NULL)
  1538. continue;
  1539. *sep = '\0';
  1540. snprintf(name, sizeof(name), "[%s]", line);
  1541. map = machine__new_module(machine, start, name);
  1542. if (map == NULL)
  1543. goto out_delete_line;
  1544. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1545. }
  1546. free(line);
  1547. fclose(file);
  1548. return machine__set_modules_path(machine);
  1549. out_delete_line:
  1550. free(line);
  1551. out_failure:
  1552. return -1;
  1553. }
  1554. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1555. const char *vmlinux, symbol_filter_t filter)
  1556. {
  1557. int err = -1, fd;
  1558. char symfs_vmlinux[PATH_MAX];
  1559. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1560. symbol_conf.symfs, vmlinux);
  1561. fd = open(symfs_vmlinux, O_RDONLY);
  1562. if (fd < 0)
  1563. return -1;
  1564. dso__set_long_name(dso, (char *)vmlinux);
  1565. dso__set_loaded(dso, map->type);
  1566. err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
  1567. close(fd);
  1568. if (err > 0)
  1569. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1570. return err;
  1571. }
  1572. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1573. symbol_filter_t filter)
  1574. {
  1575. int i, err = 0;
  1576. char *filename;
  1577. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1578. vmlinux_path__nr_entries + 1);
  1579. filename = dso__build_id_filename(dso, NULL, 0);
  1580. if (filename != NULL) {
  1581. err = dso__load_vmlinux(dso, map, filename, filter);
  1582. if (err > 0) {
  1583. dso__set_long_name(dso, filename);
  1584. goto out;
  1585. }
  1586. free(filename);
  1587. }
  1588. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1589. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1590. if (err > 0) {
  1591. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1592. break;
  1593. }
  1594. }
  1595. out:
  1596. return err;
  1597. }
  1598. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1599. symbol_filter_t filter)
  1600. {
  1601. int err;
  1602. const char *kallsyms_filename = NULL;
  1603. char *kallsyms_allocated_filename = NULL;
  1604. /*
  1605. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1606. * it and only it, reporting errors to the user if it cannot be used.
  1607. *
  1608. * For instance, try to analyse an ARM perf.data file _without_ a
  1609. * build-id, or if the user specifies the wrong path to the right
  1610. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1611. * x86_86 one, on the machine where analysis is being performed, say),
  1612. * or worse, /proc/kallsyms.
  1613. *
  1614. * If the specified file _has_ a build-id and there is a build-id
  1615. * section in the perf.data file, we will still do the expected
  1616. * validation in dso__load_vmlinux and will bail out if they don't
  1617. * match.
  1618. */
  1619. if (symbol_conf.kallsyms_name != NULL) {
  1620. kallsyms_filename = symbol_conf.kallsyms_name;
  1621. goto do_kallsyms;
  1622. }
  1623. if (symbol_conf.vmlinux_name != NULL) {
  1624. err = dso__load_vmlinux(dso, map,
  1625. symbol_conf.vmlinux_name, filter);
  1626. if (err > 0) {
  1627. dso__set_long_name(dso,
  1628. strdup(symbol_conf.vmlinux_name));
  1629. goto out_fixup;
  1630. }
  1631. return err;
  1632. }
  1633. if (vmlinux_path != NULL) {
  1634. err = dso__load_vmlinux_path(dso, map, filter);
  1635. if (err > 0)
  1636. goto out_fixup;
  1637. }
  1638. /* do not try local files if a symfs was given */
  1639. if (symbol_conf.symfs[0] != 0)
  1640. return -1;
  1641. /*
  1642. * Say the kernel DSO was created when processing the build-id header table,
  1643. * we have a build-id, so check if it is the same as the running kernel,
  1644. * using it if it is.
  1645. */
  1646. if (dso->has_build_id) {
  1647. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1648. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1649. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1650. sizeof(kallsyms_build_id)) == 0) {
  1651. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1652. kallsyms_filename = "/proc/kallsyms";
  1653. goto do_kallsyms;
  1654. }
  1655. }
  1656. /*
  1657. * Now look if we have it on the build-id cache in
  1658. * $HOME/.debug/[kernel.kallsyms].
  1659. */
  1660. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1661. sbuild_id);
  1662. if (asprintf(&kallsyms_allocated_filename,
  1663. "%s/.debug/[kernel.kallsyms]/%s",
  1664. getenv("HOME"), sbuild_id) == -1) {
  1665. pr_err("Not enough memory for kallsyms file lookup\n");
  1666. return -1;
  1667. }
  1668. kallsyms_filename = kallsyms_allocated_filename;
  1669. if (access(kallsyms_filename, F_OK)) {
  1670. pr_err("No kallsyms or vmlinux with build-id %s "
  1671. "was found\n", sbuild_id);
  1672. free(kallsyms_allocated_filename);
  1673. return -1;
  1674. }
  1675. } else {
  1676. /*
  1677. * Last resort, if we don't have a build-id and couldn't find
  1678. * any vmlinux file, try the running kernel kallsyms table.
  1679. */
  1680. kallsyms_filename = "/proc/kallsyms";
  1681. }
  1682. do_kallsyms:
  1683. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1684. if (err > 0)
  1685. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1686. free(kallsyms_allocated_filename);
  1687. if (err > 0) {
  1688. out_fixup:
  1689. if (kallsyms_filename != NULL)
  1690. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1691. map__fixup_start(map);
  1692. map__fixup_end(map);
  1693. }
  1694. return err;
  1695. }
  1696. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1697. symbol_filter_t filter)
  1698. {
  1699. int err;
  1700. const char *kallsyms_filename = NULL;
  1701. struct machine *machine;
  1702. char path[PATH_MAX];
  1703. if (!map->groups) {
  1704. pr_debug("Guest kernel map hasn't the point to groups\n");
  1705. return -1;
  1706. }
  1707. machine = map->groups->machine;
  1708. if (machine__is_default_guest(machine)) {
  1709. /*
  1710. * if the user specified a vmlinux filename, use it and only
  1711. * it, reporting errors to the user if it cannot be used.
  1712. * Or use file guest_kallsyms inputted by user on commandline
  1713. */
  1714. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1715. err = dso__load_vmlinux(dso, map,
  1716. symbol_conf.default_guest_vmlinux_name, filter);
  1717. goto out_try_fixup;
  1718. }
  1719. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1720. if (!kallsyms_filename)
  1721. return -1;
  1722. } else {
  1723. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1724. kallsyms_filename = path;
  1725. }
  1726. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1727. if (err > 0)
  1728. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1729. out_try_fixup:
  1730. if (err > 0) {
  1731. if (kallsyms_filename != NULL) {
  1732. machine__mmap_name(machine, path, sizeof(path));
  1733. dso__set_long_name(dso, strdup(path));
  1734. }
  1735. map__fixup_start(map);
  1736. map__fixup_end(map);
  1737. }
  1738. return err;
  1739. }
  1740. static void dsos__add(struct list_head *head, struct dso *dso)
  1741. {
  1742. list_add_tail(&dso->node, head);
  1743. }
  1744. static struct dso *dsos__find(struct list_head *head, const char *name)
  1745. {
  1746. struct dso *pos;
  1747. list_for_each_entry(pos, head, node)
  1748. if (strcmp(pos->long_name, name) == 0)
  1749. return pos;
  1750. return NULL;
  1751. }
  1752. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1753. {
  1754. struct dso *dso = dsos__find(head, name);
  1755. if (!dso) {
  1756. dso = dso__new(name);
  1757. if (dso != NULL) {
  1758. dsos__add(head, dso);
  1759. dso__set_basename(dso);
  1760. }
  1761. }
  1762. return dso;
  1763. }
  1764. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1765. {
  1766. struct dso *pos;
  1767. size_t ret = 0;
  1768. list_for_each_entry(pos, head, node) {
  1769. int i;
  1770. for (i = 0; i < MAP__NR_TYPES; ++i)
  1771. ret += dso__fprintf(pos, i, fp);
  1772. }
  1773. return ret;
  1774. }
  1775. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
  1776. {
  1777. struct rb_node *nd;
  1778. size_t ret = 0;
  1779. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1780. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1781. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1782. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1783. }
  1784. return ret;
  1785. }
  1786. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1787. bool with_hits)
  1788. {
  1789. struct dso *pos;
  1790. size_t ret = 0;
  1791. list_for_each_entry(pos, head, node) {
  1792. if (with_hits && !pos->hit)
  1793. continue;
  1794. ret += dso__fprintf_buildid(pos, fp);
  1795. ret += fprintf(fp, " %s\n", pos->long_name);
  1796. }
  1797. return ret;
  1798. }
  1799. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  1800. bool with_hits)
  1801. {
  1802. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
  1803. __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
  1804. }
  1805. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  1806. FILE *fp, bool with_hits)
  1807. {
  1808. struct rb_node *nd;
  1809. size_t ret = 0;
  1810. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1811. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1812. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1813. }
  1814. return ret;
  1815. }
  1816. struct dso *dso__new_kernel(const char *name)
  1817. {
  1818. struct dso *dso = dso__new(name ?: "[kernel.kallsyms]");
  1819. if (dso != NULL) {
  1820. dso__set_short_name(dso, "[kernel]");
  1821. dso->kernel = DSO_TYPE_KERNEL;
  1822. }
  1823. return dso;
  1824. }
  1825. static struct dso *dso__new_guest_kernel(struct machine *machine,
  1826. const char *name)
  1827. {
  1828. char bf[PATH_MAX];
  1829. struct dso *dso = dso__new(name ?: machine__mmap_name(machine, bf,
  1830. sizeof(bf)));
  1831. if (dso != NULL) {
  1832. dso__set_short_name(dso, "[guest.kernel]");
  1833. dso->kernel = DSO_TYPE_GUEST_KERNEL;
  1834. }
  1835. return dso;
  1836. }
  1837. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1838. {
  1839. char path[PATH_MAX];
  1840. if (machine__is_default_guest(machine))
  1841. return;
  1842. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1843. if (sysfs__read_build_id(path, dso->build_id,
  1844. sizeof(dso->build_id)) == 0)
  1845. dso->has_build_id = true;
  1846. }
  1847. static struct dso *machine__create_kernel(struct machine *machine)
  1848. {
  1849. const char *vmlinux_name = NULL;
  1850. struct dso *kernel;
  1851. if (machine__is_host(machine)) {
  1852. vmlinux_name = symbol_conf.vmlinux_name;
  1853. kernel = dso__new_kernel(vmlinux_name);
  1854. } else {
  1855. if (machine__is_default_guest(machine))
  1856. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1857. kernel = dso__new_guest_kernel(machine, vmlinux_name);
  1858. }
  1859. if (kernel != NULL) {
  1860. dso__read_running_kernel_build_id(kernel, machine);
  1861. dsos__add(&machine->kernel_dsos, kernel);
  1862. }
  1863. return kernel;
  1864. }
  1865. struct process_args {
  1866. u64 start;
  1867. };
  1868. static int symbol__in_kernel(void *arg, const char *name,
  1869. char type __used, u64 start, u64 end __used)
  1870. {
  1871. struct process_args *args = arg;
  1872. if (strchr(name, '['))
  1873. return 0;
  1874. args->start = start;
  1875. return 1;
  1876. }
  1877. /* Figure out the start address of kernel map from /proc/kallsyms */
  1878. static u64 machine__get_kernel_start_addr(struct machine *machine)
  1879. {
  1880. const char *filename;
  1881. char path[PATH_MAX];
  1882. struct process_args args;
  1883. if (machine__is_host(machine)) {
  1884. filename = "/proc/kallsyms";
  1885. } else {
  1886. if (machine__is_default_guest(machine))
  1887. filename = (char *)symbol_conf.default_guest_kallsyms;
  1888. else {
  1889. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1890. filename = path;
  1891. }
  1892. }
  1893. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1894. return 0;
  1895. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  1896. return 0;
  1897. return args.start;
  1898. }
  1899. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  1900. {
  1901. enum map_type type;
  1902. u64 start = machine__get_kernel_start_addr(machine);
  1903. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1904. struct kmap *kmap;
  1905. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  1906. if (machine->vmlinux_maps[type] == NULL)
  1907. return -1;
  1908. machine->vmlinux_maps[type]->map_ip =
  1909. machine->vmlinux_maps[type]->unmap_ip =
  1910. identity__map_ip;
  1911. kmap = map__kmap(machine->vmlinux_maps[type]);
  1912. kmap->kmaps = &machine->kmaps;
  1913. map_groups__insert(&machine->kmaps,
  1914. machine->vmlinux_maps[type]);
  1915. }
  1916. return 0;
  1917. }
  1918. void machine__destroy_kernel_maps(struct machine *machine)
  1919. {
  1920. enum map_type type;
  1921. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1922. struct kmap *kmap;
  1923. if (machine->vmlinux_maps[type] == NULL)
  1924. continue;
  1925. kmap = map__kmap(machine->vmlinux_maps[type]);
  1926. map_groups__remove(&machine->kmaps,
  1927. machine->vmlinux_maps[type]);
  1928. if (kmap->ref_reloc_sym) {
  1929. /*
  1930. * ref_reloc_sym is shared among all maps, so free just
  1931. * on one of them.
  1932. */
  1933. if (type == MAP__FUNCTION) {
  1934. free((char *)kmap->ref_reloc_sym->name);
  1935. kmap->ref_reloc_sym->name = NULL;
  1936. free(kmap->ref_reloc_sym);
  1937. }
  1938. kmap->ref_reloc_sym = NULL;
  1939. }
  1940. map__delete(machine->vmlinux_maps[type]);
  1941. machine->vmlinux_maps[type] = NULL;
  1942. }
  1943. }
  1944. int machine__create_kernel_maps(struct machine *machine)
  1945. {
  1946. struct dso *kernel = machine__create_kernel(machine);
  1947. if (kernel == NULL ||
  1948. __machine__create_kernel_maps(machine, kernel) < 0)
  1949. return -1;
  1950. if (symbol_conf.use_modules && machine__create_modules(machine) < 0)
  1951. pr_debug("Problems creating module maps, continuing anyway...\n");
  1952. /*
  1953. * Now that we have all the maps created, just set the ->end of them:
  1954. */
  1955. map_groups__fixup_end(&machine->kmaps);
  1956. return 0;
  1957. }
  1958. static void vmlinux_path__exit(void)
  1959. {
  1960. while (--vmlinux_path__nr_entries >= 0) {
  1961. free(vmlinux_path[vmlinux_path__nr_entries]);
  1962. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1963. }
  1964. free(vmlinux_path);
  1965. vmlinux_path = NULL;
  1966. }
  1967. static int vmlinux_path__init(void)
  1968. {
  1969. struct utsname uts;
  1970. char bf[PATH_MAX];
  1971. vmlinux_path = malloc(sizeof(char *) * 5);
  1972. if (vmlinux_path == NULL)
  1973. return -1;
  1974. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1975. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1976. goto out_fail;
  1977. ++vmlinux_path__nr_entries;
  1978. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1979. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1980. goto out_fail;
  1981. ++vmlinux_path__nr_entries;
  1982. /* only try running kernel version if no symfs was given */
  1983. if (symbol_conf.symfs[0] != 0)
  1984. return 0;
  1985. if (uname(&uts) < 0)
  1986. return -1;
  1987. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1988. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1989. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1990. goto out_fail;
  1991. ++vmlinux_path__nr_entries;
  1992. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1993. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1994. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1995. goto out_fail;
  1996. ++vmlinux_path__nr_entries;
  1997. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1998. uts.release);
  1999. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2000. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2001. goto out_fail;
  2002. ++vmlinux_path__nr_entries;
  2003. return 0;
  2004. out_fail:
  2005. vmlinux_path__exit();
  2006. return -1;
  2007. }
  2008. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  2009. {
  2010. int i;
  2011. size_t printed = 0;
  2012. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  2013. if (kdso->has_build_id) {
  2014. char filename[PATH_MAX];
  2015. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  2016. printed += fprintf(fp, "[0] %s\n", filename);
  2017. }
  2018. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  2019. printed += fprintf(fp, "[%d] %s\n",
  2020. i + kdso->has_build_id, vmlinux_path[i]);
  2021. return printed;
  2022. }
  2023. static int setup_list(struct strlist **list, const char *list_str,
  2024. const char *list_name)
  2025. {
  2026. if (list_str == NULL)
  2027. return 0;
  2028. *list = strlist__new(true, list_str);
  2029. if (!*list) {
  2030. pr_err("problems parsing %s list\n", list_name);
  2031. return -1;
  2032. }
  2033. return 0;
  2034. }
  2035. static bool symbol__read_kptr_restrict(void)
  2036. {
  2037. bool value = false;
  2038. if (geteuid() != 0) {
  2039. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  2040. if (fp != NULL) {
  2041. char line[8];
  2042. if (fgets(line, sizeof(line), fp) != NULL)
  2043. value = atoi(line) != 0;
  2044. fclose(fp);
  2045. }
  2046. }
  2047. return value;
  2048. }
  2049. int symbol__init(void)
  2050. {
  2051. const char *symfs;
  2052. if (symbol_conf.initialized)
  2053. return 0;
  2054. symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
  2055. elf_version(EV_CURRENT);
  2056. if (symbol_conf.sort_by_name)
  2057. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  2058. sizeof(struct symbol));
  2059. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  2060. return -1;
  2061. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  2062. pr_err("'.' is the only non valid --field-separator argument\n");
  2063. return -1;
  2064. }
  2065. if (setup_list(&symbol_conf.dso_list,
  2066. symbol_conf.dso_list_str, "dso") < 0)
  2067. return -1;
  2068. if (setup_list(&symbol_conf.comm_list,
  2069. symbol_conf.comm_list_str, "comm") < 0)
  2070. goto out_free_dso_list;
  2071. if (setup_list(&symbol_conf.sym_list,
  2072. symbol_conf.sym_list_str, "symbol") < 0)
  2073. goto out_free_comm_list;
  2074. /*
  2075. * A path to symbols of "/" is identical to ""
  2076. * reset here for simplicity.
  2077. */
  2078. symfs = realpath(symbol_conf.symfs, NULL);
  2079. if (symfs == NULL)
  2080. symfs = symbol_conf.symfs;
  2081. if (strcmp(symfs, "/") == 0)
  2082. symbol_conf.symfs = "";
  2083. if (symfs != symbol_conf.symfs)
  2084. free((void *)symfs);
  2085. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  2086. symbol_conf.initialized = true;
  2087. return 0;
  2088. out_free_dso_list:
  2089. strlist__delete(symbol_conf.dso_list);
  2090. out_free_comm_list:
  2091. strlist__delete(symbol_conf.comm_list);
  2092. return -1;
  2093. }
  2094. void symbol__exit(void)
  2095. {
  2096. if (!symbol_conf.initialized)
  2097. return;
  2098. strlist__delete(symbol_conf.sym_list);
  2099. strlist__delete(symbol_conf.dso_list);
  2100. strlist__delete(symbol_conf.comm_list);
  2101. vmlinux_path__exit();
  2102. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  2103. symbol_conf.initialized = false;
  2104. }
  2105. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
  2106. {
  2107. struct machine *machine = machines__findnew(machines, pid);
  2108. if (machine == NULL)
  2109. return -1;
  2110. return machine__create_kernel_maps(machine);
  2111. }
  2112. static int hex(char ch)
  2113. {
  2114. if ((ch >= '0') && (ch <= '9'))
  2115. return ch - '0';
  2116. if ((ch >= 'a') && (ch <= 'f'))
  2117. return ch - 'a' + 10;
  2118. if ((ch >= 'A') && (ch <= 'F'))
  2119. return ch - 'A' + 10;
  2120. return -1;
  2121. }
  2122. /*
  2123. * While we find nice hex chars, build a long_val.
  2124. * Return number of chars processed.
  2125. */
  2126. int hex2u64(const char *ptr, u64 *long_val)
  2127. {
  2128. const char *p = ptr;
  2129. *long_val = 0;
  2130. while (*p) {
  2131. const int hex_val = hex(*p);
  2132. if (hex_val < 0)
  2133. break;
  2134. *long_val = (*long_val << 4) | hex_val;
  2135. p++;
  2136. }
  2137. return p - ptr;
  2138. }
  2139. char *strxfrchar(char *s, char from, char to)
  2140. {
  2141. char *p = s;
  2142. while ((p = strchr(p, from)) != NULL)
  2143. *p++ = to;
  2144. return s;
  2145. }
  2146. int machines__create_guest_kernel_maps(struct rb_root *machines)
  2147. {
  2148. int ret = 0;
  2149. struct dirent **namelist = NULL;
  2150. int i, items = 0;
  2151. char path[PATH_MAX];
  2152. pid_t pid;
  2153. if (symbol_conf.default_guest_vmlinux_name ||
  2154. symbol_conf.default_guest_modules ||
  2155. symbol_conf.default_guest_kallsyms) {
  2156. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  2157. }
  2158. if (symbol_conf.guestmount) {
  2159. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  2160. if (items <= 0)
  2161. return -ENOENT;
  2162. for (i = 0; i < items; i++) {
  2163. if (!isdigit(namelist[i]->d_name[0])) {
  2164. /* Filter out . and .. */
  2165. continue;
  2166. }
  2167. pid = atoi(namelist[i]->d_name);
  2168. sprintf(path, "%s/%s/proc/kallsyms",
  2169. symbol_conf.guestmount,
  2170. namelist[i]->d_name);
  2171. ret = access(path, R_OK);
  2172. if (ret) {
  2173. pr_debug("Can't access file %s\n", path);
  2174. goto failure;
  2175. }
  2176. machines__create_kernel_maps(machines, pid);
  2177. }
  2178. failure:
  2179. free(namelist);
  2180. }
  2181. return ret;
  2182. }
  2183. void machines__destroy_guest_kernel_maps(struct rb_root *machines)
  2184. {
  2185. struct rb_node *next = rb_first(machines);
  2186. while (next) {
  2187. struct machine *pos = rb_entry(next, struct machine, rb_node);
  2188. next = rb_next(&pos->rb_node);
  2189. rb_erase(&pos->rb_node, machines);
  2190. machine__delete(pos);
  2191. }
  2192. }
  2193. int machine__load_kallsyms(struct machine *machine, const char *filename,
  2194. enum map_type type, symbol_filter_t filter)
  2195. {
  2196. struct map *map = machine->vmlinux_maps[type];
  2197. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  2198. if (ret > 0) {
  2199. dso__set_loaded(map->dso, type);
  2200. /*
  2201. * Since /proc/kallsyms will have multiple sessions for the
  2202. * kernel, with modules between them, fixup the end of all
  2203. * sections.
  2204. */
  2205. __map_groups__fixup_end(&machine->kmaps, type);
  2206. }
  2207. return ret;
  2208. }
  2209. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  2210. symbol_filter_t filter)
  2211. {
  2212. struct map *map = machine->vmlinux_maps[type];
  2213. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2214. if (ret > 0) {
  2215. dso__set_loaded(map->dso, type);
  2216. map__reloc_vmlinux(map);
  2217. }
  2218. return ret;
  2219. }