symbol.c 63 KB

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