event.c 36 KB

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