event.c 24 KB

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