ftrace.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. void trace_print_events(FILE *stream, fprintf_function stream_printf)
  39. {
  40. TraceEventID i;
  41. for (i = 0; i < trace_event_count(); i++) {
  42. TraceEvent *ev = trace_event_id(i);
  43. stream_printf(stream, "%s [Event ID %u] : state %u\n",
  44. trace_event_get_name(ev), i, trace_event_get_state_dynamic(ev));
  45. }
  46. }
  47. void trace_event_set_state_dynamic_backend(TraceEvent *ev, bool state)
  48. {
  49. ev->dstate = state;
  50. }
  51. bool trace_backend_init(const char *events, const char *file)
  52. {
  53. char debugfs[PATH_MAX];
  54. char path[PATH_MAX];
  55. int debugfs_found;
  56. int trace_fd = -1;
  57. if (file) {
  58. fprintf(stderr, "error: -trace file=...: "
  59. "option not supported by the selected tracing backend\n");
  60. return false;
  61. }
  62. debugfs_found = find_debugfs(debugfs);
  63. if (debugfs_found) {
  64. snprintf(path, PATH_MAX, "%s/tracing/tracing_on", debugfs);
  65. trace_fd = open(path, O_WRONLY);
  66. if (trace_fd < 0) {
  67. perror("Could not open ftrace 'tracing_on' file");
  68. return false;
  69. } else {
  70. if (write(trace_fd, "1", 1) < 0) {
  71. perror("Could not write to 'tracing_on' file");
  72. close(trace_fd);
  73. return false;
  74. }
  75. close(trace_fd);
  76. }
  77. snprintf(path, PATH_MAX, "%s/tracing/trace_marker", debugfs);
  78. trace_marker_fd = open(path, O_WRONLY);
  79. if (trace_marker_fd < 0) {
  80. perror("Could not open ftrace 'trace_marker' file");
  81. return false;
  82. }
  83. } else {
  84. fprintf(stderr, "debugfs is not mounted\n");
  85. return false;
  86. }
  87. trace_backend_init_events(events);
  88. return true;
  89. }