symbol.c 56 KB

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