event.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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, perf_event__handler_t process,
  225. struct perf_tool *tool,
  226. struct machine *machine)
  227. {
  228. pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, 1,
  229. process, machine);
  230. if (tgid == -1)
  231. return -1;
  232. return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  233. process, machine);
  234. }
  235. int perf_event__synthesize_thread_map(struct perf_tool *tool,
  236. struct thread_map *threads,
  237. perf_event__handler_t process,
  238. struct machine *machine)
  239. {
  240. union perf_event *comm_event, *mmap_event;
  241. int err = -1, thread;
  242. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  243. if (comm_event == NULL)
  244. goto out;
  245. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  246. if (mmap_event == NULL)
  247. goto out_free_comm;
  248. err = 0;
  249. for (thread = 0; thread < threads->nr; ++thread) {
  250. if (__event__synthesize_thread(comm_event, mmap_event,
  251. threads->map[thread],
  252. process, tool, machine)) {
  253. err = -1;
  254. break;
  255. }
  256. }
  257. free(mmap_event);
  258. out_free_comm:
  259. free(comm_event);
  260. out:
  261. return err;
  262. }
  263. int perf_event__synthesize_threads(struct perf_tool *tool,
  264. perf_event__handler_t process,
  265. struct machine *machine)
  266. {
  267. DIR *proc;
  268. struct dirent dirent, *next;
  269. union perf_event *comm_event, *mmap_event;
  270. int err = -1;
  271. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  272. if (comm_event == NULL)
  273. goto out;
  274. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  275. if (mmap_event == NULL)
  276. goto out_free_comm;
  277. proc = opendir("/proc");
  278. if (proc == NULL)
  279. goto out_free_mmap;
  280. while (!readdir_r(proc, &dirent, &next) && next) {
  281. char *end;
  282. pid_t pid = strtol(dirent.d_name, &end, 10);
  283. if (*end) /* only interested in proper numerical dirents */
  284. continue;
  285. __event__synthesize_thread(comm_event, mmap_event, pid,
  286. process, tool, machine);
  287. }
  288. closedir(proc);
  289. err = 0;
  290. out_free_mmap:
  291. free(mmap_event);
  292. out_free_comm:
  293. free(comm_event);
  294. out:
  295. return err;
  296. }
  297. struct process_symbol_args {
  298. const char *name;
  299. u64 start;
  300. };
  301. static int find_symbol_cb(void *arg, const char *name, char type,
  302. u64 start, u64 end __used)
  303. {
  304. struct process_symbol_args *args = arg;
  305. /*
  306. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  307. * an 'A' to the same address as "_stext".
  308. */
  309. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  310. type == 'A') || strcmp(name, args->name))
  311. return 0;
  312. args->start = start;
  313. return 1;
  314. }
  315. int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
  316. perf_event__handler_t process,
  317. struct machine *machine,
  318. const char *symbol_name)
  319. {
  320. size_t size;
  321. const char *filename, *mmap_name;
  322. char path[PATH_MAX];
  323. char name_buff[PATH_MAX];
  324. struct map *map;
  325. int err;
  326. /*
  327. * We should get this from /sys/kernel/sections/.text, but till that is
  328. * available use this, and after it is use this as a fallback for older
  329. * kernels.
  330. */
  331. struct process_symbol_args args = { .name = symbol_name, };
  332. union perf_event *event = zalloc((sizeof(event->mmap) +
  333. machine->id_hdr_size));
  334. if (event == NULL) {
  335. pr_debug("Not enough memory synthesizing mmap event "
  336. "for kernel modules\n");
  337. return -1;
  338. }
  339. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  340. if (machine__is_host(machine)) {
  341. /*
  342. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  343. * see kernel/perf_event.c __perf_event_mmap
  344. */
  345. event->header.misc = PERF_RECORD_MISC_KERNEL;
  346. filename = "/proc/kallsyms";
  347. } else {
  348. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  349. if (machine__is_default_guest(machine))
  350. filename = (char *) symbol_conf.default_guest_kallsyms;
  351. else {
  352. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  353. filename = path;
  354. }
  355. }
  356. if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
  357. return -ENOENT;
  358. map = machine->vmlinux_maps[MAP__FUNCTION];
  359. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  360. "%s%s", mmap_name, symbol_name) + 1;
  361. size = ALIGN(size, sizeof(u64));
  362. event->mmap.header.type = PERF_RECORD_MMAP;
  363. event->mmap.header.size = (sizeof(event->mmap) -
  364. (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
  365. event->mmap.pgoff = args.start;
  366. event->mmap.start = map->start;
  367. event->mmap.len = map->end - event->mmap.start;
  368. event->mmap.pid = machine->pid;
  369. err = process(tool, event, &synth_sample, machine);
  370. free(event);
  371. return err;
  372. }
  373. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  374. {
  375. return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
  376. }
  377. int perf_event__process_comm(struct perf_tool *tool __used,
  378. union perf_event *event,
  379. struct perf_sample *sample __used,
  380. struct machine *machine)
  381. {
  382. struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
  383. if (dump_trace)
  384. perf_event__fprintf_comm(event, stdout);
  385. if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
  386. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  387. return -1;
  388. }
  389. return 0;
  390. }
  391. int perf_event__process_lost(struct perf_tool *tool __used,
  392. union perf_event *event,
  393. struct perf_sample *sample __used,
  394. struct machine *machine __used)
  395. {
  396. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  397. event->lost.id, event->lost.lost);
  398. return 0;
  399. }
  400. static void perf_event__set_kernel_mmap_len(union perf_event *event,
  401. struct map **maps)
  402. {
  403. maps[MAP__FUNCTION]->start = event->mmap.start;
  404. maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
  405. /*
  406. * Be a bit paranoid here, some perf.data file came with
  407. * a zero sized synthesized MMAP event for the kernel.
  408. */
  409. if (maps[MAP__FUNCTION]->end == 0)
  410. maps[MAP__FUNCTION]->end = ~0ULL;
  411. }
  412. static int perf_event__process_kernel_mmap(struct perf_tool *tool __used,
  413. union perf_event *event,
  414. struct machine *machine)
  415. {
  416. struct map *map;
  417. char kmmap_prefix[PATH_MAX];
  418. enum dso_kernel_type kernel_type;
  419. bool is_kernel_mmap;
  420. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  421. if (machine__is_host(machine))
  422. kernel_type = DSO_TYPE_KERNEL;
  423. else
  424. kernel_type = DSO_TYPE_GUEST_KERNEL;
  425. is_kernel_mmap = memcmp(event->mmap.filename,
  426. kmmap_prefix,
  427. strlen(kmmap_prefix)) == 0;
  428. if (event->mmap.filename[0] == '/' ||
  429. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  430. char short_module_name[1024];
  431. char *name, *dot;
  432. if (event->mmap.filename[0] == '/') {
  433. name = strrchr(event->mmap.filename, '/');
  434. if (name == NULL)
  435. goto out_problem;
  436. ++name; /* skip / */
  437. dot = strrchr(name, '.');
  438. if (dot == NULL)
  439. goto out_problem;
  440. snprintf(short_module_name, sizeof(short_module_name),
  441. "[%.*s]", (int)(dot - name), name);
  442. strxfrchar(short_module_name, '-', '_');
  443. } else
  444. strcpy(short_module_name, event->mmap.filename);
  445. map = machine__new_module(machine, event->mmap.start,
  446. event->mmap.filename);
  447. if (map == NULL)
  448. goto out_problem;
  449. name = strdup(short_module_name);
  450. if (name == NULL)
  451. goto out_problem;
  452. map->dso->short_name = name;
  453. map->dso->sname_alloc = 1;
  454. map->end = map->start + event->mmap.len;
  455. } else if (is_kernel_mmap) {
  456. const char *symbol_name = (event->mmap.filename +
  457. strlen(kmmap_prefix));
  458. /*
  459. * Should be there already, from the build-id table in
  460. * the header.
  461. */
  462. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  463. kmmap_prefix);
  464. if (kernel == NULL)
  465. goto out_problem;
  466. kernel->kernel = kernel_type;
  467. if (__machine__create_kernel_maps(machine, kernel) < 0)
  468. goto out_problem;
  469. perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
  470. /*
  471. * Avoid using a zero address (kptr_restrict) for the ref reloc
  472. * symbol. Effectively having zero here means that at record
  473. * time /proc/sys/kernel/kptr_restrict was non zero.
  474. */
  475. if (event->mmap.pgoff != 0) {
  476. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  477. symbol_name,
  478. event->mmap.pgoff);
  479. }
  480. if (machine__is_default_guest(machine)) {
  481. /*
  482. * preload dso of guest kernel and modules
  483. */
  484. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  485. NULL);
  486. }
  487. }
  488. return 0;
  489. out_problem:
  490. return -1;
  491. }
  492. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  493. {
  494. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
  495. event->mmap.pid, event->mmap.tid, event->mmap.start,
  496. event->mmap.len, event->mmap.pgoff, event->mmap.filename);
  497. }
  498. int perf_event__process_mmap(struct perf_tool *tool,
  499. union perf_event *event,
  500. struct perf_sample *sample __used,
  501. struct machine *machine)
  502. {
  503. struct thread *thread;
  504. struct map *map;
  505. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  506. int ret = 0;
  507. if (dump_trace)
  508. perf_event__fprintf_mmap(event, stdout);
  509. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  510. cpumode == PERF_RECORD_MISC_KERNEL) {
  511. ret = perf_event__process_kernel_mmap(tool, event, machine);
  512. if (ret < 0)
  513. goto out_problem;
  514. return 0;
  515. }
  516. thread = machine__findnew_thread(machine, event->mmap.pid);
  517. if (thread == NULL)
  518. goto out_problem;
  519. map = map__new(&machine->user_dsos, event->mmap.start,
  520. event->mmap.len, event->mmap.pgoff,
  521. event->mmap.pid, event->mmap.filename,
  522. MAP__FUNCTION);
  523. if (map == NULL)
  524. goto out_problem;
  525. thread__insert_map(thread, map);
  526. return 0;
  527. out_problem:
  528. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  529. return 0;
  530. }
  531. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  532. {
  533. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  534. event->fork.pid, event->fork.tid,
  535. event->fork.ppid, event->fork.ptid);
  536. }
  537. int perf_event__process_task(struct perf_tool *tool __used,
  538. union perf_event *event,
  539. struct perf_sample *sample __used,
  540. struct machine *machine)
  541. {
  542. struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
  543. struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
  544. if (dump_trace)
  545. perf_event__fprintf_task(event, stdout);
  546. if (event->header.type == PERF_RECORD_EXIT) {
  547. machine__remove_thread(machine, thread);
  548. return 0;
  549. }
  550. if (thread == NULL || parent == NULL ||
  551. thread__fork(thread, parent) < 0) {
  552. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  553. return -1;
  554. }
  555. return 0;
  556. }
  557. size_t perf_event__fprintf(union perf_event *event, FILE *fp)
  558. {
  559. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  560. perf_event__name(event->header.type));
  561. switch (event->header.type) {
  562. case PERF_RECORD_COMM:
  563. ret += perf_event__fprintf_comm(event, fp);
  564. break;
  565. case PERF_RECORD_FORK:
  566. case PERF_RECORD_EXIT:
  567. ret += perf_event__fprintf_task(event, fp);
  568. break;
  569. case PERF_RECORD_MMAP:
  570. ret += perf_event__fprintf_mmap(event, fp);
  571. break;
  572. default:
  573. ret += fprintf(fp, "\n");
  574. }
  575. return ret;
  576. }
  577. int perf_event__process(struct perf_tool *tool, union perf_event *event,
  578. struct perf_sample *sample, struct machine *machine)
  579. {
  580. switch (event->header.type) {
  581. case PERF_RECORD_COMM:
  582. perf_event__process_comm(tool, event, sample, machine);
  583. break;
  584. case PERF_RECORD_MMAP:
  585. perf_event__process_mmap(tool, event, sample, machine);
  586. break;
  587. case PERF_RECORD_FORK:
  588. case PERF_RECORD_EXIT:
  589. perf_event__process_task(tool, event, sample, machine);
  590. break;
  591. case PERF_RECORD_LOST:
  592. perf_event__process_lost(tool, event, sample, machine);
  593. default:
  594. break;
  595. }
  596. return 0;
  597. }
  598. void thread__find_addr_map(struct thread *self,
  599. struct machine *machine, u8 cpumode,
  600. enum map_type type, u64 addr,
  601. struct addr_location *al)
  602. {
  603. struct map_groups *mg = &self->mg;
  604. al->thread = self;
  605. al->addr = addr;
  606. al->cpumode = cpumode;
  607. al->filtered = false;
  608. if (machine == NULL) {
  609. al->map = NULL;
  610. return;
  611. }
  612. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  613. al->level = 'k';
  614. mg = &machine->kmaps;
  615. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  616. al->level = '.';
  617. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  618. al->level = 'g';
  619. mg = &machine->kmaps;
  620. } else {
  621. /*
  622. * 'u' means guest os user space.
  623. * TODO: We don't support guest user space. Might support late.
  624. */
  625. if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
  626. al->level = 'u';
  627. else
  628. al->level = 'H';
  629. al->map = NULL;
  630. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  631. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  632. !perf_guest)
  633. al->filtered = true;
  634. if ((cpumode == PERF_RECORD_MISC_USER ||
  635. cpumode == PERF_RECORD_MISC_KERNEL) &&
  636. !perf_host)
  637. al->filtered = true;
  638. return;
  639. }
  640. try_again:
  641. al->map = map_groups__find(mg, type, al->addr);
  642. if (al->map == NULL) {
  643. /*
  644. * If this is outside of all known maps, and is a negative
  645. * address, try to look it up in the kernel dso, as it might be
  646. * a vsyscall or vdso (which executes in user-mode).
  647. *
  648. * XXX This is nasty, we should have a symbol list in the
  649. * "[vdso]" dso, but for now lets use the old trick of looking
  650. * in the whole kernel symbol list.
  651. */
  652. if ((long long)al->addr < 0 &&
  653. cpumode == PERF_RECORD_MISC_USER &&
  654. machine && mg != &machine->kmaps) {
  655. mg = &machine->kmaps;
  656. goto try_again;
  657. }
  658. } else
  659. al->addr = al->map->map_ip(al->map, al->addr);
  660. }
  661. void thread__find_addr_location(struct thread *thread, struct machine *machine,
  662. u8 cpumode, enum map_type type, u64 addr,
  663. struct addr_location *al,
  664. symbol_filter_t filter)
  665. {
  666. thread__find_addr_map(thread, machine, cpumode, type, addr, al);
  667. if (al->map != NULL)
  668. al->sym = map__find_symbol(al->map, al->addr, filter);
  669. else
  670. al->sym = NULL;
  671. }
  672. int perf_event__preprocess_sample(const union perf_event *event,
  673. struct machine *machine,
  674. struct addr_location *al,
  675. struct perf_sample *sample,
  676. symbol_filter_t filter)
  677. {
  678. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  679. struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
  680. if (thread == NULL)
  681. return -1;
  682. if (symbol_conf.comm_list &&
  683. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  684. goto out_filtered;
  685. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  686. /*
  687. * Have we already created the kernel maps for this machine?
  688. *
  689. * This should have happened earlier, when we processed the kernel MMAP
  690. * events, but for older perf.data files there was no such thing, so do
  691. * it now.
  692. */
  693. if (cpumode == PERF_RECORD_MISC_KERNEL &&
  694. machine->vmlinux_maps[MAP__FUNCTION] == NULL)
  695. machine__create_kernel_maps(machine);
  696. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  697. event->ip.ip, al);
  698. dump_printf(" ...... dso: %s\n",
  699. al->map ? al->map->dso->long_name :
  700. al->level == 'H' ? "[hypervisor]" : "<not found>");
  701. al->sym = NULL;
  702. al->cpu = sample->cpu;
  703. if (al->map) {
  704. if (symbol_conf.dso_list &&
  705. (!al->map || !al->map->dso ||
  706. !(strlist__has_entry(symbol_conf.dso_list,
  707. al->map->dso->short_name) ||
  708. (al->map->dso->short_name != al->map->dso->long_name &&
  709. strlist__has_entry(symbol_conf.dso_list,
  710. al->map->dso->long_name)))))
  711. goto out_filtered;
  712. al->sym = map__find_symbol(al->map, al->addr, filter);
  713. }
  714. if (symbol_conf.sym_list && al->sym &&
  715. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  716. goto out_filtered;
  717. return 0;
  718. out_filtered:
  719. al->filtered = true;
  720. return 0;
  721. }