event.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. #include <linux/types.h>
  2. #include "event.h"
  3. #include "debug.h"
  4. #include "sort.h"
  5. #include "string.h"
  6. #include "strlist.h"
  7. #include "thread.h"
  8. #include "thread_map.h"
  9. static const char *perf_event__names[] = {
  10. [0] = "TOTAL",
  11. [PERF_RECORD_MMAP] = "MMAP",
  12. [PERF_RECORD_LOST] = "LOST",
  13. [PERF_RECORD_COMM] = "COMM",
  14. [PERF_RECORD_EXIT] = "EXIT",
  15. [PERF_RECORD_THROTTLE] = "THROTTLE",
  16. [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
  17. [PERF_RECORD_FORK] = "FORK",
  18. [PERF_RECORD_READ] = "READ",
  19. [PERF_RECORD_SAMPLE] = "SAMPLE",
  20. [PERF_RECORD_HEADER_ATTR] = "ATTR",
  21. [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
  22. [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
  23. [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
  24. [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
  25. };
  26. const char *perf_event__name(unsigned int id)
  27. {
  28. if (id >= ARRAY_SIZE(perf_event__names))
  29. return "INVALID";
  30. if (!perf_event__names[id])
  31. return "UNKNOWN";
  32. return perf_event__names[id];
  33. }
  34. static struct perf_sample synth_sample = {
  35. .pid = -1,
  36. .tid = -1,
  37. .time = -1,
  38. .stream_id = -1,
  39. .cpu = -1,
  40. .period = 1,
  41. };
  42. static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
  43. union perf_event *event, pid_t pid,
  44. int full, perf_event__handler_t process,
  45. struct machine *machine)
  46. {
  47. char filename[PATH_MAX];
  48. char bf[BUFSIZ];
  49. FILE *fp;
  50. size_t size = 0;
  51. DIR *tasks;
  52. struct dirent dirent, *next;
  53. pid_t tgid = 0;
  54. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  55. fp = fopen(filename, "r");
  56. if (fp == NULL) {
  57. out_race:
  58. /*
  59. * We raced with a task exiting - just return:
  60. */
  61. pr_debug("couldn't open %s\n", filename);
  62. return 0;
  63. }
  64. memset(&event->comm, 0, sizeof(event->comm));
  65. while (!event->comm.comm[0] || !event->comm.pid) {
  66. if (fgets(bf, sizeof(bf), fp) == NULL) {
  67. pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
  68. goto out;
  69. }
  70. if (memcmp(bf, "Name:", 5) == 0) {
  71. char *name = bf + 5;
  72. while (*name && isspace(*name))
  73. ++name;
  74. size = strlen(name) - 1;
  75. memcpy(event->comm.comm, name, size++);
  76. } else if (memcmp(bf, "Tgid:", 5) == 0) {
  77. char *tgids = bf + 5;
  78. while (*tgids && isspace(*tgids))
  79. ++tgids;
  80. tgid = event->comm.pid = atoi(tgids);
  81. }
  82. }
  83. event->comm.header.type = PERF_RECORD_COMM;
  84. size = ALIGN(size, sizeof(u64));
  85. memset(event->comm.comm + size, 0, machine->id_hdr_size);
  86. event->comm.header.size = (sizeof(event->comm) -
  87. (sizeof(event->comm.comm) - size) +
  88. machine->id_hdr_size);
  89. if (!full) {
  90. event->comm.tid = pid;
  91. process(tool, event, &synth_sample, machine);
  92. goto out;
  93. }
  94. snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
  95. tasks = opendir(filename);
  96. if (tasks == NULL)
  97. goto out_race;
  98. while (!readdir_r(tasks, &dirent, &next) && next) {
  99. char *end;
  100. pid = strtol(dirent.d_name, &end, 10);
  101. if (*end)
  102. continue;
  103. event->comm.tid = pid;
  104. process(tool, event, &synth_sample, machine);
  105. }
  106. closedir(tasks);
  107. out:
  108. fclose(fp);
  109. return tgid;
  110. }
  111. static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
  112. union perf_event *event,
  113. pid_t pid, pid_t tgid,
  114. perf_event__handler_t process,
  115. struct machine *machine)
  116. {
  117. char filename[PATH_MAX];
  118. FILE *fp;
  119. snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
  120. fp = fopen(filename, "r");
  121. if (fp == NULL) {
  122. /*
  123. * We raced with a task exiting - just return:
  124. */
  125. pr_debug("couldn't open %s\n", filename);
  126. return -1;
  127. }
  128. event->header.type = PERF_RECORD_MMAP;
  129. /*
  130. * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
  131. */
  132. event->header.misc = PERF_RECORD_MISC_USER;
  133. while (1) {
  134. char bf[BUFSIZ], *pbf = bf;
  135. int n;
  136. size_t size;
  137. if (fgets(bf, sizeof(bf), fp) == NULL)
  138. break;
  139. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  140. n = hex2u64(pbf, &event->mmap.start);
  141. if (n < 0)
  142. continue;
  143. pbf += n + 1;
  144. n = hex2u64(pbf, &event->mmap.len);
  145. if (n < 0)
  146. continue;
  147. pbf += n + 3;
  148. if (*pbf == 'x') { /* vm_exec */
  149. char anonstr[] = "//anon\n";
  150. char *execname = strchr(bf, '/');
  151. /* Catch VDSO */
  152. if (execname == NULL)
  153. execname = strstr(bf, "[vdso]");
  154. /* Catch anonymous mmaps */
  155. if ((execname == NULL) && !strstr(bf, "["))
  156. execname = anonstr;
  157. if (execname == NULL)
  158. continue;
  159. pbf += 3;
  160. n = hex2u64(pbf, &event->mmap.pgoff);
  161. size = strlen(execname);
  162. execname[size - 1] = '\0'; /* Remove \n */
  163. memcpy(event->mmap.filename, execname, size);
  164. size = ALIGN(size, sizeof(u64));
  165. event->mmap.len -= event->mmap.start;
  166. event->mmap.header.size = (sizeof(event->mmap) -
  167. (sizeof(event->mmap.filename) - size));
  168. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  169. event->mmap.header.size += machine->id_hdr_size;
  170. event->mmap.pid = tgid;
  171. event->mmap.tid = pid;
  172. process(tool, event, &synth_sample, machine);
  173. }
  174. }
  175. fclose(fp);
  176. return 0;
  177. }
  178. int perf_event__synthesize_modules(struct perf_tool *tool,
  179. perf_event__handler_t process,
  180. struct machine *machine)
  181. {
  182. struct rb_node *nd;
  183. struct map_groups *kmaps = &machine->kmaps;
  184. union perf_event *event = zalloc((sizeof(event->mmap) +
  185. machine->id_hdr_size));
  186. if (event == NULL) {
  187. pr_debug("Not enough memory synthesizing mmap event "
  188. "for kernel modules\n");
  189. return -1;
  190. }
  191. event->header.type = PERF_RECORD_MMAP;
  192. /*
  193. * kernel uses 0 for user space maps, see kernel/perf_event.c
  194. * __perf_event_mmap
  195. */
  196. if (machine__is_host(machine))
  197. event->header.misc = PERF_RECORD_MISC_KERNEL;
  198. else
  199. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  200. for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
  201. nd; nd = rb_next(nd)) {
  202. size_t size;
  203. struct map *pos = rb_entry(nd, struct map, rb_node);
  204. if (pos->dso->kernel)
  205. continue;
  206. size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  207. event->mmap.header.type = PERF_RECORD_MMAP;
  208. event->mmap.header.size = (sizeof(event->mmap) -
  209. (sizeof(event->mmap.filename) - size));
  210. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  211. event->mmap.header.size += machine->id_hdr_size;
  212. event->mmap.start = pos->start;
  213. event->mmap.len = pos->end - pos->start;
  214. event->mmap.pid = machine->pid;
  215. memcpy(event->mmap.filename, pos->dso->long_name,
  216. pos->dso->long_name_len + 1);
  217. process(tool, event, &synth_sample, machine);
  218. }
  219. free(event);
  220. return 0;
  221. }
  222. static int __event__synthesize_thread(union perf_event *comm_event,
  223. union perf_event *mmap_event,
  224. pid_t pid, int full,
  225. perf_event__handler_t process,
  226. struct perf_tool *tool,
  227. struct machine *machine)
  228. {
  229. pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
  230. process, machine);
  231. if (tgid == -1)
  232. return -1;
  233. return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  234. process, machine);
  235. }
  236. int perf_event__synthesize_thread_map(struct perf_tool *tool,
  237. struct thread_map *threads,
  238. perf_event__handler_t process,
  239. struct machine *machine)
  240. {
  241. union perf_event *comm_event, *mmap_event;
  242. int err = -1, thread, j;
  243. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  244. if (comm_event == NULL)
  245. goto out;
  246. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  247. if (mmap_event == NULL)
  248. goto out_free_comm;
  249. err = 0;
  250. for (thread = 0; thread < threads->nr; ++thread) {
  251. if (__event__synthesize_thread(comm_event, mmap_event,
  252. threads->map[thread], 0,
  253. process, tool, machine)) {
  254. err = -1;
  255. break;
  256. }
  257. /*
  258. * comm.pid is set to thread group id by
  259. * perf_event__synthesize_comm
  260. */
  261. if ((int) comm_event->comm.pid != threads->map[thread]) {
  262. bool need_leader = true;
  263. /* is thread group leader in thread_map? */
  264. for (j = 0; j < threads->nr; ++j) {
  265. if ((int) comm_event->comm.pid == threads->map[j]) {
  266. need_leader = false;
  267. break;
  268. }
  269. }
  270. /* if not, generate events for it */
  271. if (need_leader &&
  272. __event__synthesize_thread(comm_event,
  273. mmap_event,
  274. comm_event->comm.pid, 0,
  275. process, tool, machine)) {
  276. err = -1;
  277. break;
  278. }
  279. }
  280. }
  281. free(mmap_event);
  282. out_free_comm:
  283. free(comm_event);
  284. out:
  285. return err;
  286. }
  287. int perf_event__synthesize_threads(struct perf_tool *tool,
  288. perf_event__handler_t process,
  289. struct machine *machine)
  290. {
  291. DIR *proc;
  292. struct dirent dirent, *next;
  293. union perf_event *comm_event, *mmap_event;
  294. int err = -1;
  295. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  296. if (comm_event == NULL)
  297. goto out;
  298. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  299. if (mmap_event == NULL)
  300. goto out_free_comm;
  301. proc = opendir("/proc");
  302. if (proc == NULL)
  303. goto out_free_mmap;
  304. while (!readdir_r(proc, &dirent, &next) && next) {
  305. char *end;
  306. pid_t pid = strtol(dirent.d_name, &end, 10);
  307. if (*end) /* only interested in proper numerical dirents */
  308. continue;
  309. __event__synthesize_thread(comm_event, mmap_event, pid, 1,
  310. process, tool, machine);
  311. }
  312. closedir(proc);
  313. err = 0;
  314. out_free_mmap:
  315. free(mmap_event);
  316. out_free_comm:
  317. free(comm_event);
  318. out:
  319. return err;
  320. }
  321. struct process_symbol_args {
  322. const char *name;
  323. u64 start;
  324. };
  325. static int find_symbol_cb(void *arg, const char *name, char type,
  326. u64 start, u64 end __used)
  327. {
  328. struct process_symbol_args *args = arg;
  329. /*
  330. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  331. * an 'A' to the same address as "_stext".
  332. */
  333. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  334. type == 'A') || strcmp(name, args->name))
  335. return 0;
  336. args->start = start;
  337. return 1;
  338. }
  339. int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
  340. perf_event__handler_t process,
  341. struct machine *machine,
  342. const char *symbol_name)
  343. {
  344. size_t size;
  345. const char *filename, *mmap_name;
  346. char path[PATH_MAX];
  347. char name_buff[PATH_MAX];
  348. struct map *map;
  349. int err;
  350. /*
  351. * We should get this from /sys/kernel/sections/.text, but till that is
  352. * available use this, and after it is use this as a fallback for older
  353. * kernels.
  354. */
  355. struct process_symbol_args args = { .name = symbol_name, };
  356. union perf_event *event = zalloc((sizeof(event->mmap) +
  357. machine->id_hdr_size));
  358. if (event == NULL) {
  359. pr_debug("Not enough memory synthesizing mmap event "
  360. "for kernel modules\n");
  361. return -1;
  362. }
  363. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  364. if (machine__is_host(machine)) {
  365. /*
  366. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  367. * see kernel/perf_event.c __perf_event_mmap
  368. */
  369. event->header.misc = PERF_RECORD_MISC_KERNEL;
  370. filename = "/proc/kallsyms";
  371. } else {
  372. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  373. if (machine__is_default_guest(machine))
  374. filename = (char *) symbol_conf.default_guest_kallsyms;
  375. else {
  376. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  377. filename = path;
  378. }
  379. }
  380. if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
  381. return -ENOENT;
  382. map = machine->vmlinux_maps[MAP__FUNCTION];
  383. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  384. "%s%s", mmap_name, symbol_name) + 1;
  385. size = ALIGN(size, sizeof(u64));
  386. event->mmap.header.type = PERF_RECORD_MMAP;
  387. event->mmap.header.size = (sizeof(event->mmap) -
  388. (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
  389. event->mmap.pgoff = args.start;
  390. event->mmap.start = map->start;
  391. event->mmap.len = map->end - event->mmap.start;
  392. event->mmap.pid = machine->pid;
  393. err = process(tool, event, &synth_sample, machine);
  394. free(event);
  395. return err;
  396. }
  397. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  398. {
  399. return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
  400. }
  401. int perf_event__process_comm(struct perf_tool *tool __used,
  402. union perf_event *event,
  403. struct perf_sample *sample __used,
  404. struct machine *machine)
  405. {
  406. struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
  407. if (dump_trace)
  408. perf_event__fprintf_comm(event, stdout);
  409. if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
  410. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  411. return -1;
  412. }
  413. return 0;
  414. }
  415. int perf_event__process_lost(struct perf_tool *tool __used,
  416. union perf_event *event,
  417. struct perf_sample *sample __used,
  418. struct machine *machine __used)
  419. {
  420. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  421. event->lost.id, event->lost.lost);
  422. return 0;
  423. }
  424. static void perf_event__set_kernel_mmap_len(union perf_event *event,
  425. struct map **maps)
  426. {
  427. maps[MAP__FUNCTION]->start = event->mmap.start;
  428. maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
  429. /*
  430. * Be a bit paranoid here, some perf.data file came with
  431. * a zero sized synthesized MMAP event for the kernel.
  432. */
  433. if (maps[MAP__FUNCTION]->end == 0)
  434. maps[MAP__FUNCTION]->end = ~0ULL;
  435. }
  436. static int perf_event__process_kernel_mmap(struct perf_tool *tool __used,
  437. union perf_event *event,
  438. struct machine *machine)
  439. {
  440. struct map *map;
  441. char kmmap_prefix[PATH_MAX];
  442. enum dso_kernel_type kernel_type;
  443. bool is_kernel_mmap;
  444. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  445. if (machine__is_host(machine))
  446. kernel_type = DSO_TYPE_KERNEL;
  447. else
  448. kernel_type = DSO_TYPE_GUEST_KERNEL;
  449. is_kernel_mmap = memcmp(event->mmap.filename,
  450. kmmap_prefix,
  451. strlen(kmmap_prefix)) == 0;
  452. if (event->mmap.filename[0] == '/' ||
  453. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  454. char short_module_name[1024];
  455. char *name, *dot;
  456. if (event->mmap.filename[0] == '/') {
  457. name = strrchr(event->mmap.filename, '/');
  458. if (name == NULL)
  459. goto out_problem;
  460. ++name; /* skip / */
  461. dot = strrchr(name, '.');
  462. if (dot == NULL)
  463. goto out_problem;
  464. snprintf(short_module_name, sizeof(short_module_name),
  465. "[%.*s]", (int)(dot - name), name);
  466. strxfrchar(short_module_name, '-', '_');
  467. } else
  468. strcpy(short_module_name, event->mmap.filename);
  469. map = machine__new_module(machine, event->mmap.start,
  470. event->mmap.filename);
  471. if (map == NULL)
  472. goto out_problem;
  473. name = strdup(short_module_name);
  474. if (name == NULL)
  475. goto out_problem;
  476. map->dso->short_name = name;
  477. map->dso->sname_alloc = 1;
  478. map->end = map->start + event->mmap.len;
  479. } else if (is_kernel_mmap) {
  480. const char *symbol_name = (event->mmap.filename +
  481. strlen(kmmap_prefix));
  482. /*
  483. * Should be there already, from the build-id table in
  484. * the header.
  485. */
  486. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  487. kmmap_prefix);
  488. if (kernel == NULL)
  489. goto out_problem;
  490. kernel->kernel = kernel_type;
  491. if (__machine__create_kernel_maps(machine, kernel) < 0)
  492. goto out_problem;
  493. perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
  494. /*
  495. * Avoid using a zero address (kptr_restrict) for the ref reloc
  496. * symbol. Effectively having zero here means that at record
  497. * time /proc/sys/kernel/kptr_restrict was non zero.
  498. */
  499. if (event->mmap.pgoff != 0) {
  500. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  501. symbol_name,
  502. event->mmap.pgoff);
  503. }
  504. if (machine__is_default_guest(machine)) {
  505. /*
  506. * preload dso of guest kernel and modules
  507. */
  508. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  509. NULL);
  510. }
  511. }
  512. return 0;
  513. out_problem:
  514. return -1;
  515. }
  516. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  517. {
  518. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
  519. event->mmap.pid, event->mmap.tid, event->mmap.start,
  520. event->mmap.len, event->mmap.pgoff, event->mmap.filename);
  521. }
  522. int perf_event__process_mmap(struct perf_tool *tool,
  523. union perf_event *event,
  524. struct perf_sample *sample __used,
  525. struct machine *machine)
  526. {
  527. struct thread *thread;
  528. struct map *map;
  529. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  530. int ret = 0;
  531. if (dump_trace)
  532. perf_event__fprintf_mmap(event, stdout);
  533. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  534. cpumode == PERF_RECORD_MISC_KERNEL) {
  535. ret = perf_event__process_kernel_mmap(tool, event, machine);
  536. if (ret < 0)
  537. goto out_problem;
  538. return 0;
  539. }
  540. thread = machine__findnew_thread(machine, event->mmap.pid);
  541. if (thread == NULL)
  542. goto out_problem;
  543. map = map__new(&machine->user_dsos, event->mmap.start,
  544. event->mmap.len, event->mmap.pgoff,
  545. event->mmap.pid, event->mmap.filename,
  546. MAP__FUNCTION);
  547. if (map == NULL)
  548. goto out_problem;
  549. thread__insert_map(thread, map);
  550. return 0;
  551. out_problem:
  552. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  553. return 0;
  554. }
  555. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  556. {
  557. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  558. event->fork.pid, event->fork.tid,
  559. event->fork.ppid, event->fork.ptid);
  560. }
  561. int perf_event__process_task(struct perf_tool *tool __used,
  562. union perf_event *event,
  563. struct perf_sample *sample __used,
  564. struct machine *machine)
  565. {
  566. struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
  567. struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
  568. if (dump_trace)
  569. perf_event__fprintf_task(event, stdout);
  570. if (event->header.type == PERF_RECORD_EXIT) {
  571. machine__remove_thread(machine, thread);
  572. return 0;
  573. }
  574. if (thread == NULL || parent == NULL ||
  575. thread__fork(thread, parent) < 0) {
  576. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  577. return -1;
  578. }
  579. return 0;
  580. }
  581. size_t perf_event__fprintf(union perf_event *event, FILE *fp)
  582. {
  583. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  584. perf_event__name(event->header.type));
  585. switch (event->header.type) {
  586. case PERF_RECORD_COMM:
  587. ret += perf_event__fprintf_comm(event, fp);
  588. break;
  589. case PERF_RECORD_FORK:
  590. case PERF_RECORD_EXIT:
  591. ret += perf_event__fprintf_task(event, fp);
  592. break;
  593. case PERF_RECORD_MMAP:
  594. ret += perf_event__fprintf_mmap(event, fp);
  595. break;
  596. default:
  597. ret += fprintf(fp, "\n");
  598. }
  599. return ret;
  600. }
  601. int perf_event__process(struct perf_tool *tool, union perf_event *event,
  602. struct perf_sample *sample, struct machine *machine)
  603. {
  604. switch (event->header.type) {
  605. case PERF_RECORD_COMM:
  606. perf_event__process_comm(tool, event, sample, machine);
  607. break;
  608. case PERF_RECORD_MMAP:
  609. perf_event__process_mmap(tool, event, sample, machine);
  610. break;
  611. case PERF_RECORD_FORK:
  612. case PERF_RECORD_EXIT:
  613. perf_event__process_task(tool, event, sample, machine);
  614. break;
  615. case PERF_RECORD_LOST:
  616. perf_event__process_lost(tool, event, sample, machine);
  617. default:
  618. break;
  619. }
  620. return 0;
  621. }
  622. void thread__find_addr_map(struct thread *self,
  623. struct machine *machine, u8 cpumode,
  624. enum map_type type, u64 addr,
  625. struct addr_location *al)
  626. {
  627. struct map_groups *mg = &self->mg;
  628. al->thread = self;
  629. al->addr = addr;
  630. al->cpumode = cpumode;
  631. al->filtered = false;
  632. if (machine == NULL) {
  633. al->map = NULL;
  634. return;
  635. }
  636. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  637. al->level = 'k';
  638. mg = &machine->kmaps;
  639. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  640. al->level = '.';
  641. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  642. al->level = 'g';
  643. mg = &machine->kmaps;
  644. } else {
  645. /*
  646. * 'u' means guest os user space.
  647. * TODO: We don't support guest user space. Might support late.
  648. */
  649. if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
  650. al->level = 'u';
  651. else
  652. al->level = 'H';
  653. al->map = NULL;
  654. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  655. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  656. !perf_guest)
  657. al->filtered = true;
  658. if ((cpumode == PERF_RECORD_MISC_USER ||
  659. cpumode == PERF_RECORD_MISC_KERNEL) &&
  660. !perf_host)
  661. al->filtered = true;
  662. return;
  663. }
  664. try_again:
  665. al->map = map_groups__find(mg, type, al->addr);
  666. if (al->map == NULL) {
  667. /*
  668. * If this is outside of all known maps, and is a negative
  669. * address, try to look it up in the kernel dso, as it might be
  670. * a vsyscall or vdso (which executes in user-mode).
  671. *
  672. * XXX This is nasty, we should have a symbol list in the
  673. * "[vdso]" dso, but for now lets use the old trick of looking
  674. * in the whole kernel symbol list.
  675. */
  676. if ((long long)al->addr < 0 &&
  677. cpumode == PERF_RECORD_MISC_USER &&
  678. machine && mg != &machine->kmaps) {
  679. mg = &machine->kmaps;
  680. goto try_again;
  681. }
  682. } else
  683. al->addr = al->map->map_ip(al->map, al->addr);
  684. }
  685. void thread__find_addr_location(struct thread *thread, struct machine *machine,
  686. u8 cpumode, enum map_type type, u64 addr,
  687. struct addr_location *al,
  688. symbol_filter_t filter)
  689. {
  690. thread__find_addr_map(thread, machine, cpumode, type, addr, al);
  691. if (al->map != NULL)
  692. al->sym = map__find_symbol(al->map, al->addr, filter);
  693. else
  694. al->sym = NULL;
  695. }
  696. int perf_event__preprocess_sample(const union perf_event *event,
  697. struct machine *machine,
  698. struct addr_location *al,
  699. struct perf_sample *sample,
  700. symbol_filter_t filter)
  701. {
  702. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  703. struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
  704. if (thread == NULL)
  705. return -1;
  706. if (symbol_conf.comm_list &&
  707. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  708. goto out_filtered;
  709. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  710. /*
  711. * Have we already created the kernel maps for this machine?
  712. *
  713. * This should have happened earlier, when we processed the kernel MMAP
  714. * events, but for older perf.data files there was no such thing, so do
  715. * it now.
  716. */
  717. if (cpumode == PERF_RECORD_MISC_KERNEL &&
  718. machine->vmlinux_maps[MAP__FUNCTION] == NULL)
  719. machine__create_kernel_maps(machine);
  720. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  721. event->ip.ip, al);
  722. dump_printf(" ...... dso: %s\n",
  723. al->map ? al->map->dso->long_name :
  724. al->level == 'H' ? "[hypervisor]" : "<not found>");
  725. al->sym = NULL;
  726. al->cpu = sample->cpu;
  727. if (al->map) {
  728. struct dso *dso = al->map->dso;
  729. if (symbol_conf.dso_list &&
  730. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  731. dso->short_name) ||
  732. (dso->short_name != dso->long_name &&
  733. strlist__has_entry(symbol_conf.dso_list,
  734. dso->long_name)))))
  735. goto out_filtered;
  736. al->sym = map__find_symbol(al->map, al->addr, filter);
  737. }
  738. if (symbol_conf.sym_list && al->sym &&
  739. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  740. goto out_filtered;
  741. return 0;
  742. out_filtered:
  743. al->filtered = true;
  744. return 0;
  745. }