event.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 anonstr[] = "//anon\n";
  149. char *execname = strchr(bf, '/');
  150. /* Catch VDSO */
  151. if (execname == NULL)
  152. execname = strstr(bf, "[vdso]");
  153. /* Catch anonymous mmaps */
  154. if ((execname == NULL) && !strstr(bf, "["))
  155. execname = anonstr;
  156. if (execname == NULL)
  157. continue;
  158. pbf += 3;
  159. n = hex2u64(pbf, &event->mmap.pgoff);
  160. size = strlen(execname);
  161. execname[size - 1] = '\0'; /* Remove \n */
  162. memcpy(event->mmap.filename, execname, size);
  163. size = ALIGN(size, sizeof(u64));
  164. event->mmap.len -= event->mmap.start;
  165. event->mmap.header.size = (sizeof(event->mmap) -
  166. (sizeof(event->mmap.filename) - size));
  167. memset(event->mmap.filename + size, 0, session->id_hdr_size);
  168. event->mmap.header.size += session->id_hdr_size;
  169. event->mmap.pid = tgid;
  170. event->mmap.tid = pid;
  171. process(event, &synth_sample, session);
  172. }
  173. }
  174. fclose(fp);
  175. return 0;
  176. }
  177. int perf_event__synthesize_modules(perf_event__handler_t process,
  178. struct perf_session *session,
  179. struct machine *machine)
  180. {
  181. struct rb_node *nd;
  182. struct map_groups *kmaps = &machine->kmaps;
  183. union perf_event *event = zalloc((sizeof(event->mmap) +
  184. session->id_hdr_size));
  185. if (event == NULL) {
  186. pr_debug("Not enough memory synthesizing mmap event "
  187. "for kernel modules\n");
  188. return -1;
  189. }
  190. event->header.type = PERF_RECORD_MMAP;
  191. /*
  192. * kernel uses 0 for user space maps, see kernel/perf_event.c
  193. * __perf_event_mmap
  194. */
  195. if (machine__is_host(machine))
  196. event->header.misc = PERF_RECORD_MISC_KERNEL;
  197. else
  198. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  199. for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
  200. nd; nd = rb_next(nd)) {
  201. size_t size;
  202. struct map *pos = rb_entry(nd, struct map, rb_node);
  203. if (pos->dso->kernel)
  204. continue;
  205. size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  206. event->mmap.header.type = PERF_RECORD_MMAP;
  207. event->mmap.header.size = (sizeof(event->mmap) -
  208. (sizeof(event->mmap.filename) - size));
  209. memset(event->mmap.filename + size, 0, session->id_hdr_size);
  210. event->mmap.header.size += session->id_hdr_size;
  211. event->mmap.start = pos->start;
  212. event->mmap.len = pos->end - pos->start;
  213. event->mmap.pid = machine->pid;
  214. memcpy(event->mmap.filename, pos->dso->long_name,
  215. pos->dso->long_name_len + 1);
  216. process(event, &synth_sample, session);
  217. }
  218. free(event);
  219. return 0;
  220. }
  221. static int __event__synthesize_thread(union perf_event *comm_event,
  222. union perf_event *mmap_event,
  223. pid_t pid, perf_event__handler_t process,
  224. struct perf_session *session)
  225. {
  226. pid_t tgid = perf_event__synthesize_comm(comm_event, pid, 1, process,
  227. session);
  228. if (tgid == -1)
  229. return -1;
  230. return perf_event__synthesize_mmap_events(mmap_event, pid, tgid,
  231. process, session);
  232. }
  233. int perf_event__synthesize_thread_map(struct thread_map *threads,
  234. perf_event__handler_t process,
  235. struct perf_session *session)
  236. {
  237. union perf_event *comm_event, *mmap_event;
  238. int err = -1, thread;
  239. comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
  240. if (comm_event == NULL)
  241. goto out;
  242. mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
  243. if (mmap_event == NULL)
  244. goto out_free_comm;
  245. err = 0;
  246. for (thread = 0; thread < threads->nr; ++thread) {
  247. if (__event__synthesize_thread(comm_event, mmap_event,
  248. threads->map[thread],
  249. process, session)) {
  250. err = -1;
  251. break;
  252. }
  253. }
  254. free(mmap_event);
  255. out_free_comm:
  256. free(comm_event);
  257. out:
  258. return err;
  259. }
  260. int perf_event__synthesize_threads(perf_event__handler_t process,
  261. struct perf_session *session)
  262. {
  263. DIR *proc;
  264. struct dirent dirent, *next;
  265. union perf_event *comm_event, *mmap_event;
  266. int err = -1;
  267. comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
  268. if (comm_event == NULL)
  269. goto out;
  270. mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
  271. if (mmap_event == NULL)
  272. goto out_free_comm;
  273. proc = opendir("/proc");
  274. if (proc == NULL)
  275. goto out_free_mmap;
  276. while (!readdir_r(proc, &dirent, &next) && next) {
  277. char *end;
  278. pid_t pid = strtol(dirent.d_name, &end, 10);
  279. if (*end) /* only interested in proper numerical dirents */
  280. continue;
  281. __event__synthesize_thread(comm_event, mmap_event, pid,
  282. process, session);
  283. }
  284. closedir(proc);
  285. err = 0;
  286. out_free_mmap:
  287. free(mmap_event);
  288. out_free_comm:
  289. free(comm_event);
  290. out:
  291. return err;
  292. }
  293. struct process_symbol_args {
  294. const char *name;
  295. u64 start;
  296. };
  297. static int find_symbol_cb(void *arg, const char *name, char type,
  298. u64 start, u64 end __used)
  299. {
  300. struct process_symbol_args *args = arg;
  301. /*
  302. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  303. * an 'A' to the same address as "_stext".
  304. */
  305. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  306. type == 'A') || strcmp(name, args->name))
  307. return 0;
  308. args->start = start;
  309. return 1;
  310. }
  311. int perf_event__synthesize_kernel_mmap(perf_event__handler_t process,
  312. struct perf_session *session,
  313. struct machine *machine,
  314. const char *symbol_name)
  315. {
  316. size_t size;
  317. const char *filename, *mmap_name;
  318. char path[PATH_MAX];
  319. char name_buff[PATH_MAX];
  320. struct map *map;
  321. int err;
  322. /*
  323. * We should get this from /sys/kernel/sections/.text, but till that is
  324. * available use this, and after it is use this as a fallback for older
  325. * kernels.
  326. */
  327. struct process_symbol_args args = { .name = symbol_name, };
  328. union perf_event *event = zalloc((sizeof(event->mmap) +
  329. session->id_hdr_size));
  330. if (event == NULL) {
  331. pr_debug("Not enough memory synthesizing mmap event "
  332. "for kernel modules\n");
  333. return -1;
  334. }
  335. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  336. if (machine__is_host(machine)) {
  337. /*
  338. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  339. * see kernel/perf_event.c __perf_event_mmap
  340. */
  341. event->header.misc = PERF_RECORD_MISC_KERNEL;
  342. filename = "/proc/kallsyms";
  343. } else {
  344. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  345. if (machine__is_default_guest(machine))
  346. filename = (char *) symbol_conf.default_guest_kallsyms;
  347. else {
  348. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  349. filename = path;
  350. }
  351. }
  352. if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
  353. return -ENOENT;
  354. map = machine->vmlinux_maps[MAP__FUNCTION];
  355. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  356. "%s%s", mmap_name, symbol_name) + 1;
  357. size = ALIGN(size, sizeof(u64));
  358. event->mmap.header.type = PERF_RECORD_MMAP;
  359. event->mmap.header.size = (sizeof(event->mmap) -
  360. (sizeof(event->mmap.filename) - size) + session->id_hdr_size);
  361. event->mmap.pgoff = args.start;
  362. event->mmap.start = map->start;
  363. event->mmap.len = map->end - event->mmap.start;
  364. event->mmap.pid = machine->pid;
  365. err = process(event, &synth_sample, session);
  366. free(event);
  367. return err;
  368. }
  369. int perf_event__process_comm(union perf_event *event,
  370. struct perf_sample *sample __used,
  371. struct perf_session *session)
  372. {
  373. struct thread *thread = perf_session__findnew(session, event->comm.tid);
  374. dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
  375. if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
  376. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  377. return -1;
  378. }
  379. return 0;
  380. }
  381. int perf_event__process_lost(union perf_event *event,
  382. struct perf_sample *sample __used,
  383. struct perf_session *session)
  384. {
  385. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  386. event->lost.id, event->lost.lost);
  387. session->hists.stats.total_lost += event->lost.lost;
  388. return 0;
  389. }
  390. static void perf_event__set_kernel_mmap_len(union perf_event *event,
  391. struct map **maps)
  392. {
  393. maps[MAP__FUNCTION]->start = event->mmap.start;
  394. maps[MAP__FUNCTION]->end = event->mmap.start + event->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 perf_event__process_kernel_mmap(union perf_event *event,
  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, event->mmap.pid);
  411. if (!machine) {
  412. pr_err("Can't find id %d's machine\n", event->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(event->mmap.filename,
  421. kmmap_prefix,
  422. strlen(kmmap_prefix)) == 0;
  423. if (event->mmap.filename[0] == '/' ||
  424. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  425. char short_module_name[1024];
  426. char *name, *dot;
  427. if (event->mmap.filename[0] == '/') {
  428. name = strrchr(event->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, event->mmap.filename);
  440. map = machine__new_module(machine, event->mmap.start,
  441. event->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 + event->mmap.len;
  450. } else if (is_kernel_mmap) {
  451. const char *symbol_name = (event->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. perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
  465. /*
  466. * Avoid using a zero address (kptr_restrict) for the ref reloc
  467. * symbol. Effectively having zero here means that at record
  468. * time /proc/sys/kernel/kptr_restrict was non zero.
  469. */
  470. if (event->mmap.pgoff != 0) {
  471. perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  472. symbol_name,
  473. event->mmap.pgoff);
  474. }
  475. if (machine__is_default_guest(machine)) {
  476. /*
  477. * preload dso of guest kernel and modules
  478. */
  479. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  480. NULL);
  481. }
  482. }
  483. return 0;
  484. out_problem:
  485. return -1;
  486. }
  487. int perf_event__process_mmap(union perf_event *event,
  488. struct perf_sample *sample __used,
  489. struct perf_session *session)
  490. {
  491. struct machine *machine;
  492. struct thread *thread;
  493. struct map *map;
  494. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  495. int ret = 0;
  496. dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
  497. event->mmap.pid, event->mmap.tid, event->mmap.start,
  498. event->mmap.len, event->mmap.pgoff, event->mmap.filename);
  499. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  500. cpumode == PERF_RECORD_MISC_KERNEL) {
  501. ret = perf_event__process_kernel_mmap(event, session);
  502. if (ret < 0)
  503. goto out_problem;
  504. return 0;
  505. }
  506. machine = perf_session__find_host_machine(session);
  507. if (machine == NULL)
  508. goto out_problem;
  509. thread = perf_session__findnew(session, event->mmap.pid);
  510. if (thread == NULL)
  511. goto out_problem;
  512. map = map__new(&machine->user_dsos, event->mmap.start,
  513. event->mmap.len, event->mmap.pgoff,
  514. event->mmap.pid, event->mmap.filename,
  515. MAP__FUNCTION);
  516. if (map == NULL)
  517. goto out_problem;
  518. thread__insert_map(thread, map);
  519. return 0;
  520. out_problem:
  521. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  522. return 0;
  523. }
  524. int perf_event__process_task(union perf_event *event,
  525. struct perf_sample *sample __used,
  526. struct perf_session *session)
  527. {
  528. struct thread *thread = perf_session__findnew(session, event->fork.tid);
  529. struct thread *parent = perf_session__findnew(session, event->fork.ptid);
  530. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  531. event->fork.ppid, event->fork.ptid);
  532. if (event->header.type == PERF_RECORD_EXIT) {
  533. perf_session__remove_thread(session, thread);
  534. return 0;
  535. }
  536. if (thread == NULL || parent == NULL ||
  537. thread__fork(thread, parent) < 0) {
  538. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  539. return -1;
  540. }
  541. return 0;
  542. }
  543. int perf_event__process(union perf_event *event, struct perf_sample *sample,
  544. struct perf_session *session)
  545. {
  546. switch (event->header.type) {
  547. case PERF_RECORD_COMM:
  548. perf_event__process_comm(event, sample, session);
  549. break;
  550. case PERF_RECORD_MMAP:
  551. perf_event__process_mmap(event, sample, session);
  552. break;
  553. case PERF_RECORD_FORK:
  554. case PERF_RECORD_EXIT:
  555. perf_event__process_task(event, sample, session);
  556. break;
  557. case PERF_RECORD_LOST:
  558. perf_event__process_lost(event, sample, session);
  559. default:
  560. break;
  561. }
  562. return 0;
  563. }
  564. void thread__find_addr_map(struct thread *self,
  565. struct perf_session *session, u8 cpumode,
  566. enum map_type type, pid_t pid, u64 addr,
  567. struct addr_location *al)
  568. {
  569. struct map_groups *mg = &self->mg;
  570. struct machine *machine = NULL;
  571. al->thread = self;
  572. al->addr = addr;
  573. al->cpumode = cpumode;
  574. al->filtered = false;
  575. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  576. al->level = 'k';
  577. machine = perf_session__find_host_machine(session);
  578. if (machine == NULL) {
  579. al->map = NULL;
  580. return;
  581. }
  582. mg = &machine->kmaps;
  583. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  584. al->level = '.';
  585. machine = perf_session__find_host_machine(session);
  586. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  587. al->level = 'g';
  588. machine = perf_session__find_machine(session, pid);
  589. if (machine == NULL) {
  590. al->map = NULL;
  591. return;
  592. }
  593. mg = &machine->kmaps;
  594. } else {
  595. /*
  596. * 'u' means guest os user space.
  597. * TODO: We don't support guest user space. Might support late.
  598. */
  599. if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
  600. al->level = 'u';
  601. else
  602. al->level = 'H';
  603. al->map = NULL;
  604. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  605. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  606. !perf_guest)
  607. al->filtered = true;
  608. if ((cpumode == PERF_RECORD_MISC_USER ||
  609. cpumode == PERF_RECORD_MISC_KERNEL) &&
  610. !perf_host)
  611. al->filtered = true;
  612. return;
  613. }
  614. try_again:
  615. al->map = map_groups__find(mg, type, al->addr);
  616. if (al->map == NULL) {
  617. /*
  618. * If this is outside of all known maps, and is a negative
  619. * address, try to look it up in the kernel dso, as it might be
  620. * a vsyscall or vdso (which executes in user-mode).
  621. *
  622. * XXX This is nasty, we should have a symbol list in the
  623. * "[vdso]" dso, but for now lets use the old trick of looking
  624. * in the whole kernel symbol list.
  625. */
  626. if ((long long)al->addr < 0 &&
  627. cpumode == PERF_RECORD_MISC_USER &&
  628. machine && mg != &machine->kmaps) {
  629. mg = &machine->kmaps;
  630. goto try_again;
  631. }
  632. } else
  633. al->addr = al->map->map_ip(al->map, al->addr);
  634. }
  635. void thread__find_addr_location(struct thread *self,
  636. struct perf_session *session, u8 cpumode,
  637. enum map_type type, pid_t pid, u64 addr,
  638. struct addr_location *al,
  639. symbol_filter_t filter)
  640. {
  641. thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
  642. if (al->map != NULL)
  643. al->sym = map__find_symbol(al->map, al->addr, filter);
  644. else
  645. al->sym = NULL;
  646. }
  647. int perf_event__preprocess_sample(const union perf_event *event,
  648. struct perf_session *session,
  649. struct addr_location *al,
  650. struct perf_sample *sample,
  651. symbol_filter_t filter)
  652. {
  653. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  654. struct thread *thread = perf_session__findnew(session, event->ip.pid);
  655. if (thread == NULL)
  656. return -1;
  657. if (symbol_conf.comm_list &&
  658. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  659. goto out_filtered;
  660. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  661. /*
  662. * Have we already created the kernel maps for the host machine?
  663. *
  664. * This should have happened earlier, when we processed the kernel MMAP
  665. * events, but for older perf.data files there was no such thing, so do
  666. * it now.
  667. */
  668. if (cpumode == PERF_RECORD_MISC_KERNEL &&
  669. session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
  670. machine__create_kernel_maps(&session->host_machine);
  671. thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
  672. event->ip.pid, event->ip.ip, al);
  673. dump_printf(" ...... dso: %s\n",
  674. al->map ? al->map->dso->long_name :
  675. al->level == 'H' ? "[hypervisor]" : "<not found>");
  676. al->sym = NULL;
  677. al->cpu = sample->cpu;
  678. if (al->map) {
  679. if (symbol_conf.dso_list &&
  680. (!al->map || !al->map->dso ||
  681. !(strlist__has_entry(symbol_conf.dso_list,
  682. al->map->dso->short_name) ||
  683. (al->map->dso->short_name != al->map->dso->long_name &&
  684. strlist__has_entry(symbol_conf.dso_list,
  685. al->map->dso->long_name)))))
  686. goto out_filtered;
  687. al->sym = map__find_symbol(al->map, al->addr, filter);
  688. }
  689. if (symbol_conf.sym_list && al->sym &&
  690. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  691. goto out_filtered;
  692. return 0;
  693. out_filtered:
  694. al->filtered = true;
  695. return 0;
  696. }