trace-event.c 2.3 KB

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