symbol.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  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_dso->long_name = self->long_name;
  989. curr_dso->long_name_len = self->long_name_len;
  990. curr_map = map__new2(start, curr_dso,
  991. map->type);
  992. if (curr_map == NULL) {
  993. dso__delete(curr_dso);
  994. goto out_elf_end;
  995. }
  996. curr_map->map_ip = identity__map_ip;
  997. curr_map->unmap_ip = identity__map_ip;
  998. curr_dso->symtab_type = self->symtab_type;
  999. map_groups__insert(kmap->kmaps, curr_map);
  1000. dsos__add(&self->node, curr_dso);
  1001. dso__set_loaded(curr_dso, map->type);
  1002. } else
  1003. curr_dso = curr_map->dso;
  1004. goto new_symbol;
  1005. }
  1006. if (curr_dso->adjust_symbols) {
  1007. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  1008. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  1009. (u64)sym.st_value, (u64)shdr.sh_addr,
  1010. (u64)shdr.sh_offset);
  1011. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  1012. }
  1013. /*
  1014. * We need to figure out if the object was created from C++ sources
  1015. * DWARF DW_compile_unit has this, but we don't always have access
  1016. * to it...
  1017. */
  1018. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  1019. if (demangled != NULL)
  1020. elf_name = demangled;
  1021. new_symbol:
  1022. f = symbol__new(sym.st_value, sym.st_size,
  1023. GELF_ST_BIND(sym.st_info), elf_name);
  1024. free(demangled);
  1025. if (!f)
  1026. goto out_elf_end;
  1027. if (filter && filter(curr_map, f))
  1028. symbol__delete(f);
  1029. else {
  1030. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  1031. nr++;
  1032. }
  1033. }
  1034. /*
  1035. * For misannotated, zeroed, ASM function sizes.
  1036. */
  1037. if (nr > 0) {
  1038. symbols__fixup_end(&self->symbols[map->type]);
  1039. if (kmap) {
  1040. /*
  1041. * We need to fixup this here too because we create new
  1042. * maps here, for things like vsyscall sections.
  1043. */
  1044. __map_groups__fixup_end(kmap->kmaps, map->type);
  1045. }
  1046. }
  1047. err = nr;
  1048. out_elf_end:
  1049. elf_end(elf);
  1050. out_close:
  1051. return err;
  1052. }
  1053. static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
  1054. {
  1055. return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
  1056. }
  1057. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  1058. {
  1059. bool have_build_id = false;
  1060. struct dso *pos;
  1061. list_for_each_entry(pos, head, node) {
  1062. if (with_hits && !pos->hit)
  1063. continue;
  1064. if (pos->has_build_id) {
  1065. have_build_id = true;
  1066. continue;
  1067. }
  1068. if (filename__read_build_id(pos->long_name, pos->build_id,
  1069. sizeof(pos->build_id)) > 0) {
  1070. have_build_id = true;
  1071. pos->has_build_id = true;
  1072. }
  1073. }
  1074. return have_build_id;
  1075. }
  1076. /*
  1077. * Align offset to 4 bytes as needed for note name and descriptor data.
  1078. */
  1079. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  1080. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  1081. {
  1082. int err = -1;
  1083. GElf_Ehdr ehdr;
  1084. GElf_Shdr shdr;
  1085. Elf_Data *data;
  1086. Elf_Scn *sec;
  1087. Elf_Kind ek;
  1088. void *ptr;
  1089. if (size < BUILD_ID_SIZE)
  1090. goto out;
  1091. ek = elf_kind(elf);
  1092. if (ek != ELF_K_ELF)
  1093. goto out;
  1094. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1095. pr_err("%s: cannot get elf header.\n", __func__);
  1096. goto out;
  1097. }
  1098. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1099. ".note.gnu.build-id", NULL);
  1100. if (sec == NULL) {
  1101. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1102. ".notes", NULL);
  1103. if (sec == NULL)
  1104. goto out;
  1105. }
  1106. data = elf_getdata(sec, NULL);
  1107. if (data == NULL)
  1108. goto out;
  1109. ptr = data->d_buf;
  1110. while (ptr < (data->d_buf + data->d_size)) {
  1111. GElf_Nhdr *nhdr = ptr;
  1112. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  1113. descsz = NOTE_ALIGN(nhdr->n_descsz);
  1114. const char *name;
  1115. ptr += sizeof(*nhdr);
  1116. name = ptr;
  1117. ptr += namesz;
  1118. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1119. nhdr->n_namesz == sizeof("GNU")) {
  1120. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1121. memcpy(bf, ptr, BUILD_ID_SIZE);
  1122. err = BUILD_ID_SIZE;
  1123. break;
  1124. }
  1125. }
  1126. ptr += descsz;
  1127. }
  1128. out:
  1129. return err;
  1130. }
  1131. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1132. {
  1133. int fd, err = -1;
  1134. Elf *elf;
  1135. if (size < BUILD_ID_SIZE)
  1136. goto out;
  1137. fd = open(filename, O_RDONLY);
  1138. if (fd < 0)
  1139. goto out;
  1140. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1141. if (elf == NULL) {
  1142. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1143. goto out_close;
  1144. }
  1145. err = elf_read_build_id(elf, bf, size);
  1146. elf_end(elf);
  1147. out_close:
  1148. close(fd);
  1149. out:
  1150. return err;
  1151. }
  1152. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1153. {
  1154. int fd, err = -1;
  1155. if (size < BUILD_ID_SIZE)
  1156. goto out;
  1157. fd = open(filename, O_RDONLY);
  1158. if (fd < 0)
  1159. goto out;
  1160. while (1) {
  1161. char bf[BUFSIZ];
  1162. GElf_Nhdr nhdr;
  1163. int namesz, descsz;
  1164. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1165. break;
  1166. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1167. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1168. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1169. nhdr.n_namesz == sizeof("GNU")) {
  1170. if (read(fd, bf, namesz) != namesz)
  1171. break;
  1172. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1173. if (read(fd, build_id,
  1174. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1175. err = 0;
  1176. break;
  1177. }
  1178. } else if (read(fd, bf, descsz) != descsz)
  1179. break;
  1180. } else {
  1181. int n = namesz + descsz;
  1182. if (read(fd, bf, n) != n)
  1183. break;
  1184. }
  1185. }
  1186. close(fd);
  1187. out:
  1188. return err;
  1189. }
  1190. char dso__symtab_origin(const struct dso *self)
  1191. {
  1192. static const char origin[] = {
  1193. [SYMTAB__KALLSYMS] = 'k',
  1194. [SYMTAB__JAVA_JIT] = 'j',
  1195. [SYMTAB__BUILD_ID_CACHE] = 'B',
  1196. [SYMTAB__FEDORA_DEBUGINFO] = 'f',
  1197. [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
  1198. [SYMTAB__BUILDID_DEBUGINFO] = 'b',
  1199. [SYMTAB__SYSTEM_PATH_DSO] = 'd',
  1200. [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
  1201. [SYMTAB__GUEST_KALLSYMS] = 'g',
  1202. [SYMTAB__GUEST_KMODULE] = 'G',
  1203. };
  1204. if (self == NULL || self->symtab_type == SYMTAB__NOT_FOUND)
  1205. return '!';
  1206. return origin[self->symtab_type];
  1207. }
  1208. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  1209. {
  1210. int size = PATH_MAX;
  1211. char *name;
  1212. int ret = -1;
  1213. int fd;
  1214. struct machine *machine;
  1215. const char *root_dir;
  1216. int want_symtab;
  1217. dso__set_loaded(self, map->type);
  1218. if (self->kernel == DSO_TYPE_KERNEL)
  1219. return dso__load_kernel_sym(self, map, filter);
  1220. else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
  1221. return dso__load_guest_kernel_sym(self, map, filter);
  1222. if (map->groups && map->groups->machine)
  1223. machine = map->groups->machine;
  1224. else
  1225. machine = NULL;
  1226. name = malloc(size);
  1227. if (!name)
  1228. return -1;
  1229. self->adjust_symbols = 0;
  1230. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  1231. ret = dso__load_perf_map(self, map, filter);
  1232. self->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
  1233. SYMTAB__NOT_FOUND;
  1234. return ret;
  1235. }
  1236. /* Iterate over candidate debug images.
  1237. * On the first pass, only load images if they have a full symtab.
  1238. * Failing that, do a second pass where we accept .dynsym also
  1239. */
  1240. want_symtab = 1;
  1241. restart:
  1242. for (self->symtab_type = SYMTAB__BUILD_ID_CACHE;
  1243. self->symtab_type != SYMTAB__NOT_FOUND;
  1244. self->symtab_type++) {
  1245. switch (self->symtab_type) {
  1246. case SYMTAB__BUILD_ID_CACHE:
  1247. /* skip the locally configured cache if a symfs is given */
  1248. if (symbol_conf.symfs[0] ||
  1249. (dso__build_id_filename(self, name, size) == NULL)) {
  1250. continue;
  1251. }
  1252. break;
  1253. case SYMTAB__FEDORA_DEBUGINFO:
  1254. snprintf(name, size, "%s/usr/lib/debug%s.debug",
  1255. symbol_conf.symfs, self->long_name);
  1256. break;
  1257. case SYMTAB__UBUNTU_DEBUGINFO:
  1258. snprintf(name, size, "%s/usr/lib/debug%s",
  1259. symbol_conf.symfs, self->long_name);
  1260. break;
  1261. case SYMTAB__BUILDID_DEBUGINFO: {
  1262. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1263. if (!self->has_build_id)
  1264. continue;
  1265. build_id__sprintf(self->build_id,
  1266. sizeof(self->build_id),
  1267. build_id_hex);
  1268. snprintf(name, size,
  1269. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  1270. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  1271. }
  1272. break;
  1273. case SYMTAB__SYSTEM_PATH_DSO:
  1274. snprintf(name, size, "%s%s",
  1275. symbol_conf.symfs, self->long_name);
  1276. break;
  1277. case SYMTAB__GUEST_KMODULE:
  1278. if (map->groups && machine)
  1279. root_dir = machine->root_dir;
  1280. else
  1281. root_dir = "";
  1282. snprintf(name, size, "%s%s%s", symbol_conf.symfs,
  1283. root_dir, self->long_name);
  1284. break;
  1285. case SYMTAB__SYSTEM_PATH_KMODULE:
  1286. snprintf(name, size, "%s%s", symbol_conf.symfs,
  1287. self->long_name);
  1288. break;
  1289. default:;
  1290. }
  1291. /* Name is now the name of the next image to try */
  1292. fd = open(name, O_RDONLY);
  1293. if (fd < 0)
  1294. continue;
  1295. ret = dso__load_sym(self, map, name, fd, filter, 0,
  1296. want_symtab);
  1297. close(fd);
  1298. /*
  1299. * Some people seem to have debuginfo files _WITHOUT_ debug
  1300. * info!?!?
  1301. */
  1302. if (!ret)
  1303. continue;
  1304. if (ret > 0) {
  1305. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  1306. if (nr_plt > 0)
  1307. ret += nr_plt;
  1308. break;
  1309. }
  1310. }
  1311. /*
  1312. * If we wanted a full symtab but no image had one,
  1313. * relax our requirements and repeat the search.
  1314. */
  1315. if (ret <= 0 && want_symtab) {
  1316. want_symtab = 0;
  1317. goto restart;
  1318. }
  1319. free(name);
  1320. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  1321. return 0;
  1322. return ret;
  1323. }
  1324. struct map *map_groups__find_by_name(struct map_groups *self,
  1325. enum map_type type, const char *name)
  1326. {
  1327. struct rb_node *nd;
  1328. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  1329. struct map *map = rb_entry(nd, struct map, rb_node);
  1330. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1331. return map;
  1332. }
  1333. return NULL;
  1334. }
  1335. static int dso__kernel_module_get_build_id(struct dso *self,
  1336. const char *root_dir)
  1337. {
  1338. char filename[PATH_MAX];
  1339. /*
  1340. * kernel module short names are of the form "[module]" and
  1341. * we need just "module" here.
  1342. */
  1343. const char *name = self->short_name + 1;
  1344. snprintf(filename, sizeof(filename),
  1345. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1346. root_dir, (int)strlen(name) - 1, name);
  1347. if (sysfs__read_build_id(filename, self->build_id,
  1348. sizeof(self->build_id)) == 0)
  1349. self->has_build_id = true;
  1350. return 0;
  1351. }
  1352. static int map_groups__set_modules_path_dir(struct map_groups *self,
  1353. const char *dir_name)
  1354. {
  1355. struct dirent *dent;
  1356. DIR *dir = opendir(dir_name);
  1357. int ret = 0;
  1358. if (!dir) {
  1359. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1360. return -1;
  1361. }
  1362. while ((dent = readdir(dir)) != NULL) {
  1363. char path[PATH_MAX];
  1364. struct stat st;
  1365. /*sshfs might return bad dent->d_type, so we have to stat*/
  1366. sprintf(path, "%s/%s", dir_name, dent->d_name);
  1367. if (stat(path, &st))
  1368. continue;
  1369. if (S_ISDIR(st.st_mode)) {
  1370. if (!strcmp(dent->d_name, ".") ||
  1371. !strcmp(dent->d_name, ".."))
  1372. continue;
  1373. snprintf(path, sizeof(path), "%s/%s",
  1374. dir_name, dent->d_name);
  1375. ret = map_groups__set_modules_path_dir(self, path);
  1376. if (ret < 0)
  1377. goto out;
  1378. } else {
  1379. char *dot = strrchr(dent->d_name, '.'),
  1380. dso_name[PATH_MAX];
  1381. struct map *map;
  1382. char *long_name;
  1383. if (dot == NULL || strcmp(dot, ".ko"))
  1384. continue;
  1385. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1386. (int)(dot - dent->d_name), dent->d_name);
  1387. strxfrchar(dso_name, '-', '_');
  1388. map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
  1389. if (map == NULL)
  1390. continue;
  1391. snprintf(path, sizeof(path), "%s/%s",
  1392. dir_name, dent->d_name);
  1393. long_name = strdup(path);
  1394. if (long_name == NULL) {
  1395. ret = -1;
  1396. goto out;
  1397. }
  1398. dso__set_long_name(map->dso, long_name);
  1399. map->dso->lname_alloc = 1;
  1400. dso__kernel_module_get_build_id(map->dso, "");
  1401. }
  1402. }
  1403. out:
  1404. closedir(dir);
  1405. return ret;
  1406. }
  1407. static char *get_kernel_version(const char *root_dir)
  1408. {
  1409. char version[PATH_MAX];
  1410. FILE *file;
  1411. char *name, *tmp;
  1412. const char *prefix = "Linux version ";
  1413. sprintf(version, "%s/proc/version", root_dir);
  1414. file = fopen(version, "r");
  1415. if (!file)
  1416. return NULL;
  1417. version[0] = '\0';
  1418. tmp = fgets(version, sizeof(version), file);
  1419. fclose(file);
  1420. name = strstr(version, prefix);
  1421. if (!name)
  1422. return NULL;
  1423. name += strlen(prefix);
  1424. tmp = strchr(name, ' ');
  1425. if (tmp)
  1426. *tmp = '\0';
  1427. return strdup(name);
  1428. }
  1429. static int machine__set_modules_path(struct machine *self)
  1430. {
  1431. char *version;
  1432. char modules_path[PATH_MAX];
  1433. version = get_kernel_version(self->root_dir);
  1434. if (!version)
  1435. return -1;
  1436. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1437. self->root_dir, version);
  1438. free(version);
  1439. return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
  1440. }
  1441. /*
  1442. * Constructor variant for modules (where we know from /proc/modules where
  1443. * they are loaded) and for vmlinux, where only after we load all the
  1444. * symbols we'll know where it starts and ends.
  1445. */
  1446. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1447. {
  1448. struct map *self = calloc(1, (sizeof(*self) +
  1449. (dso->kernel ? sizeof(struct kmap) : 0)));
  1450. if (self != NULL) {
  1451. /*
  1452. * ->end will be filled after we load all the symbols
  1453. */
  1454. map__init(self, type, start, 0, 0, dso);
  1455. }
  1456. return self;
  1457. }
  1458. struct map *machine__new_module(struct machine *self, u64 start,
  1459. const char *filename)
  1460. {
  1461. struct map *map;
  1462. struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
  1463. if (dso == NULL)
  1464. return NULL;
  1465. map = map__new2(start, dso, MAP__FUNCTION);
  1466. if (map == NULL)
  1467. return NULL;
  1468. if (machine__is_host(self))
  1469. dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
  1470. else
  1471. dso->symtab_type = SYMTAB__GUEST_KMODULE;
  1472. map_groups__insert(&self->kmaps, map);
  1473. return map;
  1474. }
  1475. static int machine__create_modules(struct machine *self)
  1476. {
  1477. char *line = NULL;
  1478. size_t n;
  1479. FILE *file;
  1480. struct map *map;
  1481. const char *modules;
  1482. char path[PATH_MAX];
  1483. if (machine__is_default_guest(self))
  1484. modules = symbol_conf.default_guest_modules;
  1485. else {
  1486. sprintf(path, "%s/proc/modules", self->root_dir);
  1487. modules = path;
  1488. }
  1489. file = fopen(modules, "r");
  1490. if (file == NULL)
  1491. return -1;
  1492. while (!feof(file)) {
  1493. char name[PATH_MAX];
  1494. u64 start;
  1495. char *sep;
  1496. int line_len;
  1497. line_len = getline(&line, &n, file);
  1498. if (line_len < 0)
  1499. break;
  1500. if (!line)
  1501. goto out_failure;
  1502. line[--line_len] = '\0'; /* \n */
  1503. sep = strrchr(line, 'x');
  1504. if (sep == NULL)
  1505. continue;
  1506. hex2u64(sep + 1, &start);
  1507. sep = strchr(line, ' ');
  1508. if (sep == NULL)
  1509. continue;
  1510. *sep = '\0';
  1511. snprintf(name, sizeof(name), "[%s]", line);
  1512. map = machine__new_module(self, start, name);
  1513. if (map == NULL)
  1514. goto out_delete_line;
  1515. dso__kernel_module_get_build_id(map->dso, self->root_dir);
  1516. }
  1517. free(line);
  1518. fclose(file);
  1519. return machine__set_modules_path(self);
  1520. out_delete_line:
  1521. free(line);
  1522. out_failure:
  1523. return -1;
  1524. }
  1525. int dso__load_vmlinux(struct dso *self, struct map *map,
  1526. const char *vmlinux, symbol_filter_t filter)
  1527. {
  1528. int err = -1, fd;
  1529. char symfs_vmlinux[PATH_MAX];
  1530. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1531. symbol_conf.symfs, vmlinux);
  1532. fd = open(symfs_vmlinux, O_RDONLY);
  1533. if (fd < 0)
  1534. return -1;
  1535. dso__set_long_name(self, (char *)vmlinux);
  1536. dso__set_loaded(self, map->type);
  1537. err = dso__load_sym(self, map, symfs_vmlinux, fd, filter, 0, 0);
  1538. close(fd);
  1539. if (err > 0)
  1540. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1541. return err;
  1542. }
  1543. int dso__load_vmlinux_path(struct dso *self, struct map *map,
  1544. symbol_filter_t filter)
  1545. {
  1546. int i, err = 0;
  1547. char *filename;
  1548. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1549. vmlinux_path__nr_entries + 1);
  1550. filename = dso__build_id_filename(self, NULL, 0);
  1551. if (filename != NULL) {
  1552. err = dso__load_vmlinux(self, map, filename, filter);
  1553. if (err > 0) {
  1554. dso__set_long_name(self, filename);
  1555. goto out;
  1556. }
  1557. free(filename);
  1558. }
  1559. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1560. err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
  1561. if (err > 0) {
  1562. dso__set_long_name(self, strdup(vmlinux_path[i]));
  1563. break;
  1564. }
  1565. }
  1566. out:
  1567. return err;
  1568. }
  1569. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1570. symbol_filter_t filter)
  1571. {
  1572. int err;
  1573. const char *kallsyms_filename = NULL;
  1574. char *kallsyms_allocated_filename = NULL;
  1575. /*
  1576. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1577. * it and only it, reporting errors to the user if it cannot be used.
  1578. *
  1579. * For instance, try to analyse an ARM perf.data file _without_ a
  1580. * build-id, or if the user specifies the wrong path to the right
  1581. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1582. * x86_86 one, on the machine where analysis is being performed, say),
  1583. * or worse, /proc/kallsyms.
  1584. *
  1585. * If the specified file _has_ a build-id and there is a build-id
  1586. * section in the perf.data file, we will still do the expected
  1587. * validation in dso__load_vmlinux and will bail out if they don't
  1588. * match.
  1589. */
  1590. if (symbol_conf.kallsyms_name != NULL) {
  1591. kallsyms_filename = symbol_conf.kallsyms_name;
  1592. goto do_kallsyms;
  1593. }
  1594. if (symbol_conf.vmlinux_name != NULL) {
  1595. err = dso__load_vmlinux(self, map,
  1596. symbol_conf.vmlinux_name, filter);
  1597. if (err > 0) {
  1598. dso__set_long_name(self,
  1599. strdup(symbol_conf.vmlinux_name));
  1600. goto out_fixup;
  1601. }
  1602. return err;
  1603. }
  1604. if (vmlinux_path != NULL) {
  1605. err = dso__load_vmlinux_path(self, map, filter);
  1606. if (err > 0)
  1607. goto out_fixup;
  1608. }
  1609. /* do not try local files if a symfs was given */
  1610. if (symbol_conf.symfs[0] != 0)
  1611. return -1;
  1612. /*
  1613. * Say the kernel DSO was created when processing the build-id header table,
  1614. * we have a build-id, so check if it is the same as the running kernel,
  1615. * using it if it is.
  1616. */
  1617. if (self->has_build_id) {
  1618. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1619. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1620. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1621. sizeof(kallsyms_build_id)) == 0) {
  1622. if (dso__build_id_equal(self, kallsyms_build_id)) {
  1623. kallsyms_filename = "/proc/kallsyms";
  1624. goto do_kallsyms;
  1625. }
  1626. }
  1627. /*
  1628. * Now look if we have it on the build-id cache in
  1629. * $HOME/.debug/[kernel.kallsyms].
  1630. */
  1631. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1632. sbuild_id);
  1633. if (asprintf(&kallsyms_allocated_filename,
  1634. "%s/.debug/[kernel.kallsyms]/%s",
  1635. getenv("HOME"), sbuild_id) == -1) {
  1636. pr_err("Not enough memory for kallsyms file lookup\n");
  1637. return -1;
  1638. }
  1639. kallsyms_filename = kallsyms_allocated_filename;
  1640. if (access(kallsyms_filename, F_OK)) {
  1641. pr_err("No kallsyms or vmlinux with build-id %s "
  1642. "was found\n", sbuild_id);
  1643. free(kallsyms_allocated_filename);
  1644. return -1;
  1645. }
  1646. } else {
  1647. /*
  1648. * Last resort, if we don't have a build-id and couldn't find
  1649. * any vmlinux file, try the running kernel kallsyms table.
  1650. */
  1651. kallsyms_filename = "/proc/kallsyms";
  1652. }
  1653. do_kallsyms:
  1654. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1655. if (err > 0)
  1656. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1657. free(kallsyms_allocated_filename);
  1658. if (err > 0) {
  1659. out_fixup:
  1660. if (kallsyms_filename != NULL)
  1661. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1662. map__fixup_start(map);
  1663. map__fixup_end(map);
  1664. }
  1665. return err;
  1666. }
  1667. static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
  1668. symbol_filter_t filter)
  1669. {
  1670. int err;
  1671. const char *kallsyms_filename = NULL;
  1672. struct machine *machine;
  1673. char path[PATH_MAX];
  1674. if (!map->groups) {
  1675. pr_debug("Guest kernel map hasn't the point to groups\n");
  1676. return -1;
  1677. }
  1678. machine = map->groups->machine;
  1679. if (machine__is_default_guest(machine)) {
  1680. /*
  1681. * if the user specified a vmlinux filename, use it and only
  1682. * it, reporting errors to the user if it cannot be used.
  1683. * Or use file guest_kallsyms inputted by user on commandline
  1684. */
  1685. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1686. err = dso__load_vmlinux(self, map,
  1687. symbol_conf.default_guest_vmlinux_name, filter);
  1688. goto out_try_fixup;
  1689. }
  1690. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1691. if (!kallsyms_filename)
  1692. return -1;
  1693. } else {
  1694. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1695. kallsyms_filename = path;
  1696. }
  1697. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1698. if (err > 0)
  1699. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1700. out_try_fixup:
  1701. if (err > 0) {
  1702. if (kallsyms_filename != NULL) {
  1703. machine__mmap_name(machine, path, sizeof(path));
  1704. dso__set_long_name(self, strdup(path));
  1705. }
  1706. map__fixup_start(map);
  1707. map__fixup_end(map);
  1708. }
  1709. return err;
  1710. }
  1711. static void dsos__add(struct list_head *head, struct dso *dso)
  1712. {
  1713. list_add_tail(&dso->node, head);
  1714. }
  1715. static struct dso *dsos__find(struct list_head *head, const char *name)
  1716. {
  1717. struct dso *pos;
  1718. list_for_each_entry(pos, head, node)
  1719. if (strcmp(pos->long_name, name) == 0)
  1720. return pos;
  1721. return NULL;
  1722. }
  1723. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1724. {
  1725. struct dso *dso = dsos__find(head, name);
  1726. if (!dso) {
  1727. dso = dso__new(name);
  1728. if (dso != NULL) {
  1729. dsos__add(head, dso);
  1730. dso__set_basename(dso);
  1731. }
  1732. }
  1733. return dso;
  1734. }
  1735. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1736. {
  1737. struct dso *pos;
  1738. size_t ret = 0;
  1739. list_for_each_entry(pos, head, node) {
  1740. int i;
  1741. for (i = 0; i < MAP__NR_TYPES; ++i)
  1742. ret += dso__fprintf(pos, i, fp);
  1743. }
  1744. return ret;
  1745. }
  1746. size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
  1747. {
  1748. struct rb_node *nd;
  1749. size_t ret = 0;
  1750. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1751. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1752. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1753. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1754. }
  1755. return ret;
  1756. }
  1757. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1758. bool with_hits)
  1759. {
  1760. struct dso *pos;
  1761. size_t ret = 0;
  1762. list_for_each_entry(pos, head, node) {
  1763. if (with_hits && !pos->hit)
  1764. continue;
  1765. ret += dso__fprintf_buildid(pos, fp);
  1766. ret += fprintf(fp, " %s\n", pos->long_name);
  1767. }
  1768. return ret;
  1769. }
  1770. size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
  1771. {
  1772. return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
  1773. __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
  1774. }
  1775. size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
  1776. {
  1777. struct rb_node *nd;
  1778. size_t ret = 0;
  1779. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1780. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1781. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1782. }
  1783. return ret;
  1784. }
  1785. struct dso *dso__new_kernel(const char *name)
  1786. {
  1787. struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
  1788. if (self != NULL) {
  1789. dso__set_short_name(self, "[kernel]");
  1790. self->kernel = DSO_TYPE_KERNEL;
  1791. }
  1792. return self;
  1793. }
  1794. static struct dso *dso__new_guest_kernel(struct machine *machine,
  1795. const char *name)
  1796. {
  1797. char bf[PATH_MAX];
  1798. struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
  1799. if (self != NULL) {
  1800. dso__set_short_name(self, "[guest.kernel]");
  1801. self->kernel = DSO_TYPE_GUEST_KERNEL;
  1802. }
  1803. return self;
  1804. }
  1805. void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
  1806. {
  1807. char path[PATH_MAX];
  1808. if (machine__is_default_guest(machine))
  1809. return;
  1810. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1811. if (sysfs__read_build_id(path, self->build_id,
  1812. sizeof(self->build_id)) == 0)
  1813. self->has_build_id = true;
  1814. }
  1815. static struct dso *machine__create_kernel(struct machine *self)
  1816. {
  1817. const char *vmlinux_name = NULL;
  1818. struct dso *kernel;
  1819. if (machine__is_host(self)) {
  1820. vmlinux_name = symbol_conf.vmlinux_name;
  1821. kernel = dso__new_kernel(vmlinux_name);
  1822. } else {
  1823. if (machine__is_default_guest(self))
  1824. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1825. kernel = dso__new_guest_kernel(self, vmlinux_name);
  1826. }
  1827. if (kernel != NULL) {
  1828. dso__read_running_kernel_build_id(kernel, self);
  1829. dsos__add(&self->kernel_dsos, kernel);
  1830. }
  1831. return kernel;
  1832. }
  1833. struct process_args {
  1834. u64 start;
  1835. };
  1836. static int symbol__in_kernel(void *arg, const char *name,
  1837. char type __used, u64 start, u64 end __used)
  1838. {
  1839. struct process_args *args = arg;
  1840. if (strchr(name, '['))
  1841. return 0;
  1842. args->start = start;
  1843. return 1;
  1844. }
  1845. /* Figure out the start address of kernel map from /proc/kallsyms */
  1846. static u64 machine__get_kernel_start_addr(struct machine *machine)
  1847. {
  1848. const char *filename;
  1849. char path[PATH_MAX];
  1850. struct process_args args;
  1851. if (machine__is_host(machine)) {
  1852. filename = "/proc/kallsyms";
  1853. } else {
  1854. if (machine__is_default_guest(machine))
  1855. filename = (char *)symbol_conf.default_guest_kallsyms;
  1856. else {
  1857. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1858. filename = path;
  1859. }
  1860. }
  1861. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  1862. return 0;
  1863. return args.start;
  1864. }
  1865. int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
  1866. {
  1867. enum map_type type;
  1868. u64 start = machine__get_kernel_start_addr(self);
  1869. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1870. struct kmap *kmap;
  1871. self->vmlinux_maps[type] = map__new2(start, kernel, type);
  1872. if (self->vmlinux_maps[type] == NULL)
  1873. return -1;
  1874. self->vmlinux_maps[type]->map_ip =
  1875. self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
  1876. kmap = map__kmap(self->vmlinux_maps[type]);
  1877. kmap->kmaps = &self->kmaps;
  1878. map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
  1879. }
  1880. return 0;
  1881. }
  1882. void machine__destroy_kernel_maps(struct machine *self)
  1883. {
  1884. enum map_type type;
  1885. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1886. struct kmap *kmap;
  1887. if (self->vmlinux_maps[type] == NULL)
  1888. continue;
  1889. kmap = map__kmap(self->vmlinux_maps[type]);
  1890. map_groups__remove(&self->kmaps, self->vmlinux_maps[type]);
  1891. if (kmap->ref_reloc_sym) {
  1892. /*
  1893. * ref_reloc_sym is shared among all maps, so free just
  1894. * on one of them.
  1895. */
  1896. if (type == MAP__FUNCTION) {
  1897. free((char *)kmap->ref_reloc_sym->name);
  1898. kmap->ref_reloc_sym->name = NULL;
  1899. free(kmap->ref_reloc_sym);
  1900. }
  1901. kmap->ref_reloc_sym = NULL;
  1902. }
  1903. map__delete(self->vmlinux_maps[type]);
  1904. self->vmlinux_maps[type] = NULL;
  1905. }
  1906. }
  1907. int machine__create_kernel_maps(struct machine *self)
  1908. {
  1909. struct dso *kernel = machine__create_kernel(self);
  1910. if (kernel == NULL ||
  1911. __machine__create_kernel_maps(self, kernel) < 0)
  1912. return -1;
  1913. if (symbol_conf.use_modules && machine__create_modules(self) < 0)
  1914. pr_debug("Problems creating module maps, continuing anyway...\n");
  1915. /*
  1916. * Now that we have all the maps created, just set the ->end of them:
  1917. */
  1918. map_groups__fixup_end(&self->kmaps);
  1919. return 0;
  1920. }
  1921. static void vmlinux_path__exit(void)
  1922. {
  1923. while (--vmlinux_path__nr_entries >= 0) {
  1924. free(vmlinux_path[vmlinux_path__nr_entries]);
  1925. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1926. }
  1927. free(vmlinux_path);
  1928. vmlinux_path = NULL;
  1929. }
  1930. static int vmlinux_path__init(void)
  1931. {
  1932. struct utsname uts;
  1933. char bf[PATH_MAX];
  1934. vmlinux_path = malloc(sizeof(char *) * 5);
  1935. if (vmlinux_path == NULL)
  1936. return -1;
  1937. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1938. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1939. goto out_fail;
  1940. ++vmlinux_path__nr_entries;
  1941. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1942. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1943. goto out_fail;
  1944. ++vmlinux_path__nr_entries;
  1945. /* only try running kernel version if no symfs was given */
  1946. if (symbol_conf.symfs[0] != 0)
  1947. return 0;
  1948. if (uname(&uts) < 0)
  1949. return -1;
  1950. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1951. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1952. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1953. goto out_fail;
  1954. ++vmlinux_path__nr_entries;
  1955. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1956. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1957. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1958. goto out_fail;
  1959. ++vmlinux_path__nr_entries;
  1960. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1961. uts.release);
  1962. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1963. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1964. goto out_fail;
  1965. ++vmlinux_path__nr_entries;
  1966. return 0;
  1967. out_fail:
  1968. vmlinux_path__exit();
  1969. return -1;
  1970. }
  1971. size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
  1972. {
  1973. int i;
  1974. size_t printed = 0;
  1975. struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
  1976. if (kdso->has_build_id) {
  1977. char filename[PATH_MAX];
  1978. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  1979. printed += fprintf(fp, "[0] %s\n", filename);
  1980. }
  1981. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  1982. printed += fprintf(fp, "[%d] %s\n",
  1983. i + kdso->has_build_id, vmlinux_path[i]);
  1984. return printed;
  1985. }
  1986. static int setup_list(struct strlist **list, const char *list_str,
  1987. const char *list_name)
  1988. {
  1989. if (list_str == NULL)
  1990. return 0;
  1991. *list = strlist__new(true, list_str);
  1992. if (!*list) {
  1993. pr_err("problems parsing %s list\n", list_name);
  1994. return -1;
  1995. }
  1996. return 0;
  1997. }
  1998. int symbol__init(void)
  1999. {
  2000. const char *symfs;
  2001. if (symbol_conf.initialized)
  2002. return 0;
  2003. symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
  2004. elf_version(EV_CURRENT);
  2005. if (symbol_conf.sort_by_name)
  2006. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  2007. sizeof(struct symbol));
  2008. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  2009. return -1;
  2010. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  2011. pr_err("'.' is the only non valid --field-separator argument\n");
  2012. return -1;
  2013. }
  2014. if (setup_list(&symbol_conf.dso_list,
  2015. symbol_conf.dso_list_str, "dso") < 0)
  2016. return -1;
  2017. if (setup_list(&symbol_conf.comm_list,
  2018. symbol_conf.comm_list_str, "comm") < 0)
  2019. goto out_free_dso_list;
  2020. if (setup_list(&symbol_conf.sym_list,
  2021. symbol_conf.sym_list_str, "symbol") < 0)
  2022. goto out_free_comm_list;
  2023. /*
  2024. * A path to symbols of "/" is identical to ""
  2025. * reset here for simplicity.
  2026. */
  2027. symfs = realpath(symbol_conf.symfs, NULL);
  2028. if (symfs == NULL)
  2029. symfs = symbol_conf.symfs;
  2030. if (strcmp(symfs, "/") == 0)
  2031. symbol_conf.symfs = "";
  2032. if (symfs != symbol_conf.symfs)
  2033. free((void *)symfs);
  2034. symbol_conf.initialized = true;
  2035. return 0;
  2036. out_free_dso_list:
  2037. strlist__delete(symbol_conf.dso_list);
  2038. out_free_comm_list:
  2039. strlist__delete(symbol_conf.comm_list);
  2040. return -1;
  2041. }
  2042. void symbol__exit(void)
  2043. {
  2044. if (!symbol_conf.initialized)
  2045. return;
  2046. strlist__delete(symbol_conf.sym_list);
  2047. strlist__delete(symbol_conf.dso_list);
  2048. strlist__delete(symbol_conf.comm_list);
  2049. vmlinux_path__exit();
  2050. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  2051. symbol_conf.initialized = false;
  2052. }
  2053. int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
  2054. {
  2055. struct machine *machine = machines__findnew(self, pid);
  2056. if (machine == NULL)
  2057. return -1;
  2058. return machine__create_kernel_maps(machine);
  2059. }
  2060. static int hex(char ch)
  2061. {
  2062. if ((ch >= '0') && (ch <= '9'))
  2063. return ch - '0';
  2064. if ((ch >= 'a') && (ch <= 'f'))
  2065. return ch - 'a' + 10;
  2066. if ((ch >= 'A') && (ch <= 'F'))
  2067. return ch - 'A' + 10;
  2068. return -1;
  2069. }
  2070. /*
  2071. * While we find nice hex chars, build a long_val.
  2072. * Return number of chars processed.
  2073. */
  2074. int hex2u64(const char *ptr, u64 *long_val)
  2075. {
  2076. const char *p = ptr;
  2077. *long_val = 0;
  2078. while (*p) {
  2079. const int hex_val = hex(*p);
  2080. if (hex_val < 0)
  2081. break;
  2082. *long_val = (*long_val << 4) | hex_val;
  2083. p++;
  2084. }
  2085. return p - ptr;
  2086. }
  2087. char *strxfrchar(char *s, char from, char to)
  2088. {
  2089. char *p = s;
  2090. while ((p = strchr(p, from)) != NULL)
  2091. *p++ = to;
  2092. return s;
  2093. }
  2094. int machines__create_guest_kernel_maps(struct rb_root *self)
  2095. {
  2096. int ret = 0;
  2097. struct dirent **namelist = NULL;
  2098. int i, items = 0;
  2099. char path[PATH_MAX];
  2100. pid_t pid;
  2101. if (symbol_conf.default_guest_vmlinux_name ||
  2102. symbol_conf.default_guest_modules ||
  2103. symbol_conf.default_guest_kallsyms) {
  2104. machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
  2105. }
  2106. if (symbol_conf.guestmount) {
  2107. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  2108. if (items <= 0)
  2109. return -ENOENT;
  2110. for (i = 0; i < items; i++) {
  2111. if (!isdigit(namelist[i]->d_name[0])) {
  2112. /* Filter out . and .. */
  2113. continue;
  2114. }
  2115. pid = atoi(namelist[i]->d_name);
  2116. sprintf(path, "%s/%s/proc/kallsyms",
  2117. symbol_conf.guestmount,
  2118. namelist[i]->d_name);
  2119. ret = access(path, R_OK);
  2120. if (ret) {
  2121. pr_debug("Can't access file %s\n", path);
  2122. goto failure;
  2123. }
  2124. machines__create_kernel_maps(self, pid);
  2125. }
  2126. failure:
  2127. free(namelist);
  2128. }
  2129. return ret;
  2130. }
  2131. void machines__destroy_guest_kernel_maps(struct rb_root *self)
  2132. {
  2133. struct rb_node *next = rb_first(self);
  2134. while (next) {
  2135. struct machine *pos = rb_entry(next, struct machine, rb_node);
  2136. next = rb_next(&pos->rb_node);
  2137. rb_erase(&pos->rb_node, self);
  2138. machine__delete(pos);
  2139. }
  2140. }
  2141. int machine__load_kallsyms(struct machine *self, const char *filename,
  2142. enum map_type type, symbol_filter_t filter)
  2143. {
  2144. struct map *map = self->vmlinux_maps[type];
  2145. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  2146. if (ret > 0) {
  2147. dso__set_loaded(map->dso, type);
  2148. /*
  2149. * Since /proc/kallsyms will have multiple sessions for the
  2150. * kernel, with modules between them, fixup the end of all
  2151. * sections.
  2152. */
  2153. __map_groups__fixup_end(&self->kmaps, type);
  2154. }
  2155. return ret;
  2156. }
  2157. int machine__load_vmlinux_path(struct machine *self, enum map_type type,
  2158. symbol_filter_t filter)
  2159. {
  2160. struct map *map = self->vmlinux_maps[type];
  2161. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2162. if (ret > 0) {
  2163. dso__set_loaded(map->dso, type);
  2164. map__reloc_vmlinux(map);
  2165. }
  2166. return ret;
  2167. }