From bafc86326af9bbffa94280faabac166349679348 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 31 Dec 2023 22:52:13 -0500 Subject: [PATCH] check for the presence of the initialization record --- import-fuchsia/src/import-fuchsia.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/import-fuchsia/src/import-fuchsia.cpp b/import-fuchsia/src/import-fuchsia.cpp index 152070fe..9b4ef02a 100644 --- a/import-fuchsia/src/import-fuchsia.cpp +++ b/import-fuchsia/src/import-fuchsia.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #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 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;