trace-event.c 2.0 KB

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