event.c 42 KB

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