symbol.c 66 KB

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