symbol.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  1. #define _GNU_SOURCE
  2. #include <ctype.h>
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <libgen.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/param.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <inttypes.h>
  15. #include "build-id.h"
  16. #include "debug.h"
  17. #include "symbol.h"
  18. #include "strlist.h"
  19. #include <libelf.h>
  20. #include <gelf.h>
  21. #include <elf.h>
  22. #include <limits.h>
  23. #include <sys/utsname.h>
  24. #ifndef KSYM_NAME_LEN
  25. #define KSYM_NAME_LEN 128
  26. #endif
  27. #ifndef NT_GNU_BUILD_ID
  28. #define NT_GNU_BUILD_ID 3
  29. #endif
  30. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  31. static int elf_read_build_id(Elf *elf, void *bf, size_t size);
  32. static void dsos__add(struct list_head *head, struct dso *dso);
  33. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  34. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  35. symbol_filter_t filter);
  36. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  37. symbol_filter_t filter);
  38. static int vmlinux_path__nr_entries;
  39. static char **vmlinux_path;
  40. struct symbol_conf symbol_conf = {
  41. .exclude_other = true,
  42. .use_modules = true,
  43. .try_vmlinux_path = true,
  44. .symfs = "",
  45. };
  46. int dso__name_len(const struct dso *dso)
  47. {
  48. if (verbose)
  49. return dso->long_name_len;
  50. return dso->short_name_len;
  51. }
  52. bool dso__loaded(const struct dso *dso, enum map_type type)
  53. {
  54. return dso->loaded & (1 << type);
  55. }
  56. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  57. {
  58. return dso->sorted_by_name & (1 << type);
  59. }
  60. static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  61. {
  62. dso->sorted_by_name |= (1 << type);
  63. }
  64. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  65. {
  66. 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,
  971. BUILD_ID_SIZE) != BUILD_ID_SIZE)
  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. int 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. memcpy(bf, ptr, BUILD_ID_SIZE);
  1210. err = BUILD_ID_SIZE;
  1211. break;
  1212. }
  1213. }
  1214. ptr += descsz;
  1215. }
  1216. out:
  1217. return err;
  1218. }
  1219. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1220. {
  1221. int fd, err = -1;
  1222. Elf *elf;
  1223. if (size < BUILD_ID_SIZE)
  1224. goto out;
  1225. fd = open(filename, O_RDONLY);
  1226. if (fd < 0)
  1227. goto out;
  1228. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1229. if (elf == NULL) {
  1230. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1231. goto out_close;
  1232. }
  1233. err = elf_read_build_id(elf, bf, size);
  1234. elf_end(elf);
  1235. out_close:
  1236. close(fd);
  1237. out:
  1238. return err;
  1239. }
  1240. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1241. {
  1242. int fd, err = -1;
  1243. if (size < BUILD_ID_SIZE)
  1244. goto out;
  1245. fd = open(filename, O_RDONLY);
  1246. if (fd < 0)
  1247. goto out;
  1248. while (1) {
  1249. char bf[BUFSIZ];
  1250. GElf_Nhdr nhdr;
  1251. int namesz, descsz;
  1252. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1253. break;
  1254. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1255. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1256. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1257. nhdr.n_namesz == sizeof("GNU")) {
  1258. if (read(fd, bf, namesz) != namesz)
  1259. break;
  1260. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1261. if (read(fd, build_id,
  1262. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1263. err = 0;
  1264. break;
  1265. }
  1266. } else if (read(fd, bf, descsz) != descsz)
  1267. break;
  1268. } else {
  1269. int n = namesz + descsz;
  1270. if (read(fd, bf, n) != n)
  1271. break;
  1272. }
  1273. }
  1274. close(fd);
  1275. out:
  1276. return err;
  1277. }
  1278. char dso__symtab_origin(const struct dso *dso)
  1279. {
  1280. static const char origin[] = {
  1281. [SYMTAB__KALLSYMS] = 'k',
  1282. [SYMTAB__JAVA_JIT] = 'j',
  1283. [SYMTAB__BUILD_ID_CACHE] = 'B',
  1284. [SYMTAB__FEDORA_DEBUGINFO] = 'f',
  1285. [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
  1286. [SYMTAB__BUILDID_DEBUGINFO] = 'b',
  1287. [SYMTAB__SYSTEM_PATH_DSO] = 'd',
  1288. [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
  1289. [SYMTAB__GUEST_KALLSYMS] = 'g',
  1290. [SYMTAB__GUEST_KMODULE] = 'G',
  1291. };
  1292. if (dso == NULL || dso->symtab_type == SYMTAB__NOT_FOUND)
  1293. return '!';
  1294. return origin[dso->symtab_type];
  1295. }
  1296. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1297. {
  1298. int size = PATH_MAX;
  1299. char *name;
  1300. int ret = -1;
  1301. int fd;
  1302. struct machine *machine;
  1303. const char *root_dir;
  1304. int want_symtab;
  1305. dso__set_loaded(dso, map->type);
  1306. if (dso->kernel == DSO_TYPE_KERNEL)
  1307. return dso__load_kernel_sym(dso, map, filter);
  1308. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1309. return dso__load_guest_kernel_sym(dso, map, filter);
  1310. if (map->groups && map->groups->machine)
  1311. machine = map->groups->machine;
  1312. else
  1313. machine = NULL;
  1314. name = malloc(size);
  1315. if (!name)
  1316. return -1;
  1317. dso->adjust_symbols = 0;
  1318. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1319. struct stat st;
  1320. if (lstat(dso->name, &st) < 0)
  1321. return -1;
  1322. if (st.st_uid && (st.st_uid != geteuid())) {
  1323. pr_warning("File %s not owned by current user or root, "
  1324. "ignoring it.\n", dso->name);
  1325. return -1;
  1326. }
  1327. ret = dso__load_perf_map(dso, map, filter);
  1328. dso->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
  1329. SYMTAB__NOT_FOUND;
  1330. return ret;
  1331. }
  1332. /* Iterate over candidate debug images.
  1333. * On the first pass, only load images if they have a full symtab.
  1334. * Failing that, do a second pass where we accept .dynsym also
  1335. */
  1336. want_symtab = 1;
  1337. restart:
  1338. for (dso->symtab_type = SYMTAB__BUILD_ID_CACHE;
  1339. dso->symtab_type != SYMTAB__NOT_FOUND;
  1340. dso->symtab_type++) {
  1341. switch (dso->symtab_type) {
  1342. case SYMTAB__BUILD_ID_CACHE:
  1343. /* skip the locally configured cache if a symfs is given */
  1344. if (symbol_conf.symfs[0] ||
  1345. (dso__build_id_filename(dso, name, size) == NULL)) {
  1346. continue;
  1347. }
  1348. break;
  1349. case SYMTAB__FEDORA_DEBUGINFO:
  1350. snprintf(name, size, "%s/usr/lib/debug%s.debug",
  1351. symbol_conf.symfs, dso->long_name);
  1352. break;
  1353. case SYMTAB__UBUNTU_DEBUGINFO:
  1354. snprintf(name, size, "%s/usr/lib/debug%s",
  1355. symbol_conf.symfs, dso->long_name);
  1356. break;
  1357. case SYMTAB__BUILDID_DEBUGINFO: {
  1358. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1359. if (!dso->has_build_id)
  1360. continue;
  1361. build_id__sprintf(dso->build_id,
  1362. sizeof(dso->build_id),
  1363. build_id_hex);
  1364. snprintf(name, size,
  1365. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  1366. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  1367. }
  1368. break;
  1369. case SYMTAB__SYSTEM_PATH_DSO:
  1370. snprintf(name, size, "%s%s",
  1371. symbol_conf.symfs, dso->long_name);
  1372. break;
  1373. case SYMTAB__GUEST_KMODULE:
  1374. if (map->groups && machine)
  1375. root_dir = machine->root_dir;
  1376. else
  1377. root_dir = "";
  1378. snprintf(name, size, "%s%s%s", symbol_conf.symfs,
  1379. root_dir, dso->long_name);
  1380. break;
  1381. case SYMTAB__SYSTEM_PATH_KMODULE:
  1382. snprintf(name, size, "%s%s", symbol_conf.symfs,
  1383. dso->long_name);
  1384. break;
  1385. default:;
  1386. }
  1387. /* Name is now the name of the next image to try */
  1388. fd = open(name, O_RDONLY);
  1389. if (fd < 0)
  1390. continue;
  1391. ret = dso__load_sym(dso, map, name, fd, filter, 0,
  1392. want_symtab);
  1393. close(fd);
  1394. /*
  1395. * Some people seem to have debuginfo files _WITHOUT_ debug
  1396. * info!?!?
  1397. */
  1398. if (!ret)
  1399. continue;
  1400. if (ret > 0) {
  1401. int nr_plt = dso__synthesize_plt_symbols(dso, map,
  1402. filter);
  1403. if (nr_plt > 0)
  1404. ret += nr_plt;
  1405. break;
  1406. }
  1407. }
  1408. /*
  1409. * If we wanted a full symtab but no image had one,
  1410. * relax our requirements and repeat the search.
  1411. */
  1412. if (ret <= 0 && want_symtab) {
  1413. want_symtab = 0;
  1414. goto restart;
  1415. }
  1416. free(name);
  1417. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1418. return 0;
  1419. return ret;
  1420. }
  1421. struct map *map_groups__find_by_name(struct map_groups *mg,
  1422. enum map_type type, const char *name)
  1423. {
  1424. struct rb_node *nd;
  1425. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1426. struct map *map = rb_entry(nd, struct map, rb_node);
  1427. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1428. return map;
  1429. }
  1430. return NULL;
  1431. }
  1432. static int dso__kernel_module_get_build_id(struct dso *dso,
  1433. const char *root_dir)
  1434. {
  1435. char filename[PATH_MAX];
  1436. /*
  1437. * kernel module short names are of the form "[module]" and
  1438. * we need just "module" here.
  1439. */
  1440. const char *name = dso->short_name + 1;
  1441. snprintf(filename, sizeof(filename),
  1442. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1443. root_dir, (int)strlen(name) - 1, name);
  1444. if (sysfs__read_build_id(filename, dso->build_id,
  1445. sizeof(dso->build_id)) == 0)
  1446. dso->has_build_id = true;
  1447. return 0;
  1448. }
  1449. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  1450. const char *dir_name)
  1451. {
  1452. struct dirent *dent;
  1453. DIR *dir = opendir(dir_name);
  1454. int ret = 0;
  1455. if (!dir) {
  1456. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1457. return -1;
  1458. }
  1459. while ((dent = readdir(dir)) != NULL) {
  1460. char path[PATH_MAX];
  1461. struct stat st;
  1462. /*sshfs might return bad dent->d_type, so we have to stat*/
  1463. sprintf(path, "%s/%s", dir_name, dent->d_name);
  1464. if (stat(path, &st))
  1465. continue;
  1466. if (S_ISDIR(st.st_mode)) {
  1467. if (!strcmp(dent->d_name, ".") ||
  1468. !strcmp(dent->d_name, ".."))
  1469. continue;
  1470. snprintf(path, sizeof(path), "%s/%s",
  1471. dir_name, dent->d_name);
  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. snprintf(path, sizeof(path), "%s/%s",
  1490. dir_name, dent->d_name);
  1491. long_name = strdup(path);
  1492. if (long_name == NULL) {
  1493. ret = -1;
  1494. goto out;
  1495. }
  1496. dso__set_long_name(map->dso, long_name);
  1497. map->dso->lname_alloc = 1;
  1498. dso__kernel_module_get_build_id(map->dso, "");
  1499. }
  1500. }
  1501. out:
  1502. closedir(dir);
  1503. return ret;
  1504. }
  1505. static char *get_kernel_version(const char *root_dir)
  1506. {
  1507. char version[PATH_MAX];
  1508. FILE *file;
  1509. char *name, *tmp;
  1510. const char *prefix = "Linux version ";
  1511. sprintf(version, "%s/proc/version", root_dir);
  1512. file = fopen(version, "r");
  1513. if (!file)
  1514. return NULL;
  1515. version[0] = '\0';
  1516. tmp = fgets(version, sizeof(version), file);
  1517. fclose(file);
  1518. name = strstr(version, prefix);
  1519. if (!name)
  1520. return NULL;
  1521. name += strlen(prefix);
  1522. tmp = strchr(name, ' ');
  1523. if (tmp)
  1524. *tmp = '\0';
  1525. return strdup(name);
  1526. }
  1527. static int machine__set_modules_path(struct machine *machine)
  1528. {
  1529. char *version;
  1530. char modules_path[PATH_MAX];
  1531. version = get_kernel_version(machine->root_dir);
  1532. if (!version)
  1533. return -1;
  1534. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1535. machine->root_dir, version);
  1536. free(version);
  1537. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  1538. }
  1539. /*
  1540. * Constructor variant for modules (where we know from /proc/modules where
  1541. * they are loaded) and for vmlinux, where only after we load all the
  1542. * symbols we'll know where it starts and ends.
  1543. */
  1544. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1545. {
  1546. struct map *map = calloc(1, (sizeof(*map) +
  1547. (dso->kernel ? sizeof(struct kmap) : 0)));
  1548. if (map != NULL) {
  1549. /*
  1550. * ->end will be filled after we load all the symbols
  1551. */
  1552. map__init(map, type, start, 0, 0, dso);
  1553. }
  1554. return map;
  1555. }
  1556. struct map *machine__new_module(struct machine *machine, u64 start,
  1557. const char *filename)
  1558. {
  1559. struct map *map;
  1560. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  1561. if (dso == NULL)
  1562. return NULL;
  1563. map = map__new2(start, dso, MAP__FUNCTION);
  1564. if (map == NULL)
  1565. return NULL;
  1566. if (machine__is_host(machine))
  1567. dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
  1568. else
  1569. dso->symtab_type = SYMTAB__GUEST_KMODULE;
  1570. map_groups__insert(&machine->kmaps, map);
  1571. return map;
  1572. }
  1573. static int machine__create_modules(struct machine *machine)
  1574. {
  1575. char *line = NULL;
  1576. size_t n;
  1577. FILE *file;
  1578. struct map *map;
  1579. const char *modules;
  1580. char path[PATH_MAX];
  1581. if (machine__is_default_guest(machine))
  1582. modules = symbol_conf.default_guest_modules;
  1583. else {
  1584. sprintf(path, "%s/proc/modules", machine->root_dir);
  1585. modules = path;
  1586. }
  1587. if (symbol__restricted_filename(path, "/proc/modules"))
  1588. return -1;
  1589. file = fopen(modules, "r");
  1590. if (file == NULL)
  1591. return -1;
  1592. while (!feof(file)) {
  1593. char name[PATH_MAX];
  1594. u64 start;
  1595. char *sep;
  1596. int line_len;
  1597. line_len = getline(&line, &n, file);
  1598. if (line_len < 0)
  1599. break;
  1600. if (!line)
  1601. goto out_failure;
  1602. line[--line_len] = '\0'; /* \n */
  1603. sep = strrchr(line, 'x');
  1604. if (sep == NULL)
  1605. continue;
  1606. hex2u64(sep + 1, &start);
  1607. sep = strchr(line, ' ');
  1608. if (sep == NULL)
  1609. continue;
  1610. *sep = '\0';
  1611. snprintf(name, sizeof(name), "[%s]", line);
  1612. map = machine__new_module(machine, start, name);
  1613. if (map == NULL)
  1614. goto out_delete_line;
  1615. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1616. }
  1617. free(line);
  1618. fclose(file);
  1619. return machine__set_modules_path(machine);
  1620. out_delete_line:
  1621. free(line);
  1622. out_failure:
  1623. return -1;
  1624. }
  1625. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1626. const char *vmlinux, symbol_filter_t filter)
  1627. {
  1628. int err = -1, fd;
  1629. char symfs_vmlinux[PATH_MAX];
  1630. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1631. symbol_conf.symfs, vmlinux);
  1632. fd = open(symfs_vmlinux, O_RDONLY);
  1633. if (fd < 0)
  1634. return -1;
  1635. dso__set_long_name(dso, (char *)vmlinux);
  1636. dso__set_loaded(dso, map->type);
  1637. err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
  1638. close(fd);
  1639. if (err > 0)
  1640. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1641. return err;
  1642. }
  1643. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1644. symbol_filter_t filter)
  1645. {
  1646. int i, err = 0;
  1647. char *filename;
  1648. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1649. vmlinux_path__nr_entries + 1);
  1650. filename = dso__build_id_filename(dso, NULL, 0);
  1651. if (filename != NULL) {
  1652. err = dso__load_vmlinux(dso, map, filename, filter);
  1653. if (err > 0) {
  1654. dso__set_long_name(dso, filename);
  1655. goto out;
  1656. }
  1657. free(filename);
  1658. }
  1659. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1660. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1661. if (err > 0) {
  1662. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1663. break;
  1664. }
  1665. }
  1666. out:
  1667. return err;
  1668. }
  1669. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1670. symbol_filter_t filter)
  1671. {
  1672. int err;
  1673. const char *kallsyms_filename = NULL;
  1674. char *kallsyms_allocated_filename = NULL;
  1675. /*
  1676. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1677. * it and only it, reporting errors to the user if it cannot be used.
  1678. *
  1679. * For instance, try to analyse an ARM perf.data file _without_ a
  1680. * build-id, or if the user specifies the wrong path to the right
  1681. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1682. * x86_86 one, on the machine where analysis is being performed, say),
  1683. * or worse, /proc/kallsyms.
  1684. *
  1685. * If the specified file _has_ a build-id and there is a build-id
  1686. * section in the perf.data file, we will still do the expected
  1687. * validation in dso__load_vmlinux and will bail out if they don't
  1688. * match.
  1689. */
  1690. if (symbol_conf.kallsyms_name != NULL) {
  1691. kallsyms_filename = symbol_conf.kallsyms_name;
  1692. goto do_kallsyms;
  1693. }
  1694. if (symbol_conf.vmlinux_name != NULL) {
  1695. err = dso__load_vmlinux(dso, map,
  1696. symbol_conf.vmlinux_name, filter);
  1697. if (err > 0) {
  1698. dso__set_long_name(dso,
  1699. strdup(symbol_conf.vmlinux_name));
  1700. goto out_fixup;
  1701. }
  1702. return err;
  1703. }
  1704. if (vmlinux_path != NULL) {
  1705. err = dso__load_vmlinux_path(dso, map, filter);
  1706. if (err > 0)
  1707. goto out_fixup;
  1708. }
  1709. /* do not try local files if a symfs was given */
  1710. if (symbol_conf.symfs[0] != 0)
  1711. return -1;
  1712. /*
  1713. * Say the kernel DSO was created when processing the build-id header table,
  1714. * we have a build-id, so check if it is the same as the running kernel,
  1715. * using it if it is.
  1716. */
  1717. if (dso->has_build_id) {
  1718. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1719. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1720. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1721. sizeof(kallsyms_build_id)) == 0) {
  1722. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1723. kallsyms_filename = "/proc/kallsyms";
  1724. goto do_kallsyms;
  1725. }
  1726. }
  1727. /*
  1728. * Now look if we have it on the build-id cache in
  1729. * $HOME/.debug/[kernel.kallsyms].
  1730. */
  1731. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1732. sbuild_id);
  1733. if (asprintf(&kallsyms_allocated_filename,
  1734. "%s/.debug/[kernel.kallsyms]/%s",
  1735. getenv("HOME"), sbuild_id) == -1) {
  1736. pr_err("Not enough memory for kallsyms file lookup\n");
  1737. return -1;
  1738. }
  1739. kallsyms_filename = kallsyms_allocated_filename;
  1740. if (access(kallsyms_filename, F_OK)) {
  1741. pr_err("No kallsyms or vmlinux with build-id %s "
  1742. "was found\n", sbuild_id);
  1743. free(kallsyms_allocated_filename);
  1744. return -1;
  1745. }
  1746. } else {
  1747. /*
  1748. * Last resort, if we don't have a build-id and couldn't find
  1749. * any vmlinux file, try the running kernel kallsyms table.
  1750. */
  1751. kallsyms_filename = "/proc/kallsyms";
  1752. }
  1753. do_kallsyms:
  1754. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1755. if (err > 0)
  1756. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1757. free(kallsyms_allocated_filename);
  1758. if (err > 0) {
  1759. out_fixup:
  1760. if (kallsyms_filename != NULL)
  1761. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1762. map__fixup_start(map);
  1763. map__fixup_end(map);
  1764. }
  1765. return err;
  1766. }
  1767. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1768. symbol_filter_t filter)
  1769. {
  1770. int err;
  1771. const char *kallsyms_filename = NULL;
  1772. struct machine *machine;
  1773. char path[PATH_MAX];
  1774. if (!map->groups) {
  1775. pr_debug("Guest kernel map hasn't the point to groups\n");
  1776. return -1;
  1777. }
  1778. machine = map->groups->machine;
  1779. if (machine__is_default_guest(machine)) {
  1780. /*
  1781. * if the user specified a vmlinux filename, use it and only
  1782. * it, reporting errors to the user if it cannot be used.
  1783. * Or use file guest_kallsyms inputted by user on commandline
  1784. */
  1785. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1786. err = dso__load_vmlinux(dso, map,
  1787. symbol_conf.default_guest_vmlinux_name, filter);
  1788. goto out_try_fixup;
  1789. }
  1790. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1791. if (!kallsyms_filename)
  1792. return -1;
  1793. } else {
  1794. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1795. kallsyms_filename = path;
  1796. }
  1797. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1798. if (err > 0)
  1799. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1800. out_try_fixup:
  1801. if (err > 0) {
  1802. if (kallsyms_filename != NULL) {
  1803. machine__mmap_name(machine, path, sizeof(path));
  1804. dso__set_long_name(dso, strdup(path));
  1805. }
  1806. map__fixup_start(map);
  1807. map__fixup_end(map);
  1808. }
  1809. return err;
  1810. }
  1811. static void dsos__add(struct list_head *head, struct dso *dso)
  1812. {
  1813. list_add_tail(&dso->node, head);
  1814. }
  1815. static struct dso *dsos__find(struct list_head *head, const char *name)
  1816. {
  1817. struct dso *pos;
  1818. list_for_each_entry(pos, head, node)
  1819. if (strcmp(pos->long_name, name) == 0)
  1820. return pos;
  1821. return NULL;
  1822. }
  1823. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1824. {
  1825. struct dso *dso = dsos__find(head, name);
  1826. if (!dso) {
  1827. dso = dso__new(name);
  1828. if (dso != NULL) {
  1829. dsos__add(head, dso);
  1830. dso__set_basename(dso);
  1831. }
  1832. }
  1833. return dso;
  1834. }
  1835. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1836. {
  1837. struct dso *pos;
  1838. size_t ret = 0;
  1839. list_for_each_entry(pos, head, node) {
  1840. int i;
  1841. for (i = 0; i < MAP__NR_TYPES; ++i)
  1842. ret += dso__fprintf(pos, i, fp);
  1843. }
  1844. return ret;
  1845. }
  1846. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
  1847. {
  1848. struct rb_node *nd;
  1849. size_t ret = 0;
  1850. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1851. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1852. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1853. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1854. }
  1855. return ret;
  1856. }
  1857. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1858. bool with_hits)
  1859. {
  1860. struct dso *pos;
  1861. size_t ret = 0;
  1862. list_for_each_entry(pos, head, node) {
  1863. if (with_hits && !pos->hit)
  1864. continue;
  1865. ret += dso__fprintf_buildid(pos, fp);
  1866. ret += fprintf(fp, " %s\n", pos->long_name);
  1867. }
  1868. return ret;
  1869. }
  1870. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  1871. bool with_hits)
  1872. {
  1873. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
  1874. __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
  1875. }
  1876. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  1877. FILE *fp, bool with_hits)
  1878. {
  1879. struct rb_node *nd;
  1880. size_t ret = 0;
  1881. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1882. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1883. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1884. }
  1885. return ret;
  1886. }
  1887. static struct dso*
  1888. dso__kernel_findnew(struct machine *machine, const char *name,
  1889. const char *short_name, int dso_type)
  1890. {
  1891. /*
  1892. * The kernel dso could be created by build_id processing.
  1893. */
  1894. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  1895. /*
  1896. * We need to run this in all cases, since during the build_id
  1897. * processing we had no idea this was the kernel dso.
  1898. */
  1899. if (dso != NULL) {
  1900. dso__set_short_name(dso, short_name);
  1901. dso->kernel = dso_type;
  1902. }
  1903. return dso;
  1904. }
  1905. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1906. {
  1907. char path[PATH_MAX];
  1908. if (machine__is_default_guest(machine))
  1909. return;
  1910. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1911. if (sysfs__read_build_id(path, dso->build_id,
  1912. sizeof(dso->build_id)) == 0)
  1913. dso->has_build_id = true;
  1914. }
  1915. static struct dso *machine__get_kernel(struct machine *machine)
  1916. {
  1917. const char *vmlinux_name = NULL;
  1918. struct dso *kernel;
  1919. if (machine__is_host(machine)) {
  1920. vmlinux_name = symbol_conf.vmlinux_name;
  1921. if (!vmlinux_name)
  1922. vmlinux_name = "[kernel.kallsyms]";
  1923. kernel = dso__kernel_findnew(machine, vmlinux_name,
  1924. "[kernel]",
  1925. DSO_TYPE_KERNEL);
  1926. } else {
  1927. char bf[PATH_MAX];
  1928. if (machine__is_default_guest(machine))
  1929. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1930. if (!vmlinux_name)
  1931. vmlinux_name = machine__mmap_name(machine, bf,
  1932. sizeof(bf));
  1933. kernel = dso__kernel_findnew(machine, vmlinux_name,
  1934. "[guest.kernel]",
  1935. DSO_TYPE_GUEST_KERNEL);
  1936. }
  1937. if (kernel != NULL && (!kernel->has_build_id))
  1938. dso__read_running_kernel_build_id(kernel, machine);
  1939. return kernel;
  1940. }
  1941. struct process_args {
  1942. u64 start;
  1943. };
  1944. static int symbol__in_kernel(void *arg, const char *name,
  1945. char type __used, u64 start, u64 end __used)
  1946. {
  1947. struct process_args *args = arg;
  1948. if (strchr(name, '['))
  1949. return 0;
  1950. args->start = start;
  1951. return 1;
  1952. }
  1953. /* Figure out the start address of kernel map from /proc/kallsyms */
  1954. static u64 machine__get_kernel_start_addr(struct machine *machine)
  1955. {
  1956. const char *filename;
  1957. char path[PATH_MAX];
  1958. struct process_args args;
  1959. if (machine__is_host(machine)) {
  1960. filename = "/proc/kallsyms";
  1961. } else {
  1962. if (machine__is_default_guest(machine))
  1963. filename = (char *)symbol_conf.default_guest_kallsyms;
  1964. else {
  1965. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1966. filename = path;
  1967. }
  1968. }
  1969. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1970. return 0;
  1971. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  1972. return 0;
  1973. return args.start;
  1974. }
  1975. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  1976. {
  1977. enum map_type type;
  1978. u64 start = machine__get_kernel_start_addr(machine);
  1979. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1980. struct kmap *kmap;
  1981. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  1982. if (machine->vmlinux_maps[type] == NULL)
  1983. return -1;
  1984. machine->vmlinux_maps[type]->map_ip =
  1985. machine->vmlinux_maps[type]->unmap_ip =
  1986. identity__map_ip;
  1987. kmap = map__kmap(machine->vmlinux_maps[type]);
  1988. kmap->kmaps = &machine->kmaps;
  1989. map_groups__insert(&machine->kmaps,
  1990. machine->vmlinux_maps[type]);
  1991. }
  1992. return 0;
  1993. }
  1994. void machine__destroy_kernel_maps(struct machine *machine)
  1995. {
  1996. enum map_type type;
  1997. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1998. struct kmap *kmap;
  1999. if (machine->vmlinux_maps[type] == NULL)
  2000. continue;
  2001. kmap = map__kmap(machine->vmlinux_maps[type]);
  2002. map_groups__remove(&machine->kmaps,
  2003. machine->vmlinux_maps[type]);
  2004. if (kmap->ref_reloc_sym) {
  2005. /*
  2006. * ref_reloc_sym is shared among all maps, so free just
  2007. * on one of them.
  2008. */
  2009. if (type == MAP__FUNCTION) {
  2010. free((char *)kmap->ref_reloc_sym->name);
  2011. kmap->ref_reloc_sym->name = NULL;
  2012. free(kmap->ref_reloc_sym);
  2013. }
  2014. kmap->ref_reloc_sym = NULL;
  2015. }
  2016. map__delete(machine->vmlinux_maps[type]);
  2017. machine->vmlinux_maps[type] = NULL;
  2018. }
  2019. }
  2020. int machine__create_kernel_maps(struct machine *machine)
  2021. {
  2022. struct dso *kernel = machine__get_kernel(machine);
  2023. if (kernel == NULL ||
  2024. __machine__create_kernel_maps(machine, kernel) < 0)
  2025. return -1;
  2026. if (symbol_conf.use_modules && machine__create_modules(machine) < 0)
  2027. pr_debug("Problems creating module maps, continuing anyway...\n");
  2028. /*
  2029. * Now that we have all the maps created, just set the ->end of them:
  2030. */
  2031. map_groups__fixup_end(&machine->kmaps);
  2032. return 0;
  2033. }
  2034. static void vmlinux_path__exit(void)
  2035. {
  2036. while (--vmlinux_path__nr_entries >= 0) {
  2037. free(vmlinux_path[vmlinux_path__nr_entries]);
  2038. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  2039. }
  2040. free(vmlinux_path);
  2041. vmlinux_path = NULL;
  2042. }
  2043. static int vmlinux_path__init(void)
  2044. {
  2045. struct utsname uts;
  2046. char bf[PATH_MAX];
  2047. vmlinux_path = malloc(sizeof(char *) * 5);
  2048. if (vmlinux_path == NULL)
  2049. return -1;
  2050. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  2051. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2052. goto out_fail;
  2053. ++vmlinux_path__nr_entries;
  2054. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  2055. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2056. goto out_fail;
  2057. ++vmlinux_path__nr_entries;
  2058. /* only try running kernel version if no symfs was given */
  2059. if (symbol_conf.symfs[0] != 0)
  2060. return 0;
  2061. if (uname(&uts) < 0)
  2062. return -1;
  2063. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  2064. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2065. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2066. goto out_fail;
  2067. ++vmlinux_path__nr_entries;
  2068. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  2069. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2070. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2071. goto out_fail;
  2072. ++vmlinux_path__nr_entries;
  2073. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  2074. uts.release);
  2075. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2076. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2077. goto out_fail;
  2078. ++vmlinux_path__nr_entries;
  2079. return 0;
  2080. out_fail:
  2081. vmlinux_path__exit();
  2082. return -1;
  2083. }
  2084. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  2085. {
  2086. int i;
  2087. size_t printed = 0;
  2088. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  2089. if (kdso->has_build_id) {
  2090. char filename[PATH_MAX];
  2091. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  2092. printed += fprintf(fp, "[0] %s\n", filename);
  2093. }
  2094. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  2095. printed += fprintf(fp, "[%d] %s\n",
  2096. i + kdso->has_build_id, vmlinux_path[i]);
  2097. return printed;
  2098. }
  2099. static int setup_list(struct strlist **list, const char *list_str,
  2100. const char *list_name)
  2101. {
  2102. if (list_str == NULL)
  2103. return 0;
  2104. *list = strlist__new(true, list_str);
  2105. if (!*list) {
  2106. pr_err("problems parsing %s list\n", list_name);
  2107. return -1;
  2108. }
  2109. return 0;
  2110. }
  2111. static bool symbol__read_kptr_restrict(void)
  2112. {
  2113. bool value = false;
  2114. if (geteuid() != 0) {
  2115. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  2116. if (fp != NULL) {
  2117. char line[8];
  2118. if (fgets(line, sizeof(line), fp) != NULL)
  2119. value = atoi(line) != 0;
  2120. fclose(fp);
  2121. }
  2122. }
  2123. return value;
  2124. }
  2125. int symbol__init(void)
  2126. {
  2127. const char *symfs;
  2128. if (symbol_conf.initialized)
  2129. return 0;
  2130. symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
  2131. elf_version(EV_CURRENT);
  2132. if (symbol_conf.sort_by_name)
  2133. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  2134. sizeof(struct symbol));
  2135. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  2136. return -1;
  2137. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  2138. pr_err("'.' is the only non valid --field-separator argument\n");
  2139. return -1;
  2140. }
  2141. if (setup_list(&symbol_conf.dso_list,
  2142. symbol_conf.dso_list_str, "dso") < 0)
  2143. return -1;
  2144. if (setup_list(&symbol_conf.comm_list,
  2145. symbol_conf.comm_list_str, "comm") < 0)
  2146. goto out_free_dso_list;
  2147. if (setup_list(&symbol_conf.sym_list,
  2148. symbol_conf.sym_list_str, "symbol") < 0)
  2149. goto out_free_comm_list;
  2150. /*
  2151. * A path to symbols of "/" is identical to ""
  2152. * reset here for simplicity.
  2153. */
  2154. symfs = realpath(symbol_conf.symfs, NULL);
  2155. if (symfs == NULL)
  2156. symfs = symbol_conf.symfs;
  2157. if (strcmp(symfs, "/") == 0)
  2158. symbol_conf.symfs = "";
  2159. if (symfs != symbol_conf.symfs)
  2160. free((void *)symfs);
  2161. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  2162. symbol_conf.initialized = true;
  2163. return 0;
  2164. out_free_dso_list:
  2165. strlist__delete(symbol_conf.dso_list);
  2166. out_free_comm_list:
  2167. strlist__delete(symbol_conf.comm_list);
  2168. return -1;
  2169. }
  2170. void symbol__exit(void)
  2171. {
  2172. if (!symbol_conf.initialized)
  2173. return;
  2174. strlist__delete(symbol_conf.sym_list);
  2175. strlist__delete(symbol_conf.dso_list);
  2176. strlist__delete(symbol_conf.comm_list);
  2177. vmlinux_path__exit();
  2178. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  2179. symbol_conf.initialized = false;
  2180. }
  2181. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
  2182. {
  2183. struct machine *machine = machines__findnew(machines, pid);
  2184. if (machine == NULL)
  2185. return -1;
  2186. return machine__create_kernel_maps(machine);
  2187. }
  2188. static int hex(char ch)
  2189. {
  2190. if ((ch >= '0') && (ch <= '9'))
  2191. return ch - '0';
  2192. if ((ch >= 'a') && (ch <= 'f'))
  2193. return ch - 'a' + 10;
  2194. if ((ch >= 'A') && (ch <= 'F'))
  2195. return ch - 'A' + 10;
  2196. return -1;
  2197. }
  2198. /*
  2199. * While we find nice hex chars, build a long_val.
  2200. * Return number of chars processed.
  2201. */
  2202. int hex2u64(const char *ptr, u64 *long_val)
  2203. {
  2204. const char *p = ptr;
  2205. *long_val = 0;
  2206. while (*p) {
  2207. const int hex_val = hex(*p);
  2208. if (hex_val < 0)
  2209. break;
  2210. *long_val = (*long_val << 4) | hex_val;
  2211. p++;
  2212. }
  2213. return p - ptr;
  2214. }
  2215. char *strxfrchar(char *s, char from, char to)
  2216. {
  2217. char *p = s;
  2218. while ((p = strchr(p, from)) != NULL)
  2219. *p++ = to;
  2220. return s;
  2221. }
  2222. int machines__create_guest_kernel_maps(struct rb_root *machines)
  2223. {
  2224. int ret = 0;
  2225. struct dirent **namelist = NULL;
  2226. int i, items = 0;
  2227. char path[PATH_MAX];
  2228. pid_t pid;
  2229. if (symbol_conf.default_guest_vmlinux_name ||
  2230. symbol_conf.default_guest_modules ||
  2231. symbol_conf.default_guest_kallsyms) {
  2232. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  2233. }
  2234. if (symbol_conf.guestmount) {
  2235. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  2236. if (items <= 0)
  2237. return -ENOENT;
  2238. for (i = 0; i < items; i++) {
  2239. if (!isdigit(namelist[i]->d_name[0])) {
  2240. /* Filter out . and .. */
  2241. continue;
  2242. }
  2243. pid = atoi(namelist[i]->d_name);
  2244. sprintf(path, "%s/%s/proc/kallsyms",
  2245. symbol_conf.guestmount,
  2246. namelist[i]->d_name);
  2247. ret = access(path, R_OK);
  2248. if (ret) {
  2249. pr_debug("Can't access file %s\n", path);
  2250. goto failure;
  2251. }
  2252. machines__create_kernel_maps(machines, pid);
  2253. }
  2254. failure:
  2255. free(namelist);
  2256. }
  2257. return ret;
  2258. }
  2259. void machines__destroy_guest_kernel_maps(struct rb_root *machines)
  2260. {
  2261. struct rb_node *next = rb_first(machines);
  2262. while (next) {
  2263. struct machine *pos = rb_entry(next, struct machine, rb_node);
  2264. next = rb_next(&pos->rb_node);
  2265. rb_erase(&pos->rb_node, machines);
  2266. machine__delete(pos);
  2267. }
  2268. }
  2269. int machine__load_kallsyms(struct machine *machine, const char *filename,
  2270. enum map_type type, symbol_filter_t filter)
  2271. {
  2272. struct map *map = machine->vmlinux_maps[type];
  2273. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  2274. if (ret > 0) {
  2275. dso__set_loaded(map->dso, type);
  2276. /*
  2277. * Since /proc/kallsyms will have multiple sessions for the
  2278. * kernel, with modules between them, fixup the end of all
  2279. * sections.
  2280. */
  2281. __map_groups__fixup_end(&machine->kmaps, type);
  2282. }
  2283. return ret;
  2284. }
  2285. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  2286. symbol_filter_t filter)
  2287. {
  2288. struct map *map = machine->vmlinux_maps[type];
  2289. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2290. if (ret > 0) {
  2291. dso__set_loaded(map->dso, type);
  2292. map__reloc_vmlinux(map);
  2293. }
  2294. return ret;
  2295. }