symbol.c 62 KB

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