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 dso__synthesize_plt_symbols(struct dso *dso, struct map *map,
  808. symbol_filter_t filter)
  809. {
  810. uint32_t nr_rel_entries, idx;
  811. GElf_Sym sym;
  812. u64 plt_offset;
  813. GElf_Shdr shdr_plt;
  814. struct symbol *f;
  815. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  816. Elf_Data *reldata, *syms, *symstrs;
  817. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  818. size_t dynsym_idx;
  819. GElf_Ehdr ehdr;
  820. char sympltname[1024];
  821. Elf *elf;
  822. int nr = 0, symidx, fd, err = 0;
  823. char name[PATH_MAX];
  824. snprintf(name, sizeof(name), "%s%s",
  825. symbol_conf.symfs, dso->long_name);
  826. fd = open(name, O_RDONLY);
  827. if (fd < 0)
  828. goto out;
  829. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  830. if (elf == NULL)
  831. goto out_close;
  832. if (gelf_getehdr(elf, &ehdr) == NULL)
  833. goto out_elf_end;
  834. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  835. ".dynsym", &dynsym_idx);
  836. if (scn_dynsym == NULL)
  837. goto out_elf_end;
  838. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  839. ".rela.plt", NULL);
  840. if (scn_plt_rel == NULL) {
  841. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  842. ".rel.plt", NULL);
  843. if (scn_plt_rel == NULL)
  844. goto out_elf_end;
  845. }
  846. err = -1;
  847. if (shdr_rel_plt.sh_link != dynsym_idx)
  848. goto out_elf_end;
  849. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  850. goto out_elf_end;
  851. /*
  852. * Fetch the relocation section to find the idxes to the GOT
  853. * and the symbols in the .dynsym they refer to.
  854. */
  855. reldata = elf_getdata(scn_plt_rel, NULL);
  856. if (reldata == NULL)
  857. goto out_elf_end;
  858. syms = elf_getdata(scn_dynsym, NULL);
  859. if (syms == NULL)
  860. goto out_elf_end;
  861. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  862. if (scn_symstrs == NULL)
  863. goto out_elf_end;
  864. symstrs = elf_getdata(scn_symstrs, NULL);
  865. if (symstrs == NULL)
  866. goto out_elf_end;
  867. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  868. plt_offset = shdr_plt.sh_offset;
  869. if (shdr_rel_plt.sh_type == SHT_RELA) {
  870. GElf_Rela pos_mem, *pos;
  871. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  872. nr_rel_entries) {
  873. symidx = GELF_R_SYM(pos->r_info);
  874. plt_offset += shdr_plt.sh_entsize;
  875. gelf_getsym(syms, symidx, &sym);
  876. snprintf(sympltname, sizeof(sympltname),
  877. "%s@plt", elf_sym__name(&sym, symstrs));
  878. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  879. STB_GLOBAL, sympltname);
  880. if (!f)
  881. goto out_elf_end;
  882. if (filter && filter(map, f))
  883. symbol__delete(f);
  884. else {
  885. symbols__insert(&dso->symbols[map->type], f);
  886. ++nr;
  887. }
  888. }
  889. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  890. GElf_Rel pos_mem, *pos;
  891. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  892. nr_rel_entries) {
  893. symidx = GELF_R_SYM(pos->r_info);
  894. plt_offset += shdr_plt.sh_entsize;
  895. gelf_getsym(syms, symidx, &sym);
  896. snprintf(sympltname, sizeof(sympltname),
  897. "%s@plt", elf_sym__name(&sym, symstrs));
  898. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  899. STB_GLOBAL, sympltname);
  900. if (!f)
  901. goto out_elf_end;
  902. if (filter && filter(map, f))
  903. symbol__delete(f);
  904. else {
  905. symbols__insert(&dso->symbols[map->type], f);
  906. ++nr;
  907. }
  908. }
  909. }
  910. err = 0;
  911. out_elf_end:
  912. elf_end(elf);
  913. out_close:
  914. close(fd);
  915. if (err == 0)
  916. return nr;
  917. out:
  918. pr_debug("%s: problems reading %s PLT info.\n",
  919. __func__, dso->long_name);
  920. return 0;
  921. }
  922. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  923. {
  924. switch (type) {
  925. case MAP__FUNCTION:
  926. return elf_sym__is_function(sym);
  927. case MAP__VARIABLE:
  928. return elf_sym__is_object(sym);
  929. default:
  930. return false;
  931. }
  932. }
  933. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  934. enum map_type type)
  935. {
  936. switch (type) {
  937. case MAP__FUNCTION:
  938. return elf_sec__is_text(shdr, secstrs);
  939. case MAP__VARIABLE:
  940. return elf_sec__is_data(shdr, secstrs);
  941. default:
  942. return false;
  943. }
  944. }
  945. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  946. {
  947. Elf_Scn *sec = NULL;
  948. GElf_Shdr shdr;
  949. size_t cnt = 1;
  950. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  951. gelf_getshdr(sec, &shdr);
  952. if ((addr >= shdr.sh_addr) &&
  953. (addr < (shdr.sh_addr + shdr.sh_size)))
  954. return cnt;
  955. ++cnt;
  956. }
  957. return -1;
  958. }
  959. static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
  960. int fd, symbol_filter_t filter, int kmodule,
  961. int want_symtab)
  962. {
  963. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  964. struct map *curr_map = map;
  965. struct dso *curr_dso = dso;
  966. Elf_Data *symstrs, *secstrs;
  967. uint32_t nr_syms;
  968. int err = -1;
  969. uint32_t idx;
  970. GElf_Ehdr ehdr;
  971. GElf_Shdr shdr, opdshdr;
  972. Elf_Data *syms, *opddata = NULL;
  973. GElf_Sym sym;
  974. Elf_Scn *sec, *sec_strndx, *opdsec;
  975. Elf *elf;
  976. int nr = 0;
  977. size_t opdidx = 0;
  978. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  979. if (elf == NULL) {
  980. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  981. goto out_close;
  982. }
  983. if (gelf_getehdr(elf, &ehdr) == NULL) {
  984. pr_debug("%s: cannot get elf header.\n", __func__);
  985. goto out_elf_end;
  986. }
  987. /* Always reject images with a mismatched build-id: */
  988. if (dso->has_build_id) {
  989. u8 build_id[BUILD_ID_SIZE];
  990. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  991. goto out_elf_end;
  992. if (!dso__build_id_equal(dso, build_id))
  993. goto out_elf_end;
  994. }
  995. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  996. if (sec == NULL) {
  997. if (want_symtab)
  998. goto out_elf_end;
  999. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  1000. if (sec == NULL)
  1001. goto out_elf_end;
  1002. }
  1003. opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
  1004. if (opdshdr.sh_type != SHT_PROGBITS)
  1005. opdsec = NULL;
  1006. if (opdsec)
  1007. opddata = elf_rawdata(opdsec, NULL);
  1008. syms = elf_getdata(sec, NULL);
  1009. if (syms == NULL)
  1010. goto out_elf_end;
  1011. sec = elf_getscn(elf, shdr.sh_link);
  1012. if (sec == NULL)
  1013. goto out_elf_end;
  1014. symstrs = elf_getdata(sec, NULL);
  1015. if (symstrs == NULL)
  1016. goto out_elf_end;
  1017. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  1018. if (sec_strndx == NULL)
  1019. goto out_elf_end;
  1020. secstrs = elf_getdata(sec_strndx, NULL);
  1021. if (secstrs == NULL)
  1022. goto out_elf_end;
  1023. nr_syms = shdr.sh_size / shdr.sh_entsize;
  1024. memset(&sym, 0, sizeof(sym));
  1025. if (dso->kernel == DSO_TYPE_USER) {
  1026. dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  1027. elf_section_by_name(elf, &ehdr, &shdr,
  1028. ".gnu.prelink_undo",
  1029. NULL) != NULL);
  1030. } else {
  1031. dso->adjust_symbols = 0;
  1032. }
  1033. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  1034. struct symbol *f;
  1035. const char *elf_name = elf_sym__name(&sym, symstrs);
  1036. char *demangled = NULL;
  1037. int is_label = elf_sym__is_label(&sym);
  1038. const char *section_name;
  1039. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  1040. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  1041. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  1042. if (!is_label && !elf_sym__is_a(&sym, map->type))
  1043. continue;
  1044. /* Reject ARM ELF "mapping symbols": these aren't unique and
  1045. * don't identify functions, so will confuse the profile
  1046. * output: */
  1047. if (ehdr.e_machine == EM_ARM) {
  1048. if (!strcmp(elf_name, "$a") ||
  1049. !strcmp(elf_name, "$d") ||
  1050. !strcmp(elf_name, "$t"))
  1051. continue;
  1052. }
  1053. if (opdsec && sym.st_shndx == opdidx) {
  1054. u32 offset = sym.st_value - opdshdr.sh_addr;
  1055. u64 *opd = opddata->d_buf + offset;
  1056. sym.st_value = *opd;
  1057. sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
  1058. }
  1059. sec = elf_getscn(elf, sym.st_shndx);
  1060. if (!sec)
  1061. goto out_elf_end;
  1062. gelf_getshdr(sec, &shdr);
  1063. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  1064. continue;
  1065. section_name = elf_sec__name(&shdr, secstrs);
  1066. /* On ARM, symbols for thumb functions have 1 added to
  1067. * the symbol address as a flag - remove it */
  1068. if ((ehdr.e_machine == EM_ARM) &&
  1069. (map->type == MAP__FUNCTION) &&
  1070. (sym.st_value & 1))
  1071. --sym.st_value;
  1072. if (dso->kernel != DSO_TYPE_USER || kmodule) {
  1073. char dso_name[PATH_MAX];
  1074. if (strcmp(section_name,
  1075. (curr_dso->short_name +
  1076. dso->short_name_len)) == 0)
  1077. goto new_symbol;
  1078. if (strcmp(section_name, ".text") == 0) {
  1079. curr_map = map;
  1080. curr_dso = dso;
  1081. goto new_symbol;
  1082. }
  1083. snprintf(dso_name, sizeof(dso_name),
  1084. "%s%s", dso->short_name, section_name);
  1085. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  1086. if (curr_map == NULL) {
  1087. u64 start = sym.st_value;
  1088. if (kmodule)
  1089. start += map->start + shdr.sh_offset;
  1090. curr_dso = dso__new(dso_name);
  1091. if (curr_dso == NULL)
  1092. goto out_elf_end;
  1093. curr_dso->kernel = dso->kernel;
  1094. curr_dso->long_name = dso->long_name;
  1095. curr_dso->long_name_len = dso->long_name_len;
  1096. curr_map = map__new2(start, curr_dso,
  1097. map->type);
  1098. if (curr_map == NULL) {
  1099. dso__delete(curr_dso);
  1100. goto out_elf_end;
  1101. }
  1102. curr_map->map_ip = identity__map_ip;
  1103. curr_map->unmap_ip = identity__map_ip;
  1104. curr_dso->symtab_type = dso->symtab_type;
  1105. map_groups__insert(kmap->kmaps, curr_map);
  1106. dsos__add(&dso->node, curr_dso);
  1107. dso__set_loaded(curr_dso, map->type);
  1108. } else
  1109. curr_dso = curr_map->dso;
  1110. goto new_symbol;
  1111. }
  1112. if (curr_dso->adjust_symbols) {
  1113. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  1114. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  1115. (u64)sym.st_value, (u64)shdr.sh_addr,
  1116. (u64)shdr.sh_offset);
  1117. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  1118. }
  1119. /*
  1120. * We need to figure out if the object was created from C++ sources
  1121. * DWARF DW_compile_unit has this, but we don't always have access
  1122. * to it...
  1123. */
  1124. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  1125. if (demangled != NULL)
  1126. elf_name = demangled;
  1127. new_symbol:
  1128. f = symbol__new(sym.st_value, sym.st_size,
  1129. GELF_ST_BIND(sym.st_info), elf_name);
  1130. free(demangled);
  1131. if (!f)
  1132. goto out_elf_end;
  1133. if (filter && filter(curr_map, f))
  1134. symbol__delete(f);
  1135. else {
  1136. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  1137. nr++;
  1138. }
  1139. }
  1140. /*
  1141. * For misannotated, zeroed, ASM function sizes.
  1142. */
  1143. if (nr > 0) {
  1144. symbols__fixup_duplicate(&dso->symbols[map->type]);
  1145. symbols__fixup_end(&dso->symbols[map->type]);
  1146. if (kmap) {
  1147. /*
  1148. * We need to fixup this here too because we create new
  1149. * maps here, for things like vsyscall sections.
  1150. */
  1151. __map_groups__fixup_end(kmap->kmaps, map->type);
  1152. }
  1153. }
  1154. err = nr;
  1155. out_elf_end:
  1156. elf_end(elf);
  1157. out_close:
  1158. return err;
  1159. }
  1160. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  1161. {
  1162. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  1163. }
  1164. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  1165. {
  1166. bool have_build_id = false;
  1167. struct dso *pos;
  1168. list_for_each_entry(pos, head, node) {
  1169. if (with_hits && !pos->hit)
  1170. continue;
  1171. if (pos->has_build_id) {
  1172. have_build_id = true;
  1173. continue;
  1174. }
  1175. if (filename__read_build_id(pos->long_name, pos->build_id,
  1176. sizeof(pos->build_id)) > 0) {
  1177. have_build_id = true;
  1178. pos->has_build_id = true;
  1179. }
  1180. }
  1181. return have_build_id;
  1182. }
  1183. /*
  1184. * Align offset to 4 bytes as needed for note name and descriptor data.
  1185. */
  1186. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  1187. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  1188. {
  1189. int err = -1;
  1190. GElf_Ehdr ehdr;
  1191. GElf_Shdr shdr;
  1192. Elf_Data *data;
  1193. Elf_Scn *sec;
  1194. Elf_Kind ek;
  1195. void *ptr;
  1196. if (size < BUILD_ID_SIZE)
  1197. goto out;
  1198. ek = elf_kind(elf);
  1199. if (ek != ELF_K_ELF)
  1200. goto out;
  1201. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1202. pr_err("%s: cannot get elf header.\n", __func__);
  1203. goto out;
  1204. }
  1205. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1206. ".note.gnu.build-id", NULL);
  1207. if (sec == NULL) {
  1208. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1209. ".notes", NULL);
  1210. if (sec == NULL)
  1211. goto out;
  1212. }
  1213. data = elf_getdata(sec, NULL);
  1214. if (data == NULL)
  1215. goto out;
  1216. ptr = data->d_buf;
  1217. while (ptr < (data->d_buf + data->d_size)) {
  1218. GElf_Nhdr *nhdr = ptr;
  1219. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  1220. descsz = NOTE_ALIGN(nhdr->n_descsz);
  1221. const char *name;
  1222. ptr += sizeof(*nhdr);
  1223. name = ptr;
  1224. ptr += namesz;
  1225. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1226. nhdr->n_namesz == sizeof("GNU")) {
  1227. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1228. size_t sz = min(size, descsz);
  1229. memcpy(bf, ptr, sz);
  1230. memset(bf + sz, 0, size - sz);
  1231. err = descsz;
  1232. break;
  1233. }
  1234. }
  1235. ptr += descsz;
  1236. }
  1237. out:
  1238. return err;
  1239. }
  1240. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1241. {
  1242. int fd, err = -1;
  1243. Elf *elf;
  1244. if (size < BUILD_ID_SIZE)
  1245. goto out;
  1246. fd = open(filename, O_RDONLY);
  1247. if (fd < 0)
  1248. goto out;
  1249. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1250. if (elf == NULL) {
  1251. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1252. goto out_close;
  1253. }
  1254. err = elf_read_build_id(elf, bf, size);
  1255. elf_end(elf);
  1256. out_close:
  1257. close(fd);
  1258. out:
  1259. return err;
  1260. }
  1261. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1262. {
  1263. int fd, err = -1;
  1264. if (size < BUILD_ID_SIZE)
  1265. goto out;
  1266. fd = open(filename, O_RDONLY);
  1267. if (fd < 0)
  1268. goto out;
  1269. while (1) {
  1270. char bf[BUFSIZ];
  1271. GElf_Nhdr nhdr;
  1272. size_t namesz, descsz;
  1273. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1274. break;
  1275. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1276. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1277. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1278. nhdr.n_namesz == sizeof("GNU")) {
  1279. if (read(fd, bf, namesz) != (ssize_t)namesz)
  1280. break;
  1281. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1282. size_t sz = min(descsz, size);
  1283. if (read(fd, build_id, sz) == (ssize_t)sz) {
  1284. memset(build_id + sz, 0, size - sz);
  1285. err = 0;
  1286. break;
  1287. }
  1288. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  1289. break;
  1290. } else {
  1291. int n = namesz + descsz;
  1292. if (read(fd, bf, n) != n)
  1293. break;
  1294. }
  1295. }
  1296. close(fd);
  1297. out:
  1298. return err;
  1299. }
  1300. char dso__symtab_origin(const struct dso *dso)
  1301. {
  1302. static const char origin[] = {
  1303. [SYMTAB__KALLSYMS] = 'k',
  1304. [SYMTAB__JAVA_JIT] = 'j',
  1305. [SYMTAB__BUILD_ID_CACHE] = 'B',
  1306. [SYMTAB__FEDORA_DEBUGINFO] = 'f',
  1307. [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
  1308. [SYMTAB__BUILDID_DEBUGINFO] = 'b',
  1309. [SYMTAB__SYSTEM_PATH_DSO] = 'd',
  1310. [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
  1311. [SYMTAB__GUEST_KALLSYMS] = 'g',
  1312. [SYMTAB__GUEST_KMODULE] = 'G',
  1313. };
  1314. if (dso == NULL || dso->symtab_type == SYMTAB__NOT_FOUND)
  1315. return '!';
  1316. return origin[dso->symtab_type];
  1317. }
  1318. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1319. {
  1320. int size = PATH_MAX;
  1321. char *name;
  1322. int ret = -1;
  1323. int fd;
  1324. struct machine *machine;
  1325. const char *root_dir;
  1326. int want_symtab;
  1327. dso__set_loaded(dso, map->type);
  1328. if (dso->kernel == DSO_TYPE_KERNEL)
  1329. return dso__load_kernel_sym(dso, map, filter);
  1330. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1331. return dso__load_guest_kernel_sym(dso, map, filter);
  1332. if (map->groups && map->groups->machine)
  1333. machine = map->groups->machine;
  1334. else
  1335. machine = NULL;
  1336. name = malloc(size);
  1337. if (!name)
  1338. return -1;
  1339. dso->adjust_symbols = 0;
  1340. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1341. struct stat st;
  1342. if (lstat(dso->name, &st) < 0)
  1343. return -1;
  1344. if (st.st_uid && (st.st_uid != geteuid())) {
  1345. pr_warning("File %s not owned by current user or root, "
  1346. "ignoring it.\n", dso->name);
  1347. return -1;
  1348. }
  1349. ret = dso__load_perf_map(dso, map, filter);
  1350. dso->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
  1351. SYMTAB__NOT_FOUND;
  1352. return ret;
  1353. }
  1354. /* Iterate over candidate debug images.
  1355. * On the first pass, only load images if they have a full symtab.
  1356. * Failing that, do a second pass where we accept .dynsym also
  1357. */
  1358. want_symtab = 1;
  1359. restart:
  1360. for (dso->symtab_type = SYMTAB__BUILD_ID_CACHE;
  1361. dso->symtab_type != SYMTAB__NOT_FOUND;
  1362. dso->symtab_type++) {
  1363. switch (dso->symtab_type) {
  1364. case SYMTAB__BUILD_ID_CACHE:
  1365. /* skip the locally configured cache if a symfs is given */
  1366. if (symbol_conf.symfs[0] ||
  1367. (dso__build_id_filename(dso, name, size) == NULL)) {
  1368. continue;
  1369. }
  1370. break;
  1371. case SYMTAB__FEDORA_DEBUGINFO:
  1372. snprintf(name, size, "%s/usr/lib/debug%s.debug",
  1373. symbol_conf.symfs, dso->long_name);
  1374. break;
  1375. case SYMTAB__UBUNTU_DEBUGINFO:
  1376. snprintf(name, size, "%s/usr/lib/debug%s",
  1377. symbol_conf.symfs, dso->long_name);
  1378. break;
  1379. case SYMTAB__BUILDID_DEBUGINFO: {
  1380. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1381. if (!dso->has_build_id)
  1382. continue;
  1383. build_id__sprintf(dso->build_id,
  1384. sizeof(dso->build_id),
  1385. build_id_hex);
  1386. snprintf(name, size,
  1387. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  1388. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  1389. }
  1390. break;
  1391. case SYMTAB__SYSTEM_PATH_DSO:
  1392. snprintf(name, size, "%s%s",
  1393. symbol_conf.symfs, dso->long_name);
  1394. break;
  1395. case SYMTAB__GUEST_KMODULE:
  1396. if (map->groups && machine)
  1397. root_dir = machine->root_dir;
  1398. else
  1399. root_dir = "";
  1400. snprintf(name, size, "%s%s%s", symbol_conf.symfs,
  1401. root_dir, dso->long_name);
  1402. break;
  1403. case SYMTAB__SYSTEM_PATH_KMODULE:
  1404. snprintf(name, size, "%s%s", symbol_conf.symfs,
  1405. dso->long_name);
  1406. break;
  1407. default:;
  1408. }
  1409. /* Name is now the name of the next image to try */
  1410. fd = open(name, O_RDONLY);
  1411. if (fd < 0)
  1412. continue;
  1413. ret = dso__load_sym(dso, map, name, fd, filter, 0,
  1414. want_symtab);
  1415. close(fd);
  1416. /*
  1417. * Some people seem to have debuginfo files _WITHOUT_ debug
  1418. * info!?!?
  1419. */
  1420. if (!ret)
  1421. continue;
  1422. if (ret > 0) {
  1423. int nr_plt = dso__synthesize_plt_symbols(dso, map,
  1424. filter);
  1425. if (nr_plt > 0)
  1426. ret += nr_plt;
  1427. break;
  1428. }
  1429. }
  1430. /*
  1431. * If we wanted a full symtab but no image had one,
  1432. * relax our requirements and repeat the search.
  1433. */
  1434. if (ret <= 0 && want_symtab) {
  1435. want_symtab = 0;
  1436. goto restart;
  1437. }
  1438. free(name);
  1439. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1440. return 0;
  1441. return ret;
  1442. }
  1443. struct map *map_groups__find_by_name(struct map_groups *mg,
  1444. enum map_type type, const char *name)
  1445. {
  1446. struct rb_node *nd;
  1447. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1448. struct map *map = rb_entry(nd, struct map, rb_node);
  1449. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1450. return map;
  1451. }
  1452. return NULL;
  1453. }
  1454. static int dso__kernel_module_get_build_id(struct dso *dso,
  1455. const char *root_dir)
  1456. {
  1457. char filename[PATH_MAX];
  1458. /*
  1459. * kernel module short names are of the form "[module]" and
  1460. * we need just "module" here.
  1461. */
  1462. const char *name = dso->short_name + 1;
  1463. snprintf(filename, sizeof(filename),
  1464. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1465. root_dir, (int)strlen(name) - 1, name);
  1466. if (sysfs__read_build_id(filename, dso->build_id,
  1467. sizeof(dso->build_id)) == 0)
  1468. dso->has_build_id = true;
  1469. return 0;
  1470. }
  1471. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  1472. const char *dir_name)
  1473. {
  1474. struct dirent *dent;
  1475. DIR *dir = opendir(dir_name);
  1476. int ret = 0;
  1477. if (!dir) {
  1478. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1479. return -1;
  1480. }
  1481. while ((dent = readdir(dir)) != NULL) {
  1482. char path[PATH_MAX];
  1483. struct stat st;
  1484. /*sshfs might return bad dent->d_type, so we have to stat*/
  1485. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  1486. if (stat(path, &st))
  1487. continue;
  1488. if (S_ISDIR(st.st_mode)) {
  1489. if (!strcmp(dent->d_name, ".") ||
  1490. !strcmp(dent->d_name, ".."))
  1491. continue;
  1492. ret = map_groups__set_modules_path_dir(mg, path);
  1493. if (ret < 0)
  1494. goto out;
  1495. } else {
  1496. char *dot = strrchr(dent->d_name, '.'),
  1497. dso_name[PATH_MAX];
  1498. struct map *map;
  1499. char *long_name;
  1500. if (dot == NULL || strcmp(dot, ".ko"))
  1501. continue;
  1502. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1503. (int)(dot - dent->d_name), dent->d_name);
  1504. strxfrchar(dso_name, '-', '_');
  1505. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  1506. dso_name);
  1507. if (map == NULL)
  1508. continue;
  1509. long_name = strdup(path);
  1510. if (long_name == NULL) {
  1511. ret = -1;
  1512. goto out;
  1513. }
  1514. dso__set_long_name(map->dso, long_name);
  1515. map->dso->lname_alloc = 1;
  1516. dso__kernel_module_get_build_id(map->dso, "");
  1517. }
  1518. }
  1519. out:
  1520. closedir(dir);
  1521. return ret;
  1522. }
  1523. static char *get_kernel_version(const char *root_dir)
  1524. {
  1525. char version[PATH_MAX];
  1526. FILE *file;
  1527. char *name, *tmp;
  1528. const char *prefix = "Linux version ";
  1529. sprintf(version, "%s/proc/version", root_dir);
  1530. file = fopen(version, "r");
  1531. if (!file)
  1532. return NULL;
  1533. version[0] = '\0';
  1534. tmp = fgets(version, sizeof(version), file);
  1535. fclose(file);
  1536. name = strstr(version, prefix);
  1537. if (!name)
  1538. return NULL;
  1539. name += strlen(prefix);
  1540. tmp = strchr(name, ' ');
  1541. if (tmp)
  1542. *tmp = '\0';
  1543. return strdup(name);
  1544. }
  1545. static int machine__set_modules_path(struct machine *machine)
  1546. {
  1547. char *version;
  1548. char modules_path[PATH_MAX];
  1549. version = get_kernel_version(machine->root_dir);
  1550. if (!version)
  1551. return -1;
  1552. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1553. machine->root_dir, version);
  1554. free(version);
  1555. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  1556. }
  1557. /*
  1558. * Constructor variant for modules (where we know from /proc/modules where
  1559. * they are loaded) and for vmlinux, where only after we load all the
  1560. * symbols we'll know where it starts and ends.
  1561. */
  1562. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1563. {
  1564. struct map *map = calloc(1, (sizeof(*map) +
  1565. (dso->kernel ? sizeof(struct kmap) : 0)));
  1566. if (map != NULL) {
  1567. /*
  1568. * ->end will be filled after we load all the symbols
  1569. */
  1570. map__init(map, type, start, 0, 0, dso);
  1571. }
  1572. return map;
  1573. }
  1574. struct map *machine__new_module(struct machine *machine, u64 start,
  1575. const char *filename)
  1576. {
  1577. struct map *map;
  1578. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  1579. if (dso == NULL)
  1580. return NULL;
  1581. map = map__new2(start, dso, MAP__FUNCTION);
  1582. if (map == NULL)
  1583. return NULL;
  1584. if (machine__is_host(machine))
  1585. dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
  1586. else
  1587. dso->symtab_type = SYMTAB__GUEST_KMODULE;
  1588. map_groups__insert(&machine->kmaps, map);
  1589. return map;
  1590. }
  1591. static int machine__create_modules(struct machine *machine)
  1592. {
  1593. char *line = NULL;
  1594. size_t n;
  1595. FILE *file;
  1596. struct map *map;
  1597. const char *modules;
  1598. char path[PATH_MAX];
  1599. if (machine__is_default_guest(machine))
  1600. modules = symbol_conf.default_guest_modules;
  1601. else {
  1602. sprintf(path, "%s/proc/modules", machine->root_dir);
  1603. modules = path;
  1604. }
  1605. if (symbol__restricted_filename(path, "/proc/modules"))
  1606. return -1;
  1607. file = fopen(modules, "r");
  1608. if (file == NULL)
  1609. return -1;
  1610. while (!feof(file)) {
  1611. char name[PATH_MAX];
  1612. u64 start;
  1613. char *sep;
  1614. int line_len;
  1615. line_len = getline(&line, &n, file);
  1616. if (line_len < 0)
  1617. break;
  1618. if (!line)
  1619. goto out_failure;
  1620. line[--line_len] = '\0'; /* \n */
  1621. sep = strrchr(line, 'x');
  1622. if (sep == NULL)
  1623. continue;
  1624. hex2u64(sep + 1, &start);
  1625. sep = strchr(line, ' ');
  1626. if (sep == NULL)
  1627. continue;
  1628. *sep = '\0';
  1629. snprintf(name, sizeof(name), "[%s]", line);
  1630. map = machine__new_module(machine, start, name);
  1631. if (map == NULL)
  1632. goto out_delete_line;
  1633. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1634. }
  1635. free(line);
  1636. fclose(file);
  1637. return machine__set_modules_path(machine);
  1638. out_delete_line:
  1639. free(line);
  1640. out_failure:
  1641. return -1;
  1642. }
  1643. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1644. const char *vmlinux, symbol_filter_t filter)
  1645. {
  1646. int err = -1, fd;
  1647. char symfs_vmlinux[PATH_MAX];
  1648. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1649. symbol_conf.symfs, vmlinux);
  1650. fd = open(symfs_vmlinux, O_RDONLY);
  1651. if (fd < 0)
  1652. return -1;
  1653. dso__set_long_name(dso, (char *)vmlinux);
  1654. dso__set_loaded(dso, map->type);
  1655. err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
  1656. close(fd);
  1657. if (err > 0)
  1658. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1659. return err;
  1660. }
  1661. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1662. symbol_filter_t filter)
  1663. {
  1664. int i, err = 0;
  1665. char *filename;
  1666. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1667. vmlinux_path__nr_entries + 1);
  1668. filename = dso__build_id_filename(dso, NULL, 0);
  1669. if (filename != NULL) {
  1670. err = dso__load_vmlinux(dso, map, filename, filter);
  1671. if (err > 0) {
  1672. dso__set_long_name(dso, filename);
  1673. goto out;
  1674. }
  1675. free(filename);
  1676. }
  1677. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1678. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1679. if (err > 0) {
  1680. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1681. break;
  1682. }
  1683. }
  1684. out:
  1685. return err;
  1686. }
  1687. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1688. symbol_filter_t filter)
  1689. {
  1690. int err;
  1691. const char *kallsyms_filename = NULL;
  1692. char *kallsyms_allocated_filename = NULL;
  1693. /*
  1694. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1695. * it and only it, reporting errors to the user if it cannot be used.
  1696. *
  1697. * For instance, try to analyse an ARM perf.data file _without_ a
  1698. * build-id, or if the user specifies the wrong path to the right
  1699. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1700. * x86_86 one, on the machine where analysis is being performed, say),
  1701. * or worse, /proc/kallsyms.
  1702. *
  1703. * If the specified file _has_ a build-id and there is a build-id
  1704. * section in the perf.data file, we will still do the expected
  1705. * validation in dso__load_vmlinux and will bail out if they don't
  1706. * match.
  1707. */
  1708. if (symbol_conf.kallsyms_name != NULL) {
  1709. kallsyms_filename = symbol_conf.kallsyms_name;
  1710. goto do_kallsyms;
  1711. }
  1712. if (symbol_conf.vmlinux_name != NULL) {
  1713. err = dso__load_vmlinux(dso, map,
  1714. symbol_conf.vmlinux_name, filter);
  1715. if (err > 0) {
  1716. dso__set_long_name(dso,
  1717. strdup(symbol_conf.vmlinux_name));
  1718. goto out_fixup;
  1719. }
  1720. return err;
  1721. }
  1722. if (vmlinux_path != NULL) {
  1723. err = dso__load_vmlinux_path(dso, map, filter);
  1724. if (err > 0)
  1725. goto out_fixup;
  1726. }
  1727. /* do not try local files if a symfs was given */
  1728. if (symbol_conf.symfs[0] != 0)
  1729. return -1;
  1730. /*
  1731. * Say the kernel DSO was created when processing the build-id header table,
  1732. * we have a build-id, so check if it is the same as the running kernel,
  1733. * using it if it is.
  1734. */
  1735. if (dso->has_build_id) {
  1736. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1737. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1738. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1739. sizeof(kallsyms_build_id)) == 0) {
  1740. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1741. kallsyms_filename = "/proc/kallsyms";
  1742. goto do_kallsyms;
  1743. }
  1744. }
  1745. /*
  1746. * Now look if we have it on the build-id cache in
  1747. * $HOME/.debug/[kernel.kallsyms].
  1748. */
  1749. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1750. sbuild_id);
  1751. if (asprintf(&kallsyms_allocated_filename,
  1752. "%s/.debug/[kernel.kallsyms]/%s",
  1753. getenv("HOME"), sbuild_id) == -1) {
  1754. pr_err("Not enough memory for kallsyms file lookup\n");
  1755. return -1;
  1756. }
  1757. kallsyms_filename = kallsyms_allocated_filename;
  1758. if (access(kallsyms_filename, F_OK)) {
  1759. pr_err("No kallsyms or vmlinux with build-id %s "
  1760. "was found\n", sbuild_id);
  1761. free(kallsyms_allocated_filename);
  1762. return -1;
  1763. }
  1764. } else {
  1765. /*
  1766. * Last resort, if we don't have a build-id and couldn't find
  1767. * any vmlinux file, try the running kernel kallsyms table.
  1768. */
  1769. kallsyms_filename = "/proc/kallsyms";
  1770. }
  1771. do_kallsyms:
  1772. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1773. if (err > 0)
  1774. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1775. free(kallsyms_allocated_filename);
  1776. if (err > 0) {
  1777. out_fixup:
  1778. if (kallsyms_filename != NULL)
  1779. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1780. map__fixup_start(map);
  1781. map__fixup_end(map);
  1782. }
  1783. return err;
  1784. }
  1785. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1786. symbol_filter_t filter)
  1787. {
  1788. int err;
  1789. const char *kallsyms_filename = NULL;
  1790. struct machine *machine;
  1791. char path[PATH_MAX];
  1792. if (!map->groups) {
  1793. pr_debug("Guest kernel map hasn't the point to groups\n");
  1794. return -1;
  1795. }
  1796. machine = map->groups->machine;
  1797. if (machine__is_default_guest(machine)) {
  1798. /*
  1799. * if the user specified a vmlinux filename, use it and only
  1800. * it, reporting errors to the user if it cannot be used.
  1801. * Or use file guest_kallsyms inputted by user on commandline
  1802. */
  1803. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1804. err = dso__load_vmlinux(dso, map,
  1805. symbol_conf.default_guest_vmlinux_name, filter);
  1806. goto out_try_fixup;
  1807. }
  1808. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1809. if (!kallsyms_filename)
  1810. return -1;
  1811. } else {
  1812. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1813. kallsyms_filename = path;
  1814. }
  1815. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1816. if (err > 0)
  1817. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1818. out_try_fixup:
  1819. if (err > 0) {
  1820. if (kallsyms_filename != NULL) {
  1821. machine__mmap_name(machine, path, sizeof(path));
  1822. dso__set_long_name(dso, strdup(path));
  1823. }
  1824. map__fixup_start(map);
  1825. map__fixup_end(map);
  1826. }
  1827. return err;
  1828. }
  1829. static void dsos__add(struct list_head *head, struct dso *dso)
  1830. {
  1831. list_add_tail(&dso->node, head);
  1832. }
  1833. static struct dso *dsos__find(struct list_head *head, const char *name)
  1834. {
  1835. struct dso *pos;
  1836. list_for_each_entry(pos, head, node)
  1837. if (strcmp(pos->long_name, name) == 0)
  1838. return pos;
  1839. return NULL;
  1840. }
  1841. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1842. {
  1843. struct dso *dso = dsos__find(head, name);
  1844. if (!dso) {
  1845. dso = dso__new(name);
  1846. if (dso != NULL) {
  1847. dsos__add(head, dso);
  1848. dso__set_basename(dso);
  1849. }
  1850. }
  1851. return dso;
  1852. }
  1853. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1854. {
  1855. struct dso *pos;
  1856. size_t ret = 0;
  1857. list_for_each_entry(pos, head, node) {
  1858. int i;
  1859. for (i = 0; i < MAP__NR_TYPES; ++i)
  1860. ret += dso__fprintf(pos, i, fp);
  1861. }
  1862. return ret;
  1863. }
  1864. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
  1865. {
  1866. struct rb_node *nd;
  1867. size_t ret = 0;
  1868. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1869. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1870. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1871. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1872. }
  1873. return ret;
  1874. }
  1875. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1876. bool with_hits)
  1877. {
  1878. struct dso *pos;
  1879. size_t ret = 0;
  1880. list_for_each_entry(pos, head, node) {
  1881. if (with_hits && !pos->hit)
  1882. continue;
  1883. ret += dso__fprintf_buildid(pos, fp);
  1884. ret += fprintf(fp, " %s\n", pos->long_name);
  1885. }
  1886. return ret;
  1887. }
  1888. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  1889. bool with_hits)
  1890. {
  1891. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
  1892. __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
  1893. }
  1894. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  1895. FILE *fp, bool with_hits)
  1896. {
  1897. struct rb_node *nd;
  1898. size_t ret = 0;
  1899. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1900. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1901. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1902. }
  1903. return ret;
  1904. }
  1905. static struct dso*
  1906. dso__kernel_findnew(struct machine *machine, const char *name,
  1907. const char *short_name, int dso_type)
  1908. {
  1909. /*
  1910. * The kernel dso could be created by build_id processing.
  1911. */
  1912. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  1913. /*
  1914. * We need to run this in all cases, since during the build_id
  1915. * processing we had no idea this was the kernel dso.
  1916. */
  1917. if (dso != NULL) {
  1918. dso__set_short_name(dso, short_name);
  1919. dso->kernel = dso_type;
  1920. }
  1921. return dso;
  1922. }
  1923. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1924. {
  1925. char path[PATH_MAX];
  1926. if (machine__is_default_guest(machine))
  1927. return;
  1928. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1929. if (sysfs__read_build_id(path, dso->build_id,
  1930. sizeof(dso->build_id)) == 0)
  1931. dso->has_build_id = true;
  1932. }
  1933. static struct dso *machine__get_kernel(struct machine *machine)
  1934. {
  1935. const char *vmlinux_name = NULL;
  1936. struct dso *kernel;
  1937. if (machine__is_host(machine)) {
  1938. vmlinux_name = symbol_conf.vmlinux_name;
  1939. if (!vmlinux_name)
  1940. vmlinux_name = "[kernel.kallsyms]";
  1941. kernel = dso__kernel_findnew(machine, vmlinux_name,
  1942. "[kernel]",
  1943. DSO_TYPE_KERNEL);
  1944. } else {
  1945. char bf[PATH_MAX];
  1946. if (machine__is_default_guest(machine))
  1947. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1948. if (!vmlinux_name)
  1949. vmlinux_name = machine__mmap_name(machine, bf,
  1950. sizeof(bf));
  1951. kernel = dso__kernel_findnew(machine, vmlinux_name,
  1952. "[guest.kernel]",
  1953. DSO_TYPE_GUEST_KERNEL);
  1954. }
  1955. if (kernel != NULL && (!kernel->has_build_id))
  1956. dso__read_running_kernel_build_id(kernel, machine);
  1957. return kernel;
  1958. }
  1959. struct process_args {
  1960. u64 start;
  1961. };
  1962. static int symbol__in_kernel(void *arg, const char *name,
  1963. char type __used, u64 start, u64 end __used)
  1964. {
  1965. struct process_args *args = arg;
  1966. if (strchr(name, '['))
  1967. return 0;
  1968. args->start = start;
  1969. return 1;
  1970. }
  1971. /* Figure out the start address of kernel map from /proc/kallsyms */
  1972. static u64 machine__get_kernel_start_addr(struct machine *machine)
  1973. {
  1974. const char *filename;
  1975. char path[PATH_MAX];
  1976. struct process_args args;
  1977. if (machine__is_host(machine)) {
  1978. filename = "/proc/kallsyms";
  1979. } else {
  1980. if (machine__is_default_guest(machine))
  1981. filename = (char *)symbol_conf.default_guest_kallsyms;
  1982. else {
  1983. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1984. filename = path;
  1985. }
  1986. }
  1987. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1988. return 0;
  1989. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  1990. return 0;
  1991. return args.start;
  1992. }
  1993. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  1994. {
  1995. enum map_type type;
  1996. u64 start = machine__get_kernel_start_addr(machine);
  1997. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1998. struct kmap *kmap;
  1999. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  2000. if (machine->vmlinux_maps[type] == NULL)
  2001. return -1;
  2002. machine->vmlinux_maps[type]->map_ip =
  2003. machine->vmlinux_maps[type]->unmap_ip =
  2004. identity__map_ip;
  2005. kmap = map__kmap(machine->vmlinux_maps[type]);
  2006. kmap->kmaps = &machine->kmaps;
  2007. map_groups__insert(&machine->kmaps,
  2008. machine->vmlinux_maps[type]);
  2009. }
  2010. return 0;
  2011. }
  2012. void machine__destroy_kernel_maps(struct machine *machine)
  2013. {
  2014. enum map_type type;
  2015. for (type = 0; type < MAP__NR_TYPES; ++type) {
  2016. struct kmap *kmap;
  2017. if (machine->vmlinux_maps[type] == NULL)
  2018. continue;
  2019. kmap = map__kmap(machine->vmlinux_maps[type]);
  2020. map_groups__remove(&machine->kmaps,
  2021. machine->vmlinux_maps[type]);
  2022. if (kmap->ref_reloc_sym) {
  2023. /*
  2024. * ref_reloc_sym is shared among all maps, so free just
  2025. * on one of them.
  2026. */
  2027. if (type == MAP__FUNCTION) {
  2028. free((char *)kmap->ref_reloc_sym->name);
  2029. kmap->ref_reloc_sym->name = NULL;
  2030. free(kmap->ref_reloc_sym);
  2031. }
  2032. kmap->ref_reloc_sym = NULL;
  2033. }
  2034. map__delete(machine->vmlinux_maps[type]);
  2035. machine->vmlinux_maps[type] = NULL;
  2036. }
  2037. }
  2038. int machine__create_kernel_maps(struct machine *machine)
  2039. {
  2040. struct dso *kernel = machine__get_kernel(machine);
  2041. if (kernel == NULL ||
  2042. __machine__create_kernel_maps(machine, kernel) < 0)
  2043. return -1;
  2044. if (symbol_conf.use_modules && machine__create_modules(machine) < 0)
  2045. pr_debug("Problems creating module maps, continuing anyway...\n");
  2046. /*
  2047. * Now that we have all the maps created, just set the ->end of them:
  2048. */
  2049. map_groups__fixup_end(&machine->kmaps);
  2050. return 0;
  2051. }
  2052. static void vmlinux_path__exit(void)
  2053. {
  2054. while (--vmlinux_path__nr_entries >= 0) {
  2055. free(vmlinux_path[vmlinux_path__nr_entries]);
  2056. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  2057. }
  2058. free(vmlinux_path);
  2059. vmlinux_path = NULL;
  2060. }
  2061. static int vmlinux_path__init(void)
  2062. {
  2063. struct utsname uts;
  2064. char bf[PATH_MAX];
  2065. vmlinux_path = malloc(sizeof(char *) * 5);
  2066. if (vmlinux_path == NULL)
  2067. return -1;
  2068. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  2069. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2070. goto out_fail;
  2071. ++vmlinux_path__nr_entries;
  2072. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  2073. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2074. goto out_fail;
  2075. ++vmlinux_path__nr_entries;
  2076. /* only try running kernel version if no symfs was given */
  2077. if (symbol_conf.symfs[0] != 0)
  2078. return 0;
  2079. if (uname(&uts) < 0)
  2080. return -1;
  2081. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  2082. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2083. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2084. goto out_fail;
  2085. ++vmlinux_path__nr_entries;
  2086. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  2087. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2088. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2089. goto out_fail;
  2090. ++vmlinux_path__nr_entries;
  2091. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  2092. uts.release);
  2093. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2094. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2095. goto out_fail;
  2096. ++vmlinux_path__nr_entries;
  2097. return 0;
  2098. out_fail:
  2099. vmlinux_path__exit();
  2100. return -1;
  2101. }
  2102. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  2103. {
  2104. int i;
  2105. size_t printed = 0;
  2106. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  2107. if (kdso->has_build_id) {
  2108. char filename[PATH_MAX];
  2109. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  2110. printed += fprintf(fp, "[0] %s\n", filename);
  2111. }
  2112. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  2113. printed += fprintf(fp, "[%d] %s\n",
  2114. i + kdso->has_build_id, vmlinux_path[i]);
  2115. return printed;
  2116. }
  2117. static int setup_list(struct strlist **list, const char *list_str,
  2118. const char *list_name)
  2119. {
  2120. if (list_str == NULL)
  2121. return 0;
  2122. *list = strlist__new(true, list_str);
  2123. if (!*list) {
  2124. pr_err("problems parsing %s list\n", list_name);
  2125. return -1;
  2126. }
  2127. return 0;
  2128. }
  2129. static bool symbol__read_kptr_restrict(void)
  2130. {
  2131. bool value = false;
  2132. if (geteuid() != 0) {
  2133. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  2134. if (fp != NULL) {
  2135. char line[8];
  2136. if (fgets(line, sizeof(line), fp) != NULL)
  2137. value = atoi(line) != 0;
  2138. fclose(fp);
  2139. }
  2140. }
  2141. return value;
  2142. }
  2143. int symbol__init(void)
  2144. {
  2145. const char *symfs;
  2146. if (symbol_conf.initialized)
  2147. return 0;
  2148. symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
  2149. elf_version(EV_CURRENT);
  2150. if (symbol_conf.sort_by_name)
  2151. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  2152. sizeof(struct symbol));
  2153. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  2154. return -1;
  2155. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  2156. pr_err("'.' is the only non valid --field-separator argument\n");
  2157. return -1;
  2158. }
  2159. if (setup_list(&symbol_conf.dso_list,
  2160. symbol_conf.dso_list_str, "dso") < 0)
  2161. return -1;
  2162. if (setup_list(&symbol_conf.comm_list,
  2163. symbol_conf.comm_list_str, "comm") < 0)
  2164. goto out_free_dso_list;
  2165. if (setup_list(&symbol_conf.sym_list,
  2166. symbol_conf.sym_list_str, "symbol") < 0)
  2167. goto out_free_comm_list;
  2168. /*
  2169. * A path to symbols of "/" is identical to ""
  2170. * reset here for simplicity.
  2171. */
  2172. symfs = realpath(symbol_conf.symfs, NULL);
  2173. if (symfs == NULL)
  2174. symfs = symbol_conf.symfs;
  2175. if (strcmp(symfs, "/") == 0)
  2176. symbol_conf.symfs = "";
  2177. if (symfs != symbol_conf.symfs)
  2178. free((void *)symfs);
  2179. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  2180. symbol_conf.initialized = true;
  2181. return 0;
  2182. out_free_comm_list:
  2183. strlist__delete(symbol_conf.comm_list);
  2184. out_free_dso_list:
  2185. strlist__delete(symbol_conf.dso_list);
  2186. return -1;
  2187. }
  2188. void symbol__exit(void)
  2189. {
  2190. if (!symbol_conf.initialized)
  2191. return;
  2192. strlist__delete(symbol_conf.sym_list);
  2193. strlist__delete(symbol_conf.dso_list);
  2194. strlist__delete(symbol_conf.comm_list);
  2195. vmlinux_path__exit();
  2196. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  2197. symbol_conf.initialized = false;
  2198. }
  2199. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
  2200. {
  2201. struct machine *machine = machines__findnew(machines, pid);
  2202. if (machine == NULL)
  2203. return -1;
  2204. return machine__create_kernel_maps(machine);
  2205. }
  2206. static int hex(char ch)
  2207. {
  2208. if ((ch >= '0') && (ch <= '9'))
  2209. return ch - '0';
  2210. if ((ch >= 'a') && (ch <= 'f'))
  2211. return ch - 'a' + 10;
  2212. if ((ch >= 'A') && (ch <= 'F'))
  2213. return ch - 'A' + 10;
  2214. return -1;
  2215. }
  2216. /*
  2217. * While we find nice hex chars, build a long_val.
  2218. * Return number of chars processed.
  2219. */
  2220. int hex2u64(const char *ptr, u64 *long_val)
  2221. {
  2222. const char *p = ptr;
  2223. *long_val = 0;
  2224. while (*p) {
  2225. const int hex_val = hex(*p);
  2226. if (hex_val < 0)
  2227. break;
  2228. *long_val = (*long_val << 4) | hex_val;
  2229. p++;
  2230. }
  2231. return p - ptr;
  2232. }
  2233. char *strxfrchar(char *s, char from, char to)
  2234. {
  2235. char *p = s;
  2236. while ((p = strchr(p, from)) != NULL)
  2237. *p++ = to;
  2238. return s;
  2239. }
  2240. int machines__create_guest_kernel_maps(struct rb_root *machines)
  2241. {
  2242. int ret = 0;
  2243. struct dirent **namelist = NULL;
  2244. int i, items = 0;
  2245. char path[PATH_MAX];
  2246. pid_t pid;
  2247. if (symbol_conf.default_guest_vmlinux_name ||
  2248. symbol_conf.default_guest_modules ||
  2249. symbol_conf.default_guest_kallsyms) {
  2250. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  2251. }
  2252. if (symbol_conf.guestmount) {
  2253. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  2254. if (items <= 0)
  2255. return -ENOENT;
  2256. for (i = 0; i < items; i++) {
  2257. if (!isdigit(namelist[i]->d_name[0])) {
  2258. /* Filter out . and .. */
  2259. continue;
  2260. }
  2261. pid = atoi(namelist[i]->d_name);
  2262. sprintf(path, "%s/%s/proc/kallsyms",
  2263. symbol_conf.guestmount,
  2264. namelist[i]->d_name);
  2265. ret = access(path, R_OK);
  2266. if (ret) {
  2267. pr_debug("Can't access file %s\n", path);
  2268. goto failure;
  2269. }
  2270. machines__create_kernel_maps(machines, pid);
  2271. }
  2272. failure:
  2273. free(namelist);
  2274. }
  2275. return ret;
  2276. }
  2277. void machines__destroy_guest_kernel_maps(struct rb_root *machines)
  2278. {
  2279. struct rb_node *next = rb_first(machines);
  2280. while (next) {
  2281. struct machine *pos = rb_entry(next, struct machine, rb_node);
  2282. next = rb_next(&pos->rb_node);
  2283. rb_erase(&pos->rb_node, machines);
  2284. machine__delete(pos);
  2285. }
  2286. }
  2287. int machine__load_kallsyms(struct machine *machine, const char *filename,
  2288. enum map_type type, symbol_filter_t filter)
  2289. {
  2290. struct map *map = machine->vmlinux_maps[type];
  2291. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  2292. if (ret > 0) {
  2293. dso__set_loaded(map->dso, type);
  2294. /*
  2295. * Since /proc/kallsyms will have multiple sessions for the
  2296. * kernel, with modules between them, fixup the end of all
  2297. * sections.
  2298. */
  2299. __map_groups__fixup_end(&machine->kmaps, type);
  2300. }
  2301. return ret;
  2302. }
  2303. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  2304. symbol_filter_t filter)
  2305. {
  2306. struct map *map = machine->vmlinux_maps[type];
  2307. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2308. if (ret > 0) {
  2309. dso__set_loaded(map->dso, type);
  2310. map__reloc_vmlinux(map);
  2311. }
  2312. return ret;
  2313. }