1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_INTERCEPTOR_TRACE_WRITER_H_ 18 #define INCLUDE_PERFETTO_TRACING_INTERNAL_INTERCEPTOR_TRACE_WRITER_H_ 19 20 #include "perfetto/protozero/scattered_heap_buffer.h" 21 #include "perfetto/tracing/interceptor.h" 22 #include "perfetto/tracing/internal/basic_types.h" 23 #include "perfetto/tracing/trace_writer_base.h" 24 #include "protos/perfetto/trace/trace_packet.pbzero.h" 25 26 namespace perfetto { 27 namespace internal { 28 29 // A heap-backed trace writer used to reroute trace packets to an interceptor. 30 class InterceptorTraceWriter : public TraceWriterBase { 31 public: 32 InterceptorTraceWriter(std::unique_ptr<InterceptorBase::ThreadLocalState> tls, 33 InterceptorBase::TracePacketCallback packet_callback, 34 DataSourceStaticState* static_state, 35 uint32_t instance_index); 36 ~InterceptorTraceWriter() override; 37 38 // TraceWriterBase implementation. 39 protozero::MessageHandle<protos::pbzero::TracePacket> NewTracePacket() 40 override; 41 void FinishTracePacket() override; 42 void Flush(std::function<void()> callback = {}) override; 43 uint64_t written() const override; 44 45 private: 46 std::unique_ptr<InterceptorBase::ThreadLocalState> tls_; 47 InterceptorBase::TracePacketCallback packet_callback_; 48 49 protozero::HeapBuffered<protos::pbzero::TracePacket> cur_packet_; 50 uint64_t bytes_written_ = 0; 51 52 // Static state of the data source we are intercepting. 53 DataSourceStaticState* const static_state_; 54 55 // Index of the data source tracing session which we are intercepting 56 // (0...kMaxDataSourceInstances - 1). Used to look up this interceptor's 57 // session state (i.e., the Interceptor class instance) in the 58 // DataSourceStaticState::instances array. 59 const uint32_t instance_index_; 60 61 const uint32_t sequence_id_; 62 63 static std::atomic<uint32_t> next_sequence_id_; 64 }; 65 66 } // namespace internal 67 } // namespace perfetto 68 69 #endif // INCLUDE_PERFETTO_TRACING_INTERNAL_INTERCEPTOR_TRACE_WRITER_H_ 70