-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTracebuffer.cpp
More file actions
361 lines (323 loc) · 13.4 KB
/
Copy pathTracebuffer.cpp
File metadata and controls
361 lines (323 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "CommonLowLevelTracingKit/decoder/Tracebuffer.hpp"
#include <array>
#include <boost/sort/block_indirect_sort/block_indirect_sort.hpp>
#include <boost/sort/common/range.hpp>
#include <boost/sort/common/util/merge.hpp>
#include <fstream>
#include <optional>
#include <span>
#include <stdint.h>
#include <utility>
#include "CommonLowLevelTracingKit/decoder/Common.hpp"
#include "CommonLowLevelTracingKit/decoder/Tracepoint.hpp"
#include "TracepointInternal.hpp"
#include "archive.hpp"
#include "definition.hpp"
#include "file.hpp"
#include "ringbuffer.hpp"
#include "tracebufferfile.hpp"
using Archive = CommonLowLevelTracingKit::decoder::source::low_level::Archive;
static constexpr std::string_view little_endian_magic{"?#$~tracebuffer", 16};
static constexpr std::string_view big_endian_magic{"cart~$#?\0reffube", 16};
namespace fs = std::filesystem;
using namespace std::string_literals;
using namespace CommonLowLevelTracingKit::decoder;
using namespace CommonLowLevelTracingKit::decoder::exception;
using namespace source;
// Convert internal DefinitionSourceType to public SourceType
static SourceType toPublicSourceType(DefinitionSourceType internal) {
switch (internal) {
case DefinitionSourceType::Unknown: return SourceType::Unknown;
case DefinitionSourceType::Userspace: return SourceType::Userspace;
case DefinitionSourceType::Kernel: return SourceType::Kernel;
case DefinitionSourceType::TTY: return SourceType::TTY;
}
return SourceType::Unknown;
}
// Determine source type from definition (V2) or file extension (V1 fallback)
static SourceType determineSourceType(const fs::path &path, const Definition &def) {
SourceType src = toPublicSourceType(def.sourceType());
if (src == SourceType::Unknown) {
// V1 fallback: use file extension
if (path.extension() == ".clltk_ktrace") {
// Check if it's TTY by name
if (def.name() == "TTY") {
src = SourceType::TTY;
} else {
src = SourceType::Kernel;
}
} else if (path.extension() == ".clltk_trace") {
src = SourceType::Userspace;
}
}
return src;
}
class SyncTbInternal : public SyncTracebuffer {
public:
SyncTbInternal(const fs::path &path)
: SyncTracebuffer(path, determineSourceType(path, TracebufferFile(path).getDefinition()))
, m_tracebuffer_file(path)
, m_file(m_tracebuffer_file.getFilePart())
, m_file_size(m_file.getFileSize()) {
if (size() < 3) CLLTK_DECODER_THROW(InvalidTracebuffer, "ringbuffer too small");
}
~SyncTbInternal() override {}
const std::string_view name() const noexcept override;
size_t size() const noexcept override;
TracepointPtr next(const TracepointFilterFunc &filter) noexcept override;
TracepointPtr next_pooled(TracepointPool &pool,
const TracepointFilterFunc &filter) noexcept override;
uint64_t pending() noexcept override;
uint64_t current_top_entries_nr() const noexcept override;
void skipToEnd() noexcept override;
private:
TracebufferFile m_tracebuffer_file;
const FilePart m_file;
size_t m_file_size;
};
SyncTracebufferPtr SyncTracebuffer::make(const fs::path &path) {
if (is_tracebuffer(path)) try {
return std::make_unique<SyncTbInternal>(path);
} catch (...) {}
return nullptr;
}
const std::string_view SyncTbInternal::name() const noexcept {
return m_tracebuffer_file.getDefinition().name();
}
size_t SyncTbInternal::size() const noexcept {
return m_tracebuffer_file.getRingbuffer().getSize();
}
uint64_t SyncTbInternal::current_top_entries_nr() const noexcept {
return m_tracebuffer_file.getRingbuffer().getEntryCount();
};
TracepointPtr SyncTbInternal::next(const TracepointFilterFunc &filter) noexcept {
while (true) {
auto ringbuffer_entry = m_tracebuffer_file.getRingbuffer().getNextEntry();
if (auto s = std::get_if<std::string>(&ringbuffer_entry))
return ErrorTracepoint::make(name(), *s);
auto &e = std::get<Ringbuffer::EntryPtr>(ringbuffer_entry);
if (e == nullptr) return {};
// the file offset is stored as 6 bytes in the writer's byte order at
// the start of the entry body; the remaining 2 bytes of the uint64
// read belong to the next field and are discarded
const uint64_t fileoffset_raw = get<uint64_t>(e->body(), 0, e->foreignEndian());
const uint64_t fileoffset =
e->foreignEndian() ? (fileoffset_raw >> 16) : (fileoffset_raw & ((1ULL << 48) - 1));
if (fileoffset == 0x01) {
auto tp = make_tracepoint<TracepointDynamic>(std::string(name()), std::move(e),
m_source_type);
if (!filter || filter(*tp)) return tp;
} else if (fileoffset < 0xFF) {
return ErrorTracepoint::make(
name(), "invalid file offset: value is less than minimum valid offset (0xFF)");
} else {
if (fileoffset > m_file_size) m_file_size = m_file.grow();
if ((fileoffset + sizeof(uint32_t)) > m_file_size)
return ErrorTracepoint::make(name(), "file offset bigger than file");
if (m_file.get<char>(fileoffset) != '{')
return ErrorTracepoint::make(
name(), "invalid meta magic at offset " + std::to_string(fileoffset) +
": expected '{', found '" +
std::string(1, m_file.get<char>(fileoffset)) + "'");
const uint32_t meta_size = m_file.get<uint32_t>(fileoffset + 1);
if (meta_size == 0) return ErrorTracepoint::make(name(), "invalid meta size (0)");
if ((fileoffset + meta_size) > m_file_size)
return ErrorTracepoint::make(name(), "meta entry bigger than file end");
const std::span<const uint8_t> meta{&m_file.getReference<const uint8_t>(fileoffset),
meta_size};
auto tp = make_tracepoint<TracepointStatic>(std::string(name()), std::move(e), meta,
m_file.getFileInternal(), m_source_type);
if (!filter || filter(*tp)) return tp;
}
}
}
TracepointPtr SyncTbInternal::next_pooled(TracepointPool &pool,
const TracepointFilterFunc &filter) noexcept {
while (true) {
auto ringbuffer_entry = m_tracebuffer_file.getRingbuffer().getNextEntry();
if (auto s = std::get_if<std::string>(&ringbuffer_entry))
return ErrorTracepoint::make(name(), *s);
auto &e = std::get<Ringbuffer::EntryPtr>(ringbuffer_entry);
if (e == nullptr) return {};
// the file offset is stored as 6 bytes in the writer's byte order at
// the start of the entry body; the remaining 2 bytes of the uint64
// read belong to the next field and are discarded
const uint64_t fileoffset_raw = get<uint64_t>(e->body(), 0, e->foreignEndian());
const uint64_t fileoffset =
e->foreignEndian() ? (fileoffset_raw >> 16) : (fileoffset_raw & ((1ULL << 48) - 1));
if (fileoffset == 0x01) {
auto tp = make_pooled_tracepoint<TracepointDynamic>(pool, std::string(name()),
std::move(e), m_source_type);
if (!filter || filter(*tp)) return tp;
} else if (fileoffset < 0xFF) {
return ErrorTracepoint::make(
name(), "invalid file offset: value is less than minimum valid offset (0xFF)");
} else {
if (fileoffset > m_file_size) m_file_size = m_file.grow();
if ((fileoffset + sizeof(uint32_t)) > m_file_size)
return ErrorTracepoint::make(name(), "file offset bigger than file");
if (m_file.get<char>(fileoffset) != '{')
return ErrorTracepoint::make(
name(), "invalid meta magic at offset " + std::to_string(fileoffset) +
": expected '{', found '" +
std::string(1, m_file.get<char>(fileoffset)) + "'");
const uint32_t meta_size = m_file.get<uint32_t>(fileoffset + 1);
if (meta_size == 0) return ErrorTracepoint::make(name(), "invalid meta size (0)");
if ((fileoffset + meta_size) > m_file_size)
return ErrorTracepoint::make(name(), "meta entry bigger than file end");
const std::span<const uint8_t> meta{&m_file.getReference<const uint8_t>(fileoffset),
meta_size};
auto tp = make_pooled_tracepoint<TracepointStatic>(
pool, std::string(name()), std::move(e), meta, m_file.getFileInternal(),
m_source_type);
if (!filter || filter(*tp)) return tp;
}
}
}
uint64_t SyncTbInternal::pending() noexcept {
return m_tracebuffer_file.getRingbuffer().pendingBytes();
}
void SyncTbInternal::skipToEnd() noexcept {
m_tracebuffer_file.getRingbuffer().skipToEnd();
}
bool Tracebuffer::is_tracebuffer(const fs::path &path) {
if (!path.has_extension()) return false;
const auto extension = path.extension();
if (extension != ".clltk_trace" && extension != ".clltk_ktrace") return false;
if (!fs::exists(path)) return false;
std::ifstream file{path, std::ios::binary};
if (!file) return false;
std::array<char, 16> fileHead{};
file.read(fileHead.begin(), fileHead.size());
if (safe_cast<size_t>(file.gcount()) < fileHead.size()) return false;
const std::string_view magic{fileHead.data(), fileHead.size()};
if (magic == little_endian_magic) return true;
if (magic == big_endian_magic) return true; // foreign byte order, byte-swapped while reading
return false;
}
bool SnapTracebuffer::is_formattable(const fs::path &path) {
if (is_tracebuffer(path)) return true;
if (Archive::is_archive(path)) return true;
return false;
}
SnapTracebuffer::SnapTracebuffer(const std::filesystem::path &path, TracepointCollection &&tps,
std::string &&name, size_t size, SourceType source_type) noexcept
: Tracebuffer(path, source_type)
, tracepoints(std::move(tps))
, m_name(std::move(name))
, m_size(size) {}
SnapTracebufferPtr SnapTracebuffer::make(const fs::path &path,
const TracepointFilterFunc &tracepointFilter) {
auto sync_tb = SyncTracebuffer::make(path);
if (!sync_tb) return {};
std::string name{sync_tb->name()};
auto size = sync_tb->size();
auto source_type = sync_tb->sourceType();
TracepointCollection tps{};
const uint64_t top_nr = sync_tb->current_top_entries_nr();
while (true) {
TracepointPtr e = sync_tb->next();
if (!e || (e->nr > top_nr)) break;
if (!tracepointFilter || tracepointFilter(*e)) tps.push_back(std::move(e));
}
static constexpr auto cmp = [](const auto &a, const auto &b) -> bool {
return a->timestamp_ns < b->timestamp_ns;
};
boost::sort::block_indirect_sort(tps.begin(), tps.end(), cmp);
SnapTracebufferPtr tb{
new SnapTracebuffer(path, std::move(tps), std::move(name), size, source_type)};
return tb;
}
static INLINE void add(SnapTracebufferCollection &out, SnapTracebufferCollection &&in) {
for (auto &t : in)
if (t) out.push_back(std::move(t));
}
SnapTracebufferCollection SnapTracebuffer::collect(const fs::path &path,
const TracebufferFilterFunc &tracebufferFilter,
const TracepointFilterFunc &tracepointFilter,
bool recursive) {
SnapTracebufferCollection out{};
if (!fs::exists(path)) return out;
if (fs::is_directory(path)) {
if (recursive) {
for (auto const &entry : fs::recursive_directory_iterator(path)) {
if (Tracebuffer::is_tracebuffer(entry))
add(out, collect(entry, tracebufferFilter, tracepointFilter, recursive));
}
} else {
for (auto const &entry : fs::directory_iterator(path)) {
if (Tracebuffer::is_tracebuffer(entry))
add(out, collect(entry, tracebufferFilter, tracepointFilter, recursive));
}
}
} else if (auto archive = Archive::make(path)) {
add(out, collect(archive->dir(), tracebufferFilter, tracepointFilter, recursive));
} else if (Tracebuffer::is_tracebuffer(path)) {
auto tp = make(path, tracepointFilter);
if (tp && (!tracebufferFilter || tracebufferFilter(*tp))) out.push_back(std::move(tp));
}
return out;
}
// Helper to get TraceBufferInfo from a single tracebuffer file
static TraceBufferInfo getTraceBufferInfo(const fs::path &path) {
TraceBufferInfo info;
info.path = path;
info.name = path.stem().string();
// Get file modification time
std::error_code ec;
info.modified = fs::last_write_time(path, ec);
try {
TracebufferFile tbf(path.string());
const auto &def = tbf.getDefinition();
auto &rb = tbf.getRingbuffer();
info.name = std::string(def.name());
info.source_type = toPublicSourceType(def.sourceType());
// If source type unknown, fall back to extension-based detection
if (info.source_type == SourceType::Unknown) {
if (path.extension() == ".clltk_ktrace") {
info.source_type = (info.name == "TTY") ? SourceType::TTY : SourceType::Kernel;
} else if (path.extension() == ".clltk_trace") {
info.source_type = SourceType::Userspace;
}
}
info.capacity = rb.getSize();
info.used = rb.getUsed();
info.available = rb.getAvailable();
info.fill_percent =
(info.capacity > 0)
? (static_cast<double>(info.used) / static_cast<double>(info.capacity) * 100.0)
: 0.0;
info.entries = rb.getEntryCount();
info.dropped = rb.getDropped();
info.pending = info.entries - info.dropped;
info.wrapped = rb.getWrapped();
} catch (const std::exception &e) { info.error = e.what(); } catch (...) {
info.error = "unknown error reading tracebuffer";
}
return info;
}
TraceBufferInfoCollection CommonLowLevelTracingKit::decoder::listTraceBuffers(
const fs::path &path, bool recursive, const std::function<bool(const std::string &)> &filter) {
TraceBufferInfoCollection result;
if (!fs::exists(path)) { return result; }
auto processEntry = [&](const fs::path &entry_path) {
if (!Tracebuffer::is_tracebuffer(entry_path)) { return; }
const std::string name = entry_path.stem().string();
if (filter && !filter(name)) { return; }
result.push_back(getTraceBufferInfo(entry_path));
};
if (fs::is_directory(path)) {
if (recursive) {
for (const auto &entry : fs::recursive_directory_iterator(path)) {
if (entry.is_regular_file()) { processEntry(entry.path()); }
}
} else {
for (const auto &entry : fs::directory_iterator(path)) {
if (entry.is_regular_file()) { processEntry(entry.path()); }
}
}
} else if (fs::is_regular_file(path)) {
processEntry(path);
}
return result;
}