builtin-ftrace.c 11 KB

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