event.c 28 KB

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