event.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. static void thread__comm_adjust(struct thread *self)
  160. {
  161. char *comm = self->comm;
  162. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  163. (!symbol_conf.comm_list ||
  164. strlist__has_entry(symbol_conf.comm_list, comm))) {
  165. unsigned int slen = strlen(comm);
  166. if (slen > comms__col_width) {
  167. comms__col_width = slen;
  168. threads__col_width = slen + 6;
  169. }
  170. }
  171. }
  172. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  173. {
  174. int ret = thread__set_comm(self, comm);
  175. if (ret)
  176. return ret;
  177. thread__comm_adjust(self);
  178. return 0;
  179. }
  180. int event__process_comm(event_t *self, struct perf_session *session)
  181. {
  182. struct thread *thread = perf_session__findnew(session, self->comm.pid);
  183. dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
  184. if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
  185. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. int event__process_lost(event_t *self, struct perf_session *session)
  191. {
  192. dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
  193. session->events_stats.lost += self->lost.lost;
  194. return 0;
  195. }
  196. int event__process_mmap(event_t *self, struct perf_session *session)
  197. {
  198. struct thread *thread = perf_session__findnew(session, self->mmap.pid);
  199. struct map *map = map__new(&self->mmap, MAP__FUNCTION,
  200. session->cwd, session->cwdlen);
  201. dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
  202. self->mmap.pid, self->mmap.tid,
  203. (void *)(long)self->mmap.start,
  204. (void *)(long)self->mmap.len,
  205. (void *)(long)self->mmap.pgoff,
  206. self->mmap.filename);
  207. if (thread == NULL || map == NULL)
  208. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  209. else
  210. thread__insert_map(thread, map);
  211. return 0;
  212. }
  213. int event__process_task(event_t *self, struct perf_session *session)
  214. {
  215. struct thread *thread = perf_session__findnew(session, self->fork.pid);
  216. struct thread *parent = perf_session__findnew(session, self->fork.ppid);
  217. dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
  218. self->fork.ppid, self->fork.ptid);
  219. /*
  220. * A thread clone will have the same PID for both parent and child.
  221. */
  222. if (thread == parent)
  223. return 0;
  224. if (self->header.type == PERF_RECORD_EXIT)
  225. return 0;
  226. if (thread == NULL || parent == NULL ||
  227. thread__fork(thread, parent) < 0) {
  228. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. void thread__find_addr_location(struct thread *self,
  234. struct perf_session *session, u8 cpumode,
  235. enum map_type type, u64 addr,
  236. struct addr_location *al,
  237. symbol_filter_t filter)
  238. {
  239. struct map_groups *mg = &self->mg;
  240. al->thread = self;
  241. al->addr = addr;
  242. if (cpumode & PERF_RECORD_MISC_KERNEL) {
  243. al->level = 'k';
  244. mg = &session->kmaps;
  245. } else if (cpumode & PERF_RECORD_MISC_USER)
  246. al->level = '.';
  247. else {
  248. al->level = 'H';
  249. al->map = NULL;
  250. al->sym = NULL;
  251. return;
  252. }
  253. try_again:
  254. al->map = map_groups__find(mg, type, al->addr);
  255. if (al->map == NULL) {
  256. /*
  257. * If this is outside of all known maps, and is a negative
  258. * address, try to look it up in the kernel dso, as it might be
  259. * a vsyscall or vdso (which executes in user-mode).
  260. *
  261. * XXX This is nasty, we should have a symbol list in the
  262. * "[vdso]" dso, but for now lets use the old trick of looking
  263. * in the whole kernel symbol list.
  264. */
  265. if ((long long)al->addr < 0 && mg != &session->kmaps) {
  266. mg = &session->kmaps;
  267. goto try_again;
  268. }
  269. al->sym = NULL;
  270. } else {
  271. al->addr = al->map->map_ip(al->map, al->addr);
  272. al->sym = map__find_symbol(al->map, session, al->addr, filter);
  273. }
  274. }
  275. static void dso__calc_col_width(struct dso *self)
  276. {
  277. if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  278. (!symbol_conf.dso_list ||
  279. strlist__has_entry(symbol_conf.dso_list, self->name))) {
  280. unsigned int slen = strlen(self->name);
  281. if (slen > dsos__col_width)
  282. dsos__col_width = slen;
  283. }
  284. self->slen_calculated = 1;
  285. }
  286. int event__preprocess_sample(const event_t *self, struct perf_session *session,
  287. struct addr_location *al, symbol_filter_t filter)
  288. {
  289. u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  290. struct thread *thread = perf_session__findnew(session, self->ip.pid);
  291. if (thread == NULL)
  292. return -1;
  293. if (symbol_conf.comm_list &&
  294. !strlist__has_entry(symbol_conf.comm_list, thread->comm))
  295. goto out_filtered;
  296. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  297. thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
  298. self->ip.ip, al, filter);
  299. dump_printf(" ...... dso: %s\n",
  300. al->map ? al->map->dso->long_name :
  301. al->level == 'H' ? "[hypervisor]" : "<not found>");
  302. /*
  303. * We have to do this here as we may have a dso with no symbol hit that
  304. * has a name longer than the ones with symbols sampled.
  305. */
  306. if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
  307. dso__calc_col_width(al->map->dso);
  308. if (symbol_conf.dso_list &&
  309. (!al->map || !al->map->dso ||
  310. !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
  311. (al->map->dso->short_name != al->map->dso->long_name &&
  312. strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
  313. goto out_filtered;
  314. if (symbol_conf.sym_list && al->sym &&
  315. !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
  316. goto out_filtered;
  317. al->filtered = false;
  318. return 0;
  319. out_filtered:
  320. al->filtered = true;
  321. return 0;
  322. }
  323. int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
  324. {
  325. u64 *array = event->sample.array;
  326. if (type & PERF_SAMPLE_IP) {
  327. data->ip = event->ip.ip;
  328. array++;
  329. }
  330. if (type & PERF_SAMPLE_TID) {
  331. u32 *p = (u32 *)array;
  332. data->pid = p[0];
  333. data->tid = p[1];
  334. array++;
  335. }
  336. if (type & PERF_SAMPLE_TIME) {
  337. data->time = *array;
  338. array++;
  339. }
  340. if (type & PERF_SAMPLE_ADDR) {
  341. data->addr = *array;
  342. array++;
  343. }
  344. if (type & PERF_SAMPLE_ID) {
  345. data->id = *array;
  346. array++;
  347. }
  348. if (type & PERF_SAMPLE_STREAM_ID) {
  349. data->stream_id = *array;
  350. array++;
  351. }
  352. if (type & PERF_SAMPLE_CPU) {
  353. u32 *p = (u32 *)array;
  354. data->cpu = *p;
  355. array++;
  356. }
  357. if (type & PERF_SAMPLE_PERIOD) {
  358. data->period = *array;
  359. array++;
  360. }
  361. if (type & PERF_SAMPLE_READ) {
  362. pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
  363. return -1;
  364. }
  365. if (type & PERF_SAMPLE_CALLCHAIN) {
  366. data->callchain = (struct ip_callchain *)array;
  367. array += 1 + data->callchain->nr;
  368. }
  369. if (type & PERF_SAMPLE_RAW) {
  370. u32 *p = (u32 *)array;
  371. data->raw_size = *p;
  372. p++;
  373. data->raw_data = p;
  374. }
  375. return 0;
  376. }