symbol.c 59 KB

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