event.c 23 KB

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