event.c 26 KB

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