event.c 8.1 KB

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