symbol.c 63 KB

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