symbol.c 64 KB

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