symbol.c 61 KB

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