symbol.c 53 KB

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