symbol.c 63 KB

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