symbol.c 50 KB

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