machine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #include "debug.h"
  2. #include "event.h"
  3. #include "machine.h"
  4. #include "map.h"
  5. #include "strlist.h"
  6. #include "thread.h"
  7. #include <stdbool.h>
  8. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  9. {
  10. map_groups__init(&machine->kmaps);
  11. RB_CLEAR_NODE(&machine->rb_node);
  12. INIT_LIST_HEAD(&machine->user_dsos);
  13. INIT_LIST_HEAD(&machine->kernel_dsos);
  14. machine->threads = RB_ROOT;
  15. INIT_LIST_HEAD(&machine->dead_threads);
  16. machine->last_match = NULL;
  17. machine->kmaps.machine = machine;
  18. machine->pid = pid;
  19. machine->root_dir = strdup(root_dir);
  20. if (machine->root_dir == NULL)
  21. return -ENOMEM;
  22. if (pid != HOST_KERNEL_ID) {
  23. struct thread *thread = machine__findnew_thread(machine, pid);
  24. char comm[64];
  25. if (thread == NULL)
  26. return -ENOMEM;
  27. snprintf(comm, sizeof(comm), "[guest/%d]", pid);
  28. thread__set_comm(thread, comm);
  29. }
  30. return 0;
  31. }
  32. static void dsos__delete(struct list_head *dsos)
  33. {
  34. struct dso *pos, *n;
  35. list_for_each_entry_safe(pos, n, dsos, node) {
  36. list_del(&pos->node);
  37. dso__delete(pos);
  38. }
  39. }
  40. void machine__exit(struct machine *machine)
  41. {
  42. map_groups__exit(&machine->kmaps);
  43. dsos__delete(&machine->user_dsos);
  44. dsos__delete(&machine->kernel_dsos);
  45. free(machine->root_dir);
  46. machine->root_dir = NULL;
  47. }
  48. void machine__delete(struct machine *machine)
  49. {
  50. machine__exit(machine);
  51. free(machine);
  52. }
  53. struct machine *machines__add(struct rb_root *machines, pid_t pid,
  54. const char *root_dir)
  55. {
  56. struct rb_node **p = &machines->rb_node;
  57. struct rb_node *parent = NULL;
  58. struct machine *pos, *machine = malloc(sizeof(*machine));
  59. if (machine == NULL)
  60. return NULL;
  61. if (machine__init(machine, root_dir, pid) != 0) {
  62. free(machine);
  63. return NULL;
  64. }
  65. while (*p != NULL) {
  66. parent = *p;
  67. pos = rb_entry(parent, struct machine, rb_node);
  68. if (pid < pos->pid)
  69. p = &(*p)->rb_left;
  70. else
  71. p = &(*p)->rb_right;
  72. }
  73. rb_link_node(&machine->rb_node, parent, p);
  74. rb_insert_color(&machine->rb_node, machines);
  75. return machine;
  76. }
  77. struct machine *machines__find(struct rb_root *machines, pid_t pid)
  78. {
  79. struct rb_node **p = &machines->rb_node;
  80. struct rb_node *parent = NULL;
  81. struct machine *machine;
  82. struct machine *default_machine = NULL;
  83. while (*p != NULL) {
  84. parent = *p;
  85. machine = rb_entry(parent, struct machine, rb_node);
  86. if (pid < machine->pid)
  87. p = &(*p)->rb_left;
  88. else if (pid > machine->pid)
  89. p = &(*p)->rb_right;
  90. else
  91. return machine;
  92. if (!machine->pid)
  93. default_machine = machine;
  94. }
  95. return default_machine;
  96. }
  97. struct machine *machines__findnew(struct rb_root *machines, pid_t pid)
  98. {
  99. char path[PATH_MAX];
  100. const char *root_dir = "";
  101. struct machine *machine = machines__find(machines, pid);
  102. if (machine && (machine->pid == pid))
  103. goto out;
  104. if ((pid != HOST_KERNEL_ID) &&
  105. (pid != DEFAULT_GUEST_KERNEL_ID) &&
  106. (symbol_conf.guestmount)) {
  107. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  108. if (access(path, R_OK)) {
  109. static struct strlist *seen;
  110. if (!seen)
  111. seen = strlist__new(true, NULL);
  112. if (!strlist__has_entry(seen, path)) {
  113. pr_err("Can't access file %s\n", path);
  114. strlist__add(seen, path);
  115. }
  116. machine = NULL;
  117. goto out;
  118. }
  119. root_dir = path;
  120. }
  121. machine = machines__add(machines, pid, root_dir);
  122. out:
  123. return machine;
  124. }
  125. void machines__process(struct rb_root *machines,
  126. machine__process_t process, void *data)
  127. {
  128. struct rb_node *nd;
  129. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  130. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  131. process(pos, data);
  132. }
  133. }
  134. char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
  135. {
  136. if (machine__is_host(machine))
  137. snprintf(bf, size, "[%s]", "kernel.kallsyms");
  138. else if (machine__is_default_guest(machine))
  139. snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
  140. else {
  141. snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
  142. machine->pid);
  143. }
  144. return bf;
  145. }
  146. void machines__set_id_hdr_size(struct rb_root *machines, u16 id_hdr_size)
  147. {
  148. struct rb_node *node;
  149. struct machine *machine;
  150. for (node = rb_first(machines); node; node = rb_next(node)) {
  151. machine = rb_entry(node, struct machine, rb_node);
  152. machine->id_hdr_size = id_hdr_size;
  153. }
  154. return;
  155. }
  156. static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid,
  157. bool create)
  158. {
  159. struct rb_node **p = &machine->threads.rb_node;
  160. struct rb_node *parent = NULL;
  161. struct thread *th;
  162. /*
  163. * Font-end cache - PID lookups come in blocks,
  164. * so most of the time we dont have to look up
  165. * the full rbtree:
  166. */
  167. if (machine->last_match && machine->last_match->pid == pid)
  168. return machine->last_match;
  169. while (*p != NULL) {
  170. parent = *p;
  171. th = rb_entry(parent, struct thread, rb_node);
  172. if (th->pid == pid) {
  173. machine->last_match = th;
  174. return th;
  175. }
  176. if (pid < th->pid)
  177. p = &(*p)->rb_left;
  178. else
  179. p = &(*p)->rb_right;
  180. }
  181. if (!create)
  182. return NULL;
  183. th = thread__new(pid);
  184. if (th != NULL) {
  185. rb_link_node(&th->rb_node, parent, p);
  186. rb_insert_color(&th->rb_node, &machine->threads);
  187. machine->last_match = th;
  188. }
  189. return th;
  190. }
  191. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid)
  192. {
  193. return __machine__findnew_thread(machine, pid, true);
  194. }
  195. struct thread *machine__find_thread(struct machine *machine, pid_t pid)
  196. {
  197. return __machine__findnew_thread(machine, pid, false);
  198. }
  199. int machine__process_comm_event(struct machine *machine, union perf_event *event)
  200. {
  201. struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
  202. if (dump_trace)
  203. perf_event__fprintf_comm(event, stdout);
  204. if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
  205. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. int machine__process_lost_event(struct machine *machine __maybe_unused,
  211. union perf_event *event)
  212. {
  213. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  214. event->lost.id, event->lost.lost);
  215. return 0;
  216. }
  217. static void machine__set_kernel_mmap_len(struct machine *machine,
  218. union perf_event *event)
  219. {
  220. int i;
  221. for (i = 0; i < MAP__NR_TYPES; i++) {
  222. machine->vmlinux_maps[i]->start = event->mmap.start;
  223. machine->vmlinux_maps[i]->end = (event->mmap.start +
  224. event->mmap.len);
  225. /*
  226. * Be a bit paranoid here, some perf.data file came with
  227. * a zero sized synthesized MMAP event for the kernel.
  228. */
  229. if (machine->vmlinux_maps[i]->end == 0)
  230. machine->vmlinux_maps[i]->end = ~0ULL;
  231. }
  232. }
  233. static int machine__process_kernel_mmap_event(struct machine *machine,
  234. union perf_event *event)
  235. {
  236. struct map *map;
  237. char kmmap_prefix[PATH_MAX];
  238. enum dso_kernel_type kernel_type;
  239. bool is_kernel_mmap;
  240. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  241. if (machine__is_host(machine))
  242. kernel_type = DSO_TYPE_KERNEL;
  243. else
  244. kernel_type = DSO_TYPE_GUEST_KERNEL;
  245. is_kernel_mmap = memcmp(event->mmap.filename,
  246. kmmap_prefix,
  247. strlen(kmmap_prefix) - 1) == 0;
  248. if (event->mmap.filename[0] == '/' ||
  249. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  250. char short_module_name[1024];
  251. char *name, *dot;
  252. if (event->mmap.filename[0] == '/') {
  253. name = strrchr(event->mmap.filename, '/');
  254. if (name == NULL)
  255. goto out_problem;
  256. ++name; /* skip / */
  257. dot = strrchr(name, '.');
  258. if (dot == NULL)
  259. goto out_problem;
  260. snprintf(short_module_name, sizeof(short_module_name),
  261. "[%.*s]", (int)(dot - name), name);
  262. strxfrchar(short_module_name, '-', '_');
  263. } else
  264. strcpy(short_module_name, event->mmap.filename);
  265. map = machine__new_module(machine, event->mmap.start,
  266. event->mmap.filename);
  267. if (map == NULL)
  268. goto out_problem;
  269. name = strdup(short_module_name);
  270. if (name == NULL)
  271. goto out_problem;
  272. map->dso->short_name = name;
  273. map->dso->sname_alloc = 1;
  274. map->end = map->start + event->mmap.len;
  275. } else if (is_kernel_mmap) {
  276. const char *symbol_name = (event->mmap.filename +
  277. strlen(kmmap_prefix));
  278. /*
  279. * Should be there already, from the build-id table in
  280. * the header.
  281. */
  282. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  283. kmmap_prefix);
  284. if (kernel == NULL)
  285. goto out_problem;
  286. kernel->kernel = kernel_type;
  287. if (__machine__create_kernel_maps(machine, kernel) < 0)
  288. goto out_problem;
  289. machine__set_kernel_mmap_len(machine, event);
  290. /*
  291. * Avoid using a zero address (kptr_restrict) for the ref reloc
  292. * symbol. Effectively having zero here means that at record
  293. * time /proc/sys/kernel/kptr_restrict was non zero.
  294. */
  295. if (event->mmap.pgoff != 0) {
  296. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  297. symbol_name,
  298. event->mmap.pgoff);
  299. }
  300. if (machine__is_default_guest(machine)) {
  301. /*
  302. * preload dso of guest kernel and modules
  303. */
  304. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  305. NULL);
  306. }
  307. }
  308. return 0;
  309. out_problem:
  310. return -1;
  311. }
  312. int machine__process_mmap_event(struct machine *machine, union perf_event *event)
  313. {
  314. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  315. struct thread *thread;
  316. struct map *map;
  317. int ret = 0;
  318. if (dump_trace)
  319. perf_event__fprintf_mmap(event, stdout);
  320. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  321. cpumode == PERF_RECORD_MISC_KERNEL) {
  322. ret = machine__process_kernel_mmap_event(machine, event);
  323. if (ret < 0)
  324. goto out_problem;
  325. return 0;
  326. }
  327. thread = machine__findnew_thread(machine, event->mmap.pid);
  328. if (thread == NULL)
  329. goto out_problem;
  330. map = map__new(&machine->user_dsos, event->mmap.start,
  331. event->mmap.len, event->mmap.pgoff,
  332. event->mmap.pid, event->mmap.filename,
  333. MAP__FUNCTION);
  334. if (map == NULL)
  335. goto out_problem;
  336. thread__insert_map(thread, map);
  337. return 0;
  338. out_problem:
  339. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  340. return 0;
  341. }
  342. int machine__process_fork_event(struct machine *machine, union perf_event *event)
  343. {
  344. struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
  345. struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
  346. if (dump_trace)
  347. perf_event__fprintf_task(event, stdout);
  348. if (thread == NULL || parent == NULL ||
  349. thread__fork(thread, parent) < 0) {
  350. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  351. return -1;
  352. }
  353. return 0;
  354. }
  355. int machine__process_exit_event(struct machine *machine, union perf_event *event)
  356. {
  357. struct thread *thread = machine__find_thread(machine, event->fork.tid);
  358. if (dump_trace)
  359. perf_event__fprintf_task(event, stdout);
  360. if (thread != NULL)
  361. machine__remove_thread(machine, thread);
  362. return 0;
  363. }
  364. int machine__process_event(struct machine *machine, union perf_event *event)
  365. {
  366. int ret;
  367. switch (event->header.type) {
  368. case PERF_RECORD_COMM:
  369. ret = machine__process_comm_event(machine, event); break;
  370. case PERF_RECORD_MMAP:
  371. ret = machine__process_mmap_event(machine, event); break;
  372. case PERF_RECORD_FORK:
  373. ret = machine__process_fork_event(machine, event); break;
  374. case PERF_RECORD_EXIT:
  375. ret = machine__process_exit_event(machine, event); break;
  376. case PERF_RECORD_LOST:
  377. ret = machine__process_lost_event(machine, event); break;
  378. default:
  379. ret = -1;
  380. break;
  381. }
  382. return ret;
  383. }