event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. if (!symbol_type__is_a(type, MAP__FUNCTION) || strcmp(name, args->name))
  190. return 0;
  191. args->start = start;
  192. return 1;
  193. }
  194. int event__synthesize_kernel_mmap(event__handler_t process,
  195. struct perf_session *session,
  196. const char *symbol_name)
  197. {
  198. size_t size;
  199. event_t ev = {
  200. .header = {
  201. .type = PERF_RECORD_MMAP,
  202. .misc = 1, /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */
  203. },
  204. };
  205. /*
  206. * We should get this from /sys/kernel/sections/.text, but till that is
  207. * available use this, and after it is use this as a fallback for older
  208. * kernels.
  209. */
  210. struct process_symbol_args args = { .name = symbol_name, };
  211. if (kallsyms__parse("/proc/kallsyms", &args, find_symbol_cb) <= 0)
  212. return -ENOENT;
  213. size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
  214. "[kernel.kallsyms.%s]", symbol_name) + 1;
  215. size = ALIGN(size, sizeof(u64));
  216. ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size));
  217. ev.mmap.pgoff = args.start;
  218. ev.mmap.start = session->vmlinux_maps[MAP__FUNCTION]->start;
  219. ev.mmap.len = session->vmlinux_maps[MAP__FUNCTION]->end - ev.mmap.start ;
  220. return process(&ev, session);
  221. }
  222. static void thread__comm_adjust(struct thread *self)
  223. {
  224. char *comm = self->comm;
  225. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  226. (!symbol_conf.comm_list ||
  227. strlist__has_entry(symbol_conf.comm_list, comm))) {
  228. unsigned int slen = strlen(comm);
  229. if (slen > comms__col_width) {
  230. comms__col_width = slen;
  231. threads__col_width = slen + 6;
  232. }
  233. }
  234. }
  235. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  236. {
  237. int ret = thread__set_comm(self, comm);
  238. if (ret)
  239. return ret;
  240. thread__comm_adjust(self);
  241. return 0;
  242. }
  243. int event__process_comm(event_t *self, struct perf_session *session)
  244. {
  245. struct thread *thread = perf_session__findnew(session, self->comm.pid);
  246. dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
  247. if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
  248. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. int event__process_lost(event_t *self, struct perf_session *session)
  254. {
  255. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  256. session->events_stats.lost += self->lost.lost;
  257. return 0;
  258. }
  259. int event__process_mmap(event_t *self, struct perf_session *session)
  260. {
  261. struct thread *thread;
  262. struct map *map;
  263. dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
  264. self->mmap.pid, self->mmap.tid, self->mmap.start,
  265. self->mmap.len, self->mmap.pgoff, self->mmap.filename);
  266. if (self->mmap.pid == 0) {
  267. static const char kmmap_prefix[] = "[kernel.kallsyms.";
  268. if (self->mmap.filename[0] == '/') {
  269. char short_module_name[1024];
  270. char *name = strrchr(self->mmap.filename, '/'), *dot;
  271. if (name == NULL)
  272. goto out_problem;
  273. ++name; /* skip / */
  274. dot = strrchr(name, '.');
  275. if (dot == NULL)
  276. goto out_problem;
  277. snprintf(short_module_name, sizeof(short_module_name),
  278. "[%.*s]", (int)(dot - name), name);
  279. strxfrchar(short_module_name, '-', '_');
  280. map = perf_session__new_module_map(session,
  281. self->mmap.start,
  282. short_module_name);
  283. if (map == NULL)
  284. goto out_problem;
  285. name = strdup(self->mmap.filename);
  286. if (name == NULL)
  287. goto out_problem;
  288. dso__set_long_name(map->dso, name);
  289. map->end = map->start + self->mmap.len;
  290. } else if (memcmp(self->mmap.filename, kmmap_prefix,
  291. sizeof(kmmap_prefix) - 1) == 0) {
  292. const char *symbol_name = (self->mmap.filename +
  293. sizeof(kmmap_prefix) - 1);
  294. /*
  295. * Should be there already, from the build-id table in
  296. * the header.
  297. */
  298. struct dso *kernel = __dsos__findnew(&dsos__kernel,
  299. "[kernel.kallsyms]");
  300. if (kernel == NULL)
  301. goto out_problem;
  302. if (__map_groups__create_kernel_maps(&session->kmaps,
  303. session->vmlinux_maps,
  304. kernel) < 0)
  305. goto out_problem;
  306. session->vmlinux_maps[MAP__FUNCTION]->start = self->mmap.start;
  307. session->vmlinux_maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
  308. perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name,
  309. self->mmap.pgoff);
  310. }
  311. return 0;
  312. }
  313. thread = perf_session__findnew(session, self->mmap.pid);
  314. map = map__new(&self->mmap, MAP__FUNCTION,
  315. session->cwd, session->cwdlen);
  316. if (thread == NULL || map == NULL)
  317. goto out_problem;
  318. thread__insert_map(thread, map);
  319. return 0;
  320. out_problem:
  321. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  322. return 0;
  323. }
  324. int event__process_task(event_t *self, struct perf_session *session)
  325. {
  326. struct thread *thread = perf_session__findnew(session, self->fork.pid);
  327. struct thread *parent = perf_session__findnew(session, self->fork.ppid);
  328. dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
  329. self->fork.ppid, self->fork.ptid);
  330. /*
  331. * A thread clone will have the same PID for both parent and child.
  332. */
  333. if (thread == parent)
  334. return 0;
  335. if (self->header.type == PERF_RECORD_EXIT)
  336. return 0;
  337. if (thread == NULL || parent == NULL ||
  338. thread__fork(thread, parent) < 0) {
  339. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  340. return -1;
  341. }
  342. return 0;
  343. }
  344. void thread__find_addr_location(struct thread *self,
  345. struct perf_session *session, u8 cpumode,
  346. enum map_type type, u64 addr,
  347. struct addr_location *al,
  348. symbol_filter_t filter)
  349. {
  350. struct map_groups *mg = &self->mg;
  351. al->thread = self;
  352. al->addr = addr;
  353. if (cpumode & PERF_RECORD_MISC_KERNEL) {
  354. al->level = 'k';
  355. mg = &session->kmaps;
  356. } else if (cpumode & PERF_RECORD_MISC_USER)
  357. al->level = '.';
  358. else {
  359. al->level = 'H';
  360. al->map = NULL;
  361. al->sym = NULL;
  362. return;
  363. }
  364. try_again:
  365. al->map = map_groups__find(mg, type, al->addr);
  366. if (al->map == NULL) {
  367. /*
  368. * If this is outside of all known maps, and is a negative
  369. * address, try to look it up in the kernel dso, as it might be
  370. * a vsyscall or vdso (which executes in user-mode).
  371. *
  372. * XXX This is nasty, we should have a symbol list in the
  373. * "[vdso]" dso, but for now lets use the old trick of looking
  374. * in the whole kernel symbol list.
  375. */
  376. if ((long long)al->addr < 0 && mg != &session->kmaps) {
  377. mg = &session->kmaps;
  378. goto try_again;
  379. }
  380. al->sym = NULL;
  381. } else {
  382. al->addr = al->map->map_ip(al->map, al->addr);
  383. al->sym = map__find_symbol(al->map, session, al->addr, filter);
  384. }
  385. }
  386. static void dso__calc_col_width(struct dso *self)
  387. {
  388. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  389. (!symbol_conf.dso_list ||
  390. strlist__has_entry(symbol_conf.dso_list, self->name))) {
  391. unsigned int slen = strlen(self->name);
  392. if (slen > dsos__col_width)
  393. dsos__col_width = slen;
  394. }
  395. self->slen_calculated = 1;
  396. }
  397. int event__preprocess_sample(const event_t *self, struct perf_session *session,
  398. struct addr_location *al, symbol_filter_t filter)
  399. {
  400. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  401. struct thread *thread = perf_session__findnew(session, self->ip.pid);
  402. if (thread == NULL)
  403. return -1;
  404. if (symbol_conf.comm_list &&
  405. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  406. goto out_filtered;
  407. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  408. thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
  409. self->ip.ip, al, filter);
  410. dump_printf(" ...... dso: %s\n",
  411. al->map ? al->map->dso->long_name :
  412. al->level == 'H' ? "[hypervisor]" : "<not found>");
  413. /*
  414. * We have to do this here as we may have a dso with no symbol hit that
  415. * has a name longer than the ones with symbols sampled.
  416. */
  417. if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
  418. dso__calc_col_width(al->map->dso);
  419. if (symbol_conf.dso_list &&
  420. (!al->map || !al->map->dso ||
  421. !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
  422. (al->map->dso->short_name != al->map->dso->long_name &&
  423. strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
  424. goto out_filtered;
  425. if (symbol_conf.sym_list && al->sym &&
  426. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  427. goto out_filtered;
  428. al->filtered = false;
  429. return 0;
  430. out_filtered:
  431. al->filtered = true;
  432. return 0;
  433. }
  434. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  435. {
  436. u64 *array = event->sample.array;
  437. if (type & PERF_SAMPLE_IP) {
  438. data->ip = event->ip.ip;
  439. array++;
  440. }
  441. if (type & PERF_SAMPLE_TID) {
  442. u32 *p = (u32 *)array;
  443. data->pid = p[0];
  444. data->tid = p[1];
  445. array++;
  446. }
  447. if (type & PERF_SAMPLE_TIME) {
  448. data->time = *array;
  449. array++;
  450. }
  451. if (type & PERF_SAMPLE_ADDR) {
  452. data->addr = *array;
  453. array++;
  454. }
  455. if (type & PERF_SAMPLE_ID) {
  456. data->id = *array;
  457. array++;
  458. }
  459. if (type & PERF_SAMPLE_STREAM_ID) {
  460. data->stream_id = *array;
  461. array++;
  462. }
  463. if (type & PERF_SAMPLE_CPU) {
  464. u32 *p = (u32 *)array;
  465. data->cpu = *p;
  466. array++;
  467. }
  468. if (type & PERF_SAMPLE_PERIOD) {
  469. data->period = *array;
  470. array++;
  471. }
  472. if (type & PERF_SAMPLE_READ) {
  473. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  474. return -1;
  475. }
  476. if (type & PERF_SAMPLE_CALLCHAIN) {
  477. data->callchain = (struct ip_callchain *)array;
  478. array += 1 + data->callchain->nr;
  479. }
  480. if (type & PERF_SAMPLE_RAW) {
  481. u32 *p = (u32 *)array;
  482. data->raw_size = *p;
  483. p++;
  484. data->raw_data = p;
  485. }
  486. return 0;
  487. }