trace-event.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /*
  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 tep_handle *pevent = tep_alloc();
  28. if (pevent) {
  29. t->plugin_list = tep_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 = tep_is_bigendian();
  37. struct tep_handle *pevent;
  38. if (trace_event__init(&tevent))
  39. return -1;
  40. pevent = tevent.pevent;
  41. tep_set_flag(pevent, TEP_NSEC_OUTPUT);
  42. tep_set_file_bigendian(pevent, be);
  43. tep_set_local_bigendian(pevent, be);
  44. tevent_initialized = true;
  45. return 0;
  46. }
  47. int trace_event__register_resolver(struct machine *machine,
  48. tep_func_resolver_t *func)
  49. {
  50. if (!tevent_initialized && trace_event__init2())
  51. return -1;
  52. return tep_set_function_resolver(tevent.pevent, func, machine);
  53. }
  54. void trace_event__cleanup(struct trace_event *t)
  55. {
  56. tep_unload_plugins(t->plugin_list, t->pevent);
  57. tep_free(t->pevent);
  58. }
  59. /*
  60. * Returns pointer with encoded error via <linux/err.h> interface.
  61. */
  62. static struct tep_event*
  63. tp_format(const char *sys, const char *name)
  64. {
  65. char *tp_dir = get_events_file(sys);
  66. struct tep_handle *pevent = tevent.pevent;
  67. struct tep_event *event = NULL;
  68. char path[PATH_MAX];
  69. size_t size;
  70. char *data;
  71. int err;
  72. if (!tp_dir)
  73. return ERR_PTR(-errno);
  74. scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
  75. put_events_file(tp_dir);
  76. err = filename__read_str(path, &data, &size);
  77. if (err)
  78. return ERR_PTR(err);
  79. tep_parse_format(pevent, &event, data, size, sys);
  80. free(data);
  81. return event;
  82. }
  83. /*
  84. * Returns pointer with encoded error via <linux/err.h> interface.
  85. */
  86. struct tep_event*
  87. trace_event__tp_format(const char *sys, const char *name)
  88. {
  89. if (!tevent_initialized && trace_event__init2())
  90. return ERR_PTR(-ENOMEM);
  91. return tp_format(sys, name);
  92. }
  93. struct tep_event *trace_event__tp_format_id(int id)
  94. {
  95. if (!tevent_initialized && trace_event__init2())
  96. return ERR_PTR(-ENOMEM);
  97. return tep_find_event(tevent.pevent, id);
  98. }