event.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #include <linux/types.h>
  2. #include "event.h"
  3. #include "debug.h"
  4. #include "session.h"
  5. #include "sort.h"
  6. #include "string.h"
  7. #include "strlist.h"
  8. #include "thread.h"
  9. static pid_t event__synthesize_comm(pid_t pid, int full,
  10. event__handler_t process,
  11. struct perf_session *session)
  12. {
  13. event_t ev;
  14. char filename[PATH_MAX];
  15. char bf[BUFSIZ];
  16. FILE *fp;
  17. size_t size = 0;
  18. DIR *tasks;
  19. struct dirent dirent, *next;
  20. pid_t tgid = 0;
  21. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  22. fp = fopen(filename, "r");
  23. if (fp == NULL) {
  24. out_race:
  25. /*
  26. * We raced with a task exiting - just return:
  27. */
  28. pr_debug("couldn't open %s\n", filename);
  29. return 0;
  30. }
  31. memset(&ev.comm, 0, sizeof(ev.comm));
  32. while (!ev.comm.comm[0] || !ev.comm.pid) {
  33. if (fgets(bf, sizeof(bf), fp) == NULL)
  34. goto out_failure;
  35. if (memcmp(bf, "Name:", 5) == 0) {
  36. char *name = bf + 5;
  37. while (*name && isspace(*name))
  38. ++name;
  39. size = strlen(name) - 1;
  40. memcpy(ev.comm.comm, name, size++);
  41. } else if (memcmp(bf, "Tgid:", 5) == 0) {
  42. char *tgids = bf + 5;
  43. while (*tgids && isspace(*tgids))
  44. ++tgids;
  45. tgid = ev.comm.pid = atoi(tgids);
  46. }
  47. }
  48. ev.comm.header.type = PERF_RECORD_COMM;
  49. size = ALIGN(size, sizeof(u64));
  50. ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
  51. if (!full) {
  52. ev.comm.tid = pid;
  53. process(&ev, session);
  54. goto out_fclose;
  55. }
  56. snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
  57. tasks = opendir(filename);
  58. if (tasks == NULL)
  59. goto out_race;
  60. while (!readdir_r(tasks, &dirent, &next) && next) {
  61. char *end;
  62. pid = strtol(dirent.d_name, &end, 10);
  63. if (*end)
  64. continue;
  65. ev.comm.tid = pid;
  66. process(&ev, session);
  67. }
  68. closedir(tasks);
  69. out_fclose:
  70. fclose(fp);
  71. return tgid;
  72. out_failure:
  73. pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
  74. return -1;
  75. }
  76. static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
  77. event__handler_t process,
  78. struct perf_session *session)
  79. {
  80. char filename[PATH_MAX];
  81. FILE *fp;
  82. snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
  83. fp = fopen(filename, "r");
  84. if (fp == NULL) {
  85. /*
  86. * We raced with a task exiting - just return:
  87. */
  88. pr_debug("couldn't open %s\n", filename);
  89. return -1;
  90. }
  91. while (1) {
  92. char bf[BUFSIZ], *pbf = bf;
  93. event_t ev = {
  94. .header = {
  95. .type = PERF_RECORD_MMAP,
  96. .misc = 0, /* Just like the kernel, see kernel/perf_event.c __perf_event_mmap */
  97. },
  98. };
  99. int n;
  100. size_t size;
  101. if (fgets(bf, sizeof(bf), fp) == NULL)
  102. break;
  103. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  104. n = hex2u64(pbf, &ev.mmap.start);
  105. if (n < 0)
  106. continue;
  107. pbf += n + 1;
  108. n = hex2u64(pbf, &ev.mmap.len);
  109. if (n < 0)
  110. continue;
  111. pbf += n + 3;
  112. if (*pbf == 'x') { /* vm_exec */
  113. u64 vm_pgoff;
  114. char *execname = strchr(bf, '/');
  115. /* Catch VDSO */
  116. if (execname == NULL)
  117. execname = strstr(bf, "[vdso]");
  118. if (execname == NULL)
  119. continue;
  120. pbf += 3;
  121. n = hex2u64(pbf, &vm_pgoff);
  122. /* pgoff is in bytes, not pages */
  123. if (n >= 0)
  124. ev.mmap.pgoff = vm_pgoff << getpagesize();
  125. else
  126. ev.mmap.pgoff = 0;
  127. size = strlen(execname);
  128. execname[size - 1] = '\0'; /* Remove \n */
  129. memcpy(ev.mmap.filename, execname, size);
  130. size = ALIGN(size, sizeof(u64));
  131. ev.mmap.len -= ev.mmap.start;
  132. ev.mmap.header.size = (sizeof(ev.mmap) -
  133. (sizeof(ev.mmap.filename) - size));
  134. ev.mmap.pid = tgid;
  135. ev.mmap.tid = pid;
  136. process(&ev, session);
  137. }
  138. }
  139. fclose(fp);
  140. return 0;
  141. }
  142. int event__synthesize_modules(event__handler_t process,
  143. struct perf_session *session)
  144. {
  145. struct rb_node *nd;
  146. for (nd = rb_first(&session->kmaps.maps[MAP__FUNCTION]);
  147. nd; nd = rb_next(nd)) {
  148. event_t ev;
  149. size_t size;
  150. struct map *pos = rb_entry(nd, struct map, rb_node);
  151. if (pos->dso->kernel)
  152. continue;
  153. size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
  154. memset(&ev, 0, sizeof(ev));
  155. ev.mmap.header.misc = 1; /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */
  156. ev.mmap.header.type = PERF_RECORD_MMAP;
  157. ev.mmap.header.size = (sizeof(ev.mmap) -
  158. (sizeof(ev.mmap.filename) - size));
  159. ev.mmap.start = pos->start;
  160. ev.mmap.len = pos->end - pos->start;
  161. memcpy(ev.mmap.filename, pos->dso->long_name,
  162. pos->dso->long_name_len + 1);
  163. process(&ev, session);
  164. }
  165. return 0;
  166. }
  167. int event__synthesize_thread(pid_t pid, event__handler_t process,
  168. struct perf_session *session)
  169. {
  170. pid_t tgid = event__synthesize_comm(pid, 1, process, session);
  171. if (tgid == -1)
  172. return -1;
  173. return event__synthesize_mmap_events(pid, tgid, process, session);
  174. }
  175. void event__synthesize_threads(event__handler_t process,
  176. struct perf_session *session)
  177. {
  178. DIR *proc;
  179. struct dirent dirent, *next;
  180. proc = opendir("/proc");
  181. while (!readdir_r(proc, &dirent, &next) && next) {
  182. char *end;
  183. pid_t pid = strtol(dirent.d_name, &end, 10);
  184. if (*end) /* only interested in proper numerical dirents */
  185. continue;
  186. event__synthesize_thread(pid, process, session);
  187. }
  188. closedir(proc);
  189. }
  190. struct process_symbol_args {
  191. const char *name;
  192. u64 start;
  193. };
  194. static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
  195. {
  196. struct process_symbol_args *args = arg;
  197. /*
  198. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  199. * an 'A' to the same address as "_stext".
  200. */
  201. if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
  202. type == 'A') || strcmp(name, args->name))
  203. return 0;
  204. args->start = start;
  205. return 1;
  206. }
  207. int event__synthesize_kernel_mmap(event__handler_t process,
  208. struct perf_session *session,
  209. const char *symbol_name)
  210. {
  211. size_t size;
  212. event_t ev = {
  213. .header = {
  214. .type = PERF_RECORD_MMAP,
  215. .misc = 1, /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */
  216. },
  217. };
  218. /*
  219. * We should get this from /sys/kernel/sections/.text, but till that is
  220. * available use this, and after it is use this as a fallback for older
  221. * kernels.
  222. */
  223. struct process_symbol_args args = { .name = symbol_name, };
  224. if (kallsyms__parse("/proc/kallsyms", &args, find_symbol_cb) <= 0)
  225. return -ENOENT;
  226. size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
  227. "[kernel.kallsyms.%s]", symbol_name) + 1;
  228. size = ALIGN(size, sizeof(u64));
  229. ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size));
  230. ev.mmap.pgoff = args.start;
  231. ev.mmap.start = session->vmlinux_maps[MAP__FUNCTION]->start;
  232. ev.mmap.len = session->vmlinux_maps[MAP__FUNCTION]->end - ev.mmap.start ;
  233. return process(&ev, session);
  234. }
  235. static void thread__comm_adjust(struct thread *self)
  236. {
  237. char *comm = self->comm;
  238. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  239. (!symbol_conf.comm_list ||
  240. strlist__has_entry(symbol_conf.comm_list, comm))) {
  241. unsigned int slen = strlen(comm);
  242. if (slen > comms__col_width) {
  243. comms__col_width = slen;
  244. threads__col_width = slen + 6;
  245. }
  246. }
  247. }
  248. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  249. {
  250. int ret = thread__set_comm(self, comm);
  251. if (ret)
  252. return ret;
  253. thread__comm_adjust(self);
  254. return 0;
  255. }
  256. int event__process_comm(event_t *self, struct perf_session *session)
  257. {
  258. struct thread *thread = perf_session__findnew(session, self->comm.pid);
  259. dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
  260. if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
  261. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. int event__process_lost(event_t *self, struct perf_session *session)
  267. {
  268. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  269. session->events_stats.lost += self->lost.lost;
  270. return 0;
  271. }
  272. int event__process_mmap(event_t *self, struct perf_session *session)
  273. {
  274. struct thread *thread;
  275. struct map *map;
  276. dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
  277. self->mmap.pid, self->mmap.tid, self->mmap.start,
  278. self->mmap.len, self->mmap.pgoff, self->mmap.filename);
  279. if (self->mmap.pid == 0) {
  280. static const char kmmap_prefix[] = "[kernel.kallsyms.";
  281. if (self->mmap.filename[0] == '/') {
  282. char short_module_name[1024];
  283. char *name = strrchr(self->mmap.filename, '/'), *dot;
  284. if (name == NULL)
  285. goto out_problem;
  286. ++name; /* skip / */
  287. dot = strrchr(name, '.');
  288. if (dot == NULL)
  289. goto out_problem;
  290. snprintf(short_module_name, sizeof(short_module_name),
  291. "[%.*s]", (int)(dot - name), name);
  292. strxfrchar(short_module_name, '-', '_');
  293. map = perf_session__new_module_map(session,
  294. self->mmap.start,
  295. self->mmap.filename);
  296. if (map == NULL)
  297. goto out_problem;
  298. name = strdup(short_module_name);
  299. if (name == NULL)
  300. goto out_problem;
  301. map->dso->short_name = name;
  302. map->end = map->start + self->mmap.len;
  303. } else if (memcmp(self->mmap.filename, kmmap_prefix,
  304. sizeof(kmmap_prefix) - 1) == 0) {
  305. const char *symbol_name = (self->mmap.filename +
  306. sizeof(kmmap_prefix) - 1);
  307. /*
  308. * Should be there already, from the build-id table in
  309. * the header.
  310. */
  311. struct dso *kernel = __dsos__findnew(&dsos__kernel,
  312. "[kernel.kallsyms]");
  313. if (kernel == NULL)
  314. goto out_problem;
  315. kernel->kernel = 1;
  316. if (__perf_session__create_kernel_maps(session, kernel) < 0)
  317. goto out_problem;
  318. session->vmlinux_maps[MAP__FUNCTION]->start = self->mmap.start;
  319. session->vmlinux_maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
  320. /*
  321. * Be a bit paranoid here, some perf.data file came with
  322. * a zero sized synthesized MMAP event for the kernel.
  323. */
  324. if (session->vmlinux_maps[MAP__FUNCTION]->end == 0)
  325. session->vmlinux_maps[MAP__FUNCTION]->end = ~0UL;
  326. perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name,
  327. self->mmap.pgoff);
  328. }
  329. return 0;
  330. }
  331. thread = perf_session__findnew(session, self->mmap.pid);
  332. map = map__new(self->mmap.start, self->mmap.len, self->mmap.pgoff,
  333. self->mmap.pid, self->mmap.filename, MAP__FUNCTION,
  334. session->cwd, session->cwdlen);
  335. if (thread == NULL || map == NULL)
  336. goto out_problem;
  337. thread__insert_map(thread, map);
  338. return 0;
  339. out_problem:
  340. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  341. return 0;
  342. }
  343. int event__process_task(event_t *self, struct perf_session *session)
  344. {
  345. struct thread *thread = perf_session__findnew(session, self->fork.pid);
  346. struct thread *parent = perf_session__findnew(session, self->fork.ppid);
  347. dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
  348. self->fork.ppid, self->fork.ptid);
  349. /*
  350. * A thread clone will have the same PID for both parent and child.
  351. */
  352. if (thread == parent)
  353. return 0;
  354. if (self->header.type == PERF_RECORD_EXIT)
  355. return 0;
  356. if (thread == NULL || parent == NULL ||
  357. thread__fork(thread, parent) < 0) {
  358. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  359. return -1;
  360. }
  361. return 0;
  362. }
  363. void thread__find_addr_map(struct thread *self,
  364. struct perf_session *session, u8 cpumode,
  365. enum map_type type, u64 addr,
  366. struct addr_location *al)
  367. {
  368. struct map_groups *mg = &self->mg;
  369. al->thread = self;
  370. al->addr = addr;
  371. if (cpumode == PERF_RECORD_MISC_KERNEL) {
  372. al->level = 'k';
  373. mg = &session->kmaps;
  374. } else if (cpumode == PERF_RECORD_MISC_USER)
  375. al->level = '.';
  376. else {
  377. al->level = 'H';
  378. al->map = NULL;
  379. return;
  380. }
  381. try_again:
  382. al->map = map_groups__find(mg, type, al->addr);
  383. if (al->map == NULL) {
  384. /*
  385. * If this is outside of all known maps, and is a negative
  386. * address, try to look it up in the kernel dso, as it might be
  387. * a vsyscall or vdso (which executes in user-mode).
  388. *
  389. * XXX This is nasty, we should have a symbol list in the
  390. * "[vdso]" dso, but for now lets use the old trick of looking
  391. * in the whole kernel symbol list.
  392. */
  393. if ((long long)al->addr < 0 && mg != &session->kmaps) {
  394. mg = &session->kmaps;
  395. goto try_again;
  396. }
  397. } else
  398. al->addr = al->map->map_ip(al->map, al->addr);
  399. }
  400. void thread__find_addr_location(struct thread *self,
  401. struct perf_session *session, u8 cpumode,
  402. enum map_type type, u64 addr,
  403. struct addr_location *al,
  404. symbol_filter_t filter)
  405. {
  406. thread__find_addr_map(self, session, cpumode, type, addr, al);
  407. if (al->map != NULL)
  408. al->sym = map__find_symbol(al->map, al->addr, filter);
  409. else
  410. al->sym = NULL;
  411. }
  412. static void dso__calc_col_width(struct dso *self)
  413. {
  414. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  415. (!symbol_conf.dso_list ||
  416. strlist__has_entry(symbol_conf.dso_list, self->name))) {
  417. unsigned int slen = strlen(self->name);
  418. if (slen > dsos__col_width)
  419. dsos__col_width = slen;
  420. }
  421. self->slen_calculated = 1;
  422. }
  423. int event__preprocess_sample(const event_t *self, struct perf_session *session,
  424. struct addr_location *al, symbol_filter_t filter)
  425. {
  426. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  427. struct thread *thread = perf_session__findnew(session, self->ip.pid);
  428. if (thread == NULL)
  429. return -1;
  430. if (symbol_conf.comm_list &&
  431. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  432. goto out_filtered;
  433. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  434. thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
  435. self->ip.ip, al);
  436. dump_printf(" ...... dso: %s\n",
  437. al->map ? al->map->dso->long_name :
  438. al->level == 'H' ? "[hypervisor]" : "<not found>");
  439. al->sym = NULL;
  440. if (al->map) {
  441. if (symbol_conf.dso_list &&
  442. (!al->map || !al->map->dso ||
  443. !(strlist__has_entry(symbol_conf.dso_list,
  444. al->map->dso->short_name) ||
  445. (al->map->dso->short_name != al->map->dso->long_name &&
  446. strlist__has_entry(symbol_conf.dso_list,
  447. al->map->dso->long_name)))))
  448. goto out_filtered;
  449. /*
  450. * We have to do this here as we may have a dso with no symbol
  451. * hit that has a name longer than the ones with symbols
  452. * sampled.
  453. */
  454. if (!sort_dso.elide && !al->map->dso->slen_calculated)
  455. dso__calc_col_width(al->map->dso);
  456. al->sym = map__find_symbol(al->map, al->addr, filter);
  457. }
  458. if (symbol_conf.sym_list && al->sym &&
  459. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  460. goto out_filtered;
  461. al->filtered = false;
  462. return 0;
  463. out_filtered:
  464. al->filtered = true;
  465. return 0;
  466. }
  467. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  468. {
  469. u64 *array = event->sample.array;
  470. if (type & PERF_SAMPLE_IP) {
  471. data->ip = event->ip.ip;
  472. array++;
  473. }
  474. if (type & PERF_SAMPLE_TID) {
  475. u32 *p = (u32 *)array;
  476. data->pid = p[0];
  477. data->tid = p[1];
  478. array++;
  479. }
  480. if (type & PERF_SAMPLE_TIME) {
  481. data->time = *array;
  482. array++;
  483. }
  484. if (type & PERF_SAMPLE_ADDR) {
  485. data->addr = *array;
  486. array++;
  487. }
  488. if (type & PERF_SAMPLE_ID) {
  489. data->id = *array;
  490. array++;
  491. }
  492. if (type & PERF_SAMPLE_STREAM_ID) {
  493. data->stream_id = *array;
  494. array++;
  495. }
  496. if (type & PERF_SAMPLE_CPU) {
  497. u32 *p = (u32 *)array;
  498. data->cpu = *p;
  499. array++;
  500. }
  501. if (type & PERF_SAMPLE_PERIOD) {
  502. data->period = *array;
  503. array++;
  504. }
  505. if (type & PERF_SAMPLE_READ) {
  506. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  507. return -1;
  508. }
  509. if (type & PERF_SAMPLE_CALLCHAIN) {
  510. data->callchain = (struct ip_callchain *)array;
  511. array += 1 + data->callchain->nr;
  512. }
  513. if (type & PERF_SAMPLE_RAW) {
  514. u32 *p = (u32 *)array;
  515. data->raw_size = *p;
  516. p++;
  517. data->raw_data = p;
  518. }
  519. return 0;
  520. }