symbol.c 65 KB

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