symbol.c 63 KB

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