| 1 | /* |
| 2 | * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_TSDL_TRACE_CLASS_VISITOR_H |
| 9 | #define LTTNG_TSDL_TRACE_CLASS_VISITOR_H |
| 10 | |
| 11 | #include "event-class.hpp" |
| 12 | #include "stream-class.hpp" |
| 13 | #include "trace-class.hpp" |
| 14 | |
| 15 | #include <vendor/optional.hpp> |
| 16 | |
| 17 | #include <functional> |
| 18 | #include <unordered_map> |
| 19 | |
| 20 | namespace lttng { |
| 21 | namespace sessiond { |
| 22 | namespace tsdl { |
| 23 | |
| 24 | using append_metadata_fragment_function = std::function<void(const std::string& fragment)>; |
| 25 | |
| 26 | namespace details { |
| 27 | /* |
| 28 | * Register types to be overriden. For example, a TSDL-safe copy of a type can |
| 29 | * be added to be overriden whenever the original type is encountered. |
| 30 | * |
| 31 | * Note that this class assumes no ownership of the original types. It assumes |
| 32 | * that the original types live as long as the original trace. |
| 33 | */ |
| 34 | class type_overrider { |
| 35 | public: |
| 36 | type_overrider() = default; |
| 37 | |
| 38 | void publish(const lttng::sessiond::trace::type& original, |
| 39 | lttng::sessiond::trace::type::cuptr new_type_override); |
| 40 | const lttng::sessiond::trace::type& |
| 41 | type(const lttng::sessiond::trace::type& original) const noexcept; |
| 42 | |
| 43 | private: |
| 44 | std::unordered_map<const lttng::sessiond::trace::type *, lttng::sessiond::trace::type::cuptr> |
| 45 | _overriden_types; |
| 46 | }; |
| 47 | } /* namespace details. */ |
| 48 | |
| 49 | /* |
| 50 | * TSDL-producing trace class visitor. |
| 51 | * |
| 52 | * An instance of this class must not be used on multiple trace class instances. |
| 53 | * The `append_metadata` callback is automatically invoked when a coherent |
| 54 | * fragment of TSDL is available. |
| 55 | */ |
| 56 | class trace_class_visitor : public lttng::sessiond::trace::trace_class_visitor { |
| 57 | public: |
| 58 | trace_class_visitor(const lttng::sessiond::trace::abi& trace_abi, |
| 59 | append_metadata_fragment_function append_metadata); |
| 60 | |
| 61 | void visit(const lttng::sessiond::trace::trace_class& trace_class) final; |
| 62 | void visit(const lttng::sessiond::trace::clock_class& clock_class) final; |
| 63 | void visit(const lttng::sessiond::trace::stream_class& stream_class) final; |
| 64 | void visit(const lttng::sessiond::trace::event_class& event_class) final; |
| 65 | |
| 66 | private: |
| 67 | /* Coherent (parseable) fragments must be appended. */ |
| 68 | void append_metadata_fragment(const std::string& fragment) const; |
| 69 | const lttng::sessiond::trace::type& |
| 70 | _lookup_field_type(const lttng::sessiond::trace::field_location& field_location) const; |
| 71 | |
| 72 | const lttng::sessiond::trace::abi& _trace_abi; |
| 73 | const append_metadata_fragment_function _append_metadata_fragment; |
| 74 | details::type_overrider _sanitized_types_overrides; |
| 75 | |
| 76 | /* |
| 77 | * Current visit context. |
| 78 | * |
| 79 | * The members of a trace class hierarchy do not provide back-references |
| 80 | * up the hierarchy (e.g. stream class to its parent trace class). |
| 81 | * |
| 82 | * This context allows the visitor to evaluate a "field location". |
| 83 | * |
| 84 | * _current_trace_class is set the first time a trace class is visited and |
| 85 | * remains valid until the destruction of this object. |
| 86 | * |
| 87 | * _current_stream_class and _current_event_class are set only in the |
| 88 | * context of the visit of a stream class and of its event class(es). |
| 89 | */ |
| 90 | const lttng::sessiond::trace::trace_class *_current_trace_class = nullptr; |
| 91 | const lttng::sessiond::trace::stream_class *_current_stream_class = nullptr; |
| 92 | const lttng::sessiond::trace::event_class *_current_event_class = nullptr; |
| 93 | }; |
| 94 | |
| 95 | } /* namespace tsdl */ |
| 96 | } /* namespace sessiond */ |
| 97 | } /* namespace lttng */ |
| 98 | |
| 99 | #endif /* LTTNG_TSDL_TRACE_CLASS_VISITOR_H */ |