1 /* 2 * Copyright (C) 2018 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 SRC_TRACECONV_UTILS_H_ 18 #define SRC_TRACECONV_UTILS_H_ 19 20 #include <stdio.h> 21 22 #include <functional> 23 #include <iostream> 24 #include <memory> 25 #include <optional> 26 #include <vector> 27 28 #include "perfetto/base/build_config.h" 29 #include "perfetto/ext/base/paged_memory.h" 30 #include "src/profiling/deobfuscator.h" 31 32 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 33 #include <zlib.h> 34 #endif 35 36 namespace perfetto { 37 38 namespace trace_processor { 39 class TraceProcessor; 40 } 41 42 namespace protos { 43 class TracePacket; 44 } 45 46 namespace trace_to_text { 47 48 // When running in Web Assembly, fflush() is a no-op and the stdio buffering 49 // sends progress updates to JS only when a write ends with \n. 50 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM) 51 constexpr char kProgressChar = '\n'; 52 #else 53 constexpr char kProgressChar = '\r'; 54 #endif 55 56 bool ReadTraceUnfinalized(trace_processor::TraceProcessor* tp, 57 std::istream* input); 58 void IngestTraceOrDie(trace_processor::TraceProcessor* tp, 59 const std::string& trace_proto); 60 61 class TraceWriter { 62 public: 63 TraceWriter(std::ostream* output); 64 virtual ~TraceWriter(); 65 66 void Write(const std::string& s); 67 virtual void Write(const char* data, size_t sz); 68 69 private: 70 std::ostream* output_; 71 }; 72 73 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 74 class DeflateTraceWriter : public TraceWriter { 75 public: 76 DeflateTraceWriter(std::ostream* output); 77 ~DeflateTraceWriter() override; 78 79 void Write(const char* data, size_t sz) override; 80 81 private: 82 void Flush(); 83 void CheckEq(int actual_code, int expected_code); 84 85 z_stream stream_{}; 86 base::PagedMemory buf_; 87 uint8_t* const start_; 88 uint8_t* const end_; 89 }; 90 91 #else 92 93 // Fallback implementation. Will print an error and write uncompressed. 94 class DeflateTraceWriter : public TraceWriter { 95 public: 96 DeflateTraceWriter(std::ostream* output); 97 ~DeflateTraceWriter() override; 98 }; 99 100 #endif // PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 101 102 } // namespace trace_to_text 103 } // namespace perfetto 104 105 #endif // SRC_TRACECONV_UTILS_H_ 106