symbol.c 62 KB

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