event.c 24 KB

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