symbol.c 52 KB

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