Add StructuredData plugin type; showcase with new DarwinLog feature

Take 2, with missing cmake line fixed.  Build tested on
Ubuntu 14.04 with clang-3.6.

See docs/structured_data/StructuredDataPlugins.md for details.

differential review: https://reviews.llvm.org/D22976

reviewers: clayborg, jingham
llvm-svn: 279202
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index a30ce67..200fa1a 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -56,6 +56,7 @@
 #include "lldb/Target/RegisterContext.h"
 #include "lldb/Target/SectionLoadList.h"
 #include "lldb/Target/StopInfo.h"
+#include "lldb/Target/StructuredDataPlugin.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Target/Thread.h"
 #include "lldb/Utility/AnsiTerminal.h"
@@ -1476,14 +1477,15 @@
     return total_bytes;
 }
 
-
 // This function handles events that were broadcast by the process.
 void
 Debugger::HandleProcessEvent (const EventSP &event_sp)
 {
     using namespace lldb;
     const uint32_t event_type = event_sp->GetType();
-    ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
+    ProcessSP process_sp = (event_type == Process::eBroadcastBitStructuredData)
+        ? EventDataStructuredData::GetProcessFromEvent(event_sp.get())
+        : Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
 
     StreamSP output_stream_sp = GetAsyncOutputStream();
     StreamSP error_stream_sp = GetAsyncErrorStream();
@@ -1498,6 +1500,9 @@
         const bool got_state_changed = (event_type & Process::eBroadcastBitStateChanged) != 0;
         const bool got_stdout = (event_type & Process::eBroadcastBitSTDOUT) != 0;
         const bool got_stderr = (event_type & Process::eBroadcastBitSTDERR) != 0;
+        const bool got_structured_data = (event_type &
+             Process::eBroadcastBitStructuredData) != 0;
+
         if (got_state_changed)
         {
             StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
@@ -1522,6 +1527,45 @@
             GetProcessSTDERR (process_sp.get(), error_stream_sp.get());
         }
 
+        // Give structured data events an opportunity to display.
+        if (got_structured_data)
+        {
+            StructuredDataPluginSP plugin_sp =
+                EventDataStructuredData::GetPluginFromEvent(event_sp.get());
+            if (plugin_sp)
+            {
+                auto structured_data_sp =
+                    EventDataStructuredData::GetObjectFromEvent(event_sp.get());
+                if (output_stream_sp)
+                {
+                    StreamString content_stream;
+                    Error error = plugin_sp->GetDescription(structured_data_sp,
+                                                            content_stream);
+                    if (error.Success())
+                    {
+                        if (!content_stream.GetString().empty())
+                        {
+                            // Add newline.
+                            content_stream.PutChar('\n');
+                            content_stream.Flush();
+
+                            // Print it.
+                            output_stream_sp->PutCString(content_stream
+                                                         .GetString().c_str());
+                        }
+                    }
+                    else
+                    {
+                        error_stream_sp->Printf("Failed to print structured "
+                                                "data with plugin %s: %s",
+                                                plugin_sp->GetPluginName()
+                                                    .AsCString(),
+                                                error.AsCString());
+                    }
+                }
+            }
+        }
+
         // Now display any stopped state changes after any STDIO
         if (got_state_changed && state_is_stopped)
         {
@@ -1586,7 +1630,8 @@
     BroadcastEventSpec process_event_spec (broadcaster_class_process,
                                            Process::eBroadcastBitStateChanged   |
                                            Process::eBroadcastBitSTDOUT         |
-                                           Process::eBroadcastBitSTDERR);
+                                           Process::eBroadcastBitSTDERR         |
+                                           Process::eBroadcastBitStructuredData);
 
     BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
                                           Thread::eBroadcastBitStackChanged     |