Add more logging to 2246-trace-stream test

2246-trace-stream is failing flakily with a low frequency with a NPE.
Adding some logging to see what's going on. The parser seems to see an
event on a thread that doesn't have a corresponding log for information
about the thread.

Bug: 279547877
Test: art/test.py -t 2246
Change-Id: Id1bd3ce3c98ef382410986f5d4a0e06d82f688c0
diff --git a/test/2246-trace-stream/src/BaseTraceParser.java b/test/2246-trace-stream/src/BaseTraceParser.java
index c4b14aa..ebc346c 100644
--- a/test/2246-trace-stream/src/BaseTraceParser.java
+++ b/test/2246-trace-stream/src/BaseTraceParser.java
@@ -101,12 +101,14 @@
         }
     }
 
-    public int GetEntryHeader() throws IOException {
-        // Read 2-byte thread-id. On host thread-ids can be greater than 16-bit.
+    public int GetThreadID() throws IOException {
+        // Read 2-byte thread-id. On host thread-ids can be greater than 16-bit but it is truncated
+        // to 16-bits in the trace.
         int threadId = readNumber(2);
-        if (threadId != 0) {
-            return threadId;
-        }
+        return threadId;
+    }
+
+    public int GetEntryHeader() throws IOException {
         // Read 1-byte header type
         return readNumber(1);
     }
@@ -135,10 +137,15 @@
     }
 
     public boolean ShouldIgnoreThread(int threadId) throws Exception {
-        if (threadIdMap.get(threadId).contains("Daemon")) {
-            return true;
+        if (!threadIdMap.containsKey(threadId)) {
+          System.out.println("no threadId -> name  mapping for thread " + threadId);
+          // TODO(b/279547877): Ideally we should throw here, since it isn't expected. Just
+          // continuing to get more logs from the bots to see what's happening here. The
+          // test will fail anyway because the diff will be different.
+          return false;
         }
-        return false;
+
+        return threadIdMap.get(threadId).contains("Daemon");
     }
 
     public String eventTypeToString(int eventType, int threadId) {
diff --git a/test/2246-trace-stream/src/NonStreamTraceParser.java b/test/2246-trace-stream/src/NonStreamTraceParser.java
index 7763c9c..49903ca 100644
--- a/test/2246-trace-stream/src/NonStreamTraceParser.java
+++ b/test/2246-trace-stream/src/NonStreamTraceParser.java
@@ -79,7 +79,7 @@
         boolean hasEntries = true;
         boolean seenStopTracingMethod = false;
         for (int i = 0; i < numEntries; i++) {
-            int threadId = GetEntryHeader();
+            int threadId = GetThreadID();
             String eventString = ProcessEventEntry(threadId);
             // Ignore daemons (ex: heap task daemon, reference queue daemon) because they may not
             // be deterministic.
diff --git a/test/2246-trace-stream/src/StreamTraceParser.java b/test/2246-trace-stream/src/StreamTraceParser.java
index c723d3c..d7ef9b9 100644
--- a/test/2246-trace-stream/src/StreamTraceParser.java
+++ b/test/2246-trace-stream/src/StreamTraceParser.java
@@ -26,32 +26,36 @@
         boolean hasEntries = true;
         boolean seenStopTracingMethod = false;
         while (hasEntries) {
-            int headerType = GetEntryHeader();
-            switch (headerType) {
+            int threadId = GetThreadID();
+            if (threadId != 0) {
+              String eventString = ProcessEventEntry(threadId);
+              if (ShouldIgnoreThread(threadId)) {
+                break;
+              }
+              // Ignore events after method tracing was stopped. The code that is executed
+              // later could be non-deterministic.
+              if (!seenStopTracingMethod) {
+                UpdateThreadEvents(threadId, eventString);
+              }
+              if (eventString.contains("Main$VMDebug $noinline$stopMethodTracing")) {
+                seenStopTracingMethod = true;
+              }
+            } else {
+              int headerType = GetEntryHeader();
+              switch (headerType) {
                 case 1:
-                    ProcessMethodInfoEntry();
-                    break;
+                  ProcessMethodInfoEntry();
+                  break;
                 case 2:
-                    ProcessThreadInfoEntry();
-                    break;
+                  ProcessThreadInfoEntry();
+                  break;
                 case 3:
-                    // TODO(mythria): Add test to also check format of trace summary.
-                    hasEntries = false;
-                    break;
+                  // TODO(mythria): Add test to also check format of trace summary.
+                  hasEntries = false;
+                  break;
                 default:
-                    int threadId = headerType;
-                    String eventString = ProcessEventEntry(threadId);
-                    if (ShouldIgnoreThread(threadId)) {
-                        break;
-                    }
-                    // Ignore events after method tracing was stopped. The code that is executed
-                    // later could be non-deterministic.
-                    if (!seenStopTracingMethod) {
-                        UpdateThreadEvents(threadId, eventString);
-                    }
-                    if (eventString.contains("Main$VMDebug $noinline$stopMethodTracing")) {
-                        seenStopTracingMethod = true;
-                    }
+                  System.out.println("Unexpected header in the trace " + headerType);
+              }
             }
         }
         closeFile();