11#include < cinttypes>
22#include < cstddef>
33#include < cstdint>
4+ #include < exception>
45#include < ostream>
56#include < vector>
67#ifdef _WIN32
@@ -357,6 +358,8 @@ void readLoc(std::string &locFile, uint32_t &locLine,
357358 }
358359}
359360
361+ struct TraceNotInitialized : std::exception {};
362+
360363int main (int argc, char **argv) {
361364#ifdef _WIN32
362365 if (!AttachConsole (ATTACH_PARENT_PROCESS)) {
@@ -390,6 +393,9 @@ int main(int argc, char **argv) {
390393 int n_records = 0 ;
391394 std::string name;
392395 std::vector<Argument> arguments;
396+ bool initialized = false ;
397+
398+ #define CHECK_INIT () if (!initialized) throw TraceNotInitialized{}
393399
394400 while (offset < buf.size ()) {
395401 Record r = read_next_record (buf, offset);
@@ -398,8 +404,37 @@ int main(int argc, char **argv) {
398404 uint8_t ty = r.header & 0xf ;
399405
400406 switch (ty) {
407+ case 0 : {
408+ // metadata record
409+ if (!initialized) {
410+ if (r.header == 0x0016547846040010 ) {
411+ // magic string "FxT"
412+ // https://fuchsia.dev/fuchsia-src/reference/tracing/trace-format#magic-number-record
413+ initialized = true ;
414+ }
415+
416+ }
417+ break ;
418+ }
419+ case 1 : {
420+ CHECK_INIT ();
421+ break ; // initialization record
422+ }
423+ case 2 : {
424+ // string
425+ CHECK_INIT ();
426+ uint16_t str_ref = (r.header >> 16 ) & 0xffff ;
427+ assert ((str_ref & 0x8000 ) == 0 );
428+ uint16_t str_len = (r.header >> 32 ) & 0x7fff ;
429+
430+ name.resize (str_len + 1 );
431+ memcpy (name.data (), (uint8_t *)&r.p [1 ], str_len);
432+ dec.stringRefs [str_ref] = name;
433+ break ;
434+ }
401435 case 3 : {
402436 // thread record
437+ CHECK_INIT ();
403438 uint8_t th_ref = (r.header >> 16 ) & 0xff ;
404439 uint64_t pid = r.p [1 ];
405440 uint64_t tid = r.p [2 ];
@@ -409,6 +444,7 @@ int main(int argc, char **argv) {
409444 }
410445 case 4 : {
411446 // event
447+ CHECK_INIT ();
412448 uint8_t ev_ty = (r.header >> 16 ) & 0xf ;
413449 uint8_t n_args = (r.header >> 20 ) & 0xf ;
414450
@@ -512,6 +548,7 @@ int main(int argc, char **argv) {
512548
513549 case 7 : {
514550 // kernel object
551+ CHECK_INIT ();
515552
516553 uint8_t ty = (r.header >> 16 ) & 0xff ;
517554 uint16_t name_ref = (r.header >> 24 ) & 0xffff ;
0 commit comments