event.c 15 KB

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