event.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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;
  67. ssize_t n;
  68. char *nl, *name, *tgids, *ppids;
  69. *tgid = -1;
  70. *ppid = -1;
  71. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  72. fd = open(filename, O_RDONLY);
  73. if (fd < 0) {
  74. pr_debug("couldn't open %s\n", filename);
  75. return -1;
  76. }
  77. n = read(fd, bf, sizeof(bf) - 1);
  78. close(fd);
  79. if (n <= 0) {
  80. pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
  81. pid);
  82. return -1;
  83. }
  84. bf[n] = '\0';
  85. name = strstr(bf, "Name:");
  86. tgids = strstr(bf, "Tgid:");
  87. ppids = strstr(bf, "PPid:");
  88. if (name) {
  89. name += 5; /* strlen("Name:") */
  90. while (*name && isspace(*name))
  91. ++name;
  92. nl = strchr(name, '\n');
  93. if (nl)
  94. *nl = '\0';
  95. size = strlen(name);
  96. if (size >= len)
  97. size = len - 1;
  98. memcpy(comm, name, size);
  99. comm[size] = '\0';
  100. } else {
  101. pr_debug("Name: string not found for pid %d\n", pid);
  102. }
  103. if (tgids) {
  104. tgids += 5; /* strlen("Tgid:") */
  105. *tgid = atoi(tgids);
  106. } else {
  107. pr_debug("Tgid: string not found for pid %d\n", pid);
  108. }
  109. if (ppids) {
  110. ppids += 5; /* strlen("PPid:") */
  111. *ppid = atoi(ppids);
  112. } else {
  113. pr_debug("PPid: string not found for pid %d\n", pid);
  114. }
  115. return 0;
  116. }
  117. static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
  118. struct machine *machine,
  119. pid_t *tgid, pid_t *ppid)
  120. {
  121. size_t size;
  122. *ppid = -1;
  123. memset(&event->comm, 0, sizeof(event->comm));
  124. if (machine__is_host(machine)) {
  125. if (perf_event__get_comm_ids(pid, event->comm.comm,
  126. sizeof(event->comm.comm),
  127. tgid, ppid) != 0) {
  128. return -1;
  129. }
  130. } else {
  131. *tgid = machine->pid;
  132. }
  133. if (*tgid < 0)
  134. return -1;
  135. event->comm.pid = *tgid;
  136. event->comm.header.type = PERF_RECORD_COMM;
  137. size = strlen(event->comm.comm) + 1;
  138. size = PERF_ALIGN(size, sizeof(u64));
  139. memset(event->comm.comm + size, 0, machine->id_hdr_size);
  140. event->comm.header.size = (sizeof(event->comm) -
  141. (sizeof(event->comm.comm) - size) +
  142. machine->id_hdr_size);
  143. event->comm.tid = pid;
  144. return 0;
  145. }
  146. pid_t perf_event__synthesize_comm(struct perf_tool *tool,
  147. union perf_event *event, pid_t pid,
  148. perf_event__handler_t process,
  149. struct machine *machine)
  150. {
  151. pid_t tgid, ppid;
  152. if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
  153. return -1;
  154. if (process(tool, event, &synth_sample, machine) != 0)
  155. return -1;
  156. return tgid;
  157. }
  158. static int perf_event__synthesize_fork(struct perf_tool *tool,
  159. union perf_event *event,
  160. pid_t pid, pid_t tgid, pid_t ppid,
  161. perf_event__handler_t process,
  162. struct machine *machine)
  163. {
  164. memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
  165. /*
  166. * for main thread set parent to ppid from status file. For other
  167. * threads set parent pid to main thread. ie., assume main thread
  168. * spawns all threads in a process
  169. */
  170. if (tgid == pid) {
  171. event->fork.ppid = ppid;
  172. event->fork.ptid = ppid;
  173. } else {
  174. event->fork.ppid = tgid;
  175. event->fork.ptid = tgid;
  176. }
  177. event->fork.pid = tgid;
  178. event->fork.tid = pid;
  179. event->fork.header.type = PERF_RECORD_FORK;
  180. event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
  181. if (process(tool, event, &synth_sample, machine) != 0)
  182. return -1;
  183. return 0;
  184. }
  185. int perf_event__synthesize_mmap_events(struct perf_tool *tool,
  186. union perf_event *event,
  187. pid_t pid, pid_t tgid,
  188. perf_event__handler_t process,
  189. struct machine *machine,
  190. bool mmap_data,
  191. unsigned int proc_map_timeout)
  192. {
  193. char filename[PATH_MAX];
  194. FILE *fp;
  195. unsigned long long t;
  196. bool truncation = false;
  197. unsigned long long timeout = proc_map_timeout * 1000000ULL;
  198. int rc = 0;
  199. if (machine__is_default_guest(machine))
  200. return 0;
  201. snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
  202. machine->root_dir, pid, pid);
  203. fp = fopen(filename, "r");
  204. if (fp == NULL) {
  205. /*
  206. * We raced with a task exiting - just return:
  207. */
  208. pr_debug("couldn't open %s\n", filename);
  209. return -1;
  210. }
  211. event->header.type = PERF_RECORD_MMAP2;
  212. t = rdclock();
  213. while (1) {
  214. char bf[BUFSIZ];
  215. char prot[5];
  216. char execname[PATH_MAX];
  217. char anonstr[] = "//anon";
  218. unsigned int ino;
  219. size_t size;
  220. ssize_t n;
  221. if (fgets(bf, sizeof(bf), fp) == NULL)
  222. break;
  223. if ((rdclock() - t) > timeout) {
  224. pr_warning("Reading %s time out. "
  225. "You may want to increase "
  226. "the time limit by --proc-map-timeout\n",
  227. filename);
  228. truncation = true;
  229. goto out;
  230. }
  231. /* ensure null termination since stack will be reused. */
  232. strcpy(execname, "");
  233. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  234. n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
  235. &event->mmap2.start, &event->mmap2.len, prot,
  236. &event->mmap2.pgoff, &event->mmap2.maj,
  237. &event->mmap2.min,
  238. &ino, execname);
  239. /*
  240. * Anon maps don't have the execname.
  241. */
  242. if (n < 7)
  243. continue;
  244. event->mmap2.ino = (u64)ino;
  245. /*
  246. * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
  247. */
  248. if (machine__is_host(machine))
  249. event->header.misc = PERF_RECORD_MISC_USER;
  250. else
  251. event->header.misc = PERF_RECORD_MISC_GUEST_USER;
  252. /* map protection and flags bits */
  253. event->mmap2.prot = 0;
  254. event->mmap2.flags = 0;
  255. if (prot[0] == 'r')
  256. event->mmap2.prot |= PROT_READ;
  257. if (prot[1] == 'w')
  258. event->mmap2.prot |= PROT_WRITE;
  259. if (prot[2] == 'x')
  260. event->mmap2.prot |= PROT_EXEC;
  261. if (prot[3] == 's')
  262. event->mmap2.flags |= MAP_SHARED;
  263. else
  264. event->mmap2.flags |= MAP_PRIVATE;
  265. if (prot[2] != 'x') {
  266. if (!mmap_data || prot[0] != 'r')
  267. continue;
  268. event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  269. }
  270. out:
  271. if (truncation)
  272. event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
  273. if (!strcmp(execname, ""))
  274. strcpy(execname, anonstr);
  275. size = strlen(execname) + 1;
  276. memcpy(event->mmap2.filename, execname, size);
  277. size = PERF_ALIGN(size, sizeof(u64));
  278. event->mmap2.len -= event->mmap.start;
  279. event->mmap2.header.size = (sizeof(event->mmap2) -
  280. (sizeof(event->mmap2.filename) - size));
  281. memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
  282. event->mmap2.header.size += machine->id_hdr_size;
  283. event->mmap2.pid = tgid;
  284. event->mmap2.tid = pid;
  285. if (process(tool, event, &synth_sample, machine) != 0) {
  286. rc = -1;
  287. break;
  288. }
  289. if (truncation)
  290. break;
  291. }
  292. fclose(fp);
  293. return rc;
  294. }
  295. int perf_event__synthesize_modules(struct perf_tool *tool,
  296. perf_event__handler_t process,
  297. struct machine *machine)
  298. {
  299. int rc = 0;
  300. struct map *pos;
  301. struct map_groups *kmaps = &machine->kmaps;
  302. struct maps *maps = &kmaps->maps[MAP__FUNCTION];
  303. union perf_event *event = zalloc((sizeof(event->mmap) +
  304. machine->id_hdr_size));
  305. if (event == NULL) {
  306. pr_debug("Not enough memory synthesizing mmap event "
  307. "for kernel modules\n");
  308. return -1;
  309. }
  310. event->header.type = PERF_RECORD_MMAP;
  311. /*
  312. * kernel uses 0 for user space maps, see kernel/perf_event.c
  313. * __perf_event_mmap
  314. */
  315. if (machine__is_host(machine))
  316. event->header.misc = PERF_RECORD_MISC_KERNEL;
  317. else
  318. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  319. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  320. size_t size;
  321. if (__map__is_kernel(pos))
  322. continue;
  323. size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  324. event->mmap.header.type = PERF_RECORD_MMAP;
  325. event->mmap.header.size = (sizeof(event->mmap) -
  326. (sizeof(event->mmap.filename) - size));
  327. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  328. event->mmap.header.size += machine->id_hdr_size;
  329. event->mmap.start = pos->start;
  330. event->mmap.len = pos->end - pos->start;
  331. event->mmap.pid = machine->pid;
  332. memcpy(event->mmap.filename, pos->dso->long_name,
  333. pos->dso->long_name_len + 1);
  334. if (process(tool, event, &synth_sample, machine) != 0) {
  335. rc = -1;
  336. break;
  337. }
  338. }
  339. free(event);
  340. return rc;
  341. }
  342. static int __event__synthesize_thread(union perf_event *comm_event,
  343. union perf_event *mmap_event,
  344. union perf_event *fork_event,
  345. pid_t pid, int full,
  346. perf_event__handler_t process,
  347. struct perf_tool *tool,
  348. struct machine *machine,
  349. bool mmap_data,
  350. unsigned int proc_map_timeout)
  351. {
  352. char filename[PATH_MAX];
  353. DIR *tasks;
  354. struct dirent *dirent;
  355. pid_t tgid, ppid;
  356. int rc = 0;
  357. /* special case: only send one comm event using passed in pid */
  358. if (!full) {
  359. tgid = perf_event__synthesize_comm(tool, comm_event, pid,
  360. process, machine);
  361. if (tgid == -1)
  362. return -1;
  363. return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  364. process, machine, mmap_data,
  365. proc_map_timeout);
  366. }
  367. if (machine__is_default_guest(machine))
  368. return 0;
  369. snprintf(filename, sizeof(filename), "%s/proc/%d/task",
  370. machine->root_dir, pid);
  371. tasks = opendir(filename);
  372. if (tasks == NULL) {
  373. pr_debug("couldn't open %s\n", filename);
  374. return 0;
  375. }
  376. while ((dirent = readdir(tasks)) != NULL) {
  377. char *end;
  378. pid_t _pid;
  379. _pid = strtol(dirent->d_name, &end, 10);
  380. if (*end)
  381. continue;
  382. rc = -1;
  383. if (perf_event__prepare_comm(comm_event, _pid, machine,
  384. &tgid, &ppid) != 0)
  385. break;
  386. if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
  387. ppid, process, machine) < 0)
  388. break;
  389. /*
  390. * Send the prepared comm event
  391. */
  392. if (process(tool, comm_event, &synth_sample, machine) != 0)
  393. break;
  394. rc = 0;
  395. if (_pid == pid) {
  396. /* process the parent's maps too */
  397. rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  398. process, machine, mmap_data, proc_map_timeout);
  399. if (rc)
  400. break;
  401. }
  402. }
  403. closedir(tasks);
  404. return rc;
  405. }
  406. int perf_event__synthesize_thread_map(struct perf_tool *tool,
  407. struct thread_map *threads,
  408. perf_event__handler_t process,
  409. struct machine *machine,
  410. bool mmap_data,
  411. unsigned int proc_map_timeout)
  412. {
  413. union perf_event *comm_event, *mmap_event, *fork_event;
  414. int err = -1, thread, j;
  415. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  416. if (comm_event == NULL)
  417. goto out;
  418. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  419. if (mmap_event == NULL)
  420. goto out_free_comm;
  421. fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
  422. if (fork_event == NULL)
  423. goto out_free_mmap;
  424. err = 0;
  425. for (thread = 0; thread < threads->nr; ++thread) {
  426. if (__event__synthesize_thread(comm_event, mmap_event,
  427. fork_event,
  428. thread_map__pid(threads, thread), 0,
  429. process, tool, machine,
  430. mmap_data, proc_map_timeout)) {
  431. err = -1;
  432. break;
  433. }
  434. /*
  435. * comm.pid is set to thread group id by
  436. * perf_event__synthesize_comm
  437. */
  438. if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
  439. bool need_leader = true;
  440. /* is thread group leader in thread_map? */
  441. for (j = 0; j < threads->nr; ++j) {
  442. if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
  443. need_leader = false;
  444. break;
  445. }
  446. }
  447. /* if not, generate events for it */
  448. if (need_leader &&
  449. __event__synthesize_thread(comm_event, mmap_event,
  450. fork_event,
  451. comm_event->comm.pid, 0,
  452. process, tool, machine,
  453. mmap_data, proc_map_timeout)) {
  454. err = -1;
  455. break;
  456. }
  457. }
  458. }
  459. free(fork_event);
  460. out_free_mmap:
  461. free(mmap_event);
  462. out_free_comm:
  463. free(comm_event);
  464. out:
  465. return err;
  466. }
  467. int perf_event__synthesize_threads(struct perf_tool *tool,
  468. perf_event__handler_t process,
  469. struct machine *machine,
  470. bool mmap_data,
  471. unsigned int proc_map_timeout)
  472. {
  473. DIR *proc;
  474. char proc_path[PATH_MAX];
  475. struct dirent *dirent;
  476. union perf_event *comm_event, *mmap_event, *fork_event;
  477. int err = -1;
  478. if (machine__is_default_guest(machine))
  479. return 0;
  480. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  481. if (comm_event == NULL)
  482. goto out;
  483. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  484. if (mmap_event == NULL)
  485. goto out_free_comm;
  486. fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
  487. if (fork_event == NULL)
  488. goto out_free_mmap;
  489. snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
  490. proc = opendir(proc_path);
  491. if (proc == NULL)
  492. goto out_free_fork;
  493. while ((dirent = readdir(proc)) != NULL) {
  494. char *end;
  495. pid_t pid = strtol(dirent->d_name, &end, 10);
  496. if (*end) /* only interested in proper numerical dirents */
  497. continue;
  498. /*
  499. * We may race with exiting thread, so don't stop just because
  500. * one thread couldn't be synthesized.
  501. */
  502. __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
  503. 1, process, tool, machine, mmap_data,
  504. proc_map_timeout);
  505. }
  506. err = 0;
  507. closedir(proc);
  508. out_free_fork:
  509. free(fork_event);
  510. out_free_mmap:
  511. free(mmap_event);
  512. out_free_comm:
  513. free(comm_event);
  514. out:
  515. return err;
  516. }
  517. struct process_symbol_args {
  518. const char *name;
  519. u64 start;
  520. };
  521. static int find_symbol_cb(void *arg, const char *name, char type,
  522. u64 start)
  523. {
  524. struct process_symbol_args *args = arg;
  525. /*
  526. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  527. * an 'A' to the same address as "_stext".
  528. */
  529. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  530. type == 'A') || strcmp(name, args->name))
  531. return 0;
  532. args->start = start;
  533. return 1;
  534. }
  535. u64 kallsyms__get_function_start(const char *kallsyms_filename,
  536. const char *symbol_name)
  537. {
  538. struct process_symbol_args args = { .name = symbol_name, };
  539. if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
  540. return 0;
  541. return args.start;
  542. }
  543. int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
  544. perf_event__handler_t process,
  545. struct machine *machine)
  546. {
  547. size_t size;
  548. const char *mmap_name;
  549. char name_buff[PATH_MAX];
  550. struct map *map = machine__kernel_map(machine);
  551. struct kmap *kmap;
  552. int err;
  553. union perf_event *event;
  554. if (map == NULL)
  555. return -1;
  556. /*
  557. * We should get this from /sys/kernel/sections/.text, but till that is
  558. * available use this, and after it is use this as a fallback for older
  559. * kernels.
  560. */
  561. event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
  562. if (event == NULL) {
  563. pr_debug("Not enough memory synthesizing mmap event "
  564. "for kernel modules\n");
  565. return -1;
  566. }
  567. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  568. if (machine__is_host(machine)) {
  569. /*
  570. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  571. * see kernel/perf_event.c __perf_event_mmap
  572. */
  573. event->header.misc = PERF_RECORD_MISC_KERNEL;
  574. } else {
  575. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  576. }
  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__kernel_map(machine) == 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. al->socket = -1;
  878. if (al->cpu >= 0) {
  879. struct perf_env *env = machine->env;
  880. if (env && env->cpu)
  881. al->socket = env->cpu[al->cpu].socket_id;
  882. }
  883. if (al->map) {
  884. struct dso *dso = al->map->dso;
  885. if (symbol_conf.dso_list &&
  886. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  887. dso->short_name) ||
  888. (dso->short_name != dso->long_name &&
  889. strlist__has_entry(symbol_conf.dso_list,
  890. dso->long_name))))) {
  891. al->filtered |= (1 << HIST_FILTER__DSO);
  892. }
  893. al->sym = map__find_symbol(al->map, al->addr,
  894. machine->symbol_filter);
  895. }
  896. if (symbol_conf.sym_list &&
  897. (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
  898. al->sym->name))) {
  899. al->filtered |= (1 << HIST_FILTER__SYMBOL);
  900. }
  901. return 0;
  902. }
  903. /*
  904. * The preprocess_sample method will return with reference counts for the
  905. * in it, when done using (and perhaps getting ref counts if needing to
  906. * keep a pointer to one of those entries) it must be paired with
  907. * addr_location__put(), so that the refcounts can be decremented.
  908. */
  909. void addr_location__put(struct addr_location *al)
  910. {
  911. thread__zput(al->thread);
  912. }
  913. bool is_bts_event(struct perf_event_attr *attr)
  914. {
  915. return attr->type == PERF_TYPE_HARDWARE &&
  916. (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  917. attr->sample_period == 1;
  918. }
  919. bool sample_addr_correlates_sym(struct perf_event_attr *attr)
  920. {
  921. if (attr->type == PERF_TYPE_SOFTWARE &&
  922. (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
  923. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  924. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
  925. return true;
  926. if (is_bts_event(attr))
  927. return true;
  928. return false;
  929. }
  930. void perf_event__preprocess_sample_addr(union perf_event *event,
  931. struct perf_sample *sample,
  932. struct thread *thread,
  933. struct addr_location *al)
  934. {
  935. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  936. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
  937. if (!al->map)
  938. thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
  939. sample->addr, al);
  940. al->cpu = sample->cpu;
  941. al->sym = NULL;
  942. if (al->map)
  943. al->sym = map__find_symbol(al->map, al->addr, NULL);
  944. }