symbol.c 59 KB

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