builtin-ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 <errno.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <fcntl.h>
  14. #include <poll.h>
  15. #include "debug.h"
  16. #include <subcmd/parse-options.h>
  17. #include <api/fs/tracing_path.h>
  18. #include "evlist.h"
  19. #include "target.h"
  20. #include "cpumap.h"
  21. #include "thread_map.h"
  22. #include "util/config.h"
  23. #define DEFAULT_TRACER "function_graph"
  24. struct perf_ftrace {
  25. struct perf_evlist *evlist;
  26. struct target target;
  27. const char *tracer;
  28. struct list_head filters;
  29. struct list_head notrace;
  30. struct list_head graph_funcs;
  31. struct list_head nograph_funcs;
  32. int graph_depth;
  33. };
  34. struct filter_entry {
  35. struct list_head list;
  36. char name[];
  37. };
  38. static bool done;
  39. static void sig_handler(int sig __maybe_unused)
  40. {
  41. done = true;
  42. }
  43. /*
  44. * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
  45. * we asked by setting its exec_error to the function below,
  46. * ftrace__workload_exec_failed_signal.
  47. *
  48. * XXX We need to handle this more appropriately, emitting an error, etc.
  49. */
  50. static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
  51. siginfo_t *info __maybe_unused,
  52. void *ucontext __maybe_unused)
  53. {
  54. /* workload_exec_errno = info->si_value.sival_int; */
  55. done = true;
  56. }
  57. static int __write_tracing_file(const char *name, const char *val, bool append)
  58. {
  59. char *file;
  60. int fd, ret = -1;
  61. ssize_t size = strlen(val);
  62. int flags = O_WRONLY;
  63. char errbuf[512];
  64. file = get_tracing_file(name);
  65. if (!file) {
  66. pr_debug("cannot get tracing file: %s\n", name);
  67. return -1;
  68. }
  69. if (append)
  70. flags |= O_APPEND;
  71. else
  72. flags |= O_TRUNC;
  73. fd = open(file, flags);
  74. if (fd < 0) {
  75. pr_debug("cannot open tracing file: %s: %s\n",
  76. name, str_error_r(errno, errbuf, sizeof(errbuf)));
  77. goto out;
  78. }
  79. if (write(fd, val, size) == size)
  80. ret = 0;
  81. else
  82. pr_debug("write '%s' to tracing/%s failed: %s\n",
  83. val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
  84. close(fd);
  85. out:
  86. put_tracing_file(file);
  87. return ret;
  88. }
  89. static int write_tracing_file(const char *name, const char *val)
  90. {
  91. return __write_tracing_file(name, val, false);
  92. }
  93. static int append_tracing_file(const char *name, const char *val)
  94. {
  95. return __write_tracing_file(name, val, true);
  96. }
  97. static int reset_tracing_cpu(void);
  98. static void reset_tracing_filters(void);
  99. static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
  100. {
  101. if (write_tracing_file("tracing_on", "0") < 0)
  102. return -1;
  103. if (write_tracing_file("current_tracer", "nop") < 0)
  104. return -1;
  105. if (write_tracing_file("set_ftrace_pid", " ") < 0)
  106. return -1;
  107. if (reset_tracing_cpu() < 0)
  108. return -1;
  109. if (write_tracing_file("max_graph_depth", "0") < 0)
  110. return -1;
  111. reset_tracing_filters();
  112. return 0;
  113. }
  114. static int set_tracing_pid(struct perf_ftrace *ftrace)
  115. {
  116. int i;
  117. char buf[16];
  118. if (target__has_cpu(&ftrace->target))
  119. return 0;
  120. for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
  121. scnprintf(buf, sizeof(buf), "%d",
  122. ftrace->evlist->threads->map[i]);
  123. if (append_tracing_file("set_ftrace_pid", buf) < 0)
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. static int set_tracing_cpumask(struct cpu_map *cpumap)
  129. {
  130. char *cpumask;
  131. size_t mask_size;
  132. int ret;
  133. int last_cpu;
  134. last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
  135. mask_size = (last_cpu + 3) / 4 + 1;
  136. mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
  137. cpumask = malloc(mask_size);
  138. if (cpumask == NULL) {
  139. pr_debug("failed to allocate cpu mask\n");
  140. return -1;
  141. }
  142. cpu_map__snprint_mask(cpumap, cpumask, mask_size);
  143. ret = write_tracing_file("tracing_cpumask", cpumask);
  144. free(cpumask);
  145. return ret;
  146. }
  147. static int set_tracing_cpu(struct perf_ftrace *ftrace)
  148. {
  149. struct cpu_map *cpumap = ftrace->evlist->cpus;
  150. if (!target__has_cpu(&ftrace->target))
  151. return 0;
  152. return set_tracing_cpumask(cpumap);
  153. }
  154. static int reset_tracing_cpu(void)
  155. {
  156. struct cpu_map *cpumap = cpu_map__new(NULL);
  157. int ret;
  158. ret = set_tracing_cpumask(cpumap);
  159. cpu_map__put(cpumap);
  160. return ret;
  161. }
  162. static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
  163. {
  164. struct filter_entry *pos;
  165. list_for_each_entry(pos, funcs, list) {
  166. if (append_tracing_file(filter_file, pos->name) < 0)
  167. return -1;
  168. }
  169. return 0;
  170. }
  171. static int set_tracing_filters(struct perf_ftrace *ftrace)
  172. {
  173. int ret;
  174. ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
  175. if (ret < 0)
  176. return ret;
  177. ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
  178. if (ret < 0)
  179. return ret;
  180. ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
  181. if (ret < 0)
  182. return ret;
  183. /* old kernels do not have this filter */
  184. __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
  185. return ret;
  186. }
  187. static void reset_tracing_filters(void)
  188. {
  189. write_tracing_file("set_ftrace_filter", " ");
  190. write_tracing_file("set_ftrace_notrace", " ");
  191. write_tracing_file("set_graph_function", " ");
  192. write_tracing_file("set_graph_notrace", " ");
  193. }
  194. static int set_tracing_depth(struct perf_ftrace *ftrace)
  195. {
  196. char buf[16];
  197. if (ftrace->graph_depth == 0)
  198. return 0;
  199. if (ftrace->graph_depth < 0) {
  200. pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
  201. return -1;
  202. }
  203. snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
  204. if (write_tracing_file("max_graph_depth", buf) < 0)
  205. return -1;
  206. return 0;
  207. }
  208. static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
  209. {
  210. char *trace_file;
  211. int trace_fd;
  212. char buf[4096];
  213. struct pollfd pollfd = {
  214. .events = POLLIN,
  215. };
  216. if (geteuid() != 0) {
  217. pr_err("ftrace only works for root!\n");
  218. return -1;
  219. }
  220. signal(SIGINT, sig_handler);
  221. signal(SIGUSR1, sig_handler);
  222. signal(SIGCHLD, sig_handler);
  223. signal(SIGPIPE, sig_handler);
  224. if (reset_tracing_files(ftrace) < 0)
  225. goto out;
  226. /* reset ftrace buffer */
  227. if (write_tracing_file("trace", "0") < 0)
  228. goto out;
  229. if (argc && perf_evlist__prepare_workload(ftrace->evlist,
  230. &ftrace->target, argv, false,
  231. ftrace__workload_exec_failed_signal) < 0) {
  232. goto out;
  233. }
  234. if (set_tracing_pid(ftrace) < 0) {
  235. pr_err("failed to set ftrace pid\n");
  236. goto out_reset;
  237. }
  238. if (set_tracing_cpu(ftrace) < 0) {
  239. pr_err("failed to set tracing cpumask\n");
  240. goto out_reset;
  241. }
  242. if (set_tracing_filters(ftrace) < 0) {
  243. pr_err("failed to set tracing filters\n");
  244. goto out_reset;
  245. }
  246. if (set_tracing_depth(ftrace) < 0) {
  247. pr_err("failed to set graph depth\n");
  248. goto out_reset;
  249. }
  250. if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
  251. pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
  252. goto out_reset;
  253. }
  254. setup_pager();
  255. trace_file = get_tracing_file("trace_pipe");
  256. if (!trace_file) {
  257. pr_err("failed to open trace_pipe\n");
  258. goto out_reset;
  259. }
  260. trace_fd = open(trace_file, O_RDONLY);
  261. put_tracing_file(trace_file);
  262. if (trace_fd < 0) {
  263. pr_err("failed to open trace_pipe\n");
  264. goto out_reset;
  265. }
  266. fcntl(trace_fd, F_SETFL, O_NONBLOCK);
  267. pollfd.fd = trace_fd;
  268. if (write_tracing_file("tracing_on", "1") < 0) {
  269. pr_err("can't enable tracing\n");
  270. goto out_close_fd;
  271. }
  272. perf_evlist__start_workload(ftrace->evlist);
  273. while (!done) {
  274. if (poll(&pollfd, 1, -1) < 0)
  275. break;
  276. if (pollfd.revents & POLLIN) {
  277. int n = read(trace_fd, buf, sizeof(buf));
  278. if (n < 0)
  279. break;
  280. if (fwrite(buf, n, 1, stdout) != 1)
  281. break;
  282. }
  283. }
  284. write_tracing_file("tracing_on", "0");
  285. /* read remaining buffer contents */
  286. while (true) {
  287. int n = read(trace_fd, buf, sizeof(buf));
  288. if (n <= 0)
  289. break;
  290. if (fwrite(buf, n, 1, stdout) != 1)
  291. break;
  292. }
  293. out_close_fd:
  294. close(trace_fd);
  295. out_reset:
  296. reset_tracing_files(ftrace);
  297. out:
  298. return done ? 0 : -1;
  299. }
  300. static int perf_ftrace_config(const char *var, const char *value, void *cb)
  301. {
  302. struct perf_ftrace *ftrace = cb;
  303. if (prefixcmp(var, "ftrace."))
  304. return 0;
  305. if (strcmp(var, "ftrace.tracer"))
  306. return -1;
  307. if (!strcmp(value, "function_graph") ||
  308. !strcmp(value, "function")) {
  309. ftrace->tracer = value;
  310. return 0;
  311. }
  312. pr_err("Please select \"function_graph\" (default) or \"function\"\n");
  313. return -1;
  314. }
  315. static int parse_filter_func(const struct option *opt, const char *str,
  316. int unset __maybe_unused)
  317. {
  318. struct list_head *head = opt->value;
  319. struct filter_entry *entry;
  320. entry = malloc(sizeof(*entry) + strlen(str) + 1);
  321. if (entry == NULL)
  322. return -ENOMEM;
  323. strcpy(entry->name, str);
  324. list_add_tail(&entry->list, head);
  325. return 0;
  326. }
  327. static void delete_filter_func(struct list_head *head)
  328. {
  329. struct filter_entry *pos, *tmp;
  330. list_for_each_entry_safe(pos, tmp, head, list) {
  331. list_del(&pos->list);
  332. free(pos);
  333. }
  334. }
  335. int cmd_ftrace(int argc, const char **argv)
  336. {
  337. int ret;
  338. struct perf_ftrace ftrace = {
  339. .tracer = DEFAULT_TRACER,
  340. .target = { .uid = UINT_MAX, },
  341. };
  342. const char * const ftrace_usage[] = {
  343. "perf ftrace [<options>] [<command>]",
  344. "perf ftrace [<options>] -- <command> [<options>]",
  345. NULL
  346. };
  347. const struct option ftrace_options[] = {
  348. OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
  349. "tracer to use: function_graph(default) or function"),
  350. OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
  351. "trace on existing process id"),
  352. OPT_INCR('v', "verbose", &verbose,
  353. "be more verbose"),
  354. OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
  355. "system-wide collection from all CPUs"),
  356. OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
  357. "list of cpus to monitor"),
  358. OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
  359. "trace given functions only", parse_filter_func),
  360. OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
  361. "do not trace given functions", parse_filter_func),
  362. OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
  363. "Set graph filter on given functions", parse_filter_func),
  364. OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
  365. "Set nograph filter on given functions", parse_filter_func),
  366. OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
  367. "Max depth for function graph tracer"),
  368. OPT_END()
  369. };
  370. INIT_LIST_HEAD(&ftrace.filters);
  371. INIT_LIST_HEAD(&ftrace.notrace);
  372. INIT_LIST_HEAD(&ftrace.graph_funcs);
  373. INIT_LIST_HEAD(&ftrace.nograph_funcs);
  374. ret = perf_config(perf_ftrace_config, &ftrace);
  375. if (ret < 0)
  376. return -1;
  377. argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
  378. PARSE_OPT_STOP_AT_NON_OPTION);
  379. if (!argc && target__none(&ftrace.target))
  380. usage_with_options(ftrace_usage, ftrace_options);
  381. ret = target__validate(&ftrace.target);
  382. if (ret) {
  383. char errbuf[512];
  384. target__strerror(&ftrace.target, ret, errbuf, 512);
  385. pr_err("%s\n", errbuf);
  386. goto out_delete_filters;
  387. }
  388. ftrace.evlist = perf_evlist__new();
  389. if (ftrace.evlist == NULL) {
  390. ret = -ENOMEM;
  391. goto out_delete_filters;
  392. }
  393. ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
  394. if (ret < 0)
  395. goto out_delete_evlist;
  396. ret = __cmd_ftrace(&ftrace, argc, argv);
  397. out_delete_evlist:
  398. perf_evlist__delete(ftrace.evlist);
  399. out_delete_filters:
  400. delete_filter_func(&ftrace.filters);
  401. delete_filter_func(&ftrace.notrace);
  402. delete_filter_func(&ftrace.graph_funcs);
  403. delete_filter_func(&ftrace.nograph_funcs);
  404. return ret;
  405. }