event.c 14 KB

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