event.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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 (map == NULL)
  572. return -1;
  573. /*
  574. * We should get this from /sys/kernel/sections/.text, but till that is
  575. * available use this, and after it is use this as a fallback for older
  576. * kernels.
  577. */
  578. event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
  579. if (event == NULL) {
  580. pr_debug("Not enough memory synthesizing mmap event "
  581. "for kernel modules\n");
  582. return -1;
  583. }
  584. mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
  585. if (machine__is_host(machine)) {
  586. /*
  587. * kernel uses PERF_RECORD_MISC_USER for user space maps,
  588. * see kernel/perf_event.c __perf_event_mmap
  589. */
  590. event->header.misc = PERF_RECORD_MISC_KERNEL;
  591. } else {
  592. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  593. }
  594. kmap = map__kmap(map);
  595. size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
  596. "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
  597. size = PERF_ALIGN(size, sizeof(u64));
  598. event->mmap.header.type = PERF_RECORD_MMAP;
  599. event->mmap.header.size = (sizeof(event->mmap) -
  600. (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
  601. event->mmap.pgoff = kmap->ref_reloc_sym->addr;
  602. event->mmap.start = map->start;
  603. event->mmap.len = map->end - event->mmap.start;
  604. event->mmap.pid = machine->pid;
  605. err = perf_tool__process_synth_event(tool, event, machine, process);
  606. free(event);
  607. return err;
  608. }
  609. int perf_event__synthesize_thread_map2(struct perf_tool *tool,
  610. struct thread_map *threads,
  611. perf_event__handler_t process,
  612. struct machine *machine)
  613. {
  614. union perf_event *event;
  615. int i, err, size;
  616. size = sizeof(event->thread_map);
  617. size += threads->nr * sizeof(event->thread_map.entries[0]);
  618. event = zalloc(size);
  619. if (!event)
  620. return -ENOMEM;
  621. event->header.type = PERF_RECORD_THREAD_MAP;
  622. event->header.size = size;
  623. event->thread_map.nr = threads->nr;
  624. for (i = 0; i < threads->nr; i++) {
  625. struct thread_map_event_entry *entry = &event->thread_map.entries[i];
  626. char *comm = thread_map__comm(threads, i);
  627. if (!comm)
  628. comm = (char *) "";
  629. entry->pid = thread_map__pid(threads, i);
  630. strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
  631. }
  632. err = process(tool, event, NULL, machine);
  633. free(event);
  634. return err;
  635. }
  636. static void synthesize_cpus(struct cpu_map_entries *cpus,
  637. struct cpu_map *map)
  638. {
  639. int i;
  640. cpus->nr = map->nr;
  641. for (i = 0; i < map->nr; i++)
  642. cpus->cpu[i] = map->map[i];
  643. }
  644. static void synthesize_mask(struct cpu_map_mask *mask,
  645. struct cpu_map *map, int max)
  646. {
  647. int i;
  648. mask->nr = BITS_TO_LONGS(max);
  649. mask->long_size = sizeof(long);
  650. for (i = 0; i < map->nr; i++)
  651. set_bit(map->map[i], mask->mask);
  652. }
  653. static size_t cpus_size(struct cpu_map *map)
  654. {
  655. return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
  656. }
  657. static size_t mask_size(struct cpu_map *map, int *max)
  658. {
  659. int i;
  660. *max = 0;
  661. for (i = 0; i < map->nr; i++) {
  662. /* bit possition of the cpu is + 1 */
  663. int bit = map->map[i] + 1;
  664. if (bit > *max)
  665. *max = bit;
  666. }
  667. return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
  668. }
  669. void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
  670. {
  671. size_t size_cpus, size_mask;
  672. bool is_dummy = cpu_map__empty(map);
  673. /*
  674. * Both array and mask data have variable size based
  675. * on the number of cpus and their actual values.
  676. * The size of the 'struct cpu_map_data' is:
  677. *
  678. * array = size of 'struct cpu_map_entries' +
  679. * number of cpus * sizeof(u64)
  680. *
  681. * mask = size of 'struct cpu_map_mask' +
  682. * maximum cpu bit converted to size of longs
  683. *
  684. * and finaly + the size of 'struct cpu_map_data'.
  685. */
  686. size_cpus = cpus_size(map);
  687. size_mask = mask_size(map, max);
  688. if (is_dummy || (size_cpus < size_mask)) {
  689. *size += size_cpus;
  690. *type = PERF_CPU_MAP__CPUS;
  691. } else {
  692. *size += size_mask;
  693. *type = PERF_CPU_MAP__MASK;
  694. }
  695. *size += sizeof(struct cpu_map_data);
  696. return zalloc(*size);
  697. }
  698. void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
  699. u16 type, int max)
  700. {
  701. data->type = type;
  702. switch (type) {
  703. case PERF_CPU_MAP__CPUS:
  704. synthesize_cpus((struct cpu_map_entries *) data->data, map);
  705. break;
  706. case PERF_CPU_MAP__MASK:
  707. synthesize_mask((struct cpu_map_mask *) data->data, map, max);
  708. default:
  709. break;
  710. };
  711. }
  712. static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
  713. {
  714. size_t size = sizeof(struct cpu_map_event);
  715. struct cpu_map_event *event;
  716. int max;
  717. u16 type;
  718. event = cpu_map_data__alloc(map, &size, &type, &max);
  719. if (!event)
  720. return NULL;
  721. event->header.type = PERF_RECORD_CPU_MAP;
  722. event->header.size = size;
  723. event->data.type = type;
  724. cpu_map_data__synthesize(&event->data, map, type, max);
  725. return event;
  726. }
  727. int perf_event__synthesize_cpu_map(struct perf_tool *tool,
  728. struct cpu_map *map,
  729. perf_event__handler_t process,
  730. struct machine *machine)
  731. {
  732. struct cpu_map_event *event;
  733. int err;
  734. event = cpu_map_event__new(map);
  735. if (!event)
  736. return -ENOMEM;
  737. err = process(tool, (union perf_event *) event, NULL, machine);
  738. free(event);
  739. return err;
  740. }
  741. int perf_event__synthesize_stat_config(struct perf_tool *tool,
  742. struct perf_stat_config *config,
  743. perf_event__handler_t process,
  744. struct machine *machine)
  745. {
  746. struct stat_config_event *event;
  747. int size, i = 0, err;
  748. size = sizeof(*event);
  749. size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
  750. event = zalloc(size);
  751. if (!event)
  752. return -ENOMEM;
  753. event->header.type = PERF_RECORD_STAT_CONFIG;
  754. event->header.size = size;
  755. event->nr = PERF_STAT_CONFIG_TERM__MAX;
  756. #define ADD(__term, __val) \
  757. event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
  758. event->data[i].val = __val; \
  759. i++;
  760. ADD(AGGR_MODE, config->aggr_mode)
  761. ADD(INTERVAL, config->interval)
  762. ADD(SCALE, config->scale)
  763. WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
  764. "stat config terms unbalanced\n");
  765. #undef ADD
  766. err = process(tool, (union perf_event *) event, NULL, machine);
  767. free(event);
  768. return err;
  769. }
  770. int perf_event__synthesize_stat(struct perf_tool *tool,
  771. u32 cpu, u32 thread, u64 id,
  772. struct perf_counts_values *count,
  773. perf_event__handler_t process,
  774. struct machine *machine)
  775. {
  776. struct stat_event event;
  777. event.header.type = PERF_RECORD_STAT;
  778. event.header.size = sizeof(event);
  779. event.header.misc = 0;
  780. event.id = id;
  781. event.cpu = cpu;
  782. event.thread = thread;
  783. event.val = count->val;
  784. event.ena = count->ena;
  785. event.run = count->run;
  786. return process(tool, (union perf_event *) &event, NULL, machine);
  787. }
  788. int perf_event__synthesize_stat_round(struct perf_tool *tool,
  789. u64 evtime, u64 type,
  790. perf_event__handler_t process,
  791. struct machine *machine)
  792. {
  793. struct stat_round_event event;
  794. event.header.type = PERF_RECORD_STAT_ROUND;
  795. event.header.size = sizeof(event);
  796. event.header.misc = 0;
  797. event.time = evtime;
  798. event.type = type;
  799. return process(tool, (union perf_event *) &event, NULL, machine);
  800. }
  801. void perf_event__read_stat_config(struct perf_stat_config *config,
  802. struct stat_config_event *event)
  803. {
  804. unsigned i;
  805. for (i = 0; i < event->nr; i++) {
  806. switch (event->data[i].tag) {
  807. #define CASE(__term, __val) \
  808. case PERF_STAT_CONFIG_TERM__##__term: \
  809. config->__val = event->data[i].val; \
  810. break;
  811. CASE(AGGR_MODE, aggr_mode)
  812. CASE(SCALE, scale)
  813. CASE(INTERVAL, interval)
  814. #undef CASE
  815. default:
  816. pr_warning("unknown stat config term %" PRIu64 "\n",
  817. event->data[i].tag);
  818. }
  819. }
  820. }
  821. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  822. {
  823. const char *s;
  824. if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
  825. s = " exec";
  826. else
  827. s = "";
  828. return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
  829. }
  830. int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
  831. union perf_event *event,
  832. struct perf_sample *sample,
  833. struct machine *machine)
  834. {
  835. return machine__process_comm_event(machine, event, sample);
  836. }
  837. int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
  838. union perf_event *event,
  839. struct perf_sample *sample,
  840. struct machine *machine)
  841. {
  842. return machine__process_lost_event(machine, event, sample);
  843. }
  844. int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
  845. union perf_event *event,
  846. struct perf_sample *sample __maybe_unused,
  847. struct machine *machine)
  848. {
  849. return machine__process_aux_event(machine, event);
  850. }
  851. int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
  852. union perf_event *event,
  853. struct perf_sample *sample __maybe_unused,
  854. struct machine *machine)
  855. {
  856. return machine__process_itrace_start_event(machine, event);
  857. }
  858. int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
  859. union perf_event *event,
  860. struct perf_sample *sample,
  861. struct machine *machine)
  862. {
  863. return machine__process_lost_samples_event(machine, event, sample);
  864. }
  865. int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
  866. union perf_event *event,
  867. struct perf_sample *sample __maybe_unused,
  868. struct machine *machine)
  869. {
  870. return machine__process_switch_event(machine, event);
  871. }
  872. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  873. {
  874. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
  875. event->mmap.pid, event->mmap.tid, event->mmap.start,
  876. event->mmap.len, event->mmap.pgoff,
  877. (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
  878. event->mmap.filename);
  879. }
  880. size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
  881. {
  882. return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
  883. " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
  884. event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
  885. event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
  886. event->mmap2.min, event->mmap2.ino,
  887. event->mmap2.ino_generation,
  888. (event->mmap2.prot & PROT_READ) ? 'r' : '-',
  889. (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
  890. (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
  891. (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
  892. event->mmap2.filename);
  893. }
  894. size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
  895. {
  896. struct thread_map *threads = thread_map__new_event(&event->thread_map);
  897. size_t ret;
  898. ret = fprintf(fp, " nr: ");
  899. if (threads)
  900. ret += thread_map__fprintf(threads, fp);
  901. else
  902. ret += fprintf(fp, "failed to get threads from event\n");
  903. thread_map__put(threads);
  904. return ret;
  905. }
  906. size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
  907. {
  908. struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
  909. size_t ret;
  910. ret = fprintf(fp, " nr: ");
  911. if (cpus)
  912. ret += cpu_map__fprintf(cpus, fp);
  913. else
  914. ret += fprintf(fp, "failed to get cpumap from event\n");
  915. cpu_map__put(cpus);
  916. return ret;
  917. }
  918. int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
  919. union perf_event *event,
  920. struct perf_sample *sample,
  921. struct machine *machine)
  922. {
  923. return machine__process_mmap_event(machine, event, sample);
  924. }
  925. int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
  926. union perf_event *event,
  927. struct perf_sample *sample,
  928. struct machine *machine)
  929. {
  930. return machine__process_mmap2_event(machine, event, sample);
  931. }
  932. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  933. {
  934. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  935. event->fork.pid, event->fork.tid,
  936. event->fork.ppid, event->fork.ptid);
  937. }
  938. int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
  939. union perf_event *event,
  940. struct perf_sample *sample,
  941. struct machine *machine)
  942. {
  943. return machine__process_fork_event(machine, event, sample);
  944. }
  945. int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
  946. union perf_event *event,
  947. struct perf_sample *sample,
  948. struct machine *machine)
  949. {
  950. return machine__process_exit_event(machine, event, sample);
  951. }
  952. size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
  953. {
  954. return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
  955. event->aux.aux_offset, event->aux.aux_size,
  956. event->aux.flags,
  957. event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
  958. event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
  959. }
  960. size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
  961. {
  962. return fprintf(fp, " pid: %u tid: %u\n",
  963. event->itrace_start.pid, event->itrace_start.tid);
  964. }
  965. size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
  966. {
  967. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  968. const char *in_out = out ? "OUT" : "IN ";
  969. if (event->header.type == PERF_RECORD_SWITCH)
  970. return fprintf(fp, " %s\n", in_out);
  971. return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
  972. in_out, out ? "next" : "prev",
  973. event->context_switch.next_prev_pid,
  974. event->context_switch.next_prev_tid);
  975. }
  976. size_t perf_event__fprintf(union perf_event *event, FILE *fp)
  977. {
  978. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  979. perf_event__name(event->header.type));
  980. switch (event->header.type) {
  981. case PERF_RECORD_COMM:
  982. ret += perf_event__fprintf_comm(event, fp);
  983. break;
  984. case PERF_RECORD_FORK:
  985. case PERF_RECORD_EXIT:
  986. ret += perf_event__fprintf_task(event, fp);
  987. break;
  988. case PERF_RECORD_MMAP:
  989. ret += perf_event__fprintf_mmap(event, fp);
  990. break;
  991. case PERF_RECORD_MMAP2:
  992. ret += perf_event__fprintf_mmap2(event, fp);
  993. break;
  994. case PERF_RECORD_AUX:
  995. ret += perf_event__fprintf_aux(event, fp);
  996. break;
  997. case PERF_RECORD_ITRACE_START:
  998. ret += perf_event__fprintf_itrace_start(event, fp);
  999. break;
  1000. case PERF_RECORD_SWITCH:
  1001. case PERF_RECORD_SWITCH_CPU_WIDE:
  1002. ret += perf_event__fprintf_switch(event, fp);
  1003. break;
  1004. default:
  1005. ret += fprintf(fp, "\n");
  1006. }
  1007. return ret;
  1008. }
  1009. int perf_event__process(struct perf_tool *tool __maybe_unused,
  1010. union perf_event *event,
  1011. struct perf_sample *sample,
  1012. struct machine *machine)
  1013. {
  1014. return machine__process_event(machine, event, sample);
  1015. }
  1016. void thread__find_addr_map(struct thread *thread, u8 cpumode,
  1017. enum map_type type, u64 addr,
  1018. struct addr_location *al)
  1019. {
  1020. struct map_groups *mg = thread->mg;
  1021. struct machine *machine = mg->machine;
  1022. bool load_map = false;
  1023. al->machine = machine;
  1024. al->thread = thread;
  1025. al->addr = addr;
  1026. al->cpumode = cpumode;
  1027. al->filtered = 0;
  1028. if (machine == NULL) {
  1029. al->map = NULL;
  1030. return;
  1031. }
  1032. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  1033. al->level = 'k';
  1034. mg = &machine->kmaps;
  1035. load_map = true;
  1036. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  1037. al->level = '.';
  1038. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  1039. al->level = 'g';
  1040. mg = &machine->kmaps;
  1041. load_map = true;
  1042. } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
  1043. al->level = 'u';
  1044. } else {
  1045. al->level = 'H';
  1046. al->map = NULL;
  1047. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  1048. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  1049. !perf_guest)
  1050. al->filtered |= (1 << HIST_FILTER__GUEST);
  1051. if ((cpumode == PERF_RECORD_MISC_USER ||
  1052. cpumode == PERF_RECORD_MISC_KERNEL) &&
  1053. !perf_host)
  1054. al->filtered |= (1 << HIST_FILTER__HOST);
  1055. return;
  1056. }
  1057. try_again:
  1058. al->map = map_groups__find(mg, type, al->addr);
  1059. if (al->map == NULL) {
  1060. /*
  1061. * If this is outside of all known maps, and is a negative
  1062. * address, try to look it up in the kernel dso, as it might be
  1063. * a vsyscall or vdso (which executes in user-mode).
  1064. *
  1065. * XXX This is nasty, we should have a symbol list in the
  1066. * "[vdso]" dso, but for now lets use the old trick of looking
  1067. * in the whole kernel symbol list.
  1068. */
  1069. if (cpumode == PERF_RECORD_MISC_USER && machine &&
  1070. mg != &machine->kmaps &&
  1071. machine__kernel_ip(machine, al->addr)) {
  1072. mg = &machine->kmaps;
  1073. load_map = true;
  1074. goto try_again;
  1075. }
  1076. } else {
  1077. /*
  1078. * Kernel maps might be changed when loading symbols so loading
  1079. * must be done prior to using kernel maps.
  1080. */
  1081. if (load_map)
  1082. map__load(al->map, machine->symbol_filter);
  1083. al->addr = al->map->map_ip(al->map, al->addr);
  1084. }
  1085. }
  1086. void thread__find_addr_location(struct thread *thread,
  1087. u8 cpumode, enum map_type type, u64 addr,
  1088. struct addr_location *al)
  1089. {
  1090. thread__find_addr_map(thread, cpumode, type, addr, al);
  1091. if (al->map != NULL)
  1092. al->sym = map__find_symbol(al->map, al->addr,
  1093. thread->mg->machine->symbol_filter);
  1094. else
  1095. al->sym = NULL;
  1096. }
  1097. /*
  1098. * Callers need to drop the reference to al->thread, obtained in
  1099. * machine__findnew_thread()
  1100. */
  1101. int machine__resolve(struct machine *machine, struct addr_location *al,
  1102. struct perf_sample *sample)
  1103. {
  1104. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  1105. sample->tid);
  1106. if (thread == NULL)
  1107. return -1;
  1108. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
  1109. /*
  1110. * Have we already created the kernel maps for this machine?
  1111. *
  1112. * This should have happened earlier, when we processed the kernel MMAP
  1113. * events, but for older perf.data files there was no such thing, so do
  1114. * it now.
  1115. */
  1116. if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
  1117. machine__kernel_map(machine) == NULL)
  1118. machine__create_kernel_maps(machine);
  1119. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
  1120. dump_printf(" ...... dso: %s\n",
  1121. al->map ? al->map->dso->long_name :
  1122. al->level == 'H' ? "[hypervisor]" : "<not found>");
  1123. if (thread__is_filtered(thread))
  1124. al->filtered |= (1 << HIST_FILTER__THREAD);
  1125. al->sym = NULL;
  1126. al->cpu = sample->cpu;
  1127. al->socket = -1;
  1128. if (al->cpu >= 0) {
  1129. struct perf_env *env = machine->env;
  1130. if (env && env->cpu)
  1131. al->socket = env->cpu[al->cpu].socket_id;
  1132. }
  1133. if (al->map) {
  1134. struct dso *dso = al->map->dso;
  1135. if (symbol_conf.dso_list &&
  1136. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  1137. dso->short_name) ||
  1138. (dso->short_name != dso->long_name &&
  1139. strlist__has_entry(symbol_conf.dso_list,
  1140. dso->long_name))))) {
  1141. al->filtered |= (1 << HIST_FILTER__DSO);
  1142. }
  1143. al->sym = map__find_symbol(al->map, al->addr,
  1144. machine->symbol_filter);
  1145. }
  1146. if (symbol_conf.sym_list &&
  1147. (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
  1148. al->sym->name))) {
  1149. al->filtered |= (1 << HIST_FILTER__SYMBOL);
  1150. }
  1151. return 0;
  1152. }
  1153. /*
  1154. * The preprocess_sample method will return with reference counts for the
  1155. * in it, when done using (and perhaps getting ref counts if needing to
  1156. * keep a pointer to one of those entries) it must be paired with
  1157. * addr_location__put(), so that the refcounts can be decremented.
  1158. */
  1159. void addr_location__put(struct addr_location *al)
  1160. {
  1161. thread__zput(al->thread);
  1162. }
  1163. bool is_bts_event(struct perf_event_attr *attr)
  1164. {
  1165. return attr->type == PERF_TYPE_HARDWARE &&
  1166. (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  1167. attr->sample_period == 1;
  1168. }
  1169. bool sample_addr_correlates_sym(struct perf_event_attr *attr)
  1170. {
  1171. if (attr->type == PERF_TYPE_SOFTWARE &&
  1172. (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
  1173. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  1174. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
  1175. return true;
  1176. if (is_bts_event(attr))
  1177. return true;
  1178. return false;
  1179. }
  1180. void thread__resolve(struct thread *thread, struct addr_location *al,
  1181. struct perf_sample *sample)
  1182. {
  1183. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
  1184. if (!al->map)
  1185. thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
  1186. sample->addr, al);
  1187. al->cpu = sample->cpu;
  1188. al->sym = NULL;
  1189. if (al->map)
  1190. al->sym = map__find_symbol(al->map, al->addr, NULL);
  1191. }