event.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include <linux/types.h>
  2. #include "event.h"
  3. #include "debug.h"
  4. #include "session.h"
  5. #include "string.h"
  6. #include "thread.h"
  7. static pid_t event__synthesize_comm(pid_t pid, int full,
  8. int (*process)(event_t *event,
  9. struct perf_session *session),
  10. struct perf_session *session)
  11. {
  12. event_t ev;
  13. char filename[PATH_MAX];
  14. char bf[BUFSIZ];
  15. FILE *fp;
  16. size_t size = 0;
  17. DIR *tasks;
  18. struct dirent dirent, *next;
  19. pid_t tgid = 0;
  20. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  21. fp = fopen(filename, "r");
  22. if (fp == NULL) {
  23. out_race:
  24. /*
  25. * We raced with a task exiting - just return:
  26. */
  27. pr_debug("couldn't open %s\n", filename);
  28. return 0;
  29. }
  30. memset(&ev.comm, 0, sizeof(ev.comm));
  31. while (!ev.comm.comm[0] || !ev.comm.pid) {
  32. if (fgets(bf, sizeof(bf), fp) == NULL)
  33. goto out_failure;
  34. if (memcmp(bf, "Name:", 5) == 0) {
  35. char *name = bf + 5;
  36. while (*name && isspace(*name))
  37. ++name;
  38. size = strlen(name) - 1;
  39. memcpy(ev.comm.comm, name, size++);
  40. } else if (memcmp(bf, "Tgid:", 5) == 0) {
  41. char *tgids = bf + 5;
  42. while (*tgids && isspace(*tgids))
  43. ++tgids;
  44. tgid = ev.comm.pid = atoi(tgids);
  45. }
  46. }
  47. ev.comm.header.type = PERF_RECORD_COMM;
  48. size = ALIGN(size, sizeof(u64));
  49. ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
  50. if (!full) {
  51. ev.comm.tid = pid;
  52. process(&ev, session);
  53. goto out_fclose;
  54. }
  55. snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
  56. tasks = opendir(filename);
  57. if (tasks == NULL)
  58. goto out_race;
  59. while (!readdir_r(tasks, &dirent, &next) && next) {
  60. char *end;
  61. pid = strtol(dirent.d_name, &end, 10);
  62. if (*end)
  63. continue;
  64. ev.comm.tid = pid;
  65. process(&ev, session);
  66. }
  67. closedir(tasks);
  68. out_fclose:
  69. fclose(fp);
  70. return tgid;
  71. out_failure:
  72. pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
  73. return -1;
  74. }
  75. static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
  76. int (*process)(event_t *event,
  77. struct perf_session *session),
  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,
  132. int (*process)(event_t *event,
  133. struct perf_session *session),
  134. struct perf_session *session)
  135. {
  136. pid_t tgid = event__synthesize_comm(pid, 1, process, session);
  137. if (tgid == -1)
  138. return -1;
  139. return event__synthesize_mmap_events(pid, tgid, process, session);
  140. }
  141. void event__synthesize_threads(int (*process)(event_t *event,
  142. struct perf_session *session),
  143. struct perf_session *session)
  144. {
  145. DIR *proc;
  146. struct dirent dirent, *next;
  147. proc = opendir("/proc");
  148. while (!readdir_r(proc, &dirent, &next) && next) {
  149. char *end;
  150. pid_t pid = strtol(dirent.d_name, &end, 10);
  151. if (*end) /* only interested in proper numerical dirents */
  152. continue;
  153. event__synthesize_thread(pid, process, session);
  154. }
  155. closedir(proc);
  156. }
  157. struct events_stats event__stats;
  158. int event__process_comm(event_t *self, struct perf_session *session __used)
  159. {
  160. struct thread *thread = threads__findnew(self->comm.pid);
  161. dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
  162. if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
  163. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. int event__process_lost(event_t *self, struct perf_session *session __used)
  169. {
  170. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  171. event__stats.lost += self->lost.lost;
  172. return 0;
  173. }
  174. int event__process_mmap(event_t *self, struct perf_session *session)
  175. {
  176. struct thread *thread = threads__findnew(self->mmap.pid);
  177. struct map *map = map__new(&self->mmap, MAP__FUNCTION,
  178. session->cwd, session->cwdlen);
  179. dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
  180. self->mmap.pid, self->mmap.tid,
  181. (void *)(long)self->mmap.start,
  182. (void *)(long)self->mmap.len,
  183. (void *)(long)self->mmap.pgoff,
  184. self->mmap.filename);
  185. if (thread == NULL || map == NULL)
  186. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  187. else
  188. thread__insert_map(thread, map);
  189. return 0;
  190. }
  191. int event__process_task(event_t *self, struct perf_session *session __used)
  192. {
  193. struct thread *thread = threads__findnew(self->fork.pid);
  194. struct thread *parent = threads__findnew(self->fork.ppid);
  195. dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
  196. self->fork.ppid, self->fork.ptid);
  197. /*
  198. * A thread clone will have the same PID for both parent and child.
  199. */
  200. if (thread == parent)
  201. return 0;
  202. if (self->header.type == PERF_RECORD_EXIT)
  203. return 0;
  204. if (thread == NULL || parent == NULL ||
  205. thread__fork(thread, parent) < 0) {
  206. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. void thread__find_addr_location(struct thread *self, u8 cpumode,
  212. enum map_type type, u64 addr,
  213. struct addr_location *al,
  214. symbol_filter_t filter)
  215. {
  216. struct map_groups *mg = &self->mg;
  217. al->thread = self;
  218. al->addr = addr;
  219. if (cpumode & PERF_RECORD_MISC_KERNEL) {
  220. al->level = 'k';
  221. mg = kmaps;
  222. } else if (cpumode & PERF_RECORD_MISC_USER)
  223. al->level = '.';
  224. else {
  225. al->level = 'H';
  226. al->map = NULL;
  227. al->sym = NULL;
  228. return;
  229. }
  230. try_again:
  231. al->map = map_groups__find(mg, type, al->addr);
  232. if (al->map == NULL) {
  233. /*
  234. * If this is outside of all known maps, and is a negative
  235. * address, try to look it up in the kernel dso, as it might be
  236. * a vsyscall or vdso (which executes in user-mode).
  237. *
  238. * XXX This is nasty, we should have a symbol list in the
  239. * "[vdso]" dso, but for now lets use the old trick of looking
  240. * in the whole kernel symbol list.
  241. */
  242. if ((long long)al->addr < 0 && mg != kmaps) {
  243. mg = kmaps;
  244. goto try_again;
  245. }
  246. al->sym = NULL;
  247. } else {
  248. al->addr = al->map->map_ip(al->map, al->addr);
  249. al->sym = map__find_symbol(al->map, al->addr, filter);
  250. }
  251. }
  252. int event__preprocess_sample(const event_t *self, struct addr_location *al,
  253. symbol_filter_t filter)
  254. {
  255. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  256. struct thread *thread = threads__findnew(self->ip.pid);
  257. if (thread == NULL)
  258. return -1;
  259. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  260. thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
  261. self->ip.ip, al, filter);
  262. dump_printf(" ...... dso: %s\n",
  263. al->map ? al->map->dso->long_name :
  264. al->level == 'H' ? "[hypervisor]" : "<not found>");
  265. return 0;
  266. }
  267. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  268. {
  269. u64 *array = event->sample.array;
  270. if (type & PERF_SAMPLE_IP) {
  271. data->ip = event->ip.ip;
  272. array++;
  273. }
  274. if (type & PERF_SAMPLE_TID) {
  275. u32 *p = (u32 *)array;
  276. data->pid = p[0];
  277. data->tid = p[1];
  278. array++;
  279. }
  280. if (type & PERF_SAMPLE_TIME) {
  281. data->time = *array;
  282. array++;
  283. }
  284. if (type & PERF_SAMPLE_ADDR) {
  285. data->addr = *array;
  286. array++;
  287. }
  288. if (type & PERF_SAMPLE_ID) {
  289. data->id = *array;
  290. array++;
  291. }
  292. if (type & PERF_SAMPLE_STREAM_ID) {
  293. data->stream_id = *array;
  294. array++;
  295. }
  296. if (type & PERF_SAMPLE_CPU) {
  297. u32 *p = (u32 *)array;
  298. data->cpu = *p;
  299. array++;
  300. }
  301. if (type & PERF_SAMPLE_PERIOD) {
  302. data->period = *array;
  303. array++;
  304. }
  305. if (type & PERF_SAMPLE_READ) {
  306. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  307. return -1;
  308. }
  309. if (type & PERF_SAMPLE_CALLCHAIN) {
  310. data->callchain = (struct ip_callchain *)array;
  311. array += 1 + data->callchain->nr;
  312. }
  313. if (type & PERF_SAMPLE_RAW) {
  314. u32 *p = (u32 *)array;
  315. data->raw_size = *p;
  316. p++;
  317. data->raw_data = p;
  318. }
  319. return 0;
  320. }