check for the presence of the initialization record

This commit is contained in:
Simon Cruanes 2023-12-31 22:52:13 -05:00
parent d2bdcc2e2c
commit bafc86326a
No known key found for this signature in database
GPG Key ID: EBFFF6F283F3A2B4

View File

@ -1,6 +1,7 @@
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <ostream>
#include <vector>
#ifdef _WIN32
@ -357,6 +358,8 @@ void readLoc(std::string &locFile, uint32_t &locLine,
}
}
struct TraceNotInitialized : std::exception {};
int main(int argc, char **argv) {
#ifdef _WIN32
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
@ -390,6 +393,9 @@ int main(int argc, char **argv) {
int n_records = 0;
std::string name;
std::vector<Argument> arguments;
bool initialized = false;
#define CHECK_INIT() if (!initialized) throw TraceNotInitialized{}
while (offset < buf.size()) {
Record r = read_next_record(buf, offset);
@ -398,11 +404,25 @@ int main(int argc, char **argv) {
uint8_t ty = r.header & 0xf;
switch (ty) {
case 0: {
// metadata record
if (!initialized) {
if (r.header == 0x0016547846040010) {
// magic string "FxT"
// https://fuchsia.dev/fuchsia-src/reference/tracing/trace-format#magic-number-record
initialized = true;
}
}
break;
}
case 1: {
break; // init record, ignore
CHECK_INIT();
break; // initialization record
}
case 2: {
// string
CHECK_INIT();
uint16_t str_ref = (r.header >> 16) & 0xffff;
assert((str_ref & 0x8000) == 0);
uint16_t str_len = (r.header >> 32) & 0x7fff;
@ -414,6 +434,7 @@ int main(int argc, char **argv) {
}
case 3: {
// thread record
CHECK_INIT();
uint8_t th_ref = (r.header >> 16) & 0xff;
uint64_t pid = r.p[1];
uint64_t tid = r.p[2];
@ -423,6 +444,7 @@ int main(int argc, char **argv) {
}
case 4: {
// event
CHECK_INIT();
uint8_t ev_ty = (r.header >> 16) & 0xf;
uint8_t n_args = (r.header >> 20) & 0xf;
@ -526,6 +548,7 @@ int main(int argc, char **argv) {
case 7: {
// kernel object
CHECK_INIT();
uint8_t ty = (r.header >> 16) & 0xff;
uint16_t name_ref = (r.header >> 24) & 0xffff;