symbol.c 53 KB

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