event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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_thread(pid_t pid, event__handler_t process,
  132. struct perf_session *session)
  133. {
  134. pid_t tgid = event__synthesize_comm(pid, 1, process, session);
  135. if (tgid == -1)
  136. return -1;
  137. return event__synthesize_mmap_events(pid, tgid, process, session);
  138. }
  139. void event__synthesize_threads(event__handler_t process,
  140. struct perf_session *session)
  141. {
  142. DIR *proc;
  143. struct dirent dirent, *next;
  144. proc = opendir("/proc");
  145. while (!readdir_r(proc, &dirent, &next) && next) {
  146. char *end;
  147. pid_t pid = strtol(dirent.d_name, &end, 10);
  148. if (*end) /* only interested in proper numerical dirents */
  149. continue;
  150. event__synthesize_thread(pid, process, session);
  151. }
  152. closedir(proc);
  153. }
  154. struct process_symbol_args {
  155. const char *name;
  156. u64 start;
  157. };
  158. static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
  159. {
  160. struct process_symbol_args *args = arg;
  161. if (!symbol_type__is_a(type, MAP__FUNCTION) || strcmp(name, args->name))
  162. return 0;
  163. args->start = start;
  164. return 1;
  165. }
  166. int event__synthesize_kernel_mmap(event__handler_t process,
  167. struct perf_session *session,
  168. const char *symbol_name)
  169. {
  170. size_t size;
  171. event_t ev = {
  172. .header = { .type = PERF_RECORD_MMAP },
  173. };
  174. /*
  175. * We should get this from /sys/kernel/sections/.text, but till that is
  176. * available use this, and after it is use this as a fallback for older
  177. * kernels.
  178. */
  179. struct process_symbol_args args = { .name = symbol_name, };
  180. if (kallsyms__parse(&args, find_symbol_cb) <= 0)
  181. return -ENOENT;
  182. size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
  183. "[kernel.kallsyms.%s]", symbol_name) + 1;
  184. size = ALIGN(size, sizeof(u64));
  185. ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size));
  186. ev.mmap.start = args.start;
  187. return process(&ev, session);
  188. }
  189. static void thread__comm_adjust(struct thread *self)
  190. {
  191. char *comm = self->comm;
  192. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  193. (!symbol_conf.comm_list ||
  194. strlist__has_entry(symbol_conf.comm_list, comm))) {
  195. unsigned int slen = strlen(comm);
  196. if (slen > comms__col_width) {
  197. comms__col_width = slen;
  198. threads__col_width = slen + 6;
  199. }
  200. }
  201. }
  202. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  203. {
  204. int ret = thread__set_comm(self, comm);
  205. if (ret)
  206. return ret;
  207. thread__comm_adjust(self);
  208. return 0;
  209. }
  210. int event__process_comm(event_t *self, struct perf_session *session)
  211. {
  212. struct thread *thread = perf_session__findnew(session, self->comm.pid);
  213. dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
  214. if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
  215. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  216. return -1;
  217. }
  218. return 0;
  219. }
  220. int event__process_lost(event_t *self, struct perf_session *session)
  221. {
  222. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  223. session->events_stats.lost += self->lost.lost;
  224. return 0;
  225. }
  226. int event__process_mmap(event_t *self, struct perf_session *session)
  227. {
  228. struct thread *thread;
  229. struct map *map;
  230. static const char kmmap_prefix[] = "[kernel.kallsyms.";
  231. dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
  232. self->mmap.pid, self->mmap.tid,
  233. (void *)(long)self->mmap.start,
  234. (void *)(long)self->mmap.len,
  235. (void *)(long)self->mmap.pgoff,
  236. self->mmap.filename);
  237. if (self->mmap.pid == 0 &&
  238. memcmp(self->mmap.filename, kmmap_prefix,
  239. sizeof(kmmap_prefix) - 1) == 0) {
  240. const char *symbol_name = (self->mmap.filename +
  241. sizeof(kmmap_prefix) - 1);
  242. perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name,
  243. self->mmap.start);
  244. return 0;
  245. }
  246. thread = perf_session__findnew(session, self->mmap.pid);
  247. map = map__new(&self->mmap, MAP__FUNCTION,
  248. session->cwd, session->cwdlen);
  249. if (thread == NULL || map == NULL)
  250. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  251. else
  252. thread__insert_map(thread, map);
  253. return 0;
  254. }
  255. int event__process_task(event_t *self, struct perf_session *session)
  256. {
  257. struct thread *thread = perf_session__findnew(session, self->fork.pid);
  258. struct thread *parent = perf_session__findnew(session, self->fork.ppid);
  259. dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
  260. self->fork.ppid, self->fork.ptid);
  261. /*
  262. * A thread clone will have the same PID for both parent and child.
  263. */
  264. if (thread == parent)
  265. return 0;
  266. if (self->header.type == PERF_RECORD_EXIT)
  267. return 0;
  268. if (thread == NULL || parent == NULL ||
  269. thread__fork(thread, parent) < 0) {
  270. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. void thread__find_addr_location(struct thread *self,
  276. struct perf_session *session, u8 cpumode,
  277. enum map_type type, u64 addr,
  278. struct addr_location *al,
  279. symbol_filter_t filter)
  280. {
  281. struct map_groups *mg = &self->mg;
  282. al->thread = self;
  283. al->addr = addr;
  284. if (cpumode & PERF_RECORD_MISC_KERNEL) {
  285. al->level = 'k';
  286. mg = &session->kmaps;
  287. } else if (cpumode & PERF_RECORD_MISC_USER)
  288. al->level = '.';
  289. else {
  290. al->level = 'H';
  291. al->map = NULL;
  292. al->sym = NULL;
  293. return;
  294. }
  295. try_again:
  296. al->map = map_groups__find(mg, type, al->addr);
  297. if (al->map == NULL) {
  298. /*
  299. * If this is outside of all known maps, and is a negative
  300. * address, try to look it up in the kernel dso, as it might be
  301. * a vsyscall or vdso (which executes in user-mode).
  302. *
  303. * XXX This is nasty, we should have a symbol list in the
  304. * "[vdso]" dso, but for now lets use the old trick of looking
  305. * in the whole kernel symbol list.
  306. */
  307. if ((long long)al->addr < 0 && mg != &session->kmaps) {
  308. mg = &session->kmaps;
  309. goto try_again;
  310. }
  311. al->sym = NULL;
  312. } else {
  313. al->addr = al->map->map_ip(al->map, al->addr);
  314. al->sym = map__find_symbol(al->map, session, al->addr, filter);
  315. }
  316. }
  317. static void dso__calc_col_width(struct dso *self)
  318. {
  319. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  320. (!symbol_conf.dso_list ||
  321. strlist__has_entry(symbol_conf.dso_list, self->name))) {
  322. unsigned int slen = strlen(self->name);
  323. if (slen > dsos__col_width)
  324. dsos__col_width = slen;
  325. }
  326. self->slen_calculated = 1;
  327. }
  328. int event__preprocess_sample(const event_t *self, struct perf_session *session,
  329. struct addr_location *al, symbol_filter_t filter)
  330. {
  331. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  332. struct thread *thread = perf_session__findnew(session, self->ip.pid);
  333. if (thread == NULL)
  334. return -1;
  335. if (symbol_conf.comm_list &&
  336. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  337. goto out_filtered;
  338. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  339. thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
  340. self->ip.ip, al, filter);
  341. dump_printf(" ...... dso: %s\n",
  342. al->map ? al->map->dso->long_name :
  343. al->level == 'H' ? "[hypervisor]" : "<not found>");
  344. /*
  345. * We have to do this here as we may have a dso with no symbol hit that
  346. * has a name longer than the ones with symbols sampled.
  347. */
  348. if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
  349. dso__calc_col_width(al->map->dso);
  350. if (symbol_conf.dso_list &&
  351. (!al->map || !al->map->dso ||
  352. !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
  353. (al->map->dso->short_name != al->map->dso->long_name &&
  354. strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
  355. goto out_filtered;
  356. if (symbol_conf.sym_list && al->sym &&
  357. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  358. goto out_filtered;
  359. al->filtered = false;
  360. return 0;
  361. out_filtered:
  362. al->filtered = true;
  363. return 0;
  364. }
  365. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  366. {
  367. u64 *array = event->sample.array;
  368. if (type & PERF_SAMPLE_IP) {
  369. data->ip = event->ip.ip;
  370. array++;
  371. }
  372. if (type & PERF_SAMPLE_TID) {
  373. u32 *p = (u32 *)array;
  374. data->pid = p[0];
  375. data->tid = p[1];
  376. array++;
  377. }
  378. if (type & PERF_SAMPLE_TIME) {
  379. data->time = *array;
  380. array++;
  381. }
  382. if (type & PERF_SAMPLE_ADDR) {
  383. data->addr = *array;
  384. array++;
  385. }
  386. if (type & PERF_SAMPLE_ID) {
  387. data->id = *array;
  388. array++;
  389. }
  390. if (type & PERF_SAMPLE_STREAM_ID) {
  391. data->stream_id = *array;
  392. array++;
  393. }
  394. if (type & PERF_SAMPLE_CPU) {
  395. u32 *p = (u32 *)array;
  396. data->cpu = *p;
  397. array++;
  398. }
  399. if (type & PERF_SAMPLE_PERIOD) {
  400. data->period = *array;
  401. array++;
  402. }
  403. if (type & PERF_SAMPLE_READ) {
  404. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  405. return -1;
  406. }
  407. if (type & PERF_SAMPLE_CALLCHAIN) {
  408. data->callchain = (struct ip_callchain *)array;
  409. array += 1 + data->callchain->nr;
  410. }
  411. if (type & PERF_SAMPLE_RAW) {
  412. u32 *p = (u32 *)array;
  413. data->raw_size = *p;
  414. p++;
  415. data->raw_data = p;
  416. }
  417. return 0;
  418. }