event.c 21 KB

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