blob: db33a2c7b69e8a08a0f97c0bdb44e14cb850f036 [file] [log] [blame]
Hector Dearman554627f2019-06-04 17:58:22 +01001/*
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#include "src/perfetto_cmd/packet_writer.h"
18
19#include <array>
20
21#include <fcntl.h>
Hector Dearman554627f2019-06-04 17:58:22 +010022#include <signal.h>
23#include <stdio.h>
24#include <sys/stat.h>
Hector Dearman554627f2019-06-04 17:58:22 +010025
Primiano Tucci1b5fdae2020-01-16 09:28:05 +000026#include "perfetto/base/build_config.h"
Primiano Tucciccaf6db2021-01-05 22:32:26 +010027#include "perfetto/ext/base/getopt.h"
Hector Dearman554627f2019-06-04 17:58:22 +010028#include "perfetto/ext/base/paged_memory.h"
Primiano Tuccib6a89252019-06-05 09:21:23 +010029#include "perfetto/ext/base/utils.h"
Hector Dearman554627f2019-06-04 17:58:22 +010030#include "perfetto/ext/tracing/core/trace_packet.h"
31#include "perfetto/protozero/proto_utils.h"
Daniele Di Proiettoe41a6902023-04-26 12:13:58 +000032#include "protos/perfetto/trace/trace.pbzero.h"
Primiano Tucci1b5fdae2020-01-16 09:28:05 +000033
Hector Dearman554627f2019-06-04 17:58:22 +010034namespace perfetto {
35namespace {
36
Hector Dearman554627f2019-06-04 17:58:22 +010037using protozero::proto_utils::MakeTagLengthDelimited;
Hector Dearman554627f2019-06-04 17:58:22 +010038using protozero::proto_utils::WriteVarInt;
39using Preamble = std::array<char, 16>;
40
Hector Dearman554627f2019-06-04 17:58:22 +010041template <uint32_t id>
42size_t GetPreamble(size_t sz, Preamble* preamble) {
43 uint8_t* ptr = reinterpret_cast<uint8_t*>(preamble->data());
44 constexpr uint32_t tag = MakeTagLengthDelimited(id);
45 ptr = WriteVarInt(tag, ptr);
46 ptr = WriteVarInt(sz, ptr);
47 size_t preamble_size = reinterpret_cast<uintptr_t>(ptr) -
48 reinterpret_cast<uintptr_t>(preamble->data());
49 PERFETTO_DCHECK(preamble_size < preamble->size());
50 return preamble_size;
51}
52
Daniele Di Proiettoe41a6902023-04-26 12:13:58 +000053} // namespace
Hector Dearman554627f2019-06-04 17:58:22 +010054
Daniele Di Proiettoe41a6902023-04-26 12:13:58 +000055PacketWriter::~PacketWriter() {
Primiano Tucci1b5fdae2020-01-16 09:28:05 +000056 fflush(fd_);
57}
58
Daniele Di Proiettoe41a6902023-04-26 12:13:58 +000059bool PacketWriter::WritePacket(const TracePacket& packet) {
Hector Dearmanaf7d66b2020-02-13 17:01:49 +000060 Preamble preamble;
Daniele Di Proiettoe41a6902023-04-26 12:13:58 +000061 size_t size = GetPreamble<protos::pbzero::Trace::kPacketFieldNumber>(
62 packet.size(), &preamble);
Hector Dearmanaf7d66b2020-02-13 17:01:49 +000063 if (fwrite(preamble.data(), 1, size, fd_) != size)
64 return false;
65 for (const Slice& slice : packet.slices()) {
66 if (fwrite(reinterpret_cast<const char*>(slice.start), 1, slice.size,
67 fd_) != slice.size) {
Primiano Tucci1b5fdae2020-01-16 09:28:05 +000068 return false;
Primiano Tucci1b5fdae2020-01-16 09:28:05 +000069 }
70 }
71
72 return true;
73}
74
Hector Dearman554627f2019-06-04 17:58:22 +010075} // namespace perfetto