event.c 27 KB

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