event.c 20 KB

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