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 bool ReadTrace(trace_processor::TraceProcessor* tp, std::istream* input); 57 void IngestTraceOrDie(trace_processor::TraceProcessor* tp, 58 const std::string& trace_proto); 59 60 class TraceWriter { 61 public: 62 TraceWriter(std::ostream* output); 63 virtual ~TraceWriter(); 64 65 void Write(const std::string& s); 66 virtual void Write(const char* data, size_t sz); 67 68 private: 69 std::ostream* output_; 70 }; 71 72 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 73 class DeflateTraceWriter : public TraceWriter { 74 public: 75 DeflateTraceWriter(std::ostream* output); 76 ~DeflateTraceWriter() override; 77 78 void Write(const char* data, size_t sz) override; 79 80 private: 81 void Flush(); 82 void CheckEq(int actual_code, int expected_code); 83 84 z_stream stream_{}; 85 base::PagedMemory buf_; 86 uint8_t* const start_; 87 uint8_t* const end_; 88 }; 89 90 #else 91 92 // Fallback implementation. Will print an error and write uncompressed. 93 class DeflateTraceWriter : public TraceWriter { 94 public: 95 DeflateTraceWriter(std::ostream* output); 96 ~DeflateTraceWriter() override; 97 }; 98 99 #endif // PERFETTO_BUILDFLAG(PERFETTO_ZLIB) 100 101 } // namespace trace_to_text 102 } // namespace perfetto 103 104 #endif // TOOLS_TRACE_TO_TEXT_UTILS_H_ 105