builtin-ftrace.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * builtin-ftrace.c
  3. *
  4. * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
  5. *
  6. * Released under the GPL v2.
  7. */
  8. #include "builtin.h"
  9. #include "perf.h"
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include "debug.h"
  13. #include <subcmd/parse-options.h>
  14. #include "evlist.h"
  15. #include "target.h"
  16. #include "thread_map.h"
  17. #include "util/config.h"
  18. #define DEFAULT_TRACER "function_graph"
  19. struct perf_ftrace {
  20. struct perf_evlist *evlist;
  21. struct target target;
  22. const char *tracer;
  23. };
  24. static bool done;
  25. static void sig_handler(int sig __maybe_unused)
  26. {
  27. done = true;
  28. }
  29. /*
  30. * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
  31. * we asked by setting its exec_error to the function below,
  32. * ftrace__workload_exec_failed_signal.
  33. *
  34. * XXX We need to handle this more appropriately, emitting an error, etc.
  35. */
  36. static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
  37. siginfo_t *info __maybe_unused,
  38. void *ucontext __maybe_unused)
  39. {
  40. /* workload_exec_errno = info->si_value.sival_int; */
  41. done = true;
  42. }
  43. static int write_tracing_file(const char *name, const char *val)
  44. {
  45. char *file;
  46. int fd, ret = -1;
  47. ssize_t size = strlen(val);
  48. file = get_tracing_file(name);
  49. if (!file) {
  50. pr_debug("cannot get tracing file: %s\n", name);
  51. return -1;
  52. }
  53. fd = open(file, O_WRONLY);
  54. if (fd < 0) {
  55. pr_debug("cannot open tracing file: %s\n", name);
  56. goto out;
  57. }
  58. if (write(fd, val, size) == size)
  59. ret = 0;
  60. else
  61. pr_debug("write '%s' to tracing/%s failed\n", val, name);
  62. close(fd);
  63. out:
  64. put_tracing_file(file);
  65. return ret;
  66. }
  67. static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
  68. {
  69. if (write_tracing_file("tracing_on", "0") < 0)
  70. return -1;
  71. if (write_tracing_file("current_tracer", "nop") < 0)
  72. return -1;
  73. if (write_tracing_file("set_ftrace_pid", " ") < 0)
  74. return -1;
  75. return 0;
  76. }
  77. static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
  78. {
  79. char *trace_file;
  80. int trace_fd;
  81. char *trace_pid;
  82. char buf[4096];
  83. struct pollfd pollfd = {
  84. .events = POLLIN,
  85. };
  86. if (geteuid() != 0) {
  87. pr_err("ftrace only works for root!\n");
  88. return -1;
  89. }
  90. if (argc < 1)
  91. return -1;
  92. signal(SIGINT, sig_handler);
  93. signal(SIGUSR1, sig_handler);
  94. signal(SIGCHLD, sig_handler);
  95. reset_tracing_files(ftrace);
  96. /* reset ftrace buffer */
  97. if (write_tracing_file("trace", "0") < 0)
  98. goto out;
  99. if (perf_evlist__prepare_workload(ftrace->evlist, &ftrace->target,
  100. argv, false, ftrace__workload_exec_failed_signal) < 0)
  101. goto out;
  102. if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
  103. pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
  104. goto out;
  105. }
  106. if (asprintf(&trace_pid, "%d", thread_map__pid(ftrace->evlist->threads, 0)) < 0) {
  107. pr_err("failed to allocate pid string\n");
  108. goto out;
  109. }
  110. if (write_tracing_file("set_ftrace_pid", trace_pid) < 0) {
  111. pr_err("failed to set pid: %s\n", trace_pid);
  112. goto out_free_pid;
  113. }
  114. trace_file = get_tracing_file("trace_pipe");
  115. if (!trace_file) {
  116. pr_err("failed to open trace_pipe\n");
  117. goto out_free_pid;
  118. }
  119. trace_fd = open(trace_file, O_RDONLY);
  120. put_tracing_file(trace_file);
  121. if (trace_fd < 0) {
  122. pr_err("failed to open trace_pipe\n");
  123. goto out_free_pid;
  124. }
  125. fcntl(trace_fd, F_SETFL, O_NONBLOCK);
  126. pollfd.fd = trace_fd;
  127. if (write_tracing_file("tracing_on", "1") < 0) {
  128. pr_err("can't enable tracing\n");
  129. goto out_close_fd;
  130. }
  131. perf_evlist__start_workload(ftrace->evlist);
  132. while (!done) {
  133. if (poll(&pollfd, 1, -1) < 0)
  134. break;
  135. if (pollfd.revents & POLLIN) {
  136. int n = read(trace_fd, buf, sizeof(buf));
  137. if (n < 0)
  138. break;
  139. if (fwrite(buf, n, 1, stdout) != 1)
  140. break;
  141. }
  142. }
  143. write_tracing_file("tracing_on", "0");
  144. /* read remaining buffer contents */
  145. while (true) {
  146. int n = read(trace_fd, buf, sizeof(buf));
  147. if (n <= 0)
  148. break;
  149. if (fwrite(buf, n, 1, stdout) != 1)
  150. break;
  151. }
  152. out_close_fd:
  153. close(trace_fd);
  154. out_free_pid:
  155. free(trace_pid);
  156. out:
  157. reset_tracing_files(ftrace);
  158. return done ? 0 : -1;
  159. }
  160. static int perf_ftrace_config(const char *var, const char *value, void *cb)
  161. {
  162. struct perf_ftrace *ftrace = cb;
  163. if (prefixcmp(var, "ftrace."))
  164. return 0;
  165. if (strcmp(var, "ftrace.tracer"))
  166. return -1;
  167. if (!strcmp(value, "function_graph") ||
  168. !strcmp(value, "function")) {
  169. ftrace->tracer = value;
  170. return 0;
  171. }
  172. pr_err("Please select \"function_graph\" (default) or \"function\"\n");
  173. return -1;
  174. }
  175. int cmd_ftrace(int argc, const char **argv, const char *prefix __maybe_unused)
  176. {
  177. int ret;
  178. struct perf_ftrace ftrace = {
  179. .tracer = DEFAULT_TRACER,
  180. .target = { .uid = UINT_MAX, },
  181. };
  182. const char * const ftrace_usage[] = {
  183. "perf ftrace [<options>] <command>",
  184. "perf ftrace [<options>] -- <command> [<options>]",
  185. NULL
  186. };
  187. const struct option ftrace_options[] = {
  188. OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
  189. "tracer to use: function_graph(default) or function"),
  190. OPT_INCR('v', "verbose", &verbose,
  191. "be more verbose"),
  192. OPT_END()
  193. };
  194. ret = perf_config(perf_ftrace_config, &ftrace);
  195. if (ret < 0)
  196. return -1;
  197. argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
  198. PARSE_OPT_STOP_AT_NON_OPTION);
  199. if (!argc)
  200. usage_with_options(ftrace_usage, ftrace_options);
  201. ftrace.evlist = perf_evlist__new();
  202. if (ftrace.evlist == NULL)
  203. return -ENOMEM;
  204. ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
  205. if (ret < 0)
  206. goto out_delete_evlist;
  207. ret = __cmd_ftrace(&ftrace, argc, argv);
  208. out_delete_evlist:
  209. perf_evlist__delete(ftrace.evlist);
  210. return ret;
  211. }