event.c 21 KB

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