machine.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. #include "callchain.h"
  2. #include "debug.h"
  3. #include "event.h"
  4. #include "evsel.h"
  5. #include "hist.h"
  6. #include "machine.h"
  7. #include "map.h"
  8. #include "sort.h"
  9. #include "strlist.h"
  10. #include "thread.h"
  11. #include <stdbool.h>
  12. #include "unwind.h"
  13. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  14. {
  15. map_groups__init(&machine->kmaps);
  16. RB_CLEAR_NODE(&machine->rb_node);
  17. INIT_LIST_HEAD(&machine->user_dsos);
  18. INIT_LIST_HEAD(&machine->kernel_dsos);
  19. machine->threads = RB_ROOT;
  20. INIT_LIST_HEAD(&machine->dead_threads);
  21. machine->last_match = NULL;
  22. machine->kmaps.machine = machine;
  23. machine->pid = pid;
  24. machine->symbol_filter = NULL;
  25. machine->root_dir = strdup(root_dir);
  26. if (machine->root_dir == NULL)
  27. return -ENOMEM;
  28. if (pid != HOST_KERNEL_ID) {
  29. struct thread *thread = machine__findnew_thread(machine, 0,
  30. pid);
  31. char comm[64];
  32. if (thread == NULL)
  33. return -ENOMEM;
  34. snprintf(comm, sizeof(comm), "[guest/%d]", pid);
  35. thread__set_comm(thread, comm);
  36. }
  37. return 0;
  38. }
  39. static void dsos__delete(struct list_head *dsos)
  40. {
  41. struct dso *pos, *n;
  42. list_for_each_entry_safe(pos, n, dsos, node) {
  43. list_del(&pos->node);
  44. dso__delete(pos);
  45. }
  46. }
  47. void machine__delete_dead_threads(struct machine *machine)
  48. {
  49. struct thread *n, *t;
  50. list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
  51. list_del(&t->node);
  52. thread__delete(t);
  53. }
  54. }
  55. void machine__delete_threads(struct machine *machine)
  56. {
  57. struct rb_node *nd = rb_first(&machine->threads);
  58. while (nd) {
  59. struct thread *t = rb_entry(nd, struct thread, rb_node);
  60. rb_erase(&t->rb_node, &machine->threads);
  61. nd = rb_next(nd);
  62. thread__delete(t);
  63. }
  64. }
  65. void machine__exit(struct machine *machine)
  66. {
  67. map_groups__exit(&machine->kmaps);
  68. dsos__delete(&machine->user_dsos);
  69. dsos__delete(&machine->kernel_dsos);
  70. free(machine->root_dir);
  71. machine->root_dir = NULL;
  72. }
  73. void machine__delete(struct machine *machine)
  74. {
  75. machine__exit(machine);
  76. free(machine);
  77. }
  78. void machines__init(struct machines *machines)
  79. {
  80. machine__init(&machines->host, "", HOST_KERNEL_ID);
  81. machines->guests = RB_ROOT;
  82. machines->symbol_filter = NULL;
  83. }
  84. void machines__exit(struct machines *machines)
  85. {
  86. machine__exit(&machines->host);
  87. /* XXX exit guest */
  88. }
  89. struct machine *machines__add(struct machines *machines, pid_t pid,
  90. const char *root_dir)
  91. {
  92. struct rb_node **p = &machines->guests.rb_node;
  93. struct rb_node *parent = NULL;
  94. struct machine *pos, *machine = malloc(sizeof(*machine));
  95. if (machine == NULL)
  96. return NULL;
  97. if (machine__init(machine, root_dir, pid) != 0) {
  98. free(machine);
  99. return NULL;
  100. }
  101. machine->symbol_filter = machines->symbol_filter;
  102. while (*p != NULL) {
  103. parent = *p;
  104. pos = rb_entry(parent, struct machine, rb_node);
  105. if (pid < pos->pid)
  106. p = &(*p)->rb_left;
  107. else
  108. p = &(*p)->rb_right;
  109. }
  110. rb_link_node(&machine->rb_node, parent, p);
  111. rb_insert_color(&machine->rb_node, &machines->guests);
  112. return machine;
  113. }
  114. void machines__set_symbol_filter(struct machines *machines,
  115. symbol_filter_t symbol_filter)
  116. {
  117. struct rb_node *nd;
  118. machines->symbol_filter = symbol_filter;
  119. machines->host.symbol_filter = symbol_filter;
  120. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  121. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  122. machine->symbol_filter = symbol_filter;
  123. }
  124. }
  125. struct machine *machines__find(struct machines *machines, pid_t pid)
  126. {
  127. struct rb_node **p = &machines->guests.rb_node;
  128. struct rb_node *parent = NULL;
  129. struct machine *machine;
  130. struct machine *default_machine = NULL;
  131. if (pid == HOST_KERNEL_ID)
  132. return &machines->host;
  133. while (*p != NULL) {
  134. parent = *p;
  135. machine = rb_entry(parent, struct machine, rb_node);
  136. if (pid < machine->pid)
  137. p = &(*p)->rb_left;
  138. else if (pid > machine->pid)
  139. p = &(*p)->rb_right;
  140. else
  141. return machine;
  142. if (!machine->pid)
  143. default_machine = machine;
  144. }
  145. return default_machine;
  146. }
  147. struct machine *machines__findnew(struct machines *machines, pid_t pid)
  148. {
  149. char path[PATH_MAX];
  150. const char *root_dir = "";
  151. struct machine *machine = machines__find(machines, pid);
  152. if (machine && (machine->pid == pid))
  153. goto out;
  154. if ((pid != HOST_KERNEL_ID) &&
  155. (pid != DEFAULT_GUEST_KERNEL_ID) &&
  156. (symbol_conf.guestmount)) {
  157. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  158. if (access(path, R_OK)) {
  159. static struct strlist *seen;
  160. if (!seen)
  161. seen = strlist__new(true, NULL);
  162. if (!strlist__has_entry(seen, path)) {
  163. pr_err("Can't access file %s\n", path);
  164. strlist__add(seen, path);
  165. }
  166. machine = NULL;
  167. goto out;
  168. }
  169. root_dir = path;
  170. }
  171. machine = machines__add(machines, pid, root_dir);
  172. out:
  173. return machine;
  174. }
  175. void machines__process_guests(struct machines *machines,
  176. machine__process_t process, void *data)
  177. {
  178. struct rb_node *nd;
  179. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  180. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  181. process(pos, data);
  182. }
  183. }
  184. char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
  185. {
  186. if (machine__is_host(machine))
  187. snprintf(bf, size, "[%s]", "kernel.kallsyms");
  188. else if (machine__is_default_guest(machine))
  189. snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
  190. else {
  191. snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
  192. machine->pid);
  193. }
  194. return bf;
  195. }
  196. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
  197. {
  198. struct rb_node *node;
  199. struct machine *machine;
  200. machines->host.id_hdr_size = id_hdr_size;
  201. for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
  202. machine = rb_entry(node, struct machine, rb_node);
  203. machine->id_hdr_size = id_hdr_size;
  204. }
  205. return;
  206. }
  207. static struct thread *__machine__findnew_thread(struct machine *machine,
  208. pid_t pid, pid_t tid,
  209. bool create)
  210. {
  211. struct rb_node **p = &machine->threads.rb_node;
  212. struct rb_node *parent = NULL;
  213. struct thread *th;
  214. /*
  215. * Front-end cache - TID lookups come in blocks,
  216. * so most of the time we dont have to look up
  217. * the full rbtree:
  218. */
  219. if (machine->last_match && machine->last_match->tid == tid) {
  220. if (pid && pid != machine->last_match->pid_)
  221. machine->last_match->pid_ = pid;
  222. return machine->last_match;
  223. }
  224. while (*p != NULL) {
  225. parent = *p;
  226. th = rb_entry(parent, struct thread, rb_node);
  227. if (th->tid == tid) {
  228. machine->last_match = th;
  229. if (pid && pid != th->pid_)
  230. th->pid_ = pid;
  231. return th;
  232. }
  233. if (tid < th->tid)
  234. p = &(*p)->rb_left;
  235. else
  236. p = &(*p)->rb_right;
  237. }
  238. if (!create)
  239. return NULL;
  240. th = thread__new(pid, tid);
  241. if (th != NULL) {
  242. rb_link_node(&th->rb_node, parent, p);
  243. rb_insert_color(&th->rb_node, &machine->threads);
  244. machine->last_match = th;
  245. }
  246. return th;
  247. }
  248. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
  249. pid_t tid)
  250. {
  251. return __machine__findnew_thread(machine, pid, tid, true);
  252. }
  253. struct thread *machine__find_thread(struct machine *machine, pid_t tid)
  254. {
  255. return __machine__findnew_thread(machine, 0, tid, false);
  256. }
  257. int machine__process_comm_event(struct machine *machine, union perf_event *event)
  258. {
  259. struct thread *thread = machine__findnew_thread(machine,
  260. event->comm.pid,
  261. event->comm.tid);
  262. if (dump_trace)
  263. perf_event__fprintf_comm(event, stdout);
  264. if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
  265. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  266. return -1;
  267. }
  268. return 0;
  269. }
  270. int machine__process_lost_event(struct machine *machine __maybe_unused,
  271. union perf_event *event)
  272. {
  273. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  274. event->lost.id, event->lost.lost);
  275. return 0;
  276. }
  277. struct map *machine__new_module(struct machine *machine, u64 start,
  278. const char *filename)
  279. {
  280. struct map *map;
  281. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  282. if (dso == NULL)
  283. return NULL;
  284. map = map__new2(start, dso, MAP__FUNCTION);
  285. if (map == NULL)
  286. return NULL;
  287. if (machine__is_host(machine))
  288. dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
  289. else
  290. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
  291. map_groups__insert(&machine->kmaps, map);
  292. return map;
  293. }
  294. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
  295. {
  296. struct rb_node *nd;
  297. size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
  298. __dsos__fprintf(&machines->host.user_dsos, fp);
  299. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  300. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  301. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  302. ret += __dsos__fprintf(&pos->user_dsos, fp);
  303. }
  304. return ret;
  305. }
  306. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  307. bool (skip)(struct dso *dso, int parm), int parm)
  308. {
  309. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
  310. __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
  311. }
  312. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  313. bool (skip)(struct dso *dso, int parm), int parm)
  314. {
  315. struct rb_node *nd;
  316. size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
  317. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  318. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  319. ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
  320. }
  321. return ret;
  322. }
  323. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  324. {
  325. int i;
  326. size_t printed = 0;
  327. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  328. if (kdso->has_build_id) {
  329. char filename[PATH_MAX];
  330. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  331. printed += fprintf(fp, "[0] %s\n", filename);
  332. }
  333. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  334. printed += fprintf(fp, "[%d] %s\n",
  335. i + kdso->has_build_id, vmlinux_path[i]);
  336. return printed;
  337. }
  338. size_t machine__fprintf(struct machine *machine, FILE *fp)
  339. {
  340. size_t ret = 0;
  341. struct rb_node *nd;
  342. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  343. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  344. ret += thread__fprintf(pos, fp);
  345. }
  346. return ret;
  347. }
  348. static struct dso *machine__get_kernel(struct machine *machine)
  349. {
  350. const char *vmlinux_name = NULL;
  351. struct dso *kernel;
  352. if (machine__is_host(machine)) {
  353. vmlinux_name = symbol_conf.vmlinux_name;
  354. if (!vmlinux_name)
  355. vmlinux_name = "[kernel.kallsyms]";
  356. kernel = dso__kernel_findnew(machine, vmlinux_name,
  357. "[kernel]",
  358. DSO_TYPE_KERNEL);
  359. } else {
  360. char bf[PATH_MAX];
  361. if (machine__is_default_guest(machine))
  362. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  363. if (!vmlinux_name)
  364. vmlinux_name = machine__mmap_name(machine, bf,
  365. sizeof(bf));
  366. kernel = dso__kernel_findnew(machine, vmlinux_name,
  367. "[guest.kernel]",
  368. DSO_TYPE_GUEST_KERNEL);
  369. }
  370. if (kernel != NULL && (!kernel->has_build_id))
  371. dso__read_running_kernel_build_id(kernel, machine);
  372. return kernel;
  373. }
  374. struct process_args {
  375. u64 start;
  376. };
  377. static int symbol__in_kernel(void *arg, const char *name,
  378. char type __maybe_unused, u64 start)
  379. {
  380. struct process_args *args = arg;
  381. if (strchr(name, '['))
  382. return 0;
  383. args->start = start;
  384. return 1;
  385. }
  386. /* Figure out the start address of kernel map from /proc/kallsyms */
  387. static u64 machine__get_kernel_start_addr(struct machine *machine)
  388. {
  389. const char *filename;
  390. char path[PATH_MAX];
  391. struct process_args args;
  392. if (machine__is_host(machine)) {
  393. filename = "/proc/kallsyms";
  394. } else {
  395. if (machine__is_default_guest(machine))
  396. filename = (char *)symbol_conf.default_guest_kallsyms;
  397. else {
  398. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  399. filename = path;
  400. }
  401. }
  402. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  403. return 0;
  404. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  405. return 0;
  406. return args.start;
  407. }
  408. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  409. {
  410. enum map_type type;
  411. u64 start = machine__get_kernel_start_addr(machine);
  412. for (type = 0; type < MAP__NR_TYPES; ++type) {
  413. struct kmap *kmap;
  414. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  415. if (machine->vmlinux_maps[type] == NULL)
  416. return -1;
  417. machine->vmlinux_maps[type]->map_ip =
  418. machine->vmlinux_maps[type]->unmap_ip =
  419. identity__map_ip;
  420. kmap = map__kmap(machine->vmlinux_maps[type]);
  421. kmap->kmaps = &machine->kmaps;
  422. map_groups__insert(&machine->kmaps,
  423. machine->vmlinux_maps[type]);
  424. }
  425. return 0;
  426. }
  427. void machine__destroy_kernel_maps(struct machine *machine)
  428. {
  429. enum map_type type;
  430. for (type = 0; type < MAP__NR_TYPES; ++type) {
  431. struct kmap *kmap;
  432. if (machine->vmlinux_maps[type] == NULL)
  433. continue;
  434. kmap = map__kmap(machine->vmlinux_maps[type]);
  435. map_groups__remove(&machine->kmaps,
  436. machine->vmlinux_maps[type]);
  437. if (kmap->ref_reloc_sym) {
  438. /*
  439. * ref_reloc_sym is shared among all maps, so free just
  440. * on one of them.
  441. */
  442. if (type == MAP__FUNCTION) {
  443. free((char *)kmap->ref_reloc_sym->name);
  444. kmap->ref_reloc_sym->name = NULL;
  445. free(kmap->ref_reloc_sym);
  446. }
  447. kmap->ref_reloc_sym = NULL;
  448. }
  449. map__delete(machine->vmlinux_maps[type]);
  450. machine->vmlinux_maps[type] = NULL;
  451. }
  452. }
  453. int machines__create_guest_kernel_maps(struct machines *machines)
  454. {
  455. int ret = 0;
  456. struct dirent **namelist = NULL;
  457. int i, items = 0;
  458. char path[PATH_MAX];
  459. pid_t pid;
  460. char *endp;
  461. if (symbol_conf.default_guest_vmlinux_name ||
  462. symbol_conf.default_guest_modules ||
  463. symbol_conf.default_guest_kallsyms) {
  464. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  465. }
  466. if (symbol_conf.guestmount) {
  467. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  468. if (items <= 0)
  469. return -ENOENT;
  470. for (i = 0; i < items; i++) {
  471. if (!isdigit(namelist[i]->d_name[0])) {
  472. /* Filter out . and .. */
  473. continue;
  474. }
  475. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  476. if ((*endp != '\0') ||
  477. (endp == namelist[i]->d_name) ||
  478. (errno == ERANGE)) {
  479. pr_debug("invalid directory (%s). Skipping.\n",
  480. namelist[i]->d_name);
  481. continue;
  482. }
  483. sprintf(path, "%s/%s/proc/kallsyms",
  484. symbol_conf.guestmount,
  485. namelist[i]->d_name);
  486. ret = access(path, R_OK);
  487. if (ret) {
  488. pr_debug("Can't access file %s\n", path);
  489. goto failure;
  490. }
  491. machines__create_kernel_maps(machines, pid);
  492. }
  493. failure:
  494. free(namelist);
  495. }
  496. return ret;
  497. }
  498. void machines__destroy_kernel_maps(struct machines *machines)
  499. {
  500. struct rb_node *next = rb_first(&machines->guests);
  501. machine__destroy_kernel_maps(&machines->host);
  502. while (next) {
  503. struct machine *pos = rb_entry(next, struct machine, rb_node);
  504. next = rb_next(&pos->rb_node);
  505. rb_erase(&pos->rb_node, &machines->guests);
  506. machine__delete(pos);
  507. }
  508. }
  509. int machines__create_kernel_maps(struct machines *machines, pid_t pid)
  510. {
  511. struct machine *machine = machines__findnew(machines, pid);
  512. if (machine == NULL)
  513. return -1;
  514. return machine__create_kernel_maps(machine);
  515. }
  516. int machine__load_kallsyms(struct machine *machine, const char *filename,
  517. enum map_type type, symbol_filter_t filter)
  518. {
  519. struct map *map = machine->vmlinux_maps[type];
  520. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  521. if (ret > 0) {
  522. dso__set_loaded(map->dso, type);
  523. /*
  524. * Since /proc/kallsyms will have multiple sessions for the
  525. * kernel, with modules between them, fixup the end of all
  526. * sections.
  527. */
  528. __map_groups__fixup_end(&machine->kmaps, type);
  529. }
  530. return ret;
  531. }
  532. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  533. symbol_filter_t filter)
  534. {
  535. struct map *map = machine->vmlinux_maps[type];
  536. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  537. if (ret > 0)
  538. dso__set_loaded(map->dso, type);
  539. return ret;
  540. }
  541. static void map_groups__fixup_end(struct map_groups *mg)
  542. {
  543. int i;
  544. for (i = 0; i < MAP__NR_TYPES; ++i)
  545. __map_groups__fixup_end(mg, i);
  546. }
  547. static char *get_kernel_version(const char *root_dir)
  548. {
  549. char version[PATH_MAX];
  550. FILE *file;
  551. char *name, *tmp;
  552. const char *prefix = "Linux version ";
  553. sprintf(version, "%s/proc/version", root_dir);
  554. file = fopen(version, "r");
  555. if (!file)
  556. return NULL;
  557. version[0] = '\0';
  558. tmp = fgets(version, sizeof(version), file);
  559. fclose(file);
  560. name = strstr(version, prefix);
  561. if (!name)
  562. return NULL;
  563. name += strlen(prefix);
  564. tmp = strchr(name, ' ');
  565. if (tmp)
  566. *tmp = '\0';
  567. return strdup(name);
  568. }
  569. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  570. const char *dir_name)
  571. {
  572. struct dirent *dent;
  573. DIR *dir = opendir(dir_name);
  574. int ret = 0;
  575. if (!dir) {
  576. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  577. return -1;
  578. }
  579. while ((dent = readdir(dir)) != NULL) {
  580. char path[PATH_MAX];
  581. struct stat st;
  582. /*sshfs might return bad dent->d_type, so we have to stat*/
  583. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  584. if (stat(path, &st))
  585. continue;
  586. if (S_ISDIR(st.st_mode)) {
  587. if (!strcmp(dent->d_name, ".") ||
  588. !strcmp(dent->d_name, ".."))
  589. continue;
  590. ret = map_groups__set_modules_path_dir(mg, path);
  591. if (ret < 0)
  592. goto out;
  593. } else {
  594. char *dot = strrchr(dent->d_name, '.'),
  595. dso_name[PATH_MAX];
  596. struct map *map;
  597. char *long_name;
  598. if (dot == NULL || strcmp(dot, ".ko"))
  599. continue;
  600. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  601. (int)(dot - dent->d_name), dent->d_name);
  602. strxfrchar(dso_name, '-', '_');
  603. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  604. dso_name);
  605. if (map == NULL)
  606. continue;
  607. long_name = strdup(path);
  608. if (long_name == NULL) {
  609. ret = -1;
  610. goto out;
  611. }
  612. dso__set_long_name(map->dso, long_name);
  613. map->dso->lname_alloc = 1;
  614. dso__kernel_module_get_build_id(map->dso, "");
  615. }
  616. }
  617. out:
  618. closedir(dir);
  619. return ret;
  620. }
  621. static int machine__set_modules_path(struct machine *machine)
  622. {
  623. char *version;
  624. char modules_path[PATH_MAX];
  625. version = get_kernel_version(machine->root_dir);
  626. if (!version)
  627. return -1;
  628. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  629. machine->root_dir, version);
  630. free(version);
  631. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  632. }
  633. static int machine__create_modules(struct machine *machine)
  634. {
  635. char *line = NULL;
  636. size_t n;
  637. FILE *file;
  638. struct map *map;
  639. const char *modules;
  640. char path[PATH_MAX];
  641. if (machine__is_default_guest(machine))
  642. modules = symbol_conf.default_guest_modules;
  643. else {
  644. sprintf(path, "%s/proc/modules", machine->root_dir);
  645. modules = path;
  646. }
  647. if (symbol__restricted_filename(modules, "/proc/modules"))
  648. return -1;
  649. file = fopen(modules, "r");
  650. if (file == NULL)
  651. return -1;
  652. while (!feof(file)) {
  653. char name[PATH_MAX];
  654. u64 start;
  655. char *sep;
  656. int line_len;
  657. line_len = getline(&line, &n, file);
  658. if (line_len < 0)
  659. break;
  660. if (!line)
  661. goto out_failure;
  662. line[--line_len] = '\0'; /* \n */
  663. sep = strrchr(line, 'x');
  664. if (sep == NULL)
  665. continue;
  666. hex2u64(sep + 1, &start);
  667. sep = strchr(line, ' ');
  668. if (sep == NULL)
  669. continue;
  670. *sep = '\0';
  671. snprintf(name, sizeof(name), "[%s]", line);
  672. map = machine__new_module(machine, start, name);
  673. if (map == NULL)
  674. goto out_delete_line;
  675. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  676. }
  677. free(line);
  678. fclose(file);
  679. if (machine__set_modules_path(machine) < 0) {
  680. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  681. }
  682. return 0;
  683. out_delete_line:
  684. free(line);
  685. out_failure:
  686. return -1;
  687. }
  688. int machine__create_kernel_maps(struct machine *machine)
  689. {
  690. struct dso *kernel = machine__get_kernel(machine);
  691. if (kernel == NULL ||
  692. __machine__create_kernel_maps(machine, kernel) < 0)
  693. return -1;
  694. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  695. if (machine__is_host(machine))
  696. pr_debug("Problems creating module maps, "
  697. "continuing anyway...\n");
  698. else
  699. pr_debug("Problems creating module maps for guest %d, "
  700. "continuing anyway...\n", machine->pid);
  701. }
  702. /*
  703. * Now that we have all the maps created, just set the ->end of them:
  704. */
  705. map_groups__fixup_end(&machine->kmaps);
  706. return 0;
  707. }
  708. static void machine__set_kernel_mmap_len(struct machine *machine,
  709. union perf_event *event)
  710. {
  711. int i;
  712. for (i = 0; i < MAP__NR_TYPES; i++) {
  713. machine->vmlinux_maps[i]->start = event->mmap.start;
  714. machine->vmlinux_maps[i]->end = (event->mmap.start +
  715. event->mmap.len);
  716. /*
  717. * Be a bit paranoid here, some perf.data file came with
  718. * a zero sized synthesized MMAP event for the kernel.
  719. */
  720. if (machine->vmlinux_maps[i]->end == 0)
  721. machine->vmlinux_maps[i]->end = ~0ULL;
  722. }
  723. }
  724. static bool machine__uses_kcore(struct machine *machine)
  725. {
  726. struct dso *dso;
  727. list_for_each_entry(dso, &machine->kernel_dsos, node) {
  728. if (dso__is_kcore(dso))
  729. return true;
  730. }
  731. return false;
  732. }
  733. static int machine__process_kernel_mmap_event(struct machine *machine,
  734. union perf_event *event)
  735. {
  736. struct map *map;
  737. char kmmap_prefix[PATH_MAX];
  738. enum dso_kernel_type kernel_type;
  739. bool is_kernel_mmap;
  740. /* If we have maps from kcore then we do not need or want any others */
  741. if (machine__uses_kcore(machine))
  742. return 0;
  743. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  744. if (machine__is_host(machine))
  745. kernel_type = DSO_TYPE_KERNEL;
  746. else
  747. kernel_type = DSO_TYPE_GUEST_KERNEL;
  748. is_kernel_mmap = memcmp(event->mmap.filename,
  749. kmmap_prefix,
  750. strlen(kmmap_prefix) - 1) == 0;
  751. if (event->mmap.filename[0] == '/' ||
  752. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  753. char short_module_name[1024];
  754. char *name, *dot;
  755. if (event->mmap.filename[0] == '/') {
  756. name = strrchr(event->mmap.filename, '/');
  757. if (name == NULL)
  758. goto out_problem;
  759. ++name; /* skip / */
  760. dot = strrchr(name, '.');
  761. if (dot == NULL)
  762. goto out_problem;
  763. snprintf(short_module_name, sizeof(short_module_name),
  764. "[%.*s]", (int)(dot - name), name);
  765. strxfrchar(short_module_name, '-', '_');
  766. } else
  767. strcpy(short_module_name, event->mmap.filename);
  768. map = machine__new_module(machine, event->mmap.start,
  769. event->mmap.filename);
  770. if (map == NULL)
  771. goto out_problem;
  772. name = strdup(short_module_name);
  773. if (name == NULL)
  774. goto out_problem;
  775. map->dso->short_name = name;
  776. map->dso->sname_alloc = 1;
  777. map->end = map->start + event->mmap.len;
  778. } else if (is_kernel_mmap) {
  779. const char *symbol_name = (event->mmap.filename +
  780. strlen(kmmap_prefix));
  781. /*
  782. * Should be there already, from the build-id table in
  783. * the header.
  784. */
  785. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  786. kmmap_prefix);
  787. if (kernel == NULL)
  788. goto out_problem;
  789. kernel->kernel = kernel_type;
  790. if (__machine__create_kernel_maps(machine, kernel) < 0)
  791. goto out_problem;
  792. machine__set_kernel_mmap_len(machine, event);
  793. /*
  794. * Avoid using a zero address (kptr_restrict) for the ref reloc
  795. * symbol. Effectively having zero here means that at record
  796. * time /proc/sys/kernel/kptr_restrict was non zero.
  797. */
  798. if (event->mmap.pgoff != 0) {
  799. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  800. symbol_name,
  801. event->mmap.pgoff);
  802. }
  803. if (machine__is_default_guest(machine)) {
  804. /*
  805. * preload dso of guest kernel and modules
  806. */
  807. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  808. NULL);
  809. }
  810. }
  811. return 0;
  812. out_problem:
  813. return -1;
  814. }
  815. int machine__process_mmap2_event(struct machine *machine,
  816. union perf_event *event)
  817. {
  818. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  819. struct thread *thread;
  820. struct map *map;
  821. enum map_type type;
  822. int ret = 0;
  823. if (dump_trace)
  824. perf_event__fprintf_mmap2(event, stdout);
  825. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  826. cpumode == PERF_RECORD_MISC_KERNEL) {
  827. ret = machine__process_kernel_mmap_event(machine, event);
  828. if (ret < 0)
  829. goto out_problem;
  830. return 0;
  831. }
  832. thread = machine__findnew_thread(machine, event->mmap2.pid,
  833. event->mmap2.pid);
  834. if (thread == NULL)
  835. goto out_problem;
  836. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  837. type = MAP__VARIABLE;
  838. else
  839. type = MAP__FUNCTION;
  840. map = map__new(&machine->user_dsos, event->mmap2.start,
  841. event->mmap2.len, event->mmap2.pgoff,
  842. event->mmap2.pid, event->mmap2.maj,
  843. event->mmap2.min, event->mmap2.ino,
  844. event->mmap2.ino_generation,
  845. event->mmap2.filename, type);
  846. if (map == NULL)
  847. goto out_problem;
  848. thread__insert_map(thread, map);
  849. return 0;
  850. out_problem:
  851. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  852. return 0;
  853. }
  854. int machine__process_mmap_event(struct machine *machine, union perf_event *event)
  855. {
  856. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  857. struct thread *thread;
  858. struct map *map;
  859. enum map_type type;
  860. int ret = 0;
  861. if (dump_trace)
  862. perf_event__fprintf_mmap(event, stdout);
  863. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  864. cpumode == PERF_RECORD_MISC_KERNEL) {
  865. ret = machine__process_kernel_mmap_event(machine, event);
  866. if (ret < 0)
  867. goto out_problem;
  868. return 0;
  869. }
  870. thread = machine__findnew_thread(machine, event->mmap.pid,
  871. event->mmap.pid);
  872. if (thread == NULL)
  873. goto out_problem;
  874. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  875. type = MAP__VARIABLE;
  876. else
  877. type = MAP__FUNCTION;
  878. map = map__new(&machine->user_dsos, event->mmap.start,
  879. event->mmap.len, event->mmap.pgoff,
  880. event->mmap.pid, 0, 0, 0, 0,
  881. event->mmap.filename,
  882. type);
  883. if (map == NULL)
  884. goto out_problem;
  885. thread__insert_map(thread, map);
  886. return 0;
  887. out_problem:
  888. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  889. return 0;
  890. }
  891. static void machine__remove_thread(struct machine *machine, struct thread *th)
  892. {
  893. machine->last_match = NULL;
  894. rb_erase(&th->rb_node, &machine->threads);
  895. /*
  896. * We may have references to this thread, for instance in some hist_entry
  897. * instances, so just move them to a separate list.
  898. */
  899. list_add_tail(&th->node, &machine->dead_threads);
  900. }
  901. int machine__process_fork_event(struct machine *machine, union perf_event *event)
  902. {
  903. struct thread *thread = machine__find_thread(machine, event->fork.tid);
  904. struct thread *parent = machine__findnew_thread(machine,
  905. event->fork.ppid,
  906. event->fork.ptid);
  907. /* if a thread currently exists for the thread id remove it */
  908. if (thread != NULL)
  909. machine__remove_thread(machine, thread);
  910. thread = machine__findnew_thread(machine, event->fork.pid,
  911. event->fork.tid);
  912. if (dump_trace)
  913. perf_event__fprintf_task(event, stdout);
  914. if (thread == NULL || parent == NULL ||
  915. thread__fork(thread, parent) < 0) {
  916. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  917. return -1;
  918. }
  919. return 0;
  920. }
  921. int machine__process_exit_event(struct machine *machine __maybe_unused,
  922. union perf_event *event)
  923. {
  924. struct thread *thread = machine__find_thread(machine, event->fork.tid);
  925. if (dump_trace)
  926. perf_event__fprintf_task(event, stdout);
  927. if (thread != NULL)
  928. thread__exited(thread);
  929. return 0;
  930. }
  931. int machine__process_event(struct machine *machine, union perf_event *event)
  932. {
  933. int ret;
  934. switch (event->header.type) {
  935. case PERF_RECORD_COMM:
  936. ret = machine__process_comm_event(machine, event); break;
  937. case PERF_RECORD_MMAP:
  938. ret = machine__process_mmap_event(machine, event); break;
  939. case PERF_RECORD_MMAP2:
  940. ret = machine__process_mmap2_event(machine, event); break;
  941. case PERF_RECORD_FORK:
  942. ret = machine__process_fork_event(machine, event); break;
  943. case PERF_RECORD_EXIT:
  944. ret = machine__process_exit_event(machine, event); break;
  945. case PERF_RECORD_LOST:
  946. ret = machine__process_lost_event(machine, event); break;
  947. default:
  948. ret = -1;
  949. break;
  950. }
  951. return ret;
  952. }
  953. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  954. {
  955. if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
  956. return 1;
  957. return 0;
  958. }
  959. static const u8 cpumodes[] = {
  960. PERF_RECORD_MISC_USER,
  961. PERF_RECORD_MISC_KERNEL,
  962. PERF_RECORD_MISC_GUEST_USER,
  963. PERF_RECORD_MISC_GUEST_KERNEL
  964. };
  965. #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
  966. static void ip__resolve_ams(struct machine *machine, struct thread *thread,
  967. struct addr_map_symbol *ams,
  968. u64 ip)
  969. {
  970. struct addr_location al;
  971. size_t i;
  972. u8 m;
  973. memset(&al, 0, sizeof(al));
  974. for (i = 0; i < NCPUMODES; i++) {
  975. m = cpumodes[i];
  976. /*
  977. * We cannot use the header.misc hint to determine whether a
  978. * branch stack address is user, kernel, guest, hypervisor.
  979. * Branches may straddle the kernel/user/hypervisor boundaries.
  980. * Thus, we have to try consecutively until we find a match
  981. * or else, the symbol is unknown
  982. */
  983. thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
  984. ip, &al);
  985. if (al.sym)
  986. goto found;
  987. }
  988. found:
  989. ams->addr = ip;
  990. ams->al_addr = al.addr;
  991. ams->sym = al.sym;
  992. ams->map = al.map;
  993. }
  994. static void ip__resolve_data(struct machine *machine, struct thread *thread,
  995. u8 m, struct addr_map_symbol *ams, u64 addr)
  996. {
  997. struct addr_location al;
  998. memset(&al, 0, sizeof(al));
  999. thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
  1000. &al);
  1001. ams->addr = addr;
  1002. ams->al_addr = al.addr;
  1003. ams->sym = al.sym;
  1004. ams->map = al.map;
  1005. }
  1006. struct mem_info *machine__resolve_mem(struct machine *machine,
  1007. struct thread *thr,
  1008. struct perf_sample *sample,
  1009. u8 cpumode)
  1010. {
  1011. struct mem_info *mi = zalloc(sizeof(*mi));
  1012. if (!mi)
  1013. return NULL;
  1014. ip__resolve_ams(machine, thr, &mi->iaddr, sample->ip);
  1015. ip__resolve_data(machine, thr, cpumode, &mi->daddr, sample->addr);
  1016. mi->data_src.val = sample->data_src;
  1017. return mi;
  1018. }
  1019. struct branch_info *machine__resolve_bstack(struct machine *machine,
  1020. struct thread *thr,
  1021. struct branch_stack *bs)
  1022. {
  1023. struct branch_info *bi;
  1024. unsigned int i;
  1025. bi = calloc(bs->nr, sizeof(struct branch_info));
  1026. if (!bi)
  1027. return NULL;
  1028. for (i = 0; i < bs->nr; i++) {
  1029. ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to);
  1030. ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from);
  1031. bi[i].flags = bs->entries[i].flags;
  1032. }
  1033. return bi;
  1034. }
  1035. static int machine__resolve_callchain_sample(struct machine *machine,
  1036. struct thread *thread,
  1037. struct ip_callchain *chain,
  1038. struct symbol **parent,
  1039. struct addr_location *root_al)
  1040. {
  1041. u8 cpumode = PERF_RECORD_MISC_USER;
  1042. unsigned int i;
  1043. int err;
  1044. callchain_cursor_reset(&callchain_cursor);
  1045. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  1046. pr_warning("corrupted callchain. skipping...\n");
  1047. return 0;
  1048. }
  1049. for (i = 0; i < chain->nr; i++) {
  1050. u64 ip;
  1051. struct addr_location al;
  1052. if (callchain_param.order == ORDER_CALLEE)
  1053. ip = chain->ips[i];
  1054. else
  1055. ip = chain->ips[chain->nr - i - 1];
  1056. if (ip >= PERF_CONTEXT_MAX) {
  1057. switch (ip) {
  1058. case PERF_CONTEXT_HV:
  1059. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1060. break;
  1061. case PERF_CONTEXT_KERNEL:
  1062. cpumode = PERF_RECORD_MISC_KERNEL;
  1063. break;
  1064. case PERF_CONTEXT_USER:
  1065. cpumode = PERF_RECORD_MISC_USER;
  1066. break;
  1067. default:
  1068. pr_debug("invalid callchain context: "
  1069. "%"PRId64"\n", (s64) ip);
  1070. /*
  1071. * It seems the callchain is corrupted.
  1072. * Discard all.
  1073. */
  1074. callchain_cursor_reset(&callchain_cursor);
  1075. return 0;
  1076. }
  1077. continue;
  1078. }
  1079. al.filtered = false;
  1080. thread__find_addr_location(thread, machine, cpumode,
  1081. MAP__FUNCTION, ip, &al);
  1082. if (al.sym != NULL) {
  1083. if (sort__has_parent && !*parent &&
  1084. symbol__match_regex(al.sym, &parent_regex))
  1085. *parent = al.sym;
  1086. else if (have_ignore_callees && root_al &&
  1087. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1088. /* Treat this symbol as the root,
  1089. forgetting its callees. */
  1090. *root_al = al;
  1091. callchain_cursor_reset(&callchain_cursor);
  1092. }
  1093. if (!symbol_conf.use_callchain)
  1094. break;
  1095. }
  1096. err = callchain_cursor_append(&callchain_cursor,
  1097. ip, al.map, al.sym);
  1098. if (err)
  1099. return err;
  1100. }
  1101. return 0;
  1102. }
  1103. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1104. {
  1105. struct callchain_cursor *cursor = arg;
  1106. return callchain_cursor_append(cursor, entry->ip,
  1107. entry->map, entry->sym);
  1108. }
  1109. int machine__resolve_callchain(struct machine *machine,
  1110. struct perf_evsel *evsel,
  1111. struct thread *thread,
  1112. struct perf_sample *sample,
  1113. struct symbol **parent,
  1114. struct addr_location *root_al)
  1115. {
  1116. int ret;
  1117. ret = machine__resolve_callchain_sample(machine, thread,
  1118. sample->callchain, parent, root_al);
  1119. if (ret)
  1120. return ret;
  1121. /* Can we do dwarf post unwind? */
  1122. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1123. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1124. return 0;
  1125. /* Bail out if nothing was captured. */
  1126. if ((!sample->user_regs.regs) ||
  1127. (!sample->user_stack.size))
  1128. return 0;
  1129. return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
  1130. thread, evsel->attr.sample_regs_user,
  1131. sample);
  1132. }