ftrace.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "qemu/osdep.h"
  12. #include "trace/control.h"
  13. #include "trace/ftrace.h"
  14. int trace_marker_fd;
  15. static int find_debugfs(char *debugfs)
  16. {
  17. char type[100];
  18. FILE *fp;
  19. fp = fopen("/proc/mounts", "r");
  20. if (fp == NULL) {
  21. return 0;
  22. }
  23. while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  24. debugfs, type) == 2) {
  25. if (strcmp(type, "debugfs") == 0) {
  26. break;
  27. }
  28. }
  29. fclose(fp);
  30. if (strcmp(type, "debugfs") != 0) {
  31. return 0;
  32. }
  33. return 1;
  34. }
  35. bool ftrace_init(void)
  36. {
  37. char debugfs[PATH_MAX];
  38. char path[PATH_MAX];
  39. int debugfs_found;
  40. int trace_fd = -1;
  41. debugfs_found = find_debugfs(debugfs);
  42. if (debugfs_found) {
  43. snprintf(path, PATH_MAX, "%s/tracing/tracing_on", debugfs);
  44. trace_fd = open(path, O_WRONLY);
  45. if (trace_fd < 0) {
  46. if (errno == EACCES) {
  47. trace_marker_fd = open("/dev/null", O_WRONLY);
  48. if (trace_marker_fd != -1) {
  49. return true;
  50. }
  51. }
  52. perror("Could not open ftrace 'tracing_on' file");
  53. return false;
  54. } else {
  55. if (write(trace_fd, "1", 1) < 0) {
  56. perror("Could not write to 'tracing_on' file");
  57. close(trace_fd);
  58. return false;
  59. }
  60. close(trace_fd);
  61. }
  62. snprintf(path, PATH_MAX, "%s/tracing/trace_marker", debugfs);
  63. trace_marker_fd = open(path, O_WRONLY);
  64. if (trace_marker_fd < 0) {
  65. perror("Could not open ftrace 'trace_marker' file");
  66. return false;
  67. }
  68. } else {
  69. fprintf(stderr, "debugfs is not mounted\n");
  70. return false;
  71. }
  72. return true;
  73. }