event.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. int perf_event__process_comm(union perf_event *event,
  366. struct perf_sample *sample __used,
  367. struct perf_session *session)
  368. {
  369. struct thread *thread = perf_session__findnew(session, event->comm.tid);
  370. dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
  371. if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
  372. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. int perf_event__process_lost(union perf_event *event,
  378. struct perf_sample *sample __used,
  379. struct perf_session *session)
  380. {
  381. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  382. event->lost.id, event->lost.lost);
  383. session->hists.stats.total_lost += event->lost.lost;
  384. return 0;
  385. }
  386. static void perf_event__set_kernel_mmap_len(union perf_event *event,
  387. struct map **maps)
  388. {
  389. maps[MAP__FUNCTION]->start = event->mmap.start;
  390. maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
  391. /*
  392. * Be a bit paranoid here, some perf.data file came with
  393. * a zero sized synthesized MMAP event for the kernel.
  394. */
  395. if (maps[MAP__FUNCTION]->end == 0)
  396. maps[MAP__FUNCTION]->end = ~0ULL;
  397. }
  398. static int perf_event__process_kernel_mmap(union perf_event *event,
  399. struct perf_session *session)
  400. {
  401. struct map *map;
  402. char kmmap_prefix[PATH_MAX];
  403. struct machine *machine;
  404. enum dso_kernel_type kernel_type;
  405. bool is_kernel_mmap;
  406. machine = perf_session__findnew_machine(session, event->mmap.pid);
  407. if (!machine) {
  408. pr_err("Can't find id %d's machine\n", event->mmap.pid);
  409. goto out_problem;
  410. }
  411. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  412. if (machine__is_host(machine))
  413. kernel_type = DSO_TYPE_KERNEL;
  414. else
  415. kernel_type = DSO_TYPE_GUEST_KERNEL;
  416. is_kernel_mmap = memcmp(event->mmap.filename,
  417. kmmap_prefix,
  418. strlen(kmmap_prefix)) == 0;
  419. if (event->mmap.filename[0] == '/' ||
  420. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  421. char short_module_name[1024];
  422. char *name, *dot;
  423. if (event->mmap.filename[0] == '/') {
  424. name = strrchr(event->mmap.filename, '/');
  425. if (name == NULL)
  426. goto out_problem;
  427. ++name; /* skip / */
  428. dot = strrchr(name, '.');
  429. if (dot == NULL)
  430. goto out_problem;
  431. snprintf(short_module_name, sizeof(short_module_name),
  432. "[%.*s]", (int)(dot - name), name);
  433. strxfrchar(short_module_name, '-', '_');
  434. } else
  435. strcpy(short_module_name, event->mmap.filename);
  436. map = machine__new_module(machine, event->mmap.start,
  437. event->mmap.filename);
  438. if (map == NULL)
  439. goto out_problem;
  440. name = strdup(short_module_name);
  441. if (name == NULL)
  442. goto out_problem;
  443. map->dso->short_name = name;
  444. map->dso->sname_alloc = 1;
  445. map->end = map->start + event->mmap.len;
  446. } else if (is_kernel_mmap) {
  447. const char *symbol_name = (event->mmap.filename +
  448. strlen(kmmap_prefix));
  449. /*
  450. * Should be there already, from the build-id table in
  451. * the header.
  452. */
  453. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  454. kmmap_prefix);
  455. if (kernel == NULL)
  456. goto out_problem;
  457. kernel->kernel = kernel_type;
  458. if (__machine__create_kernel_maps(machine, kernel) < 0)
  459. goto out_problem;
  460. perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
  461. /*
  462. * Avoid using a zero address (kptr_restrict) for the ref reloc
  463. * symbol. Effectively having zero here means that at record
  464. * time /proc/sys/kernel/kptr_restrict was non zero.
  465. */
  466. if (event->mmap.pgoff != 0) {
  467. perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  468. symbol_name,
  469. event->mmap.pgoff);
  470. }
  471. if (machine__is_default_guest(machine)) {
  472. /*
  473. * preload dso of guest kernel and modules
  474. */
  475. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  476. NULL);
  477. }
  478. }
  479. return 0;
  480. out_problem:
  481. return -1;
  482. }
  483. int perf_event__process_mmap(union perf_event *event,
  484. struct perf_sample *sample __used,
  485. struct perf_session *session)
  486. {
  487. struct machine *machine;
  488. struct thread *thread;
  489. struct map *map;
  490. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  491. int ret = 0;
  492. dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
  493. event->mmap.pid, event->mmap.tid, event->mmap.start,
  494. event->mmap.len, event->mmap.pgoff, event->mmap.filename);
  495. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  496. cpumode == PERF_RECORD_MISC_KERNEL) {
  497. ret = perf_event__process_kernel_mmap(event, session);
  498. if (ret < 0)
  499. goto out_problem;
  500. return 0;
  501. }
  502. machine = perf_session__find_host_machine(session);
  503. if (machine == NULL)
  504. goto out_problem;
  505. thread = perf_session__findnew(session, event->mmap.pid);
  506. if (thread == NULL)
  507. goto out_problem;
  508. map = map__new(&machine->user_dsos, event->mmap.start,
  509. event->mmap.len, event->mmap.pgoff,
  510. event->mmap.pid, event->mmap.filename,
  511. MAP__FUNCTION);
  512. if (map == NULL)
  513. goto out_problem;
  514. thread__insert_map(thread, map);
  515. return 0;
  516. out_problem:
  517. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  518. return 0;
  519. }
  520. int perf_event__process_task(union perf_event *event,
  521. struct perf_sample *sample __used,
  522. struct perf_session *session)
  523. {
  524. struct thread *thread = perf_session__findnew(session, event->fork.tid);
  525. struct thread *parent = perf_session__findnew(session, event->fork.ptid);
  526. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  527. event->fork.ppid, event->fork.ptid);
  528. if (event->header.type == PERF_RECORD_EXIT) {
  529. perf_session__remove_thread(session, thread);
  530. return 0;
  531. }
  532. if (thread == NULL || parent == NULL ||
  533. thread__fork(thread, parent) < 0) {
  534. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  535. return -1;
  536. }
  537. return 0;
  538. }
  539. int perf_event__process(union perf_event *event, struct perf_sample *sample,
  540. struct perf_session *session)
  541. {
  542. switch (event->header.type) {
  543. case PERF_RECORD_COMM:
  544. perf_event__process_comm(event, sample, session);
  545. break;
  546. case PERF_RECORD_MMAP:
  547. perf_event__process_mmap(event, sample, session);
  548. break;
  549. case PERF_RECORD_FORK:
  550. case PERF_RECORD_EXIT:
  551. perf_event__process_task(event, sample, session);
  552. break;
  553. case PERF_RECORD_LOST:
  554. perf_event__process_lost(event, sample, session);
  555. default:
  556. break;
  557. }
  558. return 0;
  559. }
  560. void thread__find_addr_map(struct thread *self,
  561. struct perf_session *session, u8 cpumode,
  562. enum map_type type, pid_t pid, u64 addr,
  563. struct addr_location *al)
  564. {
  565. struct map_groups *mg = &self->mg;
  566. struct machine *machine = NULL;
  567. al->thread = self;
  568. al->addr = addr;
  569. al->cpumode = cpumode;
  570. al->filtered = false;
  571. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  572. al->level = 'k';
  573. machine = perf_session__find_host_machine(session);
  574. if (machine == NULL) {
  575. al->map = NULL;
  576. return;
  577. }
  578. mg = &machine->kmaps;
  579. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  580. al->level = '.';
  581. machine = perf_session__find_host_machine(session);
  582. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  583. al->level = 'g';
  584. machine = perf_session__find_machine(session, pid);
  585. if (machine == NULL) {
  586. al->map = NULL;
  587. return;
  588. }
  589. mg = &machine->kmaps;
  590. } else {
  591. /*
  592. * 'u' means guest os user space.
  593. * TODO: We don't support guest user space. Might support late.
  594. */
  595. if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
  596. al->level = 'u';
  597. else
  598. al->level = 'H';
  599. al->map = NULL;
  600. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  601. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  602. !perf_guest)
  603. al->filtered = true;
  604. if ((cpumode == PERF_RECORD_MISC_USER ||
  605. cpumode == PERF_RECORD_MISC_KERNEL) &&
  606. !perf_host)
  607. al->filtered = true;
  608. return;
  609. }
  610. try_again:
  611. al->map = map_groups__find(mg, type, al->addr);
  612. if (al->map == NULL) {
  613. /*
  614. * If this is outside of all known maps, and is a negative
  615. * address, try to look it up in the kernel dso, as it might be
  616. * a vsyscall or vdso (which executes in user-mode).
  617. *
  618. * XXX This is nasty, we should have a symbol list in the
  619. * "[vdso]" dso, but for now lets use the old trick of looking
  620. * in the whole kernel symbol list.
  621. */
  622. if ((long long)al->addr < 0 &&
  623. cpumode == PERF_RECORD_MISC_USER &&
  624. machine && mg != &machine->kmaps) {
  625. mg = &machine->kmaps;
  626. goto try_again;
  627. }
  628. } else
  629. al->addr = al->map->map_ip(al->map, al->addr);
  630. }
  631. void thread__find_addr_location(struct thread *self,
  632. struct perf_session *session, u8 cpumode,
  633. enum map_type type, pid_t pid, u64 addr,
  634. struct addr_location *al,
  635. symbol_filter_t filter)
  636. {
  637. thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
  638. if (al->map != NULL)
  639. al->sym = map__find_symbol(al->map, al->addr, filter);
  640. else
  641. al->sym = NULL;
  642. }
  643. int perf_event__preprocess_sample(const union perf_event *event,
  644. struct perf_session *session,
  645. struct addr_location *al,
  646. struct perf_sample *sample,
  647. symbol_filter_t filter)
  648. {
  649. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  650. struct thread *thread = perf_session__findnew(session, event->ip.pid);
  651. if (thread == NULL)
  652. return -1;
  653. if (symbol_conf.comm_list &&
  654. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  655. goto out_filtered;
  656. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  657. /*
  658. * Have we already created the kernel maps for the host machine?
  659. *
  660. * This should have happened earlier, when we processed the kernel MMAP
  661. * events, but for older perf.data files there was no such thing, so do
  662. * it now.
  663. */
  664. if (cpumode == PERF_RECORD_MISC_KERNEL &&
  665. session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
  666. machine__create_kernel_maps(&session->host_machine);
  667. thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
  668. event->ip.pid, event->ip.ip, al);
  669. dump_printf(" ...... dso: %s\n",
  670. al->map ? al->map->dso->long_name :
  671. al->level == 'H' ? "[hypervisor]" : "<not found>");
  672. al->sym = NULL;
  673. al->cpu = sample->cpu;
  674. if (al->map) {
  675. if (symbol_conf.dso_list &&
  676. (!al->map || !al->map->dso ||
  677. !(strlist__has_entry(symbol_conf.dso_list,
  678. al->map->dso->short_name) ||
  679. (al->map->dso->short_name != al->map->dso->long_name &&
  680. strlist__has_entry(symbol_conf.dso_list,
  681. al->map->dso->long_name)))))
  682. goto out_filtered;
  683. al->sym = map__find_symbol(al->map, al->addr, filter);
  684. }
  685. if (symbol_conf.sym_list && al->sym &&
  686. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  687. goto out_filtered;
  688. return 0;
  689. out_filtered:
  690. al->filtered = true;
  691. return 0;
  692. }