event.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. int (*process)(event_t *event,
  11. struct perf_session *session),
  12. struct perf_session *session)
  13. {
  14. event_t ev;
  15. char filename[PATH_MAX];
  16. char bf[BUFSIZ];
  17. FILE *fp;
  18. size_t size = 0;
  19. DIR *tasks;
  20. struct dirent dirent, *next;
  21. pid_t tgid = 0;
  22. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  23. fp = fopen(filename, "r");
  24. if (fp == NULL) {
  25. out_race:
  26. /*
  27. * We raced with a task exiting - just return:
  28. */
  29. pr_debug("couldn't open %s\n", filename);
  30. return 0;
  31. }
  32. memset(&ev.comm, 0, sizeof(ev.comm));
  33. while (!ev.comm.comm[0] || !ev.comm.pid) {
  34. if (fgets(bf, sizeof(bf), fp) == NULL)
  35. goto out_failure;
  36. if (memcmp(bf, "Name:", 5) == 0) {
  37. char *name = bf + 5;
  38. while (*name && isspace(*name))
  39. ++name;
  40. size = strlen(name) - 1;
  41. memcpy(ev.comm.comm, name, size++);
  42. } else if (memcmp(bf, "Tgid:", 5) == 0) {
  43. char *tgids = bf + 5;
  44. while (*tgids && isspace(*tgids))
  45. ++tgids;
  46. tgid = ev.comm.pid = atoi(tgids);
  47. }
  48. }
  49. ev.comm.header.type = PERF_RECORD_COMM;
  50. size = ALIGN(size, sizeof(u64));
  51. ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
  52. if (!full) {
  53. ev.comm.tid = pid;
  54. process(&ev, session);
  55. goto out_fclose;
  56. }
  57. snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
  58. tasks = opendir(filename);
  59. if (tasks == NULL)
  60. goto out_race;
  61. while (!readdir_r(tasks, &dirent, &next) && next) {
  62. char *end;
  63. pid = strtol(dirent.d_name, &end, 10);
  64. if (*end)
  65. continue;
  66. ev.comm.tid = pid;
  67. process(&ev, session);
  68. }
  69. closedir(tasks);
  70. out_fclose:
  71. fclose(fp);
  72. return tgid;
  73. out_failure:
  74. pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
  75. return -1;
  76. }
  77. static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
  78. int (*process)(event_t *event,
  79. struct perf_session *session),
  80. struct perf_session *session)
  81. {
  82. char filename[PATH_MAX];
  83. FILE *fp;
  84. snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
  85. fp = fopen(filename, "r");
  86. if (fp == NULL) {
  87. /*
  88. * We raced with a task exiting - just return:
  89. */
  90. pr_debug("couldn't open %s\n", filename);
  91. return -1;
  92. }
  93. while (1) {
  94. char bf[BUFSIZ], *pbf = bf;
  95. event_t ev = {
  96. .header = { .type = PERF_RECORD_MMAP },
  97. };
  98. int n;
  99. size_t size;
  100. if (fgets(bf, sizeof(bf), fp) == NULL)
  101. break;
  102. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  103. n = hex2u64(pbf, &ev.mmap.start);
  104. if (n < 0)
  105. continue;
  106. pbf += n + 1;
  107. n = hex2u64(pbf, &ev.mmap.len);
  108. if (n < 0)
  109. continue;
  110. pbf += n + 3;
  111. if (*pbf == 'x') { /* vm_exec */
  112. char *execname = strchr(bf, '/');
  113. /* Catch VDSO */
  114. if (execname == NULL)
  115. execname = strstr(bf, "[vdso]");
  116. if (execname == NULL)
  117. continue;
  118. size = strlen(execname);
  119. execname[size - 1] = '\0'; /* Remove \n */
  120. memcpy(ev.mmap.filename, execname, size);
  121. size = ALIGN(size, sizeof(u64));
  122. ev.mmap.len -= ev.mmap.start;
  123. ev.mmap.header.size = (sizeof(ev.mmap) -
  124. (sizeof(ev.mmap.filename) - size));
  125. ev.mmap.pid = tgid;
  126. ev.mmap.tid = pid;
  127. process(&ev, session);
  128. }
  129. }
  130. fclose(fp);
  131. return 0;
  132. }
  133. int event__synthesize_thread(pid_t pid,
  134. int (*process)(event_t *event,
  135. struct perf_session *session),
  136. struct perf_session *session)
  137. {
  138. pid_t tgid = event__synthesize_comm(pid, 1, process, session);
  139. if (tgid == -1)
  140. return -1;
  141. return event__synthesize_mmap_events(pid, tgid, process, session);
  142. }
  143. void event__synthesize_threads(int (*process)(event_t *event,
  144. struct perf_session *session),
  145. struct perf_session *session)
  146. {
  147. DIR *proc;
  148. struct dirent dirent, *next;
  149. proc = opendir("/proc");
  150. while (!readdir_r(proc, &dirent, &next) && next) {
  151. char *end;
  152. pid_t pid = strtol(dirent.d_name, &end, 10);
  153. if (*end) /* only interested in proper numerical dirents */
  154. continue;
  155. event__synthesize_thread(pid, process, session);
  156. }
  157. closedir(proc);
  158. }
  159. int event__process_comm(event_t *self, struct perf_session *session)
  160. {
  161. struct thread *thread = perf_session__findnew(session, self->comm.pid);
  162. dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
  163. if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
  164. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. int event__process_lost(event_t *self, struct perf_session *session)
  170. {
  171. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  172. session->events_stats.lost += self->lost.lost;
  173. return 0;
  174. }
  175. int event__process_mmap(event_t *self, struct perf_session *session)
  176. {
  177. struct thread *thread = perf_session__findnew(session, self->mmap.pid);
  178. struct map *map = map__new(&self->mmap, MAP__FUNCTION,
  179. session->cwd, session->cwdlen);
  180. dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
  181. self->mmap.pid, self->mmap.tid,
  182. (void *)(long)self->mmap.start,
  183. (void *)(long)self->mmap.len,
  184. (void *)(long)self->mmap.pgoff,
  185. self->mmap.filename);
  186. if (thread == NULL || map == NULL)
  187. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  188. else
  189. thread__insert_map(thread, map);
  190. return 0;
  191. }
  192. int event__process_task(event_t *self, struct perf_session *session)
  193. {
  194. struct thread *thread = perf_session__findnew(session, self->fork.pid);
  195. struct thread *parent = perf_session__findnew(session, self->fork.ppid);
  196. dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
  197. self->fork.ppid, self->fork.ptid);
  198. /*
  199. * A thread clone will have the same PID for both parent and child.
  200. */
  201. if (thread == parent)
  202. return 0;
  203. if (self->header.type == PERF_RECORD_EXIT)
  204. return 0;
  205. if (thread == NULL || parent == NULL ||
  206. thread__fork(thread, parent) < 0) {
  207. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. void thread__find_addr_location(struct thread *self,
  213. struct perf_session *session, u8 cpumode,
  214. enum map_type type, u64 addr,
  215. struct addr_location *al,
  216. symbol_filter_t filter)
  217. {
  218. struct map_groups *mg = &self->mg;
  219. al->thread = self;
  220. al->addr = addr;
  221. if (cpumode & PERF_RECORD_MISC_KERNEL) {
  222. al->level = 'k';
  223. mg = &session->kmaps;
  224. } else if (cpumode & PERF_RECORD_MISC_USER)
  225. al->level = '.';
  226. else {
  227. al->level = 'H';
  228. al->map = NULL;
  229. al->sym = NULL;
  230. return;
  231. }
  232. try_again:
  233. al->map = map_groups__find(mg, type, al->addr);
  234. if (al->map == NULL) {
  235. /*
  236. * If this is outside of all known maps, and is a negative
  237. * address, try to look it up in the kernel dso, as it might be
  238. * a vsyscall or vdso (which executes in user-mode).
  239. *
  240. * XXX This is nasty, we should have a symbol list in the
  241. * "[vdso]" dso, but for now lets use the old trick of looking
  242. * in the whole kernel symbol list.
  243. */
  244. if ((long long)al->addr < 0 && mg != &session->kmaps) {
  245. mg = &session->kmaps;
  246. goto try_again;
  247. }
  248. al->sym = NULL;
  249. } else {
  250. al->addr = al->map->map_ip(al->map, al->addr);
  251. al->sym = map__find_symbol(al->map, session, al->addr, filter);
  252. }
  253. }
  254. static void dso__calc_col_width(struct dso *self)
  255. {
  256. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  257. (!symbol_conf.dso_list ||
  258. strlist__has_entry(symbol_conf.dso_list, self->name))) {
  259. unsigned int slen = strlen(self->name);
  260. if (slen > dsos__col_width)
  261. dsos__col_width = slen;
  262. }
  263. self->slen_calculated = 1;
  264. }
  265. int event__preprocess_sample(const event_t *self, struct perf_session *session,
  266. struct addr_location *al, symbol_filter_t filter)
  267. {
  268. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  269. struct thread *thread = perf_session__findnew(session, self->ip.pid);
  270. if (thread == NULL)
  271. return -1;
  272. if (symbol_conf.comm_list &&
  273. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  274. goto out_filtered;
  275. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  276. thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
  277. self->ip.ip, al, filter);
  278. dump_printf(" ...... dso: %s\n",
  279. al->map ? al->map->dso->long_name :
  280. al->level == 'H' ? "[hypervisor]" : "<not found>");
  281. /*
  282. * We have to do this here as we may have a dso with no symbol hit that
  283. * has a name longer than the ones with symbols sampled.
  284. */
  285. if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
  286. dso__calc_col_width(al->map->dso);
  287. if (symbol_conf.dso_list &&
  288. (!al->map || !al->map->dso ||
  289. !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
  290. (al->map->dso->short_name != al->map->dso->long_name &&
  291. strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
  292. goto out_filtered;
  293. if (symbol_conf.sym_list && al->sym &&
  294. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  295. goto out_filtered;
  296. al->filtered = false;
  297. return 0;
  298. out_filtered:
  299. al->filtered = true;
  300. return 0;
  301. }
  302. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  303. {
  304. u64 *array = event->sample.array;
  305. if (type & PERF_SAMPLE_IP) {
  306. data->ip = event->ip.ip;
  307. array++;
  308. }
  309. if (type & PERF_SAMPLE_TID) {
  310. u32 *p = (u32 *)array;
  311. data->pid = p[0];
  312. data->tid = p[1];
  313. array++;
  314. }
  315. if (type & PERF_SAMPLE_TIME) {
  316. data->time = *array;
  317. array++;
  318. }
  319. if (type & PERF_SAMPLE_ADDR) {
  320. data->addr = *array;
  321. array++;
  322. }
  323. if (type & PERF_SAMPLE_ID) {
  324. data->id = *array;
  325. array++;
  326. }
  327. if (type & PERF_SAMPLE_STREAM_ID) {
  328. data->stream_id = *array;
  329. array++;
  330. }
  331. if (type & PERF_SAMPLE_CPU) {
  332. u32 *p = (u32 *)array;
  333. data->cpu = *p;
  334. array++;
  335. }
  336. if (type & PERF_SAMPLE_PERIOD) {
  337. data->period = *array;
  338. array++;
  339. }
  340. if (type & PERF_SAMPLE_READ) {
  341. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  342. return -1;
  343. }
  344. if (type & PERF_SAMPLE_CALLCHAIN) {
  345. data->callchain = (struct ip_callchain *)array;
  346. array += 1 + data->callchain->nr;
  347. }
  348. if (type & PERF_SAMPLE_RAW) {
  349. u32 *p = (u32 *)array;
  350. data->raw_size = *p;
  351. p++;
  352. data->raw_data = p;
  353. }
  354. return 0;
  355. }