event.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. static const char *perf_event__names[] = {
  14. [0] = "TOTAL",
  15. [PERF_RECORD_MMAP] = "MMAP",
  16. [PERF_RECORD_MMAP2] = "MMAP2",
  17. [PERF_RECORD_LOST] = "LOST",
  18. [PERF_RECORD_COMM] = "COMM",
  19. [PERF_RECORD_EXIT] = "EXIT",
  20. [PERF_RECORD_THROTTLE] = "THROTTLE",
  21. [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
  22. [PERF_RECORD_FORK] = "FORK",
  23. [PERF_RECORD_READ] = "READ",
  24. [PERF_RECORD_SAMPLE] = "SAMPLE",
  25. [PERF_RECORD_AUX] = "AUX",
  26. [PERF_RECORD_ITRACE_START] = "ITRACE_START",
  27. [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
  28. [PERF_RECORD_HEADER_ATTR] = "ATTR",
  29. [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
  30. [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
  31. [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
  32. [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
  33. [PERF_RECORD_ID_INDEX] = "ID_INDEX",
  34. [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
  35. [PERF_RECORD_AUXTRACE] = "AUXTRACE",
  36. [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
  37. };
  38. const char *perf_event__name(unsigned int id)
  39. {
  40. if (id >= ARRAY_SIZE(perf_event__names))
  41. return "INVALID";
  42. if (!perf_event__names[id])
  43. return "UNKNOWN";
  44. return perf_event__names[id];
  45. }
  46. static struct perf_sample synth_sample = {
  47. .pid = -1,
  48. .tid = -1,
  49. .time = -1,
  50. .stream_id = -1,
  51. .cpu = -1,
  52. .period = 1,
  53. };
  54. /*
  55. * Assumes that the first 4095 bytes of /proc/pid/stat contains
  56. * the comm, tgid and ppid.
  57. */
  58. static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
  59. pid_t *tgid, pid_t *ppid)
  60. {
  61. char filename[PATH_MAX];
  62. char bf[4096];
  63. int fd;
  64. size_t size = 0, n;
  65. char *nl, *name, *tgids, *ppids;
  66. *tgid = -1;
  67. *ppid = -1;
  68. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  69. fd = open(filename, O_RDONLY);
  70. if (fd < 0) {
  71. pr_debug("couldn't open %s\n", filename);
  72. return -1;
  73. }
  74. n = read(fd, bf, sizeof(bf) - 1);
  75. close(fd);
  76. if (n <= 0) {
  77. pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
  78. pid);
  79. return -1;
  80. }
  81. bf[n] = '\0';
  82. name = strstr(bf, "Name:");
  83. tgids = strstr(bf, "Tgid:");
  84. ppids = strstr(bf, "PPid:");
  85. if (name) {
  86. name += 5; /* strlen("Name:") */
  87. while (*name && isspace(*name))
  88. ++name;
  89. nl = strchr(name, '\n');
  90. if (nl)
  91. *nl = '\0';
  92. size = strlen(name);
  93. if (size >= len)
  94. size = len - 1;
  95. memcpy(comm, name, size);
  96. comm[size] = '\0';
  97. } else {
  98. pr_debug("Name: string not found for pid %d\n", pid);
  99. }
  100. if (tgids) {
  101. tgids += 5; /* strlen("Tgid:") */
  102. *tgid = atoi(tgids);
  103. } else {
  104. pr_debug("Tgid: string not found for pid %d\n", pid);
  105. }
  106. if (ppids) {
  107. ppids += 5; /* strlen("PPid:") */
  108. *ppid = atoi(ppids);
  109. } else {
  110. pr_debug("PPid: string not found for pid %d\n", pid);
  111. }
  112. return 0;
  113. }
  114. static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
  115. struct machine *machine,
  116. pid_t *tgid, pid_t *ppid)
  117. {
  118. size_t size;
  119. *ppid = -1;
  120. memset(&event->comm, 0, sizeof(event->comm));
  121. if (machine__is_host(machine)) {
  122. if (perf_event__get_comm_ids(pid, event->comm.comm,
  123. sizeof(event->comm.comm),
  124. tgid, ppid) != 0) {
  125. return -1;
  126. }
  127. } else {
  128. *tgid = machine->pid;
  129. }
  130. if (*tgid < 0)
  131. return -1;
  132. event->comm.pid = *tgid;
  133. event->comm.header.type = PERF_RECORD_COMM;
  134. size = strlen(event->comm.comm) + 1;
  135. size = PERF_ALIGN(size, sizeof(u64));
  136. memset(event->comm.comm + size, 0, machine->id_hdr_size);
  137. event->comm.header.size = (sizeof(event->comm) -
  138. (sizeof(event->comm.comm) - size) +
  139. machine->id_hdr_size);
  140. event->comm.tid = pid;
  141. return 0;
  142. }
  143. static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
  144. union perf_event *event, pid_t pid,
  145. perf_event__handler_t process,
  146. struct machine *machine)
  147. {
  148. pid_t tgid, ppid;
  149. if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
  150. return -1;
  151. if (process(tool, event, &synth_sample, machine) != 0)
  152. return -1;
  153. return tgid;
  154. }
  155. static int perf_event__synthesize_fork(struct perf_tool *tool,
  156. union perf_event *event,
  157. pid_t pid, pid_t tgid, pid_t ppid,
  158. perf_event__handler_t process,
  159. struct machine *machine)
  160. {
  161. memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
  162. /*
  163. * for main thread set parent to ppid from status file. For other
  164. * threads set parent pid to main thread. ie., assume main thread
  165. * spawns all threads in a process
  166. */
  167. if (tgid == pid) {
  168. event->fork.ppid = ppid;
  169. event->fork.ptid = ppid;
  170. } else {
  171. event->fork.ppid = tgid;
  172. event->fork.ptid = tgid;
  173. }
  174. event->fork.pid = tgid;
  175. event->fork.tid = pid;
  176. event->fork.header.type = PERF_RECORD_FORK;
  177. event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
  178. if (process(tool, event, &synth_sample, machine) != 0)
  179. return -1;
  180. return 0;
  181. }
  182. #define PROC_MAP_PARSE_TIMEOUT (500 * 1000000ULL)
  183. int perf_event__synthesize_mmap_events(struct perf_tool *tool,
  184. union perf_event *event,
  185. pid_t pid, pid_t tgid,
  186. perf_event__handler_t process,
  187. struct machine *machine,
  188. bool mmap_data)
  189. {
  190. char filename[PATH_MAX];
  191. FILE *fp;
  192. unsigned long long t;
  193. bool truncation = false;
  194. int rc = 0;
  195. if (machine__is_default_guest(machine))
  196. return 0;
  197. snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
  198. machine->root_dir, pid);
  199. fp = fopen(filename, "r");
  200. if (fp == NULL) {
  201. /*
  202. * We raced with a task exiting - just return:
  203. */
  204. pr_debug("couldn't open %s\n", filename);
  205. return -1;
  206. }
  207. event->header.type = PERF_RECORD_MMAP2;
  208. t = rdclock();
  209. while (1) {
  210. char bf[BUFSIZ];
  211. char prot[5];
  212. char execname[PATH_MAX];
  213. char anonstr[] = "//anon";
  214. unsigned int ino;
  215. size_t size;
  216. ssize_t n;
  217. if (fgets(bf, sizeof(bf), fp) == NULL)
  218. break;
  219. if ((rdclock() - t) > PROC_MAP_PARSE_TIMEOUT) {
  220. pr_warning("Reading %s time out.\n", filename);
  221. truncation = true;
  222. goto out;
  223. }
  224. /* ensure null termination since stack will be reused. */
  225. strcpy(execname, "");
  226. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  227. n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
  228. &event->mmap2.start, &event->mmap2.len, prot,
  229. &event->mmap2.pgoff, &event->mmap2.maj,
  230. &event->mmap2.min,
  231. &ino, execname);
  232. /*
  233. * Anon maps don't have the execname.
  234. */
  235. if (n < 7)
  236. continue;
  237. event->mmap2.ino = (u64)ino;
  238. /*
  239. * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
  240. */
  241. if (machine__is_host(machine))
  242. event->header.misc = PERF_RECORD_MISC_USER;
  243. else
  244. event->header.misc = PERF_RECORD_MISC_GUEST_USER;
  245. /* map protection and flags bits */
  246. event->mmap2.prot = 0;
  247. event->mmap2.flags = 0;
  248. if (prot[0] == 'r')
  249. event->mmap2.prot |= PROT_READ;
  250. if (prot[1] == 'w')
  251. event->mmap2.prot |= PROT_WRITE;
  252. if (prot[2] == 'x')
  253. event->mmap2.prot |= PROT_EXEC;
  254. if (prot[3] == 's')
  255. event->mmap2.flags |= MAP_SHARED;
  256. else
  257. event->mmap2.flags |= MAP_PRIVATE;
  258. if (prot[2] != 'x') {
  259. if (!mmap_data || prot[0] != 'r')
  260. continue;
  261. event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  262. }
  263. out:
  264. if (truncation)
  265. event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
  266. if (!strcmp(execname, ""))
  267. strcpy(execname, anonstr);
  268. size = strlen(execname) + 1;
  269. memcpy(event->mmap2.filename, execname, size);
  270. size = PERF_ALIGN(size, sizeof(u64));
  271. event->mmap2.len -= event->mmap.start;
  272. event->mmap2.header.size = (sizeof(event->mmap2) -
  273. (sizeof(event->mmap2.filename) - size));
  274. memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
  275. event->mmap2.header.size += machine->id_hdr_size;
  276. event->mmap2.pid = tgid;
  277. event->mmap2.tid = pid;
  278. if (process(tool, event, &synth_sample, machine) != 0) {
  279. rc = -1;
  280. break;
  281. }
  282. if (truncation)
  283. break;
  284. }
  285. fclose(fp);
  286. return rc;
  287. }
  288. int perf_event__synthesize_modules(struct perf_tool *tool,
  289. perf_event__handler_t process,
  290. struct machine *machine)
  291. {
  292. int rc = 0;
  293. struct map *pos;
  294. struct map_groups *kmaps = &machine->kmaps;
  295. struct maps *maps = &kmaps->maps[MAP__FUNCTION];
  296. union perf_event *event = zalloc((sizeof(event->mmap) +
  297. machine->id_hdr_size));
  298. if (event == NULL) {
  299. pr_debug("Not enough memory synthesizing mmap event "
  300. "for kernel modules\n");
  301. return -1;
  302. }
  303. event->header.type = PERF_RECORD_MMAP;
  304. /*
  305. * kernel uses 0 for user space maps, see kernel/perf_event.c
  306. * __perf_event_mmap
  307. */
  308. if (machine__is_host(machine))
  309. event->header.misc = PERF_RECORD_MISC_KERNEL;
  310. else
  311. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  312. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  313. size_t size;
  314. if (pos->dso->kernel)
  315. continue;
  316. size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  317. event->mmap.header.type = PERF_RECORD_MMAP;
  318. event->mmap.header.size = (sizeof(event->mmap) -
  319. (sizeof(event->mmap.filename) - size));
  320. memset(event->mmap.filename + size, 0, machine->id_hdr_size);
  321. event->mmap.header.size += machine->id_hdr_size;
  322. event->mmap.start = pos->start;
  323. event->mmap.len = pos->end - pos->start;
  324. event->mmap.pid = machine->pid;
  325. memcpy(event->mmap.filename, pos->dso->long_name,
  326. pos->dso->long_name_len + 1);
  327. if (process(tool, event, &synth_sample, machine) != 0) {
  328. rc = -1;
  329. break;
  330. }
  331. }
  332. free(event);
  333. return rc;
  334. }
  335. static int __event__synthesize_thread(union perf_event *comm_event,
  336. union perf_event *mmap_event,
  337. union perf_event *fork_event,
  338. pid_t pid, int full,
  339. perf_event__handler_t process,
  340. struct perf_tool *tool,
  341. struct machine *machine, bool mmap_data)
  342. {
  343. char filename[PATH_MAX];
  344. DIR *tasks;
  345. struct dirent dirent, *next;
  346. pid_t tgid, ppid;
  347. int rc = 0;
  348. /* special case: only send one comm event using passed in pid */
  349. if (!full) {
  350. tgid = perf_event__synthesize_comm(tool, comm_event, pid,
  351. process, machine);
  352. if (tgid == -1)
  353. return -1;
  354. return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  355. process, machine, mmap_data);
  356. }
  357. if (machine__is_default_guest(machine))
  358. return 0;
  359. snprintf(filename, sizeof(filename), "%s/proc/%d/task",
  360. machine->root_dir, pid);
  361. tasks = opendir(filename);
  362. if (tasks == NULL) {
  363. pr_debug("couldn't open %s\n", filename);
  364. return 0;
  365. }
  366. while (!readdir_r(tasks, &dirent, &next) && next) {
  367. char *end;
  368. pid_t _pid;
  369. _pid = strtol(dirent.d_name, &end, 10);
  370. if (*end)
  371. continue;
  372. rc = -1;
  373. if (perf_event__prepare_comm(comm_event, _pid, machine,
  374. &tgid, &ppid) != 0)
  375. break;
  376. if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
  377. ppid, process, machine) < 0)
  378. break;
  379. /*
  380. * Send the prepared comm event
  381. */
  382. if (process(tool, comm_event, &synth_sample, machine) != 0)
  383. break;
  384. rc = 0;
  385. if (_pid == pid) {
  386. /* process the parent's maps too */
  387. rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
  388. process, machine, mmap_data);
  389. if (rc)
  390. break;
  391. }
  392. }
  393. closedir(tasks);
  394. return rc;
  395. }
  396. int perf_event__synthesize_thread_map(struct perf_tool *tool,
  397. struct thread_map *threads,
  398. perf_event__handler_t process,
  399. struct machine *machine,
  400. bool mmap_data)
  401. {
  402. union perf_event *comm_event, *mmap_event, *fork_event;
  403. int err = -1, thread, j;
  404. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  405. if (comm_event == NULL)
  406. goto out;
  407. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  408. if (mmap_event == NULL)
  409. goto out_free_comm;
  410. fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
  411. if (fork_event == NULL)
  412. goto out_free_mmap;
  413. err = 0;
  414. for (thread = 0; thread < threads->nr; ++thread) {
  415. if (__event__synthesize_thread(comm_event, mmap_event,
  416. fork_event,
  417. threads->map[thread], 0,
  418. process, tool, machine,
  419. mmap_data)) {
  420. err = -1;
  421. break;
  422. }
  423. /*
  424. * comm.pid is set to thread group id by
  425. * perf_event__synthesize_comm
  426. */
  427. if ((int) comm_event->comm.pid != threads->map[thread]) {
  428. bool need_leader = true;
  429. /* is thread group leader in thread_map? */
  430. for (j = 0; j < threads->nr; ++j) {
  431. if ((int) comm_event->comm.pid == threads->map[j]) {
  432. need_leader = false;
  433. break;
  434. }
  435. }
  436. /* if not, generate events for it */
  437. if (need_leader &&
  438. __event__synthesize_thread(comm_event, mmap_event,
  439. fork_event,
  440. comm_event->comm.pid, 0,
  441. process, tool, machine,
  442. mmap_data)) {
  443. err = -1;
  444. break;
  445. }
  446. }
  447. }
  448. free(fork_event);
  449. out_free_mmap:
  450. free(mmap_event);
  451. out_free_comm:
  452. free(comm_event);
  453. out:
  454. return err;
  455. }
  456. int perf_event__synthesize_threads(struct perf_tool *tool,
  457. perf_event__handler_t process,
  458. struct machine *machine, bool mmap_data)
  459. {
  460. DIR *proc;
  461. char proc_path[PATH_MAX];
  462. struct dirent dirent, *next;
  463. union perf_event *comm_event, *mmap_event, *fork_event;
  464. int err = -1;
  465. if (machine__is_default_guest(machine))
  466. return 0;
  467. comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
  468. if (comm_event == NULL)
  469. goto out;
  470. mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
  471. if (mmap_event == NULL)
  472. goto out_free_comm;
  473. fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
  474. if (fork_event == NULL)
  475. goto out_free_mmap;
  476. snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
  477. proc = opendir(proc_path);
  478. if (proc == NULL)
  479. goto out_free_fork;
  480. while (!readdir_r(proc, &dirent, &next) && next) {
  481. char *end;
  482. pid_t pid = strtol(dirent.d_name, &end, 10);
  483. if (*end) /* only interested in proper numerical dirents */
  484. continue;
  485. /*
  486. * We may race with exiting thread, so don't stop just because
  487. * one thread couldn't be synthesized.
  488. */
  489. __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
  490. 1, process, tool, machine, mmap_data);
  491. }
  492. err = 0;
  493. closedir(proc);
  494. out_free_fork:
  495. free(fork_event);
  496. out_free_mmap:
  497. free(mmap_event);
  498. out_free_comm:
  499. free(comm_event);
  500. out:
  501. return err;
  502. }
  503. struct process_symbol_args {
  504. const char *name;
  505. u64 start;
  506. };
  507. static int find_symbol_cb(void *arg, const char *name, char type,
  508. u64 start)
  509. {
  510. struct process_symbol_args *args = arg;
  511. /*
  512. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  513. * an 'A' to the same address as "_stext".
  514. */
  515. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  516. type == 'A') || strcmp(name, args->name))
  517. return 0;
  518. args->start = start;
  519. return 1;
  520. }
  521. u64 kallsyms__get_function_start(const char *kallsyms_filename,
  522. const char *symbol_name)
  523. {
  524. struct process_symbol_args args = { .name = symbol_name, };
  525. if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
  526. return 0;
  527. return args.start;
  528. }
  529. int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
  530. perf_event__handler_t process,
  531. struct machine *machine)
  532. {
  533. size_t size;
  534. const char *mmap_name;
  535. char name_buff[PATH_MAX];
  536. struct map *map;
  537. struct kmap *kmap;
  538. int err;
  539. union perf_event *event;
  540. if (machine->vmlinux_maps[0] == NULL)
  541. return -1;
  542. /*
  543. * We should get this from /sys/kernel/sections/.text, but till that is
  544. * available use this, and after it is use this as a fallback for older
  545. * kernels.
  546. */
  547. event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
  548. if (event == NULL) {
  549. pr_debug("Not enough memory synthesizing mmap event "
  550. "for kernel modules\n");
  551. return -1;
  552. }
  553. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  554. if (machine__is_host(machine)) {
  555. /*
  556. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  557. * see kernel/perf_event.c __perf_event_mmap
  558. */
  559. event->header.misc = PERF_RECORD_MISC_KERNEL;
  560. } else {
  561. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  562. }
  563. map = machine->vmlinux_maps[MAP__FUNCTION];
  564. kmap = map__kmap(map);
  565. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  566. "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
  567. size = PERF_ALIGN(size, sizeof(u64));
  568. event->mmap.header.type = PERF_RECORD_MMAP;
  569. event->mmap.header.size = (sizeof(event->mmap) -
  570. (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
  571. event->mmap.pgoff = kmap->ref_reloc_sym->addr;
  572. event->mmap.start = map->start;
  573. event->mmap.len = map->end - event->mmap.start;
  574. event->mmap.pid = machine->pid;
  575. err = process(tool, event, &synth_sample, machine);
  576. free(event);
  577. return err;
  578. }
  579. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  580. {
  581. const char *s;
  582. if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
  583. s = " exec";
  584. else
  585. s = "";
  586. return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
  587. }
  588. int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
  589. union perf_event *event,
  590. struct perf_sample *sample,
  591. struct machine *machine)
  592. {
  593. return machine__process_comm_event(machine, event, sample);
  594. }
  595. int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
  596. union perf_event *event,
  597. struct perf_sample *sample,
  598. struct machine *machine)
  599. {
  600. return machine__process_lost_event(machine, event, sample);
  601. }
  602. int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
  603. union perf_event *event,
  604. struct perf_sample *sample __maybe_unused,
  605. struct machine *machine)
  606. {
  607. return machine__process_aux_event(machine, event);
  608. }
  609. int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
  610. union perf_event *event,
  611. struct perf_sample *sample __maybe_unused,
  612. struct machine *machine)
  613. {
  614. return machine__process_itrace_start_event(machine, event);
  615. }
  616. int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
  617. union perf_event *event,
  618. struct perf_sample *sample,
  619. struct machine *machine)
  620. {
  621. return machine__process_lost_samples_event(machine, event, sample);
  622. }
  623. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  624. {
  625. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
  626. event->mmap.pid, event->mmap.tid, event->mmap.start,
  627. event->mmap.len, event->mmap.pgoff,
  628. (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
  629. event->mmap.filename);
  630. }
  631. size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
  632. {
  633. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
  634. " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
  635. event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
  636. event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
  637. event->mmap2.min, event->mmap2.ino,
  638. event->mmap2.ino_generation,
  639. (event->mmap2.prot & PROT_READ) ? 'r' : '-',
  640. (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
  641. (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
  642. (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
  643. event->mmap2.filename);
  644. }
  645. int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
  646. union perf_event *event,
  647. struct perf_sample *sample,
  648. struct machine *machine)
  649. {
  650. return machine__process_mmap_event(machine, event, sample);
  651. }
  652. int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
  653. union perf_event *event,
  654. struct perf_sample *sample,
  655. struct machine *machine)
  656. {
  657. return machine__process_mmap2_event(machine, event, sample);
  658. }
  659. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  660. {
  661. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  662. event->fork.pid, event->fork.tid,
  663. event->fork.ppid, event->fork.ptid);
  664. }
  665. int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
  666. union perf_event *event,
  667. struct perf_sample *sample,
  668. struct machine *machine)
  669. {
  670. return machine__process_fork_event(machine, event, sample);
  671. }
  672. int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
  673. union perf_event *event,
  674. struct perf_sample *sample,
  675. struct machine *machine)
  676. {
  677. return machine__process_exit_event(machine, event, sample);
  678. }
  679. size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
  680. {
  681. return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
  682. event->aux.aux_offset, event->aux.aux_size,
  683. event->aux.flags,
  684. event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
  685. event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
  686. }
  687. size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
  688. {
  689. return fprintf(fp, " pid: %u tid: %u\n",
  690. event->itrace_start.pid, event->itrace_start.tid);
  691. }
  692. size_t perf_event__fprintf(union perf_event *event, FILE *fp)
  693. {
  694. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  695. perf_event__name(event->header.type));
  696. switch (event->header.type) {
  697. case PERF_RECORD_COMM:
  698. ret += perf_event__fprintf_comm(event, fp);
  699. break;
  700. case PERF_RECORD_FORK:
  701. case PERF_RECORD_EXIT:
  702. ret += perf_event__fprintf_task(event, fp);
  703. break;
  704. case PERF_RECORD_MMAP:
  705. ret += perf_event__fprintf_mmap(event, fp);
  706. break;
  707. case PERF_RECORD_MMAP2:
  708. ret += perf_event__fprintf_mmap2(event, fp);
  709. break;
  710. case PERF_RECORD_AUX:
  711. ret += perf_event__fprintf_aux(event, fp);
  712. break;
  713. case PERF_RECORD_ITRACE_START:
  714. ret += perf_event__fprintf_itrace_start(event, fp);
  715. break;
  716. default:
  717. ret += fprintf(fp, "\n");
  718. }
  719. return ret;
  720. }
  721. int perf_event__process(struct perf_tool *tool __maybe_unused,
  722. union perf_event *event,
  723. struct perf_sample *sample,
  724. struct machine *machine)
  725. {
  726. return machine__process_event(machine, event, sample);
  727. }
  728. void thread__find_addr_map(struct thread *thread, u8 cpumode,
  729. enum map_type type, u64 addr,
  730. struct addr_location *al)
  731. {
  732. struct map_groups *mg = thread->mg;
  733. struct machine *machine = mg->machine;
  734. bool load_map = false;
  735. al->machine = machine;
  736. al->thread = thread;
  737. al->addr = addr;
  738. al->cpumode = cpumode;
  739. al->filtered = 0;
  740. if (machine == NULL) {
  741. al->map = NULL;
  742. return;
  743. }
  744. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  745. al->level = 'k';
  746. mg = &machine->kmaps;
  747. load_map = true;
  748. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  749. al->level = '.';
  750. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  751. al->level = 'g';
  752. mg = &machine->kmaps;
  753. load_map = true;
  754. } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
  755. al->level = 'u';
  756. } else {
  757. al->level = 'H';
  758. al->map = NULL;
  759. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  760. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  761. !perf_guest)
  762. al->filtered |= (1 << HIST_FILTER__GUEST);
  763. if ((cpumode == PERF_RECORD_MISC_USER ||
  764. cpumode == PERF_RECORD_MISC_KERNEL) &&
  765. !perf_host)
  766. al->filtered |= (1 << HIST_FILTER__HOST);
  767. return;
  768. }
  769. try_again:
  770. al->map = map_groups__find(mg, type, al->addr);
  771. if (al->map == NULL) {
  772. /*
  773. * If this is outside of all known maps, and is a negative
  774. * address, try to look it up in the kernel dso, as it might be
  775. * a vsyscall or vdso (which executes in user-mode).
  776. *
  777. * XXX This is nasty, we should have a symbol list in the
  778. * "[vdso]" dso, but for now lets use the old trick of looking
  779. * in the whole kernel symbol list.
  780. */
  781. if (cpumode == PERF_RECORD_MISC_USER && machine &&
  782. mg != &machine->kmaps &&
  783. machine__kernel_ip(machine, al->addr)) {
  784. mg = &machine->kmaps;
  785. load_map = true;
  786. goto try_again;
  787. }
  788. } else {
  789. /*
  790. * Kernel maps might be changed when loading symbols so loading
  791. * must be done prior to using kernel maps.
  792. */
  793. if (load_map)
  794. map__load(al->map, machine->symbol_filter);
  795. al->addr = al->map->map_ip(al->map, al->addr);
  796. }
  797. }
  798. void thread__find_addr_location(struct thread *thread,
  799. u8 cpumode, enum map_type type, u64 addr,
  800. struct addr_location *al)
  801. {
  802. thread__find_addr_map(thread, cpumode, type, addr, al);
  803. if (al->map != NULL)
  804. al->sym = map__find_symbol(al->map, al->addr,
  805. thread->mg->machine->symbol_filter);
  806. else
  807. al->sym = NULL;
  808. }
  809. /*
  810. * Callers need to drop the reference to al->thread, obtained in
  811. * machine__findnew_thread()
  812. */
  813. int perf_event__preprocess_sample(const union perf_event *event,
  814. struct machine *machine,
  815. struct addr_location *al,
  816. struct perf_sample *sample)
  817. {
  818. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  819. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  820. sample->tid);
  821. if (thread == NULL)
  822. return -1;
  823. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
  824. /*
  825. * Have we already created the kernel maps for this machine?
  826. *
  827. * This should have happened earlier, when we processed the kernel MMAP
  828. * events, but for older perf.data files there was no such thing, so do
  829. * it now.
  830. */
  831. if (cpumode == PERF_RECORD_MISC_KERNEL &&
  832. machine->vmlinux_maps[MAP__FUNCTION] == NULL)
  833. machine__create_kernel_maps(machine);
  834. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
  835. dump_printf(" ...... dso: %s\n",
  836. al->map ? al->map->dso->long_name :
  837. al->level == 'H' ? "[hypervisor]" : "<not found>");
  838. if (thread__is_filtered(thread))
  839. al->filtered |= (1 << HIST_FILTER__THREAD);
  840. al->sym = NULL;
  841. al->cpu = sample->cpu;
  842. if (al->map) {
  843. struct dso *dso = al->map->dso;
  844. if (symbol_conf.dso_list &&
  845. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  846. dso->short_name) ||
  847. (dso->short_name != dso->long_name &&
  848. strlist__has_entry(symbol_conf.dso_list,
  849. dso->long_name))))) {
  850. al->filtered |= (1 << HIST_FILTER__DSO);
  851. }
  852. al->sym = map__find_symbol(al->map, al->addr,
  853. machine->symbol_filter);
  854. }
  855. if (symbol_conf.sym_list &&
  856. (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
  857. al->sym->name))) {
  858. al->filtered |= (1 << HIST_FILTER__SYMBOL);
  859. }
  860. return 0;
  861. }
  862. /*
  863. * The preprocess_sample method will return with reference counts for the
  864. * in it, when done using (and perhaps getting ref counts if needing to
  865. * keep a pointer to one of those entries) it must be paired with
  866. * addr_location__put(), so that the refcounts can be decremented.
  867. */
  868. void addr_location__put(struct addr_location *al)
  869. {
  870. thread__zput(al->thread);
  871. }
  872. bool is_bts_event(struct perf_event_attr *attr)
  873. {
  874. return attr->type == PERF_TYPE_HARDWARE &&
  875. (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  876. attr->sample_period == 1;
  877. }
  878. bool sample_addr_correlates_sym(struct perf_event_attr *attr)
  879. {
  880. if (attr->type == PERF_TYPE_SOFTWARE &&
  881. (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
  882. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  883. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
  884. return true;
  885. if (is_bts_event(attr))
  886. return true;
  887. return false;
  888. }
  889. void perf_event__preprocess_sample_addr(union perf_event *event,
  890. struct perf_sample *sample,
  891. struct thread *thread,
  892. struct addr_location *al)
  893. {
  894. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  895. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
  896. if (!al->map)
  897. thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
  898. sample->addr, al);
  899. al->cpu = sample->cpu;
  900. al->sym = NULL;
  901. if (al->map)
  902. al->sym = map__find_symbol(al->map, al->addr, NULL);
  903. }