builtin-trace.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "builtin.h"
  2. #include "util/evlist.h"
  3. #include "util/parse-options.h"
  4. #include "util/thread_map.h"
  5. #include "event-parse.h"
  6. #include <libaudit.h>
  7. #include <stdlib.h>
  8. static struct syscall_fmt {
  9. const char *name;
  10. const char *alias;
  11. bool errmsg;
  12. bool timeout;
  13. } syscall_fmts[] = {
  14. { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
  15. { .name = "fstat", .errmsg = true, .alias = "newfstat", },
  16. { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
  17. { .name = "futex", .errmsg = true, },
  18. { .name = "poll", .errmsg = true, .timeout = true, },
  19. { .name = "ppoll", .errmsg = true, .timeout = true, },
  20. { .name = "read", .errmsg = true, },
  21. { .name = "recvfrom", .errmsg = true, },
  22. { .name = "select", .errmsg = true, .timeout = true, },
  23. { .name = "stat", .errmsg = true, .alias = "newstat", },
  24. };
  25. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  26. {
  27. const struct syscall_fmt *fmt = fmtp;
  28. return strcmp(name, fmt->name);
  29. }
  30. static struct syscall_fmt *syscall_fmt__find(const char *name)
  31. {
  32. const int nmemb = ARRAY_SIZE(syscall_fmts);
  33. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  34. }
  35. struct syscall {
  36. struct event_format *tp_format;
  37. const char *name;
  38. struct syscall_fmt *fmt;
  39. };
  40. struct trace {
  41. int audit_machine;
  42. struct {
  43. int max;
  44. struct syscall *table;
  45. } syscalls;
  46. struct perf_record_opts opts;
  47. };
  48. static int trace__read_syscall_info(struct trace *trace, int id)
  49. {
  50. char tp_name[128];
  51. struct syscall *sc;
  52. if (id > trace->syscalls.max) {
  53. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  54. if (nsyscalls == NULL)
  55. return -1;
  56. if (trace->syscalls.max != -1) {
  57. memset(nsyscalls + trace->syscalls.max + 1, 0,
  58. (id - trace->syscalls.max) * sizeof(*sc));
  59. } else {
  60. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  61. }
  62. trace->syscalls.table = nsyscalls;
  63. trace->syscalls.max = id;
  64. }
  65. sc = trace->syscalls.table + id;
  66. sc->name = audit_syscall_to_name(id, trace->audit_machine);
  67. if (sc->name == NULL)
  68. return -1;
  69. sc->fmt = syscall_fmt__find(sc->name);
  70. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  71. sc->tp_format = event_format__new("syscalls", tp_name);
  72. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  73. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  74. sc->tp_format = event_format__new("syscalls", tp_name);
  75. }
  76. return sc->tp_format != NULL ? 0 : -1;
  77. }
  78. static size_t syscall__fprintf_args(struct syscall *sc, unsigned long *args, FILE *fp)
  79. {
  80. int i = 0;
  81. size_t printed = 0;
  82. if (sc->tp_format != NULL) {
  83. struct format_field *field;
  84. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  85. printed += fprintf(fp, "%s%s: %ld", printed ? ", " : "",
  86. field->name, args[i++]);
  87. }
  88. } else {
  89. while (i < 6) {
  90. printed += fprintf(fp, "%sarg%d: %ld", printed ? ", " : "", i, args[i]);
  91. ++i;
  92. }
  93. }
  94. return printed;
  95. }
  96. static int trace__run(struct trace *trace)
  97. {
  98. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  99. struct perf_evsel *evsel, *evsel_enter, *evsel_exit;
  100. int err = -1, i, nr_events = 0, before;
  101. if (evlist == NULL) {
  102. printf("Not enough memory to run!\n");
  103. goto out;
  104. }
  105. evsel_enter = perf_evsel__newtp("raw_syscalls", "sys_enter", 0);
  106. if (evsel_enter == NULL) {
  107. printf("Couldn't read the raw_syscalls:sys_enter tracepoint information!\n");
  108. goto out_delete_evlist;
  109. }
  110. perf_evlist__add(evlist, evsel_enter);
  111. evsel_exit = perf_evsel__newtp("raw_syscalls", "sys_exit", 1);
  112. if (evsel_exit == NULL) {
  113. printf("Couldn't read the raw_syscalls:sys_exit tracepoint information!\n");
  114. goto out_delete_evlist;
  115. }
  116. perf_evlist__add(evlist, evsel_exit);
  117. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  118. if (err < 0) {
  119. printf("Problems parsing the target to trace, check your options!\n");
  120. goto out_delete_evlist;
  121. }
  122. perf_evlist__config_attrs(evlist, &trace->opts);
  123. err = perf_evlist__open(evlist);
  124. if (err < 0) {
  125. printf("Couldn't create the events: %s\n", strerror(errno));
  126. goto out_delete_evlist;
  127. }
  128. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  129. if (err < 0) {
  130. printf("Couldn't mmap the events: %s\n", strerror(errno));
  131. goto out_delete_evlist;
  132. }
  133. perf_evlist__enable(evlist);
  134. again:
  135. before = nr_events;
  136. for (i = 0; i < evlist->nr_mmaps; i++) {
  137. union perf_event *event;
  138. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  139. const u32 type = event->header.type;
  140. struct syscall *sc;
  141. struct perf_sample sample;
  142. int id;
  143. ++nr_events;
  144. switch (type) {
  145. case PERF_RECORD_SAMPLE:
  146. break;
  147. case PERF_RECORD_LOST:
  148. printf("LOST %" PRIu64 " events!\n", event->lost.lost);
  149. continue;
  150. default:
  151. printf("Unexpected %s event, skipping...\n",
  152. perf_event__name(type));
  153. continue;
  154. }
  155. err = perf_evlist__parse_sample(evlist, event, &sample);
  156. if (err) {
  157. printf("Can't parse sample, err = %d, skipping...\n", err);
  158. continue;
  159. }
  160. evsel = perf_evlist__id2evsel(evlist, sample.id);
  161. if (evsel == NULL) {
  162. printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  163. continue;
  164. }
  165. id = perf_evsel__intval(evsel, &sample, "id");
  166. if (id < 0) {
  167. printf("Invalid syscall %d id, skipping...\n", id);
  168. continue;
  169. }
  170. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  171. trace__read_syscall_info(trace, id))
  172. continue;
  173. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  174. continue;
  175. sc = &trace->syscalls.table[id];
  176. if (evlist->threads->map[0] == -1 || evlist->threads->nr > 1)
  177. printf("%d ", sample.tid);
  178. if (evsel == evsel_enter) {
  179. void *args = perf_evsel__rawptr(evsel, &sample, "args");
  180. printf("%s(", sc->name);
  181. syscall__fprintf_args(sc, args, stdout);
  182. } else if (evsel == evsel_exit) {
  183. int ret = perf_evsel__intval(evsel, &sample, "ret");
  184. if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
  185. char bf[256];
  186. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  187. *e = audit_errno_to_name(-ret);
  188. printf(") = -1 %s %s", e, emsg);
  189. } else if (ret == 0 && sc->fmt && sc->fmt->timeout)
  190. printf(") = 0 Timeout");
  191. else
  192. printf(") = %d", ret);
  193. putchar('\n');
  194. }
  195. }
  196. }
  197. if (nr_events == before)
  198. poll(evlist->pollfd, evlist->nr_fds, -1);
  199. goto again;
  200. out_delete_evlist:
  201. perf_evlist__delete(evlist);
  202. out:
  203. return err;
  204. }
  205. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  206. {
  207. const char * const trace_usage[] = {
  208. "perf trace [<options>]",
  209. NULL
  210. };
  211. struct trace trace = {
  212. .audit_machine = audit_detect_machine(),
  213. .syscalls = {
  214. . max = -1,
  215. },
  216. .opts = {
  217. .target = {
  218. .uid = UINT_MAX,
  219. .uses_mmap = true,
  220. },
  221. .user_freq = UINT_MAX,
  222. .user_interval = ULLONG_MAX,
  223. .no_delay = true,
  224. .mmap_pages = 1024,
  225. },
  226. };
  227. const struct option trace_options[] = {
  228. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  229. "trace events on existing process id"),
  230. OPT_STRING(0, "tid", &trace.opts.target.tid, "tid",
  231. "trace events on existing thread id"),
  232. OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide,
  233. "system-wide collection from all CPUs"),
  234. OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu",
  235. "list of cpus to monitor"),
  236. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  237. "child tasks do not inherit counters"),
  238. OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages,
  239. "number of mmap data pages"),
  240. OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user",
  241. "user to profile"),
  242. OPT_END()
  243. };
  244. int err;
  245. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  246. if (argc)
  247. usage_with_options(trace_usage, trace_options);
  248. err = perf_target__parse_uid(&trace.opts.target);
  249. if (err) {
  250. char bf[BUFSIZ];
  251. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  252. printf("%s", bf);
  253. return err;
  254. }
  255. return trace__run(&trace);
  256. }