Thread hardening part 3. Now lldb_private::Thread objects have std::weak_ptr
objects for the backlink to the lldb_private::Process. The issues we were
running into before was someone was holding onto a shared pointer to a 
lldb_private::Thread for too long, and the lldb_private::Process parent object
would get destroyed and the lldb_private::Thread had a "Process &m_process"
member which would just treat whatever memory that used to be a Process as a
valid Process. This was mostly happening for lldb_private::StackFrame objects
that had a member like "Thread &m_thread". So this completes the internal
strong/weak changes.

Documented the ExecutionContext and ExecutionContextRef classes so that our
LLDB developers can understand when and where to use ExecutionContext and 
ExecutionContextRef objects.

llvm-svn: 151009
diff --git a/lldb/source/Plugins/UnwindAssembly/x86/UnwindAssembly-x86.cpp b/lldb/source/Plugins/UnwindAssembly/x86/UnwindAssembly-x86.cpp
index 1379207..c0daaac 100644
--- a/lldb/source/Plugins/UnwindAssembly/x86/UnwindAssembly-x86.cpp
+++ b/lldb/source/Plugins/UnwindAssembly/x86/UnwindAssembly-x86.cpp
@@ -117,7 +117,7 @@
 class AssemblyParse_x86 {
 public:
 
-    AssemblyParse_x86 (Target &target, Thread *thread, int cpu, AddressRange func);
+    AssemblyParse_x86 (const ExecutionContext &exe_ctx, int cpu, AddressRange func);
 
     bool get_non_call_site_unwind_plan (UnwindPlan &unwind_plan);
 
@@ -140,8 +140,7 @@
     bool machine_regno_to_lldb_regno (int machine_regno, uint32_t& lldb_regno);
     bool instruction_length (Address addr, int &length);
 
-    Target &m_target;
-    Thread* m_thread;
+    const ExecutionContext m_exe_ctx;
 
     AddressRange m_func_bounds;
 
@@ -162,14 +161,20 @@
     DISALLOW_COPY_AND_ASSIGN (AssemblyParse_x86);
 };
 
-AssemblyParse_x86::AssemblyParse_x86 (Target& target, Thread* thread, int cpu, AddressRange func) :
-                         m_target (target), m_thread (thread), m_func_bounds(func), m_cur_insn (),
-                         m_machine_ip_regnum (-1), m_machine_sp_regnum (-1), m_machine_fp_regnum (-1),
-                         m_lldb_ip_regnum (-1), m_lldb_sp_regnum (-1), m_lldb_fp_regnum (-1),
-                         m_wordsize (-1), m_cpu(cpu)
+AssemblyParse_x86::AssemblyParse_x86 (const ExecutionContext &exe_ctx, int cpu, AddressRange func) :
+    m_exe_ctx (exe_ctx), 
+    m_func_bounds(func), 
+    m_cur_insn (),
+    m_machine_ip_regnum (LLDB_INVALID_REGNUM),
+    m_machine_sp_regnum (LLDB_INVALID_REGNUM),
+    m_machine_fp_regnum (LLDB_INVALID_REGNUM),
+    m_lldb_ip_regnum (LLDB_INVALID_REGNUM), 
+    m_lldb_sp_regnum (LLDB_INVALID_REGNUM),
+    m_lldb_fp_regnum (LLDB_INVALID_REGNUM),
+    m_wordsize (-1), 
+    m_cpu(cpu)
 {
     int *initialized_flag = NULL;
-    m_lldb_ip_regnum = m_lldb_sp_regnum = m_lldb_fp_regnum = -1;
     if (cpu == k_i386)
     {
         m_machine_ip_regnum = k_machine_eip;
@@ -191,9 +196,10 @@
     if (m_func_bounds.GetByteSize() == 0)
         m_func_bounds.SetByteSize(512);
 
-    if (m_thread && *initialized_flag == 0)
+    Thread *thread = m_exe_ctx.GetThreadPtr();
+    if (thread && *initialized_flag == 0)
     {
-        RegisterContext *reg_ctx = m_thread->GetRegisterContext().get();
+        RegisterContext *reg_ctx = thread->GetRegisterContext().get();
         if (reg_ctx)
         {
             struct regmap_ent *ent;
@@ -501,7 +507,7 @@
     } InitializeLLVM;
 
     EDDisassemblerRef disasm;
-    EDInstRef         cur_insn;
+    EDInstRef cur_insn;
 
     if (EDGetDisassembler (&disasm, triple, kEDAssemblySyntaxX86ATT) != 0)
     {
@@ -511,7 +517,7 @@
     uint64_t addr_offset = addr.GetOffset();
     struct edis_byte_read_token arg;
     arg.address = &addr;
-    arg.target = &m_target;
+    arg.target = m_exe_ctx.GetTargetPtr();
     if (EDCreateInsts (&cur_insn, 1, disasm, read_byte_for_edis, addr_offset, &arg) != 1)
     {
         return false;
@@ -558,6 +564,7 @@
     unwind_plan.AppendRow (row);
     const bool prefer_file_cache = true;
 
+    Target *target = m_exe_ctx.GetTargetPtr();
     while (m_func_bounds.ContainsFileAddress (m_cur_insn) && non_prologue_insn_count < 10)
     {
         int stack_offset, insn_len;
@@ -569,7 +576,7 @@
             // An unrecognized/junk instruction
             break;
         }
-        if (m_target.ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
+        if (target->ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
         {
            // Error reading the instruction out of the file, stop scanning
            break;
@@ -679,7 +686,7 @@
         Address last_insn (m_func_bounds.GetBaseAddress());
         last_insn.SetOffset (last_insn.GetOffset() + m_func_bounds.GetByteSize() - 1);
         uint8_t bytebuf[1];
-        if (m_target.ReadMemory (last_insn, prefer_file_cache, bytebuf, 1, error) != -1)
+        if (target->ReadMemory (last_insn, prefer_file_cache, bytebuf, 1, error) != -1)
         {
             if (bytebuf[0] == 0xc3)   // ret aka retq
             {
@@ -728,10 +735,12 @@
     if (!func.GetBaseAddress().IsValid())
         return false;
 
+    Target *target = m_exe_ctx.GetTargetPtr();
+
     uint8_t bytebuf[4];
     Error error;
     const bool prefer_file_cache = true;
-    if (m_target.ReadMemory (func.GetBaseAddress(), prefer_file_cache, bytebuf, sizeof (bytebuf), error) == -1)
+    if (target->ReadMemory (func.GetBaseAddress(), prefer_file_cache, bytebuf, sizeof (bytebuf), error) == -1)
         return false;
 
     uint8_t i386_prologue[] = {0x55, 0x89, 0xe5};
@@ -790,6 +799,7 @@
     }
 
     const bool prefer_file_cache = true;
+    Target *target = m_exe_ctx.GetTargetPtr();
     while (m_func_bounds.ContainsFileAddress (m_cur_insn))
     {
         Error error;
@@ -799,7 +809,7 @@
             // An error parsing the instruction, i.e. probably data/garbage - stop scanning
             break;
         }
-        if (m_target.ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
+        if (target->ReadMemory (m_cur_insn, prefer_file_cache, m_cur_insn_bytes, insn_len, error) == -1)
         {
            // Error reading the instruction out of the file, stop scanning
            break;
@@ -843,21 +853,23 @@
 bool
 UnwindAssembly_x86::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& func, Thread& thread, UnwindPlan& unwind_plan)
 {
-    AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
+    ExecutionContext exe_ctx (thread.shared_from_this());
+    AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, func);
     return asm_parse.get_non_call_site_unwind_plan (unwind_plan);
 }
 
 bool
 UnwindAssembly_x86::GetFastUnwindPlan (AddressRange& func, Thread& thread, UnwindPlan &unwind_plan)
 {
-    AssemblyParse_x86 asm_parse(thread.GetProcess().GetTarget(), &thread, m_cpu, func);
+    ExecutionContext exe_ctx (thread.shared_from_this());
+    AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, func);
     return asm_parse.get_fast_unwind_plan (func, unwind_plan);
 }
 
 bool
-UnwindAssembly_x86::FirstNonPrologueInsn (AddressRange& func, Target& target, Thread* thread, Address& first_non_prologue_insn)
+UnwindAssembly_x86::FirstNonPrologueInsn (AddressRange& func, const ExecutionContext &exe_ctx, Address& first_non_prologue_insn)
 {
-    AssemblyParse_x86 asm_parse(target, thread, m_cpu, func);
+    AssemblyParse_x86 asm_parse(exe_ctx, m_cpu, func);
     return asm_parse.find_first_non_prologue_insn (first_non_prologue_insn);
 }