symbol.c 58 KB

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