symbol.c 56 KB

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