builtin-trace.c 8.7 KB

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