event.c 20 KB

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