trace-event.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <linux/kernel.h>
  10. #include <linux/err.h>
  11. #include <traceevent/event-parse.h>
  12. #include <api/fs/tracing_path.h>
  13. #include <api/fs/fs.h>
  14. #include "trace-event.h"
  15. #include "machine.h"
  16. #include "util.h"
  17. /*
  18. * global trace_event object used by trace_event__tp_format
  19. *
  20. * TODO There's no cleanup call for this. Add some sort of
  21. * __exit function support and call trace_event__cleanup
  22. * there.
  23. */
  24. static struct trace_event tevent;
  25. static bool tevent_initialized;
  26. int trace_event__init(struct trace_event *t)
  27. {
  28. struct tep_handle *pevent = tep_alloc();
  29. if (pevent) {
  30. t->plugin_list = tep_load_plugins(pevent);
  31. t->pevent = pevent;
  32. }
  33. return pevent ? 0 : -1;
  34. }
  35. static int trace_event__init2(void)
  36. {
  37. int be = tep_host_bigendian();
  38. struct tep_handle *pevent;
  39. if (trace_event__init(&tevent))
  40. return -1;
  41. pevent = tevent.pevent;
  42. tep_set_flag(pevent, TEP_NSEC_OUTPUT);
  43. tep_set_file_bigendian(pevent, be);
  44. tep_set_host_bigendian(pevent, be);
  45. tevent_initialized = true;
  46. return 0;
  47. }
  48. int trace_event__register_resolver(struct machine *machine,
  49. tep_func_resolver_t *func)
  50. {
  51. if (!tevent_initialized && trace_event__init2())
  52. return -1;
  53. return tep_set_function_resolver(tevent.pevent, func, machine);
  54. }
  55. void trace_event__cleanup(struct trace_event *t)
  56. {
  57. tep_unload_plugins(t->plugin_list, t->pevent);
  58. tep_free(t->pevent);
  59. }
  60. /*
  61. * Returns pointer with encoded error via <linux/err.h> interface.
  62. */
  63. static struct event_format*
  64. tp_format(const char *sys, const char *name)
  65. {
  66. char *tp_dir = get_events_file(sys);
  67. struct tep_handle *pevent = tevent.pevent;
  68. struct event_format *event = NULL;
  69. char path[PATH_MAX];
  70. size_t size;
  71. char *data;
  72. int err;
  73. if (!tp_dir)
  74. return ERR_PTR(-errno);
  75. scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
  76. put_events_file(tp_dir);
  77. err = filename__read_str(path, &data, &size);
  78. if (err)
  79. return ERR_PTR(err);
  80. tep_parse_format(pevent, &event, data, size, sys);
  81. free(data);
  82. return event;
  83. }
  84. /*
  85. * Returns pointer with encoded error via <linux/err.h> interface.
  86. */
  87. struct event_format*
  88. trace_event__tp_format(const char *sys, const char *name)
  89. {
  90. if (!tevent_initialized && trace_event__init2())
  91. return ERR_PTR(-ENOMEM);
  92. return tp_format(sys, name);
  93. }
  94. struct event_format *trace_event__tp_format_id(int id)
  95. {
  96. if (!tevent_initialized && trace_event__init2())
  97. return ERR_PTR(-ENOMEM);
  98. return tep_find_event(tevent.pevent, id);
  99. }