Bug Summary

File:tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp
Warning:line 1802, column 9
Value stored to 'cfa_reg_contents' is never read

Annotated Source Code

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