symbol.c 52 KB

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