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_TRACING_TEST_FAKE_PACKET_H_ 18 #define SRC_TRACING_TEST_FAKE_PACKET_H_ 19 20 #include <stdint.h> 21 22 #include <array> 23 #include <initializer_list> 24 #include <string> 25 #include <vector> 26 27 #include "perfetto/ext/base/utils.h" 28 #include "perfetto/ext/tracing/core/basic_types.h" 29 30 namespace perfetto { 31 32 class TraceBuffer; 33 34 class FakePacketFragment { 35 public: 36 // Generates a packet of given |size| with pseudo random payload. The |size| 37 // argument includes the size of the varint header. 38 FakePacketFragment(size_t size, char prefix); 39 FakePacketFragment(const void* payload, size_t payload_size); 40 void CopyInto(std::vector<uint8_t>* data) const; 41 size_t GetSizeHeader() const; 42 bool operator==(const FakePacketFragment& other) const; payload()43 const std::string& payload() const { return payload_; } 44 45 private: 46 std::array<uint8_t, 2> header_{}; 47 size_t header_size_ = 0; 48 std::string payload_; 49 }; 50 51 std::ostream& operator<<(std::ostream&, const FakePacketFragment&); 52 53 class FakeChunk { 54 public: 55 FakeChunk(TraceBuffer* t, ProducerID p, WriterID w, ChunkID c); 56 57 // Appends a packet of exactly |size| bytes (including the varint header 58 // that states the size of the packet itself. The payload of the packet is 59 // NOT a valid proto and is just filled with pseudo-random bytes generated 60 // from |seed|. 61 FakeChunk& AddPacket(size_t size, char seed, uint8_t packet_flag = 0); 62 63 // Appends a packet with the given raw content (including varint header). 64 FakeChunk& AddPacket(std::initializer_list<uint8_t>); 65 66 // Increments the number of packets in the chunk without adding new data. 67 FakeChunk& IncrementNumPackets(); 68 69 FakeChunk& SetFlags(uint8_t flags_to_set); 70 71 FakeChunk& ClearBytes(size_t offset, size_t len); 72 73 FakeChunk& SetUID(uid_t); 74 75 // Extends the size of the FakeChunk to |chunk_size| by 0-filling. 76 FakeChunk& PadTo(size_t chunk_size); 77 78 // Returns the full size of the chunk including the ChunkRecord header. 79 size_t CopyIntoTraceBuffer(bool chunk_complete = true); 80 81 private: 82 TraceBuffer* trace_buffer_; 83 ProducerID producer_id; 84 WriterID writer_id; 85 ChunkID chunk_id; 86 uint8_t flags = 0; 87 uint16_t num_packets = 0; 88 uid_t uid = base::kInvalidUid; 89 pid_t pid = base::kInvalidPid; 90 91 std::vector<uint8_t> data; 92 }; 93 94 } // namespace perfetto 95 96 #endif // SRC_TRACING_TEST_FAKE_PACKET_H_ 97