trace-event.c 2.5 KB

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