symbol.c 71 KB

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