builtin-trace.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  97. struct perf_sample *sample);
  98. static struct syscall *trace__syscall_info(struct trace *trace,
  99. struct perf_evsel *evsel,
  100. struct perf_sample *sample)
  101. {
  102. int id = perf_evsel__intval(evsel, sample, "id");
  103. if (id < 0) {
  104. printf("Invalid syscall %d id, skipping...\n", id);
  105. return NULL;
  106. }
  107. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  108. trace__read_syscall_info(trace, id))
  109. goto out_cant_read;
  110. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  111. goto out_cant_read;
  112. return &trace->syscalls.table[id];
  113. out_cant_read:
  114. printf("Problems reading syscall %d information\n", id);
  115. return NULL;
  116. }
  117. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  118. struct perf_sample *sample)
  119. {
  120. void *args;
  121. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  122. if (sc == NULL)
  123. return -1;
  124. args = perf_evsel__rawptr(evsel, sample, "args");
  125. if (args == NULL) {
  126. printf("Problems reading syscall arguments\n");
  127. return -1;
  128. }
  129. printf("%s(", sc->name);
  130. syscall__fprintf_args(sc, args, stdout);
  131. return 0;
  132. }
  133. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  134. struct perf_sample *sample)
  135. {
  136. int ret;
  137. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  138. if (sc == NULL)
  139. return -1;
  140. ret = perf_evsel__intval(evsel, sample, "ret");
  141. if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
  142. char bf[256];
  143. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  144. *e = audit_errno_to_name(-ret);
  145. printf(") = -1 %s %s", e, emsg);
  146. } else if (ret == 0 && sc->fmt && sc->fmt->timeout)
  147. printf(") = 0 Timeout");
  148. else
  149. printf(") = %d", ret);
  150. putchar('\n');
  151. return 0;
  152. }
  153. static int trace__run(struct trace *trace)
  154. {
  155. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  156. struct perf_evsel *evsel;
  157. int err = -1, i, nr_events = 0, before;
  158. if (evlist == NULL) {
  159. printf("Not enough memory to run!\n");
  160. goto out;
  161. }
  162. if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
  163. perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
  164. printf("Couldn't read the raw_syscalls tracepoints information!\n");
  165. goto out_delete_evlist;
  166. }
  167. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  168. if (err < 0) {
  169. printf("Problems parsing the target to trace, check your options!\n");
  170. goto out_delete_evlist;
  171. }
  172. perf_evlist__config_attrs(evlist, &trace->opts);
  173. err = perf_evlist__open(evlist);
  174. if (err < 0) {
  175. printf("Couldn't create the events: %s\n", strerror(errno));
  176. goto out_delete_evlist;
  177. }
  178. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  179. if (err < 0) {
  180. printf("Couldn't mmap the events: %s\n", strerror(errno));
  181. goto out_delete_evlist;
  182. }
  183. perf_evlist__enable(evlist);
  184. again:
  185. before = nr_events;
  186. for (i = 0; i < evlist->nr_mmaps; i++) {
  187. union perf_event *event;
  188. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  189. const u32 type = event->header.type;
  190. tracepoint_handler handler;
  191. struct perf_sample sample;
  192. ++nr_events;
  193. switch (type) {
  194. case PERF_RECORD_SAMPLE:
  195. break;
  196. case PERF_RECORD_LOST:
  197. printf("LOST %" PRIu64 " events!\n", event->lost.lost);
  198. continue;
  199. default:
  200. printf("Unexpected %s event, skipping...\n",
  201. perf_event__name(type));
  202. continue;
  203. }
  204. err = perf_evlist__parse_sample(evlist, event, &sample);
  205. if (err) {
  206. printf("Can't parse sample, err = %d, skipping...\n", err);
  207. continue;
  208. }
  209. evsel = perf_evlist__id2evsel(evlist, sample.id);
  210. if (evsel == NULL) {
  211. printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  212. continue;
  213. }
  214. if (evlist->threads->map[0] == -1 || evlist->threads->nr > 1)
  215. printf("%d ", sample.tid);
  216. handler = evsel->handler.func;
  217. handler(trace, evsel, &sample);
  218. }
  219. }
  220. if (nr_events == before)
  221. poll(evlist->pollfd, evlist->nr_fds, -1);
  222. goto again;
  223. out_delete_evlist:
  224. perf_evlist__delete(evlist);
  225. out:
  226. return err;
  227. }
  228. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  229. {
  230. const char * const trace_usage[] = {
  231. "perf trace [<options>]",
  232. NULL
  233. };
  234. struct trace trace = {
  235. .audit_machine = audit_detect_machine(),
  236. .syscalls = {
  237. . max = -1,
  238. },
  239. .opts = {
  240. .target = {
  241. .uid = UINT_MAX,
  242. .uses_mmap = true,
  243. },
  244. .user_freq = UINT_MAX,
  245. .user_interval = ULLONG_MAX,
  246. .no_delay = true,
  247. .mmap_pages = 1024,
  248. },
  249. };
  250. const struct option trace_options[] = {
  251. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  252. "trace events on existing process id"),
  253. OPT_STRING(0, "tid", &trace.opts.target.tid, "tid",
  254. "trace events on existing thread id"),
  255. OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide,
  256. "system-wide collection from all CPUs"),
  257. OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu",
  258. "list of cpus to monitor"),
  259. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  260. "child tasks do not inherit counters"),
  261. OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages,
  262. "number of mmap data pages"),
  263. OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user",
  264. "user to profile"),
  265. OPT_END()
  266. };
  267. int err;
  268. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  269. if (argc)
  270. usage_with_options(trace_usage, trace_options);
  271. err = perf_target__parse_uid(&trace.opts.target);
  272. if (err) {
  273. char bf[BUFSIZ];
  274. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  275. printf("%s", bf);
  276. return err;
  277. }
  278. return trace__run(&trace);
  279. }