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