Modified all logging calls to hand out shared pointers to make sure we
don't crash if we disable logging when some code already has a copy of the
logger. Prior to this fix, logs were handed out as pointers and if they were
held onto while a log got disabled, then it could cause a crash. Now all logs
are handed out as shared pointers so this problem shouldn't happen anymore.
We are also using our new shared pointers that put the shared pointer count
and the object into the same allocation for a tad better performance.



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@118319 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index d53bf81..50a7aec 100644
--- a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -193,8 +193,8 @@
     StringExtractorGDBRemote &response
 )
 {
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS);
-    Log *async_log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_ASYNC);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
+    LogSP async_log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_ASYNC));
     if (log)
         log->Printf ("GDBRemoteCommunication::%s ()", __FUNCTION__);
 
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index 8d3675d..7e5c3c9 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -633,7 +633,7 @@
     Clear();
     ArchSpec arch_spec = GetTarget().GetArchitecture();
     
-    //Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS);
+    //LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
     
     
     if (attach_pid != LLDB_INVALID_PROCESS_ID)
@@ -735,7 +735,7 @@
     // HACK: require arch be set correctly at the target level until we can
     // figure out a good way to determine the arch of what we are attaching to
 
-    //Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS);
+    //LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
     if (process_name && process_name[0])
     {
         char host_port[128];
@@ -948,7 +948,7 @@
 ProcessGDBRemote::UpdateThreadListIfNeeded ()
 {
     // locker will keep a mutex locked until it goes out of scope
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
     if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
         log->Printf ("ProcessGDBRemote::%s (pid = %i)", __FUNCTION__, GetID());
 
@@ -1166,7 +1166,7 @@
 ProcessGDBRemote::DoDetach()
 {
     Error error;
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
     if (log)
         log->Printf ("ProcessGDBRemote::DoDetach()");
 
@@ -1199,7 +1199,7 @@
 ProcessGDBRemote::DoDestroy ()
 {
     Error error;
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
     if (log)
         log->Printf ("ProcessGDBRemote::DoDestroy()");
 
@@ -1407,7 +1407,7 @@
     Error error;
     assert (bp_site != NULL);
 
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
     user_id_t site_id = bp_site->GetID();
     const addr_t addr = bp_site->GetLoadAddress();
     if (log)
@@ -1483,7 +1483,7 @@
     assert (bp_site != NULL);
     addr_t addr = bp_site->GetLoadAddress();
     user_id_t site_id = bp_site->GetID();
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
     if (log)
         log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %d) addr = 0x%8.8llx", site_id, (uint64_t)addr);
 
@@ -1550,7 +1550,7 @@
     {
         user_id_t watchID = wp->GetID();
         addr_t addr = wp->GetLoadAddress();
-        Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS);
+        LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
         if (log)
             log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %d)", watchID);
         if (wp->IsEnabled())
@@ -1582,7 +1582,7 @@
     {
         user_id_t watchID = wp->GetID();
 
-        Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS);
+        LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
 
         addr_t addr = wp->GetLoadAddress();
         if (log)
@@ -1620,7 +1620,7 @@
 ProcessGDBRemote::DoSignal (int signo)
 {
     Error error;
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
     if (log)
         log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
 
@@ -1706,14 +1706,14 @@
             m_stdio_communication.Clear();
             posix_spawnattr_t attr;
 
-            Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS);
+            LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
 
             Error local_err;    // Errors that don't affect the spawning.
             if (log)
                 log->Printf ("%s ( path='%s', argv=%p, envp=%p, arch=%s )", __FUNCTION__, debugserver_path, inferior_argv, inferior_envp, inferior_arch.AsCString());
             error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX);
             if (error.Fail() || log)
-                error.PutToLog(log, "::posix_spawnattr_init ( &attr )");
+                error.PutToLog(log.get(), "::posix_spawnattr_init ( &attr )");
             if (error.Fail())
                 return error;;
 
@@ -1730,7 +1730,7 @@
                     size_t ocount = 0;
                     error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX);
                     if (error.Fail() || log)
-                        error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %zu )", cpu, ocount);
+                        error.PutToLog(log.get(), "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %zu )", cpu, ocount);
 
                     if (error.Fail() != 0 || ocount != 1)
                         return error;
@@ -1879,7 +1879,7 @@
                 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
 
             if (error.Fail() || log)
-                error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", m_debugserver_pid, debugserver_path, NULL, &attr, inferior_argv, inferior_envp);
+                error.PutToLog(log.get(), "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", m_debugserver_pid, debugserver_path, NULL, &attr, inferior_argv, inferior_envp);
 
             if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
             {
@@ -2055,7 +2055,7 @@
 {
     ResetGDBRemoteState ();
 
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
 
     if (log)
         log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
@@ -2069,7 +2069,7 @@
 void
 ProcessGDBRemote::StopAsyncThread ()
 {
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
 
     if (log)
         log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
@@ -2089,7 +2089,7 @@
 {
     ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
 
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
     if (log)
         log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, arg, process->GetID());
 
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp b/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp
index 776ffeb..963a468 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp
@@ -18,38 +18,37 @@
 using namespace lldb_private;
 
 
-static Log* g_log = NULL; // Leak for now as auto_ptr was being cleaned up
-                          // by global constructors before other threads
-                          // were done with it.
-Log *
+// We want to avoid global constructors where code needs to be run so here we
+// control access to our static g_log_sp by hiding it in a singleton function
+// that will construct the static g_lob_sp the first time this function is 
+// called.
+static LogSP &
+GetLog ()
+{
+    static LogSP g_log_sp;
+    return g_log_sp;
+}
+
+LogSP
 ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (uint32_t mask)
 {
-    Log *log = g_log;
+    LogSP log(GetLog ());
     if (log && mask)
     {
         uint32_t log_mask = log->GetMask().Get();
         if ((log_mask & mask) != mask)
-            return NULL;
+            return LogSP();
     }
     return log;
 }
 
 void
-ProcessGDBRemoteLog::DeleteLog ()
-{
-    if (g_log)
-    {
-        delete g_log;
-        g_log = NULL;
-    }
-}
-
-void
 ProcessGDBRemoteLog::DisableLog (Args &args, Stream *feedback_strm)
 {
-    if (g_log)
+    LogSP log (GetLog ());
+    if (log)
     {
-        uint32_t flag_bits = g_log->GetMask().Get();
+        uint32_t flag_bits = log->GetMask().Get();
         const size_t argc = args.GetArgumentCount ();
         for (size_t i = 0; i < argc; ++i)
         {
@@ -79,20 +78,34 @@
         }
         
         if (flag_bits == 0)
-            DeleteLog();
+            GetLog ().reset();
         else
-            g_log->GetMask().Reset (flag_bits);
+            log->GetMask().Reset (flag_bits);
     }
     
     return;
 }
 
-Log *
+LogSP
 ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
 {
-    DeleteLog ();
-    g_log = new Log (log_stream_sp);
-    if (g_log)
+    // Try see if there already is a log - that way we can reuse its settings.
+    // We could reuse the log in toto, but we don't know that the stream is the same.
+    uint32_t flag_bits;
+    LogSP log(GetLog ());
+    if (log)
+        flag_bits = log->GetMask().Get();
+    else
+        flag_bits = 0;
+
+    // Now make a new log with this stream if one was provided
+    if (log_stream_sp)
+    {
+        log = make_shared<Log>(log_stream_sp);
+        GetLog () = log;
+    }
+
+    if (log)
     {
         uint32_t flag_bits = 0;
         bool got_unknown_category = false;
@@ -127,10 +140,10 @@
         }
         if (flag_bits == 0)
             flag_bits = GDBR_LOG_DEFAULT;
-        g_log->GetMask().Reset(flag_bits);
-        g_log->GetOptions().Reset(log_options);
+        log->GetMask().Reset(flag_bits);
+        log->GetOptions().Reset(log_options);
     }
-    return g_log;
+    return log;
 }
 
 void
@@ -157,7 +170,7 @@
 void
 ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...)
 {
-    Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask);
+    LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask));
     if (log)
     {
         va_list args;
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h b/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h
index 2dbd94f..5e98403 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h
@@ -35,16 +35,13 @@
 class ProcessGDBRemoteLog
 {
 public:
-    static lldb_private::Log *
+    static lldb::LogSP
     GetLogIfAllCategoriesSet(uint32_t mask = 0);
 
     static void
     DisableLog (lldb_private::Args &args, lldb_private::Stream *feedback_strm);
 
-    static void
-    DeleteLog ();
-    
-    static lldb_private::Log *
+    static lldb::LogSP
     EnableLog (lldb::StreamSP &log_stream_sp, uint32_t log_options, lldb_private::Args &args, lldb_private::Stream *feedback_strm);
 
     static void