event.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include <linux/types.h>
  2. #include "event.h"
  3. #include "debug.h"
  4. #include "string.h"
  5. #include "thread.h"
  6. static pid_t event__synthesize_comm(pid_t pid, int full,
  7. int (*process)(event_t *event,
  8. struct perf_session *session),
  9. struct perf_session *session)
  10. {
  11. event_t ev;
  12. char filename[PATH_MAX];
  13. char bf[BUFSIZ];
  14. FILE *fp;
  15. size_t size = 0;
  16. DIR *tasks;
  17. struct dirent dirent, *next;
  18. pid_t tgid = 0;
  19. snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
  20. fp = fopen(filename, "r");
  21. if (fp == NULL) {
  22. out_race:
  23. /*
  24. * We raced with a task exiting - just return:
  25. */
  26. pr_debug("couldn't open %s\n", filename);
  27. return 0;
  28. }
  29. memset(&ev.comm, 0, sizeof(ev.comm));
  30. while (!ev.comm.comm[0] || !ev.comm.pid) {
  31. if (fgets(bf, sizeof(bf), fp) == NULL)
  32. goto out_failure;
  33. if (memcmp(bf, "Name:", 5) == 0) {
  34. char *name = bf + 5;
  35. while (*name && isspace(*name))
  36. ++name;
  37. size = strlen(name) - 1;
  38. memcpy(ev.comm.comm, name, size++);
  39. } else if (memcmp(bf, "Tgid:", 5) == 0) {
  40. char *tgids = bf + 5;
  41. while (*tgids && isspace(*tgids))
  42. ++tgids;
  43. tgid = ev.comm.pid = atoi(tgids);
  44. }
  45. }
  46. ev.comm.header.type = PERF_RECORD_COMM;
  47. size = ALIGN(size, sizeof(u64));
  48. ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
  49. if (!full) {
  50. ev.comm.tid = pid;
  51. process(&ev, session);
  52. goto out_fclose;
  53. }
  54. snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
  55. tasks = opendir(filename);
  56. if (tasks == NULL)
  57. goto out_race;
  58. while (!readdir_r(tasks, &dirent, &next) && next) {
  59. char *end;
  60. pid = strtol(dirent.d_name, &end, 10);
  61. if (*end)
  62. continue;
  63. ev.comm.tid = pid;
  64. process(&ev, session);
  65. }
  66. closedir(tasks);
  67. out_fclose:
  68. fclose(fp);
  69. return tgid;
  70. out_failure:
  71. pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
  72. return -1;
  73. }
  74. static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
  75. int (*process)(event_t *event,
  76. struct perf_session *session),
  77. struct perf_session *session)
  78. {
  79. char filename[PATH_MAX];
  80. FILE *fp;
  81. snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
  82. fp = fopen(filename, "r");
  83. if (fp == NULL) {
  84. /*
  85. * We raced with a task exiting - just return:
  86. */
  87. pr_debug("couldn't open %s\n", filename);
  88. return -1;
  89. }
  90. while (1) {
  91. char bf[BUFSIZ], *pbf = bf;
  92. event_t ev = {
  93. .header = { .type = PERF_RECORD_MMAP },
  94. };
  95. int n;
  96. size_t size;
  97. if (fgets(bf, sizeof(bf), fp) == NULL)
  98. break;
  99. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  100. n = hex2u64(pbf, &ev.mmap.start);
  101. if (n < 0)
  102. continue;
  103. pbf += n + 1;
  104. n = hex2u64(pbf, &ev.mmap.len);
  105. if (n < 0)
  106. continue;
  107. pbf += n + 3;
  108. if (*pbf == 'x') { /* vm_exec */
  109. char *execname = strchr(bf, '/');
  110. /* Catch VDSO */
  111. if (execname == NULL)
  112. execname = strstr(bf, "[vdso]");
  113. if (execname == NULL)
  114. continue;
  115. size = strlen(execname);
  116. execname[size - 1] = '\0'; /* Remove \n */
  117. memcpy(ev.mmap.filename, execname, size);
  118. size = ALIGN(size, sizeof(u64));
  119. ev.mmap.len -= ev.mmap.start;
  120. ev.mmap.header.size = (sizeof(ev.mmap) -
  121. (sizeof(ev.mmap.filename) - size));
  122. ev.mmap.pid = tgid;
  123. ev.mmap.tid = pid;
  124. process(&ev, session);
  125. }
  126. }
  127. fclose(fp);
  128. return 0;
  129. }
  130. int event__synthesize_thread(pid_t pid,
  131. int (*process)(event_t *event,
  132. struct perf_session *session),
  133. struct perf_session *session)
  134. {
  135. pid_t tgid = event__synthesize_comm(pid, 1, process, session);
  136. if (tgid == -1)
  137. return -1;
  138. return event__synthesize_mmap_events(pid, tgid, process, session);
  139. }
  140. void event__synthesize_threads(int (*process)(event_t *event,
  141. struct perf_session *session),
  142. struct perf_session *session)
  143. {
  144. DIR *proc;
  145. struct dirent dirent, *next;
  146. proc = opendir("/proc");
  147. while (!readdir_r(proc, &dirent, &next) && next) {
  148. char *end;
  149. pid_t pid = strtol(dirent.d_name, &end, 10);
  150. if (*end) /* only interested in proper numerical dirents */
  151. continue;
  152. event__synthesize_thread(pid, process, session);
  153. }
  154. closedir(proc);
  155. }
  156. char *event__cwd;
  157. int event__cwdlen;
  158. struct events_stats event__stats;
  159. int event__process_comm(event_t *self, struct perf_session *session __used)
  160. {
  161. struct thread *thread = threads__findnew(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 __used)
  170. {
  171. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  172. event__stats.lost += self->lost.lost;
  173. return 0;
  174. }
  175. int event__process_mmap(event_t *self, struct perf_session *session __used)
  176. {
  177. struct thread *thread = threads__findnew(self->mmap.pid);
  178. struct map *map = map__new(&self->mmap, MAP__FUNCTION,
  179. event__cwd, event__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 __used)
  193. {
  194. struct thread *thread = threads__findnew(self->fork.pid);
  195. struct thread *parent = threads__findnew(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, u8 cpumode,
  213. enum map_type type, u64 addr,
  214. struct addr_location *al,
  215. symbol_filter_t filter)
  216. {
  217. struct map_groups *mg = &self->mg;
  218. al->thread = self;
  219. al->addr = addr;
  220. if (cpumode & PERF_RECORD_MISC_KERNEL) {
  221. al->level = 'k';
  222. mg = kmaps;
  223. } else if (cpumode & PERF_RECORD_MISC_USER)
  224. al->level = '.';
  225. else {
  226. al->level = 'H';
  227. al->map = NULL;
  228. al->sym = NULL;
  229. return;
  230. }
  231. try_again:
  232. al->map = map_groups__find(mg, type, al->addr);
  233. if (al->map == NULL) {
  234. /*
  235. * If this is outside of all known maps, and is a negative
  236. * address, try to look it up in the kernel dso, as it might be
  237. * a vsyscall or vdso (which executes in user-mode).
  238. *
  239. * XXX This is nasty, we should have a symbol list in the
  240. * "[vdso]" dso, but for now lets use the old trick of looking
  241. * in the whole kernel symbol list.
  242. */
  243. if ((long long)al->addr < 0 && mg != kmaps) {
  244. mg = kmaps;
  245. goto try_again;
  246. }
  247. al->sym = NULL;
  248. } else {
  249. al->addr = al->map->map_ip(al->map, al->addr);
  250. al->sym = map__find_symbol(al->map, al->addr, filter);
  251. }
  252. }
  253. int event__preprocess_sample(const event_t *self, struct addr_location *al,
  254. symbol_filter_t filter)
  255. {
  256. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  257. struct thread *thread = threads__findnew(self->ip.pid);
  258. if (thread == NULL)
  259. return -1;
  260. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  261. thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
  262. self->ip.ip, al, filter);
  263. dump_printf(" ...... dso: %s\n",
  264. al->map ? al->map->dso->long_name :
  265. al->level == 'H' ? "[hypervisor]" : "<not found>");
  266. return 0;
  267. }
  268. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  269. {
  270. u64 *array = event->sample.array;
  271. if (type & PERF_SAMPLE_IP) {
  272. data->ip = event->ip.ip;
  273. array++;
  274. }
  275. if (type & PERF_SAMPLE_TID) {
  276. u32 *p = (u32 *)array;
  277. data->pid = p[0];
  278. data->tid = p[1];
  279. array++;
  280. }
  281. if (type & PERF_SAMPLE_TIME) {
  282. data->time = *array;
  283. array++;
  284. }
  285. if (type & PERF_SAMPLE_ADDR) {
  286. data->addr = *array;
  287. array++;
  288. }
  289. if (type & PERF_SAMPLE_ID) {
  290. data->id = *array;
  291. array++;
  292. }
  293. if (type & PERF_SAMPLE_STREAM_ID) {
  294. data->stream_id = *array;
  295. array++;
  296. }
  297. if (type & PERF_SAMPLE_CPU) {
  298. u32 *p = (u32 *)array;
  299. data->cpu = *p;
  300. array++;
  301. }
  302. if (type & PERF_SAMPLE_PERIOD) {
  303. data->period = *array;
  304. array++;
  305. }
  306. if (type & PERF_SAMPLE_READ) {
  307. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  308. return -1;
  309. }
  310. if (type & PERF_SAMPLE_CALLCHAIN) {
  311. data->callchain = (struct ip_callchain *)array;
  312. array += 1 + data->callchain->nr;
  313. }
  314. if (type & PERF_SAMPLE_RAW) {
  315. u32 *p = (u32 *)array;
  316. data->raw_size = *p;
  317. p++;
  318. data->raw_data = p;
  319. }
  320. return 0;
  321. }