event.c 20 KB

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