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 TOOLS_TRACE_TO_TEXT_UTILS_H_ 18 #define TOOLS_TRACE_TO_TEXT_UTILS_H_ 19 20 #include <stdio.h> 21 22 #include <functional> 23 #include <iostream> 24 #include <memory> 25 #include <vector> 26 27 #include "perfetto/base/build_config.h" 28 #include "perfetto/ext/base/optional.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 void ForEachPacketBlobInTrace( 57 std::istream* input, 58 const std::function<void(std::unique_ptr<char[]>, size_t)>&); 59 60 61 bool ReadTrace(trace_processor::TraceProcessor* tp, std::istream* input); 62 void IngestTraceOrDie(trace_processor::TraceProcessor* tp, 63 const std::string& trace_proto); 64 65 class TraceWriter { 66 public: 67 TraceWriter(std::ostream* output); 68 virtual ~TraceWriter(); 69 70 void Write(const std::string& s); 71 virtual void Write(const char* data, size_t sz); 72 73 private: 74 std::ostream* output_; 75 }; 76 77 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 78 class DeflateTraceWriter : public TraceWriter { 79 public: 80 DeflateTraceWriter(std::ostream* output); 81 ~DeflateTraceWriter() override; 82 83 void Write(const char* data, size_t sz) override; 84 85 private: 86 void Flush(); 87 void CheckEq(int actual_code, int expected_code); 88 89 z_stream stream_{}; 90 base::PagedMemory buf_; 91 uint8_t* const start_; 92 uint8_t* const end_; 93 }; 94 95 #else 96 97 // Fallback implementation. Will print an error and write uncompressed. 98 class DeflateTraceWriter : public TraceWriter { 99 public: 100 DeflateTraceWriter(std::ostream* output); 101 ~DeflateTraceWriter() override; 102 }; 103 104 #endif // PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 105 106 } // namespace trace_to_text 107 } // namespace perfetto 108 109 #endif // TOOLS_TRACE_TO_TEXT_UTILS_H_ 110