ftrace.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_mount(char *mount_point, const char *fstype)
  16. {
  17. char type[100];
  18. FILE *fp;
  19. int ret = 0;
  20. fp = fopen("/proc/mounts", "r");
  21. if (fp == NULL) {
  22. return 0;
  23. }
  24. while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  25. mount_point, type) == 2) {
  26. if (strcmp(type, fstype) == 0) {
  27. ret = 1;
  28. break;
  29. }
  30. }
  31. fclose(fp);
  32. return ret;
  33. }
  34. bool ftrace_init(void)
  35. {
  36. char mount_point[PATH_MAX];
  37. char path[PATH_MAX];
  38. int tracefs_found;
  39. int trace_fd = -1;
  40. const char *subdir = "";
  41. tracefs_found = find_mount(mount_point, "tracefs");
  42. if (!tracefs_found) {
  43. tracefs_found = find_mount(mount_point, "debugfs");
  44. subdir = "/tracing";
  45. }
  46. if (tracefs_found) {
  47. if (snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir)
  48. >= sizeof(path)) {
  49. fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
  50. return false;
  51. }
  52. trace_fd = open(path, O_WRONLY);
  53. if (trace_fd < 0) {
  54. if (errno == EACCES) {
  55. trace_marker_fd = open("/dev/null", O_WRONLY);
  56. if (trace_marker_fd != -1) {
  57. return true;
  58. }
  59. }
  60. perror("Could not open ftrace 'tracing_on' file");
  61. return false;
  62. } else {
  63. if (write(trace_fd, "1", 1) < 0) {
  64. perror("Could not write to 'tracing_on' file");
  65. close(trace_fd);
  66. return false;
  67. }
  68. close(trace_fd);
  69. }
  70. if (snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir)
  71. >= sizeof(path)) {
  72. fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
  73. return false;
  74. }
  75. trace_marker_fd = open(path, O_WRONLY);
  76. if (trace_marker_fd < 0) {
  77. perror("Could not open ftrace 'trace_marker' file");
  78. return false;
  79. }
  80. } else {
  81. fprintf(stderr, "tracefs is not mounted\n");
  82. return false;
  83. }
  84. return true;
  85. }