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), 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)); 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) 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) 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; 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 " (cfa_regval = 0x%16.16" PRIx64 ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset); 254 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64 " 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)); 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_PC, 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, pc); 298 addr_t reg_val; 299 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val)) 300 UnwindLogMsg ("fp = 0x%16.16" PRIx64, reg_val); 301 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val)) 302 UnwindLogMsg ("sp = 0x%16.16" PRIx64, 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 " 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; 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 || 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 UnwindLogMsg ("initialized frame cfa is 0x%" PRIx64, (uint64_t) m_cfa); 410 return; 411 } 412 m_frame_type = eNotAValidFrame; 413 UnwindLogMsg ("could not find any symbol for this pc, or a default unwind plan, to continue unwind."); 414 return; 415 } 416 417 bool resolve_tail_call_address = true; // m_current_pc can be one past the address range of the function... 418 // This will handle the case where the saved pc does not point to 419 // a function/symbol because it is beyond the bounds of the correct 420 // function and there's no symbol there. ResolveSymbolContextForAddress 421 // will fail to find a symbol, back up the pc by 1 and re-search. 422 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; 423 uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, 424 resolve_scope, 425 m_sym_ctx, resolve_tail_call_address); 426 427 // We require either a symbol or function in the symbols context to be successfully 428 // filled in or this context is of no use to us. 429 if (resolve_scope & resolved_scope) 430 { 431 m_sym_ctx_valid = true; 432 } 433 434 AddressRange addr_range; 435 if (!m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range)) 436 { 437 m_sym_ctx_valid = false; 438 } 439 440 bool decr_pc_and_recompute_addr_range = false; 441 442 // If the symbol lookup failed... 443 if (m_sym_ctx_valid == false) 444 decr_pc_and_recompute_addr_range = true; 445 446 // Or if we're in the middle of the stack (and not "above" an asynchronous event like sigtramp), 447 // and our "current" pc is the start of a function... 448 if (m_sym_ctx_valid 449 && GetNextFrame()->m_frame_type != eTrapHandlerFrame 450 && GetNextFrame()->m_frame_type != eDebuggerFrame 451 && addr_range.GetBaseAddress().IsValid() 452 && addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection() 453 && addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset()) 454 { 455 decr_pc_and_recompute_addr_range = true; 456 } 457 458 // We need to back up the pc by 1 byte and re-search for the Symbol to handle the case where the "saved pc" 459 // value is pointing to the next function, e.g. if a function ends with a CALL instruction. 460 // FIXME this may need to be an architectural-dependent behavior; if so we'll need to add a member function 461 // to the ABI plugin and consult that. 462 if (decr_pc_and_recompute_addr_range) 463 { 464 Address temporary_pc(m_current_pc); 465 temporary_pc.SetOffset(m_current_pc.GetOffset() - 1); 466 m_sym_ctx.Clear(false); 467 m_sym_ctx_valid = false; 468 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; 469 470 if (pc_module_sp->ResolveSymbolContextForAddress (temporary_pc, resolve_scope, m_sym_ctx) & resolve_scope) 471 { 472 if (m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range)) 473 m_sym_ctx_valid = true; 474 } 475 } 476 477 // If we were able to find a symbol/function, set addr_range_ptr to the bounds of that symbol/function. 478 // else treat the current pc value as the start_pc and record no offset. 479 if (addr_range.GetBaseAddress().IsValid()) 480 { 481 m_start_pc = addr_range.GetBaseAddress(); 482 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset(); 483 m_current_offset_backed_up_one = m_current_offset; 484 if (decr_pc_and_recompute_addr_range && m_current_offset_backed_up_one > 0) 485 { 486 m_current_offset_backed_up_one--; 487 if (m_sym_ctx_valid) 488 m_current_pc.SetOffset(m_current_pc.GetOffset() - 1); 489 } 490 } 491 else 492 { 493 m_start_pc = m_current_pc; 494 m_current_offset = -1; 495 m_current_offset_backed_up_one = -1; 496 } 497 498 if (IsTrapHandlerSymbol (process, m_sym_ctx)) 499 { 500 m_frame_type = eTrapHandlerFrame; 501 } 502 else 503 { 504 // FIXME: Detect eDebuggerFrame here. 505 if (m_frame_type != eSkipFrame) // don't override eSkipFrame 506 { 507 m_frame_type = eNormalFrame; 508 } 509 } 510 511 // We've set m_frame_type and m_sym_ctx before this call. 512 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame (); 513 514 UnwindPlan::RowSP active_row; 515 int cfa_offset = 0; 516 RegisterKind row_register_kind = eRegisterKindGeneric; 517 518 // Try to get by with just the fast UnwindPlan if possible - the full UnwindPlan may be expensive to get 519 // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for the first time.) 520 521 if (m_fast_unwind_plan_sp && m_fast_unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 522 { 523 active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 524 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind (); 525 if (active_row.get() && log) 526 { 527 StreamString active_row_strm; 528 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); 529 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str()); 530 } 531 } 532 else 533 { 534 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); 535 int valid_offset = -1; 536 if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp, valid_offset)) 537 { 538 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (valid_offset); 539 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); 540 if (active_row.get() && log) 541 { 542 StreamString active_row_strm; 543 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); 544 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str()); 545 } 546 } 547 } 548 549 if (!active_row.get()) 550 { 551 m_frame_type = eNotAValidFrame; 552 UnwindLogMsg ("could not find unwind row for this pc"); 553 return; 554 } 555 556 addr_t cfa_regval = LLDB_INVALID_ADDRESS; 557 if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval)) 558 { 559 UnwindLogMsg ("failed to get cfa reg %d/%d", row_register_kind, active_row->GetCFARegister()); 560 m_frame_type = eNotAValidFrame; 561 return; 562 } 563 564 cfa_offset = active_row->GetCFAOffset (); 565 m_cfa = cfa_regval + cfa_offset; 566 567 UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64 " (cfa_regval = 0x%16.16" PRIx64 ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset); 568 569 // A couple of sanity checks.. 570 if (cfa_regval == LLDB_INVALID_ADDRESS || cfa_regval == 0 || cfa_regval == 1) 571 { 572 UnwindLogMsg ("could not find a valid cfa address"); 573 m_frame_type = eNotAValidFrame; 574 return; 575 } 576 577 // If we have a bad stack setup, we can get the same CFA value multiple times -- or even 578 // more devious, we can actually oscillate between two CFA values. Detect that here and 579 // break out to avoid a possible infinite loop in lldb trying to unwind the stack. 580 addr_t next_frame_cfa; 581 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS; 582 if (GetNextFrame().get() && GetNextFrame()->GetCFA(next_frame_cfa)) 583 { 584 bool repeating_frames = false; 585 if (next_frame_cfa == m_cfa) 586 { 587 repeating_frames = true; 588 } 589 else 590 { 591 if (GetNextFrame()->GetNextFrame() && GetNextFrame()->GetNextFrame()->GetCFA(next_next_frame_cfa) 592 && next_next_frame_cfa == m_cfa) 593 { 594 repeating_frames = true; 595 } 596 } 597 if (repeating_frames && abi && abi->FunctionCallsChangeCFA()) 598 { 599 UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping"); 600 m_frame_type = eNotAValidFrame; 601 return; 602 } 603 } 604 605 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64, 606 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), (uint64_t) m_cfa); 607 } 608 609 610 bool 611 RegisterContextLLDB::IsFrameZero () const 612 { 613 return m_frame_number == 0; 614 } 615 616 617 // Find a fast unwind plan for this frame, if possible. 618 // 619 // On entry to this method, 620 // 621 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct, 622 // 2. m_sym_ctx should already be filled in, and 623 // 3. m_current_pc should have the current pc value for this frame 624 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown 625 626 UnwindPlanSP 627 RegisterContextLLDB::GetFastUnwindPlanForFrame () 628 { 629 UnwindPlanSP unwind_plan_sp; 630 ModuleSP pc_module_sp (m_current_pc.GetModule()); 631 632 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL) 633 return unwind_plan_sp; 634 635 if (IsFrameZero ()) 636 return unwind_plan_sp; 637 638 FuncUnwindersSP func_unwinders_sp (pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx)); 639 if (!func_unwinders_sp) 640 return unwind_plan_sp; 641 642 // If we're in _sigtramp(), unwinding past this frame requires special knowledge. 643 if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame) 644 return unwind_plan_sp; 645 646 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind (m_thread); 647 if (unwind_plan_sp) 648 { 649 if (unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 650 { 651 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 652 if (log && log->GetVerbose()) 653 { 654 if (m_fast_unwind_plan_sp) 655 UnwindLogMsgVerbose ("frame, and has a fast UnwindPlan"); 656 else 657 UnwindLogMsgVerbose ("frame"); 658 } 659 m_frame_type = eNormalFrame; 660 return unwind_plan_sp; 661 } 662 else 663 { 664 unwind_plan_sp.reset(); 665 } 666 } 667 return unwind_plan_sp; 668 } 669 670 // On entry to this method, 671 // 672 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct, 673 // 2. m_sym_ctx should already be filled in, and 674 // 3. m_current_pc should have the current pc value for this frame 675 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown 676 677 UnwindPlanSP 678 RegisterContextLLDB::GetFullUnwindPlanForFrame () 679 { 680 UnwindPlanSP unwind_plan_sp; 681 UnwindPlanSP arch_default_unwind_plan_sp; 682 ExecutionContext exe_ctx(m_thread.shared_from_this()); 683 Process *process = exe_ctx.GetProcessPtr(); 684 ABI *abi = process ? process->GetABI().get() : NULL; 685 if (abi) 686 { 687 arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 688 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp); 689 } 690 else 691 { 692 UnwindLogMsg ("unable to get architectural default UnwindPlan from ABI plugin"); 693 } 694 695 bool behaves_like_zeroth_frame = false; 696 if (IsFrameZero () 697 || GetNextFrame()->m_frame_type == eTrapHandlerFrame 698 || GetNextFrame()->m_frame_type == eDebuggerFrame) 699 { 700 behaves_like_zeroth_frame = true; 701 // If this frame behaves like a 0th frame (currently executing or 702 // interrupted asynchronously), all registers can be retrieved. 703 m_all_registers_available = true; 704 } 705 706 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer) so the pc is 0x0 707 // in the zeroth frame, we need to use the "unwind at first instruction" arch default UnwindPlan 708 // Also, if this Process can report on memory region attributes, any non-executable region means 709 // we jumped through a bad function pointer - handle the same way as 0x0. 710 // Note, if we have a symbol context & a symbol, we don't want to follow this code path. This is 711 // for jumping to memory regions without any information available. 712 713 if ((!m_sym_ctx_valid || (m_sym_ctx.function == NULL && m_sym_ctx.symbol == NULL)) && behaves_like_zeroth_frame && m_current_pc.IsValid()) 714 { 715 uint32_t permissions; 716 addr_t current_pc_addr = m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()); 717 if (current_pc_addr == 0 718 || (process->GetLoadAddressPermissions (current_pc_addr, permissions) 719 && (permissions & ePermissionsExecutable) == 0)) 720 { 721 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 722 abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp); 723 m_frame_type = eNormalFrame; 724 return unwind_plan_sp; 725 } 726 } 727 728 // No Module for the current pc, try using the architecture default unwind. 729 ModuleSP pc_module_sp (m_current_pc.GetModule()); 730 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL) 731 { 732 m_frame_type = eNormalFrame; 733 return arch_default_unwind_plan_sp; 734 } 735 736 FuncUnwindersSP func_unwinders_sp; 737 if (m_sym_ctx_valid) 738 { 739 func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx); 740 } 741 742 // No FuncUnwinders available for this pc (i.e. a stripped function symbol and -fomit-frame-pointer). 743 // Try using the eh_frame information relative to the current PC, 744 // and finally fall back on the architectural default unwind. 745 if (!func_unwinders_sp) 746 { 747 DWARFCallFrameInfo *eh_frame = pc_module_sp && pc_module_sp->GetObjectFile() ? 748 pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo() : nullptr; 749 750 m_frame_type = eNormalFrame; 751 if (eh_frame && m_current_pc.IsValid()) 752 { 753 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 754 // Even with -fomit-frame-pointer, we can try eh_frame to get back on track. 755 if (eh_frame->GetUnwindPlan (m_current_pc, *unwind_plan_sp)) 756 return unwind_plan_sp; 757 else 758 unwind_plan_sp.reset(); 759 } 760 return arch_default_unwind_plan_sp; 761 } 762 763 // If we're in _sigtramp(), unwinding past this frame requires special knowledge. On Mac OS X this knowledge 764 // is properly encoded in the eh_frame section, so prefer that if available. 765 // On other platforms we may need to provide a platform-specific UnwindPlan which encodes the details of 766 // how to unwind out of sigtramp. 767 if (m_frame_type == eTrapHandlerFrame) 768 { 769 m_fast_unwind_plan_sp.reset(); 770 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one); 771 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc) && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) 772 { 773 return unwind_plan_sp; 774 } 775 } 776 777 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame even when it's frame zero 778 // This comes up if we have hand-written functions in a Module and hand-written eh_frame. The assembly 779 // instruction inspection may fail and the eh_frame CFI were probably written with some care to do the 780 // right thing. It'd be nice if there was a way to ask the eh_frame directly if it is asynchronous 781 // (can be trusted at every instruction point) or synchronous (the normal case - only at call sites). 782 // But there is not. 783 if (process && process->GetDynamicLoader() && process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo (m_sym_ctx)) 784 { 785 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one); 786 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 787 { 788 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan because the DynamicLoader suggested we prefer it", 789 unwind_plan_sp->GetSourceName().GetCString()); 790 return unwind_plan_sp; 791 } 792 } 793 794 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting the assembly language instructions 795 if (behaves_like_zeroth_frame) 796 { 797 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one); 798 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 799 { 800 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) 801 { 802 // We probably have an UnwindPlan created by inspecting assembly instructions, and we probably 803 // don't have any eh_frame instructions available. 804 // The assembly profilers work really well with compiler-generated functions but hand-written 805 // assembly can be problematic. We'll set the architecture default UnwindPlan as our fallback 806 // UnwindPlan in case this doesn't work out when we try to unwind. 807 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp; 808 } 809 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 810 return unwind_plan_sp; 811 } 812 } 813 814 // Typically this is unwind info from an eh_frame section intended for exception handling; only valid at call sites 815 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one); 816 int valid_offset = -1; 817 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) 818 { 819 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 820 return unwind_plan_sp; 821 } 822 823 // We'd prefer to use an UnwindPlan intended for call sites when we're at a call site but if we've 824 // struck out on that, fall back to using the non-call-site assembly inspection UnwindPlan if possible. 825 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one); 826 if (unwind_plan_sp && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) 827 { 828 // We probably have an UnwindPlan created by inspecting assembly instructions, and we probably 829 // don't have any eh_frame instructions available. 830 // The assembly profilers work really well with compiler-generated functions but hand-written 831 // assembly can be problematic. We'll set the architecture default UnwindPlan as our fallback 832 // UnwindPlan in case this doesn't work out when we try to unwind. 833 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp; 834 } 835 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 // If we're on the first instruction of a function, and we have an architectural default UnwindPlan 843 // for the initial instruction of a function, use that. 844 if (m_current_offset_backed_up_one == 0) 845 { 846 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry (m_thread); 847 if (unwind_plan_sp) 848 { 849 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 850 return unwind_plan_sp; 851 } 852 } 853 854 // If nothing else, use the architectural default UnwindPlan and hope that does the job. 855 if (arch_default_unwind_plan_sp) 856 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", arch_default_unwind_plan_sp->GetSourceName().GetCString()); 857 else 858 UnwindLogMsg ("Unable to find any UnwindPlan for full unwind of this frame."); 859 860 return arch_default_unwind_plan_sp; 861 } 862 863 864 void 865 RegisterContextLLDB::InvalidateAllRegisters () 866 { 867 m_frame_type = eNotAValidFrame; 868 } 869 870 size_t 871 RegisterContextLLDB::GetRegisterCount () 872 { 873 return m_thread.GetRegisterContext()->GetRegisterCount(); 874 } 875 876 const RegisterInfo * 877 RegisterContextLLDB::GetRegisterInfoAtIndex (size_t reg) 878 { 879 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex (reg); 880 } 881 882 size_t 883 RegisterContextLLDB::GetRegisterSetCount () 884 { 885 return m_thread.GetRegisterContext()->GetRegisterSetCount (); 886 } 887 888 const RegisterSet * 889 RegisterContextLLDB::GetRegisterSet (size_t reg_set) 890 { 891 return m_thread.GetRegisterContext()->GetRegisterSet (reg_set); 892 } 893 894 uint32_t 895 RegisterContextLLDB::ConvertRegisterKindToRegisterNumber (lldb::RegisterKind kind, uint32_t num) 896 { 897 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber (kind, num); 898 } 899 900 bool 901 RegisterContextLLDB::ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc, 902 const RegisterInfo *reg_info, 903 RegisterValue &value) 904 { 905 if (!IsValid()) 906 return false; 907 bool success = false; 908 909 switch (regloc.type) 910 { 911 case UnwindLLDB::RegisterLocation::eRegisterInRegister: 912 { 913 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); 914 915 if (!other_reg_info) 916 return false; 917 918 if (IsFrameZero ()) 919 { 920 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value); 921 } 922 else 923 { 924 success = GetNextFrame()->ReadRegister (other_reg_info, value); 925 } 926 } 927 break; 928 case UnwindLLDB::RegisterLocation::eRegisterValueInferred: 929 success = value.SetUInt (regloc.location.inferred_value, reg_info->byte_size); 930 break; 931 932 case UnwindLLDB::RegisterLocation::eRegisterNotSaved: 933 break; 934 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation: 935 assert ("FIXME debugger inferior function call unwind"); 936 break; 937 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: 938 { 939 Error error (ReadRegisterValueFromMemory(reg_info, 940 regloc.location.target_memory_location, 941 reg_info->byte_size, 942 value)); 943 success = error.Success(); 944 } 945 break; 946 default: 947 assert ("Unknown RegisterLocation type."); 948 break; 949 } 950 return success; 951 } 952 953 bool 954 RegisterContextLLDB::WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc, 955 const RegisterInfo *reg_info, 956 const RegisterValue &value) 957 { 958 if (!IsValid()) 959 return false; 960 961 bool success = false; 962 963 switch (regloc.type) 964 { 965 case UnwindLLDB::RegisterLocation::eRegisterInRegister: 966 { 967 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); 968 if (IsFrameZero ()) 969 { 970 success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value); 971 } 972 else 973 { 974 success = GetNextFrame()->WriteRegister (other_reg_info, value); 975 } 976 } 977 break; 978 case UnwindLLDB::RegisterLocation::eRegisterValueInferred: 979 case UnwindLLDB::RegisterLocation::eRegisterNotSaved: 980 break; 981 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation: 982 assert ("FIXME debugger inferior function call unwind"); 983 break; 984 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: 985 { 986 Error error (WriteRegisterValueToMemory (reg_info, 987 regloc.location.target_memory_location, 988 reg_info->byte_size, 989 value)); 990 success = error.Success(); 991 } 992 break; 993 default: 994 assert ("Unknown RegisterLocation type."); 995 break; 996 } 997 return success; 998 } 999 1000 1001 bool 1002 RegisterContextLLDB::IsValid () const 1003 { 1004 return m_frame_type != eNotAValidFrame; 1005 } 1006 1007 bool 1008 RegisterContextLLDB::IsTrapHandlerFrame () const 1009 { 1010 return m_frame_type == eTrapHandlerFrame; 1011 } 1012 1013 // A skip frame is a bogus frame on the stack -- but one where we're likely to find a real frame farther 1014 // up the stack if we keep looking. It's always the second frame in an unwind (i.e. the first frame after 1015 // frame zero) where unwinding can be the trickiest. Ideally we'll mark up this frame in some way so the 1016 // user knows we're displaying bad data and we may have skipped one frame of their real program in the 1017 // process of getting back on track. 1018 1019 bool 1020 RegisterContextLLDB::IsSkipFrame () const 1021 { 1022 return m_frame_type == eSkipFrame; 1023 } 1024 1025 bool 1026 RegisterContextLLDB::IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const 1027 { 1028 PlatformSP platform_sp (process->GetTarget().GetPlatform()); 1029 if (platform_sp) 1030 { 1031 const std::vector<ConstString> trap_handler_names (platform_sp->GetTrapHandlerSymbolNames()); 1032 for (ConstString name : trap_handler_names) 1033 { 1034 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) || 1035 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) 1036 { 1037 return true; 1038 } 1039 } 1040 } 1041 const std::vector<ConstString> user_specified_trap_handler_names (m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames()); 1042 for (ConstString name : user_specified_trap_handler_names) 1043 { 1044 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) || 1045 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) 1046 { 1047 return true; 1048 } 1049 } 1050 1051 return false; 1052 } 1053 1054 // Answer the question: Where did THIS frame save the CALLER frame ("previous" frame)'s register value? 1055 1056 enum UnwindLLDB::RegisterSearchResult 1057 RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc) 1058 { 1059 // Have we already found this register location? 1060 if (!m_registers.empty()) 1061 { 1062 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>::const_iterator iterator; 1063 iterator = m_registers.find (lldb_regnum); 1064 if (iterator != m_registers.end()) 1065 { 1066 regloc = iterator->second; 1067 UnwindLogMsg ("supplying caller's saved reg %d's location, cached", lldb_regnum); 1068 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1069 } 1070 } 1071 1072 uint32_t sp_regnum = LLDB_INVALID_REGNUM; 1073 uint32_t pc_regnum = LLDB_INVALID_REGNUM; 1074 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, eRegisterKindLLDB, sp_regnum); 1075 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, eRegisterKindLLDB, pc_regnum); 1076 1077 // Are we looking for the CALLER's stack pointer? The stack pointer is defined to be the same as THIS frame's 1078 // CFA so just return the CFA value. This is true on x86-32/x86-64 at least. 1079 if (sp_regnum != LLDB_INVALID_REGNUM && sp_regnum == lldb_regnum) 1080 { 1081 // make sure we won't lose precision copying an addr_t (m_cfa) into a uint64_t (.inferred_value) 1082 assert (sizeof (addr_t) <= sizeof (uint64_t)); 1083 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; 1084 regloc.location.inferred_value = m_cfa; 1085 m_registers[lldb_regnum] = regloc; 1086 UnwindLogMsg ("supplying caller's stack pointer (%d) value, computed from CFA", lldb_regnum); 1087 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1088 } 1089 1090 // Look through the available UnwindPlans for the register location. 1091 1092 UnwindPlan::Row::RegisterLocation unwindplan_regloc; 1093 bool have_unwindplan_regloc = false; 1094 RegisterKind unwindplan_registerkind = (RegisterKind)-1; 1095 1096 if (m_fast_unwind_plan_sp) 1097 { 1098 UnwindPlan::RowSP active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1099 unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind (); 1100 uint32_t row_regnum; 1101 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum)) 1102 { 1103 UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme", 1104 lldb_regnum, (int) unwindplan_registerkind); 1105 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1106 } 1107 if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc)) 1108 { 1109 UnwindLogMsg ("supplying caller's saved reg %d's location using FastUnwindPlan", lldb_regnum); 1110 have_unwindplan_regloc = true; 1111 } 1112 } 1113 1114 if (!have_unwindplan_regloc) 1115 { 1116 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a full UnwindPlan yet 1117 if (!m_full_unwind_plan_sp) 1118 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); 1119 1120 if (m_full_unwind_plan_sp) 1121 { 1122 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1123 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind (); 1124 uint32_t row_regnum; 1125 bool row_register_rewritten_to_return_address_reg = false; 1126 1127 // If we're fetching the saved pc and this UnwindPlan defines a ReturnAddress register (e.g. lr on arm), 1128 // look for the return address register number in the UnwindPlan's row. 1129 if (lldb_regnum == pc_regnum && m_full_unwind_plan_sp->GetReturnAddressRegister() != LLDB_INVALID_REGNUM) 1130 { 1131 row_regnum = m_full_unwind_plan_sp->GetReturnAddressRegister(); 1132 row_register_rewritten_to_return_address_reg = true; 1133 UnwindLogMsg ("requested caller's saved PC but this UnwindPlan uses a RA reg; getting reg %d instead", 1134 row_regnum); 1135 } 1136 else 1137 { 1138 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum)) 1139 { 1140 if (unwindplan_registerkind == eRegisterKindGeneric) 1141 UnwindLogMsg ("could not convert lldb regnum %d into eRegisterKindGeneric reg numbering scheme", lldb_regnum); 1142 else 1143 UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme", 1144 lldb_regnum, (int) unwindplan_registerkind); 1145 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1146 } 1147 } 1148 1149 if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc)) 1150 { 1151 have_unwindplan_regloc = true; 1152 UnwindLogMsg ("supplying caller's saved reg %d's location using %s UnwindPlan", lldb_regnum, 1153 m_full_unwind_plan_sp->GetSourceName().GetCString()); 1154 } 1155 1156 // This is frame 0 and we're retrieving the PC and it's saved in a Return Address register and 1157 // it hasn't been saved anywhere yet -- that is, it's still live in the actual register. 1158 // Handle this specially. 1159 1160 if (have_unwindplan_regloc == false 1161 && row_register_rewritten_to_return_address_reg == true 1162 && IsFrameZero() 1163 && row_regnum != LLDB_INVALID_REGNUM) 1164 { 1165 uint32_t ra_regnum_in_lldb_reg_numbering; 1166 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, row_regnum, eRegisterKindLLDB, ra_regnum_in_lldb_reg_numbering)) 1167 { 1168 lldb_private::UnwindLLDB::RegisterLocation new_regloc; 1169 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; 1170 new_regloc.location.register_number = ra_regnum_in_lldb_reg_numbering; 1171 m_registers[lldb_regnum] = new_regloc; 1172 regloc = new_regloc; 1173 UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0, saved in %d", lldb_regnum, ra_regnum_in_lldb_reg_numbering); 1174 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1175 } 1176 } 1177 1178 // If this architecture stores the return address in a register (it defines a Return Address register) 1179 // and we're on a non-zero stack frame and the Full UnwindPlan says that the pc is stored in the 1180 // RA registers (e.g. lr on arm), then we know that the full unwindplan is not trustworthy -- this 1181 // is an impossible situation and the instruction emulation code has likely been misled. 1182 // If this stack frame meets those criteria, we need to throw away the Full UnwindPlan that the 1183 // instruction emulation came up with and fall back to the architecture's Default UnwindPlan so 1184 // the stack walk can get past this point. 1185 1186 // Special note: If the Full UnwindPlan was generated from the compiler, don't second-guess it 1187 // when we're at a call site location. 1188 1189 // arch_default_ra_regnum is the return address register # in the Full UnwindPlan register numbering 1190 uint32_t arch_default_ra_regnum = LLDB_INVALID_REGNUM; 1191 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, unwindplan_registerkind, arch_default_ra_regnum) 1192 && arch_default_ra_regnum != LLDB_INVALID_REGNUM 1193 && pc_regnum != LLDB_INVALID_REGNUM 1194 && pc_regnum == lldb_regnum 1195 && unwindplan_regloc.IsInOtherRegister() 1196 && unwindplan_regloc.GetRegisterNumber() == arch_default_ra_regnum 1197 && m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes 1198 && !m_all_registers_available) 1199 { 1200 UnwindLogMsg ("%s UnwindPlan tried to restore the pc from the link register but this is a non-zero frame", 1201 m_full_unwind_plan_sp->GetSourceName().GetCString()); 1202 1203 // Throw away the full unwindplan; install the arch default unwindplan 1204 if (TryFallbackUnwindPlan()) 1205 { 1206 // Now re-fetch the pc value we're searching for 1207 uint32_t arch_default_pc_reg = LLDB_INVALID_REGNUM; 1208 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1209 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, m_full_unwind_plan_sp->GetRegisterKind(), arch_default_pc_reg) 1210 && arch_default_pc_reg != LLDB_INVALID_REGNUM 1211 && active_row 1212 && active_row->GetRegisterInfo (arch_default_pc_reg, unwindplan_regloc)) 1213 { 1214 have_unwindplan_regloc = true; 1215 } 1216 else 1217 { 1218 have_unwindplan_regloc = false; 1219 } 1220 } 1221 } 1222 } 1223 } 1224 1225 1226 ExecutionContext exe_ctx(m_thread.shared_from_this()); 1227 Process *process = exe_ctx.GetProcessPtr(); 1228 if (have_unwindplan_regloc == false) 1229 { 1230 // If a volatile register is being requested, we don't want to forward the next frame's register contents 1231 // up the stack -- the register is not retrievable at this frame. 1232 ABI *abi = process ? process->GetABI().get() : NULL; 1233 if (abi) 1234 { 1235 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum); 1236 if (reg_info && abi->RegisterIsVolatile (reg_info)) 1237 { 1238 UnwindLogMsg ("did not supply reg location for %d (%s) because it is volatile", 1239 lldb_regnum, reg_info->name ? reg_info->name : "??"); 1240 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile; 1241 } 1242 } 1243 1244 if (IsFrameZero ()) 1245 { 1246 // This is frame 0 - we should return the actual live register context value 1247 lldb_private::UnwindLLDB::RegisterLocation new_regloc; 1248 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; 1249 new_regloc.location.register_number = lldb_regnum; 1250 m_registers[lldb_regnum] = new_regloc; 1251 regloc = new_regloc; 1252 UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0", lldb_regnum); 1253 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1254 } 1255 else 1256 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); 1257 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1258 } 1259 1260 // unwindplan_regloc has valid contents about where to retrieve the register 1261 if (unwindplan_regloc.IsUnspecified()) 1262 { 1263 lldb_private::UnwindLLDB::RegisterLocation new_regloc; 1264 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved; 1265 m_registers[lldb_regnum] = new_regloc; 1266 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); 1267 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1268 } 1269 1270 if (unwindplan_regloc.IsSame()) 1271 { 1272 if (IsFrameZero ()) 1273 { 1274 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); 1275 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1276 } 1277 else 1278 { 1279 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1280 } 1281 } 1282 1283 if (unwindplan_regloc.IsCFAPlusOffset()) 1284 { 1285 int offset = unwindplan_regloc.GetOffset(); 1286 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; 1287 regloc.location.inferred_value = m_cfa + offset; 1288 m_registers[lldb_regnum] = regloc; 1289 UnwindLogMsg ("supplying caller's register %d, value is CFA plus offset %d", lldb_regnum, offset); 1290 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1291 } 1292 1293 if (unwindplan_regloc.IsAtCFAPlusOffset()) 1294 { 1295 int offset = unwindplan_regloc.GetOffset(); 1296 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation; 1297 regloc.location.target_memory_location = m_cfa + offset; 1298 m_registers[lldb_regnum] = regloc; 1299 UnwindLogMsg ("supplying caller's register %d from the stack, saved at CFA plus offset %d", lldb_regnum, offset); 1300 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1301 } 1302 1303 if (unwindplan_regloc.IsInOtherRegister()) 1304 { 1305 uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber(); 1306 uint32_t row_regnum_in_lldb; 1307 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, unwindplan_regnum, eRegisterKindLLDB, row_regnum_in_lldb)) 1308 { 1309 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); 1310 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1311 } 1312 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; 1313 regloc.location.register_number = row_regnum_in_lldb; 1314 m_registers[lldb_regnum] = regloc; 1315 UnwindLogMsg ("supplying caller's register %d, saved in register %d", lldb_regnum, row_regnum_in_lldb); 1316 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1317 } 1318 1319 if (unwindplan_regloc.IsDWARFExpression() || unwindplan_regloc.IsAtDWARFExpression()) 1320 { 1321 DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(), 1322 unwindplan_regloc.GetDWARFExpressionLength(), 1323 process->GetByteOrder(), process->GetAddressByteSize()); 1324 ModuleSP opcode_ctx; 1325 DWARFExpression dwarfexpr (opcode_ctx, dwarfdata, 0, unwindplan_regloc.GetDWARFExpressionLength()); 1326 dwarfexpr.SetRegisterKind (unwindplan_registerkind); 1327 Value result; 1328 Error error; 1329 if (dwarfexpr.Evaluate (&exe_ctx, NULL, NULL, this, 0, NULL, result, &error)) 1330 { 1331 addr_t val; 1332 val = result.GetScalar().ULongLong(); 1333 if (unwindplan_regloc.IsDWARFExpression()) 1334 { 1335 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; 1336 regloc.location.inferred_value = val; 1337 m_registers[lldb_regnum] = regloc; 1338 UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsDWARFExpression)", lldb_regnum); 1339 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1340 } 1341 else 1342 { 1343 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation; 1344 regloc.location.target_memory_location = val; 1345 m_registers[lldb_regnum] = regloc; 1346 UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsAtDWARFExpression)", lldb_regnum); 1347 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1348 } 1349 } 1350 UnwindLogMsg ("tried to use IsDWARFExpression or IsAtDWARFExpression for reg %d but failed", lldb_regnum); 1351 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1352 } 1353 1354 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum); 1355 1356 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are unsupported. 1357 1358 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1359 } 1360 1361 // If the Full unwindplan has been determined to be incorrect, this method will 1362 // replace it with the architecture's default unwindplan, if one is defined. 1363 // It will also find the FuncUnwinders object for this function and replace the 1364 // Full unwind method for the function there so we don't use the errant Full unwindplan 1365 // again in the future of this debug session. 1366 // We're most likely doing this because the Full unwindplan was generated by assembly 1367 // instruction profiling and the profiler got something wrong. 1368 1369 bool 1370 RegisterContextLLDB::TryFallbackUnwindPlan () 1371 { 1372 UnwindPlan::Row::RegisterLocation unwindplan_regloc; 1373 if (m_fallback_unwind_plan_sp.get() == NULL) 1374 return false; 1375 1376 UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp; 1377 UnwindPlan::RowSP active_row = m_fallback_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1378 1379 if (active_row && active_row->GetCFARegister() != LLDB_INVALID_REGNUM) 1380 { 1381 FuncUnwindersSP func_unwinders_sp; 1382 if (m_sym_ctx_valid && m_current_pc.IsValid() && m_current_pc.GetModule()) 1383 { 1384 func_unwinders_sp = m_current_pc.GetModule()->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx); 1385 if (func_unwinders_sp) 1386 { 1387 func_unwinders_sp->InvalidateNonCallSiteUnwindPlan (m_thread); 1388 } 1389 } 1390 m_registers.clear(); 1391 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp; 1392 addr_t cfa_regval = LLDB_INVALID_ADDRESS; 1393 if (ReadGPRValue (m_fallback_unwind_plan_sp->GetRegisterKind(), active_row->GetCFARegister(), cfa_regval)) 1394 { 1395 m_cfa = cfa_regval + active_row->GetCFAOffset (); 1396 } 1397 1398 UnwindLogMsg ("full unwind plan '%s' has been replaced by architecture default unwind plan '%s' for this function from now on.", 1399 original_full_unwind_plan_sp->GetSourceName().GetCString(), m_fallback_unwind_plan_sp->GetSourceName().GetCString()); 1400 m_fallback_unwind_plan_sp.reset(); 1401 } 1402 1403 return true; 1404 } 1405 1406 // Retrieve a general purpose register value for THIS frame, as saved by the NEXT frame, i.e. the frame that 1407 // this frame called. e.g. 1408 // 1409 // foo () { } 1410 // bar () { foo (); } 1411 // main () { bar (); } 1412 // 1413 // stopped in foo() so 1414 // frame 0 - foo 1415 // frame 1 - bar 1416 // frame 2 - main 1417 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc value for frame 1, we need to ask 1418 // where frame 0 (the "next" frame) saved that and retrieve the value. 1419 1420 bool 1421 RegisterContextLLDB::ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, addr_t &value) 1422 { 1423 if (!IsValid()) 1424 return false; 1425 1426 uint32_t lldb_regnum; 1427 if (register_kind == eRegisterKindLLDB) 1428 { 1429 lldb_regnum = regnum; 1430 } 1431 else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) 1432 { 1433 return false; 1434 } 1435 1436 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum); 1437 RegisterValue reg_value; 1438 // if this is frame 0 (currently executing frame), get the requested reg contents from the actual thread registers 1439 if (IsFrameZero ()) 1440 { 1441 if (m_thread.GetRegisterContext()->ReadRegister (reg_info, reg_value)) 1442 { 1443 value = reg_value.GetAsUInt64(); 1444 return true; 1445 } 1446 return false; 1447 } 1448 1449 bool pc_register = false; 1450 uint32_t generic_regnum; 1451 if (register_kind == eRegisterKindGeneric && regnum == LLDB_REGNUM_GENERIC_PC) 1452 { 1453 pc_register = true; 1454 } 1455 else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindGeneric, generic_regnum) 1456 && generic_regnum == LLDB_REGNUM_GENERIC_PC) 1457 { 1458 pc_register = true; 1459 } 1460 1461 lldb_private::UnwindLLDB::RegisterLocation regloc; 1462 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, pc_register)) 1463 { 1464 return false; 1465 } 1466 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value)) 1467 { 1468 value = reg_value.GetAsUInt64(); 1469 return true; 1470 } 1471 return false; 1472 } 1473 1474 // Find the value of a register in THIS frame 1475 1476 bool 1477 RegisterContextLLDB::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value) 1478 { 1479 if (!IsValid()) 1480 return false; 1481 1482 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB]; 1483 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum); 1484 1485 // If this is the 0th frame, hand this over to the live register context 1486 if (IsFrameZero ()) 1487 { 1488 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum); 1489 return m_thread.GetRegisterContext()->ReadRegister (reg_info, value); 1490 } 1491 1492 lldb_private::UnwindLLDB::RegisterLocation regloc; 1493 // Find out where the NEXT frame saved THIS frame's register contents 1494 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false)) 1495 return false; 1496 1497 return ReadRegisterValueFromRegisterLocation (regloc, reg_info, value); 1498 } 1499 1500 bool 1501 RegisterContextLLDB::WriteRegister (const RegisterInfo *reg_info, const RegisterValue &value) 1502 { 1503 if (!IsValid()) 1504 return false; 1505 1506 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB]; 1507 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum); 1508 1509 // If this is the 0th frame, hand this over to the live register context 1510 if (IsFrameZero ()) 1511 { 1512 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum); 1513 return m_thread.GetRegisterContext()->WriteRegister (reg_info, value); 1514 } 1515 1516 lldb_private::UnwindLLDB::RegisterLocation regloc; 1517 // Find out where the NEXT frame saved THIS frame's register contents 1518 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false)) 1519 return false; 1520 1521 return WriteRegisterValueToRegisterLocation (regloc, reg_info, value); 1522 } 1523 1524 // Don't need to implement this one 1525 bool 1526 RegisterContextLLDB::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) 1527 { 1528 return false; 1529 } 1530 1531 // Don't need to implement this one 1532 bool 1533 RegisterContextLLDB::WriteAllRegisterValues (const lldb::DataBufferSP& data_sp) 1534 { 1535 return false; 1536 } 1537 1538 // Retrieve the pc value for THIS from 1539 1540 bool 1541 RegisterContextLLDB::GetCFA (addr_t& cfa) 1542 { 1543 if (!IsValid()) 1544 { 1545 return false; 1546 } 1547 if (m_cfa == LLDB_INVALID_ADDRESS) 1548 { 1549 return false; 1550 } 1551 cfa = m_cfa; 1552 return true; 1553 } 1554 1555 1556 RegisterContextLLDB::SharedPtr 1557 RegisterContextLLDB::GetNextFrame () const 1558 { 1559 RegisterContextLLDB::SharedPtr regctx; 1560 if (m_frame_number == 0) 1561 return regctx; 1562 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number - 1); 1563 } 1564 1565 RegisterContextLLDB::SharedPtr 1566 RegisterContextLLDB::GetPrevFrame () const 1567 { 1568 RegisterContextLLDB::SharedPtr regctx; 1569 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number + 1); 1570 } 1571 1572 // Retrieve the address of the start of the function of THIS frame 1573 1574 bool 1575 RegisterContextLLDB::GetStartPC (addr_t& start_pc) 1576 { 1577 if (!IsValid()) 1578 return false; 1579 1580 if (!m_start_pc.IsValid()) 1581 { 1582 return ReadPC (start_pc); 1583 } 1584 start_pc = m_start_pc.GetLoadAddress (CalculateTarget().get()); 1585 return true; 1586 } 1587 1588 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame. 1589 1590 bool 1591 RegisterContextLLDB::ReadPC (addr_t& pc) 1592 { 1593 if (!IsValid()) 1594 return false; 1595 1596 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) 1597 { 1598 // A pc value of 0 or 1 is impossible in the middle of the stack -- it indicates the end of a stack walk. 1599 // On the currently executing frame (or such a frame interrupted asynchronously by sigtramp et al) this may 1600 // occur if code has jumped through a NULL pointer -- we want to be able to unwind past that frame to help 1601 // find the bug. 1602 1603 if (m_all_registers_available == false 1604 && (pc == 0 || pc == 1)) 1605 { 1606 return false; 1607 } 1608 else 1609 { 1610 return true; 1611 } 1612 } 1613 else 1614 { 1615 return false; 1616 } 1617 } 1618 1619 1620 void 1621 RegisterContextLLDB::UnwindLogMsg (const char *fmt, ...) 1622 { 1623 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 1624 if (log) 1625 { 1626 va_list args; 1627 va_start (args, fmt); 1628 1629 char *logmsg; 1630 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL) 1631 { 1632 if (logmsg) 1633 free (logmsg); 1634 va_end (args); 1635 return; 1636 } 1637 va_end (args); 1638 1639 log->Printf ("%*sth%d/fr%u %s", 1640 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number, 1641 logmsg); 1642 free (logmsg); 1643 } 1644 } 1645 1646 void 1647 RegisterContextLLDB::UnwindLogMsgVerbose (const char *fmt, ...) 1648 { 1649 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 1650 if (log && log->GetVerbose()) 1651 { 1652 va_list args; 1653 va_start (args, fmt); 1654 1655 char *logmsg; 1656 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL) 1657 { 1658 if (logmsg) 1659 free (logmsg); 1660 va_end (args); 1661 return; 1662 } 1663 va_end (args); 1664 1665 log->Printf ("%*sth%d/fr%u %s", 1666 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number, 1667 logmsg); 1668 free (logmsg); 1669 } 1670 } 1671 1672 1673