event.c 23 KB

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