event.c 23 KB

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