trace-event.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <linux/kernel.h>
  9. #include <traceevent/event-parse.h>
  10. #include <api/fs/tracing_path.h>
  11. #include "trace-event.h"
  12. #include "machine.h"
  13. #include "util.h"
  14. /*
  15. * global trace_event object used by trace_event__tp_format
  16. *
  17. * TODO There's no cleanup call for this. Add some sort of
  18. * __exit function support and call trace_event__cleanup
  19. * there.
  20. */
  21. static struct trace_event tevent;
  22. static bool tevent_initialized;
  23. int trace_event__init(struct trace_event *t)
  24. {
  25. struct pevent *pevent = pevent_alloc();
  26. if (pevent) {
  27. t->plugin_list = traceevent_load_plugins(pevent);
  28. t->pevent = pevent;
  29. }
  30. return pevent ? 0 : -1;
  31. }
  32. static int trace_event__init2(void)
  33. {
  34. int be = traceevent_host_bigendian();
  35. struct pevent *pevent;
  36. if (trace_event__init(&tevent))
  37. return -1;
  38. pevent = tevent.pevent;
  39. pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
  40. pevent_set_file_bigendian(pevent, be);
  41. pevent_set_host_bigendian(pevent, be);
  42. tevent_initialized = true;
  43. return 0;
  44. }
  45. int trace_event__register_resolver(struct machine *machine,
  46. pevent_func_resolver_t *func)
  47. {
  48. if (!tevent_initialized && trace_event__init2())
  49. return -1;
  50. return pevent_set_function_resolver(tevent.pevent, func, machine);
  51. }
  52. void trace_event__cleanup(struct trace_event *t)
  53. {
  54. traceevent_unload_plugins(t->plugin_list, t->pevent);
  55. pevent_free(t->pevent);
  56. }
  57. static struct event_format*
  58. tp_format(const char *sys, const char *name)
  59. {
  60. struct pevent *pevent = tevent.pevent;
  61. struct event_format *event = NULL;
  62. char path[PATH_MAX];
  63. size_t size;
  64. char *data;
  65. scnprintf(path, PATH_MAX, "%s/%s/%s/format",
  66. tracing_events_path, sys, name);
  67. if (filename__read_str(path, &data, &size))
  68. return NULL;
  69. pevent_parse_format(pevent, &event, data, size, sys);
  70. free(data);
  71. return event;
  72. }
  73. struct event_format*
  74. trace_event__tp_format(const char *sys, const char *name)
  75. {
  76. if (!tevent_initialized && trace_event__init2())
  77. return NULL;
  78. return tp_format(sys, name);
  79. }