ftrace.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Ftrace trace backend
  3. *
  4. * Copyright (C) 2013 Hitachi, Ltd.
  5. * Created by Eiichi Tsukata <eiichi.tsukata.xh@hitachi.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2. See
  8. * the COPYING file in the top-level directory.
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <fcntl.h>
  14. #include <limits.h>
  15. #include "trace.h"
  16. #include "trace/control.h"
  17. int trace_marker_fd;
  18. static int find_debugfs(char *debugfs)
  19. {
  20. char type[100];
  21. FILE *fp;
  22. fp = fopen("/proc/mounts", "r");
  23. if (fp == NULL) {
  24. return 0;
  25. }
  26. while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  27. debugfs, type) == 2) {
  28. if (strcmp(type, "debugfs") == 0) {
  29. break;
  30. }
  31. }
  32. fclose(fp);
  33. if (strcmp(type, "debugfs") != 0) {
  34. return 0;
  35. }
  36. return 1;
  37. }
  38. bool ftrace_init(void)
  39. {
  40. char debugfs[PATH_MAX];
  41. char path[PATH_MAX];
  42. int debugfs_found;
  43. int trace_fd = -1;
  44. debugfs_found = find_debugfs(debugfs);
  45. if (debugfs_found) {
  46. snprintf(path, PATH_MAX, "%s/tracing/tracing_on", debugfs);
  47. trace_fd = open(path, O_WRONLY);
  48. if (trace_fd < 0) {
  49. perror("Could not open ftrace 'tracing_on' file");
  50. return false;
  51. } else {
  52. if (write(trace_fd, "1", 1) < 0) {
  53. perror("Could not write to 'tracing_on' file");
  54. close(trace_fd);
  55. return false;
  56. }
  57. close(trace_fd);
  58. }
  59. snprintf(path, PATH_MAX, "%s/tracing/trace_marker", debugfs);
  60. trace_marker_fd = open(path, O_WRONLY);
  61. if (trace_marker_fd < 0) {
  62. perror("Could not open ftrace 'trace_marker' file");
  63. return false;
  64. }
  65. } else {
  66. fprintf(stderr, "debugfs is not mounted\n");
  67. return false;
  68. }
  69. return true;
  70. }