event.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. #include <linux/types.h>
  2. #include <sys/mman.h>
  3. #include "event.h"
  4. #include "debug.h"
  5. #include "hist.h"
  6. #include "machine.h"
  7. #include "sort.h"
  8. #include "string.h"
  9. #include "strlist.h"
  10. #include "thread.h"
  11. #include "thread_map.h"
  12. #include "symbol/kallsyms.h"
  13. #include "asm/bug.h"
  14. #include "stat.h"
  15. static const char *perf_event__names[] = {
  16. [0] = "TOTAL",
  17. [PERF_RECORD_MMAP] = "MMAP",
  18. [PERF_RECORD_MMAP2] = "MMAP2",
  19. [PERF_RECORD_LOST] = "LOST",
  20. [PERF_RECORD_COMM] = "COMM",
  21. [PERF_RECORD_EXIT] = "EXIT",
  22. [PERF_RECORD_THROTTLE] = "THROTTLE",
  23. [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
  24. [PERF_RECORD_FORK] = "FORK",
  25. [PERF_RECORD_READ] = "READ",
  26. [PERF_RECORD_SAMPLE] = "SAMPLE",
  27. [PERF_RECORD_AUX] = "AUX",
  28. [PERF_RECORD_ITRACE_START] = "ITRACE_START",
  29. [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
  30. [PERF_RECORD_SWITCH] = "SWITCH",
  31. [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
  32. [PERF_RECORD_HEADER_ATTR] = "ATTR",
  33. [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
  34. [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
  35. [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
  36. [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
  37. [PERF_RECORD_ID_INDEX] = "ID_INDEX",
  38. [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
  39. [PERF_RECORD_AUXTRACE] = "AUXTRACE",
  40. [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
  41. [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
  42. [PERF_RECORD_CPU_MAP] = "CPU_MAP",
  43. [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
  44. [PERF_RECORD_STAT] = "STAT",
  45. [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
  46. [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
  47. [PERF_RECORD_TIME_CONV] = "TIME_CONV",
  48. };
  49. const char *perf_event__name(unsigned int id)
  50. {
  51. if (id >= ARRAY_SIZE(perf_event__names))
  52. return "INVALID";
  53. if (!perf_event__names[id])
  54. return "UNKNOWN";
  55. return perf_event__names[id];
  56. }
  57. static int perf_tool__process_synth_event(struct perf_tool *tool,
  58. union perf_event *event,
  59. struct machine *machine,
  60. perf_event__handler_t process)
  61. {
  62. struct perf_sample synth_sample = {
  63. .pid = -1,
  64. .tid = -1,
  65. .time = -1,
  66. .stream_id = -1,
  67. .cpu = -1,
  68. .period = 1,
  69. .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
  70. };
  71. return process(tool, event, &synth_sample, machine);
  72. };
  73. /*
  74. * Assumes that the first 4095 bytes of /proc/pid/stat contains
  75. * the comm, tgid and ppid.
  76. */
  77. static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
  78. pid_t *tgid, pid_t *ppid)
  79. {
  80. char filename[PATH_MAX];
  81. char bf[4096];
  82. int fd;
  83. size_t size = 0;
  84. ssize_t n;
  85. char *nl, *name, *tgids, *ppids;
  86. *tgid = -1;
  87. *ppid = -1;
  88. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  89. fd = open(filename, O_RDONLY);
  90. if (fd < 0) {
  91. pr_debug("couldn't open %s\n", filename);
  92. return -1;
  93. }
  94. n = read(fd, bf, sizeof(bf) - 1);
  95. close(fd);
  96. if (n <= 0) {
  97. pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
  98. pid);
  99. return -1;
  100. }
  101. bf[n] = '\0';
  102. name = strstr(bf, "Name:");
  103. tgids = strstr(bf, "Tgid:");
  104. ppids = strstr(bf, "PPid:");
  105. if (name) {
  106. name += 5; /* strlen("Name:") */
  107. while (*name && isspace(*name))
  108. ++name;
  109. nl = strchr(name, '\n');
  110. if (nl)
  111. *nl = '\0';
  112. size = strlen(name);
  113. if (size >= len)
  114. size = len - 1;
  115. memcpy(comm, name, size);
  116. comm[size] = '\0';
  117. } else {
  118. pr_debug("Name: string not found for pid %d\n", pid);
  119. }
  120. if (tgids) {
  121. tgids += 5; /* strlen("Tgid:") */
  122. *tgid = atoi(tgids);
  123. } else {
  124. pr_debug("Tgid: string not found for pid %d\n", pid);
  125. }
  126. if (ppids) {
  127. ppids += 5; /* strlen("PPid:") */
  128. *ppid = atoi(ppids);
  129. } else {
  130. pr_debug("PPid: string not found for pid %d\n", pid);
  131. }
  132. return 0;
  133. }
  134. static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
  135. struct machine *machine,
  136. pid_t *tgid, pid_t *ppid)
  137. {
  138. size_t size;
  139. *ppid = -1;
  140. memset(&event->comm, 0, sizeof(event->comm));
  141. if (machine__is_host(machine)) {
  142. if (perf_event__get_comm_ids(pid, event->comm.comm,
  143. sizeof(event->comm.comm),
  144. tgid, ppid) != 0) {
  145. return -1;
  146. }
  147. } else {
  148. *tgid = machine->pid;
  149. }
  150. if (*tgid < 0)
  151. return -1;
  152. event->comm.pid = *tgid;
  153. event->comm.header.type = PERF_RECORD_COMM;
  154. size = strlen(event->comm.comm) + 1;
  155. size = PERF_ALIGN(size, sizeof(u64));
  156. memset(event->comm.comm + size, 0, machine->id_hdr_size);
  157. event->comm.header.size = (sizeof(event->comm) -
  158. (sizeof(event->comm.comm) - size) +
  159. machine->id_hdr_size);
  160. event->comm.tid = pid;
  161. return 0;
  162. }
  163. pid_t perf_event__synthesize_comm(struct perf_tool *tool,
  164. union perf_event *event, pid_t pid,
  165. perf_event__handler_t process,
  166. struct machine *machine)
  167. {
  168. pid_t tgid, ppid;
  169. if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
  170. return -1;
  171. if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
  172. return -1;
  173. return tgid;
  174. }
  175. static int perf_event__synthesize_fork(struct perf_tool *tool,
  176. union perf_event *event,
  177. pid_t pid, pid_t tgid, pid_t ppid,
  178. perf_event__handler_t process,
  179. struct machine *machine)
  180. {
  181. memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
  182. /*
  183. * for main thread set parent to ppid from status file. For other
  184. * threads set parent pid to main thread. ie., assume main thread
  185. * spawns all threads in a process
  186. */
  187. if (tgid == pid) {
  188. event->fork.ppid = ppid;
  189. event->fork.ptid = ppid;
  190. } else {
  191. event->fork.ppid = tgid;
  192. event->fork.ptid = tgid;
  193. }
  194. event->fork.pid = tgid;
  195. event->fork.tid = pid;
  196. event->fork.header.type = PERF_RECORD_FORK;
  197. event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
  198. if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
  199. return -1;
  200. return 0;
  201. }
  202. int perf_event__synthesize_mmap_events(struct perf_tool *tool,
  203. union perf_event *event,
  204. pid_t pid, pid_t tgid,
  205. perf_event__handler_t process,
  206. struct machine *machine,
  207. bool mmap_data,
  208. unsigned int proc_map_timeout)
  209. {
  210. char filename[PATH_MAX];
  211. FILE *fp;
  212. unsigned long long t;
  213. bool truncation = false;
  214. unsigned long long timeout = proc_map_timeout * 1000000ULL;
  215. int rc = 0;
  216. if (machine__is_default_guest(machine))
  217. return 0;
  218. snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
  219. machine->root_dir, pid);
  220. fp = fopen(filename, "r");
  221. if (fp == NULL) {
  222. /*
  223. * We raced with a task exiting - just return:
  224. */
  225. pr_debug("couldn't open %s\n", filename);
  226. return -1;
  227. }
  228. event->header.type = PERF_RECORD_MMAP2;
  229. t = rdclock();
  230. while (1) {
  231. char bf[BUFSIZ];
  232. char prot[5];
  233. char execname[PATH_MAX];
  234. char anonstr[] = "//anon";
  235. unsigned int ino;
  236. size_t size;
  237. ssize_t n;
  238. if (fgets(bf, sizeof(bf), fp) == NULL)
  239. break;
  240. if ((rdclock() - t) > timeout) {
  241. pr_warning("Reading %s time out. "
  242. "You may want to increase "
  243. "the time limit by --proc-map-timeout\n",
  244. filename);
  245. truncation = true;
  246. goto out;
  247. }
  248. /* ensure null termination since stack will be reused. */
  249. strcpy(execname, "");
  250. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  251. n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
  252. &event->mmap2.start, &event->mmap2.len, prot,
  253. &event->mmap2.pgoff, &event->mmap2.maj,
  254. &event->mmap2.min,
  255. &ino, execname);
  256. /*
  257. * Anon maps don't have the execname.
  258. */
  259. if (n < 7)
  260. continue;
  261. event->mmap2.ino = (u64)ino;
  262. /*
  263. * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
  264. */
  265. if (machine__is_host(machine))
  266. event->header.misc = PERF_RECORD_MISC_USER;
  267. else
  268. event->header.misc = PERF_RECORD_MISC_GUEST_USER;
  269. /* map protection and flags bits */
  270. event->mmap2.prot = 0;
  271. event->mmap2.flags = 0;
  272. if (prot[0] == 'r')
  273. event->mmap2.prot |= PROT_READ;
  274. if (prot[1] == 'w')
  275. event->mmap2.prot |= PROT_WRITE;
  276. if (prot[2] == 'x')
  277. event->mmap2.prot |= PROT_EXEC;
  278. if (prot[3] == 's')
  279. event->mmap2.flags |= MAP_SHARED;
  280. else
  281. event->mmap2.flags |= MAP_PRIVATE;
  282. if (prot[2] != 'x') {
  283. if (!mmap_data || prot[0] != 'r')
  284. continue;
  285. event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  286. }
  287. out:
  288. if (truncation)
  289. event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
  290. if (!strcmp(execname, ""))
  291. strcpy(execname, anonstr);
  292. size = strlen(execname) + 1;
  293. memcpy(event->mmap2.filename, execname, size);
  294. size = PERF_ALIGN(size, sizeof(u64));
  295. event->mmap2.len -= event->mmap.start;
  296. event->mmap2.header.size = (sizeof(event->mmap2) -
  297. (sizeof(event->mmap2.filename) - size));
  298. memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
  299. event->mmap2.header.size += machine->id_hdr_size;
  300. event->mmap2.pid = tgid;
  301. event->mmap2.tid = pid;
  302. if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
  303. rc = -1;
  304. break;
  305. }
  306. if (truncation)
  307. break;
  308. }
  309. fclose(fp);
  310. return rc;
  311. }
  312. int perf_event__synthesize_modules(struct perf_tool *tool,
  313. perf_event__handler_t process,
  314. struct machine *machine)
  315. {
  316. int rc = 0;
  317. struct map *pos;
  318. struct map_groups *kmaps = &machine->kmaps;
  319. struct maps *maps = &kmaps->maps[MAP__FUNCTION];
  320. union perf_event *event = zalloc((sizeof(event->mmap) +
  321. machine->id_hdr_size));
  322. if (event == NULL) {
  323. pr_debug("Not enough memory synthesizing mmap event "
  324. "for kernel modules\n");
  325. return -1;
  326. }
  327. event->header.type = PERF_RECORD_MMAP;
  328. /*
  329. * kernel uses 0 for user space maps, see kernel/perf_event.c
  330. * __perf_event_mmap
  331. */
  332. if (machine__is_host(machine))
  333. event->header.misc = PERF_RECORD_MISC_KERNEL;
  334. else
  335. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  336. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  337. size_t size;
  338. if (__map__is_kernel(pos))
  339. continue;
  340. size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  341. event->mmap.header.type = PERF_RECORD_MMAP;
  342. event->mmap.header.size = (sizeof(event->mmap) -
  343. (sizeof(event->mmap.filename) - size));
  344. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  345. event->mmap.header.size += machine->id_hdr_size;
  346. event->mmap.start = pos->start;
  347. event->mmap.len = pos->end - pos->start;
  348. event->mmap.pid = machine->pid;
  349. memcpy(event->mmap.filename, pos->dso->long_name,
  350. pos->dso->long_name_len + 1);
  351. if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
  352. rc = -1;
  353. break;
  354. }
  355. }
  356. free(event);
  357. return rc;
  358. }
  359. static int __event__synthesize_thread(union perf_event *comm_event,
  360. union perf_event *mmap_event,
  361. union perf_event *fork_event,
  362. pid_t pid, int full,
  363. perf_event__handler_t process,
  364. struct perf_tool *tool,
  365. struct machine *machine,
  366. bool mmap_data,
  367. unsigned int proc_map_timeout)
  368. {
  369. char filename[PATH_MAX];
  370. DIR *tasks;
  371. struct dirent *dirent;
  372. pid_t tgid, ppid;
  373. int rc = 0;
  374. /* special case: only send one comm event using passed in pid */
  375. if (!full) {
  376. tgid = perf_event__synthesize_comm(tool, comm_event, pid,
  377. process, machine);
  378. if (tgid == -1)
  379. return -1;
  380. return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  381. process, machine, mmap_data,
  382. proc_map_timeout);
  383. }
  384. if (machine__is_default_guest(machine))
  385. return 0;
  386. snprintf(filename, sizeof(filename), "%s/proc/%d/task",
  387. machine->root_dir, pid);
  388. tasks = opendir(filename);
  389. if (tasks == NULL) {
  390. pr_debug("couldn't open %s\n", filename);
  391. return 0;
  392. }
  393. while ((dirent = readdir(tasks)) != NULL) {
  394. char *end;
  395. pid_t _pid;
  396. _pid = strtol(dirent->d_name, &end, 10);
  397. if (*end)
  398. continue;
  399. rc = -1;
  400. if (perf_event__prepare_comm(comm_event, _pid, machine,
  401. &tgid, &ppid) != 0)
  402. break;
  403. if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
  404. ppid, process, machine) < 0)
  405. break;
  406. /*
  407. * Send the prepared comm event
  408. */
  409. if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
  410. break;
  411. rc = 0;
  412. if (_pid == pid) {
  413. /* process the parent's maps too */
  414. rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  415. process, machine, mmap_data, proc_map_timeout);
  416. if (rc)
  417. break;
  418. }
  419. }
  420. closedir(tasks);
  421. return rc;
  422. }
  423. int perf_event__synthesize_thread_map(struct perf_tool *tool,
  424. struct thread_map *threads,
  425. perf_event__handler_t process,
  426. struct machine *machine,
  427. bool mmap_data,
  428. unsigned int proc_map_timeout)
  429. {
  430. union perf_event *comm_event, *mmap_event, *fork_event;
  431. int err = -1, thread, j;
  432. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  433. if (comm_event == NULL)
  434. goto out;
  435. mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
  436. if (mmap_event == NULL)
  437. goto out_free_comm;
  438. fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
  439. if (fork_event == NULL)
  440. goto out_free_mmap;
  441. err = 0;
  442. for (thread = 0; thread < threads->nr; ++thread) {
  443. if (__event__synthesize_thread(comm_event, mmap_event,
  444. fork_event,
  445. thread_map__pid(threads, thread), 0,
  446. process, tool, machine,
  447. mmap_data, proc_map_timeout)) {
  448. err = -1;
  449. break;
  450. }
  451. /*
  452. * comm.pid is set to thread group id by
  453. * perf_event__synthesize_comm
  454. */
  455. if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
  456. bool need_leader = true;
  457. /* is thread group leader in thread_map? */
  458. for (j = 0; j < threads->nr; ++j) {
  459. if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
  460. need_leader = false;
  461. break;
  462. }
  463. }
  464. /* if not, generate events for it */
  465. if (need_leader &&
  466. __event__synthesize_thread(comm_event, mmap_event,
  467. fork_event,
  468. comm_event->comm.pid, 0,
  469. process, tool, machine,
  470. mmap_data, proc_map_timeout)) {
  471. err = -1;
  472. break;
  473. }
  474. }
  475. }
  476. free(fork_event);
  477. out_free_mmap:
  478. free(mmap_event);
  479. out_free_comm:
  480. free(comm_event);
  481. out:
  482. return err;
  483. }
  484. int perf_event__synthesize_threads(struct perf_tool *tool,
  485. perf_event__handler_t process,
  486. struct machine *machine,
  487. bool mmap_data,
  488. unsigned int proc_map_timeout)
  489. {
  490. DIR *proc;
  491. char proc_path[PATH_MAX];
  492. struct dirent *dirent;
  493. union perf_event *comm_event, *mmap_event, *fork_event;
  494. int err = -1;
  495. if (machine__is_default_guest(machine))
  496. return 0;
  497. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  498. if (comm_event == NULL)
  499. goto out;
  500. mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
  501. if (mmap_event == NULL)
  502. goto out_free_comm;
  503. fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
  504. if (fork_event == NULL)
  505. goto out_free_mmap;
  506. snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
  507. proc = opendir(proc_path);
  508. if (proc == NULL)
  509. goto out_free_fork;
  510. while ((dirent = readdir(proc)) != NULL) {
  511. char *end;
  512. pid_t pid = strtol(dirent->d_name, &end, 10);
  513. if (*end) /* only interested in proper numerical dirents */
  514. continue;
  515. /*
  516. * We may race with exiting thread, so don't stop just because
  517. * one thread couldn't be synthesized.
  518. */
  519. __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
  520. 1, process, tool, machine, mmap_data,
  521. proc_map_timeout);
  522. }
  523. err = 0;
  524. closedir(proc);
  525. out_free_fork:
  526. free(fork_event);
  527. out_free_mmap:
  528. free(mmap_event);
  529. out_free_comm:
  530. free(comm_event);
  531. out:
  532. return err;
  533. }
  534. struct process_symbol_args {
  535. const char *name;
  536. u64 start;
  537. };
  538. static int find_symbol_cb(void *arg, const char *name, char type,
  539. u64 start)
  540. {
  541. struct process_symbol_args *args = arg;
  542. /*
  543. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  544. * an 'A' to the same address as "_stext".
  545. */
  546. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  547. type == 'A') || strcmp(name, args->name))
  548. return 0;
  549. args->start = start;
  550. return 1;
  551. }
  552. u64 kallsyms__get_function_start(const char *kallsyms_filename,
  553. const char *symbol_name)
  554. {
  555. struct process_symbol_args args = { .name = symbol_name, };
  556. if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
  557. return 0;
  558. return args.start;
  559. }
  560. int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
  561. perf_event__handler_t process,
  562. struct machine *machine)
  563. {
  564. size_t size;
  565. const char *mmap_name;
  566. char name_buff[PATH_MAX];
  567. struct map *map = machine__kernel_map(machine);
  568. struct kmap *kmap;
  569. int err;
  570. union perf_event *event;
  571. if (symbol_conf.kptr_restrict)
  572. return -1;
  573. if (map == NULL)
  574. return -1;
  575. /*
  576. * We should get this from /sys/kernel/sections/.text, but till that is
  577. * available use this, and after it is use this as a fallback for older
  578. * kernels.
  579. */
  580. event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
  581. if (event == NULL) {
  582. pr_debug("Not enough memory synthesizing mmap event "
  583. "for kernel modules\n");
  584. return -1;
  585. }
  586. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  587. if (machine__is_host(machine)) {
  588. /*
  589. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  590. * see kernel/perf_event.c __perf_event_mmap
  591. */
  592. event->header.misc = PERF_RECORD_MISC_KERNEL;
  593. } else {
  594. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  595. }
  596. kmap = map__kmap(map);
  597. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  598. "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
  599. size = PERF_ALIGN(size, sizeof(u64));
  600. event->mmap.header.type = PERF_RECORD_MMAP;
  601. event->mmap.header.size = (sizeof(event->mmap) -
  602. (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
  603. event->mmap.pgoff = kmap->ref_reloc_sym->addr;
  604. event->mmap.start = map->start;
  605. event->mmap.len = map->end - event->mmap.start;
  606. event->mmap.pid = machine->pid;
  607. err = perf_tool__process_synth_event(tool, event, machine, process);
  608. free(event);
  609. return err;
  610. }
  611. int perf_event__synthesize_thread_map2(struct perf_tool *tool,
  612. struct thread_map *threads,
  613. perf_event__handler_t process,
  614. struct machine *machine)
  615. {
  616. union perf_event *event;
  617. int i, err, size;
  618. size = sizeof(event->thread_map);
  619. size += threads->nr * sizeof(event->thread_map.entries[0]);
  620. event = zalloc(size);
  621. if (!event)
  622. return -ENOMEM;
  623. event->header.type = PERF_RECORD_THREAD_MAP;
  624. event->header.size = size;
  625. event->thread_map.nr = threads->nr;
  626. for (i = 0; i < threads->nr; i++) {
  627. struct thread_map_event_entry *entry = &event->thread_map.entries[i];
  628. char *comm = thread_map__comm(threads, i);
  629. if (!comm)
  630. comm = (char *) "";
  631. entry->pid = thread_map__pid(threads, i);
  632. strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
  633. }
  634. err = process(tool, event, NULL, machine);
  635. free(event);
  636. return err;
  637. }
  638. static void synthesize_cpus(struct cpu_map_entries *cpus,
  639. struct cpu_map *map)
  640. {
  641. int i;
  642. cpus->nr = map->nr;
  643. for (i = 0; i < map->nr; i++)
  644. cpus->cpu[i] = map->map[i];
  645. }
  646. static void synthesize_mask(struct cpu_map_mask *mask,
  647. struct cpu_map *map, int max)
  648. {
  649. int i;
  650. mask->nr = BITS_TO_LONGS(max);
  651. mask->long_size = sizeof(long);
  652. for (i = 0; i < map->nr; i++)
  653. set_bit(map->map[i], mask->mask);
  654. }
  655. static size_t cpus_size(struct cpu_map *map)
  656. {
  657. return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
  658. }
  659. static size_t mask_size(struct cpu_map *map, int *max)
  660. {
  661. int i;
  662. *max = 0;
  663. for (i = 0; i < map->nr; i++) {
  664. /* bit possition of the cpu is + 1 */
  665. int bit = map->map[i] + 1;
  666. if (bit > *max)
  667. *max = bit;
  668. }
  669. return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
  670. }
  671. void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
  672. {
  673. size_t size_cpus, size_mask;
  674. bool is_dummy = cpu_map__empty(map);
  675. /*
  676. * Both array and mask data have variable size based
  677. * on the number of cpus and their actual values.
  678. * The size of the 'struct cpu_map_data' is:
  679. *
  680. * array = size of 'struct cpu_map_entries' +
  681. * number of cpus * sizeof(u64)
  682. *
  683. * mask = size of 'struct cpu_map_mask' +
  684. * maximum cpu bit converted to size of longs
  685. *
  686. * and finaly + the size of 'struct cpu_map_data'.
  687. */
  688. size_cpus = cpus_size(map);
  689. size_mask = mask_size(map, max);
  690. if (is_dummy || (size_cpus < size_mask)) {
  691. *size += size_cpus;
  692. *type = PERF_CPU_MAP__CPUS;
  693. } else {
  694. *size += size_mask;
  695. *type = PERF_CPU_MAP__MASK;
  696. }
  697. *size += sizeof(struct cpu_map_data);
  698. return zalloc(*size);
  699. }
  700. void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
  701. u16 type, int max)
  702. {
  703. data->type = type;
  704. switch (type) {
  705. case PERF_CPU_MAP__CPUS:
  706. synthesize_cpus((struct cpu_map_entries *) data->data, map);
  707. break;
  708. case PERF_CPU_MAP__MASK:
  709. synthesize_mask((struct cpu_map_mask *) data->data, map, max);
  710. default:
  711. break;
  712. };
  713. }
  714. static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
  715. {
  716. size_t size = sizeof(struct cpu_map_event);
  717. struct cpu_map_event *event;
  718. int max;
  719. u16 type;
  720. event = cpu_map_data__alloc(map, &size, &type, &max);
  721. if (!event)
  722. return NULL;
  723. event->header.type = PERF_RECORD_CPU_MAP;
  724. event->header.size = size;
  725. event->data.type = type;
  726. cpu_map_data__synthesize(&event->data, map, type, max);
  727. return event;
  728. }
  729. int perf_event__synthesize_cpu_map(struct perf_tool *tool,
  730. struct cpu_map *map,
  731. perf_event__handler_t process,
  732. struct machine *machine)
  733. {
  734. struct cpu_map_event *event;
  735. int err;
  736. event = cpu_map_event__new(map);
  737. if (!event)
  738. return -ENOMEM;
  739. err = process(tool, (union perf_event *) event, NULL, machine);
  740. free(event);
  741. return err;
  742. }
  743. int perf_event__synthesize_stat_config(struct perf_tool *tool,
  744. struct perf_stat_config *config,
  745. perf_event__handler_t process,
  746. struct machine *machine)
  747. {
  748. struct stat_config_event *event;
  749. int size, i = 0, err;
  750. size = sizeof(*event);
  751. size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
  752. event = zalloc(size);
  753. if (!event)
  754. return -ENOMEM;
  755. event->header.type = PERF_RECORD_STAT_CONFIG;
  756. event->header.size = size;
  757. event->nr = PERF_STAT_CONFIG_TERM__MAX;
  758. #define ADD(__term, __val) \
  759. event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
  760. event->data[i].val = __val; \
  761. i++;
  762. ADD(AGGR_MODE, config->aggr_mode)
  763. ADD(INTERVAL, config->interval)
  764. ADD(SCALE, config->scale)
  765. WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
  766. "stat config terms unbalanced\n");
  767. #undef ADD
  768. err = process(tool, (union perf_event *) event, NULL, machine);
  769. free(event);
  770. return err;
  771. }
  772. int perf_event__synthesize_stat(struct perf_tool *tool,
  773. u32 cpu, u32 thread, u64 id,
  774. struct perf_counts_values *count,
  775. perf_event__handler_t process,
  776. struct machine *machine)
  777. {
  778. struct stat_event event;
  779. event.header.type = PERF_RECORD_STAT;
  780. event.header.size = sizeof(event);
  781. event.header.misc = 0;
  782. event.id = id;
  783. event.cpu = cpu;
  784. event.thread = thread;
  785. event.val = count->val;
  786. event.ena = count->ena;
  787. event.run = count->run;
  788. return process(tool, (union perf_event *) &event, NULL, machine);
  789. }
  790. int perf_event__synthesize_stat_round(struct perf_tool *tool,
  791. u64 evtime, u64 type,
  792. perf_event__handler_t process,
  793. struct machine *machine)
  794. {
  795. struct stat_round_event event;
  796. event.header.type = PERF_RECORD_STAT_ROUND;
  797. event.header.size = sizeof(event);
  798. event.header.misc = 0;
  799. event.time = evtime;
  800. event.type = type;
  801. return process(tool, (union perf_event *) &event, NULL, machine);
  802. }
  803. void perf_event__read_stat_config(struct perf_stat_config *config,
  804. struct stat_config_event *event)
  805. {
  806. unsigned i;
  807. for (i = 0; i < event->nr; i++) {
  808. switch (event->data[i].tag) {
  809. #define CASE(__term, __val) \
  810. case PERF_STAT_CONFIG_TERM__##__term: \
  811. config->__val = event->data[i].val; \
  812. break;
  813. CASE(AGGR_MODE, aggr_mode)
  814. CASE(SCALE, scale)
  815. CASE(INTERVAL, interval)
  816. #undef CASE
  817. default:
  818. pr_warning("unknown stat config term %" PRIu64 "\n",
  819. event->data[i].tag);
  820. }
  821. }
  822. }
  823. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  824. {
  825. const char *s;
  826. if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
  827. s = " exec";
  828. else
  829. s = "";
  830. return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
  831. }
  832. int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
  833. union perf_event *event,
  834. struct perf_sample *sample,
  835. struct machine *machine)
  836. {
  837. return machine__process_comm_event(machine, event, sample);
  838. }
  839. int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
  840. union perf_event *event,
  841. struct perf_sample *sample,
  842. struct machine *machine)
  843. {
  844. return machine__process_lost_event(machine, event, sample);
  845. }
  846. int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
  847. union perf_event *event,
  848. struct perf_sample *sample __maybe_unused,
  849. struct machine *machine)
  850. {
  851. return machine__process_aux_event(machine, event);
  852. }
  853. int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
  854. union perf_event *event,
  855. struct perf_sample *sample __maybe_unused,
  856. struct machine *machine)
  857. {
  858. return machine__process_itrace_start_event(machine, event);
  859. }
  860. int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
  861. union perf_event *event,
  862. struct perf_sample *sample,
  863. struct machine *machine)
  864. {
  865. return machine__process_lost_samples_event(machine, event, sample);
  866. }
  867. int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
  868. union perf_event *event,
  869. struct perf_sample *sample __maybe_unused,
  870. struct machine *machine)
  871. {
  872. return machine__process_switch_event(machine, event);
  873. }
  874. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  875. {
  876. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
  877. event->mmap.pid, event->mmap.tid, event->mmap.start,
  878. event->mmap.len, event->mmap.pgoff,
  879. (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
  880. event->mmap.filename);
  881. }
  882. size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
  883. {
  884. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
  885. " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
  886. event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
  887. event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
  888. event->mmap2.min, event->mmap2.ino,
  889. event->mmap2.ino_generation,
  890. (event->mmap2.prot & PROT_READ) ? 'r' : '-',
  891. (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
  892. (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
  893. (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
  894. event->mmap2.filename);
  895. }
  896. size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
  897. {
  898. struct thread_map *threads = thread_map__new_event(&event->thread_map);
  899. size_t ret;
  900. ret = fprintf(fp, " nr: ");
  901. if (threads)
  902. ret += thread_map__fprintf(threads, fp);
  903. else
  904. ret += fprintf(fp, "failed to get threads from event\n");
  905. thread_map__put(threads);
  906. return ret;
  907. }
  908. size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
  909. {
  910. struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
  911. size_t ret;
  912. ret = fprintf(fp, ": ");
  913. if (cpus)
  914. ret += cpu_map__fprintf(cpus, fp);
  915. else
  916. ret += fprintf(fp, "failed to get cpumap from event\n");
  917. cpu_map__put(cpus);
  918. return ret;
  919. }
  920. int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
  921. union perf_event *event,
  922. struct perf_sample *sample,
  923. struct machine *machine)
  924. {
  925. return machine__process_mmap_event(machine, event, sample);
  926. }
  927. int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
  928. union perf_event *event,
  929. struct perf_sample *sample,
  930. struct machine *machine)
  931. {
  932. return machine__process_mmap2_event(machine, event, sample);
  933. }
  934. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  935. {
  936. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  937. event->fork.pid, event->fork.tid,
  938. event->fork.ppid, event->fork.ptid);
  939. }
  940. int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
  941. union perf_event *event,
  942. struct perf_sample *sample,
  943. struct machine *machine)
  944. {
  945. return machine__process_fork_event(machine, event, sample);
  946. }
  947. int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
  948. union perf_event *event,
  949. struct perf_sample *sample,
  950. struct machine *machine)
  951. {
  952. return machine__process_exit_event(machine, event, sample);
  953. }
  954. size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
  955. {
  956. return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
  957. event->aux.aux_offset, event->aux.aux_size,
  958. event->aux.flags,
  959. event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
  960. event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
  961. }
  962. size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
  963. {
  964. return fprintf(fp, " pid: %u tid: %u\n",
  965. event->itrace_start.pid, event->itrace_start.tid);
  966. }
  967. size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
  968. {
  969. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  970. const char *in_out = out ? "OUT" : "IN ";
  971. if (event->header.type == PERF_RECORD_SWITCH)
  972. return fprintf(fp, " %s\n", in_out);
  973. return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
  974. in_out, out ? "next" : "prev",
  975. event->context_switch.next_prev_pid,
  976. event->context_switch.next_prev_tid);
  977. }
  978. size_t perf_event__fprintf(union perf_event *event, FILE *fp)
  979. {
  980. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  981. perf_event__name(event->header.type));
  982. switch (event->header.type) {
  983. case PERF_RECORD_COMM:
  984. ret += perf_event__fprintf_comm(event, fp);
  985. break;
  986. case PERF_RECORD_FORK:
  987. case PERF_RECORD_EXIT:
  988. ret += perf_event__fprintf_task(event, fp);
  989. break;
  990. case PERF_RECORD_MMAP:
  991. ret += perf_event__fprintf_mmap(event, fp);
  992. break;
  993. case PERF_RECORD_MMAP2:
  994. ret += perf_event__fprintf_mmap2(event, fp);
  995. break;
  996. case PERF_RECORD_AUX:
  997. ret += perf_event__fprintf_aux(event, fp);
  998. break;
  999. case PERF_RECORD_ITRACE_START:
  1000. ret += perf_event__fprintf_itrace_start(event, fp);
  1001. break;
  1002. case PERF_RECORD_SWITCH:
  1003. case PERF_RECORD_SWITCH_CPU_WIDE:
  1004. ret += perf_event__fprintf_switch(event, fp);
  1005. break;
  1006. default:
  1007. ret += fprintf(fp, "\n");
  1008. }
  1009. return ret;
  1010. }
  1011. int perf_event__process(struct perf_tool *tool __maybe_unused,
  1012. union perf_event *event,
  1013. struct perf_sample *sample,
  1014. struct machine *machine)
  1015. {
  1016. return machine__process_event(machine, event, sample);
  1017. }
  1018. void thread__find_addr_map(struct thread *thread, u8 cpumode,
  1019. enum map_type type, u64 addr,
  1020. struct addr_location *al)
  1021. {
  1022. struct map_groups *mg = thread->mg;
  1023. struct machine *machine = mg->machine;
  1024. bool load_map = false;
  1025. al->machine = machine;
  1026. al->thread = thread;
  1027. al->addr = addr;
  1028. al->cpumode = cpumode;
  1029. al->filtered = 0;
  1030. if (machine == NULL) {
  1031. al->map = NULL;
  1032. return;
  1033. }
  1034. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  1035. al->level = 'k';
  1036. mg = &machine->kmaps;
  1037. load_map = true;
  1038. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  1039. al->level = '.';
  1040. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  1041. al->level = 'g';
  1042. mg = &machine->kmaps;
  1043. load_map = true;
  1044. } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
  1045. al->level = 'u';
  1046. } else {
  1047. al->level = 'H';
  1048. al->map = NULL;
  1049. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  1050. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  1051. !perf_guest)
  1052. al->filtered |= (1 << HIST_FILTER__GUEST);
  1053. if ((cpumode == PERF_RECORD_MISC_USER ||
  1054. cpumode == PERF_RECORD_MISC_KERNEL) &&
  1055. !perf_host)
  1056. al->filtered |= (1 << HIST_FILTER__HOST);
  1057. return;
  1058. }
  1059. try_again:
  1060. al->map = map_groups__find(mg, type, al->addr);
  1061. if (al->map == NULL) {
  1062. /*
  1063. * If this is outside of all known maps, and is a negative
  1064. * address, try to look it up in the kernel dso, as it might be
  1065. * a vsyscall or vdso (which executes in user-mode).
  1066. *
  1067. * XXX This is nasty, we should have a symbol list in the
  1068. * "[vdso]" dso, but for now lets use the old trick of looking
  1069. * in the whole kernel symbol list.
  1070. */
  1071. if (cpumode == PERF_RECORD_MISC_USER && machine &&
  1072. mg != &machine->kmaps &&
  1073. machine__kernel_ip(machine, al->addr)) {
  1074. mg = &machine->kmaps;
  1075. load_map = true;
  1076. goto try_again;
  1077. }
  1078. } else {
  1079. /*
  1080. * Kernel maps might be changed when loading symbols so loading
  1081. * must be done prior to using kernel maps.
  1082. */
  1083. if (load_map)
  1084. map__load(al->map, machine->symbol_filter);
  1085. al->addr = al->map->map_ip(al->map, al->addr);
  1086. }
  1087. }
  1088. void thread__find_addr_location(struct thread *thread,
  1089. u8 cpumode, enum map_type type, u64 addr,
  1090. struct addr_location *al)
  1091. {
  1092. thread__find_addr_map(thread, cpumode, type, addr, al);
  1093. if (al->map != NULL)
  1094. al->sym = map__find_symbol(al->map, al->addr,
  1095. thread->mg->machine->symbol_filter);
  1096. else
  1097. al->sym = NULL;
  1098. }
  1099. /*
  1100. * Callers need to drop the reference to al->thread, obtained in
  1101. * machine__findnew_thread()
  1102. */
  1103. int machine__resolve(struct machine *machine, struct addr_location *al,
  1104. struct perf_sample *sample)
  1105. {
  1106. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  1107. sample->tid);
  1108. if (thread == NULL)
  1109. return -1;
  1110. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
  1111. /*
  1112. * Have we already created the kernel maps for this machine?
  1113. *
  1114. * This should have happened earlier, when we processed the kernel MMAP
  1115. * events, but for older perf.data files there was no such thing, so do
  1116. * it now.
  1117. */
  1118. if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
  1119. machine__kernel_map(machine) == NULL)
  1120. machine__create_kernel_maps(machine);
  1121. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
  1122. dump_printf(" ...... dso: %s\n",
  1123. al->map ? al->map->dso->long_name :
  1124. al->level == 'H' ? "[hypervisor]" : "<not found>");
  1125. if (thread__is_filtered(thread))
  1126. al->filtered |= (1 << HIST_FILTER__THREAD);
  1127. al->sym = NULL;
  1128. al->cpu = sample->cpu;
  1129. al->socket = -1;
  1130. if (al->cpu >= 0) {
  1131. struct perf_env *env = machine->env;
  1132. if (env && env->cpu)
  1133. al->socket = env->cpu[al->cpu].socket_id;
  1134. }
  1135. if (al->map) {
  1136. struct dso *dso = al->map->dso;
  1137. if (symbol_conf.dso_list &&
  1138. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  1139. dso->short_name) ||
  1140. (dso->short_name != dso->long_name &&
  1141. strlist__has_entry(symbol_conf.dso_list,
  1142. dso->long_name))))) {
  1143. al->filtered |= (1 << HIST_FILTER__DSO);
  1144. }
  1145. al->sym = map__find_symbol(al->map, al->addr,
  1146. machine->symbol_filter);
  1147. }
  1148. if (symbol_conf.sym_list &&
  1149. (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
  1150. al->sym->name))) {
  1151. al->filtered |= (1 << HIST_FILTER__SYMBOL);
  1152. }
  1153. return 0;
  1154. }
  1155. /*
  1156. * The preprocess_sample method will return with reference counts for the
  1157. * in it, when done using (and perhaps getting ref counts if needing to
  1158. * keep a pointer to one of those entries) it must be paired with
  1159. * addr_location__put(), so that the refcounts can be decremented.
  1160. */
  1161. void addr_location__put(struct addr_location *al)
  1162. {
  1163. thread__zput(al->thread);
  1164. }
  1165. bool is_bts_event(struct perf_event_attr *attr)
  1166. {
  1167. return attr->type == PERF_TYPE_HARDWARE &&
  1168. (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  1169. attr->sample_period == 1;
  1170. }
  1171. bool sample_addr_correlates_sym(struct perf_event_attr *attr)
  1172. {
  1173. if (attr->type == PERF_TYPE_SOFTWARE &&
  1174. (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
  1175. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  1176. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
  1177. return true;
  1178. if (is_bts_event(attr))
  1179. return true;
  1180. return false;
  1181. }
  1182. void thread__resolve(struct thread *thread, struct addr_location *al,
  1183. struct perf_sample *sample)
  1184. {
  1185. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
  1186. if (!al->map)
  1187. thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
  1188. sample->addr, al);
  1189. al->cpu = sample->cpu;
  1190. al->sym = NULL;
  1191. if (al->map)
  1192. al->sym = map__find_symbol(al->map, al->addr, NULL);
  1193. }