event.c 22 KB

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