symbol.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <linux/kernel.h>
  8. #include <linux/mman.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 "annotate.h"
  16. #include "build-id.h"
  17. #include "util.h"
  18. #include "debug.h"
  19. #include "machine.h"
  20. #include "symbol.h"
  21. #include "strlist.h"
  22. #include "intlist.h"
  23. #include "namespaces.h"
  24. #include "header.h"
  25. #include "path.h"
  26. #include "sane_ctype.h"
  27. #include <elf.h>
  28. #include <limits.h>
  29. #include <symbol/kallsyms.h>
  30. #include <sys/utsname.h>
  31. static int dso__load_kernel_sym(struct dso *dso, struct map *map);
  32. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
  33. static bool symbol__is_idle(const char *name);
  34. int vmlinux_path__nr_entries;
  35. char **vmlinux_path;
  36. struct symbol_conf symbol_conf = {
  37. .use_modules = true,
  38. .try_vmlinux_path = true,
  39. .demangle = true,
  40. .demangle_kernel = false,
  41. .cumulate_callchain = true,
  42. .show_hist_headers = true,
  43. .symfs = "",
  44. .event_group = true,
  45. .inline_name = true,
  46. };
  47. static enum dso_binary_type binary_type_symtab[] = {
  48. DSO_BINARY_TYPE__KALLSYMS,
  49. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  50. DSO_BINARY_TYPE__JAVA_JIT,
  51. DSO_BINARY_TYPE__DEBUGLINK,
  52. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  53. DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
  54. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  55. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  56. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  57. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  58. DSO_BINARY_TYPE__GUEST_KMODULE,
  59. DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
  60. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  61. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
  62. DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  63. DSO_BINARY_TYPE__NOT_FOUND,
  64. };
  65. #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  66. static bool symbol_type__filter(char symbol_type)
  67. {
  68. symbol_type = toupper(symbol_type);
  69. return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B';
  70. }
  71. static int prefix_underscores_count(const char *str)
  72. {
  73. const char *tail = str;
  74. while (*tail == '_')
  75. tail++;
  76. return tail - str;
  77. }
  78. void __weak arch__symbols__fixup_end(struct symbol *p, struct symbol *c)
  79. {
  80. p->end = c->start;
  81. }
  82. const char * __weak arch__normalize_symbol_name(const char *name)
  83. {
  84. return name;
  85. }
  86. int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
  87. {
  88. return strcmp(namea, nameb);
  89. }
  90. int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
  91. unsigned int n)
  92. {
  93. return strncmp(namea, nameb, n);
  94. }
  95. int __weak arch__choose_best_symbol(struct symbol *syma,
  96. struct symbol *symb __maybe_unused)
  97. {
  98. /* Avoid "SyS" kernel syscall aliases */
  99. if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
  100. return SYMBOL_B;
  101. if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
  102. return SYMBOL_B;
  103. return SYMBOL_A;
  104. }
  105. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  106. {
  107. s64 a;
  108. s64 b;
  109. size_t na, nb;
  110. /* Prefer a symbol with non zero length */
  111. a = syma->end - syma->start;
  112. b = symb->end - symb->start;
  113. if ((b == 0) && (a > 0))
  114. return SYMBOL_A;
  115. else if ((a == 0) && (b > 0))
  116. return SYMBOL_B;
  117. /* Prefer a non weak symbol over a weak one */
  118. a = syma->binding == STB_WEAK;
  119. b = symb->binding == STB_WEAK;
  120. if (b && !a)
  121. return SYMBOL_A;
  122. if (a && !b)
  123. return SYMBOL_B;
  124. /* Prefer a global symbol over a non global one */
  125. a = syma->binding == STB_GLOBAL;
  126. b = symb->binding == STB_GLOBAL;
  127. if (a && !b)
  128. return SYMBOL_A;
  129. if (b && !a)
  130. return SYMBOL_B;
  131. /* Prefer a symbol with less underscores */
  132. a = prefix_underscores_count(syma->name);
  133. b = prefix_underscores_count(symb->name);
  134. if (b > a)
  135. return SYMBOL_A;
  136. else if (a > b)
  137. return SYMBOL_B;
  138. /* Choose the symbol with the longest name */
  139. na = strlen(syma->name);
  140. nb = strlen(symb->name);
  141. if (na > nb)
  142. return SYMBOL_A;
  143. else if (na < nb)
  144. return SYMBOL_B;
  145. return arch__choose_best_symbol(syma, symb);
  146. }
  147. void symbols__fixup_duplicate(struct rb_root *symbols)
  148. {
  149. struct rb_node *nd;
  150. struct symbol *curr, *next;
  151. if (symbol_conf.allow_aliases)
  152. return;
  153. nd = rb_first(symbols);
  154. while (nd) {
  155. curr = rb_entry(nd, struct symbol, rb_node);
  156. again:
  157. nd = rb_next(&curr->rb_node);
  158. next = rb_entry(nd, struct symbol, rb_node);
  159. if (!nd)
  160. break;
  161. if (curr->start != next->start)
  162. continue;
  163. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  164. rb_erase(&next->rb_node, symbols);
  165. symbol__delete(next);
  166. goto again;
  167. } else {
  168. nd = rb_next(&curr->rb_node);
  169. rb_erase(&curr->rb_node, symbols);
  170. symbol__delete(curr);
  171. }
  172. }
  173. }
  174. void symbols__fixup_end(struct rb_root *symbols)
  175. {
  176. struct rb_node *nd, *prevnd = rb_first(symbols);
  177. struct symbol *curr, *prev;
  178. if (prevnd == NULL)
  179. return;
  180. curr = rb_entry(prevnd, struct symbol, rb_node);
  181. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  182. prev = curr;
  183. curr = rb_entry(nd, struct symbol, rb_node);
  184. if (prev->end == prev->start && prev->end != curr->start)
  185. arch__symbols__fixup_end(prev, curr);
  186. }
  187. /* Last entry */
  188. if (curr->end == curr->start)
  189. curr->end = roundup(curr->start, 4096) + 4096;
  190. }
  191. void map_groups__fixup_end(struct map_groups *mg)
  192. {
  193. struct maps *maps = &mg->maps;
  194. struct map *next, *curr;
  195. down_write(&maps->lock);
  196. curr = maps__first(maps);
  197. if (curr == NULL)
  198. goto out_unlock;
  199. for (next = map__next(curr); next; next = map__next(curr)) {
  200. if (!curr->end)
  201. curr->end = next->start;
  202. curr = next;
  203. }
  204. /*
  205. * We still haven't the actual symbols, so guess the
  206. * last map final address.
  207. */
  208. if (!curr->end)
  209. curr->end = ~0ULL;
  210. out_unlock:
  211. up_write(&maps->lock);
  212. }
  213. struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
  214. {
  215. size_t namelen = strlen(name) + 1;
  216. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  217. sizeof(*sym) + namelen));
  218. if (sym == NULL)
  219. return NULL;
  220. if (symbol_conf.priv_size) {
  221. if (symbol_conf.init_annotation) {
  222. struct annotation *notes = (void *)sym;
  223. pthread_mutex_init(&notes->lock, NULL);
  224. }
  225. sym = ((void *)sym) + symbol_conf.priv_size;
  226. }
  227. sym->start = start;
  228. sym->end = len ? start + len : start;
  229. sym->type = type;
  230. sym->binding = binding;
  231. sym->namelen = namelen - 1;
  232. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  233. __func__, name, start, sym->end);
  234. memcpy(sym->name, name, namelen);
  235. return sym;
  236. }
  237. void symbol__delete(struct symbol *sym)
  238. {
  239. free(((void *)sym) - symbol_conf.priv_size);
  240. }
  241. void symbols__delete(struct rb_root *symbols)
  242. {
  243. struct symbol *pos;
  244. struct rb_node *next = rb_first(symbols);
  245. while (next) {
  246. pos = rb_entry(next, struct symbol, rb_node);
  247. next = rb_next(&pos->rb_node);
  248. rb_erase(&pos->rb_node, symbols);
  249. symbol__delete(pos);
  250. }
  251. }
  252. void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
  253. {
  254. struct rb_node **p = &symbols->rb_node;
  255. struct rb_node *parent = NULL;
  256. const u64 ip = sym->start;
  257. struct symbol *s;
  258. if (kernel) {
  259. const char *name = sym->name;
  260. /*
  261. * ppc64 uses function descriptors and appends a '.' to the
  262. * start of every instruction address. Remove it.
  263. */
  264. if (name[0] == '.')
  265. name++;
  266. sym->idle = symbol__is_idle(name);
  267. }
  268. while (*p != NULL) {
  269. parent = *p;
  270. s = rb_entry(parent, struct symbol, rb_node);
  271. if (ip < s->start)
  272. p = &(*p)->rb_left;
  273. else
  274. p = &(*p)->rb_right;
  275. }
  276. rb_link_node(&sym->rb_node, parent, p);
  277. rb_insert_color(&sym->rb_node, symbols);
  278. }
  279. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  280. {
  281. __symbols__insert(symbols, sym, false);
  282. }
  283. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  284. {
  285. struct rb_node *n;
  286. if (symbols == NULL)
  287. return NULL;
  288. n = symbols->rb_node;
  289. while (n) {
  290. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  291. if (ip < s->start)
  292. n = n->rb_left;
  293. else if (ip > s->end || (ip == s->end && ip != s->start))
  294. n = n->rb_right;
  295. else
  296. return s;
  297. }
  298. return NULL;
  299. }
  300. static struct symbol *symbols__first(struct rb_root *symbols)
  301. {
  302. struct rb_node *n = rb_first(symbols);
  303. if (n)
  304. return rb_entry(n, struct symbol, rb_node);
  305. return NULL;
  306. }
  307. static struct symbol *symbols__last(struct rb_root *symbols)
  308. {
  309. struct rb_node *n = rb_last(symbols);
  310. if (n)
  311. return rb_entry(n, struct symbol, rb_node);
  312. return NULL;
  313. }
  314. static struct symbol *symbols__next(struct symbol *sym)
  315. {
  316. struct rb_node *n = rb_next(&sym->rb_node);
  317. if (n)
  318. return rb_entry(n, struct symbol, rb_node);
  319. return NULL;
  320. }
  321. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  322. {
  323. struct rb_node **p = &symbols->rb_node;
  324. struct rb_node *parent = NULL;
  325. struct symbol_name_rb_node *symn, *s;
  326. symn = container_of(sym, struct symbol_name_rb_node, sym);
  327. while (*p != NULL) {
  328. parent = *p;
  329. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  330. if (strcmp(sym->name, s->sym.name) < 0)
  331. p = &(*p)->rb_left;
  332. else
  333. p = &(*p)->rb_right;
  334. }
  335. rb_link_node(&symn->rb_node, parent, p);
  336. rb_insert_color(&symn->rb_node, symbols);
  337. }
  338. static void symbols__sort_by_name(struct rb_root *symbols,
  339. struct rb_root *source)
  340. {
  341. struct rb_node *nd;
  342. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  343. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  344. symbols__insert_by_name(symbols, pos);
  345. }
  346. }
  347. int symbol__match_symbol_name(const char *name, const char *str,
  348. enum symbol_tag_include includes)
  349. {
  350. const char *versioning;
  351. if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
  352. (versioning = strstr(name, "@@"))) {
  353. int len = strlen(str);
  354. if (len < versioning - name)
  355. len = versioning - name;
  356. return arch__compare_symbol_names_n(name, str, len);
  357. } else
  358. return arch__compare_symbol_names(name, str);
  359. }
  360. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  361. const char *name,
  362. enum symbol_tag_include includes)
  363. {
  364. struct rb_node *n;
  365. struct symbol_name_rb_node *s = NULL;
  366. if (symbols == NULL)
  367. return NULL;
  368. n = symbols->rb_node;
  369. while (n) {
  370. int cmp;
  371. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  372. cmp = symbol__match_symbol_name(s->sym.name, name, includes);
  373. if (cmp > 0)
  374. n = n->rb_left;
  375. else if (cmp < 0)
  376. n = n->rb_right;
  377. else
  378. break;
  379. }
  380. if (n == NULL)
  381. return NULL;
  382. if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
  383. /* return first symbol that has same name (if any) */
  384. for (n = rb_prev(n); n; n = rb_prev(n)) {
  385. struct symbol_name_rb_node *tmp;
  386. tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
  387. if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
  388. break;
  389. s = tmp;
  390. }
  391. return &s->sym;
  392. }
  393. void dso__reset_find_symbol_cache(struct dso *dso)
  394. {
  395. dso->last_find_result.addr = 0;
  396. dso->last_find_result.symbol = NULL;
  397. }
  398. void dso__insert_symbol(struct dso *dso, struct symbol *sym)
  399. {
  400. __symbols__insert(&dso->symbols, sym, dso->kernel);
  401. /* update the symbol cache if necessary */
  402. if (dso->last_find_result.addr >= sym->start &&
  403. (dso->last_find_result.addr < sym->end ||
  404. sym->start == sym->end)) {
  405. dso->last_find_result.symbol = sym;
  406. }
  407. }
  408. struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
  409. {
  410. if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) {
  411. dso->last_find_result.addr = addr;
  412. dso->last_find_result.symbol = symbols__find(&dso->symbols, addr);
  413. }
  414. return dso->last_find_result.symbol;
  415. }
  416. struct symbol *dso__first_symbol(struct dso *dso)
  417. {
  418. return symbols__first(&dso->symbols);
  419. }
  420. struct symbol *dso__last_symbol(struct dso *dso)
  421. {
  422. return symbols__last(&dso->symbols);
  423. }
  424. struct symbol *dso__next_symbol(struct symbol *sym)
  425. {
  426. return symbols__next(sym);
  427. }
  428. struct symbol *symbol__next_by_name(struct symbol *sym)
  429. {
  430. struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
  431. struct rb_node *n = rb_next(&s->rb_node);
  432. return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
  433. }
  434. /*
  435. * Returns first symbol that matched with @name.
  436. */
  437. struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name)
  438. {
  439. struct symbol *s = symbols__find_by_name(&dso->symbol_names, name,
  440. SYMBOL_TAG_INCLUDE__NONE);
  441. if (!s)
  442. s = symbols__find_by_name(&dso->symbol_names, name,
  443. SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
  444. return s;
  445. }
  446. void dso__sort_by_name(struct dso *dso)
  447. {
  448. dso__set_sorted_by_name(dso);
  449. return symbols__sort_by_name(&dso->symbol_names, &dso->symbols);
  450. }
  451. int modules__parse(const char *filename, void *arg,
  452. int (*process_module)(void *arg, const char *name,
  453. u64 start, u64 size))
  454. {
  455. char *line = NULL;
  456. size_t n;
  457. FILE *file;
  458. int err = 0;
  459. file = fopen(filename, "r");
  460. if (file == NULL)
  461. return -1;
  462. while (1) {
  463. char name[PATH_MAX];
  464. u64 start, size;
  465. char *sep, *endptr;
  466. ssize_t line_len;
  467. line_len = getline(&line, &n, file);
  468. if (line_len < 0) {
  469. if (feof(file))
  470. break;
  471. err = -1;
  472. goto out;
  473. }
  474. if (!line) {
  475. err = -1;
  476. goto out;
  477. }
  478. line[--line_len] = '\0'; /* \n */
  479. sep = strrchr(line, 'x');
  480. if (sep == NULL)
  481. continue;
  482. hex2u64(sep + 1, &start);
  483. sep = strchr(line, ' ');
  484. if (sep == NULL)
  485. continue;
  486. *sep = '\0';
  487. scnprintf(name, sizeof(name), "[%s]", line);
  488. size = strtoul(sep + 1, &endptr, 0);
  489. if (*endptr != ' ' && *endptr != '\t')
  490. continue;
  491. err = process_module(arg, name, start, size);
  492. if (err)
  493. break;
  494. }
  495. out:
  496. free(line);
  497. fclose(file);
  498. return err;
  499. }
  500. /*
  501. * These are symbols in the kernel image, so make sure that
  502. * sym is from a kernel DSO.
  503. */
  504. static bool symbol__is_idle(const char *name)
  505. {
  506. const char * const idle_symbols[] = {
  507. "cpu_idle",
  508. "cpu_startup_entry",
  509. "intel_idle",
  510. "default_idle",
  511. "native_safe_halt",
  512. "enter_idle",
  513. "exit_idle",
  514. "mwait_idle",
  515. "mwait_idle_with_hints",
  516. "poll_idle",
  517. "ppc64_runlatch_off",
  518. "pseries_dedicated_idle_sleep",
  519. NULL
  520. };
  521. int i;
  522. for (i = 0; idle_symbols[i]; i++) {
  523. if (!strcmp(idle_symbols[i], name))
  524. return true;
  525. }
  526. return false;
  527. }
  528. static int map__process_kallsym_symbol(void *arg, const char *name,
  529. char type, u64 start)
  530. {
  531. struct symbol *sym;
  532. struct dso *dso = arg;
  533. struct rb_root *root = &dso->symbols;
  534. if (!symbol_type__filter(type))
  535. return 0;
  536. /*
  537. * module symbols are not sorted so we add all
  538. * symbols, setting length to 0, and rely on
  539. * symbols__fixup_end() to fix it up.
  540. */
  541. sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
  542. if (sym == NULL)
  543. return -ENOMEM;
  544. /*
  545. * We will pass the symbols to the filter later, in
  546. * map__split_kallsyms, when we have split the maps per module
  547. */
  548. __symbols__insert(root, sym, !strchr(name, '['));
  549. return 0;
  550. }
  551. /*
  552. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  553. * so that we can in the next step set the symbol ->end address and then
  554. * call kernel_maps__split_kallsyms.
  555. */
  556. static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
  557. {
  558. return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
  559. }
  560. static int map_groups__split_kallsyms_for_kcore(struct map_groups *kmaps, struct dso *dso)
  561. {
  562. struct map *curr_map;
  563. struct symbol *pos;
  564. int count = 0;
  565. struct rb_root old_root = dso->symbols;
  566. struct rb_root *root = &dso->symbols;
  567. struct rb_node *next = rb_first(root);
  568. if (!kmaps)
  569. return -1;
  570. *root = RB_ROOT;
  571. while (next) {
  572. char *module;
  573. pos = rb_entry(next, struct symbol, rb_node);
  574. next = rb_next(&pos->rb_node);
  575. rb_erase_init(&pos->rb_node, &old_root);
  576. module = strchr(pos->name, '\t');
  577. if (module)
  578. *module = '\0';
  579. curr_map = map_groups__find(kmaps, pos->start);
  580. if (!curr_map) {
  581. symbol__delete(pos);
  582. continue;
  583. }
  584. pos->start -= curr_map->start - curr_map->pgoff;
  585. if (pos->end > curr_map->end)
  586. pos->end = curr_map->end;
  587. if (pos->end)
  588. pos->end -= curr_map->start - curr_map->pgoff;
  589. symbols__insert(&curr_map->dso->symbols, pos);
  590. ++count;
  591. }
  592. /* Symbols have been adjusted */
  593. dso->adjust_symbols = 1;
  594. return count;
  595. }
  596. /*
  597. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  598. * kernel range is broken in several maps, named [kernel].N, as we don't have
  599. * the original ELF section names vmlinux have.
  600. */
  601. static int map_groups__split_kallsyms(struct map_groups *kmaps, struct dso *dso, u64 delta,
  602. struct map *initial_map)
  603. {
  604. struct machine *machine;
  605. struct map *curr_map = initial_map;
  606. struct symbol *pos;
  607. int count = 0, moved = 0;
  608. struct rb_root *root = &dso->symbols;
  609. struct rb_node *next = rb_first(root);
  610. int kernel_range = 0;
  611. bool x86_64;
  612. if (!kmaps)
  613. return -1;
  614. machine = kmaps->machine;
  615. x86_64 = machine__is(machine, "x86_64");
  616. while (next) {
  617. char *module;
  618. pos = rb_entry(next, struct symbol, rb_node);
  619. next = rb_next(&pos->rb_node);
  620. module = strchr(pos->name, '\t');
  621. if (module) {
  622. if (!symbol_conf.use_modules)
  623. goto discard_symbol;
  624. *module++ = '\0';
  625. if (strcmp(curr_map->dso->short_name, module)) {
  626. if (curr_map != initial_map &&
  627. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  628. machine__is_default_guest(machine)) {
  629. /*
  630. * We assume all symbols of a module are
  631. * continuous in * kallsyms, so curr_map
  632. * points to a module and all its
  633. * symbols are in its kmap. Mark it as
  634. * loaded.
  635. */
  636. dso__set_loaded(curr_map->dso);
  637. }
  638. curr_map = map_groups__find_by_name(kmaps, module);
  639. if (curr_map == NULL) {
  640. pr_debug("%s/proc/{kallsyms,modules} "
  641. "inconsistency while looking "
  642. "for \"%s\" module!\n",
  643. machine->root_dir, module);
  644. curr_map = initial_map;
  645. goto discard_symbol;
  646. }
  647. if (curr_map->dso->loaded &&
  648. !machine__is_default_guest(machine))
  649. goto discard_symbol;
  650. }
  651. /*
  652. * So that we look just like we get from .ko files,
  653. * i.e. not prelinked, relative to initial_map->start.
  654. */
  655. pos->start = curr_map->map_ip(curr_map, pos->start);
  656. pos->end = curr_map->map_ip(curr_map, pos->end);
  657. } else if (x86_64 && is_entry_trampoline(pos->name)) {
  658. /*
  659. * These symbols are not needed anymore since the
  660. * trampoline maps refer to the text section and it's
  661. * symbols instead. Avoid having to deal with
  662. * relocations, and the assumption that the first symbol
  663. * is the start of kernel text, by simply removing the
  664. * symbols at this point.
  665. */
  666. goto discard_symbol;
  667. } else if (curr_map != initial_map) {
  668. char dso_name[PATH_MAX];
  669. struct dso *ndso;
  670. if (delta) {
  671. /* Kernel was relocated at boot time */
  672. pos->start -= delta;
  673. pos->end -= delta;
  674. }
  675. if (count == 0) {
  676. curr_map = initial_map;
  677. goto add_symbol;
  678. }
  679. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  680. snprintf(dso_name, sizeof(dso_name),
  681. "[guest.kernel].%d",
  682. kernel_range++);
  683. else
  684. snprintf(dso_name, sizeof(dso_name),
  685. "[kernel].%d",
  686. kernel_range++);
  687. ndso = dso__new(dso_name);
  688. if (ndso == NULL)
  689. return -1;
  690. ndso->kernel = dso->kernel;
  691. curr_map = map__new2(pos->start, ndso);
  692. if (curr_map == NULL) {
  693. dso__put(ndso);
  694. return -1;
  695. }
  696. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  697. map_groups__insert(kmaps, curr_map);
  698. ++kernel_range;
  699. } else if (delta) {
  700. /* Kernel was relocated at boot time */
  701. pos->start -= delta;
  702. pos->end -= delta;
  703. }
  704. add_symbol:
  705. if (curr_map != initial_map) {
  706. rb_erase(&pos->rb_node, root);
  707. symbols__insert(&curr_map->dso->symbols, pos);
  708. ++moved;
  709. } else
  710. ++count;
  711. continue;
  712. discard_symbol:
  713. rb_erase(&pos->rb_node, root);
  714. symbol__delete(pos);
  715. }
  716. if (curr_map != initial_map &&
  717. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  718. machine__is_default_guest(kmaps->machine)) {
  719. dso__set_loaded(curr_map->dso);
  720. }
  721. return count + moved;
  722. }
  723. bool symbol__restricted_filename(const char *filename,
  724. const char *restricted_filename)
  725. {
  726. bool restricted = false;
  727. if (symbol_conf.kptr_restrict) {
  728. char *r = realpath(filename, NULL);
  729. if (r != NULL) {
  730. restricted = strcmp(r, restricted_filename) == 0;
  731. free(r);
  732. return restricted;
  733. }
  734. }
  735. return restricted;
  736. }
  737. struct module_info {
  738. struct rb_node rb_node;
  739. char *name;
  740. u64 start;
  741. };
  742. static void add_module(struct module_info *mi, struct rb_root *modules)
  743. {
  744. struct rb_node **p = &modules->rb_node;
  745. struct rb_node *parent = NULL;
  746. struct module_info *m;
  747. while (*p != NULL) {
  748. parent = *p;
  749. m = rb_entry(parent, struct module_info, rb_node);
  750. if (strcmp(mi->name, m->name) < 0)
  751. p = &(*p)->rb_left;
  752. else
  753. p = &(*p)->rb_right;
  754. }
  755. rb_link_node(&mi->rb_node, parent, p);
  756. rb_insert_color(&mi->rb_node, modules);
  757. }
  758. static void delete_modules(struct rb_root *modules)
  759. {
  760. struct module_info *mi;
  761. struct rb_node *next = rb_first(modules);
  762. while (next) {
  763. mi = rb_entry(next, struct module_info, rb_node);
  764. next = rb_next(&mi->rb_node);
  765. rb_erase(&mi->rb_node, modules);
  766. zfree(&mi->name);
  767. free(mi);
  768. }
  769. }
  770. static struct module_info *find_module(const char *name,
  771. struct rb_root *modules)
  772. {
  773. struct rb_node *n = modules->rb_node;
  774. while (n) {
  775. struct module_info *m;
  776. int cmp;
  777. m = rb_entry(n, struct module_info, rb_node);
  778. cmp = strcmp(name, m->name);
  779. if (cmp < 0)
  780. n = n->rb_left;
  781. else if (cmp > 0)
  782. n = n->rb_right;
  783. else
  784. return m;
  785. }
  786. return NULL;
  787. }
  788. static int __read_proc_modules(void *arg, const char *name, u64 start,
  789. u64 size __maybe_unused)
  790. {
  791. struct rb_root *modules = arg;
  792. struct module_info *mi;
  793. mi = zalloc(sizeof(struct module_info));
  794. if (!mi)
  795. return -ENOMEM;
  796. mi->name = strdup(name);
  797. mi->start = start;
  798. if (!mi->name) {
  799. free(mi);
  800. return -ENOMEM;
  801. }
  802. add_module(mi, modules);
  803. return 0;
  804. }
  805. static int read_proc_modules(const char *filename, struct rb_root *modules)
  806. {
  807. if (symbol__restricted_filename(filename, "/proc/modules"))
  808. return -1;
  809. if (modules__parse(filename, modules, __read_proc_modules)) {
  810. delete_modules(modules);
  811. return -1;
  812. }
  813. return 0;
  814. }
  815. int compare_proc_modules(const char *from, const char *to)
  816. {
  817. struct rb_root from_modules = RB_ROOT;
  818. struct rb_root to_modules = RB_ROOT;
  819. struct rb_node *from_node, *to_node;
  820. struct module_info *from_m, *to_m;
  821. int ret = -1;
  822. if (read_proc_modules(from, &from_modules))
  823. return -1;
  824. if (read_proc_modules(to, &to_modules))
  825. goto out_delete_from;
  826. from_node = rb_first(&from_modules);
  827. to_node = rb_first(&to_modules);
  828. while (from_node) {
  829. if (!to_node)
  830. break;
  831. from_m = rb_entry(from_node, struct module_info, rb_node);
  832. to_m = rb_entry(to_node, struct module_info, rb_node);
  833. if (from_m->start != to_m->start ||
  834. strcmp(from_m->name, to_m->name))
  835. break;
  836. from_node = rb_next(from_node);
  837. to_node = rb_next(to_node);
  838. }
  839. if (!from_node && !to_node)
  840. ret = 0;
  841. delete_modules(&to_modules);
  842. out_delete_from:
  843. delete_modules(&from_modules);
  844. return ret;
  845. }
  846. struct map *map_groups__first(struct map_groups *mg)
  847. {
  848. return maps__first(&mg->maps);
  849. }
  850. static int do_validate_kcore_modules(const char *filename,
  851. struct map_groups *kmaps)
  852. {
  853. struct rb_root modules = RB_ROOT;
  854. struct map *old_map;
  855. int err;
  856. err = read_proc_modules(filename, &modules);
  857. if (err)
  858. return err;
  859. old_map = map_groups__first(kmaps);
  860. while (old_map) {
  861. struct map *next = map_groups__next(old_map);
  862. struct module_info *mi;
  863. if (!__map__is_kmodule(old_map)) {
  864. old_map = next;
  865. continue;
  866. }
  867. /* Module must be in memory at the same address */
  868. mi = find_module(old_map->dso->short_name, &modules);
  869. if (!mi || mi->start != old_map->start) {
  870. err = -EINVAL;
  871. goto out;
  872. }
  873. old_map = next;
  874. }
  875. out:
  876. delete_modules(&modules);
  877. return err;
  878. }
  879. /*
  880. * If kallsyms is referenced by name then we look for filename in the same
  881. * directory.
  882. */
  883. static bool filename_from_kallsyms_filename(char *filename,
  884. const char *base_name,
  885. const char *kallsyms_filename)
  886. {
  887. char *name;
  888. strcpy(filename, kallsyms_filename);
  889. name = strrchr(filename, '/');
  890. if (!name)
  891. return false;
  892. name += 1;
  893. if (!strcmp(name, "kallsyms")) {
  894. strcpy(name, base_name);
  895. return true;
  896. }
  897. return false;
  898. }
  899. static int validate_kcore_modules(const char *kallsyms_filename,
  900. struct map *map)
  901. {
  902. struct map_groups *kmaps = map__kmaps(map);
  903. char modules_filename[PATH_MAX];
  904. if (!kmaps)
  905. return -EINVAL;
  906. if (!filename_from_kallsyms_filename(modules_filename, "modules",
  907. kallsyms_filename))
  908. return -EINVAL;
  909. if (do_validate_kcore_modules(modules_filename, kmaps))
  910. return -EINVAL;
  911. return 0;
  912. }
  913. static int validate_kcore_addresses(const char *kallsyms_filename,
  914. struct map *map)
  915. {
  916. struct kmap *kmap = map__kmap(map);
  917. if (!kmap)
  918. return -EINVAL;
  919. if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
  920. u64 start;
  921. if (kallsyms__get_function_start(kallsyms_filename,
  922. kmap->ref_reloc_sym->name, &start))
  923. return -ENOENT;
  924. if (start != kmap->ref_reloc_sym->addr)
  925. return -EINVAL;
  926. }
  927. return validate_kcore_modules(kallsyms_filename, map);
  928. }
  929. struct kcore_mapfn_data {
  930. struct dso *dso;
  931. struct list_head maps;
  932. };
  933. static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
  934. {
  935. struct kcore_mapfn_data *md = data;
  936. struct map *map;
  937. map = map__new2(start, md->dso);
  938. if (map == NULL)
  939. return -ENOMEM;
  940. map->end = map->start + len;
  941. map->pgoff = pgoff;
  942. list_add(&map->node, &md->maps);
  943. return 0;
  944. }
  945. static int dso__load_kcore(struct dso *dso, struct map *map,
  946. const char *kallsyms_filename)
  947. {
  948. struct map_groups *kmaps = map__kmaps(map);
  949. struct kcore_mapfn_data md;
  950. struct map *old_map, *new_map, *replacement_map = NULL;
  951. struct machine *machine;
  952. bool is_64_bit;
  953. int err, fd;
  954. char kcore_filename[PATH_MAX];
  955. u64 stext;
  956. if (!kmaps)
  957. return -EINVAL;
  958. machine = kmaps->machine;
  959. /* This function requires that the map is the kernel map */
  960. if (!__map__is_kernel(map))
  961. return -EINVAL;
  962. if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
  963. kallsyms_filename))
  964. return -EINVAL;
  965. /* Modules and kernel must be present at their original addresses */
  966. if (validate_kcore_addresses(kallsyms_filename, map))
  967. return -EINVAL;
  968. md.dso = dso;
  969. INIT_LIST_HEAD(&md.maps);
  970. fd = open(kcore_filename, O_RDONLY);
  971. if (fd < 0) {
  972. pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
  973. kcore_filename);
  974. return -EINVAL;
  975. }
  976. /* Read new maps into temporary lists */
  977. err = file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md,
  978. &is_64_bit);
  979. if (err)
  980. goto out_err;
  981. dso->is_64_bit = is_64_bit;
  982. if (list_empty(&md.maps)) {
  983. err = -EINVAL;
  984. goto out_err;
  985. }
  986. /* Remove old maps */
  987. old_map = map_groups__first(kmaps);
  988. while (old_map) {
  989. struct map *next = map_groups__next(old_map);
  990. if (old_map != map)
  991. map_groups__remove(kmaps, old_map);
  992. old_map = next;
  993. }
  994. machine->trampolines_mapped = false;
  995. /* Find the kernel map using the '_stext' symbol */
  996. if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
  997. list_for_each_entry(new_map, &md.maps, node) {
  998. if (stext >= new_map->start && stext < new_map->end) {
  999. replacement_map = new_map;
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. if (!replacement_map)
  1005. replacement_map = list_entry(md.maps.next, struct map, node);
  1006. /* Add new maps */
  1007. while (!list_empty(&md.maps)) {
  1008. new_map = list_entry(md.maps.next, struct map, node);
  1009. list_del_init(&new_map->node);
  1010. if (new_map == replacement_map) {
  1011. map->start = new_map->start;
  1012. map->end = new_map->end;
  1013. map->pgoff = new_map->pgoff;
  1014. map->map_ip = new_map->map_ip;
  1015. map->unmap_ip = new_map->unmap_ip;
  1016. /* Ensure maps are correctly ordered */
  1017. map__get(map);
  1018. map_groups__remove(kmaps, map);
  1019. map_groups__insert(kmaps, map);
  1020. map__put(map);
  1021. } else {
  1022. map_groups__insert(kmaps, new_map);
  1023. }
  1024. map__put(new_map);
  1025. }
  1026. if (machine__is(machine, "x86_64")) {
  1027. u64 addr;
  1028. /*
  1029. * If one of the corresponding symbols is there, assume the
  1030. * entry trampoline maps are too.
  1031. */
  1032. if (!kallsyms__get_function_start(kallsyms_filename,
  1033. ENTRY_TRAMPOLINE_NAME,
  1034. &addr))
  1035. machine->trampolines_mapped = true;
  1036. }
  1037. /*
  1038. * Set the data type and long name so that kcore can be read via
  1039. * dso__data_read_addr().
  1040. */
  1041. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1042. dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
  1043. else
  1044. dso->binary_type = DSO_BINARY_TYPE__KCORE;
  1045. dso__set_long_name(dso, strdup(kcore_filename), true);
  1046. close(fd);
  1047. if (map->prot & PROT_EXEC)
  1048. pr_debug("Using %s for kernel object code\n", kcore_filename);
  1049. else
  1050. pr_debug("Using %s for kernel data\n", kcore_filename);
  1051. return 0;
  1052. out_err:
  1053. while (!list_empty(&md.maps)) {
  1054. map = list_entry(md.maps.next, struct map, node);
  1055. list_del_init(&map->node);
  1056. map__put(map);
  1057. }
  1058. close(fd);
  1059. return -EINVAL;
  1060. }
  1061. /*
  1062. * If the kernel is relocated at boot time, kallsyms won't match. Compute the
  1063. * delta based on the relocation reference symbol.
  1064. */
  1065. static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
  1066. {
  1067. u64 addr;
  1068. if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
  1069. return 0;
  1070. if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
  1071. return -1;
  1072. *delta = addr - kmap->ref_reloc_sym->addr;
  1073. return 0;
  1074. }
  1075. int __dso__load_kallsyms(struct dso *dso, const char *filename,
  1076. struct map *map, bool no_kcore)
  1077. {
  1078. struct kmap *kmap = map__kmap(map);
  1079. u64 delta = 0;
  1080. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1081. return -1;
  1082. if (!kmap || !kmap->kmaps)
  1083. return -1;
  1084. if (dso__load_all_kallsyms(dso, filename) < 0)
  1085. return -1;
  1086. if (kallsyms__delta(kmap, filename, &delta))
  1087. return -1;
  1088. symbols__fixup_end(&dso->symbols);
  1089. symbols__fixup_duplicate(&dso->symbols);
  1090. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1091. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  1092. else
  1093. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  1094. if (!no_kcore && !dso__load_kcore(dso, map, filename))
  1095. return map_groups__split_kallsyms_for_kcore(kmap->kmaps, dso);
  1096. else
  1097. return map_groups__split_kallsyms(kmap->kmaps, dso, delta, map);
  1098. }
  1099. int dso__load_kallsyms(struct dso *dso, const char *filename,
  1100. struct map *map)
  1101. {
  1102. return __dso__load_kallsyms(dso, filename, map, false);
  1103. }
  1104. static int dso__load_perf_map(const char *map_path, struct dso *dso)
  1105. {
  1106. char *line = NULL;
  1107. size_t n;
  1108. FILE *file;
  1109. int nr_syms = 0;
  1110. file = fopen(map_path, "r");
  1111. if (file == NULL)
  1112. goto out_failure;
  1113. while (!feof(file)) {
  1114. u64 start, size;
  1115. struct symbol *sym;
  1116. int line_len, len;
  1117. line_len = getline(&line, &n, file);
  1118. if (line_len < 0)
  1119. break;
  1120. if (!line)
  1121. goto out_failure;
  1122. line[--line_len] = '\0'; /* \n */
  1123. len = hex2u64(line, &start);
  1124. len++;
  1125. if (len + 2 >= line_len)
  1126. continue;
  1127. len += hex2u64(line + len, &size);
  1128. len++;
  1129. if (len + 2 >= line_len)
  1130. continue;
  1131. sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
  1132. if (sym == NULL)
  1133. goto out_delete_line;
  1134. symbols__insert(&dso->symbols, sym);
  1135. nr_syms++;
  1136. }
  1137. free(line);
  1138. fclose(file);
  1139. return nr_syms;
  1140. out_delete_line:
  1141. free(line);
  1142. out_failure:
  1143. return -1;
  1144. }
  1145. static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
  1146. enum dso_binary_type type)
  1147. {
  1148. switch (type) {
  1149. case DSO_BINARY_TYPE__JAVA_JIT:
  1150. case DSO_BINARY_TYPE__DEBUGLINK:
  1151. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  1152. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  1153. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  1154. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  1155. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  1156. return !kmod && dso->kernel == DSO_TYPE_USER;
  1157. case DSO_BINARY_TYPE__KALLSYMS:
  1158. case DSO_BINARY_TYPE__VMLINUX:
  1159. case DSO_BINARY_TYPE__KCORE:
  1160. return dso->kernel == DSO_TYPE_KERNEL;
  1161. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  1162. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  1163. case DSO_BINARY_TYPE__GUEST_KCORE:
  1164. return dso->kernel == DSO_TYPE_GUEST_KERNEL;
  1165. case DSO_BINARY_TYPE__GUEST_KMODULE:
  1166. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  1167. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  1168. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  1169. /*
  1170. * kernel modules know their symtab type - it's set when
  1171. * creating a module dso in machine__findnew_module_map().
  1172. */
  1173. return kmod && dso->symtab_type == type;
  1174. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  1175. case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
  1176. return true;
  1177. case DSO_BINARY_TYPE__NOT_FOUND:
  1178. default:
  1179. return false;
  1180. }
  1181. }
  1182. /* Checks for the existence of the perf-<pid>.map file in two different
  1183. * locations. First, if the process is a separate mount namespace, check in
  1184. * that namespace using the pid of the innermost pid namespace. If's not in a
  1185. * namespace, or the file can't be found there, try in the mount namespace of
  1186. * the tracing process using our view of its pid.
  1187. */
  1188. static int dso__find_perf_map(char *filebuf, size_t bufsz,
  1189. struct nsinfo **nsip)
  1190. {
  1191. struct nscookie nsc;
  1192. struct nsinfo *nsi;
  1193. struct nsinfo *nnsi;
  1194. int rc = -1;
  1195. nsi = *nsip;
  1196. if (nsi->need_setns) {
  1197. snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
  1198. nsinfo__mountns_enter(nsi, &nsc);
  1199. rc = access(filebuf, R_OK);
  1200. nsinfo__mountns_exit(&nsc);
  1201. if (rc == 0)
  1202. return rc;
  1203. }
  1204. nnsi = nsinfo__copy(nsi);
  1205. if (nnsi) {
  1206. nsinfo__put(nsi);
  1207. nnsi->need_setns = false;
  1208. snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
  1209. *nsip = nnsi;
  1210. rc = 0;
  1211. }
  1212. return rc;
  1213. }
  1214. int dso__load(struct dso *dso, struct map *map)
  1215. {
  1216. char *name;
  1217. int ret = -1;
  1218. u_int i;
  1219. struct machine *machine;
  1220. char *root_dir = (char *) "";
  1221. int ss_pos = 0;
  1222. struct symsrc ss_[2];
  1223. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  1224. bool kmod;
  1225. bool perfmap;
  1226. unsigned char build_id[BUILD_ID_SIZE];
  1227. struct nscookie nsc;
  1228. char newmapname[PATH_MAX];
  1229. const char *map_path = dso->long_name;
  1230. perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
  1231. if (perfmap) {
  1232. if (dso->nsinfo && (dso__find_perf_map(newmapname,
  1233. sizeof(newmapname), &dso->nsinfo) == 0)) {
  1234. map_path = newmapname;
  1235. }
  1236. }
  1237. nsinfo__mountns_enter(dso->nsinfo, &nsc);
  1238. pthread_mutex_lock(&dso->lock);
  1239. /* check again under the dso->lock */
  1240. if (dso__loaded(dso)) {
  1241. ret = 1;
  1242. goto out;
  1243. }
  1244. if (map->groups && map->groups->machine)
  1245. machine = map->groups->machine;
  1246. else
  1247. machine = NULL;
  1248. if (dso->kernel) {
  1249. if (dso->kernel == DSO_TYPE_KERNEL)
  1250. ret = dso__load_kernel_sym(dso, map);
  1251. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1252. ret = dso__load_guest_kernel_sym(dso, map);
  1253. if (machine__is(machine, "x86_64"))
  1254. machine__map_x86_64_entry_trampolines(machine, dso);
  1255. goto out;
  1256. }
  1257. dso->adjust_symbols = 0;
  1258. if (perfmap) {
  1259. struct stat st;
  1260. if (lstat(map_path, &st) < 0)
  1261. goto out;
  1262. if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
  1263. pr_warning("File %s not owned by current user or root, "
  1264. "ignoring it (use -f to override).\n", map_path);
  1265. goto out;
  1266. }
  1267. ret = dso__load_perf_map(map_path, dso);
  1268. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  1269. DSO_BINARY_TYPE__NOT_FOUND;
  1270. goto out;
  1271. }
  1272. if (machine)
  1273. root_dir = machine->root_dir;
  1274. name = malloc(PATH_MAX);
  1275. if (!name)
  1276. goto out;
  1277. kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  1278. dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
  1279. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
  1280. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
  1281. /*
  1282. * Read the build id if possible. This is required for
  1283. * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
  1284. */
  1285. if (!dso->has_build_id &&
  1286. is_regular_file(dso->long_name)) {
  1287. __symbol__join_symfs(name, PATH_MAX, dso->long_name);
  1288. if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
  1289. dso__set_build_id(dso, build_id);
  1290. }
  1291. /*
  1292. * Iterate over candidate debug images.
  1293. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  1294. * and/or opd section) for processing.
  1295. */
  1296. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  1297. struct symsrc *ss = &ss_[ss_pos];
  1298. bool next_slot = false;
  1299. bool is_reg;
  1300. bool nsexit;
  1301. int sirc = -1;
  1302. enum dso_binary_type symtab_type = binary_type_symtab[i];
  1303. nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
  1304. symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
  1305. if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
  1306. continue;
  1307. if (dso__read_binary_type_filename(dso, symtab_type,
  1308. root_dir, name, PATH_MAX))
  1309. continue;
  1310. if (nsexit)
  1311. nsinfo__mountns_exit(&nsc);
  1312. is_reg = is_regular_file(name);
  1313. if (is_reg)
  1314. sirc = symsrc__init(ss, dso, name, symtab_type);
  1315. if (nsexit)
  1316. nsinfo__mountns_enter(dso->nsinfo, &nsc);
  1317. if (!is_reg || sirc < 0)
  1318. continue;
  1319. if (!syms_ss && symsrc__has_symtab(ss)) {
  1320. syms_ss = ss;
  1321. next_slot = true;
  1322. if (!dso->symsrc_filename)
  1323. dso->symsrc_filename = strdup(name);
  1324. }
  1325. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  1326. runtime_ss = ss;
  1327. next_slot = true;
  1328. }
  1329. if (next_slot) {
  1330. ss_pos++;
  1331. if (syms_ss && runtime_ss)
  1332. break;
  1333. } else {
  1334. symsrc__destroy(ss);
  1335. }
  1336. }
  1337. if (!runtime_ss && !syms_ss)
  1338. goto out_free;
  1339. if (runtime_ss && !syms_ss) {
  1340. syms_ss = runtime_ss;
  1341. }
  1342. /* We'll have to hope for the best */
  1343. if (!runtime_ss && syms_ss)
  1344. runtime_ss = syms_ss;
  1345. if (syms_ss)
  1346. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
  1347. else
  1348. ret = -1;
  1349. if (ret > 0) {
  1350. int nr_plt;
  1351. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
  1352. if (nr_plt > 0)
  1353. ret += nr_plt;
  1354. }
  1355. for (; ss_pos > 0; ss_pos--)
  1356. symsrc__destroy(&ss_[ss_pos - 1]);
  1357. out_free:
  1358. free(name);
  1359. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1360. ret = 0;
  1361. out:
  1362. dso__set_loaded(dso);
  1363. pthread_mutex_unlock(&dso->lock);
  1364. nsinfo__mountns_exit(&nsc);
  1365. return ret;
  1366. }
  1367. struct map *map_groups__find_by_name(struct map_groups *mg, const char *name)
  1368. {
  1369. struct maps *maps = &mg->maps;
  1370. struct map *map;
  1371. down_read(&maps->lock);
  1372. for (map = maps__first(maps); map; map = map__next(map)) {
  1373. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1374. goto out_unlock;
  1375. }
  1376. map = NULL;
  1377. out_unlock:
  1378. up_read(&maps->lock);
  1379. return map;
  1380. }
  1381. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1382. const char *vmlinux, bool vmlinux_allocated)
  1383. {
  1384. int err = -1;
  1385. struct symsrc ss;
  1386. char symfs_vmlinux[PATH_MAX];
  1387. enum dso_binary_type symtab_type;
  1388. if (vmlinux[0] == '/')
  1389. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  1390. else
  1391. symbol__join_symfs(symfs_vmlinux, vmlinux);
  1392. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1393. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1394. else
  1395. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  1396. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  1397. return -1;
  1398. err = dso__load_sym(dso, map, &ss, &ss, 0);
  1399. symsrc__destroy(&ss);
  1400. if (err > 0) {
  1401. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1402. dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1403. else
  1404. dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
  1405. dso__set_long_name(dso, vmlinux, vmlinux_allocated);
  1406. dso__set_loaded(dso);
  1407. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1408. }
  1409. return err;
  1410. }
  1411. int dso__load_vmlinux_path(struct dso *dso, struct map *map)
  1412. {
  1413. int i, err = 0;
  1414. char *filename = NULL;
  1415. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1416. vmlinux_path__nr_entries + 1);
  1417. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1418. err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
  1419. if (err > 0)
  1420. goto out;
  1421. }
  1422. if (!symbol_conf.ignore_vmlinux_buildid)
  1423. filename = dso__build_id_filename(dso, NULL, 0, false);
  1424. if (filename != NULL) {
  1425. err = dso__load_vmlinux(dso, map, filename, true);
  1426. if (err > 0)
  1427. goto out;
  1428. free(filename);
  1429. }
  1430. out:
  1431. return err;
  1432. }
  1433. static bool visible_dir_filter(const char *name, struct dirent *d)
  1434. {
  1435. if (d->d_type != DT_DIR)
  1436. return false;
  1437. return lsdir_no_dot_filter(name, d);
  1438. }
  1439. static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
  1440. {
  1441. char kallsyms_filename[PATH_MAX];
  1442. int ret = -1;
  1443. struct strlist *dirs;
  1444. struct str_node *nd;
  1445. dirs = lsdir(dir, visible_dir_filter);
  1446. if (!dirs)
  1447. return -1;
  1448. strlist__for_each_entry(nd, dirs) {
  1449. scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
  1450. "%s/%s/kallsyms", dir, nd->s);
  1451. if (!validate_kcore_addresses(kallsyms_filename, map)) {
  1452. strlcpy(dir, kallsyms_filename, dir_sz);
  1453. ret = 0;
  1454. break;
  1455. }
  1456. }
  1457. strlist__delete(dirs);
  1458. return ret;
  1459. }
  1460. /*
  1461. * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
  1462. * since access(R_OK) only checks with real UID/GID but open() use effective
  1463. * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
  1464. */
  1465. static bool filename__readable(const char *file)
  1466. {
  1467. int fd = open(file, O_RDONLY);
  1468. if (fd < 0)
  1469. return false;
  1470. close(fd);
  1471. return true;
  1472. }
  1473. static char *dso__find_kallsyms(struct dso *dso, struct map *map)
  1474. {
  1475. u8 host_build_id[BUILD_ID_SIZE];
  1476. char sbuild_id[SBUILD_ID_SIZE];
  1477. bool is_host = false;
  1478. char path[PATH_MAX];
  1479. if (!dso->has_build_id) {
  1480. /*
  1481. * Last resort, if we don't have a build-id and couldn't find
  1482. * any vmlinux file, try the running kernel kallsyms table.
  1483. */
  1484. goto proc_kallsyms;
  1485. }
  1486. if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
  1487. sizeof(host_build_id)) == 0)
  1488. is_host = dso__build_id_equal(dso, host_build_id);
  1489. /* Try a fast path for /proc/kallsyms if possible */
  1490. if (is_host) {
  1491. /*
  1492. * Do not check the build-id cache, unless we know we cannot use
  1493. * /proc/kcore or module maps don't match to /proc/kallsyms.
  1494. * To check readability of /proc/kcore, do not use access(R_OK)
  1495. * since /proc/kcore requires CAP_SYS_RAWIO to read and access
  1496. * can't check it.
  1497. */
  1498. if (filename__readable("/proc/kcore") &&
  1499. !validate_kcore_addresses("/proc/kallsyms", map))
  1500. goto proc_kallsyms;
  1501. }
  1502. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  1503. /* Find kallsyms in build-id cache with kcore */
  1504. scnprintf(path, sizeof(path), "%s/%s/%s",
  1505. buildid_dir, DSO__NAME_KCORE, sbuild_id);
  1506. if (!find_matching_kcore(map, path, sizeof(path)))
  1507. return strdup(path);
  1508. /* Use current /proc/kallsyms if possible */
  1509. if (is_host) {
  1510. proc_kallsyms:
  1511. return strdup("/proc/kallsyms");
  1512. }
  1513. /* Finally, find a cache of kallsyms */
  1514. if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
  1515. pr_err("No kallsyms or vmlinux with build-id %s was found\n",
  1516. sbuild_id);
  1517. return NULL;
  1518. }
  1519. return strdup(path);
  1520. }
  1521. static int dso__load_kernel_sym(struct dso *dso, struct map *map)
  1522. {
  1523. int err;
  1524. const char *kallsyms_filename = NULL;
  1525. char *kallsyms_allocated_filename = NULL;
  1526. /*
  1527. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1528. * it and only it, reporting errors to the user if it cannot be used.
  1529. *
  1530. * For instance, try to analyse an ARM perf.data file _without_ a
  1531. * build-id, or if the user specifies the wrong path to the right
  1532. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1533. * x86_86 one, on the machine where analysis is being performed, say),
  1534. * or worse, /proc/kallsyms.
  1535. *
  1536. * If the specified file _has_ a build-id and there is a build-id
  1537. * section in the perf.data file, we will still do the expected
  1538. * validation in dso__load_vmlinux and will bail out if they don't
  1539. * match.
  1540. */
  1541. if (symbol_conf.kallsyms_name != NULL) {
  1542. kallsyms_filename = symbol_conf.kallsyms_name;
  1543. goto do_kallsyms;
  1544. }
  1545. if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
  1546. return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
  1547. }
  1548. if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
  1549. err = dso__load_vmlinux_path(dso, map);
  1550. if (err > 0)
  1551. return err;
  1552. }
  1553. /* do not try local files if a symfs was given */
  1554. if (symbol_conf.symfs[0] != 0)
  1555. return -1;
  1556. kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
  1557. if (!kallsyms_allocated_filename)
  1558. return -1;
  1559. kallsyms_filename = kallsyms_allocated_filename;
  1560. do_kallsyms:
  1561. err = dso__load_kallsyms(dso, kallsyms_filename, map);
  1562. if (err > 0)
  1563. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1564. free(kallsyms_allocated_filename);
  1565. if (err > 0 && !dso__is_kcore(dso)) {
  1566. dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
  1567. dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
  1568. map__fixup_start(map);
  1569. map__fixup_end(map);
  1570. }
  1571. return err;
  1572. }
  1573. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
  1574. {
  1575. int err;
  1576. const char *kallsyms_filename = NULL;
  1577. struct machine *machine;
  1578. char path[PATH_MAX];
  1579. if (!map->groups) {
  1580. pr_debug("Guest kernel map hasn't the point to groups\n");
  1581. return -1;
  1582. }
  1583. machine = map->groups->machine;
  1584. if (machine__is_default_guest(machine)) {
  1585. /*
  1586. * if the user specified a vmlinux filename, use it and only
  1587. * it, reporting errors to the user if it cannot be used.
  1588. * Or use file guest_kallsyms inputted by user on commandline
  1589. */
  1590. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1591. err = dso__load_vmlinux(dso, map,
  1592. symbol_conf.default_guest_vmlinux_name,
  1593. false);
  1594. return err;
  1595. }
  1596. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1597. if (!kallsyms_filename)
  1598. return -1;
  1599. } else {
  1600. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1601. kallsyms_filename = path;
  1602. }
  1603. err = dso__load_kallsyms(dso, kallsyms_filename, map);
  1604. if (err > 0)
  1605. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1606. if (err > 0 && !dso__is_kcore(dso)) {
  1607. dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  1608. dso__set_long_name(dso, machine->mmap_name, false);
  1609. map__fixup_start(map);
  1610. map__fixup_end(map);
  1611. }
  1612. return err;
  1613. }
  1614. static void vmlinux_path__exit(void)
  1615. {
  1616. while (--vmlinux_path__nr_entries >= 0)
  1617. zfree(&vmlinux_path[vmlinux_path__nr_entries]);
  1618. vmlinux_path__nr_entries = 0;
  1619. zfree(&vmlinux_path);
  1620. }
  1621. static const char * const vmlinux_paths[] = {
  1622. "vmlinux",
  1623. "/boot/vmlinux"
  1624. };
  1625. static const char * const vmlinux_paths_upd[] = {
  1626. "/boot/vmlinux-%s",
  1627. "/usr/lib/debug/boot/vmlinux-%s",
  1628. "/lib/modules/%s/build/vmlinux",
  1629. "/usr/lib/debug/lib/modules/%s/vmlinux",
  1630. "/usr/lib/debug/boot/vmlinux-%s.debug"
  1631. };
  1632. static int vmlinux_path__add(const char *new_entry)
  1633. {
  1634. vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
  1635. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1636. return -1;
  1637. ++vmlinux_path__nr_entries;
  1638. return 0;
  1639. }
  1640. static int vmlinux_path__init(struct perf_env *env)
  1641. {
  1642. struct utsname uts;
  1643. char bf[PATH_MAX];
  1644. char *kernel_version;
  1645. unsigned int i;
  1646. vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
  1647. ARRAY_SIZE(vmlinux_paths_upd)));
  1648. if (vmlinux_path == NULL)
  1649. return -1;
  1650. for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
  1651. if (vmlinux_path__add(vmlinux_paths[i]) < 0)
  1652. goto out_fail;
  1653. /* only try kernel version if no symfs was given */
  1654. if (symbol_conf.symfs[0] != 0)
  1655. return 0;
  1656. if (env) {
  1657. kernel_version = env->os_release;
  1658. } else {
  1659. if (uname(&uts) < 0)
  1660. goto out_fail;
  1661. kernel_version = uts.release;
  1662. }
  1663. for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
  1664. snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
  1665. if (vmlinux_path__add(bf) < 0)
  1666. goto out_fail;
  1667. }
  1668. return 0;
  1669. out_fail:
  1670. vmlinux_path__exit();
  1671. return -1;
  1672. }
  1673. int setup_list(struct strlist **list, const char *list_str,
  1674. const char *list_name)
  1675. {
  1676. if (list_str == NULL)
  1677. return 0;
  1678. *list = strlist__new(list_str, NULL);
  1679. if (!*list) {
  1680. pr_err("problems parsing %s list\n", list_name);
  1681. return -1;
  1682. }
  1683. symbol_conf.has_filter = true;
  1684. return 0;
  1685. }
  1686. int setup_intlist(struct intlist **list, const char *list_str,
  1687. const char *list_name)
  1688. {
  1689. if (list_str == NULL)
  1690. return 0;
  1691. *list = intlist__new(list_str);
  1692. if (!*list) {
  1693. pr_err("problems parsing %s list\n", list_name);
  1694. return -1;
  1695. }
  1696. return 0;
  1697. }
  1698. static bool symbol__read_kptr_restrict(void)
  1699. {
  1700. bool value = false;
  1701. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1702. if (fp != NULL) {
  1703. char line[8];
  1704. if (fgets(line, sizeof(line), fp) != NULL)
  1705. value = ((geteuid() != 0) || (getuid() != 0)) ?
  1706. (atoi(line) != 0) :
  1707. (atoi(line) == 2);
  1708. fclose(fp);
  1709. }
  1710. return value;
  1711. }
  1712. int symbol__annotation_init(void)
  1713. {
  1714. if (symbol_conf.init_annotation)
  1715. return 0;
  1716. if (symbol_conf.initialized) {
  1717. pr_err("Annotation needs to be init before symbol__init()\n");
  1718. return -1;
  1719. }
  1720. symbol_conf.priv_size += sizeof(struct annotation);
  1721. symbol_conf.init_annotation = true;
  1722. return 0;
  1723. }
  1724. int symbol__init(struct perf_env *env)
  1725. {
  1726. const char *symfs;
  1727. if (symbol_conf.initialized)
  1728. return 0;
  1729. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1730. symbol__elf_init();
  1731. if (symbol_conf.sort_by_name)
  1732. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1733. sizeof(struct symbol));
  1734. if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
  1735. return -1;
  1736. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1737. pr_err("'.' is the only non valid --field-separator argument\n");
  1738. return -1;
  1739. }
  1740. if (setup_list(&symbol_conf.dso_list,
  1741. symbol_conf.dso_list_str, "dso") < 0)
  1742. return -1;
  1743. if (setup_list(&symbol_conf.comm_list,
  1744. symbol_conf.comm_list_str, "comm") < 0)
  1745. goto out_free_dso_list;
  1746. if (setup_intlist(&symbol_conf.pid_list,
  1747. symbol_conf.pid_list_str, "pid") < 0)
  1748. goto out_free_comm_list;
  1749. if (setup_intlist(&symbol_conf.tid_list,
  1750. symbol_conf.tid_list_str, "tid") < 0)
  1751. goto out_free_pid_list;
  1752. if (setup_list(&symbol_conf.sym_list,
  1753. symbol_conf.sym_list_str, "symbol") < 0)
  1754. goto out_free_tid_list;
  1755. if (setup_list(&symbol_conf.bt_stop_list,
  1756. symbol_conf.bt_stop_list_str, "symbol") < 0)
  1757. goto out_free_sym_list;
  1758. /*
  1759. * A path to symbols of "/" is identical to ""
  1760. * reset here for simplicity.
  1761. */
  1762. symfs = realpath(symbol_conf.symfs, NULL);
  1763. if (symfs == NULL)
  1764. symfs = symbol_conf.symfs;
  1765. if (strcmp(symfs, "/") == 0)
  1766. symbol_conf.symfs = "";
  1767. if (symfs != symbol_conf.symfs)
  1768. free((void *)symfs);
  1769. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1770. symbol_conf.initialized = true;
  1771. return 0;
  1772. out_free_sym_list:
  1773. strlist__delete(symbol_conf.sym_list);
  1774. out_free_tid_list:
  1775. intlist__delete(symbol_conf.tid_list);
  1776. out_free_pid_list:
  1777. intlist__delete(symbol_conf.pid_list);
  1778. out_free_comm_list:
  1779. strlist__delete(symbol_conf.comm_list);
  1780. out_free_dso_list:
  1781. strlist__delete(symbol_conf.dso_list);
  1782. return -1;
  1783. }
  1784. void symbol__exit(void)
  1785. {
  1786. if (!symbol_conf.initialized)
  1787. return;
  1788. strlist__delete(symbol_conf.bt_stop_list);
  1789. strlist__delete(symbol_conf.sym_list);
  1790. strlist__delete(symbol_conf.dso_list);
  1791. strlist__delete(symbol_conf.comm_list);
  1792. intlist__delete(symbol_conf.tid_list);
  1793. intlist__delete(symbol_conf.pid_list);
  1794. vmlinux_path__exit();
  1795. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1796. symbol_conf.bt_stop_list = NULL;
  1797. symbol_conf.initialized = false;
  1798. }
  1799. int symbol__config_symfs(const struct option *opt __maybe_unused,
  1800. const char *dir, int unset __maybe_unused)
  1801. {
  1802. char *bf = NULL;
  1803. int ret;
  1804. symbol_conf.symfs = strdup(dir);
  1805. if (symbol_conf.symfs == NULL)
  1806. return -ENOMEM;
  1807. /* skip the locally configured cache if a symfs is given, and
  1808. * config buildid dir to symfs/.debug
  1809. */
  1810. ret = asprintf(&bf, "%s/%s", dir, ".debug");
  1811. if (ret < 0)
  1812. return -ENOMEM;
  1813. set_buildid_dir(bf);
  1814. free(bf);
  1815. return 0;
  1816. }
  1817. struct mem_info *mem_info__get(struct mem_info *mi)
  1818. {
  1819. if (mi)
  1820. refcount_inc(&mi->refcnt);
  1821. return mi;
  1822. }
  1823. void mem_info__put(struct mem_info *mi)
  1824. {
  1825. if (mi && refcount_dec_and_test(&mi->refcnt))
  1826. free(mi);
  1827. }
  1828. struct mem_info *mem_info__new(void)
  1829. {
  1830. struct mem_info *mi = zalloc(sizeof(*mi));
  1831. if (mi)
  1832. refcount_set(&mi->refcnt, 1);
  1833. return mi;
  1834. }