1 /* 2 * Copyright (C) 2019 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_TRACE_PROCESSOR_JSON_TRACE_TOKENIZER_H_ 18 #define SRC_TRACE_PROCESSOR_JSON_TRACE_TOKENIZER_H_ 19 20 #include <stdint.h> 21 22 #include "src/trace_processor/chunked_trace_reader.h" 23 #include "src/trace_processor/trace_sorter.h" 24 #include "src/trace_processor/trace_storage.h" 25 26 namespace Json { 27 class Value; 28 } 29 30 namespace perfetto { 31 namespace trace_processor { 32 33 class TraceProcessorContext; 34 35 // Visible for testing. 36 enum ReadDictRes { kFoundDict, kNeedsMoreData, kEndOfTrace, kFatalError }; 37 38 // Visible for testing. 39 ReadDictRes ReadOneJsonDict(const char* start, 40 const char* end, 41 Json::Value* value, 42 const char** next); 43 44 // Reads a JSON trace in chunks and extracts top level json objects. 45 class JsonTraceTokenizer : public ChunkedTraceReader { 46 public: 47 explicit JsonTraceTokenizer(TraceProcessorContext*); 48 ~JsonTraceTokenizer() override; 49 50 // ChunkedTraceReader implementation. 51 bool Parse(std::unique_ptr<uint8_t[]>, size_t) override; 52 53 private: 54 TraceProcessorContext* const context_; 55 56 uint64_t offset_ = 0; 57 // Used to glue together JSON objects that span across two (or more) 58 // Parse boundaries. 59 std::vector<char> buffer_; 60 }; 61 62 } // namespace trace_processor 63 } // namespace perfetto 64 65 #endif // SRC_TRACE_PROCESSOR_JSON_TRACE_TOKENIZER_H_ 66