symbol.c 50 KB

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