Fixed issues with RegisterContext classes and the subclasses. There was
an issue with the way the UnwindLLDB was handing out RegisterContexts: it
was making shared pointers to register contexts and then handing out just
the pointers (which would get put into shared pointers in the thread and
stack frame classes) and cause double free issues. MallocScribble helped to
find these issues after I did some other cleanup. To help avoid any
RegisterContext issue in the future, all code that deals with them now
returns shared pointers to the register contexts so we don't end up with
multiple deletions. Also now that the RegisterContext class doesn't require
a stack frame, we patched a memory leak where a StackFrame object was being
created and leaked.

Made the RegisterContext class not have a pointer to a StackFrame object as
one register context class can be used for N inlined stack frames so there is
not a 1 - 1 mapping. Updates the ExecutionContextScope part of the 
RegisterContext class to never return a stack frame to indicate this when it
is asked to recreate the execution context. Now register contexts point to the
concrete frame using a concrete frame index. Concrete frames are all of the
frames that are actually formed on the stack of a thread. These concrete frames
can be turned into one or more user visible frames due to inlining. Each 
inlined stack frame has the exact same register context (shared via shared
pointers) as any parent inlined stack frames all the way up to the concrete 
frame itself.

So now the stack frames and the register contexts should behave much better.



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@122976 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/Process/Utility/UnwindLLDB.cpp b/source/Plugins/Process/Utility/UnwindLLDB.cpp
index 38f2c46..062d0a5 100644
--- a/source/Plugins/Process/Utility/UnwindLLDB.cpp
+++ b/source/Plugins/Process/Utility/UnwindLLDB.cpp
@@ -47,26 +47,22 @@
     // First, set up the 0th (initial) frame
     CursorSP first_cursor_sp(new Cursor ());
     RegisterContextSP no_frame; 
-    RegisterContextLLDB *first_register_ctx = new RegisterContextLLDB(m_thread, no_frame, first_cursor_sp->sctx, 0);
-    if (!first_register_ctx->IsValid())
-    {
-        delete first_register_ctx;
+    std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB(m_thread, no_frame, first_cursor_sp->sctx, 0));
+    if (first_register_ctx_ap.get() == NULL)
         return false;
-    }
-    if (!first_register_ctx->GetCFA (first_cursor_sp->cfa))
-    {
-        delete first_register_ctx;
+    
+    if (!first_register_ctx_ap->IsValid())
         return false;
-    }
-    if (!first_register_ctx->GetPC (first_cursor_sp->start_pc))
-    {
-        delete first_register_ctx;
+
+    if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa))
         return false;
-    }
-    // Reuse the StackFrame provided by the processor native machine context for the first frame
-    first_register_ctx->SetStackFrame (m_thread.GetStackFrameAtIndex(0).get());
-    RegisterContextSP first_register_ctx_sp(first_register_ctx);
-    first_cursor_sp->reg_ctx = first_register_ctx_sp;
+
+    if (!first_register_ctx_ap->GetPC (first_cursor_sp->start_pc))
+        return false;
+
+    // Everything checks out, so release the auto pointer value and let the
+    // cursor own it in its shared pointer
+    first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release());
     m_frames.push_back (first_cursor_sp);
     return true;
 }
@@ -77,18 +73,21 @@
 {
     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
     CursorSP cursor_sp(new Cursor ());
-    RegisterContextLLDB *register_ctx;
 
     // Frame zero is a little different
     if (m_frames.size() == 0)
         return false;
 
     uint32_t cur_idx = m_frames.size ();
-    register_ctx = new RegisterContextLLDB (m_thread, m_frames[cur_idx - 1]->reg_ctx, cursor_sp->sctx, cur_idx);
+    std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread, 
+                                                                                m_frames[cur_idx - 1]->reg_ctx, 
+                                                                                cursor_sp->sctx, 
+                                                                                cur_idx));
+    if (register_ctx_ap.get() == NULL)
+        return false;
 
-    if (!register_ctx->IsValid())
+    if (!register_ctx_ap->IsValid())
     {
-        delete register_ctx;
         if (log)
         {
             log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk", 
@@ -96,9 +95,8 @@
         }
         return false;
     }
-    if (!register_ctx->GetCFA (cursor_sp->cfa))
+    if (!register_ctx_ap->GetCFA (cursor_sp->cfa))
     {
-        delete register_ctx;
         if (log)
         {
             log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
@@ -108,7 +106,6 @@
     }
     if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0)
     {
-        delete register_ctx;
         if (log)
         {
             log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
@@ -116,9 +113,8 @@
         }
         return false;
     }
-    if (!register_ctx->GetPC (cursor_sp->start_pc))
+    if (!register_ctx_ap->GetPC (cursor_sp->start_pc))
     {
-        delete register_ctx;
         if (log)
         {
             log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
@@ -126,9 +122,7 @@
         }
         return false;
     }
-    RegisterContextSP register_ctx_sp(register_ctx);
-    StackFrame *frame = new StackFrame(cur_idx, cur_idx, m_thread, register_ctx_sp, cursor_sp->cfa, cursor_sp->start_pc, &(cursor_sp->sctx));
-    register_ctx->SetStackFrame (frame);
+    RegisterContextSP register_ctx_sp(register_ctx_ap.release());
     cursor_sp->reg_ctx = register_ctx_sp;
     m_frames.push_back (cursor_sp);
     return true;
@@ -155,9 +149,10 @@
     return false;
 }
 
-RegisterContext *
+lldb::RegisterContextSP
 UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame)
 {
+    lldb::RegisterContextSP reg_ctx_sp;
     uint32_t idx = frame->GetFrameIndex ();
 
     if (idx == 0)
@@ -168,13 +163,13 @@
     if (m_frames.size() == 0)
     {
         if (!AddFirstFrame())
-            return NULL;
+            return reg_ctx_sp;
     }
 
     while (idx >= m_frames.size() && AddOneMoreFrame ())
         ;
 
     if (idx < m_frames.size ())
-        return m_frames[idx]->reg_ctx.get();
-    return NULL;
+        reg_ctx_sp = m_frames[idx]->reg_ctx;
+    return reg_ctx_sp;
 }