symbol.c 57 KB

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