symbol.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  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. int want_symtab)
  802. {
  803. struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
  804. struct map *curr_map = map;
  805. struct dso *curr_dso = self;
  806. Elf_Data *symstrs, *secstrs;
  807. uint32_t nr_syms;
  808. int err = -1;
  809. uint32_t idx;
  810. GElf_Ehdr ehdr;
  811. GElf_Shdr shdr, opdshdr;
  812. Elf_Data *syms, *opddata = NULL;
  813. GElf_Sym sym;
  814. Elf_Scn *sec, *sec_strndx, *opdsec;
  815. Elf *elf;
  816. int nr = 0;
  817. size_t opdidx = 0;
  818. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  819. if (elf == NULL) {
  820. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  821. goto out_close;
  822. }
  823. if (gelf_getehdr(elf, &ehdr) == NULL) {
  824. pr_debug("%s: cannot get elf header.\n", __func__);
  825. goto out_elf_end;
  826. }
  827. /* Always reject images with a mismatched build-id: */
  828. if (self->has_build_id) {
  829. u8 build_id[BUILD_ID_SIZE];
  830. if (elf_read_build_id(elf, build_id,
  831. BUILD_ID_SIZE) != BUILD_ID_SIZE)
  832. goto out_elf_end;
  833. if (!dso__build_id_equal(self, build_id))
  834. goto out_elf_end;
  835. }
  836. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  837. if (sec == NULL) {
  838. if (want_symtab)
  839. goto out_elf_end;
  840. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  841. if (sec == NULL)
  842. goto out_elf_end;
  843. }
  844. opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
  845. if (opdsec)
  846. opddata = elf_rawdata(opdsec, NULL);
  847. syms = elf_getdata(sec, NULL);
  848. if (syms == NULL)
  849. goto out_elf_end;
  850. sec = elf_getscn(elf, shdr.sh_link);
  851. if (sec == NULL)
  852. goto out_elf_end;
  853. symstrs = elf_getdata(sec, NULL);
  854. if (symstrs == NULL)
  855. goto out_elf_end;
  856. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  857. if (sec_strndx == NULL)
  858. goto out_elf_end;
  859. secstrs = elf_getdata(sec_strndx, NULL);
  860. if (secstrs == NULL)
  861. goto out_elf_end;
  862. nr_syms = shdr.sh_size / shdr.sh_entsize;
  863. memset(&sym, 0, sizeof(sym));
  864. if (self->kernel == DSO_TYPE_USER) {
  865. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  866. elf_section_by_name(elf, &ehdr, &shdr,
  867. ".gnu.prelink_undo",
  868. NULL) != NULL);
  869. } else self->adjust_symbols = 0;
  870. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  871. struct symbol *f;
  872. const char *elf_name = elf_sym__name(&sym, symstrs);
  873. char *demangled = NULL;
  874. int is_label = elf_sym__is_label(&sym);
  875. const char *section_name;
  876. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  877. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  878. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  879. if (!is_label && !elf_sym__is_a(&sym, map->type))
  880. continue;
  881. if (opdsec && sym.st_shndx == opdidx) {
  882. u32 offset = sym.st_value - opdshdr.sh_addr;
  883. u64 *opd = opddata->d_buf + offset;
  884. sym.st_value = *opd;
  885. sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
  886. }
  887. sec = elf_getscn(elf, sym.st_shndx);
  888. if (!sec)
  889. goto out_elf_end;
  890. gelf_getshdr(sec, &shdr);
  891. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  892. continue;
  893. section_name = elf_sec__name(&shdr, secstrs);
  894. if (self->kernel != DSO_TYPE_USER || kmodule) {
  895. char dso_name[PATH_MAX];
  896. if (strcmp(section_name,
  897. (curr_dso->short_name +
  898. self->short_name_len)) == 0)
  899. goto new_symbol;
  900. if (strcmp(section_name, ".text") == 0) {
  901. curr_map = map;
  902. curr_dso = self;
  903. goto new_symbol;
  904. }
  905. snprintf(dso_name, sizeof(dso_name),
  906. "%s%s", self->short_name, section_name);
  907. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  908. if (curr_map == NULL) {
  909. u64 start = sym.st_value;
  910. if (kmodule)
  911. start += map->start + shdr.sh_offset;
  912. curr_dso = dso__new(dso_name);
  913. if (curr_dso == NULL)
  914. goto out_elf_end;
  915. curr_dso->kernel = self->kernel;
  916. curr_map = map__new2(start, curr_dso,
  917. map->type);
  918. if (curr_map == NULL) {
  919. dso__delete(curr_dso);
  920. goto out_elf_end;
  921. }
  922. curr_map->map_ip = identity__map_ip;
  923. curr_map->unmap_ip = identity__map_ip;
  924. curr_dso->origin = self->origin;
  925. map_groups__insert(kmap->kmaps, curr_map);
  926. dsos__add(&self->node, curr_dso);
  927. dso__set_loaded(curr_dso, map->type);
  928. } else
  929. curr_dso = curr_map->dso;
  930. goto new_symbol;
  931. }
  932. if (curr_dso->adjust_symbols) {
  933. pr_debug4("%s: adjusting symbol: st_value: %#Lx "
  934. "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
  935. (u64)sym.st_value, (u64)shdr.sh_addr,
  936. (u64)shdr.sh_offset);
  937. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  938. }
  939. /*
  940. * We need to figure out if the object was created from C++ sources
  941. * DWARF DW_compile_unit has this, but we don't always have access
  942. * to it...
  943. */
  944. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  945. if (demangled != NULL)
  946. elf_name = demangled;
  947. new_symbol:
  948. f = symbol__new(sym.st_value, sym.st_size, elf_name);
  949. free(demangled);
  950. if (!f)
  951. goto out_elf_end;
  952. if (filter && filter(curr_map, f))
  953. symbol__delete(f);
  954. else {
  955. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  956. nr++;
  957. }
  958. }
  959. /*
  960. * For misannotated, zeroed, ASM function sizes.
  961. */
  962. if (nr > 0) {
  963. symbols__fixup_end(&self->symbols[map->type]);
  964. if (kmap) {
  965. /*
  966. * We need to fixup this here too because we create new
  967. * maps here, for things like vsyscall sections.
  968. */
  969. __map_groups__fixup_end(kmap->kmaps, map->type);
  970. }
  971. }
  972. err = nr;
  973. out_elf_end:
  974. elf_end(elf);
  975. out_close:
  976. return err;
  977. }
  978. static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
  979. {
  980. return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
  981. }
  982. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  983. {
  984. bool have_build_id = false;
  985. struct dso *pos;
  986. list_for_each_entry(pos, head, node) {
  987. if (with_hits && !pos->hit)
  988. continue;
  989. if (pos->has_build_id) {
  990. have_build_id = true;
  991. continue;
  992. }
  993. if (filename__read_build_id(pos->long_name, pos->build_id,
  994. sizeof(pos->build_id)) > 0) {
  995. have_build_id = true;
  996. pos->has_build_id = true;
  997. }
  998. }
  999. return have_build_id;
  1000. }
  1001. /*
  1002. * Align offset to 4 bytes as needed for note name and descriptor data.
  1003. */
  1004. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  1005. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  1006. {
  1007. int err = -1;
  1008. GElf_Ehdr ehdr;
  1009. GElf_Shdr shdr;
  1010. Elf_Data *data;
  1011. Elf_Scn *sec;
  1012. Elf_Kind ek;
  1013. void *ptr;
  1014. if (size < BUILD_ID_SIZE)
  1015. goto out;
  1016. ek = elf_kind(elf);
  1017. if (ek != ELF_K_ELF)
  1018. goto out;
  1019. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1020. pr_err("%s: cannot get elf header.\n", __func__);
  1021. goto out;
  1022. }
  1023. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1024. ".note.gnu.build-id", NULL);
  1025. if (sec == NULL) {
  1026. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1027. ".notes", NULL);
  1028. if (sec == NULL)
  1029. goto out;
  1030. }
  1031. data = elf_getdata(sec, NULL);
  1032. if (data == NULL)
  1033. goto out;
  1034. ptr = data->d_buf;
  1035. while (ptr < (data->d_buf + data->d_size)) {
  1036. GElf_Nhdr *nhdr = ptr;
  1037. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  1038. descsz = NOTE_ALIGN(nhdr->n_descsz);
  1039. const char *name;
  1040. ptr += sizeof(*nhdr);
  1041. name = ptr;
  1042. ptr += namesz;
  1043. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1044. nhdr->n_namesz == sizeof("GNU")) {
  1045. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1046. memcpy(bf, ptr, BUILD_ID_SIZE);
  1047. err = BUILD_ID_SIZE;
  1048. break;
  1049. }
  1050. }
  1051. ptr += descsz;
  1052. }
  1053. out:
  1054. return err;
  1055. }
  1056. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1057. {
  1058. int fd, err = -1;
  1059. Elf *elf;
  1060. if (size < BUILD_ID_SIZE)
  1061. goto out;
  1062. fd = open(filename, O_RDONLY);
  1063. if (fd < 0)
  1064. goto out;
  1065. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1066. if (elf == NULL) {
  1067. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1068. goto out_close;
  1069. }
  1070. err = elf_read_build_id(elf, bf, size);
  1071. elf_end(elf);
  1072. out_close:
  1073. close(fd);
  1074. out:
  1075. return err;
  1076. }
  1077. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1078. {
  1079. int fd, err = -1;
  1080. if (size < BUILD_ID_SIZE)
  1081. goto out;
  1082. fd = open(filename, O_RDONLY);
  1083. if (fd < 0)
  1084. goto out;
  1085. while (1) {
  1086. char bf[BUFSIZ];
  1087. GElf_Nhdr nhdr;
  1088. int namesz, descsz;
  1089. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1090. break;
  1091. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1092. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1093. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1094. nhdr.n_namesz == sizeof("GNU")) {
  1095. if (read(fd, bf, namesz) != namesz)
  1096. break;
  1097. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1098. if (read(fd, build_id,
  1099. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1100. err = 0;
  1101. break;
  1102. }
  1103. } else if (read(fd, bf, descsz) != descsz)
  1104. break;
  1105. } else {
  1106. int n = namesz + descsz;
  1107. if (read(fd, bf, n) != n)
  1108. break;
  1109. }
  1110. }
  1111. close(fd);
  1112. out:
  1113. return err;
  1114. }
  1115. char dso__symtab_origin(const struct dso *self)
  1116. {
  1117. static const char origin[] = {
  1118. [DSO__ORIG_KERNEL] = 'k',
  1119. [DSO__ORIG_JAVA_JIT] = 'j',
  1120. [DSO__ORIG_BUILD_ID_CACHE] = 'B',
  1121. [DSO__ORIG_FEDORA] = 'f',
  1122. [DSO__ORIG_UBUNTU] = 'u',
  1123. [DSO__ORIG_BUILDID] = 'b',
  1124. [DSO__ORIG_DSO] = 'd',
  1125. [DSO__ORIG_KMODULE] = 'K',
  1126. [DSO__ORIG_GUEST_KERNEL] = 'g',
  1127. [DSO__ORIG_GUEST_KMODULE] = 'G',
  1128. };
  1129. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  1130. return '!';
  1131. return origin[self->origin];
  1132. }
  1133. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  1134. {
  1135. int size = PATH_MAX;
  1136. char *name;
  1137. int ret = -1;
  1138. int fd;
  1139. struct machine *machine;
  1140. const char *root_dir;
  1141. int want_symtab;
  1142. dso__set_loaded(self, map->type);
  1143. if (self->kernel == DSO_TYPE_KERNEL)
  1144. return dso__load_kernel_sym(self, map, filter);
  1145. else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
  1146. return dso__load_guest_kernel_sym(self, map, filter);
  1147. if (map->groups && map->groups->machine)
  1148. machine = map->groups->machine;
  1149. else
  1150. machine = NULL;
  1151. name = malloc(size);
  1152. if (!name)
  1153. return -1;
  1154. self->adjust_symbols = 0;
  1155. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  1156. ret = dso__load_perf_map(self, map, filter);
  1157. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  1158. DSO__ORIG_NOT_FOUND;
  1159. return ret;
  1160. }
  1161. /* Iterate over candidate debug images.
  1162. * On the first pass, only load images if they have a full symtab.
  1163. * Failing that, do a second pass where we accept .dynsym also
  1164. */
  1165. for (self->origin = DSO__ORIG_BUILD_ID_CACHE, want_symtab = 1;
  1166. self->origin != DSO__ORIG_NOT_FOUND;
  1167. self->origin++) {
  1168. switch (self->origin) {
  1169. case DSO__ORIG_BUILD_ID_CACHE:
  1170. if (dso__build_id_filename(self, name, size) == NULL)
  1171. continue;
  1172. break;
  1173. case DSO__ORIG_FEDORA:
  1174. snprintf(name, size, "/usr/lib/debug%s.debug",
  1175. self->long_name);
  1176. break;
  1177. case DSO__ORIG_UBUNTU:
  1178. snprintf(name, size, "/usr/lib/debug%s",
  1179. self->long_name);
  1180. break;
  1181. case DSO__ORIG_BUILDID: {
  1182. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1183. if (!self->has_build_id)
  1184. continue;
  1185. build_id__sprintf(self->build_id,
  1186. sizeof(self->build_id),
  1187. build_id_hex);
  1188. snprintf(name, size,
  1189. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  1190. build_id_hex, build_id_hex + 2);
  1191. }
  1192. break;
  1193. case DSO__ORIG_DSO:
  1194. snprintf(name, size, "%s", self->long_name);
  1195. break;
  1196. case DSO__ORIG_GUEST_KMODULE:
  1197. if (map->groups && map->groups->machine)
  1198. root_dir = map->groups->machine->root_dir;
  1199. else
  1200. root_dir = "";
  1201. snprintf(name, size, "%s%s", root_dir, self->long_name);
  1202. break;
  1203. default:
  1204. /*
  1205. * If we wanted a full symtab but no image had one,
  1206. * relax our requirements and repeat the search.
  1207. */
  1208. if (want_symtab) {
  1209. want_symtab = 0;
  1210. self->origin = DSO__ORIG_BUILD_ID_CACHE;
  1211. } else
  1212. continue;
  1213. }
  1214. /* Name is now the name of the next image to try */
  1215. fd = open(name, O_RDONLY);
  1216. if (fd < 0)
  1217. continue;
  1218. ret = dso__load_sym(self, map, name, fd, filter, 0,
  1219. want_symtab);
  1220. close(fd);
  1221. /*
  1222. * Some people seem to have debuginfo files _WITHOUT_ debug
  1223. * info!?!?
  1224. */
  1225. if (!ret)
  1226. continue;
  1227. if (ret > 0) {
  1228. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  1229. if (nr_plt > 0)
  1230. ret += nr_plt;
  1231. break;
  1232. }
  1233. }
  1234. free(name);
  1235. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  1236. return 0;
  1237. return ret;
  1238. }
  1239. struct map *map_groups__find_by_name(struct map_groups *self,
  1240. enum map_type type, const char *name)
  1241. {
  1242. struct rb_node *nd;
  1243. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  1244. struct map *map = rb_entry(nd, struct map, rb_node);
  1245. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1246. return map;
  1247. }
  1248. return NULL;
  1249. }
  1250. static int dso__kernel_module_get_build_id(struct dso *self,
  1251. const char *root_dir)
  1252. {
  1253. char filename[PATH_MAX];
  1254. /*
  1255. * kernel module short names are of the form "[module]" and
  1256. * we need just "module" here.
  1257. */
  1258. const char *name = self->short_name + 1;
  1259. snprintf(filename, sizeof(filename),
  1260. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1261. root_dir, (int)strlen(name) - 1, name);
  1262. if (sysfs__read_build_id(filename, self->build_id,
  1263. sizeof(self->build_id)) == 0)
  1264. self->has_build_id = true;
  1265. return 0;
  1266. }
  1267. static int map_groups__set_modules_path_dir(struct map_groups *self,
  1268. const char *dir_name)
  1269. {
  1270. struct dirent *dent;
  1271. DIR *dir = opendir(dir_name);
  1272. int ret = 0;
  1273. if (!dir) {
  1274. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1275. return -1;
  1276. }
  1277. while ((dent = readdir(dir)) != NULL) {
  1278. char path[PATH_MAX];
  1279. struct stat st;
  1280. /*sshfs might return bad dent->d_type, so we have to stat*/
  1281. sprintf(path, "%s/%s", dir_name, dent->d_name);
  1282. if (stat(path, &st))
  1283. continue;
  1284. if (S_ISDIR(st.st_mode)) {
  1285. if (!strcmp(dent->d_name, ".") ||
  1286. !strcmp(dent->d_name, ".."))
  1287. continue;
  1288. snprintf(path, sizeof(path), "%s/%s",
  1289. dir_name, dent->d_name);
  1290. ret = map_groups__set_modules_path_dir(self, path);
  1291. if (ret < 0)
  1292. goto out;
  1293. } else {
  1294. char *dot = strrchr(dent->d_name, '.'),
  1295. dso_name[PATH_MAX];
  1296. struct map *map;
  1297. char *long_name;
  1298. if (dot == NULL || strcmp(dot, ".ko"))
  1299. continue;
  1300. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1301. (int)(dot - dent->d_name), dent->d_name);
  1302. strxfrchar(dso_name, '-', '_');
  1303. map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
  1304. if (map == NULL)
  1305. continue;
  1306. snprintf(path, sizeof(path), "%s/%s",
  1307. dir_name, dent->d_name);
  1308. long_name = strdup(path);
  1309. if (long_name == NULL) {
  1310. ret = -1;
  1311. goto out;
  1312. }
  1313. dso__set_long_name(map->dso, long_name);
  1314. map->dso->lname_alloc = 1;
  1315. dso__kernel_module_get_build_id(map->dso, "");
  1316. }
  1317. }
  1318. out:
  1319. closedir(dir);
  1320. return ret;
  1321. }
  1322. static char *get_kernel_version(const char *root_dir)
  1323. {
  1324. char version[PATH_MAX];
  1325. FILE *file;
  1326. char *name, *tmp;
  1327. const char *prefix = "Linux version ";
  1328. sprintf(version, "%s/proc/version", root_dir);
  1329. file = fopen(version, "r");
  1330. if (!file)
  1331. return NULL;
  1332. version[0] = '\0';
  1333. tmp = fgets(version, sizeof(version), file);
  1334. fclose(file);
  1335. name = strstr(version, prefix);
  1336. if (!name)
  1337. return NULL;
  1338. name += strlen(prefix);
  1339. tmp = strchr(name, ' ');
  1340. if (tmp)
  1341. *tmp = '\0';
  1342. return strdup(name);
  1343. }
  1344. static int machine__set_modules_path(struct machine *self)
  1345. {
  1346. char *version;
  1347. char modules_path[PATH_MAX];
  1348. version = get_kernel_version(self->root_dir);
  1349. if (!version)
  1350. return -1;
  1351. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1352. self->root_dir, version);
  1353. free(version);
  1354. return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
  1355. }
  1356. /*
  1357. * Constructor variant for modules (where we know from /proc/modules where
  1358. * they are loaded) and for vmlinux, where only after we load all the
  1359. * symbols we'll know where it starts and ends.
  1360. */
  1361. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1362. {
  1363. struct map *self = calloc(1, (sizeof(*self) +
  1364. (dso->kernel ? sizeof(struct kmap) : 0)));
  1365. if (self != NULL) {
  1366. /*
  1367. * ->end will be filled after we load all the symbols
  1368. */
  1369. map__init(self, type, start, 0, 0, dso);
  1370. }
  1371. return self;
  1372. }
  1373. struct map *machine__new_module(struct machine *self, u64 start,
  1374. const char *filename)
  1375. {
  1376. struct map *map;
  1377. struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
  1378. if (dso == NULL)
  1379. return NULL;
  1380. map = map__new2(start, dso, MAP__FUNCTION);
  1381. if (map == NULL)
  1382. return NULL;
  1383. if (machine__is_host(self))
  1384. dso->origin = DSO__ORIG_KMODULE;
  1385. else
  1386. dso->origin = DSO__ORIG_GUEST_KMODULE;
  1387. map_groups__insert(&self->kmaps, map);
  1388. return map;
  1389. }
  1390. static int machine__create_modules(struct machine *self)
  1391. {
  1392. char *line = NULL;
  1393. size_t n;
  1394. FILE *file;
  1395. struct map *map;
  1396. const char *modules;
  1397. char path[PATH_MAX];
  1398. if (machine__is_default_guest(self))
  1399. modules = symbol_conf.default_guest_modules;
  1400. else {
  1401. sprintf(path, "%s/proc/modules", self->root_dir);
  1402. modules = path;
  1403. }
  1404. file = fopen(modules, "r");
  1405. if (file == NULL)
  1406. return -1;
  1407. while (!feof(file)) {
  1408. char name[PATH_MAX];
  1409. u64 start;
  1410. char *sep;
  1411. int line_len;
  1412. line_len = getline(&line, &n, file);
  1413. if (line_len < 0)
  1414. break;
  1415. if (!line)
  1416. goto out_failure;
  1417. line[--line_len] = '\0'; /* \n */
  1418. sep = strrchr(line, 'x');
  1419. if (sep == NULL)
  1420. continue;
  1421. hex2u64(sep + 1, &start);
  1422. sep = strchr(line, ' ');
  1423. if (sep == NULL)
  1424. continue;
  1425. *sep = '\0';
  1426. snprintf(name, sizeof(name), "[%s]", line);
  1427. map = machine__new_module(self, start, name);
  1428. if (map == NULL)
  1429. goto out_delete_line;
  1430. dso__kernel_module_get_build_id(map->dso, self->root_dir);
  1431. }
  1432. free(line);
  1433. fclose(file);
  1434. return machine__set_modules_path(self);
  1435. out_delete_line:
  1436. free(line);
  1437. out_failure:
  1438. return -1;
  1439. }
  1440. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1441. const char *vmlinux, symbol_filter_t filter)
  1442. {
  1443. int err = -1, fd;
  1444. fd = open(vmlinux, O_RDONLY);
  1445. if (fd < 0)
  1446. return -1;
  1447. dso__set_loaded(self, map->type);
  1448. err = dso__load_sym(self, map, vmlinux, fd, filter, 0, 0);
  1449. close(fd);
  1450. if (err > 0)
  1451. pr_debug("Using %s for symbols\n", vmlinux);
  1452. return err;
  1453. }
  1454. int dso__load_vmlinux_path(struct dso *self, struct map *map,
  1455. symbol_filter_t filter)
  1456. {
  1457. int i, err = 0;
  1458. char *filename;
  1459. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1460. vmlinux_path__nr_entries + 1);
  1461. filename = dso__build_id_filename(self, NULL, 0);
  1462. if (filename != NULL) {
  1463. err = dso__load_vmlinux(self, map, filename, filter);
  1464. if (err > 0) {
  1465. dso__set_long_name(self, filename);
  1466. goto out;
  1467. }
  1468. free(filename);
  1469. }
  1470. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1471. err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
  1472. if (err > 0) {
  1473. dso__set_long_name(self, strdup(vmlinux_path[i]));
  1474. break;
  1475. }
  1476. }
  1477. out:
  1478. return err;
  1479. }
  1480. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1481. symbol_filter_t filter)
  1482. {
  1483. int err;
  1484. const char *kallsyms_filename = NULL;
  1485. char *kallsyms_allocated_filename = NULL;
  1486. /*
  1487. * Step 1: if the user specified a vmlinux filename, use it and only
  1488. * it, reporting errors to the user if it cannot be used.
  1489. *
  1490. * For instance, try to analyse an ARM perf.data file _without_ a
  1491. * build-id, or if the user specifies the wrong path to the right
  1492. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1493. * x86_86 one, on the machine where analysis is being performed, say),
  1494. * or worse, /proc/kallsyms.
  1495. *
  1496. * If the specified file _has_ a build-id and there is a build-id
  1497. * section in the perf.data file, we will still do the expected
  1498. * validation in dso__load_vmlinux and will bail out if they don't
  1499. * match.
  1500. */
  1501. if (symbol_conf.vmlinux_name != NULL) {
  1502. err = dso__load_vmlinux(self, map,
  1503. symbol_conf.vmlinux_name, filter);
  1504. if (err > 0) {
  1505. dso__set_long_name(self,
  1506. strdup(symbol_conf.vmlinux_name));
  1507. goto out_fixup;
  1508. }
  1509. return err;
  1510. }
  1511. if (vmlinux_path != NULL) {
  1512. err = dso__load_vmlinux_path(self, map, filter);
  1513. if (err > 0)
  1514. goto out_fixup;
  1515. }
  1516. /*
  1517. * Say the kernel DSO was created when processing the build-id header table,
  1518. * we have a build-id, so check if it is the same as the running kernel,
  1519. * using it if it is.
  1520. */
  1521. if (self->has_build_id) {
  1522. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1523. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1524. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1525. sizeof(kallsyms_build_id)) == 0) {
  1526. if (dso__build_id_equal(self, kallsyms_build_id)) {
  1527. kallsyms_filename = "/proc/kallsyms";
  1528. goto do_kallsyms;
  1529. }
  1530. }
  1531. /*
  1532. * Now look if we have it on the build-id cache in
  1533. * $HOME/.debug/[kernel.kallsyms].
  1534. */
  1535. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1536. sbuild_id);
  1537. if (asprintf(&kallsyms_allocated_filename,
  1538. "%s/.debug/[kernel.kallsyms]/%s",
  1539. getenv("HOME"), sbuild_id) == -1) {
  1540. pr_err("Not enough memory for kallsyms file lookup\n");
  1541. return -1;
  1542. }
  1543. kallsyms_filename = kallsyms_allocated_filename;
  1544. if (access(kallsyms_filename, F_OK)) {
  1545. pr_err("No kallsyms or vmlinux with build-id %s "
  1546. "was found\n", sbuild_id);
  1547. free(kallsyms_allocated_filename);
  1548. return -1;
  1549. }
  1550. } else {
  1551. /*
  1552. * Last resort, if we don't have a build-id and couldn't find
  1553. * any vmlinux file, try the running kernel kallsyms table.
  1554. */
  1555. kallsyms_filename = "/proc/kallsyms";
  1556. }
  1557. do_kallsyms:
  1558. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1559. if (err > 0)
  1560. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1561. free(kallsyms_allocated_filename);
  1562. if (err > 0) {
  1563. out_fixup:
  1564. if (kallsyms_filename != NULL)
  1565. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1566. map__fixup_start(map);
  1567. map__fixup_end(map);
  1568. }
  1569. return err;
  1570. }
  1571. static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
  1572. symbol_filter_t filter)
  1573. {
  1574. int err;
  1575. const char *kallsyms_filename = NULL;
  1576. struct machine *machine;
  1577. char path[PATH_MAX];
  1578. if (!map->groups) {
  1579. pr_debug("Guest kernel map hasn't the point to groups\n");
  1580. return -1;
  1581. }
  1582. machine = map->groups->machine;
  1583. if (machine__is_default_guest(machine)) {
  1584. /*
  1585. * if the user specified a vmlinux filename, use it and only
  1586. * it, reporting errors to the user if it cannot be used.
  1587. * Or use file guest_kallsyms inputted by user on commandline
  1588. */
  1589. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1590. err = dso__load_vmlinux(self, map,
  1591. symbol_conf.default_guest_vmlinux_name, filter);
  1592. goto out_try_fixup;
  1593. }
  1594. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1595. if (!kallsyms_filename)
  1596. return -1;
  1597. } else {
  1598. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1599. kallsyms_filename = path;
  1600. }
  1601. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1602. if (err > 0)
  1603. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1604. out_try_fixup:
  1605. if (err > 0) {
  1606. if (kallsyms_filename != NULL) {
  1607. machine__mmap_name(machine, path, sizeof(path));
  1608. dso__set_long_name(self, strdup(path));
  1609. }
  1610. map__fixup_start(map);
  1611. map__fixup_end(map);
  1612. }
  1613. return err;
  1614. }
  1615. static void dsos__add(struct list_head *head, struct dso *dso)
  1616. {
  1617. list_add_tail(&dso->node, head);
  1618. }
  1619. static struct dso *dsos__find(struct list_head *head, const char *name)
  1620. {
  1621. struct dso *pos;
  1622. list_for_each_entry(pos, head, node)
  1623. if (strcmp(pos->long_name, name) == 0)
  1624. return pos;
  1625. return NULL;
  1626. }
  1627. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1628. {
  1629. struct dso *dso = dsos__find(head, name);
  1630. if (!dso) {
  1631. dso = dso__new(name);
  1632. if (dso != NULL) {
  1633. dsos__add(head, dso);
  1634. dso__set_basename(dso);
  1635. }
  1636. }
  1637. return dso;
  1638. }
  1639. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1640. {
  1641. struct dso *pos;
  1642. size_t ret = 0;
  1643. list_for_each_entry(pos, head, node) {
  1644. int i;
  1645. for (i = 0; i < MAP__NR_TYPES; ++i)
  1646. ret += dso__fprintf(pos, i, fp);
  1647. }
  1648. return ret;
  1649. }
  1650. size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
  1651. {
  1652. struct rb_node *nd;
  1653. size_t ret = 0;
  1654. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1655. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1656. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1657. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1658. }
  1659. return ret;
  1660. }
  1661. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1662. bool with_hits)
  1663. {
  1664. struct dso *pos;
  1665. size_t ret = 0;
  1666. list_for_each_entry(pos, head, node) {
  1667. if (with_hits && !pos->hit)
  1668. continue;
  1669. ret += dso__fprintf_buildid(pos, fp);
  1670. ret += fprintf(fp, " %s\n", pos->long_name);
  1671. }
  1672. return ret;
  1673. }
  1674. size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
  1675. {
  1676. return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
  1677. __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
  1678. }
  1679. size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
  1680. {
  1681. struct rb_node *nd;
  1682. size_t ret = 0;
  1683. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1684. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1685. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1686. }
  1687. return ret;
  1688. }
  1689. struct dso *dso__new_kernel(const char *name)
  1690. {
  1691. struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
  1692. if (self != NULL) {
  1693. dso__set_short_name(self, "[kernel]");
  1694. self->kernel = DSO_TYPE_KERNEL;
  1695. }
  1696. return self;
  1697. }
  1698. static struct dso *dso__new_guest_kernel(struct machine *machine,
  1699. const char *name)
  1700. {
  1701. char bf[PATH_MAX];
  1702. struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
  1703. if (self != NULL) {
  1704. dso__set_short_name(self, "[guest.kernel]");
  1705. self->kernel = DSO_TYPE_GUEST_KERNEL;
  1706. }
  1707. return self;
  1708. }
  1709. void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
  1710. {
  1711. char path[PATH_MAX];
  1712. if (machine__is_default_guest(machine))
  1713. return;
  1714. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1715. if (sysfs__read_build_id(path, self->build_id,
  1716. sizeof(self->build_id)) == 0)
  1717. self->has_build_id = true;
  1718. }
  1719. static struct dso *machine__create_kernel(struct machine *self)
  1720. {
  1721. const char *vmlinux_name = NULL;
  1722. struct dso *kernel;
  1723. if (machine__is_host(self)) {
  1724. vmlinux_name = symbol_conf.vmlinux_name;
  1725. kernel = dso__new_kernel(vmlinux_name);
  1726. } else {
  1727. if (machine__is_default_guest(self))
  1728. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1729. kernel = dso__new_guest_kernel(self, vmlinux_name);
  1730. }
  1731. if (kernel != NULL) {
  1732. dso__read_running_kernel_build_id(kernel, self);
  1733. dsos__add(&self->kernel_dsos, kernel);
  1734. }
  1735. return kernel;
  1736. }
  1737. int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
  1738. {
  1739. enum map_type type;
  1740. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1741. struct kmap *kmap;
  1742. self->vmlinux_maps[type] = map__new2(0, kernel, type);
  1743. if (self->vmlinux_maps[type] == NULL)
  1744. return -1;
  1745. self->vmlinux_maps[type]->map_ip =
  1746. self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
  1747. kmap = map__kmap(self->vmlinux_maps[type]);
  1748. kmap->kmaps = &self->kmaps;
  1749. map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
  1750. }
  1751. return 0;
  1752. }
  1753. void machine__destroy_kernel_maps(struct machine *self)
  1754. {
  1755. enum map_type type;
  1756. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1757. struct kmap *kmap;
  1758. if (self->vmlinux_maps[type] == NULL)
  1759. continue;
  1760. kmap = map__kmap(self->vmlinux_maps[type]);
  1761. map_groups__remove(&self->kmaps, self->vmlinux_maps[type]);
  1762. if (kmap->ref_reloc_sym) {
  1763. /*
  1764. * ref_reloc_sym is shared among all maps, so free just
  1765. * on one of them.
  1766. */
  1767. if (type == MAP__FUNCTION) {
  1768. free((char *)kmap->ref_reloc_sym->name);
  1769. kmap->ref_reloc_sym->name = NULL;
  1770. free(kmap->ref_reloc_sym);
  1771. }
  1772. kmap->ref_reloc_sym = NULL;
  1773. }
  1774. map__delete(self->vmlinux_maps[type]);
  1775. self->vmlinux_maps[type] = NULL;
  1776. }
  1777. }
  1778. int machine__create_kernel_maps(struct machine *self)
  1779. {
  1780. struct dso *kernel = machine__create_kernel(self);
  1781. if (kernel == NULL ||
  1782. __machine__create_kernel_maps(self, kernel) < 0)
  1783. return -1;
  1784. if (symbol_conf.use_modules && machine__create_modules(self) < 0)
  1785. pr_debug("Problems creating module maps, continuing anyway...\n");
  1786. /*
  1787. * Now that we have all the maps created, just set the ->end of them:
  1788. */
  1789. map_groups__fixup_end(&self->kmaps);
  1790. return 0;
  1791. }
  1792. static void vmlinux_path__exit(void)
  1793. {
  1794. while (--vmlinux_path__nr_entries >= 0) {
  1795. free(vmlinux_path[vmlinux_path__nr_entries]);
  1796. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1797. }
  1798. free(vmlinux_path);
  1799. vmlinux_path = NULL;
  1800. }
  1801. static int vmlinux_path__init(void)
  1802. {
  1803. struct utsname uts;
  1804. char bf[PATH_MAX];
  1805. if (uname(&uts) < 0)
  1806. return -1;
  1807. vmlinux_path = malloc(sizeof(char *) * 5);
  1808. if (vmlinux_path == NULL)
  1809. return -1;
  1810. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1811. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1812. goto out_fail;
  1813. ++vmlinux_path__nr_entries;
  1814. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1815. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1816. goto out_fail;
  1817. ++vmlinux_path__nr_entries;
  1818. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1819. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1820. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1821. goto out_fail;
  1822. ++vmlinux_path__nr_entries;
  1823. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1824. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1825. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1826. goto out_fail;
  1827. ++vmlinux_path__nr_entries;
  1828. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1829. uts.release);
  1830. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1831. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1832. goto out_fail;
  1833. ++vmlinux_path__nr_entries;
  1834. return 0;
  1835. out_fail:
  1836. vmlinux_path__exit();
  1837. return -1;
  1838. }
  1839. size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
  1840. {
  1841. int i;
  1842. size_t printed = 0;
  1843. struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
  1844. if (kdso->has_build_id) {
  1845. char filename[PATH_MAX];
  1846. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  1847. printed += fprintf(fp, "[0] %s\n", filename);
  1848. }
  1849. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  1850. printed += fprintf(fp, "[%d] %s\n",
  1851. i + kdso->has_build_id, vmlinux_path[i]);
  1852. return printed;
  1853. }
  1854. static int setup_list(struct strlist **list, const char *list_str,
  1855. const char *list_name)
  1856. {
  1857. if (list_str == NULL)
  1858. return 0;
  1859. *list = strlist__new(true, list_str);
  1860. if (!*list) {
  1861. pr_err("problems parsing %s list\n", list_name);
  1862. return -1;
  1863. }
  1864. return 0;
  1865. }
  1866. int symbol__init(void)
  1867. {
  1868. elf_version(EV_CURRENT);
  1869. if (symbol_conf.sort_by_name)
  1870. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1871. sizeof(struct symbol));
  1872. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1873. return -1;
  1874. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1875. pr_err("'.' is the only non valid --field-separator argument\n");
  1876. return -1;
  1877. }
  1878. if (setup_list(&symbol_conf.dso_list,
  1879. symbol_conf.dso_list_str, "dso") < 0)
  1880. return -1;
  1881. if (setup_list(&symbol_conf.comm_list,
  1882. symbol_conf.comm_list_str, "comm") < 0)
  1883. goto out_free_dso_list;
  1884. if (setup_list(&symbol_conf.sym_list,
  1885. symbol_conf.sym_list_str, "symbol") < 0)
  1886. goto out_free_comm_list;
  1887. return 0;
  1888. out_free_dso_list:
  1889. strlist__delete(symbol_conf.dso_list);
  1890. out_free_comm_list:
  1891. strlist__delete(symbol_conf.comm_list);
  1892. return -1;
  1893. }
  1894. void symbol__exit(void)
  1895. {
  1896. strlist__delete(symbol_conf.sym_list);
  1897. strlist__delete(symbol_conf.dso_list);
  1898. strlist__delete(symbol_conf.comm_list);
  1899. vmlinux_path__exit();
  1900. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1901. }
  1902. int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
  1903. {
  1904. struct machine *machine = machines__findnew(self, pid);
  1905. if (machine == NULL)
  1906. return -1;
  1907. return machine__create_kernel_maps(machine);
  1908. }
  1909. static int hex(char ch)
  1910. {
  1911. if ((ch >= '0') && (ch <= '9'))
  1912. return ch - '0';
  1913. if ((ch >= 'a') && (ch <= 'f'))
  1914. return ch - 'a' + 10;
  1915. if ((ch >= 'A') && (ch <= 'F'))
  1916. return ch - 'A' + 10;
  1917. return -1;
  1918. }
  1919. /*
  1920. * While we find nice hex chars, build a long_val.
  1921. * Return number of chars processed.
  1922. */
  1923. int hex2u64(const char *ptr, u64 *long_val)
  1924. {
  1925. const char *p = ptr;
  1926. *long_val = 0;
  1927. while (*p) {
  1928. const int hex_val = hex(*p);
  1929. if (hex_val < 0)
  1930. break;
  1931. *long_val = (*long_val << 4) | hex_val;
  1932. p++;
  1933. }
  1934. return p - ptr;
  1935. }
  1936. char *strxfrchar(char *s, char from, char to)
  1937. {
  1938. char *p = s;
  1939. while ((p = strchr(p, from)) != NULL)
  1940. *p++ = to;
  1941. return s;
  1942. }
  1943. int machines__create_guest_kernel_maps(struct rb_root *self)
  1944. {
  1945. int ret = 0;
  1946. struct dirent **namelist = NULL;
  1947. int i, items = 0;
  1948. char path[PATH_MAX];
  1949. pid_t pid;
  1950. if (symbol_conf.default_guest_vmlinux_name ||
  1951. symbol_conf.default_guest_modules ||
  1952. symbol_conf.default_guest_kallsyms) {
  1953. machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
  1954. }
  1955. if (symbol_conf.guestmount) {
  1956. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  1957. if (items <= 0)
  1958. return -ENOENT;
  1959. for (i = 0; i < items; i++) {
  1960. if (!isdigit(namelist[i]->d_name[0])) {
  1961. /* Filter out . and .. */
  1962. continue;
  1963. }
  1964. pid = atoi(namelist[i]->d_name);
  1965. sprintf(path, "%s/%s/proc/kallsyms",
  1966. symbol_conf.guestmount,
  1967. namelist[i]->d_name);
  1968. ret = access(path, R_OK);
  1969. if (ret) {
  1970. pr_debug("Can't access file %s\n", path);
  1971. goto failure;
  1972. }
  1973. machines__create_kernel_maps(self, pid);
  1974. }
  1975. failure:
  1976. free(namelist);
  1977. }
  1978. return ret;
  1979. }
  1980. void machines__destroy_guest_kernel_maps(struct rb_root *self)
  1981. {
  1982. struct rb_node *next = rb_first(self);
  1983. while (next) {
  1984. struct machine *pos = rb_entry(next, struct machine, rb_node);
  1985. next = rb_next(&pos->rb_node);
  1986. rb_erase(&pos->rb_node, self);
  1987. machine__delete(pos);
  1988. }
  1989. }
  1990. int machine__load_kallsyms(struct machine *self, const char *filename,
  1991. enum map_type type, symbol_filter_t filter)
  1992. {
  1993. struct map *map = self->vmlinux_maps[type];
  1994. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  1995. if (ret > 0) {
  1996. dso__set_loaded(map->dso, type);
  1997. /*
  1998. * Since /proc/kallsyms will have multiple sessions for the
  1999. * kernel, with modules between them, fixup the end of all
  2000. * sections.
  2001. */
  2002. __map_groups__fixup_end(&self->kmaps, type);
  2003. }
  2004. return ret;
  2005. }
  2006. int machine__load_vmlinux_path(struct machine *self, enum map_type type,
  2007. symbol_filter_t filter)
  2008. {
  2009. struct map *map = self->vmlinux_maps[type];
  2010. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2011. if (ret > 0) {
  2012. dso__set_loaded(map->dso, type);
  2013. map__reloc_vmlinux(map);
  2014. }
  2015. return ret;
  2016. }