event.c 22 KB

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