symbol.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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 "symbol.h"
  15. #include "strlist.h"
  16. #include <libelf.h>
  17. #include <gelf.h>
  18. #include <elf.h>
  19. #include <limits.h>
  20. #include <sys/utsname.h>
  21. #ifndef NT_GNU_BUILD_ID
  22. #define NT_GNU_BUILD_ID 3
  23. #endif
  24. static void dsos__add(struct list_head *head, struct dso *dso);
  25. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  26. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  27. symbol_filter_t filter);
  28. static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
  29. symbol_filter_t filter);
  30. static int vmlinux_path__nr_entries;
  31. static char **vmlinux_path;
  32. struct symbol_conf symbol_conf = {
  33. .exclude_other = true,
  34. .use_modules = true,
  35. .try_vmlinux_path = true,
  36. };
  37. bool dso__loaded(const struct dso *self, enum map_type type)
  38. {
  39. return self->loaded & (1 << type);
  40. }
  41. bool dso__sorted_by_name(const struct dso *self, enum map_type type)
  42. {
  43. return self->sorted_by_name & (1 << type);
  44. }
  45. static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
  46. {
  47. self->sorted_by_name |= (1 << type);
  48. }
  49. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  50. {
  51. switch (map_type) {
  52. case MAP__FUNCTION:
  53. return symbol_type == 'T' || symbol_type == 'W';
  54. case MAP__VARIABLE:
  55. return symbol_type == 'D' || symbol_type == 'd';
  56. default:
  57. return false;
  58. }
  59. }
  60. static void symbols__fixup_end(struct rb_root *self)
  61. {
  62. struct rb_node *nd, *prevnd = rb_first(self);
  63. struct symbol *curr, *prev;
  64. if (prevnd == NULL)
  65. return;
  66. curr = rb_entry(prevnd, struct symbol, rb_node);
  67. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  68. prev = curr;
  69. curr = rb_entry(nd, struct symbol, rb_node);
  70. if (prev->end == prev->start)
  71. prev->end = curr->start - 1;
  72. }
  73. /* Last entry */
  74. if (curr->end == curr->start)
  75. curr->end = roundup(curr->start, 4096);
  76. }
  77. static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
  78. {
  79. struct map *prev, *curr;
  80. struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
  81. if (prevnd == NULL)
  82. return;
  83. curr = rb_entry(prevnd, struct map, rb_node);
  84. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  85. prev = curr;
  86. curr = rb_entry(nd, struct map, rb_node);
  87. prev->end = curr->start - 1;
  88. }
  89. /*
  90. * We still haven't the actual symbols, so guess the
  91. * last map final address.
  92. */
  93. curr->end = ~0UL;
  94. }
  95. static void map_groups__fixup_end(struct map_groups *self)
  96. {
  97. int i;
  98. for (i = 0; i < MAP__NR_TYPES; ++i)
  99. __map_groups__fixup_end(self, i);
  100. }
  101. static struct symbol *symbol__new(u64 start, u64 len, const char *name)
  102. {
  103. size_t namelen = strlen(name) + 1;
  104. struct symbol *self = calloc(1, (symbol_conf.priv_size +
  105. sizeof(*self) + namelen));
  106. if (self == NULL)
  107. return NULL;
  108. if (symbol_conf.priv_size)
  109. self = ((void *)self) + symbol_conf.priv_size;
  110. self->start = start;
  111. self->end = len ? start + len - 1 : start;
  112. pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
  113. memcpy(self->name, name, namelen);
  114. return self;
  115. }
  116. void symbol__delete(struct symbol *self)
  117. {
  118. free(((void *)self) - symbol_conf.priv_size);
  119. }
  120. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  121. {
  122. return fprintf(fp, " %llx-%llx %s\n",
  123. self->start, self->end, self->name);
  124. }
  125. void dso__set_long_name(struct dso *self, char *name)
  126. {
  127. if (name == NULL)
  128. return;
  129. self->long_name = name;
  130. self->long_name_len = strlen(name);
  131. }
  132. static void dso__set_short_name(struct dso *self, const char *name)
  133. {
  134. if (name == NULL)
  135. return;
  136. self->short_name = name;
  137. self->short_name_len = strlen(name);
  138. }
  139. static void dso__set_basename(struct dso *self)
  140. {
  141. dso__set_short_name(self, basename(self->long_name));
  142. }
  143. struct dso *dso__new(const char *name)
  144. {
  145. struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
  146. if (self != NULL) {
  147. int i;
  148. strcpy(self->name, name);
  149. dso__set_long_name(self, self->name);
  150. dso__set_short_name(self, self->name);
  151. for (i = 0; i < MAP__NR_TYPES; ++i)
  152. self->symbols[i] = self->symbol_names[i] = RB_ROOT;
  153. self->slen_calculated = 0;
  154. self->origin = DSO__ORIG_NOT_FOUND;
  155. self->loaded = 0;
  156. self->sorted_by_name = 0;
  157. self->has_build_id = 0;
  158. self->kernel = DSO_TYPE_USER;
  159. INIT_LIST_HEAD(&self->node);
  160. }
  161. return self;
  162. }
  163. static void symbols__delete(struct rb_root *self)
  164. {
  165. struct symbol *pos;
  166. struct rb_node *next = rb_first(self);
  167. while (next) {
  168. pos = rb_entry(next, struct symbol, rb_node);
  169. next = rb_next(&pos->rb_node);
  170. rb_erase(&pos->rb_node, self);
  171. symbol__delete(pos);
  172. }
  173. }
  174. void dso__delete(struct dso *self)
  175. {
  176. int i;
  177. for (i = 0; i < MAP__NR_TYPES; ++i)
  178. symbols__delete(&self->symbols[i]);
  179. if (self->long_name != self->name)
  180. free(self->long_name);
  181. free(self);
  182. }
  183. void dso__set_build_id(struct dso *self, void *build_id)
  184. {
  185. memcpy(self->build_id, build_id, sizeof(self->build_id));
  186. self->has_build_id = 1;
  187. }
  188. static void symbols__insert(struct rb_root *self, struct symbol *sym)
  189. {
  190. struct rb_node **p = &self->rb_node;
  191. struct rb_node *parent = NULL;
  192. const u64 ip = sym->start;
  193. struct symbol *s;
  194. while (*p != NULL) {
  195. parent = *p;
  196. s = rb_entry(parent, struct symbol, rb_node);
  197. if (ip < s->start)
  198. p = &(*p)->rb_left;
  199. else
  200. p = &(*p)->rb_right;
  201. }
  202. rb_link_node(&sym->rb_node, parent, p);
  203. rb_insert_color(&sym->rb_node, self);
  204. }
  205. static struct symbol *symbols__find(struct rb_root *self, u64 ip)
  206. {
  207. struct rb_node *n;
  208. if (self == NULL)
  209. return NULL;
  210. n = self->rb_node;
  211. while (n) {
  212. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  213. if (ip < s->start)
  214. n = n->rb_left;
  215. else if (ip > s->end)
  216. n = n->rb_right;
  217. else
  218. return s;
  219. }
  220. return NULL;
  221. }
  222. struct symbol_name_rb_node {
  223. struct rb_node rb_node;
  224. struct symbol sym;
  225. };
  226. static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
  227. {
  228. struct rb_node **p = &self->rb_node;
  229. struct rb_node *parent = NULL;
  230. struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
  231. while (*p != NULL) {
  232. parent = *p;
  233. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  234. if (strcmp(sym->name, s->sym.name) < 0)
  235. p = &(*p)->rb_left;
  236. else
  237. p = &(*p)->rb_right;
  238. }
  239. rb_link_node(&symn->rb_node, parent, p);
  240. rb_insert_color(&symn->rb_node, self);
  241. }
  242. static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
  243. {
  244. struct rb_node *nd;
  245. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  246. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  247. symbols__insert_by_name(self, pos);
  248. }
  249. }
  250. static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
  251. {
  252. struct rb_node *n;
  253. if (self == NULL)
  254. return NULL;
  255. n = self->rb_node;
  256. while (n) {
  257. struct symbol_name_rb_node *s;
  258. int cmp;
  259. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  260. cmp = strcmp(name, s->sym.name);
  261. if (cmp < 0)
  262. n = n->rb_left;
  263. else if (cmp > 0)
  264. n = n->rb_right;
  265. else
  266. return &s->sym;
  267. }
  268. return NULL;
  269. }
  270. struct symbol *dso__find_symbol(struct dso *self,
  271. enum map_type type, u64 addr)
  272. {
  273. return symbols__find(&self->symbols[type], addr);
  274. }
  275. struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
  276. const char *name)
  277. {
  278. return symbols__find_by_name(&self->symbol_names[type], name);
  279. }
  280. void dso__sort_by_name(struct dso *self, enum map_type type)
  281. {
  282. dso__set_sorted_by_name(self, type);
  283. return symbols__sort_by_name(&self->symbol_names[type],
  284. &self->symbols[type]);
  285. }
  286. int build_id__sprintf(const u8 *self, int len, char *bf)
  287. {
  288. char *bid = bf;
  289. const u8 *raw = self;
  290. int i;
  291. for (i = 0; i < len; ++i) {
  292. sprintf(bid, "%02x", *raw);
  293. ++raw;
  294. bid += 2;
  295. }
  296. return raw - self;
  297. }
  298. size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
  299. {
  300. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  301. build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
  302. return fprintf(fp, "%s", sbuild_id);
  303. }
  304. size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
  305. {
  306. struct rb_node *nd;
  307. size_t ret = fprintf(fp, "dso: %s (", self->short_name);
  308. if (self->short_name != self->long_name)
  309. ret += fprintf(fp, "%s, ", self->long_name);
  310. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  311. self->loaded ? "" : "NOT ");
  312. ret += dso__fprintf_buildid(self, fp);
  313. ret += fprintf(fp, ")\n");
  314. for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
  315. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  316. ret += symbol__fprintf(pos, fp);
  317. }
  318. return ret;
  319. }
  320. int kallsyms__parse(const char *filename, void *arg,
  321. int (*process_symbol)(void *arg, const char *name,
  322. char type, u64 start))
  323. {
  324. char *line = NULL;
  325. size_t n;
  326. int err = 0;
  327. FILE *file = fopen(filename, "r");
  328. if (file == NULL)
  329. goto out_failure;
  330. while (!feof(file)) {
  331. u64 start;
  332. int line_len, len;
  333. char symbol_type;
  334. char *symbol_name;
  335. line_len = getline(&line, &n, file);
  336. if (line_len < 0 || !line)
  337. break;
  338. line[--line_len] = '\0'; /* \n */
  339. len = hex2u64(line, &start);
  340. len++;
  341. if (len + 2 >= line_len)
  342. continue;
  343. symbol_type = toupper(line[len]);
  344. symbol_name = line + len + 2;
  345. err = process_symbol(arg, symbol_name, symbol_type, start);
  346. if (err)
  347. break;
  348. }
  349. free(line);
  350. fclose(file);
  351. return err;
  352. out_failure:
  353. return -1;
  354. }
  355. struct process_kallsyms_args {
  356. struct map *map;
  357. struct dso *dso;
  358. };
  359. static int map__process_kallsym_symbol(void *arg, const char *name,
  360. char type, u64 start)
  361. {
  362. struct symbol *sym;
  363. struct process_kallsyms_args *a = arg;
  364. struct rb_root *root = &a->dso->symbols[a->map->type];
  365. if (!symbol_type__is_a(type, a->map->type))
  366. return 0;
  367. /*
  368. * Will fix up the end later, when we have all symbols sorted.
  369. */
  370. sym = symbol__new(start, 0, name);
  371. if (sym == NULL)
  372. return -ENOMEM;
  373. /*
  374. * We will pass the symbols to the filter later, in
  375. * map__split_kallsyms, when we have split the maps per module
  376. */
  377. symbols__insert(root, sym);
  378. return 0;
  379. }
  380. /*
  381. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  382. * so that we can in the next step set the symbol ->end address and then
  383. * call kernel_maps__split_kallsyms.
  384. */
  385. static int dso__load_all_kallsyms(struct dso *self, const char *filename,
  386. struct map *map)
  387. {
  388. struct process_kallsyms_args args = { .map = map, .dso = self, };
  389. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  390. }
  391. /*
  392. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  393. * kernel range is broken in several maps, named [kernel].N, as we don't have
  394. * the original ELF section names vmlinux have.
  395. */
  396. static int dso__split_kallsyms(struct dso *self, struct map *map,
  397. symbol_filter_t filter)
  398. {
  399. struct map_groups *kmaps = map__kmap(map)->kmaps;
  400. struct machine *machine = kmaps->machine;
  401. struct map *curr_map = map;
  402. struct symbol *pos;
  403. int count = 0;
  404. struct rb_root *root = &self->symbols[map->type];
  405. struct rb_node *next = rb_first(root);
  406. int kernel_range = 0;
  407. while (next) {
  408. char *module;
  409. pos = rb_entry(next, struct symbol, rb_node);
  410. next = rb_next(&pos->rb_node);
  411. module = strchr(pos->name, '\t');
  412. if (module) {
  413. if (!symbol_conf.use_modules)
  414. goto discard_symbol;
  415. *module++ = '\0';
  416. if (strcmp(curr_map->dso->short_name, module)) {
  417. if (curr_map != map &&
  418. self->kernel == DSO_TYPE_GUEST_KERNEL &&
  419. machine__is_default_guest(machine)) {
  420. /*
  421. * We assume all symbols of a module are
  422. * continuous in * kallsyms, so curr_map
  423. * points to a module and all its
  424. * symbols are in its kmap. Mark it as
  425. * loaded.
  426. */
  427. dso__set_loaded(curr_map->dso,
  428. curr_map->type);
  429. }
  430. curr_map = map_groups__find_by_name(kmaps,
  431. map->type, module);
  432. if (curr_map == NULL) {
  433. pr_err("%s/proc/{kallsyms,modules} "
  434. "inconsistency while looking "
  435. "for \"%s\" module!\n",
  436. machine->root_dir, module);
  437. curr_map = map;
  438. goto discard_symbol;
  439. }
  440. if (curr_map->dso->loaded &&
  441. !machine__is_default_guest(machine))
  442. goto discard_symbol;
  443. }
  444. /*
  445. * So that we look just like we get from .ko files,
  446. * i.e. not prelinked, relative to map->start.
  447. */
  448. pos->start = curr_map->map_ip(curr_map, pos->start);
  449. pos->end = curr_map->map_ip(curr_map, pos->end);
  450. } else if (curr_map != map) {
  451. char dso_name[PATH_MAX];
  452. struct dso *dso;
  453. if (self->kernel == DSO_TYPE_GUEST_KERNEL)
  454. snprintf(dso_name, sizeof(dso_name),
  455. "[guest.kernel].%d",
  456. kernel_range++);
  457. else
  458. snprintf(dso_name, sizeof(dso_name),
  459. "[kernel].%d",
  460. kernel_range++);
  461. dso = dso__new(dso_name);
  462. if (dso == NULL)
  463. return -1;
  464. dso->kernel = self->kernel;
  465. curr_map = map__new2(pos->start, dso, map->type);
  466. if (curr_map == NULL) {
  467. dso__delete(dso);
  468. return -1;
  469. }
  470. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  471. map_groups__insert(kmaps, curr_map);
  472. ++kernel_range;
  473. }
  474. if (filter && filter(curr_map, pos)) {
  475. discard_symbol: rb_erase(&pos->rb_node, root);
  476. symbol__delete(pos);
  477. } else {
  478. if (curr_map != map) {
  479. rb_erase(&pos->rb_node, root);
  480. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  481. }
  482. count++;
  483. }
  484. }
  485. if (curr_map != map &&
  486. self->kernel == DSO_TYPE_GUEST_KERNEL &&
  487. machine__is_default_guest(kmaps->machine)) {
  488. dso__set_loaded(curr_map->dso, curr_map->type);
  489. }
  490. return count;
  491. }
  492. int dso__load_kallsyms(struct dso *self, const char *filename,
  493. struct map *map, symbol_filter_t filter)
  494. {
  495. if (dso__load_all_kallsyms(self, filename, map) < 0)
  496. return -1;
  497. symbols__fixup_end(&self->symbols[map->type]);
  498. if (self->kernel == DSO_TYPE_GUEST_KERNEL)
  499. self->origin = DSO__ORIG_GUEST_KERNEL;
  500. else
  501. self->origin = DSO__ORIG_KERNEL;
  502. return dso__split_kallsyms(self, map, filter);
  503. }
  504. static int dso__load_perf_map(struct dso *self, struct map *map,
  505. symbol_filter_t filter)
  506. {
  507. char *line = NULL;
  508. size_t n;
  509. FILE *file;
  510. int nr_syms = 0;
  511. file = fopen(self->long_name, "r");
  512. if (file == NULL)
  513. goto out_failure;
  514. while (!feof(file)) {
  515. u64 start, size;
  516. struct symbol *sym;
  517. int line_len, len;
  518. line_len = getline(&line, &n, file);
  519. if (line_len < 0)
  520. break;
  521. if (!line)
  522. goto out_failure;
  523. line[--line_len] = '\0'; /* \n */
  524. len = hex2u64(line, &start);
  525. len++;
  526. if (len + 2 >= line_len)
  527. continue;
  528. len += hex2u64(line + len, &size);
  529. len++;
  530. if (len + 2 >= line_len)
  531. continue;
  532. sym = symbol__new(start, size, line + len);
  533. if (sym == NULL)
  534. goto out_delete_line;
  535. if (filter && filter(map, sym))
  536. symbol__delete(sym);
  537. else {
  538. symbols__insert(&self->symbols[map->type], sym);
  539. nr_syms++;
  540. }
  541. }
  542. free(line);
  543. fclose(file);
  544. return nr_syms;
  545. out_delete_line:
  546. free(line);
  547. out_failure:
  548. return -1;
  549. }
  550. /**
  551. * elf_symtab__for_each_symbol - iterate thru all the symbols
  552. *
  553. * @self: struct elf_symtab instance to iterate
  554. * @idx: uint32_t idx
  555. * @sym: GElf_Sym iterator
  556. */
  557. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  558. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  559. idx < nr_syms; \
  560. idx++, gelf_getsym(syms, idx, &sym))
  561. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  562. {
  563. return GELF_ST_TYPE(sym->st_info);
  564. }
  565. static inline int elf_sym__is_function(const GElf_Sym *sym)
  566. {
  567. return elf_sym__type(sym) == STT_FUNC &&
  568. sym->st_name != 0 &&
  569. sym->st_shndx != SHN_UNDEF;
  570. }
  571. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  572. {
  573. return elf_sym__type(sym) == STT_OBJECT &&
  574. sym->st_name != 0 &&
  575. sym->st_shndx != SHN_UNDEF;
  576. }
  577. static inline int elf_sym__is_label(const GElf_Sym *sym)
  578. {
  579. return elf_sym__type(sym) == STT_NOTYPE &&
  580. sym->st_name != 0 &&
  581. sym->st_shndx != SHN_UNDEF &&
  582. sym->st_shndx != SHN_ABS;
  583. }
  584. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  585. const Elf_Data *secstrs)
  586. {
  587. return secstrs->d_buf + shdr->sh_name;
  588. }
  589. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  590. const Elf_Data *secstrs)
  591. {
  592. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  593. }
  594. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  595. const Elf_Data *secstrs)
  596. {
  597. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  598. }
  599. static inline const char *elf_sym__name(const GElf_Sym *sym,
  600. const Elf_Data *symstrs)
  601. {
  602. return symstrs->d_buf + sym->st_name;
  603. }
  604. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  605. GElf_Shdr *shp, const char *name,
  606. size_t *idx)
  607. {
  608. Elf_Scn *sec = NULL;
  609. size_t cnt = 1;
  610. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  611. char *str;
  612. gelf_getshdr(sec, shp);
  613. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  614. if (!strcmp(name, str)) {
  615. if (idx)
  616. *idx = cnt;
  617. break;
  618. }
  619. ++cnt;
  620. }
  621. return sec;
  622. }
  623. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  624. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  625. idx < nr_entries; \
  626. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  627. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  628. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  629. idx < nr_entries; \
  630. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  631. /*
  632. * We need to check if we have a .dynsym, so that we can handle the
  633. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  634. * .dynsym or .symtab).
  635. * And always look at the original dso, not at debuginfo packages, that
  636. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  637. */
  638. static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
  639. symbol_filter_t filter)
  640. {
  641. uint32_t nr_rel_entries, idx;
  642. GElf_Sym sym;
  643. u64 plt_offset;
  644. GElf_Shdr shdr_plt;
  645. struct symbol *f;
  646. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  647. Elf_Data *reldata, *syms, *symstrs;
  648. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  649. size_t dynsym_idx;
  650. GElf_Ehdr ehdr;
  651. char sympltname[1024];
  652. Elf *elf;
  653. int nr = 0, symidx, fd, err = 0;
  654. fd = open(self->long_name, O_RDONLY);
  655. if (fd < 0)
  656. goto out;
  657. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  658. if (elf == NULL)
  659. goto out_close;
  660. if (gelf_getehdr(elf, &ehdr) == NULL)
  661. goto out_elf_end;
  662. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  663. ".dynsym", &dynsym_idx);
  664. if (scn_dynsym == NULL)
  665. goto out_elf_end;
  666. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  667. ".rela.plt", NULL);
  668. if (scn_plt_rel == NULL) {
  669. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  670. ".rel.plt", NULL);
  671. if (scn_plt_rel == NULL)
  672. goto out_elf_end;
  673. }
  674. err = -1;
  675. if (shdr_rel_plt.sh_link != dynsym_idx)
  676. goto out_elf_end;
  677. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  678. goto out_elf_end;
  679. /*
  680. * Fetch the relocation section to find the idxes to the GOT
  681. * and the symbols in the .dynsym they refer to.
  682. */
  683. reldata = elf_getdata(scn_plt_rel, NULL);
  684. if (reldata == NULL)
  685. goto out_elf_end;
  686. syms = elf_getdata(scn_dynsym, NULL);
  687. if (syms == NULL)
  688. goto out_elf_end;
  689. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  690. if (scn_symstrs == NULL)
  691. goto out_elf_end;
  692. symstrs = elf_getdata(scn_symstrs, NULL);
  693. if (symstrs == NULL)
  694. goto out_elf_end;
  695. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  696. plt_offset = shdr_plt.sh_offset;
  697. if (shdr_rel_plt.sh_type == SHT_RELA) {
  698. GElf_Rela pos_mem, *pos;
  699. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  700. nr_rel_entries) {
  701. symidx = GELF_R_SYM(pos->r_info);
  702. plt_offset += shdr_plt.sh_entsize;
  703. gelf_getsym(syms, symidx, &sym);
  704. snprintf(sympltname, sizeof(sympltname),
  705. "%s@plt", elf_sym__name(&sym, symstrs));
  706. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  707. sympltname);
  708. if (!f)
  709. goto out_elf_end;
  710. if (filter && filter(map, f))
  711. symbol__delete(f);
  712. else {
  713. symbols__insert(&self->symbols[map->type], f);
  714. ++nr;
  715. }
  716. }
  717. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  718. GElf_Rel pos_mem, *pos;
  719. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  720. nr_rel_entries) {
  721. symidx = GELF_R_SYM(pos->r_info);
  722. plt_offset += shdr_plt.sh_entsize;
  723. gelf_getsym(syms, symidx, &sym);
  724. snprintf(sympltname, sizeof(sympltname),
  725. "%s@plt", elf_sym__name(&sym, symstrs));
  726. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  727. sympltname);
  728. if (!f)
  729. goto out_elf_end;
  730. if (filter && filter(map, f))
  731. symbol__delete(f);
  732. else {
  733. symbols__insert(&self->symbols[map->type], f);
  734. ++nr;
  735. }
  736. }
  737. }
  738. err = 0;
  739. out_elf_end:
  740. elf_end(elf);
  741. out_close:
  742. close(fd);
  743. if (err == 0)
  744. return nr;
  745. out:
  746. pr_debug("%s: problems reading %s PLT info.\n",
  747. __func__, self->long_name);
  748. return 0;
  749. }
  750. static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
  751. {
  752. switch (type) {
  753. case MAP__FUNCTION:
  754. return elf_sym__is_function(self);
  755. case MAP__VARIABLE:
  756. return elf_sym__is_object(self);
  757. default:
  758. return false;
  759. }
  760. }
  761. static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
  762. {
  763. switch (type) {
  764. case MAP__FUNCTION:
  765. return elf_sec__is_text(self, secstrs);
  766. case MAP__VARIABLE:
  767. return elf_sec__is_data(self, secstrs);
  768. default:
  769. return false;
  770. }
  771. }
  772. static int dso__load_sym(struct dso *self, struct map *map, const char *name,
  773. int fd, symbol_filter_t filter, int kmodule)
  774. {
  775. struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
  776. struct map *curr_map = map;
  777. struct dso *curr_dso = self;
  778. Elf_Data *symstrs, *secstrs;
  779. uint32_t nr_syms;
  780. int err = -1;
  781. uint32_t idx;
  782. GElf_Ehdr ehdr;
  783. GElf_Shdr shdr;
  784. Elf_Data *syms;
  785. GElf_Sym sym;
  786. Elf_Scn *sec, *sec_strndx;
  787. Elf *elf;
  788. int nr = 0;
  789. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  790. if (elf == NULL) {
  791. pr_err("%s: cannot read %s ELF file.\n", __func__, name);
  792. goto out_close;
  793. }
  794. if (gelf_getehdr(elf, &ehdr) == NULL) {
  795. pr_err("%s: cannot get elf header.\n", __func__);
  796. goto out_elf_end;
  797. }
  798. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  799. if (sec == NULL) {
  800. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  801. if (sec == NULL)
  802. goto out_elf_end;
  803. }
  804. syms = elf_getdata(sec, NULL);
  805. if (syms == NULL)
  806. goto out_elf_end;
  807. sec = elf_getscn(elf, shdr.sh_link);
  808. if (sec == NULL)
  809. goto out_elf_end;
  810. symstrs = elf_getdata(sec, NULL);
  811. if (symstrs == NULL)
  812. goto out_elf_end;
  813. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  814. if (sec_strndx == NULL)
  815. goto out_elf_end;
  816. secstrs = elf_getdata(sec_strndx, NULL);
  817. if (secstrs == NULL)
  818. goto out_elf_end;
  819. nr_syms = shdr.sh_size / shdr.sh_entsize;
  820. memset(&sym, 0, sizeof(sym));
  821. if (self->kernel == DSO_TYPE_USER) {
  822. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  823. elf_section_by_name(elf, &ehdr, &shdr,
  824. ".gnu.prelink_undo",
  825. NULL) != NULL);
  826. } else self->adjust_symbols = 0;
  827. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  828. struct symbol *f;
  829. const char *elf_name = elf_sym__name(&sym, symstrs);
  830. char *demangled = NULL;
  831. int is_label = elf_sym__is_label(&sym);
  832. const char *section_name;
  833. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  834. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  835. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  836. if (!is_label && !elf_sym__is_a(&sym, map->type))
  837. continue;
  838. sec = elf_getscn(elf, sym.st_shndx);
  839. if (!sec)
  840. goto out_elf_end;
  841. gelf_getshdr(sec, &shdr);
  842. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  843. continue;
  844. section_name = elf_sec__name(&shdr, secstrs);
  845. if (self->kernel != DSO_TYPE_USER || kmodule) {
  846. char dso_name[PATH_MAX];
  847. if (strcmp(section_name,
  848. (curr_dso->short_name +
  849. self->short_name_len)) == 0)
  850. goto new_symbol;
  851. if (strcmp(section_name, ".text") == 0) {
  852. curr_map = map;
  853. curr_dso = self;
  854. goto new_symbol;
  855. }
  856. snprintf(dso_name, sizeof(dso_name),
  857. "%s%s", self->short_name, section_name);
  858. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  859. if (curr_map == NULL) {
  860. u64 start = sym.st_value;
  861. if (kmodule)
  862. start += map->start + shdr.sh_offset;
  863. curr_dso = dso__new(dso_name);
  864. if (curr_dso == NULL)
  865. goto out_elf_end;
  866. curr_dso->kernel = self->kernel;
  867. curr_map = map__new2(start, curr_dso,
  868. map->type);
  869. if (curr_map == NULL) {
  870. dso__delete(curr_dso);
  871. goto out_elf_end;
  872. }
  873. curr_map->map_ip = identity__map_ip;
  874. curr_map->unmap_ip = identity__map_ip;
  875. curr_dso->origin = self->origin;
  876. map_groups__insert(kmap->kmaps, curr_map);
  877. dsos__add(&self->node, curr_dso);
  878. dso__set_loaded(curr_dso, map->type);
  879. } else
  880. curr_dso = curr_map->dso;
  881. goto new_symbol;
  882. }
  883. if (curr_dso->adjust_symbols) {
  884. pr_debug4("%s: adjusting symbol: st_value: %#Lx "
  885. "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
  886. (u64)sym.st_value, (u64)shdr.sh_addr,
  887. (u64)shdr.sh_offset);
  888. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  889. }
  890. /*
  891. * We need to figure out if the object was created from C++ sources
  892. * DWARF DW_compile_unit has this, but we don't always have access
  893. * to it...
  894. */
  895. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  896. if (demangled != NULL)
  897. elf_name = demangled;
  898. new_symbol:
  899. f = symbol__new(sym.st_value, sym.st_size, elf_name);
  900. free(demangled);
  901. if (!f)
  902. goto out_elf_end;
  903. if (filter && filter(curr_map, f))
  904. symbol__delete(f);
  905. else {
  906. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  907. nr++;
  908. }
  909. }
  910. /*
  911. * For misannotated, zeroed, ASM function sizes.
  912. */
  913. if (nr > 0) {
  914. symbols__fixup_end(&self->symbols[map->type]);
  915. if (kmap) {
  916. /*
  917. * We need to fixup this here too because we create new
  918. * maps here, for things like vsyscall sections.
  919. */
  920. __map_groups__fixup_end(kmap->kmaps, map->type);
  921. }
  922. }
  923. err = nr;
  924. out_elf_end:
  925. elf_end(elf);
  926. out_close:
  927. return err;
  928. }
  929. static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
  930. {
  931. return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
  932. }
  933. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  934. {
  935. bool have_build_id = false;
  936. struct dso *pos;
  937. list_for_each_entry(pos, head, node) {
  938. if (with_hits && !pos->hit)
  939. continue;
  940. if (filename__read_build_id(pos->long_name, pos->build_id,
  941. sizeof(pos->build_id)) > 0) {
  942. have_build_id = true;
  943. pos->has_build_id = true;
  944. }
  945. }
  946. return have_build_id;
  947. }
  948. /*
  949. * Align offset to 4 bytes as needed for note name and descriptor data.
  950. */
  951. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  952. int filename__read_build_id(const char *filename, void *bf, size_t size)
  953. {
  954. int fd, err = -1;
  955. GElf_Ehdr ehdr;
  956. GElf_Shdr shdr;
  957. Elf_Data *data;
  958. Elf_Scn *sec;
  959. Elf_Kind ek;
  960. void *ptr;
  961. Elf *elf;
  962. if (size < BUILD_ID_SIZE)
  963. goto out;
  964. fd = open(filename, O_RDONLY);
  965. if (fd < 0)
  966. goto out;
  967. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  968. if (elf == NULL) {
  969. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  970. goto out_close;
  971. }
  972. ek = elf_kind(elf);
  973. if (ek != ELF_K_ELF)
  974. goto out_elf_end;
  975. if (gelf_getehdr(elf, &ehdr) == NULL) {
  976. pr_err("%s: cannot get elf header.\n", __func__);
  977. goto out_elf_end;
  978. }
  979. sec = elf_section_by_name(elf, &ehdr, &shdr,
  980. ".note.gnu.build-id", NULL);
  981. if (sec == NULL) {
  982. sec = elf_section_by_name(elf, &ehdr, &shdr,
  983. ".notes", NULL);
  984. if (sec == NULL)
  985. goto out_elf_end;
  986. }
  987. data = elf_getdata(sec, NULL);
  988. if (data == NULL)
  989. goto out_elf_end;
  990. ptr = data->d_buf;
  991. while (ptr < (data->d_buf + data->d_size)) {
  992. GElf_Nhdr *nhdr = ptr;
  993. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  994. descsz = NOTE_ALIGN(nhdr->n_descsz);
  995. const char *name;
  996. ptr += sizeof(*nhdr);
  997. name = ptr;
  998. ptr += namesz;
  999. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1000. nhdr->n_namesz == sizeof("GNU")) {
  1001. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1002. memcpy(bf, ptr, BUILD_ID_SIZE);
  1003. err = BUILD_ID_SIZE;
  1004. break;
  1005. }
  1006. }
  1007. ptr += descsz;
  1008. }
  1009. out_elf_end:
  1010. elf_end(elf);
  1011. out_close:
  1012. close(fd);
  1013. out:
  1014. return err;
  1015. }
  1016. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1017. {
  1018. int fd, err = -1;
  1019. if (size < BUILD_ID_SIZE)
  1020. goto out;
  1021. fd = open(filename, O_RDONLY);
  1022. if (fd < 0)
  1023. goto out;
  1024. while (1) {
  1025. char bf[BUFSIZ];
  1026. GElf_Nhdr nhdr;
  1027. int namesz, descsz;
  1028. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1029. break;
  1030. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1031. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1032. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1033. nhdr.n_namesz == sizeof("GNU")) {
  1034. if (read(fd, bf, namesz) != namesz)
  1035. break;
  1036. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1037. if (read(fd, build_id,
  1038. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1039. err = 0;
  1040. break;
  1041. }
  1042. } else if (read(fd, bf, descsz) != descsz)
  1043. break;
  1044. } else {
  1045. int n = namesz + descsz;
  1046. if (read(fd, bf, n) != n)
  1047. break;
  1048. }
  1049. }
  1050. close(fd);
  1051. out:
  1052. return err;
  1053. }
  1054. char dso__symtab_origin(const struct dso *self)
  1055. {
  1056. static const char origin[] = {
  1057. [DSO__ORIG_KERNEL] = 'k',
  1058. [DSO__ORIG_JAVA_JIT] = 'j',
  1059. [DSO__ORIG_BUILD_ID_CACHE] = 'B',
  1060. [DSO__ORIG_FEDORA] = 'f',
  1061. [DSO__ORIG_UBUNTU] = 'u',
  1062. [DSO__ORIG_BUILDID] = 'b',
  1063. [DSO__ORIG_DSO] = 'd',
  1064. [DSO__ORIG_KMODULE] = 'K',
  1065. [DSO__ORIG_GUEST_KERNEL] = 'g',
  1066. [DSO__ORIG_GUEST_KMODULE] = 'G',
  1067. };
  1068. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  1069. return '!';
  1070. return origin[self->origin];
  1071. }
  1072. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  1073. {
  1074. int size = PATH_MAX;
  1075. char *name;
  1076. u8 build_id[BUILD_ID_SIZE];
  1077. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1078. int ret = -1;
  1079. int fd;
  1080. struct machine *machine;
  1081. const char *root_dir;
  1082. dso__set_loaded(self, map->type);
  1083. if (self->kernel == DSO_TYPE_KERNEL)
  1084. return dso__load_kernel_sym(self, map, filter);
  1085. else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
  1086. return dso__load_guest_kernel_sym(self, map, filter);
  1087. if (map->groups && map->groups->machine)
  1088. machine = map->groups->machine;
  1089. else
  1090. machine = NULL;
  1091. name = malloc(size);
  1092. if (!name)
  1093. return -1;
  1094. self->adjust_symbols = 0;
  1095. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  1096. ret = dso__load_perf_map(self, map, filter);
  1097. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  1098. DSO__ORIG_NOT_FOUND;
  1099. return ret;
  1100. }
  1101. self->origin = DSO__ORIG_BUILD_ID_CACHE;
  1102. if (self->has_build_id) {
  1103. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1104. build_id_hex);
  1105. snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
  1106. getenv("HOME"), DEBUG_CACHE_DIR,
  1107. build_id_hex, build_id_hex + 2);
  1108. goto open_file;
  1109. }
  1110. more:
  1111. do {
  1112. self->origin++;
  1113. switch (self->origin) {
  1114. case DSO__ORIG_FEDORA:
  1115. snprintf(name, size, "/usr/lib/debug%s.debug",
  1116. self->long_name);
  1117. break;
  1118. case DSO__ORIG_UBUNTU:
  1119. snprintf(name, size, "/usr/lib/debug%s",
  1120. self->long_name);
  1121. break;
  1122. case DSO__ORIG_BUILDID:
  1123. if (filename__read_build_id(self->long_name, build_id,
  1124. sizeof(build_id))) {
  1125. build_id__sprintf(build_id, sizeof(build_id),
  1126. build_id_hex);
  1127. snprintf(name, size,
  1128. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  1129. build_id_hex, build_id_hex + 2);
  1130. if (self->has_build_id)
  1131. goto compare_build_id;
  1132. break;
  1133. }
  1134. self->origin++;
  1135. /* Fall thru */
  1136. case DSO__ORIG_DSO:
  1137. snprintf(name, size, "%s", self->long_name);
  1138. break;
  1139. case DSO__ORIG_GUEST_KMODULE:
  1140. if (map->groups && map->groups->machine)
  1141. root_dir = map->groups->machine->root_dir;
  1142. else
  1143. root_dir = "";
  1144. snprintf(name, size, "%s%s", root_dir, self->long_name);
  1145. break;
  1146. default:
  1147. goto out;
  1148. }
  1149. if (self->has_build_id) {
  1150. if (filename__read_build_id(name, build_id,
  1151. sizeof(build_id)) < 0)
  1152. goto more;
  1153. compare_build_id:
  1154. if (!dso__build_id_equal(self, build_id))
  1155. goto more;
  1156. }
  1157. open_file:
  1158. fd = open(name, O_RDONLY);
  1159. } while (fd < 0);
  1160. ret = dso__load_sym(self, map, name, fd, filter, 0);
  1161. close(fd);
  1162. /*
  1163. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  1164. */
  1165. if (!ret)
  1166. goto more;
  1167. if (ret > 0) {
  1168. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  1169. if (nr_plt > 0)
  1170. ret += nr_plt;
  1171. }
  1172. out:
  1173. free(name);
  1174. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  1175. return 0;
  1176. return ret;
  1177. }
  1178. struct map *map_groups__find_by_name(struct map_groups *self,
  1179. enum map_type type, const char *name)
  1180. {
  1181. struct rb_node *nd;
  1182. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  1183. struct map *map = rb_entry(nd, struct map, rb_node);
  1184. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1185. return map;
  1186. }
  1187. return NULL;
  1188. }
  1189. static int dso__kernel_module_get_build_id(struct dso *self,
  1190. const char *root_dir)
  1191. {
  1192. char filename[PATH_MAX];
  1193. /*
  1194. * kernel module short names are of the form "[module]" and
  1195. * we need just "module" here.
  1196. */
  1197. const char *name = self->short_name + 1;
  1198. snprintf(filename, sizeof(filename),
  1199. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1200. root_dir, (int)strlen(name) - 1, name);
  1201. if (sysfs__read_build_id(filename, self->build_id,
  1202. sizeof(self->build_id)) == 0)
  1203. self->has_build_id = true;
  1204. return 0;
  1205. }
  1206. static int map_groups__set_modules_path_dir(struct map_groups *self,
  1207. const char *dir_name)
  1208. {
  1209. struct dirent *dent;
  1210. DIR *dir = opendir(dir_name);
  1211. if (!dir) {
  1212. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1213. return -1;
  1214. }
  1215. while ((dent = readdir(dir)) != NULL) {
  1216. char path[PATH_MAX];
  1217. struct stat st;
  1218. /*sshfs might return bad dent->d_type, so we have to stat*/
  1219. sprintf(path, "%s/%s", dir_name, dent->d_name);
  1220. if (stat(path, &st))
  1221. continue;
  1222. if (S_ISDIR(st.st_mode)) {
  1223. if (!strcmp(dent->d_name, ".") ||
  1224. !strcmp(dent->d_name, ".."))
  1225. continue;
  1226. snprintf(path, sizeof(path), "%s/%s",
  1227. dir_name, dent->d_name);
  1228. if (map_groups__set_modules_path_dir(self, path) < 0)
  1229. goto failure;
  1230. } else {
  1231. char *dot = strrchr(dent->d_name, '.'),
  1232. dso_name[PATH_MAX];
  1233. struct map *map;
  1234. char *long_name;
  1235. if (dot == NULL || strcmp(dot, ".ko"))
  1236. continue;
  1237. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1238. (int)(dot - dent->d_name), dent->d_name);
  1239. strxfrchar(dso_name, '-', '_');
  1240. map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
  1241. if (map == NULL)
  1242. continue;
  1243. snprintf(path, sizeof(path), "%s/%s",
  1244. dir_name, dent->d_name);
  1245. long_name = strdup(path);
  1246. if (long_name == NULL)
  1247. goto failure;
  1248. dso__set_long_name(map->dso, long_name);
  1249. dso__kernel_module_get_build_id(map->dso, "");
  1250. }
  1251. }
  1252. return 0;
  1253. failure:
  1254. closedir(dir);
  1255. return -1;
  1256. }
  1257. static char *get_kernel_version(const char *root_dir)
  1258. {
  1259. char version[PATH_MAX];
  1260. FILE *file;
  1261. char *name, *tmp;
  1262. const char *prefix = "Linux version ";
  1263. sprintf(version, "%s/proc/version", root_dir);
  1264. file = fopen(version, "r");
  1265. if (!file)
  1266. return NULL;
  1267. version[0] = '\0';
  1268. tmp = fgets(version, sizeof(version), file);
  1269. fclose(file);
  1270. name = strstr(version, prefix);
  1271. if (!name)
  1272. return NULL;
  1273. name += strlen(prefix);
  1274. tmp = strchr(name, ' ');
  1275. if (tmp)
  1276. *tmp = '\0';
  1277. return strdup(name);
  1278. }
  1279. static int machine__set_modules_path(struct machine *self)
  1280. {
  1281. char *version;
  1282. char modules_path[PATH_MAX];
  1283. version = get_kernel_version(self->root_dir);
  1284. if (!version)
  1285. return -1;
  1286. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1287. self->root_dir, version);
  1288. free(version);
  1289. return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
  1290. }
  1291. /*
  1292. * Constructor variant for modules (where we know from /proc/modules where
  1293. * they are loaded) and for vmlinux, where only after we load all the
  1294. * symbols we'll know where it starts and ends.
  1295. */
  1296. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1297. {
  1298. struct map *self = calloc(1, (sizeof(*self) +
  1299. (dso->kernel ? sizeof(struct kmap) : 0)));
  1300. if (self != NULL) {
  1301. /*
  1302. * ->end will be filled after we load all the symbols
  1303. */
  1304. map__init(self, type, start, 0, 0, dso);
  1305. }
  1306. return self;
  1307. }
  1308. struct map *machine__new_module(struct machine *self, u64 start,
  1309. const char *filename)
  1310. {
  1311. struct map *map;
  1312. struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
  1313. if (dso == NULL)
  1314. return NULL;
  1315. map = map__new2(start, dso, MAP__FUNCTION);
  1316. if (map == NULL)
  1317. return NULL;
  1318. if (machine__is_host(self))
  1319. dso->origin = DSO__ORIG_KMODULE;
  1320. else
  1321. dso->origin = DSO__ORIG_GUEST_KMODULE;
  1322. map_groups__insert(&self->kmaps, map);
  1323. return map;
  1324. }
  1325. static int machine__create_modules(struct machine *self)
  1326. {
  1327. char *line = NULL;
  1328. size_t n;
  1329. FILE *file;
  1330. struct map *map;
  1331. const char *modules;
  1332. char path[PATH_MAX];
  1333. if (machine__is_default_guest(self))
  1334. modules = symbol_conf.default_guest_modules;
  1335. else {
  1336. sprintf(path, "%s/proc/modules", self->root_dir);
  1337. modules = path;
  1338. }
  1339. file = fopen(modules, "r");
  1340. if (file == NULL)
  1341. return -1;
  1342. while (!feof(file)) {
  1343. char name[PATH_MAX];
  1344. u64 start;
  1345. char *sep;
  1346. int line_len;
  1347. line_len = getline(&line, &n, file);
  1348. if (line_len < 0)
  1349. break;
  1350. if (!line)
  1351. goto out_failure;
  1352. line[--line_len] = '\0'; /* \n */
  1353. sep = strrchr(line, 'x');
  1354. if (sep == NULL)
  1355. continue;
  1356. hex2u64(sep + 1, &start);
  1357. sep = strchr(line, ' ');
  1358. if (sep == NULL)
  1359. continue;
  1360. *sep = '\0';
  1361. snprintf(name, sizeof(name), "[%s]", line);
  1362. map = machine__new_module(self, start, name);
  1363. if (map == NULL)
  1364. goto out_delete_line;
  1365. dso__kernel_module_get_build_id(map->dso, self->root_dir);
  1366. }
  1367. free(line);
  1368. fclose(file);
  1369. return machine__set_modules_path(self);
  1370. out_delete_line:
  1371. free(line);
  1372. out_failure:
  1373. return -1;
  1374. }
  1375. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1376. const char *vmlinux, symbol_filter_t filter)
  1377. {
  1378. int err = -1, fd;
  1379. if (self->has_build_id) {
  1380. u8 build_id[BUILD_ID_SIZE];
  1381. if (filename__read_build_id(vmlinux, build_id,
  1382. sizeof(build_id)) < 0) {
  1383. pr_debug("No build_id in %s, ignoring it\n", vmlinux);
  1384. return -1;
  1385. }
  1386. if (!dso__build_id_equal(self, build_id)) {
  1387. char expected_build_id[BUILD_ID_SIZE * 2 + 1],
  1388. vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
  1389. build_id__sprintf(self->build_id,
  1390. sizeof(self->build_id),
  1391. expected_build_id);
  1392. build_id__sprintf(build_id, sizeof(build_id),
  1393. vmlinux_build_id);
  1394. pr_debug("build_id in %s is %s while expected is %s, "
  1395. "ignoring it\n", vmlinux, vmlinux_build_id,
  1396. expected_build_id);
  1397. return -1;
  1398. }
  1399. }
  1400. fd = open(vmlinux, O_RDONLY);
  1401. if (fd < 0)
  1402. return -1;
  1403. dso__set_loaded(self, map->type);
  1404. err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
  1405. close(fd);
  1406. if (err > 0)
  1407. pr_debug("Using %s for symbols\n", vmlinux);
  1408. return err;
  1409. }
  1410. int dso__load_vmlinux_path(struct dso *self, struct map *map,
  1411. symbol_filter_t filter)
  1412. {
  1413. int i, err = 0;
  1414. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1415. vmlinux_path__nr_entries);
  1416. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1417. err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
  1418. if (err > 0) {
  1419. dso__set_long_name(self, strdup(vmlinux_path[i]));
  1420. break;
  1421. }
  1422. }
  1423. return err;
  1424. }
  1425. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1426. symbol_filter_t filter)
  1427. {
  1428. int err;
  1429. const char *kallsyms_filename = NULL;
  1430. char *kallsyms_allocated_filename = NULL;
  1431. /*
  1432. * Step 1: if the user specified a vmlinux filename, use it and only
  1433. * it, reporting errors to the user if it cannot be used.
  1434. *
  1435. * For instance, try to analyse an ARM perf.data file _without_ a
  1436. * build-id, or if the user specifies the wrong path to the right
  1437. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1438. * x86_86 one, on the machine where analysis is being performed, say),
  1439. * or worse, /proc/kallsyms.
  1440. *
  1441. * If the specified file _has_ a build-id and there is a build-id
  1442. * section in the perf.data file, we will still do the expected
  1443. * validation in dso__load_vmlinux and will bail out if they don't
  1444. * match.
  1445. */
  1446. if (symbol_conf.vmlinux_name != NULL) {
  1447. err = dso__load_vmlinux(self, map,
  1448. symbol_conf.vmlinux_name, filter);
  1449. goto out_try_fixup;
  1450. }
  1451. if (vmlinux_path != NULL) {
  1452. err = dso__load_vmlinux_path(self, map, filter);
  1453. if (err > 0)
  1454. goto out_fixup;
  1455. }
  1456. /*
  1457. * Say the kernel DSO was created when processing the build-id header table,
  1458. * we have a build-id, so check if it is the same as the running kernel,
  1459. * using it if it is.
  1460. */
  1461. if (self->has_build_id) {
  1462. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1463. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1464. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1465. sizeof(kallsyms_build_id)) == 0) {
  1466. if (dso__build_id_equal(self, kallsyms_build_id)) {
  1467. kallsyms_filename = "/proc/kallsyms";
  1468. goto do_kallsyms;
  1469. }
  1470. }
  1471. /*
  1472. * Now look if we have it on the build-id cache in
  1473. * $HOME/.debug/[kernel.kallsyms].
  1474. */
  1475. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1476. sbuild_id);
  1477. if (asprintf(&kallsyms_allocated_filename,
  1478. "%s/.debug/[kernel.kallsyms]/%s",
  1479. getenv("HOME"), sbuild_id) == -1) {
  1480. pr_err("Not enough memory for kallsyms file lookup\n");
  1481. return -1;
  1482. }
  1483. kallsyms_filename = kallsyms_allocated_filename;
  1484. if (access(kallsyms_filename, F_OK)) {
  1485. pr_err("No kallsyms or vmlinux with build-id %s "
  1486. "was found\n", sbuild_id);
  1487. free(kallsyms_allocated_filename);
  1488. return -1;
  1489. }
  1490. } else {
  1491. /*
  1492. * Last resort, if we don't have a build-id and couldn't find
  1493. * any vmlinux file, try the running kernel kallsyms table.
  1494. */
  1495. kallsyms_filename = "/proc/kallsyms";
  1496. }
  1497. do_kallsyms:
  1498. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1499. if (err > 0)
  1500. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1501. free(kallsyms_allocated_filename);
  1502. out_try_fixup:
  1503. if (err > 0) {
  1504. out_fixup:
  1505. if (kallsyms_filename != NULL)
  1506. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1507. map__fixup_start(map);
  1508. map__fixup_end(map);
  1509. }
  1510. return err;
  1511. }
  1512. static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
  1513. symbol_filter_t filter)
  1514. {
  1515. int err;
  1516. const char *kallsyms_filename = NULL;
  1517. struct machine *machine;
  1518. char path[PATH_MAX];
  1519. if (!map->groups) {
  1520. pr_debug("Guest kernel map hasn't the point to groups\n");
  1521. return -1;
  1522. }
  1523. machine = map->groups->machine;
  1524. if (machine__is_default_guest(machine)) {
  1525. /*
  1526. * if the user specified a vmlinux filename, use it and only
  1527. * it, reporting errors to the user if it cannot be used.
  1528. * Or use file guest_kallsyms inputted by user on commandline
  1529. */
  1530. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1531. err = dso__load_vmlinux(self, map,
  1532. symbol_conf.default_guest_vmlinux_name, filter);
  1533. goto out_try_fixup;
  1534. }
  1535. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1536. if (!kallsyms_filename)
  1537. return -1;
  1538. } else {
  1539. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1540. kallsyms_filename = path;
  1541. }
  1542. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1543. if (err > 0)
  1544. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1545. out_try_fixup:
  1546. if (err > 0) {
  1547. if (kallsyms_filename != NULL) {
  1548. machine__mmap_name(machine, path, sizeof(path));
  1549. dso__set_long_name(self, strdup(path));
  1550. }
  1551. map__fixup_start(map);
  1552. map__fixup_end(map);
  1553. }
  1554. return err;
  1555. }
  1556. static void dsos__add(struct list_head *head, struct dso *dso)
  1557. {
  1558. list_add_tail(&dso->node, head);
  1559. }
  1560. static struct dso *dsos__find(struct list_head *head, const char *name)
  1561. {
  1562. struct dso *pos;
  1563. list_for_each_entry(pos, head, node)
  1564. if (strcmp(pos->long_name, name) == 0)
  1565. return pos;
  1566. return NULL;
  1567. }
  1568. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1569. {
  1570. struct dso *dso = dsos__find(head, name);
  1571. if (!dso) {
  1572. dso = dso__new(name);
  1573. if (dso != NULL) {
  1574. dsos__add(head, dso);
  1575. dso__set_basename(dso);
  1576. }
  1577. }
  1578. return dso;
  1579. }
  1580. static size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1581. {
  1582. struct dso *pos;
  1583. size_t ret = 0;
  1584. list_for_each_entry(pos, head, node) {
  1585. int i;
  1586. for (i = 0; i < MAP__NR_TYPES; ++i)
  1587. ret += dso__fprintf(pos, i, fp);
  1588. }
  1589. return ret;
  1590. }
  1591. size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
  1592. {
  1593. struct rb_node *nd;
  1594. size_t ret = 0;
  1595. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1596. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1597. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1598. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1599. }
  1600. return ret;
  1601. }
  1602. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1603. bool with_hits)
  1604. {
  1605. struct dso *pos;
  1606. size_t ret = 0;
  1607. list_for_each_entry(pos, head, node) {
  1608. if (with_hits && !pos->hit)
  1609. continue;
  1610. ret += dso__fprintf_buildid(pos, fp);
  1611. ret += fprintf(fp, " %s\n", pos->long_name);
  1612. }
  1613. return ret;
  1614. }
  1615. size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
  1616. {
  1617. struct rb_node *nd;
  1618. size_t ret = 0;
  1619. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1620. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1621. ret += __dsos__fprintf_buildid(&pos->kernel_dsos, fp, with_hits);
  1622. ret += __dsos__fprintf_buildid(&pos->user_dsos, fp, with_hits);
  1623. }
  1624. return ret;
  1625. }
  1626. struct dso *dso__new_kernel(const char *name)
  1627. {
  1628. struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
  1629. if (self != NULL) {
  1630. dso__set_short_name(self, "[kernel]");
  1631. self->kernel = DSO_TYPE_KERNEL;
  1632. }
  1633. return self;
  1634. }
  1635. static struct dso *dso__new_guest_kernel(struct machine *machine,
  1636. const char *name)
  1637. {
  1638. char bf[PATH_MAX];
  1639. struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
  1640. if (self != NULL) {
  1641. dso__set_short_name(self, "[guest.kernel]");
  1642. self->kernel = DSO_TYPE_GUEST_KERNEL;
  1643. }
  1644. return self;
  1645. }
  1646. void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
  1647. {
  1648. char path[PATH_MAX];
  1649. if (machine__is_default_guest(machine))
  1650. return;
  1651. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1652. if (sysfs__read_build_id(path, self->build_id,
  1653. sizeof(self->build_id)) == 0)
  1654. self->has_build_id = true;
  1655. }
  1656. static struct dso *machine__create_kernel(struct machine *self)
  1657. {
  1658. const char *vmlinux_name = NULL;
  1659. struct dso *kernel;
  1660. if (machine__is_host(self)) {
  1661. vmlinux_name = symbol_conf.vmlinux_name;
  1662. kernel = dso__new_kernel(vmlinux_name);
  1663. } else {
  1664. if (machine__is_default_guest(self))
  1665. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1666. kernel = dso__new_guest_kernel(self, vmlinux_name);
  1667. }
  1668. if (kernel != NULL) {
  1669. dso__read_running_kernel_build_id(kernel, self);
  1670. dsos__add(&self->kernel_dsos, kernel);
  1671. }
  1672. return kernel;
  1673. }
  1674. int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
  1675. {
  1676. enum map_type type;
  1677. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1678. struct kmap *kmap;
  1679. self->vmlinux_maps[type] = map__new2(0, kernel, type);
  1680. if (self->vmlinux_maps[type] == NULL)
  1681. return -1;
  1682. self->vmlinux_maps[type]->map_ip =
  1683. self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
  1684. kmap = map__kmap(self->vmlinux_maps[type]);
  1685. kmap->kmaps = &self->kmaps;
  1686. map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
  1687. }
  1688. return 0;
  1689. }
  1690. int machine__create_kernel_maps(struct machine *self)
  1691. {
  1692. struct dso *kernel = machine__create_kernel(self);
  1693. if (kernel == NULL ||
  1694. __machine__create_kernel_maps(self, kernel) < 0)
  1695. return -1;
  1696. if (symbol_conf.use_modules && machine__create_modules(self) < 0)
  1697. pr_debug("Problems creating module maps, continuing anyway...\n");
  1698. /*
  1699. * Now that we have all the maps created, just set the ->end of them:
  1700. */
  1701. map_groups__fixup_end(&self->kmaps);
  1702. return 0;
  1703. }
  1704. static void vmlinux_path__exit(void)
  1705. {
  1706. while (--vmlinux_path__nr_entries >= 0) {
  1707. free(vmlinux_path[vmlinux_path__nr_entries]);
  1708. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1709. }
  1710. free(vmlinux_path);
  1711. vmlinux_path = NULL;
  1712. }
  1713. static int vmlinux_path__init(void)
  1714. {
  1715. struct utsname uts;
  1716. char bf[PATH_MAX];
  1717. if (uname(&uts) < 0)
  1718. return -1;
  1719. vmlinux_path = malloc(sizeof(char *) * 5);
  1720. if (vmlinux_path == NULL)
  1721. return -1;
  1722. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1723. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1724. goto out_fail;
  1725. ++vmlinux_path__nr_entries;
  1726. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1727. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1728. goto out_fail;
  1729. ++vmlinux_path__nr_entries;
  1730. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1731. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1732. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1733. goto out_fail;
  1734. ++vmlinux_path__nr_entries;
  1735. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1736. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1737. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1738. goto out_fail;
  1739. ++vmlinux_path__nr_entries;
  1740. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1741. uts.release);
  1742. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1743. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1744. goto out_fail;
  1745. ++vmlinux_path__nr_entries;
  1746. return 0;
  1747. out_fail:
  1748. vmlinux_path__exit();
  1749. return -1;
  1750. }
  1751. size_t vmlinux_path__fprintf(FILE *fp)
  1752. {
  1753. int i;
  1754. size_t printed = 0;
  1755. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  1756. printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
  1757. return printed;
  1758. }
  1759. static int setup_list(struct strlist **list, const char *list_str,
  1760. const char *list_name)
  1761. {
  1762. if (list_str == NULL)
  1763. return 0;
  1764. *list = strlist__new(true, list_str);
  1765. if (!*list) {
  1766. pr_err("problems parsing %s list\n", list_name);
  1767. return -1;
  1768. }
  1769. return 0;
  1770. }
  1771. int symbol__init(void)
  1772. {
  1773. elf_version(EV_CURRENT);
  1774. if (symbol_conf.sort_by_name)
  1775. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1776. sizeof(struct symbol));
  1777. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1778. return -1;
  1779. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1780. pr_err("'.' is the only non valid --field-separator argument\n");
  1781. return -1;
  1782. }
  1783. if (setup_list(&symbol_conf.dso_list,
  1784. symbol_conf.dso_list_str, "dso") < 0)
  1785. return -1;
  1786. if (setup_list(&symbol_conf.comm_list,
  1787. symbol_conf.comm_list_str, "comm") < 0)
  1788. goto out_free_dso_list;
  1789. if (setup_list(&symbol_conf.sym_list,
  1790. symbol_conf.sym_list_str, "symbol") < 0)
  1791. goto out_free_comm_list;
  1792. return 0;
  1793. out_free_dso_list:
  1794. strlist__delete(symbol_conf.dso_list);
  1795. out_free_comm_list:
  1796. strlist__delete(symbol_conf.comm_list);
  1797. return -1;
  1798. }
  1799. int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
  1800. {
  1801. struct machine *machine = machines__findnew(self, pid);
  1802. if (machine == NULL)
  1803. return -1;
  1804. return machine__create_kernel_maps(machine);
  1805. }
  1806. static int hex(char ch)
  1807. {
  1808. if ((ch >= '0') && (ch <= '9'))
  1809. return ch - '0';
  1810. if ((ch >= 'a') && (ch <= 'f'))
  1811. return ch - 'a' + 10;
  1812. if ((ch >= 'A') && (ch <= 'F'))
  1813. return ch - 'A' + 10;
  1814. return -1;
  1815. }
  1816. /*
  1817. * While we find nice hex chars, build a long_val.
  1818. * Return number of chars processed.
  1819. */
  1820. int hex2u64(const char *ptr, u64 *long_val)
  1821. {
  1822. const char *p = ptr;
  1823. *long_val = 0;
  1824. while (*p) {
  1825. const int hex_val = hex(*p);
  1826. if (hex_val < 0)
  1827. break;
  1828. *long_val = (*long_val << 4) | hex_val;
  1829. p++;
  1830. }
  1831. return p - ptr;
  1832. }
  1833. char *strxfrchar(char *s, char from, char to)
  1834. {
  1835. char *p = s;
  1836. while ((p = strchr(p, from)) != NULL)
  1837. *p++ = to;
  1838. return s;
  1839. }
  1840. int machines__create_guest_kernel_maps(struct rb_root *self)
  1841. {
  1842. int ret = 0;
  1843. struct dirent **namelist = NULL;
  1844. int i, items = 0;
  1845. char path[PATH_MAX];
  1846. pid_t pid;
  1847. if (symbol_conf.default_guest_vmlinux_name ||
  1848. symbol_conf.default_guest_modules ||
  1849. symbol_conf.default_guest_kallsyms) {
  1850. machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
  1851. }
  1852. if (symbol_conf.guestmount) {
  1853. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  1854. if (items <= 0)
  1855. return -ENOENT;
  1856. for (i = 0; i < items; i++) {
  1857. if (!isdigit(namelist[i]->d_name[0])) {
  1858. /* Filter out . and .. */
  1859. continue;
  1860. }
  1861. pid = atoi(namelist[i]->d_name);
  1862. sprintf(path, "%s/%s/proc/kallsyms",
  1863. symbol_conf.guestmount,
  1864. namelist[i]->d_name);
  1865. ret = access(path, R_OK);
  1866. if (ret) {
  1867. pr_debug("Can't access file %s\n", path);
  1868. goto failure;
  1869. }
  1870. machines__create_kernel_maps(self, pid);
  1871. }
  1872. failure:
  1873. free(namelist);
  1874. }
  1875. return ret;
  1876. }
  1877. int machine__load_kallsyms(struct machine *self, const char *filename,
  1878. enum map_type type, symbol_filter_t filter)
  1879. {
  1880. struct map *map = self->vmlinux_maps[type];
  1881. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  1882. if (ret > 0) {
  1883. dso__set_loaded(map->dso, type);
  1884. /*
  1885. * Since /proc/kallsyms will have multiple sessions for the
  1886. * kernel, with modules between them, fixup the end of all
  1887. * sections.
  1888. */
  1889. __map_groups__fixup_end(&self->kmaps, type);
  1890. }
  1891. return ret;
  1892. }
  1893. int machine__load_vmlinux_path(struct machine *self, enum map_type type,
  1894. symbol_filter_t filter)
  1895. {
  1896. struct map *map = self->vmlinux_maps[type];
  1897. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  1898. if (ret > 0) {
  1899. dso__set_loaded(map->dso, type);
  1900. map__reloc_vmlinux(map);
  1901. }
  1902. return ret;
  1903. }