event.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. /*
  179. * Anon maps don't have the execname.
  180. */
  181. if (n < 4)
  182. continue;
  183. /*
  184. * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
  185. */
  186. event->header.misc = PERF_RECORD_MISC_USER;
  187. if (prot[2] != 'x') {
  188. if (!mmap_data || prot[0] != 'r')
  189. continue;
  190. event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  191. }
  192. if (!strcmp(execname, ""))
  193. strcpy(execname, anonstr);
  194. size = strlen(execname) + 1;
  195. memcpy(event->mmap.filename, execname, size);
  196. size = PERF_ALIGN(size, sizeof(u64));
  197. event->mmap.len -= event->mmap.start;
  198. event->mmap.header.size = (sizeof(event->mmap) -
  199. (sizeof(event->mmap.filename) - size));
  200. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  201. event->mmap.header.size += machine->id_hdr_size;
  202. event->mmap.pid = tgid;
  203. event->mmap.tid = pid;
  204. if (process(tool, event, &synth_sample, machine) != 0) {
  205. rc = -1;
  206. break;
  207. }
  208. }
  209. fclose(fp);
  210. return rc;
  211. }
  212. int perf_event__synthesize_modules(struct perf_tool *tool,
  213. perf_event__handler_t process,
  214. struct machine *machine)
  215. {
  216. int rc = 0;
  217. struct rb_node *nd;
  218. struct map_groups *kmaps = &machine->kmaps;
  219. union perf_event *event = zalloc((sizeof(event->mmap) +
  220. machine->id_hdr_size));
  221. if (event == NULL) {
  222. pr_debug("Not enough memory synthesizing mmap event "
  223. "for kernel modules\n");
  224. return -1;
  225. }
  226. event->header.type = PERF_RECORD_MMAP;
  227. /*
  228. * kernel uses 0 for user space maps, see kernel/perf_event.c
  229. * __perf_event_mmap
  230. */
  231. if (machine__is_host(machine))
  232. event->header.misc = PERF_RECORD_MISC_KERNEL;
  233. else
  234. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  235. for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
  236. nd; nd = rb_next(nd)) {
  237. size_t size;
  238. struct map *pos = rb_entry(nd, struct map, rb_node);
  239. if (pos->dso->kernel)
  240. continue;
  241. size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  242. event->mmap.header.type = PERF_RECORD_MMAP;
  243. event->mmap.header.size = (sizeof(event->mmap) -
  244. (sizeof(event->mmap.filename) - size));
  245. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  246. event->mmap.header.size += machine->id_hdr_size;
  247. event->mmap.start = pos->start;
  248. event->mmap.len = pos->end - pos->start;
  249. event->mmap.pid = machine->pid;
  250. memcpy(event->mmap.filename, pos->dso->long_name,
  251. pos->dso->long_name_len + 1);
  252. if (process(tool, event, &synth_sample, machine) != 0) {
  253. rc = -1;
  254. break;
  255. }
  256. }
  257. free(event);
  258. return rc;
  259. }
  260. static int __event__synthesize_thread(union perf_event *comm_event,
  261. union perf_event *mmap_event,
  262. pid_t pid, int full,
  263. perf_event__handler_t process,
  264. struct perf_tool *tool,
  265. struct machine *machine, bool mmap_data)
  266. {
  267. pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
  268. process, machine);
  269. if (tgid == -1)
  270. return -1;
  271. return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  272. process, machine, mmap_data);
  273. }
  274. int perf_event__synthesize_thread_map(struct perf_tool *tool,
  275. struct thread_map *threads,
  276. perf_event__handler_t process,
  277. struct machine *machine,
  278. bool mmap_data)
  279. {
  280. union perf_event *comm_event, *mmap_event;
  281. int err = -1, thread, j;
  282. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  283. if (comm_event == NULL)
  284. goto out;
  285. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  286. if (mmap_event == NULL)
  287. goto out_free_comm;
  288. err = 0;
  289. for (thread = 0; thread < threads->nr; ++thread) {
  290. if (__event__synthesize_thread(comm_event, mmap_event,
  291. threads->map[thread], 0,
  292. process, tool, machine,
  293. mmap_data)) {
  294. err = -1;
  295. break;
  296. }
  297. /*
  298. * comm.pid is set to thread group id by
  299. * perf_event__synthesize_comm
  300. */
  301. if ((int) comm_event->comm.pid != threads->map[thread]) {
  302. bool need_leader = true;
  303. /* is thread group leader in thread_map? */
  304. for (j = 0; j < threads->nr; ++j) {
  305. if ((int) comm_event->comm.pid == threads->map[j]) {
  306. need_leader = false;
  307. break;
  308. }
  309. }
  310. /* if not, generate events for it */
  311. if (need_leader &&
  312. __event__synthesize_thread(comm_event, mmap_event,
  313. comm_event->comm.pid, 0,
  314. process, tool, machine,
  315. mmap_data)) {
  316. err = -1;
  317. break;
  318. }
  319. }
  320. }
  321. free(mmap_event);
  322. out_free_comm:
  323. free(comm_event);
  324. out:
  325. return err;
  326. }
  327. int perf_event__synthesize_threads(struct perf_tool *tool,
  328. perf_event__handler_t process,
  329. struct machine *machine, bool mmap_data)
  330. {
  331. DIR *proc;
  332. struct dirent dirent, *next;
  333. union perf_event *comm_event, *mmap_event;
  334. int err = -1;
  335. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  336. if (comm_event == NULL)
  337. goto out;
  338. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  339. if (mmap_event == NULL)
  340. goto out_free_comm;
  341. proc = opendir("/proc");
  342. if (proc == NULL)
  343. goto out_free_mmap;
  344. while (!readdir_r(proc, &dirent, &next) && next) {
  345. char *end;
  346. pid_t pid = strtol(dirent.d_name, &end, 10);
  347. if (*end) /* only interested in proper numerical dirents */
  348. continue;
  349. /*
  350. * We may race with exiting thread, so don't stop just because
  351. * one thread couldn't be synthesized.
  352. */
  353. __event__synthesize_thread(comm_event, mmap_event, pid, 1,
  354. process, tool, machine, mmap_data);
  355. }
  356. err = 0;
  357. closedir(proc);
  358. out_free_mmap:
  359. free(mmap_event);
  360. out_free_comm:
  361. free(comm_event);
  362. out:
  363. return err;
  364. }
  365. struct process_symbol_args {
  366. const char *name;
  367. u64 start;
  368. };
  369. static int find_symbol_cb(void *arg, const char *name, char type,
  370. u64 start)
  371. {
  372. struct process_symbol_args *args = arg;
  373. /*
  374. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  375. * an 'A' to the same address as "_stext".
  376. */
  377. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  378. type == 'A') || strcmp(name, args->name))
  379. return 0;
  380. args->start = start;
  381. return 1;
  382. }
  383. int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
  384. perf_event__handler_t process,
  385. struct machine *machine,
  386. const char *symbol_name)
  387. {
  388. size_t size;
  389. const char *filename, *mmap_name;
  390. char path[PATH_MAX];
  391. char name_buff[PATH_MAX];
  392. struct map *map;
  393. int err;
  394. /*
  395. * We should get this from /sys/kernel/sections/.text, but till that is
  396. * available use this, and after it is use this as a fallback for older
  397. * kernels.
  398. */
  399. struct process_symbol_args args = { .name = symbol_name, };
  400. union perf_event *event = zalloc((sizeof(event->mmap) +
  401. machine->id_hdr_size));
  402. if (event == NULL) {
  403. pr_debug("Not enough memory synthesizing mmap event "
  404. "for kernel modules\n");
  405. return -1;
  406. }
  407. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  408. if (machine__is_host(machine)) {
  409. /*
  410. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  411. * see kernel/perf_event.c __perf_event_mmap
  412. */
  413. event->header.misc = PERF_RECORD_MISC_KERNEL;
  414. filename = "/proc/kallsyms";
  415. } else {
  416. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  417. if (machine__is_default_guest(machine))
  418. filename = (char *) symbol_conf.default_guest_kallsyms;
  419. else {
  420. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  421. filename = path;
  422. }
  423. }
  424. if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
  425. free(event);
  426. return -ENOENT;
  427. }
  428. map = machine->vmlinux_maps[MAP__FUNCTION];
  429. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  430. "%s%s", mmap_name, symbol_name) + 1;
  431. size = PERF_ALIGN(size, sizeof(u64));
  432. event->mmap.header.type = PERF_RECORD_MMAP;
  433. event->mmap.header.size = (sizeof(event->mmap) -
  434. (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
  435. event->mmap.pgoff = args.start;
  436. event->mmap.start = map->start;
  437. event->mmap.len = map->end - event->mmap.start;
  438. event->mmap.pid = machine->pid;
  439. err = process(tool, event, &synth_sample, machine);
  440. free(event);
  441. return err;
  442. }
  443. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  444. {
  445. return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
  446. }
  447. int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
  448. union perf_event *event,
  449. struct perf_sample *sample,
  450. struct machine *machine)
  451. {
  452. return machine__process_comm_event(machine, event, sample);
  453. }
  454. int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
  455. union perf_event *event,
  456. struct perf_sample *sample,
  457. struct machine *machine)
  458. {
  459. return machine__process_lost_event(machine, event, sample);
  460. }
  461. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  462. {
  463. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
  464. event->mmap.pid, event->mmap.tid, event->mmap.start,
  465. event->mmap.len, event->mmap.pgoff,
  466. (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
  467. event->mmap.filename);
  468. }
  469. size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
  470. {
  471. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
  472. " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
  473. event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
  474. event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
  475. event->mmap2.min, event->mmap2.ino,
  476. event->mmap2.ino_generation,
  477. (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
  478. event->mmap2.filename);
  479. }
  480. int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
  481. union perf_event *event,
  482. struct perf_sample *sample,
  483. struct machine *machine)
  484. {
  485. return machine__process_mmap_event(machine, event, sample);
  486. }
  487. int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
  488. union perf_event *event,
  489. struct perf_sample *sample,
  490. struct machine *machine)
  491. {
  492. return machine__process_mmap2_event(machine, event, sample);
  493. }
  494. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  495. {
  496. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  497. event->fork.pid, event->fork.tid,
  498. event->fork.ppid, event->fork.ptid);
  499. }
  500. int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
  501. union perf_event *event,
  502. struct perf_sample *sample,
  503. struct machine *machine)
  504. {
  505. return machine__process_fork_event(machine, event, sample);
  506. }
  507. int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
  508. union perf_event *event,
  509. struct perf_sample *sample,
  510. struct machine *machine)
  511. {
  512. return machine__process_exit_event(machine, event, sample);
  513. }
  514. size_t perf_event__fprintf(union perf_event *event, FILE *fp)
  515. {
  516. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  517. perf_event__name(event->header.type));
  518. switch (event->header.type) {
  519. case PERF_RECORD_COMM:
  520. ret += perf_event__fprintf_comm(event, fp);
  521. break;
  522. case PERF_RECORD_FORK:
  523. case PERF_RECORD_EXIT:
  524. ret += perf_event__fprintf_task(event, fp);
  525. break;
  526. case PERF_RECORD_MMAP:
  527. ret += perf_event__fprintf_mmap(event, fp);
  528. break;
  529. case PERF_RECORD_MMAP2:
  530. ret += perf_event__fprintf_mmap2(event, fp);
  531. break;
  532. default:
  533. ret += fprintf(fp, "\n");
  534. }
  535. return ret;
  536. }
  537. int perf_event__process(struct perf_tool *tool __maybe_unused,
  538. union perf_event *event,
  539. struct perf_sample *sample,
  540. struct machine *machine)
  541. {
  542. return machine__process_event(machine, event, sample);
  543. }
  544. void thread__find_addr_map(struct thread *thread,
  545. struct machine *machine, u8 cpumode,
  546. enum map_type type, u64 addr,
  547. struct addr_location *al)
  548. {
  549. struct map_groups *mg = &thread->mg;
  550. bool load_map = false;
  551. al->thread = thread;
  552. al->addr = addr;
  553. al->cpumode = cpumode;
  554. al->filtered = false;
  555. if (machine == NULL) {
  556. al->map = NULL;
  557. return;
  558. }
  559. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  560. al->level = 'k';
  561. mg = &machine->kmaps;
  562. load_map = true;
  563. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  564. al->level = '.';
  565. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  566. al->level = 'g';
  567. mg = &machine->kmaps;
  568. load_map = true;
  569. } else {
  570. /*
  571. * 'u' means guest os user space.
  572. * TODO: We don't support guest user space. Might support late.
  573. */
  574. if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
  575. al->level = 'u';
  576. else
  577. al->level = 'H';
  578. al->map = NULL;
  579. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  580. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  581. !perf_guest)
  582. al->filtered = true;
  583. if ((cpumode == PERF_RECORD_MISC_USER ||
  584. cpumode == PERF_RECORD_MISC_KERNEL) &&
  585. !perf_host)
  586. al->filtered = true;
  587. return;
  588. }
  589. try_again:
  590. al->map = map_groups__find(mg, type, al->addr);
  591. if (al->map == NULL) {
  592. /*
  593. * If this is outside of all known maps, and is a negative
  594. * address, try to look it up in the kernel dso, as it might be
  595. * a vsyscall or vdso (which executes in user-mode).
  596. *
  597. * XXX This is nasty, we should have a symbol list in the
  598. * "[vdso]" dso, but for now lets use the old trick of looking
  599. * in the whole kernel symbol list.
  600. */
  601. if ((long long)al->addr < 0 &&
  602. cpumode == PERF_RECORD_MISC_USER &&
  603. machine && mg != &machine->kmaps) {
  604. mg = &machine->kmaps;
  605. goto try_again;
  606. }
  607. } else {
  608. /*
  609. * Kernel maps might be changed when loading symbols so loading
  610. * must be done prior to using kernel maps.
  611. */
  612. if (load_map)
  613. map__load(al->map, machine->symbol_filter);
  614. al->addr = al->map->map_ip(al->map, al->addr);
  615. }
  616. }
  617. void thread__find_addr_location(struct thread *thread, struct machine *machine,
  618. u8 cpumode, enum map_type type, u64 addr,
  619. struct addr_location *al)
  620. {
  621. thread__find_addr_map(thread, machine, cpumode, type, addr, al);
  622. if (al->map != NULL)
  623. al->sym = map__find_symbol(al->map, al->addr,
  624. machine->symbol_filter);
  625. else
  626. al->sym = NULL;
  627. }
  628. int perf_event__preprocess_sample(const union perf_event *event,
  629. struct machine *machine,
  630. struct addr_location *al,
  631. struct perf_sample *sample)
  632. {
  633. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  634. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  635. sample->pid);
  636. if (thread == NULL)
  637. return -1;
  638. if (symbol_conf.comm_list &&
  639. !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread)))
  640. goto out_filtered;
  641. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
  642. /*
  643. * Have we already created the kernel maps for this machine?
  644. *
  645. * This should have happened earlier, when we processed the kernel MMAP
  646. * events, but for older perf.data files there was no such thing, so do
  647. * it now.
  648. */
  649. if (cpumode == PERF_RECORD_MISC_KERNEL &&
  650. machine->vmlinux_maps[MAP__FUNCTION] == NULL)
  651. machine__create_kernel_maps(machine);
  652. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  653. sample->ip, al);
  654. dump_printf(" ...... dso: %s\n",
  655. al->map ? al->map->dso->long_name :
  656. al->level == 'H' ? "[hypervisor]" : "<not found>");
  657. al->sym = NULL;
  658. al->cpu = sample->cpu;
  659. if (al->map) {
  660. struct dso *dso = al->map->dso;
  661. if (symbol_conf.dso_list &&
  662. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  663. dso->short_name) ||
  664. (dso->short_name != dso->long_name &&
  665. strlist__has_entry(symbol_conf.dso_list,
  666. dso->long_name)))))
  667. goto out_filtered;
  668. al->sym = map__find_symbol(al->map, al->addr,
  669. machine->symbol_filter);
  670. }
  671. if (symbol_conf.sym_list &&
  672. (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
  673. al->sym->name)))
  674. goto out_filtered;
  675. return 0;
  676. out_filtered:
  677. al->filtered = true;
  678. return 0;
  679. }