File: | tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp |
Location: | line 737, column 17 |
Description: | Called C++ object pointer is null |
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 | ||||
11 | #include "lldb/lldb-private.h" | |||
12 | #include "lldb/Core/Address.h" | |||
13 | #include "lldb/Core/AddressRange.h" | |||
14 | #include "lldb/Core/DataBufferHeap.h" | |||
15 | #include "lldb/Core/Log.h" | |||
16 | #include "lldb/Core/Module.h" | |||
17 | #include "lldb/Core/RegisterValue.h" | |||
18 | #include "lldb/Core/Value.h" | |||
19 | #include "lldb/Expression/DWARFExpression.h" | |||
20 | #include "lldb/Symbol/DWARFCallFrameInfo.h" | |||
21 | #include "lldb/Symbol/FuncUnwinders.h" | |||
22 | #include "lldb/Symbol/Function.h" | |||
23 | #include "lldb/Symbol/ObjectFile.h" | |||
24 | #include "lldb/Symbol/Symbol.h" | |||
25 | #include "lldb/Symbol/SymbolContext.h" | |||
26 | #include "lldb/Target/ABI.h" | |||
27 | #include "lldb/Target/DynamicLoader.h" | |||
28 | #include "lldb/Target/ExecutionContext.h" | |||
29 | #include "lldb/Target/Platform.h" | |||
30 | #include "lldb/Target/Process.h" | |||
31 | #include "lldb/Target/SectionLoadList.h" | |||
32 | #include "lldb/Target/StackFrame.h" | |||
33 | #include "lldb/Target/Target.h" | |||
34 | #include "lldb/Target/Thread.h" | |||
35 | ||||
36 | #include "RegisterContextLLDB.h" | |||
37 | ||||
38 | using namespace lldb; | |||
39 | using namespace lldb_private; | |||
40 | ||||
41 | RegisterContextLLDB::RegisterContextLLDB | |||
42 | ( | |||
43 | Thread& thread, | |||
44 | const SharedPtr &next_frame, | |||
45 | SymbolContext& sym_ctx, | |||
46 | uint32_t frame_number, | |||
47 | UnwindLLDB& unwind_lldb | |||
48 | ) : | |||
49 | RegisterContext (thread, frame_number), | |||
50 | m_thread(thread), | |||
51 | m_fast_unwind_plan_sp (), | |||
52 | m_full_unwind_plan_sp (), | |||
53 | m_fallback_unwind_plan_sp (), | |||
54 | m_all_registers_available(false), | |||
55 | m_frame_type (-1), | |||
56 | m_cfa (LLDB_INVALID_ADDRESS(18446744073709551615UL)), | |||
57 | m_start_pc (), | |||
58 | m_current_pc (), | |||
59 | m_current_offset (0), | |||
60 | m_current_offset_backed_up_one (0), | |||
61 | m_sym_ctx(sym_ctx), | |||
62 | m_sym_ctx_valid (false), | |||
63 | m_frame_number (frame_number), | |||
64 | m_registers(), | |||
65 | m_parent_unwind (unwind_lldb) | |||
66 | { | |||
67 | m_sym_ctx.Clear(false); | |||
68 | m_sym_ctx_valid = false; | |||
69 | ||||
70 | if (IsFrameZero ()) | |||
71 | { | |||
72 | InitializeZerothFrame (); | |||
73 | } | |||
74 | else | |||
75 | { | |||
76 | InitializeNonZerothFrame (); | |||
77 | } | |||
78 | ||||
79 | // This same code exists over in the GetFullUnwindPlanForFrame() but it may not have been executed yet | |||
80 | if (IsFrameZero() | |||
81 | || next_frame->m_frame_type == eTrapHandlerFrame | |||
82 | || next_frame->m_frame_type == eDebuggerFrame) | |||
83 | { | |||
84 | m_all_registers_available = true; | |||
85 | } | |||
86 | } | |||
87 | ||||
88 | bool | |||
89 | RegisterContextLLDB::IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset) | |||
90 | { | |||
91 | if (!unwind_plan_sp) | |||
92 | return false; | |||
93 | ||||
94 | // check if m_current_pc is valid | |||
95 | if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) | |||
96 | { | |||
97 | // yes - current offset can be used as is | |||
98 | valid_pc_offset = m_current_offset; | |||
99 | return true; | |||
100 | } | |||
101 | ||||
102 | // if m_current_offset <= 0, we've got nothing else to try | |||
103 | if (m_current_offset <= 0) | |||
104 | return false; | |||
105 | ||||
106 | // check pc - 1 to see if it's valid | |||
107 | Address pc_minus_one (m_current_pc); | |||
108 | pc_minus_one.SetOffset(m_current_pc.GetOffset() - 1); | |||
109 | if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) | |||
110 | { | |||
111 | // *valid_pc_offset = m_current_offset - 1; | |||
112 | valid_pc_offset = m_current_pc.GetOffset() - 1; | |||
113 | return true; | |||
114 | } | |||
115 | ||||
116 | return false; | |||
117 | } | |||
118 | ||||
119 | // Initialize a RegisterContextLLDB which is the first frame of a stack -- the zeroth frame or currently | |||
120 | // executing frame. | |||
121 | ||||
122 | void | |||
123 | RegisterContextLLDB::InitializeZerothFrame() | |||
124 | { | |||
125 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND(1u << 15))); | |||
126 | ExecutionContext exe_ctx(m_thread.shared_from_this()); | |||
127 | RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext(); | |||
128 | ||||
129 | if (reg_ctx_sp.get() == NULL__null) | |||
130 | { | |||
131 | m_frame_type = eNotAValidFrame; | |||
132 | UnwindLogMsg ("frame does not have a register context"); | |||
133 | return; | |||
134 | } | |||
135 | ||||
136 | addr_t current_pc = reg_ctx_sp->GetPC(); | |||
137 | ||||
138 | if (current_pc == LLDB_INVALID_ADDRESS(18446744073709551615UL)) | |||
139 | { | |||
140 | m_frame_type = eNotAValidFrame; | |||
141 | UnwindLogMsg ("frame does not have a pc"); | |||
142 | return; | |||
143 | } | |||
144 | ||||
145 | Process *process = exe_ctx.GetProcessPtr(); | |||
146 | ||||
147 | // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs | |||
148 | // this will strip bit zero in case we read a PC from memory or from the LR. | |||
149 | // (which would be a no-op in frame 0 where we get it from the register set, | |||
150 | // but still a good idea to make the call here for other ABIs that may exist.) | |||
151 | ABI *abi = process->GetABI().get(); | |||
152 | if (abi) | |||
153 | current_pc = abi->FixCodeAddress(current_pc); | |||
154 | ||||
155 | // Initialize m_current_pc, an Address object, based on current_pc, an addr_t. | |||
156 | m_current_pc.SetLoadAddress (current_pc, &process->GetTarget()); | |||
157 | ||||
158 | // If we don't have a Module for some reason, we're not going to find symbol/function information - just | |||
159 | // stick in some reasonable defaults and hope we can unwind past this frame. | |||
160 | ModuleSP pc_module_sp (m_current_pc.GetModule()); | |||
161 | if (!m_current_pc.IsValid() || !pc_module_sp) | |||
162 | { | |||
163 | UnwindLogMsg ("using architectural default unwind method"); | |||
164 | } | |||
165 | ||||
166 | // We require either a symbol or function in the symbols context to be successfully | |||
167 | // filled in or this context is of no use to us. | |||
168 | const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; | |||
169 | if (pc_module_sp.get() | |||
170 | && (pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, resolve_scope, m_sym_ctx) & resolve_scope)) | |||
171 | { | |||
172 | m_sym_ctx_valid = true; | |||
173 | } | |||
174 | ||||
175 | AddressRange addr_range; | |||
176 | m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range); | |||
177 | ||||
178 | if (IsTrapHandlerSymbol (process, m_sym_ctx)) | |||
179 | { | |||
180 | m_frame_type = eTrapHandlerFrame; | |||
181 | } | |||
182 | else | |||
183 | { | |||
184 | // FIXME: Detect eDebuggerFrame here. | |||
185 | m_frame_type = eNormalFrame; | |||
186 | } | |||
187 | ||||
188 | // If we were able to find a symbol/function, set addr_range to the bounds of that symbol/function. | |||
189 | // else treat the current pc value as the start_pc and record no offset. | |||
190 | if (addr_range.GetBaseAddress().IsValid()) | |||
191 | { | |||
192 | m_start_pc = addr_range.GetBaseAddress(); | |||
193 | if (m_current_pc.GetSection() == m_start_pc.GetSection()) | |||
194 | { | |||
195 | m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset(); | |||
196 | } | |||
197 | else if (m_current_pc.GetModule() == m_start_pc.GetModule()) | |||
198 | { | |||
199 | // This means that whatever symbol we kicked up isn't really correct | |||
200 | // --- we should not cross section boundaries ... We really should NULL out | |||
201 | // the function/symbol in this case unless there is a bad assumption | |||
202 | // here due to inlined functions? | |||
203 | m_current_offset = m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress(); | |||
204 | } | |||
205 | m_current_offset_backed_up_one = m_current_offset; | |||
206 | } | |||
207 | else | |||
208 | { | |||
209 | m_start_pc = m_current_pc; | |||
210 | m_current_offset = -1; | |||
211 | m_current_offset_backed_up_one = -1; | |||
212 | } | |||
213 | ||||
214 | // We've set m_frame_type and m_sym_ctx before these calls. | |||
215 | ||||
216 | m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame (); | |||
217 | m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); | |||
218 | ||||
219 | UnwindPlan::RowSP active_row; | |||
220 | int cfa_offset = 0; | |||
221 | lldb::RegisterKind row_register_kind = eRegisterKindGeneric; | |||
222 | if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc)) | |||
223 | { | |||
224 | active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); | |||
225 | row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); | |||
226 | if (active_row.get() && log) | |||
227 | { | |||
228 | StreamString active_row_strm; | |||
229 | active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); | |||
230 | UnwindLogMsg ("%s", active_row_strm.GetString().c_str()); | |||
231 | } | |||
232 | } | |||
233 | ||||
234 | if (!active_row.get()) | |||
235 | { | |||
236 | UnwindLogMsg ("could not find an unwindplan row for this frame's pc"); | |||
237 | m_frame_type = eNotAValidFrame; | |||
238 | return; | |||
239 | } | |||
240 | ||||
241 | ||||
242 | addr_t cfa_regval = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
243 | if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval)) | |||
244 | { | |||
245 | UnwindLogMsg ("could not read CFA register for this frame."); | |||
246 | m_frame_type = eNotAValidFrame; | |||
247 | return; | |||
248 | } | |||
249 | ||||
250 | cfa_offset = active_row->GetCFAOffset (); | |||
251 | m_cfa = cfa_regval + cfa_offset; | |||
252 | ||||
253 | UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64"l" "x" " (cfa_regval = 0x%16.16" PRIx64"l" "x" ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset); | |||
254 | UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64"l" "x" " cfa is 0x%" PRIx64"l" "x" " using %s UnwindPlan", | |||
255 | (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), | |||
256 | (uint64_t) m_cfa, | |||
257 | m_full_unwind_plan_sp->GetSourceName().GetCString()); | |||
258 | } | |||
259 | ||||
260 | // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the RegisterContextLLDB "below" it | |||
261 | // to provide things like its current pc value. | |||
262 | ||||
263 | void | |||
264 | RegisterContextLLDB::InitializeNonZerothFrame() | |||
265 | { | |||
266 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND(1u << 15))); | |||
267 | if (IsFrameZero ()) | |||
268 | { | |||
269 | m_frame_type = eNotAValidFrame; | |||
270 | UnwindLogMsg ("non-zeroth frame tests positive for IsFrameZero -- that shouldn't happen."); | |||
271 | return; | |||
272 | } | |||
273 | ||||
274 | if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) | |||
275 | { | |||
276 | m_frame_type = eNotAValidFrame; | |||
277 | UnwindLogMsg ("Could not get next frame, marking this frame as invalid."); | |||
278 | return; | |||
279 | } | |||
280 | if (!m_thread.GetRegisterContext()) | |||
281 | { | |||
282 | m_frame_type = eNotAValidFrame; | |||
283 | UnwindLogMsg ("Could not get register context for this thread, marking this frame as invalid."); | |||
284 | return; | |||
285 | } | |||
286 | ||||
287 | addr_t pc; | |||
288 | if (!ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC0, pc)) | |||
289 | { | |||
290 | UnwindLogMsg ("could not get pc value"); | |||
291 | m_frame_type = eNotAValidFrame; | |||
292 | return; | |||
293 | } | |||
294 | ||||
295 | if (log) | |||
296 | { | |||
297 | UnwindLogMsg ("pc = 0x%16.16" PRIx64"l" "x", pc); | |||
298 | addr_t reg_val; | |||
299 | if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP2, reg_val)) | |||
300 | UnwindLogMsg ("fp = 0x%16.16" PRIx64"l" "x", reg_val); | |||
301 | if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP1, reg_val)) | |||
302 | UnwindLogMsg ("sp = 0x%16.16" PRIx64"l" "x", reg_val); | |||
303 | } | |||
304 | ||||
305 | // A pc of 0x0 means it's the end of the stack crawl | |||
306 | if (pc == 0) | |||
307 | { | |||
308 | m_frame_type = eNotAValidFrame; | |||
309 | UnwindLogMsg ("this frame has a pc of 0x0"); | |||
310 | return; | |||
311 | } | |||
312 | ||||
313 | ExecutionContext exe_ctx(m_thread.shared_from_this()); | |||
314 | Process *process = exe_ctx.GetProcessPtr(); | |||
315 | // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs | |||
316 | // this will strip bit zero in case we read a PC from memory or from the LR. | |||
317 | ABI *abi = process->GetABI().get(); | |||
318 | if (abi) | |||
319 | pc = abi->FixCodeAddress(pc); | |||
320 | ||||
321 | m_current_pc.SetLoadAddress (pc, &process->GetTarget()); | |||
322 | ||||
323 | // If we don't have a Module for some reason, we're not going to find symbol/function information - just | |||
324 | // stick in some reasonable defaults and hope we can unwind past this frame. | |||
325 | ModuleSP pc_module_sp (m_current_pc.GetModule()); | |||
326 | if (!m_current_pc.IsValid() || !pc_module_sp) | |||
327 | { | |||
328 | UnwindLogMsg ("using architectural default unwind method"); | |||
329 | ||||
330 | // Test the pc value to see if we know it's in an unmapped/non-executable region of memory. | |||
331 | uint32_t permissions; | |||
332 | if (process->GetLoadAddressPermissions(pc, permissions) | |||
333 | && (permissions & ePermissionsExecutable) == 0) | |||
334 | { | |||
335 | // If this is the second frame off the stack, we may have unwound the first frame | |||
336 | // incorrectly. But using the architecture default unwind plan may get us back on | |||
337 | // track -- albeit possibly skipping a real frame. Give this frame a clearly-invalid | |||
338 | // pc and see if we can get any further. | |||
339 | if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsFrameZero()) | |||
340 | { | |||
341 | UnwindLogMsg ("had a pc of 0x%" PRIx64"l" "x" " which is not in executable memory but on frame 1 -- allowing it once.", | |||
342 | (uint64_t) pc); | |||
343 | m_frame_type = eSkipFrame; | |||
344 | } | |||
345 | else | |||
346 | { | |||
347 | // anywhere other than the second frame, a non-executable pc means we're off in the weeds -- stop now. | |||
348 | m_frame_type = eNotAValidFrame; | |||
349 | UnwindLogMsg ("pc is in a non-executable section of memory and this isn't the 2nd frame in the stack walk."); | |||
350 | return; | |||
351 | } | |||
352 | } | |||
353 | ||||
354 | if (abi) | |||
355 | { | |||
356 | m_fast_unwind_plan_sp.reset (); | |||
357 | m_full_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); | |||
358 | abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp); | |||
359 | if (m_frame_type != eSkipFrame) // don't override eSkipFrame | |||
360 | { | |||
361 | m_frame_type = eNormalFrame; | |||
362 | } | |||
363 | m_all_registers_available = false; | |||
364 | m_current_offset = -1; | |||
365 | m_current_offset_backed_up_one = -1; | |||
366 | addr_t cfa_regval = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
367 | RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); | |||
368 | UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0); | |||
369 | if (row.get()) | |||
370 | { | |||
371 | uint32_t cfa_regnum = row->GetCFARegister(); | |||
372 | int cfa_offset = row->GetCFAOffset(); | |||
373 | if (!ReadGPRValue (row_register_kind, cfa_regnum, cfa_regval)) | |||
374 | { | |||
375 | UnwindLogMsg ("failed to get cfa value"); | |||
376 | if (m_frame_type != eSkipFrame) // don't override eSkipFrame | |||
377 | { | |||
378 | m_frame_type = eNormalFrame; | |||
379 | } | |||
380 | return; | |||
381 | } | |||
382 | m_cfa = cfa_regval + cfa_offset; | |||
383 | ||||
384 | // A couple of sanity checks.. | |||
385 | if (cfa_regval == LLDB_INVALID_ADDRESS(18446744073709551615UL) || cfa_regval == 0 || cfa_regval == 1) | |||
386 | { | |||
387 | UnwindLogMsg ("could not find a valid cfa address"); | |||
388 | m_frame_type = eNotAValidFrame; | |||
389 | return; | |||
390 | } | |||
391 | ||||
392 | // cfa_regval should point into the stack memory; if we can query memory region permissions, | |||
393 | // see if the memory is allocated & readable. | |||
394 | if (process->GetLoadAddressPermissions(cfa_regval, permissions) | |||
395 | && (permissions & ePermissionsReadable) == 0) | |||
396 | { | |||
397 | m_frame_type = eNotAValidFrame; | |||
398 | UnwindLogMsg ("the CFA points to a region of memory that is not readable"); | |||
399 | return; | |||
400 | } | |||
401 | } | |||
402 | else | |||
403 | { | |||
404 | UnwindLogMsg ("could not find a row for function offset zero"); | |||
405 | m_frame_type = eNotAValidFrame; | |||
406 | return; | |||
407 | } | |||
408 | ||||
409 | if (CheckIfLoopingStack ()) | |||
410 | { | |||
411 | TryFallbackUnwindPlan(); | |||
412 | if (CheckIfLoopingStack ()) | |||
413 | { | |||
414 | UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping"); | |||
415 | m_frame_type = eNotAValidFrame; | |||
416 | return; | |||
417 | } | |||
418 | } | |||
419 | ||||
420 | UnwindLogMsg ("initialized frame cfa is 0x%" PRIx64"l" "x", (uint64_t) m_cfa); | |||
421 | return; | |||
422 | } | |||
423 | m_frame_type = eNotAValidFrame; | |||
424 | UnwindLogMsg ("could not find any symbol for this pc, or a default unwind plan, to continue unwind."); | |||
425 | return; | |||
426 | } | |||
427 | ||||
428 | bool resolve_tail_call_address = true; // m_current_pc can be one past the address range of the function... | |||
429 | // This will handle the case where the saved pc does not point to | |||
430 | // a function/symbol because it is beyond the bounds of the correct | |||
431 | // function and there's no symbol there. ResolveSymbolContextForAddress | |||
432 | // will fail to find a symbol, back up the pc by 1 and re-search. | |||
433 | const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; | |||
434 | uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, | |||
435 | resolve_scope, | |||
436 | m_sym_ctx, resolve_tail_call_address); | |||
437 | ||||
438 | // We require either a symbol or function in the symbols context to be successfully | |||
439 | // filled in or this context is of no use to us. | |||
440 | if (resolve_scope & resolved_scope) | |||
441 | { | |||
442 | m_sym_ctx_valid = true; | |||
443 | } | |||
444 | ||||
445 | AddressRange addr_range; | |||
446 | if (!m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range)) | |||
447 | { | |||
448 | m_sym_ctx_valid = false; | |||
449 | } | |||
450 | ||||
451 | bool decr_pc_and_recompute_addr_range = false; | |||
452 | ||||
453 | // If the symbol lookup failed... | |||
454 | if (m_sym_ctx_valid == false) | |||
455 | decr_pc_and_recompute_addr_range = true; | |||
456 | ||||
457 | // Or if we're in the middle of the stack (and not "above" an asynchronous event like sigtramp), | |||
458 | // and our "current" pc is the start of a function... | |||
459 | if (m_sym_ctx_valid | |||
460 | && GetNextFrame()->m_frame_type != eTrapHandlerFrame | |||
461 | && GetNextFrame()->m_frame_type != eDebuggerFrame | |||
462 | && addr_range.GetBaseAddress().IsValid() | |||
463 | && addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection() | |||
464 | && addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset()) | |||
465 | { | |||
466 | decr_pc_and_recompute_addr_range = true; | |||
467 | } | |||
468 | ||||
469 | // We need to back up the pc by 1 byte and re-search for the Symbol to handle the case where the "saved pc" | |||
470 | // value is pointing to the next function, e.g. if a function ends with a CALL instruction. | |||
471 | // FIXME this may need to be an architectural-dependent behavior; if so we'll need to add a member function | |||
472 | // to the ABI plugin and consult that. | |||
473 | if (decr_pc_and_recompute_addr_range) | |||
474 | { | |||
475 | Address temporary_pc(m_current_pc); | |||
476 | temporary_pc.SetOffset(m_current_pc.GetOffset() - 1); | |||
477 | m_sym_ctx.Clear(false); | |||
478 | m_sym_ctx_valid = false; | |||
479 | uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; | |||
480 | ||||
481 | if (pc_module_sp->ResolveSymbolContextForAddress (temporary_pc, resolve_scope, m_sym_ctx) & resolve_scope) | |||
482 | { | |||
483 | if (m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range)) | |||
484 | m_sym_ctx_valid = true; | |||
485 | } | |||
486 | } | |||
487 | ||||
488 | // If we were able to find a symbol/function, set addr_range_ptr to the bounds of that symbol/function. | |||
489 | // else treat the current pc value as the start_pc and record no offset. | |||
490 | if (addr_range.GetBaseAddress().IsValid()) | |||
491 | { | |||
492 | m_start_pc = addr_range.GetBaseAddress(); | |||
493 | m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset(); | |||
494 | m_current_offset_backed_up_one = m_current_offset; | |||
495 | if (decr_pc_and_recompute_addr_range && m_current_offset_backed_up_one > 0) | |||
496 | { | |||
497 | m_current_offset_backed_up_one--; | |||
498 | if (m_sym_ctx_valid) | |||
499 | m_current_pc.SetOffset(m_current_pc.GetOffset() - 1); | |||
500 | } | |||
501 | } | |||
502 | else | |||
503 | { | |||
504 | m_start_pc = m_current_pc; | |||
505 | m_current_offset = -1; | |||
506 | m_current_offset_backed_up_one = -1; | |||
507 | } | |||
508 | ||||
509 | if (IsTrapHandlerSymbol (process, m_sym_ctx)) | |||
510 | { | |||
511 | m_frame_type = eTrapHandlerFrame; | |||
512 | } | |||
513 | else | |||
514 | { | |||
515 | // FIXME: Detect eDebuggerFrame here. | |||
516 | if (m_frame_type != eSkipFrame) // don't override eSkipFrame | |||
517 | { | |||
518 | m_frame_type = eNormalFrame; | |||
519 | } | |||
520 | } | |||
521 | ||||
522 | // We've set m_frame_type and m_sym_ctx before this call. | |||
523 | m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame (); | |||
524 | ||||
525 | UnwindPlan::RowSP active_row; | |||
526 | int cfa_offset = 0; | |||
527 | RegisterKind row_register_kind = eRegisterKindGeneric; | |||
528 | ||||
529 | // Try to get by with just the fast UnwindPlan if possible - the full UnwindPlan may be expensive to get | |||
530 | // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for the first time.) | |||
531 | ||||
532 | if (m_fast_unwind_plan_sp && m_fast_unwind_plan_sp->PlanValidAtAddress (m_current_pc)) | |||
533 | { | |||
534 | active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); | |||
535 | row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind (); | |||
536 | if (active_row.get() && log) | |||
537 | { | |||
538 | StreamString active_row_strm; | |||
539 | active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); | |||
540 | UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str()); | |||
541 | } | |||
542 | } | |||
543 | else | |||
544 | { | |||
545 | m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); | |||
546 | int valid_offset = -1; | |||
547 | if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp, valid_offset)) | |||
548 | { | |||
549 | active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (valid_offset); | |||
550 | row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); | |||
551 | if (active_row.get() && log) | |||
552 | { | |||
553 | StreamString active_row_strm; | |||
554 | active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); | |||
555 | UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str()); | |||
556 | } | |||
557 | } | |||
558 | } | |||
559 | ||||
560 | if (!active_row.get()) | |||
561 | { | |||
562 | m_frame_type = eNotAValidFrame; | |||
563 | UnwindLogMsg ("could not find unwind row for this pc"); | |||
564 | return; | |||
565 | } | |||
566 | ||||
567 | addr_t cfa_regval = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
568 | if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval)) | |||
569 | { | |||
570 | UnwindLogMsg ("failed to get cfa reg %d/%d", row_register_kind, active_row->GetCFARegister()); | |||
571 | m_frame_type = eNotAValidFrame; | |||
572 | return; | |||
573 | } | |||
574 | ||||
575 | cfa_offset = active_row->GetCFAOffset (); | |||
576 | m_cfa = cfa_regval + cfa_offset; | |||
577 | ||||
578 | UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64"l" "x" " (cfa_regval = 0x%16.16" PRIx64"l" "x" ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset); | |||
579 | ||||
580 | // A couple of sanity checks.. | |||
581 | if (cfa_regval == LLDB_INVALID_ADDRESS(18446744073709551615UL) || cfa_regval == 0 || cfa_regval == 1) | |||
582 | { | |||
583 | UnwindLogMsg ("could not find a valid cfa address"); | |||
584 | m_frame_type = eNotAValidFrame; | |||
585 | return; | |||
586 | } | |||
587 | ||||
588 | if (CheckIfLoopingStack ()) | |||
589 | { | |||
590 | TryFallbackUnwindPlan(); | |||
591 | if (CheckIfLoopingStack ()) | |||
592 | { | |||
593 | UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping"); | |||
594 | m_frame_type = eNotAValidFrame; | |||
595 | return; | |||
596 | } | |||
597 | } | |||
598 | ||||
599 | UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64"l" "x" " cfa is 0x%" PRIx64"l" "x", | |||
600 | (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), (uint64_t) m_cfa); | |||
601 | } | |||
602 | ||||
603 | bool | |||
604 | RegisterContextLLDB::CheckIfLoopingStack () | |||
605 | { | |||
606 | // If we have a bad stack setup, we can get the same CFA value multiple times -- or even | |||
607 | // more devious, we can actually oscillate between two CFA values. Detect that here and | |||
608 | // break out to avoid a possible infinite loop in lldb trying to unwind the stack. | |||
609 | addr_t next_frame_cfa; | |||
610 | addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
611 | if (GetNextFrame().get() && GetNextFrame()->GetCFA(next_frame_cfa)) | |||
612 | { | |||
613 | if (next_frame_cfa == m_cfa) | |||
614 | { | |||
615 | // We have a loop in the stack unwind | |||
616 | return true; | |||
617 | } | |||
618 | if (GetNextFrame()->GetNextFrame().get() && GetNextFrame()->GetNextFrame()->GetCFA(next_next_frame_cfa) | |||
619 | && next_next_frame_cfa == m_cfa) | |||
620 | { | |||
621 | // We have a loop in the stack unwind | |||
622 | return true; | |||
623 | } | |||
624 | } | |||
625 | return false; | |||
626 | } | |||
627 | ||||
628 | ||||
629 | bool | |||
630 | RegisterContextLLDB::IsFrameZero () const | |||
631 | { | |||
632 | return m_frame_number == 0; | |||
633 | } | |||
634 | ||||
635 | ||||
636 | // Find a fast unwind plan for this frame, if possible. | |||
637 | // | |||
638 | // On entry to this method, | |||
639 | // | |||
640 | // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct, | |||
641 | // 2. m_sym_ctx should already be filled in, and | |||
642 | // 3. m_current_pc should have the current pc value for this frame | |||
643 | // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown | |||
644 | ||||
645 | UnwindPlanSP | |||
646 | RegisterContextLLDB::GetFastUnwindPlanForFrame () | |||
647 | { | |||
648 | UnwindPlanSP unwind_plan_sp; | |||
649 | ModuleSP pc_module_sp (m_current_pc.GetModule()); | |||
650 | ||||
651 | if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL__null) | |||
652 | return unwind_plan_sp; | |||
653 | ||||
654 | if (IsFrameZero ()) | |||
655 | return unwind_plan_sp; | |||
656 | ||||
657 | FuncUnwindersSP func_unwinders_sp (pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx)); | |||
658 | if (!func_unwinders_sp) | |||
659 | return unwind_plan_sp; | |||
660 | ||||
661 | // If we're in _sigtramp(), unwinding past this frame requires special knowledge. | |||
662 | if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame) | |||
663 | return unwind_plan_sp; | |||
664 | ||||
665 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind (m_thread); | |||
666 | if (unwind_plan_sp) | |||
667 | { | |||
668 | if (unwind_plan_sp->PlanValidAtAddress (m_current_pc)) | |||
669 | { | |||
670 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND(1u << 15))); | |||
671 | if (log && log->GetVerbose()) | |||
672 | { | |||
673 | if (m_fast_unwind_plan_sp) | |||
674 | UnwindLogMsgVerbose ("frame, and has a fast UnwindPlan"); | |||
675 | else | |||
676 | UnwindLogMsgVerbose ("frame"); | |||
677 | } | |||
678 | m_frame_type = eNormalFrame; | |||
679 | return unwind_plan_sp; | |||
680 | } | |||
681 | else | |||
682 | { | |||
683 | unwind_plan_sp.reset(); | |||
684 | } | |||
685 | } | |||
686 | return unwind_plan_sp; | |||
687 | } | |||
688 | ||||
689 | // On entry to this method, | |||
690 | // | |||
691 | // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct, | |||
692 | // 2. m_sym_ctx should already be filled in, and | |||
693 | // 3. m_current_pc should have the current pc value for this frame | |||
694 | // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown | |||
695 | ||||
696 | UnwindPlanSP | |||
697 | RegisterContextLLDB::GetFullUnwindPlanForFrame () | |||
698 | { | |||
699 | UnwindPlanSP unwind_plan_sp; | |||
700 | UnwindPlanSP arch_default_unwind_plan_sp; | |||
701 | ExecutionContext exe_ctx(m_thread.shared_from_this()); | |||
702 | Process *process = exe_ctx.GetProcessPtr(); | |||
| ||||
703 | ABI *abi = process ? process->GetABI().get() : NULL__null; | |||
704 | if (abi) | |||
705 | { | |||
706 | arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); | |||
707 | abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp); | |||
708 | } | |||
709 | else | |||
710 | { | |||
711 | UnwindLogMsg ("unable to get architectural default UnwindPlan from ABI plugin"); | |||
712 | } | |||
713 | ||||
714 | bool behaves_like_zeroth_frame = false; | |||
715 | if (IsFrameZero () | |||
716 | || GetNextFrame()->m_frame_type == eTrapHandlerFrame | |||
717 | || GetNextFrame()->m_frame_type == eDebuggerFrame) | |||
718 | { | |||
719 | behaves_like_zeroth_frame = true; | |||
720 | // If this frame behaves like a 0th frame (currently executing or | |||
721 | // interrupted asynchronously), all registers can be retrieved. | |||
722 | m_all_registers_available = true; | |||
723 | } | |||
724 | ||||
725 | // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer) so the pc is 0x0 | |||
726 | // in the zeroth frame, we need to use the "unwind at first instruction" arch default UnwindPlan | |||
727 | // Also, if this Process can report on memory region attributes, any non-executable region means | |||
728 | // we jumped through a bad function pointer - handle the same way as 0x0. | |||
729 | // Note, if we have a symbol context & a symbol, we don't want to follow this code path. This is | |||
730 | // for jumping to memory regions without any information available. | |||
731 | ||||
732 | if ((!m_sym_ctx_valid || (m_sym_ctx.function == NULL__null && m_sym_ctx.symbol == NULL__null)) && behaves_like_zeroth_frame && m_current_pc.IsValid()) | |||
733 | { | |||
734 | uint32_t permissions; | |||
735 | addr_t current_pc_addr = m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()); | |||
736 | if (current_pc_addr == 0 | |||
737 | || (process->GetLoadAddressPermissions (current_pc_addr, permissions) | |||
| ||||
738 | && (permissions & ePermissionsExecutable) == 0)) | |||
739 | { | |||
740 | unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); | |||
741 | abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp); | |||
742 | m_frame_type = eNormalFrame; | |||
743 | return unwind_plan_sp; | |||
744 | } | |||
745 | } | |||
746 | ||||
747 | // No Module for the current pc, try using the architecture default unwind. | |||
748 | ModuleSP pc_module_sp (m_current_pc.GetModule()); | |||
749 | if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL__null) | |||
750 | { | |||
751 | m_frame_type = eNormalFrame; | |||
752 | return arch_default_unwind_plan_sp; | |||
753 | } | |||
754 | ||||
755 | FuncUnwindersSP func_unwinders_sp; | |||
756 | if (m_sym_ctx_valid) | |||
757 | { | |||
758 | func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx); | |||
759 | } | |||
760 | ||||
761 | // No FuncUnwinders available for this pc (i.e. a stripped function symbol and -fomit-frame-pointer). | |||
762 | // Try using the eh_frame information relative to the current PC, | |||
763 | // and finally fall back on the architectural default unwind. | |||
764 | if (!func_unwinders_sp) | |||
765 | { | |||
766 | DWARFCallFrameInfo *eh_frame = pc_module_sp && pc_module_sp->GetObjectFile() ? | |||
767 | pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo() : nullptr; | |||
768 | ||||
769 | m_frame_type = eNormalFrame; | |||
770 | if (eh_frame && m_current_pc.IsValid()) | |||
771 | { | |||
772 | unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); | |||
773 | // Even with -fomit-frame-pointer, we can try eh_frame to get back on track. | |||
774 | if (eh_frame->GetUnwindPlan (m_current_pc, *unwind_plan_sp)) | |||
775 | return unwind_plan_sp; | |||
776 | else | |||
777 | unwind_plan_sp.reset(); | |||
778 | } | |||
779 | return arch_default_unwind_plan_sp; | |||
780 | } | |||
781 | ||||
782 | // If we're in _sigtramp(), unwinding past this frame requires special knowledge. On Mac OS X this knowledge | |||
783 | // is properly encoded in the eh_frame section, so prefer that if available. | |||
784 | // On other platforms we may need to provide a platform-specific UnwindPlan which encodes the details of | |||
785 | // how to unwind out of sigtramp. | |||
786 | if (m_frame_type == eTrapHandlerFrame) | |||
787 | { | |||
788 | m_fast_unwind_plan_sp.reset(); | |||
789 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one); | |||
790 | if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc) && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) | |||
791 | { | |||
792 | return unwind_plan_sp; | |||
793 | } | |||
794 | } | |||
795 | ||||
796 | // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame even when it's frame zero | |||
797 | // This comes up if we have hand-written functions in a Module and hand-written eh_frame. The assembly | |||
798 | // instruction inspection may fail and the eh_frame CFI were probably written with some care to do the | |||
799 | // right thing. It'd be nice if there was a way to ask the eh_frame directly if it is asynchronous | |||
800 | // (can be trusted at every instruction point) or synchronous (the normal case - only at call sites). | |||
801 | // But there is not. | |||
802 | if (process && process->GetDynamicLoader() && process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo (m_sym_ctx)) | |||
803 | { | |||
804 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one); | |||
805 | if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc)) | |||
806 | { | |||
807 | UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan because the DynamicLoader suggested we prefer it", | |||
808 | unwind_plan_sp->GetSourceName().GetCString()); | |||
809 | return unwind_plan_sp; | |||
810 | } | |||
811 | } | |||
812 | ||||
813 | // Typically the NonCallSite UnwindPlan is the unwind created by inspecting the assembly language instructions | |||
814 | if (behaves_like_zeroth_frame) | |||
815 | { | |||
816 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one); | |||
817 | if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc)) | |||
818 | { | |||
819 | if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) | |||
820 | { | |||
821 | // We probably have an UnwindPlan created by inspecting assembly instructions, and we probably | |||
822 | // don't have any eh_frame instructions available. | |||
823 | // The assembly profilers work really well with compiler-generated functions but hand-written | |||
824 | // assembly can be problematic. We'll set the architecture default UnwindPlan as our fallback | |||
825 | // UnwindPlan in case this doesn't work out when we try to unwind. | |||
826 | m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp; | |||
827 | } | |||
828 | UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); | |||
829 | return unwind_plan_sp; | |||
830 | } | |||
831 | } | |||
832 | ||||
833 | // Typically this is unwind info from an eh_frame section intended for exception handling; only valid at call sites | |||
834 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one); | |||
835 | int valid_offset = -1; | |||
836 | if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) | |||
837 | { | |||
838 | UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); | |||
839 | return unwind_plan_sp; | |||
840 | } | |||
841 | ||||
842 | // We'd prefer to use an UnwindPlan intended for call sites when we're at a call site but if we've | |||
843 | // struck out on that, fall back to using the non-call-site assembly inspection UnwindPlan if possible. | |||
844 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one); | |||
845 | if (unwind_plan_sp && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) | |||
846 | { | |||
847 | // We probably have an UnwindPlan created by inspecting assembly instructions, and we probably | |||
848 | // don't have any eh_frame instructions available. | |||
849 | // The assembly profilers work really well with compiler-generated functions but hand-written | |||
850 | // assembly can be problematic. We'll set the architecture default UnwindPlan as our fallback | |||
851 | // UnwindPlan in case this doesn't work out when we try to unwind. | |||
852 | m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp; | |||
853 | } | |||
854 | ||||
855 | if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) | |||
856 | { | |||
857 | UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); | |||
858 | return unwind_plan_sp; | |||
859 | } | |||
860 | ||||
861 | // If we're on the first instruction of a function, and we have an architectural default UnwindPlan | |||
862 | // for the initial instruction of a function, use that. | |||
863 | if (m_current_offset_backed_up_one == 0) | |||
864 | { | |||
865 | unwind_plan_sp = func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry (m_thread); | |||
866 | if (unwind_plan_sp) | |||
867 | { | |||
868 | UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); | |||
869 | return unwind_plan_sp; | |||
870 | } | |||
871 | } | |||
872 | ||||
873 | // If nothing else, use the architectural default UnwindPlan and hope that does the job. | |||
874 | if (arch_default_unwind_plan_sp) | |||
875 | UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", arch_default_unwind_plan_sp->GetSourceName().GetCString()); | |||
876 | else | |||
877 | UnwindLogMsg ("Unable to find any UnwindPlan for full unwind of this frame."); | |||
878 | ||||
879 | return arch_default_unwind_plan_sp; | |||
880 | } | |||
881 | ||||
882 | ||||
883 | void | |||
884 | RegisterContextLLDB::InvalidateAllRegisters () | |||
885 | { | |||
886 | m_frame_type = eNotAValidFrame; | |||
887 | } | |||
888 | ||||
889 | size_t | |||
890 | RegisterContextLLDB::GetRegisterCount () | |||
891 | { | |||
892 | return m_thread.GetRegisterContext()->GetRegisterCount(); | |||
893 | } | |||
894 | ||||
895 | const RegisterInfo * | |||
896 | RegisterContextLLDB::GetRegisterInfoAtIndex (size_t reg) | |||
897 | { | |||
898 | return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex (reg); | |||
899 | } | |||
900 | ||||
901 | size_t | |||
902 | RegisterContextLLDB::GetRegisterSetCount () | |||
903 | { | |||
904 | return m_thread.GetRegisterContext()->GetRegisterSetCount (); | |||
905 | } | |||
906 | ||||
907 | const RegisterSet * | |||
908 | RegisterContextLLDB::GetRegisterSet (size_t reg_set) | |||
909 | { | |||
910 | return m_thread.GetRegisterContext()->GetRegisterSet (reg_set); | |||
911 | } | |||
912 | ||||
913 | uint32_t | |||
914 | RegisterContextLLDB::ConvertRegisterKindToRegisterNumber (lldb::RegisterKind kind, uint32_t num) | |||
915 | { | |||
916 | return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber (kind, num); | |||
917 | } | |||
918 | ||||
919 | bool | |||
920 | RegisterContextLLDB::ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc, | |||
921 | const RegisterInfo *reg_info, | |||
922 | RegisterValue &value) | |||
923 | { | |||
924 | if (!IsValid()) | |||
925 | return false; | |||
926 | bool success = false; | |||
927 | ||||
928 | switch (regloc.type) | |||
929 | { | |||
930 | case UnwindLLDB::RegisterLocation::eRegisterInRegister: | |||
931 | { | |||
932 | const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); | |||
933 | ||||
934 | if (!other_reg_info) | |||
935 | return false; | |||
936 | ||||
937 | if (IsFrameZero ()) | |||
938 | { | |||
939 | success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value); | |||
940 | } | |||
941 | else | |||
942 | { | |||
943 | success = GetNextFrame()->ReadRegister (other_reg_info, value); | |||
944 | } | |||
945 | } | |||
946 | break; | |||
947 | case UnwindLLDB::RegisterLocation::eRegisterValueInferred: | |||
948 | success = value.SetUInt (regloc.location.inferred_value, reg_info->byte_size); | |||
949 | break; | |||
950 | ||||
951 | case UnwindLLDB::RegisterLocation::eRegisterNotSaved: | |||
952 | break; | |||
953 | case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation: | |||
954 | assert ("FIXME debugger inferior function call unwind")(("FIXME debugger inferior function call unwind") ? static_cast <void> (0) : __assert_fail ("\"FIXME debugger inferior function call unwind\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn219601/tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp" , 954, __PRETTY_FUNCTION__)); | |||
955 | break; | |||
956 | case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: | |||
957 | { | |||
958 | Error error (ReadRegisterValueFromMemory(reg_info, | |||
959 | regloc.location.target_memory_location, | |||
960 | reg_info->byte_size, | |||
961 | value)); | |||
962 | success = error.Success(); | |||
963 | } | |||
964 | break; | |||
965 | default: | |||
966 | assert ("Unknown RegisterLocation type.")(("Unknown RegisterLocation type.") ? static_cast<void> (0) : __assert_fail ("\"Unknown RegisterLocation type.\"", "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn219601/tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp" , 966, __PRETTY_FUNCTION__)); | |||
967 | break; | |||
968 | } | |||
969 | return success; | |||
970 | } | |||
971 | ||||
972 | bool | |||
973 | RegisterContextLLDB::WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc, | |||
974 | const RegisterInfo *reg_info, | |||
975 | const RegisterValue &value) | |||
976 | { | |||
977 | if (!IsValid()) | |||
978 | return false; | |||
979 | ||||
980 | bool success = false; | |||
981 | ||||
982 | switch (regloc.type) | |||
983 | { | |||
984 | case UnwindLLDB::RegisterLocation::eRegisterInRegister: | |||
985 | { | |||
986 | const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); | |||
987 | if (IsFrameZero ()) | |||
988 | { | |||
989 | success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value); | |||
990 | } | |||
991 | else | |||
992 | { | |||
993 | success = GetNextFrame()->WriteRegister (other_reg_info, value); | |||
994 | } | |||
995 | } | |||
996 | break; | |||
997 | case UnwindLLDB::RegisterLocation::eRegisterValueInferred: | |||
998 | case UnwindLLDB::RegisterLocation::eRegisterNotSaved: | |||
999 | break; | |||
1000 | case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation: | |||
1001 | assert ("FIXME debugger inferior function call unwind")(("FIXME debugger inferior function call unwind") ? static_cast <void> (0) : __assert_fail ("\"FIXME debugger inferior function call unwind\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn219601/tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp" , 1001, __PRETTY_FUNCTION__)); | |||
1002 | break; | |||
1003 | case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: | |||
1004 | { | |||
1005 | Error error (WriteRegisterValueToMemory (reg_info, | |||
1006 | regloc.location.target_memory_location, | |||
1007 | reg_info->byte_size, | |||
1008 | value)); | |||
1009 | success = error.Success(); | |||
1010 | } | |||
1011 | break; | |||
1012 | default: | |||
1013 | assert ("Unknown RegisterLocation type.")(("Unknown RegisterLocation type.") ? static_cast<void> (0) : __assert_fail ("\"Unknown RegisterLocation type.\"", "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn219601/tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp" , 1013, __PRETTY_FUNCTION__)); | |||
1014 | break; | |||
1015 | } | |||
1016 | return success; | |||
1017 | } | |||
1018 | ||||
1019 | ||||
1020 | bool | |||
1021 | RegisterContextLLDB::IsValid () const | |||
1022 | { | |||
1023 | return m_frame_type != eNotAValidFrame; | |||
1024 | } | |||
1025 | ||||
1026 | bool | |||
1027 | RegisterContextLLDB::IsTrapHandlerFrame () const | |||
1028 | { | |||
1029 | return m_frame_type == eTrapHandlerFrame; | |||
1030 | } | |||
1031 | ||||
1032 | // A skip frame is a bogus frame on the stack -- but one where we're likely to find a real frame farther | |||
1033 | // up the stack if we keep looking. It's always the second frame in an unwind (i.e. the first frame after | |||
1034 | // frame zero) where unwinding can be the trickiest. Ideally we'll mark up this frame in some way so the | |||
1035 | // user knows we're displaying bad data and we may have skipped one frame of their real program in the | |||
1036 | // process of getting back on track. | |||
1037 | ||||
1038 | bool | |||
1039 | RegisterContextLLDB::IsSkipFrame () const | |||
1040 | { | |||
1041 | return m_frame_type == eSkipFrame; | |||
1042 | } | |||
1043 | ||||
1044 | bool | |||
1045 | RegisterContextLLDB::IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const | |||
1046 | { | |||
1047 | PlatformSP platform_sp (process->GetTarget().GetPlatform()); | |||
1048 | if (platform_sp) | |||
1049 | { | |||
1050 | const std::vector<ConstString> trap_handler_names (platform_sp->GetTrapHandlerSymbolNames()); | |||
1051 | for (ConstString name : trap_handler_names) | |||
1052 | { | |||
1053 | if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) || | |||
1054 | (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) | |||
1055 | { | |||
1056 | return true; | |||
1057 | } | |||
1058 | } | |||
1059 | } | |||
1060 | const std::vector<ConstString> user_specified_trap_handler_names (m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames()); | |||
1061 | for (ConstString name : user_specified_trap_handler_names) | |||
1062 | { | |||
1063 | if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) || | |||
1064 | (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) | |||
1065 | { | |||
1066 | return true; | |||
1067 | } | |||
1068 | } | |||
1069 | ||||
1070 | return false; | |||
1071 | } | |||
1072 | ||||
1073 | // Answer the question: Where did THIS frame save the CALLER frame ("previous" frame)'s register value? | |||
1074 | ||||
1075 | enum UnwindLLDB::RegisterSearchResult | |||
1076 | RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc) | |||
1077 | { | |||
1078 | // Have we already found this register location? | |||
1079 | if (!m_registers.empty()) | |||
1080 | { | |||
1081 | std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>::const_iterator iterator; | |||
1082 | iterator = m_registers.find (lldb_regnum); | |||
1083 | if (iterator != m_registers.end()) | |||
1084 | { | |||
1085 | regloc = iterator->second; | |||
1086 | UnwindLogMsg ("supplying caller's saved reg %d's location, cached", lldb_regnum); | |||
1087 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1088 | } | |||
1089 | } | |||
1090 | ||||
1091 | uint32_t sp_regnum = LLDB_INVALID_REGNUM(4294967295U); | |||
1092 | uint32_t pc_regnum = LLDB_INVALID_REGNUM(4294967295U); | |||
1093 | m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP1, eRegisterKindLLDB, sp_regnum); | |||
1094 | m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC0, eRegisterKindLLDB, pc_regnum); | |||
1095 | ||||
1096 | // Are we looking for the CALLER's stack pointer? The stack pointer is defined to be the same as THIS frame's | |||
1097 | // CFA so just return the CFA value. This is true on x86-32/x86-64 at least. | |||
1098 | if (sp_regnum != LLDB_INVALID_REGNUM(4294967295U) && sp_regnum == lldb_regnum) | |||
1099 | { | |||
1100 | // make sure we won't lose precision copying an addr_t (m_cfa) into a uint64_t (.inferred_value) | |||
1101 | assert (sizeof (addr_t) <= sizeof (uint64_t))((sizeof (addr_t) <= sizeof (uint64_t)) ? static_cast<void > (0) : __assert_fail ("sizeof (addr_t) <= sizeof (uint64_t)" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn219601/tools/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp" , 1101, __PRETTY_FUNCTION__)); | |||
1102 | regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; | |||
1103 | regloc.location.inferred_value = m_cfa; | |||
1104 | m_registers[lldb_regnum] = regloc; | |||
1105 | UnwindLogMsg ("supplying caller's stack pointer (%d) value, computed from CFA", lldb_regnum); | |||
1106 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1107 | } | |||
1108 | ||||
1109 | // Look through the available UnwindPlans for the register location. | |||
1110 | ||||
1111 | UnwindPlan::Row::RegisterLocation unwindplan_regloc; | |||
1112 | bool have_unwindplan_regloc = false; | |||
1113 | RegisterKind unwindplan_registerkind = (RegisterKind)-1; | |||
1114 | ||||
1115 | if (m_fast_unwind_plan_sp) | |||
1116 | { | |||
1117 | UnwindPlan::RowSP active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); | |||
1118 | unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind (); | |||
1119 | uint32_t row_regnum; | |||
1120 | if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum)) | |||
1121 | { | |||
1122 | UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme", | |||
1123 | lldb_regnum, (int) unwindplan_registerkind); | |||
1124 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1125 | } | |||
1126 | if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc)) | |||
1127 | { | |||
1128 | UnwindLogMsg ("supplying caller's saved reg %d's location using FastUnwindPlan", lldb_regnum); | |||
1129 | have_unwindplan_regloc = true; | |||
1130 | } | |||
1131 | } | |||
1132 | ||||
1133 | if (!have_unwindplan_regloc) | |||
1134 | { | |||
1135 | // m_full_unwind_plan_sp being NULL means that we haven't tried to find a full UnwindPlan yet | |||
1136 | if (!m_full_unwind_plan_sp) | |||
1137 | m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); | |||
1138 | ||||
1139 | if (m_full_unwind_plan_sp) | |||
1140 | { | |||
1141 | UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); | |||
1142 | unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind (); | |||
1143 | uint32_t row_regnum; | |||
1144 | bool row_register_rewritten_to_return_address_reg = false; | |||
1145 | ||||
1146 | // If we're fetching the saved pc and this UnwindPlan defines a ReturnAddress register (e.g. lr on arm), | |||
1147 | // look for the return address register number in the UnwindPlan's row. | |||
1148 | if (lldb_regnum == pc_regnum && m_full_unwind_plan_sp->GetReturnAddressRegister() != LLDB_INVALID_REGNUM(4294967295U)) | |||
1149 | { | |||
1150 | row_regnum = m_full_unwind_plan_sp->GetReturnAddressRegister(); | |||
1151 | row_register_rewritten_to_return_address_reg = true; | |||
1152 | UnwindLogMsg ("requested caller's saved PC but this UnwindPlan uses a RA reg; getting reg %d instead", | |||
1153 | row_regnum); | |||
1154 | } | |||
1155 | else | |||
1156 | { | |||
1157 | if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum)) | |||
1158 | { | |||
1159 | if (unwindplan_registerkind == eRegisterKindGeneric) | |||
1160 | UnwindLogMsg ("could not convert lldb regnum %d into eRegisterKindGeneric reg numbering scheme", lldb_regnum); | |||
1161 | else | |||
1162 | UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme", | |||
1163 | lldb_regnum, (int) unwindplan_registerkind); | |||
1164 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1165 | } | |||
1166 | } | |||
1167 | ||||
1168 | if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc)) | |||
1169 | { | |||
1170 | have_unwindplan_regloc = true; | |||
1171 | UnwindLogMsg ("supplying caller's saved reg %d's location using %s UnwindPlan", lldb_regnum, | |||
1172 | m_full_unwind_plan_sp->GetSourceName().GetCString()); | |||
1173 | } | |||
1174 | ||||
1175 | // This is frame 0 and we're retrieving the PC and it's saved in a Return Address register and | |||
1176 | // it hasn't been saved anywhere yet -- that is, it's still live in the actual register. | |||
1177 | // Handle this specially. | |||
1178 | ||||
1179 | if (have_unwindplan_regloc == false | |||
1180 | && row_register_rewritten_to_return_address_reg == true | |||
1181 | && IsFrameZero() | |||
1182 | && row_regnum != LLDB_INVALID_REGNUM(4294967295U)) | |||
1183 | { | |||
1184 | uint32_t ra_regnum_in_lldb_reg_numbering; | |||
1185 | if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, row_regnum, eRegisterKindLLDB, ra_regnum_in_lldb_reg_numbering)) | |||
1186 | { | |||
1187 | lldb_private::UnwindLLDB::RegisterLocation new_regloc; | |||
1188 | new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; | |||
1189 | new_regloc.location.register_number = ra_regnum_in_lldb_reg_numbering; | |||
1190 | m_registers[lldb_regnum] = new_regloc; | |||
1191 | regloc = new_regloc; | |||
1192 | UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0, saved in %d", lldb_regnum, ra_regnum_in_lldb_reg_numbering); | |||
1193 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1194 | } | |||
1195 | } | |||
1196 | ||||
1197 | // If this architecture stores the return address in a register (it defines a Return Address register) | |||
1198 | // and we're on a non-zero stack frame and the Full UnwindPlan says that the pc is stored in the | |||
1199 | // RA registers (e.g. lr on arm), then we know that the full unwindplan is not trustworthy -- this | |||
1200 | // is an impossible situation and the instruction emulation code has likely been misled. | |||
1201 | // If this stack frame meets those criteria, we need to throw away the Full UnwindPlan that the | |||
1202 | // instruction emulation came up with and fall back to the architecture's Default UnwindPlan so | |||
1203 | // the stack walk can get past this point. | |||
1204 | ||||
1205 | // Special note: If the Full UnwindPlan was generated from the compiler, don't second-guess it | |||
1206 | // when we're at a call site location. | |||
1207 | ||||
1208 | // arch_default_ra_regnum is the return address register # in the Full UnwindPlan register numbering | |||
1209 | uint32_t arch_default_ra_regnum = LLDB_INVALID_REGNUM(4294967295U); | |||
1210 | if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA3, unwindplan_registerkind, arch_default_ra_regnum) | |||
1211 | && arch_default_ra_regnum != LLDB_INVALID_REGNUM(4294967295U) | |||
1212 | && pc_regnum != LLDB_INVALID_REGNUM(4294967295U) | |||
1213 | && pc_regnum == lldb_regnum | |||
1214 | && unwindplan_regloc.IsInOtherRegister() | |||
1215 | && unwindplan_regloc.GetRegisterNumber() == arch_default_ra_regnum | |||
1216 | && m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes | |||
1217 | && !m_all_registers_available) | |||
1218 | { | |||
1219 | UnwindLogMsg ("%s UnwindPlan tried to restore the pc from the link register but this is a non-zero frame", | |||
1220 | m_full_unwind_plan_sp->GetSourceName().GetCString()); | |||
1221 | ||||
1222 | // Throw away the full unwindplan; install the arch default unwindplan | |||
1223 | if (TryFallbackUnwindPlan()) | |||
1224 | { | |||
1225 | // Now re-fetch the pc value we're searching for | |||
1226 | uint32_t arch_default_pc_reg = LLDB_INVALID_REGNUM(4294967295U); | |||
1227 | UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); | |||
1228 | if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC0, m_full_unwind_plan_sp->GetRegisterKind(), arch_default_pc_reg) | |||
1229 | && arch_default_pc_reg != LLDB_INVALID_REGNUM(4294967295U) | |||
1230 | && active_row | |||
1231 | && active_row->GetRegisterInfo (arch_default_pc_reg, unwindplan_regloc)) | |||
1232 | { | |||
1233 | have_unwindplan_regloc = true; | |||
1234 | } | |||
1235 | else | |||
1236 | { | |||
1237 | have_unwindplan_regloc = false; | |||
1238 | } | |||
1239 | } | |||
1240 | } | |||
1241 | } | |||
1242 | } | |||
1243 | ||||
1244 | ||||
1245 | ExecutionContext exe_ctx(m_thread.shared_from_this()); | |||
1246 | Process *process = exe_ctx.GetProcessPtr(); | |||
1247 | if (have_unwindplan_regloc == false) | |||
1248 | { | |||
1249 | // If a volatile register is being requested, we don't want to forward the next frame's register contents | |||
1250 | // up the stack -- the register is not retrievable at this frame. | |||
1251 | ABI *abi = process ? process->GetABI().get() : NULL__null; | |||
1252 | if (abi) | |||
1253 | { | |||
1254 | const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum); | |||
1255 | if (reg_info && abi->RegisterIsVolatile (reg_info)) | |||
1256 | { | |||
1257 | UnwindLogMsg ("did not supply reg location for %d (%s) because it is volatile", | |||
1258 | lldb_regnum, reg_info->name ? reg_info->name : "??"); | |||
1259 | return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile; | |||
1260 | } | |||
1261 | } | |||
1262 | ||||
1263 | if (IsFrameZero ()) | |||
1264 | { | |||
1265 | // This is frame 0 - we should return the actual live register context value | |||
1266 | lldb_private::UnwindLLDB::RegisterLocation new_regloc; | |||
1267 | new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; | |||
1268 | new_regloc.location.register_number = lldb_regnum; | |||
1269 | m_registers[lldb_regnum] = new_regloc; | |||
1270 | regloc = new_regloc; | |||
1271 | UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0", lldb_regnum); | |||
1272 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1273 | } | |||
1274 | else | |||
1275 | UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); | |||
1276 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1277 | } | |||
1278 | ||||
1279 | // unwindplan_regloc has valid contents about where to retrieve the register | |||
1280 | if (unwindplan_regloc.IsUnspecified()) | |||
1281 | { | |||
1282 | lldb_private::UnwindLLDB::RegisterLocation new_regloc; | |||
1283 | new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved; | |||
1284 | m_registers[lldb_regnum] = new_regloc; | |||
1285 | UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); | |||
1286 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1287 | } | |||
1288 | ||||
1289 | if (unwindplan_regloc.IsSame()) | |||
1290 | { | |||
1291 | if (IsFrameZero ()) | |||
1292 | { | |||
1293 | UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); | |||
1294 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1295 | } | |||
1296 | else | |||
1297 | { | |||
1298 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1299 | } | |||
1300 | } | |||
1301 | ||||
1302 | if (unwindplan_regloc.IsCFAPlusOffset()) | |||
1303 | { | |||
1304 | int offset = unwindplan_regloc.GetOffset(); | |||
1305 | regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; | |||
1306 | regloc.location.inferred_value = m_cfa + offset; | |||
1307 | m_registers[lldb_regnum] = regloc; | |||
1308 | UnwindLogMsg ("supplying caller's register %d, value is CFA plus offset %d", lldb_regnum, offset); | |||
1309 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1310 | } | |||
1311 | ||||
1312 | if (unwindplan_regloc.IsAtCFAPlusOffset()) | |||
1313 | { | |||
1314 | int offset = unwindplan_regloc.GetOffset(); | |||
1315 | regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation; | |||
1316 | regloc.location.target_memory_location = m_cfa + offset; | |||
1317 | m_registers[lldb_regnum] = regloc; | |||
1318 | UnwindLogMsg ("supplying caller's register %d from the stack, saved at CFA plus offset %d", lldb_regnum, offset); | |||
1319 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1320 | } | |||
1321 | ||||
1322 | if (unwindplan_regloc.IsInOtherRegister()) | |||
1323 | { | |||
1324 | uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber(); | |||
1325 | uint32_t row_regnum_in_lldb; | |||
1326 | if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, unwindplan_regnum, eRegisterKindLLDB, row_regnum_in_lldb)) | |||
1327 | { | |||
1328 | UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); | |||
1329 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1330 | } | |||
1331 | regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; | |||
1332 | regloc.location.register_number = row_regnum_in_lldb; | |||
1333 | m_registers[lldb_regnum] = regloc; | |||
1334 | UnwindLogMsg ("supplying caller's register %d, saved in register %d", lldb_regnum, row_regnum_in_lldb); | |||
1335 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1336 | } | |||
1337 | ||||
1338 | if (unwindplan_regloc.IsDWARFExpression() || unwindplan_regloc.IsAtDWARFExpression()) | |||
1339 | { | |||
1340 | DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(), | |||
1341 | unwindplan_regloc.GetDWARFExpressionLength(), | |||
1342 | process->GetByteOrder(), process->GetAddressByteSize()); | |||
1343 | ModuleSP opcode_ctx; | |||
1344 | DWARFExpression dwarfexpr (opcode_ctx, dwarfdata, 0, unwindplan_regloc.GetDWARFExpressionLength()); | |||
1345 | dwarfexpr.SetRegisterKind (unwindplan_registerkind); | |||
1346 | Value result; | |||
1347 | Error error; | |||
1348 | if (dwarfexpr.Evaluate (&exe_ctx, NULL__null, NULL__null, this, 0, NULL__null, result, &error)) | |||
1349 | { | |||
1350 | addr_t val; | |||
1351 | val = result.GetScalar().ULongLong(); | |||
1352 | if (unwindplan_regloc.IsDWARFExpression()) | |||
1353 | { | |||
1354 | regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; | |||
1355 | regloc.location.inferred_value = val; | |||
1356 | m_registers[lldb_regnum] = regloc; | |||
1357 | UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsDWARFExpression)", lldb_regnum); | |||
1358 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1359 | } | |||
1360 | else | |||
1361 | { | |||
1362 | regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation; | |||
1363 | regloc.location.target_memory_location = val; | |||
1364 | m_registers[lldb_regnum] = regloc; | |||
1365 | UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsAtDWARFExpression)", lldb_regnum); | |||
1366 | return UnwindLLDB::RegisterSearchResult::eRegisterFound; | |||
1367 | } | |||
1368 | } | |||
1369 | UnwindLogMsg ("tried to use IsDWARFExpression or IsAtDWARFExpression for reg %d but failed", lldb_regnum); | |||
1370 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1371 | } | |||
1372 | ||||
1373 | UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); | |||
1374 | ||||
1375 | // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are unsupported. | |||
1376 | ||||
1377 | return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; | |||
1378 | } | |||
1379 | ||||
1380 | // If the Full unwindplan has been determined to be incorrect, this method will | |||
1381 | // replace it with the architecture's default unwindplan, if one is defined. | |||
1382 | // It will also find the FuncUnwinders object for this function and replace the | |||
1383 | // Full unwind method for the function there so we don't use the errant Full unwindplan | |||
1384 | // again in the future of this debug session. | |||
1385 | // We're most likely doing this because the Full unwindplan was generated by assembly | |||
1386 | // instruction profiling and the profiler got something wrong. | |||
1387 | ||||
1388 | bool | |||
1389 | RegisterContextLLDB::TryFallbackUnwindPlan () | |||
1390 | { | |||
1391 | UnwindPlan::Row::RegisterLocation unwindplan_regloc; | |||
1392 | if (m_fallback_unwind_plan_sp.get() == NULL__null) | |||
1393 | return false; | |||
1394 | ||||
1395 | UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp; | |||
1396 | UnwindPlan::RowSP active_row = m_fallback_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); | |||
1397 | ||||
1398 | if (active_row && active_row->GetCFARegister() != LLDB_INVALID_REGNUM(4294967295U)) | |||
1399 | { | |||
1400 | FuncUnwindersSP func_unwinders_sp; | |||
1401 | if (m_sym_ctx_valid && m_current_pc.IsValid() && m_current_pc.GetModule()) | |||
1402 | { | |||
1403 | func_unwinders_sp = m_current_pc.GetModule()->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx); | |||
1404 | if (func_unwinders_sp) | |||
1405 | { | |||
1406 | func_unwinders_sp->InvalidateNonCallSiteUnwindPlan (m_thread); | |||
1407 | } | |||
1408 | } | |||
1409 | m_registers.clear(); | |||
1410 | m_full_unwind_plan_sp = m_fallback_unwind_plan_sp; | |||
1411 | addr_t cfa_regval = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
1412 | if (ReadGPRValue (m_fallback_unwind_plan_sp->GetRegisterKind(), active_row->GetCFARegister(), cfa_regval)) | |||
1413 | { | |||
1414 | m_cfa = cfa_regval + active_row->GetCFAOffset (); | |||
1415 | } | |||
1416 | ||||
1417 | UnwindLogMsg ("full unwind plan '%s' has been replaced by architecture default unwind plan '%s' for this function from now on.", | |||
1418 | original_full_unwind_plan_sp->GetSourceName().GetCString(), m_fallback_unwind_plan_sp->GetSourceName().GetCString()); | |||
1419 | m_fallback_unwind_plan_sp.reset(); | |||
1420 | } | |||
1421 | ||||
1422 | return true; | |||
1423 | } | |||
1424 | ||||
1425 | // Retrieve a general purpose register value for THIS frame, as saved by the NEXT frame, i.e. the frame that | |||
1426 | // this frame called. e.g. | |||
1427 | // | |||
1428 | // foo () { } | |||
1429 | // bar () { foo (); } | |||
1430 | // main () { bar (); } | |||
1431 | // | |||
1432 | // stopped in foo() so | |||
1433 | // frame 0 - foo | |||
1434 | // frame 1 - bar | |||
1435 | // frame 2 - main | |||
1436 | // and this RegisterContext is for frame 1 (bar) - if we want to get the pc value for frame 1, we need to ask | |||
1437 | // where frame 0 (the "next" frame) saved that and retrieve the value. | |||
1438 | ||||
1439 | bool | |||
1440 | RegisterContextLLDB::ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, addr_t &value) | |||
1441 | { | |||
1442 | if (!IsValid()) | |||
1443 | return false; | |||
1444 | ||||
1445 | uint32_t lldb_regnum; | |||
1446 | if (register_kind == eRegisterKindLLDB) | |||
1447 | { | |||
1448 | lldb_regnum = regnum; | |||
1449 | } | |||
1450 | else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) | |||
1451 | { | |||
1452 | return false; | |||
1453 | } | |||
1454 | ||||
1455 | const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum); | |||
1456 | RegisterValue reg_value; | |||
1457 | // if this is frame 0 (currently executing frame), get the requested reg contents from the actual thread registers | |||
1458 | if (IsFrameZero ()) | |||
1459 | { | |||
1460 | if (m_thread.GetRegisterContext()->ReadRegister (reg_info, reg_value)) | |||
1461 | { | |||
1462 | value = reg_value.GetAsUInt64(); | |||
1463 | return true; | |||
1464 | } | |||
1465 | return false; | |||
1466 | } | |||
1467 | ||||
1468 | bool pc_register = false; | |||
1469 | uint32_t generic_regnum; | |||
1470 | if (register_kind == eRegisterKindGeneric && regnum == LLDB_REGNUM_GENERIC_PC0) | |||
1471 | { | |||
1472 | pc_register = true; | |||
1473 | } | |||
1474 | else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindGeneric, generic_regnum) | |||
1475 | && generic_regnum == LLDB_REGNUM_GENERIC_PC0) | |||
1476 | { | |||
1477 | pc_register = true; | |||
1478 | } | |||
1479 | ||||
1480 | lldb_private::UnwindLLDB::RegisterLocation regloc; | |||
1481 | if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, pc_register)) | |||
1482 | { | |||
1483 | return false; | |||
1484 | } | |||
1485 | if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value)) | |||
1486 | { | |||
1487 | value = reg_value.GetAsUInt64(); | |||
1488 | return true; | |||
1489 | } | |||
1490 | return false; | |||
1491 | } | |||
1492 | ||||
1493 | // Find the value of a register in THIS frame | |||
1494 | ||||
1495 | bool | |||
1496 | RegisterContextLLDB::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value) | |||
1497 | { | |||
1498 | if (!IsValid()) | |||
1499 | return false; | |||
1500 | ||||
1501 | const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB]; | |||
1502 | UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum); | |||
1503 | ||||
1504 | // If this is the 0th frame, hand this over to the live register context | |||
1505 | if (IsFrameZero ()) | |||
1506 | { | |||
1507 | UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum); | |||
1508 | return m_thread.GetRegisterContext()->ReadRegister (reg_info, value); | |||
1509 | } | |||
1510 | ||||
1511 | lldb_private::UnwindLLDB::RegisterLocation regloc; | |||
1512 | // Find out where the NEXT frame saved THIS frame's register contents | |||
1513 | if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false)) | |||
1514 | return false; | |||
1515 | ||||
1516 | return ReadRegisterValueFromRegisterLocation (regloc, reg_info, value); | |||
1517 | } | |||
1518 | ||||
1519 | bool | |||
1520 | RegisterContextLLDB::WriteRegister (const RegisterInfo *reg_info, const RegisterValue &value) | |||
1521 | { | |||
1522 | if (!IsValid()) | |||
1523 | return false; | |||
1524 | ||||
1525 | const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB]; | |||
1526 | UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum); | |||
1527 | ||||
1528 | // If this is the 0th frame, hand this over to the live register context | |||
1529 | if (IsFrameZero ()) | |||
1530 | { | |||
1531 | UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum); | |||
1532 | return m_thread.GetRegisterContext()->WriteRegister (reg_info, value); | |||
1533 | } | |||
1534 | ||||
1535 | lldb_private::UnwindLLDB::RegisterLocation regloc; | |||
1536 | // Find out where the NEXT frame saved THIS frame's register contents | |||
1537 | if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false)) | |||
1538 | return false; | |||
1539 | ||||
1540 | return WriteRegisterValueToRegisterLocation (regloc, reg_info, value); | |||
1541 | } | |||
1542 | ||||
1543 | // Don't need to implement this one | |||
1544 | bool | |||
1545 | RegisterContextLLDB::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) | |||
1546 | { | |||
1547 | return false; | |||
1548 | } | |||
1549 | ||||
1550 | // Don't need to implement this one | |||
1551 | bool | |||
1552 | RegisterContextLLDB::WriteAllRegisterValues (const lldb::DataBufferSP& data_sp) | |||
1553 | { | |||
1554 | return false; | |||
1555 | } | |||
1556 | ||||
1557 | // Retrieve the pc value for THIS from | |||
1558 | ||||
1559 | bool | |||
1560 | RegisterContextLLDB::GetCFA (addr_t& cfa) | |||
1561 | { | |||
1562 | if (!IsValid()) | |||
1563 | { | |||
1564 | return false; | |||
1565 | } | |||
1566 | if (m_cfa == LLDB_INVALID_ADDRESS(18446744073709551615UL)) | |||
1567 | { | |||
1568 | return false; | |||
1569 | } | |||
1570 | cfa = m_cfa; | |||
1571 | return true; | |||
1572 | } | |||
1573 | ||||
1574 | ||||
1575 | RegisterContextLLDB::SharedPtr | |||
1576 | RegisterContextLLDB::GetNextFrame () const | |||
1577 | { | |||
1578 | RegisterContextLLDB::SharedPtr regctx; | |||
1579 | if (m_frame_number == 0) | |||
1580 | return regctx; | |||
1581 | return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number - 1); | |||
1582 | } | |||
1583 | ||||
1584 | RegisterContextLLDB::SharedPtr | |||
1585 | RegisterContextLLDB::GetPrevFrame () const | |||
1586 | { | |||
1587 | RegisterContextLLDB::SharedPtr regctx; | |||
1588 | return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number + 1); | |||
1589 | } | |||
1590 | ||||
1591 | // Retrieve the address of the start of the function of THIS frame | |||
1592 | ||||
1593 | bool | |||
1594 | RegisterContextLLDB::GetStartPC (addr_t& start_pc) | |||
1595 | { | |||
1596 | if (!IsValid()) | |||
1597 | return false; | |||
1598 | ||||
1599 | if (!m_start_pc.IsValid()) | |||
1600 | { | |||
1601 | return ReadPC (start_pc); | |||
1602 | } | |||
1603 | start_pc = m_start_pc.GetLoadAddress (CalculateTarget().get()); | |||
1604 | return true; | |||
1605 | } | |||
1606 | ||||
1607 | // Retrieve the current pc value for THIS frame, as saved by the NEXT frame. | |||
1608 | ||||
1609 | bool | |||
1610 | RegisterContextLLDB::ReadPC (addr_t& pc) | |||
1611 | { | |||
1612 | if (!IsValid()) | |||
1613 | return false; | |||
1614 | ||||
1615 | if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC0, pc)) | |||
1616 | { | |||
1617 | // A pc value of 0 or 1 is impossible in the middle of the stack -- it indicates the end of a stack walk. | |||
1618 | // On the currently executing frame (or such a frame interrupted asynchronously by sigtramp et al) this may | |||
1619 | // occur if code has jumped through a NULL pointer -- we want to be able to unwind past that frame to help | |||
1620 | // find the bug. | |||
1621 | ||||
1622 | if (m_all_registers_available == false | |||
1623 | && (pc == 0 || pc == 1)) | |||
1624 | { | |||
1625 | return false; | |||
1626 | } | |||
1627 | else | |||
1628 | { | |||
1629 | return true; | |||
1630 | } | |||
1631 | } | |||
1632 | else | |||
1633 | { | |||
1634 | return false; | |||
1635 | } | |||
1636 | } | |||
1637 | ||||
1638 | ||||
1639 | void | |||
1640 | RegisterContextLLDB::UnwindLogMsg (const char *fmt, ...) | |||
1641 | { | |||
1642 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND(1u << 15))); | |||
1643 | if (log) | |||
1644 | { | |||
1645 | va_list args; | |||
1646 | va_start (args, fmt)__builtin_va_start(args, fmt); | |||
1647 | ||||
1648 | char *logmsg; | |||
1649 | if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL__null) | |||
1650 | { | |||
1651 | if (logmsg) | |||
1652 | free (logmsg); | |||
1653 | va_end (args)__builtin_va_end(args); | |||
1654 | return; | |||
1655 | } | |||
1656 | va_end (args)__builtin_va_end(args); | |||
1657 | ||||
1658 | log->Printf ("%*sth%d/fr%u %s", | |||
1659 | m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number, | |||
1660 | logmsg); | |||
1661 | free (logmsg); | |||
1662 | } | |||
1663 | } | |||
1664 | ||||
1665 | void | |||
1666 | RegisterContextLLDB::UnwindLogMsgVerbose (const char *fmt, ...) | |||
1667 | { | |||
1668 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND(1u << 15))); | |||
1669 | if (log && log->GetVerbose()) | |||
1670 | { | |||
1671 | va_list args; | |||
1672 | va_start (args, fmt)__builtin_va_start(args, fmt); | |||
1673 | ||||
1674 | char *logmsg; | |||
1675 | if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL__null) | |||
1676 | { | |||
1677 | if (logmsg) | |||
1678 | free (logmsg); | |||
1679 | va_end (args)__builtin_va_end(args); | |||
1680 | return; | |||
1681 | } | |||
1682 | va_end (args)__builtin_va_end(args); | |||
1683 | ||||
1684 | log->Printf ("%*sth%d/fr%u %s", | |||
1685 | m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number, | |||
1686 | logmsg); | |||
1687 | free (logmsg); | |||
1688 | } | |||
1689 | } | |||
1690 | ||||
1691 |