event.c 35 KB

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