]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Process / Utility / RegisterContextLLDB.cpp
1 //===-- RegisterContextLLDB.cpp --------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/Address.h"
11 #include "lldb/Core/AddressRange.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/Value.h"
14 #include "lldb/Expression/DWARFExpression.h"
15 #include "lldb/Symbol/ArmUnwindInfo.h"
16 #include "lldb/Symbol/DWARFCallFrameInfo.h"
17 #include "lldb/Symbol/FuncUnwinders.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/DynamicLoader.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Platform.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/SectionLoadList.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Utility/DataBufferHeap.h"
32 #include "lldb/Utility/Log.h"
33 #include "lldb/Utility/RegisterValue.h"
34 #include "lldb/lldb-private.h"
35
36 #include "RegisterContextLLDB.h"
37
38 using namespace lldb;
39 using namespace lldb_private;
40
41 static ConstString GetSymbolOrFunctionName(const SymbolContext &sym_ctx) {
42   if (sym_ctx.symbol)
43     return sym_ctx.symbol->GetName();
44   else if (sym_ctx.function)
45     return sym_ctx.function->GetName();
46   return ConstString();
47 }
48
49 RegisterContextLLDB::RegisterContextLLDB(Thread &thread,
50                                          const SharedPtr &next_frame,
51                                          SymbolContext &sym_ctx,
52                                          uint32_t frame_number,
53                                          UnwindLLDB &unwind_lldb)
54     : RegisterContext(thread, frame_number), m_thread(thread),
55       m_fast_unwind_plan_sp(), m_full_unwind_plan_sp(),
56       m_fallback_unwind_plan_sp(), m_all_registers_available(false),
57       m_frame_type(-1), m_cfa(LLDB_INVALID_ADDRESS),
58       m_afa(LLDB_INVALID_ADDRESS), m_start_pc(),
59       m_current_pc(), m_current_offset(0), m_current_offset_backed_up_one(0),
60       m_sym_ctx(sym_ctx), m_sym_ctx_valid(false), m_frame_number(frame_number),
61       m_registers(), m_parent_unwind(unwind_lldb) {
62   m_sym_ctx.Clear(false);
63   m_sym_ctx_valid = false;
64
65   if (IsFrameZero()) {
66     InitializeZerothFrame();
67   } else {
68     InitializeNonZerothFrame();
69   }
70
71   // This same code exists over in the GetFullUnwindPlanForFrame() but it may
72   // not have been executed yet
73   if (IsFrameZero() || next_frame->m_frame_type == eTrapHandlerFrame ||
74       next_frame->m_frame_type == eDebuggerFrame) {
75     m_all_registers_available = true;
76   }
77 }
78
79 bool RegisterContextLLDB::IsUnwindPlanValidForCurrentPC(
80     lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset) {
81   if (!unwind_plan_sp)
82     return false;
83
84   // check if m_current_pc is valid
85   if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
86     // yes - current offset can be used as is
87     valid_pc_offset = m_current_offset;
88     return true;
89   }
90
91   // if m_current_offset <= 0, we've got nothing else to try
92   if (m_current_offset <= 0)
93     return false;
94
95   // check pc - 1 to see if it's valid
96   Address pc_minus_one(m_current_pc);
97   pc_minus_one.SetOffset(m_current_pc.GetOffset() - 1);
98   if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) {
99     // *valid_pc_offset = m_current_offset - 1;
100     valid_pc_offset = m_current_pc.GetOffset() - 1;
101     return true;
102   }
103
104   return false;
105 }
106
107 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the
108 // zeroth frame or currently executing frame.
109
110 void RegisterContextLLDB::InitializeZerothFrame() {
111   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
112   ExecutionContext exe_ctx(m_thread.shared_from_this());
113   RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
114
115   if (reg_ctx_sp.get() == NULL) {
116     m_frame_type = eNotAValidFrame;
117     UnwindLogMsg("frame does not have a register context");
118     return;
119   }
120
121   addr_t current_pc = reg_ctx_sp->GetPC();
122
123   if (current_pc == LLDB_INVALID_ADDRESS) {
124     m_frame_type = eNotAValidFrame;
125     UnwindLogMsg("frame does not have a pc");
126     return;
127   }
128
129   Process *process = exe_ctx.GetProcessPtr();
130
131   // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
132   // this will strip bit zero in case we read a PC from memory or from the LR.
133   // (which would be a no-op in frame 0 where we get it from the register set,
134   // but still a good idea to make the call here for other ABIs that may
135   // exist.)
136   ABI *abi = process->GetABI().get();
137   if (abi)
138     current_pc = abi->FixCodeAddress(current_pc);
139
140   // Initialize m_current_pc, an Address object, based on current_pc, an
141   // addr_t.
142   m_current_pc.SetLoadAddress(current_pc, &process->GetTarget());
143
144   // If we don't have a Module for some reason, we're not going to find
145   // symbol/function information - just stick in some reasonable defaults and
146   // hope we can unwind past this frame.
147   ModuleSP pc_module_sp(m_current_pc.GetModule());
148   if (!m_current_pc.IsValid() || !pc_module_sp) {
149     UnwindLogMsg("using architectural default unwind method");
150   }
151
152   // We require either a symbol or function in the symbols context to be
153   // successfully filled in or this context is of no use to us.
154   const SymbolContextItem resolve_scope =
155       eSymbolContextFunction | eSymbolContextSymbol;
156   if (pc_module_sp.get() && (pc_module_sp->ResolveSymbolContextForAddress(
157                                  m_current_pc, resolve_scope, m_sym_ctx) &
158                              resolve_scope)) {
159     m_sym_ctx_valid = true;
160   }
161
162   if (m_sym_ctx.symbol) {
163     UnwindLogMsg("with pc value of 0x%" PRIx64 ", symbol name is '%s'",
164                  current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
165   } else if (m_sym_ctx.function) {
166     UnwindLogMsg("with pc value of 0x%" PRIx64 ", function name is '%s'",
167                  current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
168   } else {
169     UnwindLogMsg("with pc value of 0x%" PRIx64
170                  ", no symbol/function name is known.",
171                  current_pc);
172   }
173
174   AddressRange addr_range;
175   m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range);
176
177   if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
178     m_frame_type = eTrapHandlerFrame;
179   } else {
180     // FIXME:  Detect eDebuggerFrame here.
181     m_frame_type = eNormalFrame;
182   }
183
184   // If we were able to find a symbol/function, set addr_range to the bounds of
185   // that symbol/function. else treat the current pc value as the start_pc and
186   // record no offset.
187   if (addr_range.GetBaseAddress().IsValid()) {
188     m_start_pc = addr_range.GetBaseAddress();
189     if (m_current_pc.GetSection() == m_start_pc.GetSection()) {
190       m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
191     } else if (m_current_pc.GetModule() == m_start_pc.GetModule()) {
192       // This means that whatever symbol we kicked up isn't really correct ---
193       // we should not cross section boundaries ... We really should NULL out
194       // the function/symbol in this case unless there is a bad assumption here
195       // due to inlined functions?
196       m_current_offset =
197           m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
198     }
199     m_current_offset_backed_up_one = m_current_offset;
200   } else {
201     m_start_pc = m_current_pc;
202     m_current_offset = -1;
203     m_current_offset_backed_up_one = -1;
204   }
205
206   // We've set m_frame_type and m_sym_ctx before these calls.
207
208   m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame();
209   m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
210
211   UnwindPlan::RowSP active_row;
212   lldb::RegisterKind row_register_kind = eRegisterKindGeneric;
213   if (m_full_unwind_plan_sp &&
214       m_full_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
215     active_row =
216         m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
217     row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
218     if (active_row.get() && log) {
219       StreamString active_row_strm;
220       active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
221                        m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
222       UnwindLogMsg("%s", active_row_strm.GetData());
223     }
224   }
225
226   if (!active_row.get()) {
227     UnwindLogMsg("could not find an unwindplan row for this frame's pc");
228     m_frame_type = eNotAValidFrame;
229     return;
230   }
231
232   if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
233     // Try the fall back unwind plan since the
234     // full unwind plan failed.
235     FuncUnwindersSP func_unwinders_sp;
236     UnwindPlanSP call_site_unwind_plan;
237     bool cfa_status = false;
238
239     if (m_sym_ctx_valid) {
240       func_unwinders_sp =
241           pc_module_sp->GetObjectFile()
242               ->GetUnwindTable()
243               .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx);
244     }
245
246     if (func_unwinders_sp.get() != nullptr)
247       call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(
248           process->GetTarget(), m_current_offset_backed_up_one);
249
250     if (call_site_unwind_plan.get() != nullptr) {
251       m_fallback_unwind_plan_sp = call_site_unwind_plan;
252       if (TryFallbackUnwindPlan())
253         cfa_status = true;
254     }
255     if (!cfa_status) {
256       UnwindLogMsg("could not read CFA value for first frame.");
257       m_frame_type = eNotAValidFrame;
258       return;
259     }
260   } else
261     ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
262
263   UnwindLogMsg("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64
264                " afa is 0x%" PRIx64 " using %s UnwindPlan",
265                (uint64_t)m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()),
266                (uint64_t)m_cfa,
267                (uint64_t)m_afa,
268                m_full_unwind_plan_sp->GetSourceName().GetCString());
269 }
270
271 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the
272 // RegisterContextLLDB "below" it to provide things like its current pc value.
273
274 void RegisterContextLLDB::InitializeNonZerothFrame() {
275   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
276   if (IsFrameZero()) {
277     m_frame_type = eNotAValidFrame;
278     UnwindLogMsg("non-zeroth frame tests positive for IsFrameZero -- that "
279                  "shouldn't happen.");
280     return;
281   }
282
283   if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) {
284     m_frame_type = eNotAValidFrame;
285     UnwindLogMsg("Could not get next frame, marking this frame as invalid.");
286     return;
287   }
288   if (!m_thread.GetRegisterContext()) {
289     m_frame_type = eNotAValidFrame;
290     UnwindLogMsg("Could not get register context for this thread, marking this "
291                  "frame as invalid.");
292     return;
293   }
294
295   addr_t pc;
296   if (!ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) {
297     UnwindLogMsg("could not get pc value");
298     m_frame_type = eNotAValidFrame;
299     return;
300   }
301
302   ExecutionContext exe_ctx(m_thread.shared_from_this());
303   Process *process = exe_ctx.GetProcessPtr();
304   // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
305   // this will strip bit zero in case we read a PC from memory or from the LR.
306   ABI *abi = process->GetABI().get();
307   if (abi)
308     pc = abi->FixCodeAddress(pc);
309
310   if (log) {
311     UnwindLogMsg("pc = 0x%" PRIx64, pc);
312     addr_t reg_val;
313     if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val))
314       UnwindLogMsg("fp = 0x%" PRIx64, reg_val);
315     if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val))
316       UnwindLogMsg("sp = 0x%" PRIx64, reg_val);
317   }
318
319   // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap
320   // handler function
321   bool above_trap_handler = false;
322   if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
323       GetNextFrame()->IsTrapHandlerFrame())
324     above_trap_handler = true;
325
326   if (pc == 0 || pc == 0x1) {
327     if (!above_trap_handler) {
328       m_frame_type = eNotAValidFrame;
329       UnwindLogMsg("this frame has a pc of 0x0");
330       return;
331     }
332   }
333
334   const bool allow_section_end = true;
335   m_current_pc.SetLoadAddress(pc, &process->GetTarget(), allow_section_end);
336
337   // If we don't have a Module for some reason, we're not going to find
338   // symbol/function information - just stick in some reasonable defaults and
339   // hope we can unwind past this frame.
340   ModuleSP pc_module_sp(m_current_pc.GetModule());
341   if (!m_current_pc.IsValid() || !pc_module_sp) {
342     UnwindLogMsg("using architectural default unwind method");
343
344     // Test the pc value to see if we know it's in an unmapped/non-executable
345     // region of memory.
346     uint32_t permissions;
347     if (process->GetLoadAddressPermissions(pc, permissions) &&
348         (permissions & ePermissionsExecutable) == 0) {
349       // If this is the second frame off the stack, we may have unwound the
350       // first frame incorrectly.  But using the architecture default unwind
351       // plan may get us back on track -- albeit possibly skipping a real
352       // frame.  Give this frame a clearly-invalid pc and see if we can get any
353       // further.
354       if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
355           GetNextFrame()->IsFrameZero()) {
356         UnwindLogMsg("had a pc of 0x%" PRIx64 " which is not in executable "
357                                               "memory but on frame 1 -- "
358                                               "allowing it once.",
359                      (uint64_t)pc);
360         m_frame_type = eSkipFrame;
361       } else {
362         // anywhere other than the second frame, a non-executable pc means
363         // we're off in the weeds -- stop now.
364         m_frame_type = eNotAValidFrame;
365         UnwindLogMsg("pc is in a non-executable section of memory and this "
366                      "isn't the 2nd frame in the stack walk.");
367         return;
368       }
369     }
370
371     if (abi) {
372       m_fast_unwind_plan_sp.reset();
373       m_full_unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
374       abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp);
375       if (m_frame_type != eSkipFrame) // don't override eSkipFrame
376       {
377         m_frame_type = eNormalFrame;
378       }
379       m_all_registers_available = false;
380       m_current_offset = -1;
381       m_current_offset_backed_up_one = -1;
382       RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
383       UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0);
384       if (row.get()) {
385         if (!ReadFrameAddress(row_register_kind, row->GetCFAValue(), m_cfa)) {
386           UnwindLogMsg("failed to get cfa value");
387           if (m_frame_type != eSkipFrame) // don't override eSkipFrame
388           {
389             m_frame_type = eNotAValidFrame;
390           }
391           return;
392         }
393
394         ReadFrameAddress(row_register_kind, row->GetAFAValue(), m_afa);
395
396         // A couple of sanity checks..
397         if (m_cfa == LLDB_INVALID_ADDRESS || m_cfa == 0 || m_cfa == 1) {
398           UnwindLogMsg("could not find a valid cfa address");
399           m_frame_type = eNotAValidFrame;
400           return;
401         }
402
403         // m_cfa should point into the stack memory; if we can query memory
404         // region permissions, see if the memory is allocated & readable.
405         if (process->GetLoadAddressPermissions(m_cfa, permissions) &&
406             (permissions & ePermissionsReadable) == 0) {
407           m_frame_type = eNotAValidFrame;
408           UnwindLogMsg(
409               "the CFA points to a region of memory that is not readable");
410           return;
411         }
412       } else {
413         UnwindLogMsg("could not find a row for function offset zero");
414         m_frame_type = eNotAValidFrame;
415         return;
416       }
417
418       if (CheckIfLoopingStack()) {
419         TryFallbackUnwindPlan();
420         if (CheckIfLoopingStack()) {
421           UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
422                        "looping - stopping");
423           m_frame_type = eNotAValidFrame;
424           return;
425         }
426       }
427
428       UnwindLogMsg("initialized frame cfa is 0x%" PRIx64 " afa is 0x%" PRIx64,
429                    (uint64_t)m_cfa, (uint64_t)m_afa);
430       return;
431     }
432     m_frame_type = eNotAValidFrame;
433     UnwindLogMsg("could not find any symbol for this pc, or a default unwind "
434                  "plan, to continue unwind.");
435     return;
436   }
437
438   bool resolve_tail_call_address = false; // m_current_pc can be one past the
439                                           // address range of the function...
440   // If the saved pc does not point to a function/symbol because it is beyond
441   // the bounds of the correct function and there's no symbol there, we do
442   // *not* want ResolveSymbolContextForAddress to back up the pc by 1, because
443   // then we might not find the correct unwind information later. Instead, let
444   // ResolveSymbolContextForAddress fail, and handle the case via
445   // decr_pc_and_recompute_addr_range below.
446   const SymbolContextItem resolve_scope =
447       eSymbolContextFunction | eSymbolContextSymbol;
448   uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress(
449       m_current_pc, resolve_scope, m_sym_ctx, resolve_tail_call_address);
450
451   // We require either a symbol or function in the symbols context to be
452   // successfully filled in or this context is of no use to us.
453   if (resolve_scope & resolved_scope) {
454     m_sym_ctx_valid = true;
455   }
456
457   if (m_sym_ctx.symbol) {
458     UnwindLogMsg("with pc value of 0x%" PRIx64 ", symbol name is '%s'", pc,
459                  GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
460   } else if (m_sym_ctx.function) {
461     UnwindLogMsg("with pc value of 0x%" PRIx64 ", function name is '%s'", pc,
462                  GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
463   } else {
464     UnwindLogMsg("with pc value of 0x%" PRIx64
465                  ", no symbol/function name is known.",
466                  pc);
467   }
468
469   AddressRange addr_range;
470   if (!m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range)) {
471     m_sym_ctx_valid = false;
472   }
473
474   bool decr_pc_and_recompute_addr_range = false;
475
476   // If the symbol lookup failed...
477   if (!m_sym_ctx_valid)
478     decr_pc_and_recompute_addr_range = true;
479
480   // Or if we're in the middle of the stack (and not "above" an asynchronous
481   // event like sigtramp), and our "current" pc is the start of a function...
482   if (GetNextFrame()->m_frame_type != eTrapHandlerFrame &&
483       GetNextFrame()->m_frame_type != eDebuggerFrame &&
484       (!m_sym_ctx_valid ||
485        (addr_range.GetBaseAddress().IsValid() &&
486         addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection() &&
487         addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset()))) {
488     decr_pc_and_recompute_addr_range = true;
489   }
490
491   // We need to back up the pc by 1 byte and re-search for the Symbol to handle
492   // the case where the "saved pc" value is pointing to the next function, e.g.
493   // if a function ends with a CALL instruction.
494   // FIXME this may need to be an architectural-dependent behavior; if so we'll
495   // need to add a member function
496   // to the ABI plugin and consult that.
497   if (decr_pc_and_recompute_addr_range) {
498     UnwindLogMsg("Backing up the pc value of 0x%" PRIx64
499                  " by 1 and re-doing symbol lookup; old symbol was %s",
500                  pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
501     Address temporary_pc;
502     temporary_pc.SetLoadAddress(pc - 1, &process->GetTarget());
503     m_sym_ctx.Clear(false);
504     m_sym_ctx_valid = false;
505     SymbolContextItem resolve_scope =
506         eSymbolContextFunction | eSymbolContextSymbol;
507
508     ModuleSP temporary_module_sp = temporary_pc.GetModule();
509     if (temporary_module_sp &&
510         temporary_module_sp->ResolveSymbolContextForAddress(
511             temporary_pc, resolve_scope, m_sym_ctx) &
512             resolve_scope) {
513       if (m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range))
514         m_sym_ctx_valid = true;
515     }
516     UnwindLogMsg("Symbol is now %s",
517                  GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
518   }
519
520   // If we were able to find a symbol/function, set addr_range_ptr to the
521   // bounds of that symbol/function. else treat the current pc value as the
522   // start_pc and record no offset.
523   if (addr_range.GetBaseAddress().IsValid()) {
524     m_start_pc = addr_range.GetBaseAddress();
525     m_current_offset = pc - m_start_pc.GetLoadAddress(&process->GetTarget());
526     m_current_offset_backed_up_one = m_current_offset;
527     if (decr_pc_and_recompute_addr_range &&
528         m_current_offset_backed_up_one > 0) {
529       m_current_offset_backed_up_one--;
530       if (m_sym_ctx_valid) {
531         m_current_pc.SetLoadAddress(pc - 1, &process->GetTarget());
532       }
533     }
534   } else {
535     m_start_pc = m_current_pc;
536     m_current_offset = -1;
537     m_current_offset_backed_up_one = -1;
538   }
539
540   if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
541     m_frame_type = eTrapHandlerFrame;
542   } else {
543     // FIXME:  Detect eDebuggerFrame here.
544     if (m_frame_type != eSkipFrame) // don't override eSkipFrame
545     {
546       m_frame_type = eNormalFrame;
547     }
548   }
549
550   // We've set m_frame_type and m_sym_ctx before this call.
551   m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame();
552
553   UnwindPlan::RowSP active_row;
554   RegisterKind row_register_kind = eRegisterKindGeneric;
555
556   // Try to get by with just the fast UnwindPlan if possible - the full
557   // UnwindPlan may be expensive to get (e.g. if we have to parse the entire
558   // eh_frame section of an ObjectFile for the first time.)
559
560   if (m_fast_unwind_plan_sp &&
561       m_fast_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
562     active_row =
563         m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
564     row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind();
565     if (active_row.get() && log) {
566       StreamString active_row_strm;
567       active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread,
568                        m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
569       UnwindLogMsg("active row: %s", active_row_strm.GetData());
570     }
571   } else {
572     m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
573     int valid_offset = -1;
574     if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp, valid_offset)) {
575       active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset(valid_offset);
576       row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
577       if (active_row.get() && log) {
578         StreamString active_row_strm;
579         active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
580                          &m_thread,
581                          m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
582         UnwindLogMsg("active row: %s", active_row_strm.GetData());
583       }
584     }
585   }
586
587   if (!active_row.get()) {
588     m_frame_type = eNotAValidFrame;
589     UnwindLogMsg("could not find unwind row for this pc");
590     return;
591   }
592
593   if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
594     UnwindLogMsg("failed to get cfa");
595     m_frame_type = eNotAValidFrame;
596     return;
597   }
598
599   ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
600
601   UnwindLogMsg("m_cfa = 0x%" PRIx64 " m_afa = 0x%" PRIx64, m_cfa, m_afa);
602
603   if (CheckIfLoopingStack()) {
604     TryFallbackUnwindPlan();
605     if (CheckIfLoopingStack()) {
606       UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
607                    "looping - stopping");
608       m_frame_type = eNotAValidFrame;
609       return;
610     }
611   }
612
613   UnwindLogMsg("initialized frame current pc is 0x%" PRIx64
614                " cfa is 0x%" PRIx64 " afa is 0x%" PRIx64,
615                (uint64_t)m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()),
616                (uint64_t)m_cfa,
617                (uint64_t)m_afa);
618 }
619
620 bool RegisterContextLLDB::CheckIfLoopingStack() {
621   // If we have a bad stack setup, we can get the same CFA value multiple times
622   // -- or even more devious, we can actually oscillate between two CFA values.
623   // Detect that here and break out to avoid a possible infinite loop in lldb
624   // trying to unwind the stack. To detect when we have the same CFA value
625   // multiple times, we compare the
626   // CFA of the current
627   // frame with the 2nd next frame because in some specail case (e.g. signal
628   // hanlders, hand written assembly without ABI compiance) we can have 2
629   // frames with the same
630   // CFA (in theory we
631   // can have arbitrary number of frames with the same CFA, but more then 2 is
632   // very very unlikely)
633
634   RegisterContextLLDB::SharedPtr next_frame = GetNextFrame();
635   if (next_frame) {
636     RegisterContextLLDB::SharedPtr next_next_frame = next_frame->GetNextFrame();
637     addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
638     if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) {
639       if (next_next_frame_cfa == m_cfa) {
640         // We have a loop in the stack unwind
641         return true;
642       }
643     }
644   }
645   return false;
646 }
647
648 bool RegisterContextLLDB::IsFrameZero() const { return m_frame_number == 0; }
649
650 // Find a fast unwind plan for this frame, if possible.
651 //
652 // On entry to this method,
653 //
654 //   1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
655 //   if either of those are correct,
656 //   2. m_sym_ctx should already be filled in, and
657 //   3. m_current_pc should have the current pc value for this frame
658 //   4. m_current_offset_backed_up_one should have the current byte offset into
659 //   the function, maybe backed up by 1, -1 if unknown
660
661 UnwindPlanSP RegisterContextLLDB::GetFastUnwindPlanForFrame() {
662   UnwindPlanSP unwind_plan_sp;
663   ModuleSP pc_module_sp(m_current_pc.GetModule());
664
665   if (!m_current_pc.IsValid() || !pc_module_sp ||
666       pc_module_sp->GetObjectFile() == NULL)
667     return unwind_plan_sp;
668
669   if (IsFrameZero())
670     return unwind_plan_sp;
671
672   FuncUnwindersSP func_unwinders_sp(
673       pc_module_sp->GetObjectFile()
674           ->GetUnwindTable()
675           .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx));
676   if (!func_unwinders_sp)
677     return unwind_plan_sp;
678
679   // If we're in _sigtramp(), unwinding past this frame requires special
680   // knowledge.
681   if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame)
682     return unwind_plan_sp;
683
684   unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind(
685       *m_thread.CalculateTarget(), m_thread);
686   if (unwind_plan_sp) {
687     if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
688       Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
689       if (log && log->GetVerbose()) {
690         if (m_fast_unwind_plan_sp)
691           UnwindLogMsgVerbose("frame, and has a fast UnwindPlan");
692         else
693           UnwindLogMsgVerbose("frame");
694       }
695       m_frame_type = eNormalFrame;
696       return unwind_plan_sp;
697     } else {
698       unwind_plan_sp.reset();
699     }
700   }
701   return unwind_plan_sp;
702 }
703
704 // On entry to this method,
705 //
706 //   1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
707 //   if either of those are correct,
708 //   2. m_sym_ctx should already be filled in, and
709 //   3. m_current_pc should have the current pc value for this frame
710 //   4. m_current_offset_backed_up_one should have the current byte offset into
711 //   the function, maybe backed up by 1, -1 if unknown
712
713 UnwindPlanSP RegisterContextLLDB::GetFullUnwindPlanForFrame() {
714   UnwindPlanSP unwind_plan_sp;
715   UnwindPlanSP arch_default_unwind_plan_sp;
716   ExecutionContext exe_ctx(m_thread.shared_from_this());
717   Process *process = exe_ctx.GetProcessPtr();
718   ABI *abi = process ? process->GetABI().get() : NULL;
719   if (abi) {
720     arch_default_unwind_plan_sp.reset(
721         new UnwindPlan(lldb::eRegisterKindGeneric));
722     abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
723   } else {
724     UnwindLogMsg(
725         "unable to get architectural default UnwindPlan from ABI plugin");
726   }
727
728   bool behaves_like_zeroth_frame = false;
729   if (IsFrameZero() || GetNextFrame()->m_frame_type == eTrapHandlerFrame ||
730       GetNextFrame()->m_frame_type == eDebuggerFrame) {
731     behaves_like_zeroth_frame = true;
732     // If this frame behaves like a 0th frame (currently executing or
733     // interrupted asynchronously), all registers can be retrieved.
734     m_all_registers_available = true;
735   }
736
737   // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
738   // so the pc is 0x0 in the zeroth frame, we need to use the "unwind at first
739   // instruction" arch default UnwindPlan Also, if this Process can report on
740   // memory region attributes, any non-executable region means we jumped
741   // through a bad function pointer - handle the same way as 0x0. Note, if we
742   // have a symbol context & a symbol, we don't want to follow this code path.
743   // This is for jumping to memory regions without any information available.
744
745   if ((!m_sym_ctx_valid ||
746        (m_sym_ctx.function == NULL && m_sym_ctx.symbol == NULL)) &&
747       behaves_like_zeroth_frame && m_current_pc.IsValid()) {
748     uint32_t permissions;
749     addr_t current_pc_addr =
750         m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr());
751     if (current_pc_addr == 0 ||
752         (process &&
753          process->GetLoadAddressPermissions(current_pc_addr, permissions) &&
754          (permissions & ePermissionsExecutable) == 0)) {
755       if (abi) {
756         unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
757         abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp);
758         m_frame_type = eNormalFrame;
759         return unwind_plan_sp;
760       }
761     }
762   }
763
764   // No Module for the current pc, try using the architecture default unwind.
765   ModuleSP pc_module_sp(m_current_pc.GetModule());
766   if (!m_current_pc.IsValid() || !pc_module_sp ||
767       pc_module_sp->GetObjectFile() == NULL) {
768     m_frame_type = eNormalFrame;
769     return arch_default_unwind_plan_sp;
770   }
771
772   FuncUnwindersSP func_unwinders_sp;
773   if (m_sym_ctx_valid) {
774     func_unwinders_sp =
775         pc_module_sp->GetObjectFile()
776             ->GetUnwindTable()
777             .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx);
778   }
779
780   // No FuncUnwinders available for this pc (stripped function symbols, lldb
781   // could not augment its function table with another source, like
782   // LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). See if eh_frame or the
783   // .ARM.exidx tables have unwind information for this address, else fall back
784   // to the architectural default unwind.
785   if (!func_unwinders_sp) {
786     m_frame_type = eNormalFrame;
787
788     if (!pc_module_sp || !pc_module_sp->GetObjectFile() ||
789         !m_current_pc.IsValid())
790       return arch_default_unwind_plan_sp;
791
792     // Even with -fomit-frame-pointer, we can try eh_frame to get back on
793     // track.
794     DWARFCallFrameInfo *eh_frame =
795         pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo();
796     if (eh_frame) {
797       unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
798       if (eh_frame->GetUnwindPlan(m_current_pc, *unwind_plan_sp))
799         return unwind_plan_sp;
800       else
801         unwind_plan_sp.reset();
802     }
803
804     ArmUnwindInfo *arm_exidx =
805         pc_module_sp->GetObjectFile()->GetUnwindTable().GetArmUnwindInfo();
806     if (arm_exidx) {
807       unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
808       if (arm_exidx->GetUnwindPlan(exe_ctx.GetTargetRef(), m_current_pc,
809                                    *unwind_plan_sp))
810         return unwind_plan_sp;
811       else
812         unwind_plan_sp.reset();
813     }
814
815     return arch_default_unwind_plan_sp;
816   }
817
818   // If we're in _sigtramp(), unwinding past this frame requires special
819   // knowledge.  On Mac OS X this knowledge is properly encoded in the eh_frame
820   // section, so prefer that if available. On other platforms we may need to
821   // provide a platform-specific UnwindPlan which encodes the details of how to
822   // unwind out of sigtramp.
823   if (m_frame_type == eTrapHandlerFrame && process) {
824     m_fast_unwind_plan_sp.reset();
825     unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan(
826         process->GetTarget(), m_current_offset_backed_up_one);
827     if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc) &&
828         unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) {
829       return unwind_plan_sp;
830     }
831   }
832
833   // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
834   // even when it's frame zero This comes up if we have hand-written functions
835   // in a Module and hand-written eh_frame.  The assembly instruction
836   // inspection may fail and the eh_frame CFI were probably written with some
837   // care to do the right thing.  It'd be nice if there was a way to ask the
838   // eh_frame directly if it is asynchronous (can be trusted at every
839   // instruction point) or synchronous (the normal case - only at call sites).
840   // But there is not.
841   if (process && process->GetDynamicLoader() &&
842       process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo(m_sym_ctx)) {
843     // We must specifically call the GetEHFrameUnwindPlan() method here --
844     // normally we would call GetUnwindPlanAtCallSite() -- because CallSite may
845     // return an unwind plan sourced from either eh_frame (that's what we
846     // intend) or compact unwind (this won't work)
847     unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan(
848         process->GetTarget(), m_current_offset_backed_up_one);
849     if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
850       UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because the "
851                           "DynamicLoader suggested we prefer it",
852                           unwind_plan_sp->GetSourceName().GetCString());
853       return unwind_plan_sp;
854     }
855   }
856
857   // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
858   // the assembly language instructions
859   if (behaves_like_zeroth_frame && process) {
860     unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
861         process->GetTarget(), m_thread, m_current_offset_backed_up_one);
862     if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
863       if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
864         // We probably have an UnwindPlan created by inspecting assembly
865         // instructions. The assembly profilers work really well with compiler-
866         // generated functions but hand- written assembly can be problematic.
867         // We set the eh_frame based unwind plan as our fallback unwind plan if
868         // instruction emulation doesn't work out even for non call sites if it
869         // is available and use the architecture default unwind plan if it is
870         // not available. The eh_frame unwind plan is more reliable even on non
871         // call sites then the architecture default plan and for hand written
872         // assembly code it is often written in a way that it valid at all
873         // location what helps in the most common cases when the instruction
874         // emulation fails.
875         UnwindPlanSP call_site_unwind_plan =
876             func_unwinders_sp->GetUnwindPlanAtCallSite(
877                 process->GetTarget(), m_current_offset_backed_up_one);
878         if (call_site_unwind_plan &&
879             call_site_unwind_plan.get() != unwind_plan_sp.get() &&
880             call_site_unwind_plan->GetSourceName() !=
881                 unwind_plan_sp->GetSourceName()) {
882           m_fallback_unwind_plan_sp = call_site_unwind_plan;
883         } else {
884           m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
885         }
886       }
887       UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
888                           unwind_plan_sp->GetSourceName().GetCString());
889       return unwind_plan_sp;
890     }
891   }
892
893   // Typically this is unwind info from an eh_frame section intended for
894   // exception handling; only valid at call sites
895   if (process) {
896     unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite(
897         process->GetTarget(), m_current_offset_backed_up_one);
898   }
899   int valid_offset = -1;
900   if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) {
901     UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
902                         unwind_plan_sp->GetSourceName().GetCString());
903     return unwind_plan_sp;
904   }
905
906   // We'd prefer to use an UnwindPlan intended for call sites when we're at a
907   // call site but if we've struck out on that, fall back to using the non-
908   // call-site assembly inspection UnwindPlan if possible.
909   if (process) {
910     unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
911         process->GetTarget(), m_thread, m_current_offset_backed_up_one);
912   }
913   if (unwind_plan_sp &&
914       unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
915     // We probably have an UnwindPlan created by inspecting assembly
916     // instructions. The assembly profilers work really well with compiler-
917     // generated functions but hand- written assembly can be problematic. We
918     // set the eh_frame based unwind plan as our fallback unwind plan if
919     // instruction emulation doesn't work out even for non call sites if it is
920     // available and use the architecture default unwind plan if it is not
921     // available. The eh_frame unwind plan is more reliable even on non call
922     // sites then the architecture default plan and for hand written assembly
923     // code it is often written in a way that it valid at all location what
924     // helps in the most common cases when the instruction emulation fails.
925     UnwindPlanSP call_site_unwind_plan =
926         func_unwinders_sp->GetUnwindPlanAtCallSite(
927             process->GetTarget(), m_current_offset_backed_up_one);
928     if (call_site_unwind_plan &&
929         call_site_unwind_plan.get() != unwind_plan_sp.get() &&
930         call_site_unwind_plan->GetSourceName() !=
931             unwind_plan_sp->GetSourceName()) {
932       m_fallback_unwind_plan_sp = call_site_unwind_plan;
933     } else {
934       m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
935     }
936   }
937
938   if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) {
939     UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
940                         unwind_plan_sp->GetSourceName().GetCString());
941     return unwind_plan_sp;
942   }
943
944   // If we're on the first instruction of a function, and we have an
945   // architectural default UnwindPlan for the initial instruction of a
946   // function, use that.
947   if (m_current_offset_backed_up_one == 0) {
948     unwind_plan_sp =
949         func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
950             m_thread);
951     if (unwind_plan_sp) {
952       UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
953                           unwind_plan_sp->GetSourceName().GetCString());
954       return unwind_plan_sp;
955     }
956   }
957
958   // If nothing else, use the architectural default UnwindPlan and hope that
959   // does the job.
960   if (arch_default_unwind_plan_sp)
961     UnwindLogMsgVerbose(
962         "frame uses %s for full UnwindPlan",
963         arch_default_unwind_plan_sp->GetSourceName().GetCString());
964   else
965     UnwindLogMsg(
966         "Unable to find any UnwindPlan for full unwind of this frame.");
967
968   return arch_default_unwind_plan_sp;
969 }
970
971 void RegisterContextLLDB::InvalidateAllRegisters() {
972   m_frame_type = eNotAValidFrame;
973 }
974
975 size_t RegisterContextLLDB::GetRegisterCount() {
976   return m_thread.GetRegisterContext()->GetRegisterCount();
977 }
978
979 const RegisterInfo *RegisterContextLLDB::GetRegisterInfoAtIndex(size_t reg) {
980   return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex(reg);
981 }
982
983 size_t RegisterContextLLDB::GetRegisterSetCount() {
984   return m_thread.GetRegisterContext()->GetRegisterSetCount();
985 }
986
987 const RegisterSet *RegisterContextLLDB::GetRegisterSet(size_t reg_set) {
988   return m_thread.GetRegisterContext()->GetRegisterSet(reg_set);
989 }
990
991 uint32_t RegisterContextLLDB::ConvertRegisterKindToRegisterNumber(
992     lldb::RegisterKind kind, uint32_t num) {
993   return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
994       kind, num);
995 }
996
997 bool RegisterContextLLDB::ReadRegisterValueFromRegisterLocation(
998     lldb_private::UnwindLLDB::RegisterLocation regloc,
999     const RegisterInfo *reg_info, RegisterValue &value) {
1000   if (!IsValid())
1001     return false;
1002   bool success = false;
1003
1004   switch (regloc.type) {
1005   case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: {
1006     const RegisterInfo *other_reg_info =
1007         GetRegisterInfoAtIndex(regloc.location.register_number);
1008
1009     if (!other_reg_info)
1010       return false;
1011
1012     success =
1013         m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1014   } break;
1015   case UnwindLLDB::RegisterLocation::eRegisterInRegister: {
1016     const RegisterInfo *other_reg_info =
1017         GetRegisterInfoAtIndex(regloc.location.register_number);
1018
1019     if (!other_reg_info)
1020       return false;
1021
1022     if (IsFrameZero()) {
1023       success =
1024           m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1025     } else {
1026       success = GetNextFrame()->ReadRegister(other_reg_info, value);
1027     }
1028   } break;
1029   case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1030     success =
1031         value.SetUInt(regloc.location.inferred_value, reg_info->byte_size);
1032     break;
1033
1034   case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1035     break;
1036   case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1037     llvm_unreachable("FIXME debugger inferior function call unwind");
1038   case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: {
1039     Status error(ReadRegisterValueFromMemory(
1040         reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1041         value));
1042     success = error.Success();
1043   } break;
1044   default:
1045     llvm_unreachable("Unknown RegisterLocation type.");
1046   }
1047   return success;
1048 }
1049
1050 bool RegisterContextLLDB::WriteRegisterValueToRegisterLocation(
1051     lldb_private::UnwindLLDB::RegisterLocation regloc,
1052     const RegisterInfo *reg_info, const RegisterValue &value) {
1053   if (!IsValid())
1054     return false;
1055
1056   bool success = false;
1057
1058   switch (regloc.type) {
1059   case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: {
1060     const RegisterInfo *other_reg_info =
1061         GetRegisterInfoAtIndex(regloc.location.register_number);
1062     success =
1063         m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1064   } break;
1065   case UnwindLLDB::RegisterLocation::eRegisterInRegister: {
1066     const RegisterInfo *other_reg_info =
1067         GetRegisterInfoAtIndex(regloc.location.register_number);
1068     if (IsFrameZero()) {
1069       success =
1070           m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1071     } else {
1072       success = GetNextFrame()->WriteRegister(other_reg_info, value);
1073     }
1074   } break;
1075   case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1076   case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1077     break;
1078   case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1079     llvm_unreachable("FIXME debugger inferior function call unwind");
1080   case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: {
1081     Status error(WriteRegisterValueToMemory(
1082         reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1083         value));
1084     success = error.Success();
1085   } break;
1086   default:
1087     llvm_unreachable("Unknown RegisterLocation type.");
1088   }
1089   return success;
1090 }
1091
1092 bool RegisterContextLLDB::IsValid() const {
1093   return m_frame_type != eNotAValidFrame;
1094 }
1095
1096 // After the final stack frame in a stack walk we'll get one invalid
1097 // (eNotAValidFrame) stack frame -- one past the end of the stack walk.  But
1098 // higher-level code will need to tell the differnece between "the unwind plan
1099 // below this frame failed" versus "we successfully completed the stack walk"
1100 // so this method helps to disambiguate that.
1101
1102 bool RegisterContextLLDB::IsTrapHandlerFrame() const {
1103   return m_frame_type == eTrapHandlerFrame;
1104 }
1105
1106 // A skip frame is a bogus frame on the stack -- but one where we're likely to
1107 // find a real frame farther
1108 // up the stack if we keep looking.  It's always the second frame in an unwind
1109 // (i.e. the first frame after frame zero) where unwinding can be the
1110 // trickiest.  Ideally we'll mark up this frame in some way so the user knows
1111 // we're displaying bad data and we may have skipped one frame of their real
1112 // program in the process of getting back on track.
1113
1114 bool RegisterContextLLDB::IsSkipFrame() const {
1115   return m_frame_type == eSkipFrame;
1116 }
1117
1118 bool RegisterContextLLDB::IsTrapHandlerSymbol(
1119     lldb_private::Process *process,
1120     const lldb_private::SymbolContext &m_sym_ctx) const {
1121   PlatformSP platform_sp(process->GetTarget().GetPlatform());
1122   if (platform_sp) {
1123     const std::vector<ConstString> trap_handler_names(
1124         platform_sp->GetTrapHandlerSymbolNames());
1125     for (ConstString name : trap_handler_names) {
1126       if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1127           (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1128         return true;
1129       }
1130     }
1131   }
1132   const std::vector<ConstString> user_specified_trap_handler_names(
1133       m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames());
1134   for (ConstString name : user_specified_trap_handler_names) {
1135     if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1136         (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1137       return true;
1138     }
1139   }
1140
1141   return false;
1142 }
1143
1144 // Answer the question: Where did THIS frame save the CALLER frame ("previous"
1145 // frame)'s register value?
1146
1147 enum UnwindLLDB::RegisterSearchResult
1148 RegisterContextLLDB::SavedLocationForRegister(
1149     uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc) {
1150   RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1151
1152   // Have we already found this register location?
1153   if (!m_registers.empty()) {
1154     std::map<uint32_t,
1155              lldb_private::UnwindLLDB::RegisterLocation>::const_iterator
1156         iterator;
1157     iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1158     if (iterator != m_registers.end()) {
1159       regloc = iterator->second;
1160       UnwindLogMsg("supplying caller's saved %s (%d)'s location, cached",
1161                    regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1162       return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1163     }
1164   }
1165
1166   // Look through the available UnwindPlans for the register location.
1167
1168   UnwindPlan::Row::RegisterLocation unwindplan_regloc;
1169   bool have_unwindplan_regloc = false;
1170   RegisterKind unwindplan_registerkind = kNumRegisterKinds;
1171
1172   if (m_fast_unwind_plan_sp) {
1173     UnwindPlan::RowSP active_row =
1174         m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1175     unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind();
1176     if (regnum.GetAsKind(unwindplan_registerkind) == LLDB_INVALID_REGNUM) {
1177       UnwindLogMsg("could not convert lldb regnum %s (%d) into %d RegisterKind "
1178                    "reg numbering scheme",
1179                    regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1180                    (int)unwindplan_registerkind);
1181       return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1182     }
1183     if (active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
1184                                     unwindplan_regloc)) {
1185       UnwindLogMsg(
1186           "supplying caller's saved %s (%d)'s location using FastUnwindPlan",
1187           regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1188       have_unwindplan_regloc = true;
1189     }
1190   }
1191
1192   if (!have_unwindplan_regloc) {
1193     // m_full_unwind_plan_sp being NULL means that we haven't tried to find a
1194     // full UnwindPlan yet
1195     if (!m_full_unwind_plan_sp)
1196       m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
1197
1198     if (m_full_unwind_plan_sp) {
1199       RegisterNumber pc_regnum(m_thread, eRegisterKindGeneric,
1200                                LLDB_REGNUM_GENERIC_PC);
1201
1202       UnwindPlan::RowSP active_row =
1203           m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1204       unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind();
1205
1206       RegisterNumber return_address_reg;
1207
1208       // If we're fetching the saved pc and this UnwindPlan defines a
1209       // ReturnAddress register (e.g. lr on arm), look for the return address
1210       // register number in the UnwindPlan's row.
1211       if (pc_regnum.IsValid() && pc_regnum == regnum &&
1212           m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1213               LLDB_INVALID_REGNUM) {
1214
1215         return_address_reg.init(
1216             m_thread, m_full_unwind_plan_sp->GetRegisterKind(),
1217             m_full_unwind_plan_sp->GetReturnAddressRegister());
1218         regnum = return_address_reg;
1219         UnwindLogMsg("requested caller's saved PC but this UnwindPlan uses a "
1220                      "RA reg; getting %s (%d) instead",
1221                      return_address_reg.GetName(),
1222                      return_address_reg.GetAsKind(eRegisterKindLLDB));
1223       } else {
1224         if (regnum.GetAsKind(unwindplan_registerkind) == LLDB_INVALID_REGNUM) {
1225           if (unwindplan_registerkind == eRegisterKindGeneric) {
1226             UnwindLogMsg("could not convert lldb regnum %s (%d) into "
1227                          "eRegisterKindGeneric reg numbering scheme",
1228                          regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1229           } else {
1230             UnwindLogMsg("could not convert lldb regnum %s (%d) into %d "
1231                          "RegisterKind reg numbering scheme",
1232                          regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1233                          (int)unwindplan_registerkind);
1234           }
1235           return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1236         }
1237       }
1238
1239       if (regnum.IsValid() &&
1240           active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
1241                                       unwindplan_regloc)) {
1242         have_unwindplan_regloc = true;
1243         UnwindLogMsg(
1244             "supplying caller's saved %s (%d)'s location using %s UnwindPlan",
1245             regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1246             m_full_unwind_plan_sp->GetSourceName().GetCString());
1247       }
1248
1249       // This is frame 0 and we're retrieving the PC and it's saved in a Return
1250       // Address register and it hasn't been saved anywhere yet -- that is,
1251       // it's still live in the actual register. Handle this specially.
1252
1253       if (!have_unwindplan_regloc && return_address_reg.IsValid() &&
1254           IsFrameZero()) {
1255         if (return_address_reg.GetAsKind(eRegisterKindLLDB) !=
1256             LLDB_INVALID_REGNUM) {
1257           lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1258           new_regloc.type =
1259               UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1260           new_regloc.location.register_number =
1261               return_address_reg.GetAsKind(eRegisterKindLLDB);
1262           m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1263           regloc = new_regloc;
1264           UnwindLogMsg("supplying caller's register %s (%d) from the live "
1265                        "RegisterContext at frame 0, saved in %d",
1266                        return_address_reg.GetName(),
1267                        return_address_reg.GetAsKind(eRegisterKindLLDB),
1268                        return_address_reg.GetAsKind(eRegisterKindLLDB));
1269           return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1270         }
1271       }
1272
1273       // If this architecture stores the return address in a register (it
1274       // defines a Return Address register) and we're on a non-zero stack frame
1275       // and the Full UnwindPlan says that the pc is stored in the
1276       // RA registers (e.g. lr on arm), then we know that the full unwindplan is
1277       // not trustworthy -- this
1278       // is an impossible situation and the instruction emulation code has
1279       // likely been misled. If this stack frame meets those criteria, we need
1280       // to throw away the Full UnwindPlan that the instruction emulation came
1281       // up with and fall back to the architecture's Default UnwindPlan so the
1282       // stack walk can get past this point.
1283
1284       // Special note:  If the Full UnwindPlan was generated from the compiler,
1285       // don't second-guess it when we're at a call site location.
1286
1287       // arch_default_ra_regnum is the return address register # in the Full
1288       // UnwindPlan register numbering
1289       RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1290                                             LLDB_REGNUM_GENERIC_RA);
1291
1292       if (arch_default_ra_regnum.GetAsKind(unwindplan_registerkind) !=
1293               LLDB_INVALID_REGNUM &&
1294           pc_regnum == regnum && unwindplan_regloc.IsInOtherRegister() &&
1295           unwindplan_regloc.GetRegisterNumber() ==
1296               arch_default_ra_regnum.GetAsKind(unwindplan_registerkind) &&
1297           m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes &&
1298           !m_all_registers_available) {
1299         UnwindLogMsg("%s UnwindPlan tried to restore the pc from the link "
1300                      "register but this is a non-zero frame",
1301                      m_full_unwind_plan_sp->GetSourceName().GetCString());
1302
1303         // Throw away the full unwindplan; install the arch default unwindplan
1304         if (ForceSwitchToFallbackUnwindPlan()) {
1305           // Update for the possibly new unwind plan
1306           unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind();
1307           UnwindPlan::RowSP active_row =
1308               m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1309
1310           // Sanity check: Verify that we can fetch a pc value and CFA value
1311           // with this unwind plan
1312
1313           RegisterNumber arch_default_pc_reg(m_thread, eRegisterKindGeneric,
1314                                              LLDB_REGNUM_GENERIC_PC);
1315           bool can_fetch_pc_value = false;
1316           bool can_fetch_cfa = false;
1317           addr_t cfa_value;
1318           if (active_row) {
1319             if (arch_default_pc_reg.GetAsKind(unwindplan_registerkind) !=
1320                     LLDB_INVALID_REGNUM &&
1321                 active_row->GetRegisterInfo(
1322                     arch_default_pc_reg.GetAsKind(unwindplan_registerkind),
1323                     unwindplan_regloc)) {
1324               can_fetch_pc_value = true;
1325             }
1326             if (ReadFrameAddress(unwindplan_registerkind,
1327                                  active_row->GetCFAValue(), cfa_value)) {
1328               can_fetch_cfa = true;
1329             }
1330           }
1331
1332           have_unwindplan_regloc = can_fetch_pc_value && can_fetch_cfa;
1333         } else {
1334           // We were unable to fall back to another unwind plan
1335           have_unwindplan_regloc = false;
1336         }
1337       }
1338     }
1339   }
1340
1341   ExecutionContext exe_ctx(m_thread.shared_from_this());
1342   Process *process = exe_ctx.GetProcessPtr();
1343   if (!have_unwindplan_regloc) {
1344     // If the UnwindPlan failed to give us an unwind location for this
1345     // register, we may be able to fall back to some ABI-defined default.  For
1346     // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1347     // the ABI may set volatile registers to the undefined state.
1348     ABI *abi = process ? process->GetABI().get() : NULL;
1349     if (abi) {
1350       const RegisterInfo *reg_info =
1351           GetRegisterInfoAtIndex(regnum.GetAsKind(eRegisterKindLLDB));
1352       if (reg_info &&
1353           abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1354         UnwindLogMsg(
1355             "supplying caller's saved %s (%d)'s location using ABI default",
1356             regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1357         have_unwindplan_regloc = true;
1358       }
1359     }
1360   }
1361
1362   if (!have_unwindplan_regloc) {
1363     if (IsFrameZero()) {
1364       // This is frame 0 - we should return the actual live register context
1365       // value
1366       lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1367       new_regloc.type =
1368           UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1369       new_regloc.location.register_number = regnum.GetAsKind(eRegisterKindLLDB);
1370       m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1371       regloc = new_regloc;
1372       UnwindLogMsg("supplying caller's register %s (%d) from the live "
1373                    "RegisterContext at frame 0",
1374                    regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1375       return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1376     } else {
1377       std::string unwindplan_name("");
1378       if (m_full_unwind_plan_sp) {
1379         unwindplan_name += "via '";
1380         unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString();
1381         unwindplan_name += "'";
1382       }
1383       UnwindLogMsg("no save location for %s (%d) %s", regnum.GetName(),
1384                    regnum.GetAsKind(eRegisterKindLLDB),
1385                    unwindplan_name.c_str());
1386     }
1387     return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1388   }
1389
1390   // unwindplan_regloc has valid contents about where to retrieve the register
1391   if (unwindplan_regloc.IsUnspecified()) {
1392     lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1393     new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved;
1394     m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1395     UnwindLogMsg("save location for %s (%d) is unspecified, continue searching",
1396                  regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1397     return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1398   }
1399
1400   if (unwindplan_regloc.IsUndefined()) {
1401     UnwindLogMsg(
1402         "did not supply reg location for %s (%d) because it is volatile",
1403         regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1404     return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile;
1405   }
1406
1407   if (unwindplan_regloc.IsSame()) {
1408     if (!IsFrameZero() &&
1409         (regnum.GetAsKind(eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_PC ||
1410          regnum.GetAsKind(eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_RA)) {
1411       UnwindLogMsg("register %s (%d) is marked as 'IsSame' - it is a pc or "
1412                    "return address reg on a non-zero frame -- treat as if we "
1413                    "have no information",
1414                    regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1415       return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1416     } else {
1417       regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1418       regloc.location.register_number = regnum.GetAsKind(eRegisterKindLLDB);
1419       m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1420       UnwindLogMsg(
1421           "supplying caller's register %s (%d), saved in register %s (%d)",
1422           regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1423           regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1424       return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1425     }
1426   }
1427
1428   if (unwindplan_regloc.IsCFAPlusOffset()) {
1429     int offset = unwindplan_regloc.GetOffset();
1430     regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1431     regloc.location.inferred_value = m_cfa + offset;
1432     m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1433     UnwindLogMsg("supplying caller's register %s (%d), value is CFA plus "
1434                  "offset %d [value is 0x%" PRIx64 "]",
1435                  regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1436                  regloc.location.inferred_value);
1437     return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1438   }
1439
1440   if (unwindplan_regloc.IsAtCFAPlusOffset()) {
1441     int offset = unwindplan_regloc.GetOffset();
1442     regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1443     regloc.location.target_memory_location = m_cfa + offset;
1444     m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1445     UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1446                  "CFA plus offset %d [saved at 0x%" PRIx64 "]",
1447                  regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1448                  regloc.location.target_memory_location);
1449     return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1450   }
1451
1452   if (unwindplan_regloc.IsAFAPlusOffset()) {
1453     if (m_afa == LLDB_INVALID_ADDRESS)
1454         return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1455
1456     int offset = unwindplan_regloc.GetOffset();
1457     regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1458     regloc.location.inferred_value = m_afa + offset;
1459     m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1460     UnwindLogMsg("supplying caller's register %s (%d), value is AFA plus "
1461                  "offset %d [value is 0x%" PRIx64 "]",
1462                  regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1463                  regloc.location.inferred_value);
1464     return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1465   }
1466
1467   if (unwindplan_regloc.IsAtAFAPlusOffset()) {
1468     if (m_afa == LLDB_INVALID_ADDRESS)
1469         return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1470
1471     int offset = unwindplan_regloc.GetOffset();
1472     regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1473     regloc.location.target_memory_location = m_afa + offset;
1474     m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1475     UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1476                  "AFA plus offset %d [saved at 0x%" PRIx64 "]",
1477                  regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1478                  regloc.location.target_memory_location);
1479     return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1480   }
1481
1482   if (unwindplan_regloc.IsInOtherRegister()) {
1483     uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber();
1484     RegisterNumber row_regnum(m_thread, unwindplan_registerkind,
1485                               unwindplan_regnum);
1486     if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1487       UnwindLogMsg("could not supply caller's %s (%d) location - was saved in "
1488                    "another reg but couldn't convert that regnum",
1489                    regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1490       return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1491     }
1492     regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1493     regloc.location.register_number = row_regnum.GetAsKind(eRegisterKindLLDB);
1494     m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1495     UnwindLogMsg(
1496         "supplying caller's register %s (%d), saved in register %s (%d)",
1497         regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1498         row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1499     return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1500   }
1501
1502   if (unwindplan_regloc.IsDWARFExpression() ||
1503       unwindplan_regloc.IsAtDWARFExpression()) {
1504     DataExtractor dwarfdata(unwindplan_regloc.GetDWARFExpressionBytes(),
1505                             unwindplan_regloc.GetDWARFExpressionLength(),
1506                             process->GetByteOrder(),
1507                             process->GetAddressByteSize());
1508     ModuleSP opcode_ctx;
1509     DWARFExpression dwarfexpr(opcode_ctx, dwarfdata, nullptr, 0,
1510                               unwindplan_regloc.GetDWARFExpressionLength());
1511     dwarfexpr.SetRegisterKind(unwindplan_registerkind);
1512     Value result;
1513     Status error;
1514     if (dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr, result,
1515                            &error)) {
1516       addr_t val;
1517       val = result.GetScalar().ULongLong();
1518       if (unwindplan_regloc.IsDWARFExpression()) {
1519         regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1520         regloc.location.inferred_value = val;
1521         m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1522         UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1523                      "(IsDWARFExpression)",
1524                      regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1525         return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1526       } else {
1527         regloc.type =
1528             UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1529         regloc.location.target_memory_location = val;
1530         m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1531         UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1532                      "(IsAtDWARFExpression)",
1533                      regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1534         return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1535       }
1536     }
1537     UnwindLogMsg("tried to use IsDWARFExpression or IsAtDWARFExpression for %s "
1538                  "(%d) but failed",
1539                  regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1540     return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1541   }
1542
1543   UnwindLogMsg("no save location for %s (%d) in this stack frame",
1544                regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1545
1546   // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1547   // unsupported.
1548
1549   return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1550 }
1551
1552 // TryFallbackUnwindPlan() -- this method is a little tricky.
1553 //
1554 // When this is called, the frame above -- the caller frame, the "previous"
1555 // frame -- is invalid or bad.
1556 //
1557 // Instead of stopping the stack walk here, we'll try a different UnwindPlan
1558 // and see if we can get a valid frame above us.
1559 //
1560 // This most often happens when an unwind plan based on assembly instruction
1561 // inspection is not correct -- mostly with hand-written assembly functions or
1562 // functions where the stack frame is set up "out of band", e.g. the kernel
1563 // saved the register context and then called an asynchronous trap handler like
1564 // _sigtramp.
1565 //
1566 // Often in these cases, if we just do a dumb stack walk we'll get past this
1567 // tricky frame and our usual techniques can continue to be used.
1568
1569 bool RegisterContextLLDB::TryFallbackUnwindPlan() {
1570   if (m_fallback_unwind_plan_sp.get() == nullptr)
1571     return false;
1572
1573   if (m_full_unwind_plan_sp.get() == nullptr)
1574     return false;
1575
1576   if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
1577       m_full_unwind_plan_sp->GetSourceName() ==
1578           m_fallback_unwind_plan_sp->GetSourceName()) {
1579     return false;
1580   }
1581
1582   // If a compiler generated unwind plan failed, trying the arch default
1583   // unwindplan isn't going to do any better.
1584   if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1585     return false;
1586
1587   // Get the caller's pc value and our own CFA value. Swap in the fallback
1588   // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1589   // same, then the fallback unwind plan provides no benefit.
1590
1591   RegisterNumber pc_regnum(m_thread, eRegisterKindGeneric,
1592                            LLDB_REGNUM_GENERIC_PC);
1593
1594   addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1595   addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1596   UnwindLLDB::RegisterLocation regloc;
1597   if (SavedLocationForRegister(pc_regnum.GetAsKind(eRegisterKindLLDB),
1598                                regloc) ==
1599       UnwindLLDB::RegisterSearchResult::eRegisterFound) {
1600     const RegisterInfo *reg_info =
1601         GetRegisterInfoAtIndex(pc_regnum.GetAsKind(eRegisterKindLLDB));
1602     if (reg_info) {
1603       RegisterValue reg_value;
1604       if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1605         old_caller_pc_value = reg_value.GetAsUInt64();
1606       }
1607     }
1608   }
1609
1610   // This is a tricky wrinkle!  If SavedLocationForRegister() detects a really
1611   // impossible register location for the full unwind plan, it may call
1612   // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1613   // unwindplan with the fallback... in short, we're done, we're using the
1614   // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1615   // at the top -- the only way it became nullptr since then is via
1616   // SavedLocationForRegister().
1617   if (m_fallback_unwind_plan_sp.get() == nullptr)
1618     return true;
1619
1620   // Switch the full UnwindPlan to be the fallback UnwindPlan.  If we decide
1621   // this isn't working, we need to restore. We'll also need to save & restore
1622   // the value of the m_cfa ivar.  Save is down below a bit in 'old_cfa'.
1623   UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp;
1624   addr_t old_cfa = m_cfa;
1625   addr_t old_afa = m_afa;
1626
1627   m_registers.clear();
1628
1629   m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1630
1631   UnwindPlan::RowSP active_row =
1632       m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1633
1634   if (active_row &&
1635       active_row->GetCFAValue().GetValueType() !=
1636           UnwindPlan::Row::FAValue::unspecified) {
1637     addr_t new_cfa;
1638     if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1639                             active_row->GetCFAValue(), new_cfa) ||
1640         new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) {
1641       UnwindLogMsg("failed to get cfa with fallback unwindplan");
1642       m_fallback_unwind_plan_sp.reset();
1643       m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1644       return false;
1645     }
1646     m_cfa = new_cfa;
1647
1648     ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1649                      active_row->GetAFAValue(), m_afa);
1650
1651     if (SavedLocationForRegister(pc_regnum.GetAsKind(eRegisterKindLLDB),
1652                                  regloc) ==
1653         UnwindLLDB::RegisterSearchResult::eRegisterFound) {
1654       const RegisterInfo *reg_info =
1655           GetRegisterInfoAtIndex(pc_regnum.GetAsKind(eRegisterKindLLDB));
1656       if (reg_info) {
1657         RegisterValue reg_value;
1658         if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
1659                                                   reg_value)) {
1660           new_caller_pc_value = reg_value.GetAsUInt64();
1661         }
1662       }
1663     }
1664
1665     if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
1666       UnwindLogMsg("failed to get a pc value for the caller frame with the "
1667                    "fallback unwind plan");
1668       m_fallback_unwind_plan_sp.reset();
1669       m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1670       m_cfa = old_cfa;
1671       m_afa = old_afa;
1672       return false;
1673     }
1674
1675     if (old_caller_pc_value == new_caller_pc_value &&
1676         m_cfa == old_cfa &&
1677         m_afa == old_afa) {
1678       UnwindLogMsg("fallback unwind plan got the same values for this frame "
1679                    "CFA and caller frame pc, not using");
1680       m_fallback_unwind_plan_sp.reset();
1681       m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1682       return false;
1683     }
1684
1685     UnwindLogMsg("trying to unwind from this function with the UnwindPlan '%s' "
1686                  "because UnwindPlan '%s' failed.",
1687                  m_fallback_unwind_plan_sp->GetSourceName().GetCString(),
1688                  original_full_unwind_plan_sp->GetSourceName().GetCString());
1689
1690     // We've copied the fallback unwind plan into the full - now clear the
1691     // fallback.
1692     m_fallback_unwind_plan_sp.reset();
1693   }
1694
1695   return true;
1696 }
1697
1698 bool RegisterContextLLDB::ForceSwitchToFallbackUnwindPlan() {
1699   if (m_fallback_unwind_plan_sp.get() == NULL)
1700     return false;
1701
1702   if (m_full_unwind_plan_sp.get() == NULL)
1703     return false;
1704
1705   if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
1706       m_full_unwind_plan_sp->GetSourceName() ==
1707           m_fallback_unwind_plan_sp->GetSourceName()) {
1708     return false;
1709   }
1710
1711   UnwindPlan::RowSP active_row =
1712       m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1713
1714   if (active_row &&
1715       active_row->GetCFAValue().GetValueType() !=
1716           UnwindPlan::Row::FAValue::unspecified) {
1717     addr_t new_cfa;
1718     if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1719                             active_row->GetCFAValue(), new_cfa) ||
1720         new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) {
1721       UnwindLogMsg("failed to get cfa with fallback unwindplan");
1722       m_fallback_unwind_plan_sp.reset();
1723       return false;
1724     }
1725
1726     ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1727                      active_row->GetAFAValue(), m_afa);
1728
1729     m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1730     m_fallback_unwind_plan_sp.reset();
1731
1732     m_registers.clear();
1733
1734     m_cfa = new_cfa;
1735
1736     UnwindLogMsg("switched unconditionally to the fallback unwindplan %s",
1737                  m_full_unwind_plan_sp->GetSourceName().GetCString());
1738     return true;
1739   }
1740   return false;
1741 }
1742
1743 bool RegisterContextLLDB::ReadFrameAddress(
1744     lldb::RegisterKind row_register_kind, UnwindPlan::Row::FAValue &fa,
1745     addr_t &address) {
1746   RegisterValue reg_value;
1747
1748   address = LLDB_INVALID_ADDRESS;
1749   addr_t cfa_reg_contents;
1750
1751   switch (fa.GetValueType()) {
1752   case UnwindPlan::Row::FAValue::isRegisterDereferenced: {
1753     RegisterNumber cfa_reg(m_thread, row_register_kind,
1754                            fa.GetRegisterNumber());
1755     if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
1756       const RegisterInfo *reg_info =
1757           GetRegisterInfoAtIndex(cfa_reg.GetAsKind(eRegisterKindLLDB));
1758       RegisterValue reg_value;
1759       if (reg_info) {
1760         Status error = ReadRegisterValueFromMemory(
1761             reg_info, cfa_reg_contents, reg_info->byte_size, reg_value);
1762         if (error.Success()) {
1763           address = reg_value.GetAsUInt64();
1764           UnwindLogMsg(
1765               "CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64
1766               ", CFA value is 0x%" PRIx64,
1767               cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1768               cfa_reg_contents, address);
1769           return true;
1770         } else {
1771           UnwindLogMsg("Tried to deref reg %s (%d) [0x%" PRIx64
1772                        "] but memory read failed.",
1773                        cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1774                        cfa_reg_contents);
1775         }
1776       }
1777     }
1778     break;
1779   }
1780   case UnwindPlan::Row::FAValue::isRegisterPlusOffset: {
1781     RegisterNumber cfa_reg(m_thread, row_register_kind,
1782                            fa.GetRegisterNumber());
1783     if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
1784       if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 ||
1785           cfa_reg_contents == 1) {
1786         UnwindLogMsg(
1787             "Got an invalid CFA register value - reg %s (%d), value 0x%" PRIx64,
1788             cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1789             cfa_reg_contents);
1790         cfa_reg_contents = LLDB_INVALID_ADDRESS;
1791         return false;
1792       }
1793       address = cfa_reg_contents + fa.GetOffset();
1794       UnwindLogMsg(
1795           "CFA is 0x%" PRIx64 ": Register %s (%d) contents are 0x%" PRIx64
1796           ", offset is %d",
1797           address, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1798           cfa_reg_contents, fa.GetOffset());
1799       return true;
1800     }
1801     break;
1802   }
1803   case UnwindPlan::Row::FAValue::isDWARFExpression: {
1804     ExecutionContext exe_ctx(m_thread.shared_from_this());
1805     Process *process = exe_ctx.GetProcessPtr();
1806     DataExtractor dwarfdata(fa.GetDWARFExpressionBytes(),
1807                             fa.GetDWARFExpressionLength(),
1808                             process->GetByteOrder(),
1809                             process->GetAddressByteSize());
1810     ModuleSP opcode_ctx;
1811     DWARFExpression dwarfexpr(opcode_ctx, dwarfdata, nullptr, 0,
1812                               fa.GetDWARFExpressionLength());
1813     dwarfexpr.SetRegisterKind(row_register_kind);
1814     Value result;
1815     Status error;
1816     if (dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr, result,
1817                            &error)) {
1818       address = result.GetScalar().ULongLong();
1819
1820       UnwindLogMsg("CFA value set by DWARF expression is 0x%" PRIx64,
1821                    address);
1822       return true;
1823     }
1824     UnwindLogMsg("Failed to set CFA value via DWARF expression: %s",
1825                  error.AsCString());
1826     break;
1827   }
1828   default:
1829     return false;
1830   }
1831   return false;
1832 }
1833
1834 // Retrieve a general purpose register value for THIS frame, as saved by the
1835 // NEXT frame, i.e. the frame that
1836 // this frame called.  e.g.
1837 //
1838 //  foo () { }
1839 //  bar () { foo (); }
1840 //  main () { bar (); }
1841 //
1842 //  stopped in foo() so
1843 //     frame 0 - foo
1844 //     frame 1 - bar
1845 //     frame 2 - main
1846 //  and this RegisterContext is for frame 1 (bar) - if we want to get the pc
1847 //  value for frame 1, we need to ask
1848 //  where frame 0 (the "next" frame) saved that and retrieve the value.
1849
1850 bool RegisterContextLLDB::ReadGPRValue(lldb::RegisterKind register_kind,
1851                                        uint32_t regnum, addr_t &value) {
1852   if (!IsValid())
1853     return false;
1854
1855   uint32_t lldb_regnum;
1856   if (register_kind == eRegisterKindLLDB) {
1857     lldb_regnum = regnum;
1858   } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
1859                  register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
1860     return false;
1861   }
1862
1863   const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
1864   RegisterValue reg_value;
1865   // if this is frame 0 (currently executing frame), get the requested reg
1866   // contents from the actual thread registers
1867   if (IsFrameZero()) {
1868     if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
1869       value = reg_value.GetAsUInt64();
1870       return true;
1871     }
1872     return false;
1873   }
1874
1875   bool pc_register = false;
1876   uint32_t generic_regnum;
1877   if (register_kind == eRegisterKindGeneric &&
1878       (regnum == LLDB_REGNUM_GENERIC_PC || regnum == LLDB_REGNUM_GENERIC_RA)) {
1879     pc_register = true;
1880   } else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
1881                  register_kind, regnum, eRegisterKindGeneric, generic_regnum) &&
1882              (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
1883               generic_regnum == LLDB_REGNUM_GENERIC_RA)) {
1884     pc_register = true;
1885   }
1886
1887   lldb_private::UnwindLLDB::RegisterLocation regloc;
1888   if (!m_parent_unwind.SearchForSavedLocationForRegister(
1889           lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
1890     return false;
1891   }
1892   if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1893     value = reg_value.GetAsUInt64();
1894     return true;
1895   }
1896   return false;
1897 }
1898
1899 bool RegisterContextLLDB::ReadGPRValue(const RegisterNumber &regnum,
1900                                        addr_t &value) {
1901   return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
1902                       value);
1903 }
1904
1905 // Find the value of a register in THIS frame
1906
1907 bool RegisterContextLLDB::ReadRegister(const RegisterInfo *reg_info,
1908                                        RegisterValue &value) {
1909   if (!IsValid())
1910     return false;
1911
1912   const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1913   UnwindLogMsgVerbose("looking for register saved location for reg %d",
1914                       lldb_regnum);
1915
1916   // If this is the 0th frame, hand this over to the live register context
1917   if (IsFrameZero()) {
1918     UnwindLogMsgVerbose("passing along to the live register context for reg %d",
1919                         lldb_regnum);
1920     return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
1921   }
1922
1923   bool is_pc_regnum = false;
1924   if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_PC ||
1925       reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA) {
1926     is_pc_regnum = true;
1927   }
1928
1929   lldb_private::UnwindLLDB::RegisterLocation regloc;
1930   // Find out where the NEXT frame saved THIS frame's register contents
1931   if (!m_parent_unwind.SearchForSavedLocationForRegister(
1932           lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
1933     return false;
1934
1935   return ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
1936 }
1937
1938 bool RegisterContextLLDB::WriteRegister(const RegisterInfo *reg_info,
1939                                         const RegisterValue &value) {
1940   if (!IsValid())
1941     return false;
1942
1943   const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1944   UnwindLogMsgVerbose("looking for register saved location for reg %d",
1945                       lldb_regnum);
1946
1947   // If this is the 0th frame, hand this over to the live register context
1948   if (IsFrameZero()) {
1949     UnwindLogMsgVerbose("passing along to the live register context for reg %d",
1950                         lldb_regnum);
1951     return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
1952   }
1953
1954   lldb_private::UnwindLLDB::RegisterLocation regloc;
1955   // Find out where the NEXT frame saved THIS frame's register contents
1956   if (!m_parent_unwind.SearchForSavedLocationForRegister(
1957           lldb_regnum, regloc, m_frame_number - 1, false))
1958     return false;
1959
1960   return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
1961 }
1962
1963 // Don't need to implement this one
1964 bool RegisterContextLLDB::ReadAllRegisterValues(lldb::DataBufferSP &data_sp) {
1965   return false;
1966 }
1967
1968 // Don't need to implement this one
1969 bool RegisterContextLLDB::WriteAllRegisterValues(
1970     const lldb::DataBufferSP &data_sp) {
1971   return false;
1972 }
1973
1974 // Retrieve the pc value for THIS from
1975
1976 bool RegisterContextLLDB::GetCFA(addr_t &cfa) {
1977   if (!IsValid()) {
1978     return false;
1979   }
1980   if (m_cfa == LLDB_INVALID_ADDRESS) {
1981     return false;
1982   }
1983   cfa = m_cfa;
1984   return true;
1985 }
1986
1987 RegisterContextLLDB::SharedPtr RegisterContextLLDB::GetNextFrame() const {
1988   RegisterContextLLDB::SharedPtr regctx;
1989   if (m_frame_number == 0)
1990     return regctx;
1991   return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
1992 }
1993
1994 RegisterContextLLDB::SharedPtr RegisterContextLLDB::GetPrevFrame() const {
1995   RegisterContextLLDB::SharedPtr regctx;
1996   return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number + 1);
1997 }
1998
1999 // Retrieve the address of the start of the function of THIS frame
2000
2001 bool RegisterContextLLDB::GetStartPC(addr_t &start_pc) {
2002   if (!IsValid())
2003     return false;
2004
2005   if (!m_start_pc.IsValid()) {
2006         bool read_successfully = ReadPC (start_pc);
2007         if (read_successfully)
2008         {
2009             ProcessSP process_sp (m_thread.GetProcess());
2010             if (process_sp)
2011             {
2012                 ABI *abi = process_sp->GetABI().get();
2013                 if (abi)
2014                     start_pc = abi->FixCodeAddress(start_pc);
2015             }
2016         }
2017         return read_successfully;
2018   }
2019   start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2020   return true;
2021 }
2022
2023 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2024
2025 bool RegisterContextLLDB::ReadPC(addr_t &pc) {
2026   if (!IsValid())
2027     return false;
2028
2029   bool above_trap_handler = false;
2030   if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2031       GetNextFrame()->IsTrapHandlerFrame())
2032     above_trap_handler = true;
2033
2034   if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) {
2035     // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2036     // indicates the end of a stack walk.
2037     // On the currently executing frame (or such a frame interrupted
2038     // asynchronously by sigtramp et al) this may occur if code has jumped
2039     // through a NULL pointer -- we want to be able to unwind past that frame
2040     // to help find the bug.
2041
2042     ProcessSP process_sp (m_thread.GetProcess());
2043     if (process_sp)
2044     {
2045         ABI *abi = process_sp->GetABI().get();
2046         if (abi)
2047             pc = abi->FixCodeAddress(pc);
2048     }
2049
2050     return !(m_all_registers_available == false &&
2051              above_trap_handler == false && (pc == 0 || pc == 1));
2052   } else {
2053     return false;
2054   }
2055 }
2056
2057 void RegisterContextLLDB::UnwindLogMsg(const char *fmt, ...) {
2058   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
2059   if (log) {
2060     va_list args;
2061     va_start(args, fmt);
2062
2063     char *logmsg;
2064     if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == NULL) {
2065       if (logmsg)
2066         free(logmsg);
2067       va_end(args);
2068       return;
2069     }
2070     va_end(args);
2071
2072     log->Printf("%*sth%d/fr%u %s", m_frame_number < 100 ? m_frame_number : 100,
2073                 "", m_thread.GetIndexID(), m_frame_number, logmsg);
2074     free(logmsg);
2075   }
2076 }
2077
2078 void RegisterContextLLDB::UnwindLogMsgVerbose(const char *fmt, ...) {
2079   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
2080   if (log && log->GetVerbose()) {
2081     va_list args;
2082     va_start(args, fmt);
2083
2084     char *logmsg;
2085     if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == NULL) {
2086       if (logmsg)
2087         free(logmsg);
2088       va_end(args);
2089       return;
2090     }
2091     va_end(args);
2092
2093     log->Printf("%*sth%d/fr%u %s", m_frame_number < 100 ? m_frame_number : 100,
2094                 "", m_thread.GetIndexID(), m_frame_number, logmsg);
2095     free(logmsg);
2096   }
2097 }