1 //===-- Target.cpp ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/lldb-python.h" 11 12 #include "lldb/Target/Target.h" 13 14 // C Includes 15 // C++ Includes 16 // Other libraries and framework includes 17 // Project includes 18 #include "lldb/Breakpoint/BreakpointResolver.h" 19 #include "lldb/Breakpoint/BreakpointResolverAddress.h" 20 #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 21 #include "lldb/Breakpoint/BreakpointResolverFileRegex.h" 22 #include "lldb/Breakpoint/BreakpointResolverName.h" 23 #include "lldb/Breakpoint/Watchpoint.h" 24 #include "lldb/Core/Debugger.h" 25 #include "lldb/Core/Event.h" 26 #include "lldb/Core/Log.h" 27 #include "lldb/Core/Module.h" 28 #include "lldb/Core/ModuleSpec.h" 29 #include "lldb/Core/Section.h" 30 #include "lldb/Core/SourceManager.h" 31 #include "lldb/Core/State.h" 32 #include "lldb/Core/StreamFile.h" 33 #include "lldb/Core/StreamString.h" 34 #include "lldb/Core/Timer.h" 35 #include "lldb/Core/ValueObject.h" 36 #include "lldb/Expression/ClangASTSource.h" 37 #include "lldb/Expression/ClangUserExpression.h" 38 #include "lldb/Host/FileSpec.h" 39 #include "lldb/Host/Host.h" 40 #include "lldb/Interpreter/CommandInterpreter.h" 41 #include "lldb/Interpreter/CommandReturnObject.h" 42 #include "lldb/Interpreter/OptionGroupWatchpoint.h" 43 #include "lldb/Interpreter/OptionValues.h" 44 #include "lldb/Interpreter/Property.h" 45 #include "lldb/lldb-private-log.h" 46 #include "lldb/Symbol/ObjectFile.h" 47 #include "lldb/Target/Process.h" 48 #include "lldb/Target/SectionLoadList.h" 49 #include "lldb/Target/StackFrame.h" 50 #include "lldb/Target/SystemRuntime.h" 51 #include "lldb/Target/Thread.h" 52 #include "lldb/Target/ThreadSpec.h" 53 54 using namespace lldb; 55 using namespace lldb_private; 56 57 ConstString & 58 Target::GetStaticBroadcasterClass () 59 { 60 static ConstString class_name ("lldb.target"); 61 return class_name; 62 } 63 64 //---------------------------------------------------------------------- 65 // Target constructor 66 //---------------------------------------------------------------------- 67 Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) : 68 TargetProperties (this), 69 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()), 70 ExecutionContextScope (), 71 m_debugger (debugger), 72 m_platform_sp (platform_sp), 73 m_mutex (Mutex::eMutexTypeRecursive), 74 m_arch (target_arch), 75 m_images (this), 76 m_section_load_history (), 77 m_breakpoint_list (false), 78 m_internal_breakpoint_list (true), 79 m_watchpoint_list (), 80 m_process_sp (), 81 m_search_filter_sp (), 82 m_image_search_paths (ImageSearchPathsChanged, this), 83 m_scratch_ast_context_ap (), 84 m_scratch_ast_source_ap (), 85 m_ast_importer_ap (), 86 m_persistent_variables (), 87 m_source_manager_ap(), 88 m_stop_hooks (), 89 m_stop_hook_next_id (0), 90 m_valid (true), 91 m_suppress_stop_hooks (false), 92 m_is_dummy_target(is_dummy_target) 93 94 { 95 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed"); 96 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded"); 97 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded"); 98 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed"); 99 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded"); 100 101 CheckInWithManager(); 102 103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 104 if (log) 105 log->Printf ("%p Target::Target()", static_cast<void*>(this)); 106 if (m_arch.IsValid()) 107 { 108 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str()); 109 } 110 } 111 112 void 113 Target::PrimeFromDummyTarget(Target *target) 114 { 115 if (!target) 116 return; 117 118 m_stop_hooks = target->m_stop_hooks; 119 120 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints()) 121 { 122 if (breakpoint_sp->IsInternal()) 123 continue; 124 125 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get())); 126 AddBreakpoint (new_bp, false); 127 } 128 } 129 130 //---------------------------------------------------------------------- 131 // Destructor 132 //---------------------------------------------------------------------- 133 Target::~Target() 134 { 135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 136 if (log) 137 log->Printf ("%p Target::~Target()", static_cast<void*>(this)); 138 DeleteCurrentProcess (); 139 } 140 141 void 142 Target::Dump (Stream *s, lldb::DescriptionLevel description_level) 143 { 144 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 145 if (description_level != lldb::eDescriptionLevelBrief) 146 { 147 s->Indent(); 148 s->PutCString("Target\n"); 149 s->IndentMore(); 150 m_images.Dump(s); 151 m_breakpoint_list.Dump(s); 152 m_internal_breakpoint_list.Dump(s); 153 s->IndentLess(); 154 } 155 else 156 { 157 Module *exe_module = GetExecutableModulePointer(); 158 if (exe_module) 159 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString()); 160 else 161 s->PutCString ("No executable module."); 162 } 163 } 164 165 void 166 Target::CleanupProcess () 167 { 168 // Do any cleanup of the target we need to do between process instances. 169 // NB It is better to do this before destroying the process in case the 170 // clean up needs some help from the process. 171 m_breakpoint_list.ClearAllBreakpointSites(); 172 m_internal_breakpoint_list.ClearAllBreakpointSites(); 173 // Disable watchpoints just on the debugger side. 174 Mutex::Locker locker; 175 this->GetWatchpointList().GetListMutex(locker); 176 DisableAllWatchpoints(false); 177 ClearAllWatchpointHitCounts(); 178 } 179 180 void 181 Target::DeleteCurrentProcess () 182 { 183 if (m_process_sp.get()) 184 { 185 m_section_load_history.Clear(); 186 if (m_process_sp->IsAlive()) 187 m_process_sp->Destroy(); 188 189 m_process_sp->Finalize(); 190 191 CleanupProcess (); 192 193 m_process_sp.reset(); 194 } 195 } 196 197 const lldb::ProcessSP & 198 Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file) 199 { 200 DeleteCurrentProcess (); 201 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file); 202 return m_process_sp; 203 } 204 205 const lldb::ProcessSP & 206 Target::GetProcessSP () const 207 { 208 return m_process_sp; 209 } 210 211 void 212 Target::Destroy() 213 { 214 Mutex::Locker locker (m_mutex); 215 m_valid = false; 216 DeleteCurrentProcess (); 217 m_platform_sp.reset(); 218 m_arch.Clear(); 219 ClearModules(true); 220 m_section_load_history.Clear(); 221 const bool notify = false; 222 m_breakpoint_list.RemoveAll(notify); 223 m_internal_breakpoint_list.RemoveAll(notify); 224 m_last_created_breakpoint.reset(); 225 m_last_created_watchpoint.reset(); 226 m_search_filter_sp.reset(); 227 m_image_search_paths.Clear(notify); 228 m_persistent_variables.Clear(); 229 m_stop_hooks.clear(); 230 m_stop_hook_next_id = 0; 231 m_suppress_stop_hooks = false; 232 } 233 234 235 BreakpointList & 236 Target::GetBreakpointList(bool internal) 237 { 238 if (internal) 239 return m_internal_breakpoint_list; 240 else 241 return m_breakpoint_list; 242 } 243 244 const BreakpointList & 245 Target::GetBreakpointList(bool internal) const 246 { 247 if (internal) 248 return m_internal_breakpoint_list; 249 else 250 return m_breakpoint_list; 251 } 252 253 BreakpointSP 254 Target::GetBreakpointByID (break_id_t break_id) 255 { 256 BreakpointSP bp_sp; 257 258 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 259 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 260 else 261 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 262 263 return bp_sp; 264 } 265 266 BreakpointSP 267 Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules, 268 const FileSpecList *source_file_spec_list, 269 RegularExpression &source_regex, 270 bool internal, 271 bool hardware) 272 { 273 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list)); 274 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex)); 275 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true); 276 } 277 278 279 BreakpointSP 280 Target::CreateBreakpoint (const FileSpecList *containingModules, 281 const FileSpec &file, 282 uint32_t line_no, 283 LazyBool check_inlines, 284 LazyBool skip_prologue, 285 bool internal, 286 bool hardware) 287 { 288 if (check_inlines == eLazyBoolCalculate) 289 { 290 const InlineStrategy inline_strategy = GetInlineStrategy(); 291 switch (inline_strategy) 292 { 293 case eInlineBreakpointsNever: 294 check_inlines = eLazyBoolNo; 295 break; 296 297 case eInlineBreakpointsHeaders: 298 if (file.IsSourceImplementationFile()) 299 check_inlines = eLazyBoolNo; 300 else 301 check_inlines = eLazyBoolYes; 302 break; 303 304 case eInlineBreakpointsAlways: 305 check_inlines = eLazyBoolYes; 306 break; 307 } 308 } 309 SearchFilterSP filter_sp; 310 if (check_inlines == eLazyBoolNo) 311 { 312 // Not checking for inlines, we are looking only for matching compile units 313 FileSpecList compile_unit_list; 314 compile_unit_list.Append (file); 315 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list); 316 } 317 else 318 { 319 filter_sp = GetSearchFilterForModuleList (containingModules); 320 } 321 if (skip_prologue == eLazyBoolCalculate) 322 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; 323 324 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, 325 file, 326 line_no, 327 check_inlines, 328 skip_prologue)); 329 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true); 330 } 331 332 333 BreakpointSP 334 Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware) 335 { 336 Address so_addr; 337 // Attempt to resolve our load address if possible, though it is ok if 338 // it doesn't resolve to section/offset. 339 340 // Try and resolve as a load address if possible 341 GetSectionLoadList().ResolveLoadAddress(addr, so_addr); 342 if (!so_addr.IsValid()) 343 { 344 // The address didn't resolve, so just set this as an absolute address 345 so_addr.SetOffset (addr); 346 } 347 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware)); 348 return bp_sp; 349 } 350 351 BreakpointSP 352 Target::CreateBreakpoint (Address &addr, bool internal, bool hardware) 353 { 354 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this())); 355 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); 356 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false); 357 } 358 359 BreakpointSP 360 Target::CreateBreakpoint (const FileSpecList *containingModules, 361 const FileSpecList *containingSourceFiles, 362 const char *func_name, 363 uint32_t func_name_type_mask, 364 LazyBool skip_prologue, 365 bool internal, 366 bool hardware) 367 { 368 BreakpointSP bp_sp; 369 if (func_name) 370 { 371 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles)); 372 373 if (skip_prologue == eLazyBoolCalculate) 374 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; 375 376 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, 377 func_name, 378 func_name_type_mask, 379 Breakpoint::Exact, 380 skip_prologue)); 381 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true); 382 } 383 return bp_sp; 384 } 385 386 lldb::BreakpointSP 387 Target::CreateBreakpoint (const FileSpecList *containingModules, 388 const FileSpecList *containingSourceFiles, 389 const std::vector<std::string> &func_names, 390 uint32_t func_name_type_mask, 391 LazyBool skip_prologue, 392 bool internal, 393 bool hardware) 394 { 395 BreakpointSP bp_sp; 396 size_t num_names = func_names.size(); 397 if (num_names > 0) 398 { 399 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles)); 400 401 if (skip_prologue == eLazyBoolCalculate) 402 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; 403 404 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, 405 func_names, 406 func_name_type_mask, 407 skip_prologue)); 408 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true); 409 } 410 return bp_sp; 411 } 412 413 BreakpointSP 414 Target::CreateBreakpoint (const FileSpecList *containingModules, 415 const FileSpecList *containingSourceFiles, 416 const char *func_names[], 417 size_t num_names, 418 uint32_t func_name_type_mask, 419 LazyBool skip_prologue, 420 bool internal, 421 bool hardware) 422 { 423 BreakpointSP bp_sp; 424 if (num_names > 0) 425 { 426 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles)); 427 428 if (skip_prologue == eLazyBoolCalculate) 429 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; 430 431 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, 432 func_names, 433 num_names, 434 func_name_type_mask, 435 skip_prologue)); 436 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true); 437 } 438 return bp_sp; 439 } 440 441 SearchFilterSP 442 Target::GetSearchFilterForModule (const FileSpec *containingModule) 443 { 444 SearchFilterSP filter_sp; 445 if (containingModule != NULL) 446 { 447 // TODO: We should look into sharing module based search filters 448 // across many breakpoints like we do for the simple target based one 449 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule)); 450 } 451 else 452 { 453 if (m_search_filter_sp.get() == NULL) 454 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this())); 455 filter_sp = m_search_filter_sp; 456 } 457 return filter_sp; 458 } 459 460 SearchFilterSP 461 Target::GetSearchFilterForModuleList (const FileSpecList *containingModules) 462 { 463 SearchFilterSP filter_sp; 464 if (containingModules && containingModules->GetSize() != 0) 465 { 466 // TODO: We should look into sharing module based search filters 467 // across many breakpoints like we do for the simple target based one 468 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules)); 469 } 470 else 471 { 472 if (m_search_filter_sp.get() == NULL) 473 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this())); 474 filter_sp = m_search_filter_sp; 475 } 476 return filter_sp; 477 } 478 479 SearchFilterSP 480 Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, 481 const FileSpecList *containingSourceFiles) 482 { 483 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0) 484 return GetSearchFilterForModuleList(containingModules); 485 486 SearchFilterSP filter_sp; 487 if (containingModules == NULL) 488 { 489 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable, 490 // but that will take a little reworking. 491 492 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles)); 493 } 494 else 495 { 496 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles)); 497 } 498 return filter_sp; 499 } 500 501 BreakpointSP 502 Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules, 503 const FileSpecList *containingSourceFiles, 504 RegularExpression &func_regex, 505 LazyBool skip_prologue, 506 bool internal, 507 bool hardware) 508 { 509 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles)); 510 bool skip = 511 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue() 512 : static_cast<bool>(skip_prologue); 513 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, 514 func_regex, 515 skip)); 516 517 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true); 518 } 519 520 lldb::BreakpointSP 521 Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal) 522 { 523 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal); 524 } 525 526 BreakpointSP 527 Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols) 528 { 529 BreakpointSP bp_sp; 530 if (filter_sp && resolver_sp) 531 { 532 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols)); 533 resolver_sp->SetBreakpoint (bp_sp.get()); 534 AddBreakpoint (bp_sp, internal); 535 } 536 return bp_sp; 537 } 538 539 void 540 Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal) 541 { 542 if (!bp_sp) 543 return; 544 if (internal) 545 m_internal_breakpoint_list.Add (bp_sp, false); 546 else 547 m_breakpoint_list.Add (bp_sp, true); 548 549 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 550 if (log) 551 { 552 StreamString s; 553 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 554 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData()); 555 } 556 557 bp_sp->ResolveBreakpoint(); 558 559 if (!internal) 560 { 561 m_last_created_breakpoint = bp_sp; 562 } 563 } 564 565 bool 566 Target::ProcessIsValid() 567 { 568 return (m_process_sp && m_process_sp->IsAlive()); 569 } 570 571 static bool 572 CheckIfWatchpointsExhausted(Target *target, Error &error) 573 { 574 uint32_t num_supported_hardware_watchpoints; 575 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints); 576 if (rc.Success()) 577 { 578 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize(); 579 if (num_current_watchpoints >= num_supported_hardware_watchpoints) 580 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached", 581 num_supported_hardware_watchpoints); 582 } 583 return false; 584 } 585 586 // See also Watchpoint::SetWatchpointType(uint32_t type) and 587 // the OptionGroupWatchpoint::WatchType enum type. 588 WatchpointSP 589 Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error) 590 { 591 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 592 if (log) 593 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n", 594 __FUNCTION__, addr, (uint64_t)size, kind); 595 596 WatchpointSP wp_sp; 597 if (!ProcessIsValid()) 598 { 599 error.SetErrorString("process is not alive"); 600 return wp_sp; 601 } 602 603 if (addr == LLDB_INVALID_ADDRESS || size == 0) 604 { 605 if (size == 0) 606 error.SetErrorString("cannot set a watchpoint with watch_size of 0"); 607 else 608 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr); 609 return wp_sp; 610 } 611 612 if (!LLDB_WATCH_TYPE_IS_VALID(kind)) 613 { 614 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind); 615 } 616 617 // Currently we only support one watchpoint per address, with total number 618 // of watchpoints limited by the hardware which the inferior is running on. 619 620 // Grab the list mutex while doing operations. 621 const bool notify = false; // Don't notify about all the state changes we do on creating the watchpoint. 622 Mutex::Locker locker; 623 this->GetWatchpointList().GetListMutex(locker); 624 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr); 625 if (matched_sp) 626 { 627 size_t old_size = matched_sp->GetByteSize(); 628 uint32_t old_type = 629 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) | 630 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0); 631 // Return the existing watchpoint if both size and type match. 632 if (size == old_size && kind == old_type) 633 { 634 wp_sp = matched_sp; 635 wp_sp->SetEnabled(false, notify); 636 } 637 else 638 { 639 // Nil the matched watchpoint; we will be creating a new one. 640 m_process_sp->DisableWatchpoint(matched_sp.get(), notify); 641 m_watchpoint_list.Remove(matched_sp->GetID(), true); 642 } 643 } 644 645 if (!wp_sp) 646 { 647 wp_sp.reset(new Watchpoint(*this, addr, size, type)); 648 wp_sp->SetWatchpointType(kind, notify); 649 m_watchpoint_list.Add (wp_sp, true); 650 } 651 652 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify); 653 if (log) 654 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n", 655 __FUNCTION__, 656 error.Success() ? "succeeded" : "failed", 657 wp_sp->GetID()); 658 659 if (error.Fail()) 660 { 661 // Enabling the watchpoint on the device side failed. 662 // Remove the said watchpoint from the list maintained by the target instance. 663 m_watchpoint_list.Remove (wp_sp->GetID(), true); 664 // See if we could provide more helpful error message. 665 if (!CheckIfWatchpointsExhausted(this, error)) 666 { 667 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size)) 668 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size); 669 } 670 wp_sp.reset(); 671 } 672 else 673 m_last_created_watchpoint = wp_sp; 674 return wp_sp; 675 } 676 677 void 678 Target::RemoveAllBreakpoints (bool internal_also) 679 { 680 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 681 if (log) 682 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 683 684 m_breakpoint_list.RemoveAll (true); 685 if (internal_also) 686 m_internal_breakpoint_list.RemoveAll (false); 687 688 m_last_created_breakpoint.reset(); 689 } 690 691 void 692 Target::DisableAllBreakpoints (bool internal_also) 693 { 694 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 695 if (log) 696 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 697 698 m_breakpoint_list.SetEnabledAll (false); 699 if (internal_also) 700 m_internal_breakpoint_list.SetEnabledAll (false); 701 } 702 703 void 704 Target::EnableAllBreakpoints (bool internal_also) 705 { 706 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 707 if (log) 708 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 709 710 m_breakpoint_list.SetEnabledAll (true); 711 if (internal_also) 712 m_internal_breakpoint_list.SetEnabledAll (true); 713 } 714 715 bool 716 Target::RemoveBreakpointByID (break_id_t break_id) 717 { 718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 719 if (log) 720 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 721 722 if (DisableBreakpointByID (break_id)) 723 { 724 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 725 m_internal_breakpoint_list.Remove(break_id, false); 726 else 727 { 728 if (m_last_created_breakpoint) 729 { 730 if (m_last_created_breakpoint->GetID() == break_id) 731 m_last_created_breakpoint.reset(); 732 } 733 m_breakpoint_list.Remove(break_id, true); 734 } 735 return true; 736 } 737 return false; 738 } 739 740 bool 741 Target::DisableBreakpointByID (break_id_t break_id) 742 { 743 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 744 if (log) 745 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 746 747 BreakpointSP bp_sp; 748 749 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 750 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 751 else 752 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 753 if (bp_sp) 754 { 755 bp_sp->SetEnabled (false); 756 return true; 757 } 758 return false; 759 } 760 761 bool 762 Target::EnableBreakpointByID (break_id_t break_id) 763 { 764 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 765 if (log) 766 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", 767 __FUNCTION__, 768 break_id, 769 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 770 771 BreakpointSP bp_sp; 772 773 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 774 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 775 else 776 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 777 778 if (bp_sp) 779 { 780 bp_sp->SetEnabled (true); 781 return true; 782 } 783 return false; 784 } 785 786 // The flag 'end_to_end', default to true, signifies that the operation is 787 // performed end to end, for both the debugger and the debuggee. 788 789 // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end 790 // to end operations. 791 bool 792 Target::RemoveAllWatchpoints (bool end_to_end) 793 { 794 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 795 if (log) 796 log->Printf ("Target::%s\n", __FUNCTION__); 797 798 if (!end_to_end) { 799 m_watchpoint_list.RemoveAll(true); 800 return true; 801 } 802 803 // Otherwise, it's an end to end operation. 804 805 if (!ProcessIsValid()) 806 return false; 807 808 size_t num_watchpoints = m_watchpoint_list.GetSize(); 809 for (size_t i = 0; i < num_watchpoints; ++i) 810 { 811 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i); 812 if (!wp_sp) 813 return false; 814 815 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get()); 816 if (rc.Fail()) 817 return false; 818 } 819 m_watchpoint_list.RemoveAll (true); 820 m_last_created_watchpoint.reset(); 821 return true; // Success! 822 } 823 824 // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to 825 // end operations. 826 bool 827 Target::DisableAllWatchpoints (bool end_to_end) 828 { 829 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 830 if (log) 831 log->Printf ("Target::%s\n", __FUNCTION__); 832 833 if (!end_to_end) { 834 m_watchpoint_list.SetEnabledAll(false); 835 return true; 836 } 837 838 // Otherwise, it's an end to end operation. 839 840 if (!ProcessIsValid()) 841 return false; 842 843 size_t num_watchpoints = m_watchpoint_list.GetSize(); 844 for (size_t i = 0; i < num_watchpoints; ++i) 845 { 846 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i); 847 if (!wp_sp) 848 return false; 849 850 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get()); 851 if (rc.Fail()) 852 return false; 853 } 854 return true; // Success! 855 } 856 857 // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to 858 // end operations. 859 bool 860 Target::EnableAllWatchpoints (bool end_to_end) 861 { 862 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 863 if (log) 864 log->Printf ("Target::%s\n", __FUNCTION__); 865 866 if (!end_to_end) { 867 m_watchpoint_list.SetEnabledAll(true); 868 return true; 869 } 870 871 // Otherwise, it's an end to end operation. 872 873 if (!ProcessIsValid()) 874 return false; 875 876 size_t num_watchpoints = m_watchpoint_list.GetSize(); 877 for (size_t i = 0; i < num_watchpoints; ++i) 878 { 879 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i); 880 if (!wp_sp) 881 return false; 882 883 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get()); 884 if (rc.Fail()) 885 return false; 886 } 887 return true; // Success! 888 } 889 890 // Assumption: Caller holds the list mutex lock for m_watchpoint_list. 891 bool 892 Target::ClearAllWatchpointHitCounts () 893 { 894 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 895 if (log) 896 log->Printf ("Target::%s\n", __FUNCTION__); 897 898 size_t num_watchpoints = m_watchpoint_list.GetSize(); 899 for (size_t i = 0; i < num_watchpoints; ++i) 900 { 901 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i); 902 if (!wp_sp) 903 return false; 904 905 wp_sp->ResetHitCount(); 906 } 907 return true; // Success! 908 } 909 910 // Assumption: Caller holds the list mutex lock for m_watchpoint_list 911 // during these operations. 912 bool 913 Target::IgnoreAllWatchpoints (uint32_t ignore_count) 914 { 915 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 916 if (log) 917 log->Printf ("Target::%s\n", __FUNCTION__); 918 919 if (!ProcessIsValid()) 920 return false; 921 922 size_t num_watchpoints = m_watchpoint_list.GetSize(); 923 for (size_t i = 0; i < num_watchpoints; ++i) 924 { 925 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i); 926 if (!wp_sp) 927 return false; 928 929 wp_sp->SetIgnoreCount(ignore_count); 930 } 931 return true; // Success! 932 } 933 934 // Assumption: Caller holds the list mutex lock for m_watchpoint_list. 935 bool 936 Target::DisableWatchpointByID (lldb::watch_id_t watch_id) 937 { 938 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 939 if (log) 940 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id); 941 942 if (!ProcessIsValid()) 943 return false; 944 945 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id); 946 if (wp_sp) 947 { 948 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get()); 949 if (rc.Success()) 950 return true; 951 952 // Else, fallthrough. 953 } 954 return false; 955 } 956 957 // Assumption: Caller holds the list mutex lock for m_watchpoint_list. 958 bool 959 Target::EnableWatchpointByID (lldb::watch_id_t watch_id) 960 { 961 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 962 if (log) 963 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id); 964 965 if (!ProcessIsValid()) 966 return false; 967 968 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id); 969 if (wp_sp) 970 { 971 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get()); 972 if (rc.Success()) 973 return true; 974 975 // Else, fallthrough. 976 } 977 return false; 978 } 979 980 // Assumption: Caller holds the list mutex lock for m_watchpoint_list. 981 bool 982 Target::RemoveWatchpointByID (lldb::watch_id_t watch_id) 983 { 984 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 985 if (log) 986 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id); 987 988 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id); 989 if (watch_to_remove_sp == m_last_created_watchpoint) 990 m_last_created_watchpoint.reset(); 991 992 if (DisableWatchpointByID (watch_id)) 993 { 994 m_watchpoint_list.Remove(watch_id, true); 995 return true; 996 } 997 return false; 998 } 999 1000 // Assumption: Caller holds the list mutex lock for m_watchpoint_list. 1001 bool 1002 Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count) 1003 { 1004 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); 1005 if (log) 1006 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id); 1007 1008 if (!ProcessIsValid()) 1009 return false; 1010 1011 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id); 1012 if (wp_sp) 1013 { 1014 wp_sp->SetIgnoreCount(ignore_count); 1015 return true; 1016 } 1017 return false; 1018 } 1019 1020 ModuleSP 1021 Target::GetExecutableModule () 1022 { 1023 return m_images.GetModuleAtIndex(0); 1024 } 1025 1026 Module* 1027 Target::GetExecutableModulePointer () 1028 { 1029 return m_images.GetModulePointerAtIndex(0); 1030 } 1031 1032 static void 1033 LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target) 1034 { 1035 Error error; 1036 StreamString feedback_stream; 1037 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream)) 1038 { 1039 if (error.AsCString()) 1040 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n", 1041 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1042 error.AsCString()); 1043 } 1044 if (feedback_stream.GetSize()) 1045 target->GetDebugger().GetErrorFile()->Printf("%s\n", 1046 feedback_stream.GetData()); 1047 } 1048 1049 void 1050 Target::ClearModules(bool delete_locations) 1051 { 1052 ModulesDidUnload (m_images, delete_locations); 1053 m_section_load_history.Clear(); 1054 m_images.Clear(); 1055 m_scratch_ast_context_ap.reset(); 1056 m_scratch_ast_source_ap.reset(); 1057 m_ast_importer_ap.reset(); 1058 } 1059 1060 void 1061 Target::DidExec () 1062 { 1063 // When a process exec's we need to know about it so we can do some cleanup. 1064 m_breakpoint_list.RemoveInvalidLocations(m_arch); 1065 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch); 1066 } 1067 1068 void 1069 Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) 1070 { 1071 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET)); 1072 ClearModules(false); 1073 1074 if (executable_sp.get()) 1075 { 1076 Timer scoped_timer (__PRETTY_FUNCTION__, 1077 "Target::SetExecutableModule (executable = '%s')", 1078 executable_sp->GetFileSpec().GetPath().c_str()); 1079 1080 m_images.Append(executable_sp); // The first image is our executable file 1081 1082 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. 1083 if (!m_arch.IsValid()) 1084 { 1085 m_arch = executable_sp->GetArchitecture(); 1086 if (log) 1087 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str()); 1088 } 1089 1090 FileSpecList dependent_files; 1091 ObjectFile *executable_objfile = executable_sp->GetObjectFile(); 1092 1093 if (executable_objfile && get_dependent_files) 1094 { 1095 executable_objfile->GetDependentModules(dependent_files); 1096 for (uint32_t i=0; i<dependent_files.GetSize(); i++) 1097 { 1098 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i)); 1099 FileSpec platform_dependent_file_spec; 1100 if (m_platform_sp) 1101 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec); 1102 else 1103 platform_dependent_file_spec = dependent_file_spec; 1104 1105 ModuleSpec module_spec (platform_dependent_file_spec, m_arch); 1106 ModuleSP image_module_sp(GetSharedModule (module_spec)); 1107 if (image_module_sp.get()) 1108 { 1109 ObjectFile *objfile = image_module_sp->GetObjectFile(); 1110 if (objfile) 1111 objfile->GetDependentModules(dependent_files); 1112 } 1113 } 1114 } 1115 } 1116 } 1117 1118 1119 bool 1120 Target::SetArchitecture (const ArchSpec &arch_spec) 1121 { 1122 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET)); 1123 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid()) 1124 { 1125 // If we haven't got a valid arch spec, or the architectures are 1126 // compatible, so just update the architecture. Architectures can be 1127 // equal, yet the triple OS and vendor might change, so we need to do 1128 // the assignment here just in case. 1129 m_arch = arch_spec; 1130 if (log) 1131 log->Printf ("Target::SetArchitecture setting architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str()); 1132 return true; 1133 } 1134 else 1135 { 1136 // If we have an executable file, try to reset the executable to the desired architecture 1137 if (log) 1138 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str()); 1139 m_arch = arch_spec; 1140 ModuleSP executable_sp = GetExecutableModule (); 1141 1142 ClearModules(true); 1143 // Need to do something about unsetting breakpoints. 1144 1145 if (executable_sp) 1146 { 1147 if (log) 1148 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str()); 1149 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec); 1150 Error error = ModuleList::GetSharedModule (module_spec, 1151 executable_sp, 1152 &GetExecutableSearchPaths(), 1153 NULL, 1154 NULL); 1155 1156 if (!error.Fail() && executable_sp) 1157 { 1158 SetExecutableModule (executable_sp, true); 1159 return true; 1160 } 1161 } 1162 } 1163 return false; 1164 } 1165 1166 void 1167 Target::WillClearList (const ModuleList& module_list) 1168 { 1169 } 1170 1171 void 1172 Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp) 1173 { 1174 // A module is being added to this target for the first time 1175 if (m_valid) 1176 { 1177 ModuleList my_module_list; 1178 my_module_list.Append(module_sp); 1179 LoadScriptingResourceForModule(module_sp, this); 1180 ModulesDidLoad (my_module_list); 1181 } 1182 } 1183 1184 void 1185 Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp) 1186 { 1187 // A module is being added to this target for the first time 1188 if (m_valid) 1189 { 1190 ModuleList my_module_list; 1191 my_module_list.Append(module_sp); 1192 ModulesDidUnload (my_module_list, false); 1193 } 1194 } 1195 1196 void 1197 Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp) 1198 { 1199 // A module is replacing an already added module 1200 if (m_valid) 1201 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp); 1202 } 1203 1204 void 1205 Target::ModulesDidLoad (ModuleList &module_list) 1206 { 1207 if (m_valid && module_list.GetSize()) 1208 { 1209 m_breakpoint_list.UpdateBreakpoints (module_list, true, false); 1210 if (m_process_sp) 1211 { 1212 m_process_sp->ModulesDidLoad (module_list); 1213 } 1214 // TODO: make event data that packages up the module_list 1215 BroadcastEvent (eBroadcastBitModulesLoaded, NULL); 1216 } 1217 } 1218 1219 void 1220 Target::SymbolsDidLoad (ModuleList &module_list) 1221 { 1222 if (m_valid && module_list.GetSize()) 1223 { 1224 if (m_process_sp) 1225 { 1226 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC); 1227 if (runtime) 1228 { 1229 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime; 1230 objc_runtime->SymbolsDidLoad(module_list); 1231 } 1232 } 1233 1234 m_breakpoint_list.UpdateBreakpoints (module_list, true, false); 1235 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL); 1236 } 1237 } 1238 1239 void 1240 Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations) 1241 { 1242 if (m_valid && module_list.GetSize()) 1243 { 1244 UnloadModuleSections (module_list); 1245 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations); 1246 // TODO: make event data that packages up the module_list 1247 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); 1248 } 1249 } 1250 1251 bool 1252 Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec) 1253 { 1254 if (GetBreakpointsConsultPlatformAvoidList()) 1255 { 1256 ModuleList matchingModules; 1257 ModuleSpec module_spec (module_file_spec); 1258 size_t num_modules = GetImages().FindModules(module_spec, matchingModules); 1259 1260 // If there is more than one module for this file spec, only return true if ALL the modules are on the 1261 // black list. 1262 if (num_modules > 0) 1263 { 1264 for (size_t i = 0; i < num_modules; i++) 1265 { 1266 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i))) 1267 return false; 1268 } 1269 return true; 1270 } 1271 } 1272 return false; 1273 } 1274 1275 bool 1276 Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp) 1277 { 1278 if (GetBreakpointsConsultPlatformAvoidList()) 1279 { 1280 if (m_platform_sp) 1281 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp); 1282 } 1283 return false; 1284 } 1285 1286 size_t 1287 Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error) 1288 { 1289 SectionSP section_sp (addr.GetSection()); 1290 if (section_sp) 1291 { 1292 // If the contents of this section are encrypted, the on-disk file is unusable. Read only from live memory. 1293 if (section_sp->IsEncrypted()) 1294 { 1295 error.SetErrorString("section is encrypted"); 1296 return 0; 1297 } 1298 ModuleSP module_sp (section_sp->GetModule()); 1299 if (module_sp) 1300 { 1301 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile(); 1302 if (objfile) 1303 { 1304 size_t bytes_read = objfile->ReadSectionData (section_sp.get(), 1305 addr.GetOffset(), 1306 dst, 1307 dst_len); 1308 if (bytes_read > 0) 1309 return bytes_read; 1310 else 1311 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString()); 1312 } 1313 else 1314 error.SetErrorString("address isn't from a object file"); 1315 } 1316 else 1317 error.SetErrorString("address isn't in a module"); 1318 } 1319 else 1320 error.SetErrorString("address doesn't contain a section that points to a section in a object file"); 1321 1322 return 0; 1323 } 1324 1325 size_t 1326 Target::ReadMemory (const Address& addr, 1327 bool prefer_file_cache, 1328 void *dst, 1329 size_t dst_len, 1330 Error &error, 1331 lldb::addr_t *load_addr_ptr) 1332 { 1333 error.Clear(); 1334 1335 // if we end up reading this from process memory, we will fill this 1336 // with the actual load address 1337 if (load_addr_ptr) 1338 *load_addr_ptr = LLDB_INVALID_ADDRESS; 1339 1340 size_t bytes_read = 0; 1341 1342 addr_t load_addr = LLDB_INVALID_ADDRESS; 1343 addr_t file_addr = LLDB_INVALID_ADDRESS; 1344 Address resolved_addr; 1345 if (!addr.IsSectionOffset()) 1346 { 1347 SectionLoadList §ion_load_list = GetSectionLoadList(); 1348 if (section_load_list.IsEmpty()) 1349 { 1350 // No sections are loaded, so we must assume we are not running 1351 // yet and anything we are given is a file address. 1352 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address 1353 m_images.ResolveFileAddress (file_addr, resolved_addr); 1354 } 1355 else 1356 { 1357 // We have at least one section loaded. This can be because 1358 // we have manually loaded some sections with "target modules load ..." 1359 // or because we have have a live process that has sections loaded 1360 // through the dynamic loader 1361 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address 1362 section_load_list.ResolveLoadAddress (load_addr, resolved_addr); 1363 } 1364 } 1365 if (!resolved_addr.IsValid()) 1366 resolved_addr = addr; 1367 1368 1369 if (prefer_file_cache) 1370 { 1371 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 1372 if (bytes_read > 0) 1373 return bytes_read; 1374 } 1375 1376 if (ProcessIsValid()) 1377 { 1378 if (load_addr == LLDB_INVALID_ADDRESS) 1379 load_addr = resolved_addr.GetLoadAddress (this); 1380 1381 if (load_addr == LLDB_INVALID_ADDRESS) 1382 { 1383 ModuleSP addr_module_sp (resolved_addr.GetModule()); 1384 if (addr_module_sp && addr_module_sp->GetFileSpec()) 1385 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded", 1386 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"), 1387 resolved_addr.GetFileAddress(), 1388 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>")); 1389 else 1390 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress()); 1391 } 1392 else 1393 { 1394 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); 1395 if (bytes_read != dst_len) 1396 { 1397 if (error.Success()) 1398 { 1399 if (bytes_read == 0) 1400 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr); 1401 else 1402 error.SetErrorStringWithFormat("only %" PRIu64 " of %" PRIu64 " bytes were read from memory at 0x%" PRIx64, (uint64_t)bytes_read, (uint64_t)dst_len, load_addr); 1403 } 1404 } 1405 if (bytes_read) 1406 { 1407 if (load_addr_ptr) 1408 *load_addr_ptr = load_addr; 1409 return bytes_read; 1410 } 1411 // If the address is not section offset we have an address that 1412 // doesn't resolve to any address in any currently loaded shared 1413 // libraries and we failed to read memory so there isn't anything 1414 // more we can do. If it is section offset, we might be able to 1415 // read cached memory from the object file. 1416 if (!resolved_addr.IsSectionOffset()) 1417 return 0; 1418 } 1419 } 1420 1421 if (!prefer_file_cache && resolved_addr.IsSectionOffset()) 1422 { 1423 // If we didn't already try and read from the object file cache, then 1424 // try it after failing to read from the process. 1425 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 1426 } 1427 return 0; 1428 } 1429 1430 size_t 1431 Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error) 1432 { 1433 char buf[256]; 1434 out_str.clear(); 1435 addr_t curr_addr = addr.GetLoadAddress(this); 1436 Address address(addr); 1437 while (1) 1438 { 1439 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error); 1440 if (length == 0) 1441 break; 1442 out_str.append(buf, length); 1443 // If we got "length - 1" bytes, we didn't get the whole C string, we 1444 // need to read some more characters 1445 if (length == sizeof(buf) - 1) 1446 curr_addr += length; 1447 else 1448 break; 1449 address = Address(curr_addr); 1450 } 1451 return out_str.size(); 1452 } 1453 1454 1455 size_t 1456 Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error) 1457 { 1458 size_t total_cstr_len = 0; 1459 if (dst && dst_max_len) 1460 { 1461 result_error.Clear(); 1462 // NULL out everything just to be safe 1463 memset (dst, 0, dst_max_len); 1464 Error error; 1465 addr_t curr_addr = addr.GetLoadAddress(this); 1466 Address address(addr); 1467 1468 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't 1469 // think this really needs to be tied to the memory cache subsystem's 1470 // cache line size, so leave this as a fixed constant. 1471 const size_t cache_line_size = 512; 1472 1473 size_t bytes_left = dst_max_len - 1; 1474 char *curr_dst = dst; 1475 1476 while (bytes_left > 0) 1477 { 1478 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); 1479 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); 1480 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error); 1481 1482 if (bytes_read == 0) 1483 { 1484 result_error = error; 1485 dst[total_cstr_len] = '\0'; 1486 break; 1487 } 1488 const size_t len = strlen(curr_dst); 1489 1490 total_cstr_len += len; 1491 1492 if (len < bytes_to_read) 1493 break; 1494 1495 curr_dst += bytes_read; 1496 curr_addr += bytes_read; 1497 bytes_left -= bytes_read; 1498 address = Address(curr_addr); 1499 } 1500 } 1501 else 1502 { 1503 if (dst == NULL) 1504 result_error.SetErrorString("invalid arguments"); 1505 else 1506 result_error.Clear(); 1507 } 1508 return total_cstr_len; 1509 } 1510 1511 size_t 1512 Target::ReadScalarIntegerFromMemory (const Address& addr, 1513 bool prefer_file_cache, 1514 uint32_t byte_size, 1515 bool is_signed, 1516 Scalar &scalar, 1517 Error &error) 1518 { 1519 uint64_t uval; 1520 1521 if (byte_size <= sizeof(uval)) 1522 { 1523 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error); 1524 if (bytes_read == byte_size) 1525 { 1526 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize()); 1527 lldb::offset_t offset = 0; 1528 if (byte_size <= 4) 1529 scalar = data.GetMaxU32 (&offset, byte_size); 1530 else 1531 scalar = data.GetMaxU64 (&offset, byte_size); 1532 1533 if (is_signed) 1534 scalar.SignExtend(byte_size * 8); 1535 return bytes_read; 1536 } 1537 } 1538 else 1539 { 1540 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); 1541 } 1542 return 0; 1543 } 1544 1545 uint64_t 1546 Target::ReadUnsignedIntegerFromMemory (const Address& addr, 1547 bool prefer_file_cache, 1548 size_t integer_byte_size, 1549 uint64_t fail_value, 1550 Error &error) 1551 { 1552 Scalar scalar; 1553 if (ReadScalarIntegerFromMemory (addr, 1554 prefer_file_cache, 1555 integer_byte_size, 1556 false, 1557 scalar, 1558 error)) 1559 return scalar.ULongLong(fail_value); 1560 return fail_value; 1561 } 1562 1563 bool 1564 Target::ReadPointerFromMemory (const Address& addr, 1565 bool prefer_file_cache, 1566 Error &error, 1567 Address &pointer_addr) 1568 { 1569 Scalar scalar; 1570 if (ReadScalarIntegerFromMemory (addr, 1571 prefer_file_cache, 1572 m_arch.GetAddressByteSize(), 1573 false, 1574 scalar, 1575 error)) 1576 { 1577 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS); 1578 if (pointer_vm_addr != LLDB_INVALID_ADDRESS) 1579 { 1580 SectionLoadList §ion_load_list = GetSectionLoadList(); 1581 if (section_load_list.IsEmpty()) 1582 { 1583 // No sections are loaded, so we must assume we are not running 1584 // yet and anything we are given is a file address. 1585 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr); 1586 } 1587 else 1588 { 1589 // We have at least one section loaded. This can be because 1590 // we have manually loaded some sections with "target modules load ..." 1591 // or because we have have a live process that has sections loaded 1592 // through the dynamic loader 1593 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr); 1594 } 1595 // We weren't able to resolve the pointer value, so just return 1596 // an address with no section 1597 if (!pointer_addr.IsValid()) 1598 pointer_addr.SetOffset (pointer_vm_addr); 1599 return true; 1600 1601 } 1602 } 1603 return false; 1604 } 1605 1606 ModuleSP 1607 Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr) 1608 { 1609 ModuleSP module_sp; 1610 1611 Error error; 1612 1613 // First see if we already have this module in our module list. If we do, then we're done, we don't need 1614 // to consult the shared modules list. But only do this if we are passed a UUID. 1615 1616 if (module_spec.GetUUID().IsValid()) 1617 module_sp = m_images.FindFirstModule(module_spec); 1618 1619 if (!module_sp) 1620 { 1621 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library 1622 bool did_create_module = false; 1623 1624 // If there are image search path entries, try to use them first to acquire a suitable image. 1625 if (m_image_search_paths.GetSize()) 1626 { 1627 ModuleSpec transformed_spec (module_spec); 1628 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory())) 1629 { 1630 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename(); 1631 error = ModuleList::GetSharedModule (transformed_spec, 1632 module_sp, 1633 &GetExecutableSearchPaths(), 1634 &old_module_sp, 1635 &did_create_module); 1636 } 1637 } 1638 1639 if (!module_sp) 1640 { 1641 // If we have a UUID, we can check our global shared module list in case 1642 // we already have it. If we don't have a valid UUID, then we can't since 1643 // the path in "module_spec" will be a platform path, and we will need to 1644 // let the platform find that file. For example, we could be asking for 1645 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick 1646 // the local copy of "/usr/lib/dyld" since our platform could be a remote 1647 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file 1648 // cache. 1649 if (module_spec.GetUUID().IsValid()) 1650 { 1651 // We have a UUID, it is OK to check the global module list... 1652 error = ModuleList::GetSharedModule (module_spec, 1653 module_sp, 1654 &GetExecutableSearchPaths(), 1655 &old_module_sp, 1656 &did_create_module); 1657 } 1658 1659 if (!module_sp) 1660 { 1661 // The platform is responsible for finding and caching an appropriate 1662 // module in the shared module cache. 1663 if (m_platform_sp) 1664 { 1665 FileSpec platform_file_spec; 1666 error = m_platform_sp->GetSharedModule (module_spec, 1667 module_sp, 1668 &GetExecutableSearchPaths(), 1669 &old_module_sp, 1670 &did_create_module); 1671 } 1672 else 1673 { 1674 error.SetErrorString("no platform is currently set"); 1675 } 1676 } 1677 } 1678 1679 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent 1680 // module in the list already, and if there was, let's remove it. 1681 if (module_sp) 1682 { 1683 ObjectFile *objfile = module_sp->GetObjectFile(); 1684 if (objfile) 1685 { 1686 switch (objfile->GetType()) 1687 { 1688 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state 1689 case ObjectFile::eTypeExecutable: /// A normal executable 1690 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable 1691 case ObjectFile::eTypeObjectFile: /// An intermediate object file 1692 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution 1693 break; 1694 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information 1695 if (error_ptr) 1696 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable"); 1697 return ModuleSP(); 1698 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution 1699 if (error_ptr) 1700 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable"); 1701 return ModuleSP(); 1702 default: 1703 if (error_ptr) 1704 error_ptr->SetErrorString("unsupported file type, please specify an executable"); 1705 return ModuleSP(); 1706 } 1707 // GetSharedModule is not guaranteed to find the old shared module, for instance 1708 // in the common case where you pass in the UUID, it is only going to find the one 1709 // module matching the UUID. In fact, it has no good way to know what the "old module" 1710 // relevant to this target is, since there might be many copies of a module with this file spec 1711 // in various running debug sessions, but only one of them will belong to this target. 1712 // So let's remove the UUID from the module list, and look in the target's module list. 1713 // Only do this if there is SOMETHING else in the module spec... 1714 if (!old_module_sp) 1715 { 1716 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty()) 1717 { 1718 ModuleSpec module_spec_copy(module_spec.GetFileSpec()); 1719 module_spec_copy.GetUUID().Clear(); 1720 1721 ModuleList found_modules; 1722 size_t num_found = m_images.FindModules (module_spec_copy, found_modules); 1723 if (num_found == 1) 1724 { 1725 old_module_sp = found_modules.GetModuleAtIndex(0); 1726 } 1727 } 1728 } 1729 1730 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) 1731 { 1732 m_images.ReplaceModule(old_module_sp, module_sp); 1733 Module *old_module_ptr = old_module_sp.get(); 1734 old_module_sp.reset(); 1735 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr); 1736 } 1737 else 1738 m_images.Append(module_sp); 1739 } 1740 else 1741 module_sp.reset(); 1742 } 1743 } 1744 if (error_ptr) 1745 *error_ptr = error; 1746 return module_sp; 1747 } 1748 1749 1750 TargetSP 1751 Target::CalculateTarget () 1752 { 1753 return shared_from_this(); 1754 } 1755 1756 ProcessSP 1757 Target::CalculateProcess () 1758 { 1759 return ProcessSP(); 1760 } 1761 1762 ThreadSP 1763 Target::CalculateThread () 1764 { 1765 return ThreadSP(); 1766 } 1767 1768 StackFrameSP 1769 Target::CalculateStackFrame () 1770 { 1771 return StackFrameSP(); 1772 } 1773 1774 void 1775 Target::CalculateExecutionContext (ExecutionContext &exe_ctx) 1776 { 1777 exe_ctx.Clear(); 1778 exe_ctx.SetTargetPtr(this); 1779 } 1780 1781 PathMappingList & 1782 Target::GetImageSearchPathList () 1783 { 1784 return m_image_search_paths; 1785 } 1786 1787 void 1788 Target::ImageSearchPathsChanged 1789 ( 1790 const PathMappingList &path_list, 1791 void *baton 1792 ) 1793 { 1794 Target *target = (Target *)baton; 1795 ModuleSP exe_module_sp (target->GetExecutableModule()); 1796 if (exe_module_sp) 1797 target->SetExecutableModule (exe_module_sp, true); 1798 } 1799 1800 ClangASTContext * 1801 Target::GetScratchClangASTContext(bool create_on_demand) 1802 { 1803 // Now see if we know the target triple, and if so, create our scratch AST context: 1804 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand) 1805 { 1806 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str())); 1807 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this())); 1808 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext()); 1809 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy()); 1810 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source); 1811 } 1812 return m_scratch_ast_context_ap.get(); 1813 } 1814 1815 ClangASTImporter * 1816 Target::GetClangASTImporter() 1817 { 1818 ClangASTImporter *ast_importer = m_ast_importer_ap.get(); 1819 1820 if (!ast_importer) 1821 { 1822 ast_importer = new ClangASTImporter(); 1823 m_ast_importer_ap.reset(ast_importer); 1824 } 1825 1826 return ast_importer; 1827 } 1828 1829 void 1830 Target::SettingsInitialize () 1831 { 1832 Process::SettingsInitialize (); 1833 } 1834 1835 void 1836 Target::SettingsTerminate () 1837 { 1838 Process::SettingsTerminate (); 1839 } 1840 1841 FileSpecList 1842 Target::GetDefaultExecutableSearchPaths () 1843 { 1844 TargetPropertiesSP properties_sp(Target::GetGlobalProperties()); 1845 if (properties_sp) 1846 return properties_sp->GetExecutableSearchPaths(); 1847 return FileSpecList(); 1848 } 1849 1850 FileSpecList 1851 Target::GetDefaultDebugFileSearchPaths () 1852 { 1853 TargetPropertiesSP properties_sp(Target::GetGlobalProperties()); 1854 if (properties_sp) 1855 return properties_sp->GetDebugFileSearchPaths(); 1856 return FileSpecList(); 1857 } 1858 1859 ArchSpec 1860 Target::GetDefaultArchitecture () 1861 { 1862 TargetPropertiesSP properties_sp(Target::GetGlobalProperties()); 1863 if (properties_sp) 1864 return properties_sp->GetDefaultArchitecture(); 1865 return ArchSpec(); 1866 } 1867 1868 void 1869 Target::SetDefaultArchitecture (const ArchSpec &arch) 1870 { 1871 TargetPropertiesSP properties_sp(Target::GetGlobalProperties()); 1872 if (properties_sp) 1873 { 1874 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::SetDefaultArchitecture setting target's default architecture to %s (%s)", arch.GetArchitectureName(), arch.GetTriple().getTriple().c_str()); 1875 return properties_sp->SetDefaultArchitecture(arch); 1876 } 1877 } 1878 1879 Target * 1880 Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) 1881 { 1882 // The target can either exist in the "process" of ExecutionContext, or in 1883 // the "target_sp" member of SymbolContext. This accessor helper function 1884 // will get the target from one of these locations. 1885 1886 Target *target = NULL; 1887 if (sc_ptr != NULL) 1888 target = sc_ptr->target_sp.get(); 1889 if (target == NULL && exe_ctx_ptr) 1890 target = exe_ctx_ptr->GetTargetPtr(); 1891 return target; 1892 } 1893 1894 ExpressionResults 1895 Target::EvaluateExpression 1896 ( 1897 const char *expr_cstr, 1898 StackFrame *frame, 1899 lldb::ValueObjectSP &result_valobj_sp, 1900 const EvaluateExpressionOptions& options 1901 ) 1902 { 1903 result_valobj_sp.reset(); 1904 1905 ExpressionResults execution_results = eExpressionSetupError; 1906 1907 if (expr_cstr == NULL || expr_cstr[0] == '\0') 1908 return execution_results; 1909 1910 // We shouldn't run stop hooks in expressions. 1911 // Be sure to reset this if you return anywhere within this function. 1912 bool old_suppress_value = m_suppress_stop_hooks; 1913 m_suppress_stop_hooks = true; 1914 1915 ExecutionContext exe_ctx; 1916 1917 if (frame) 1918 { 1919 frame->CalculateExecutionContext(exe_ctx); 1920 } 1921 else if (m_process_sp) 1922 { 1923 m_process_sp->CalculateExecutionContext(exe_ctx); 1924 } 1925 else 1926 { 1927 CalculateExecutionContext(exe_ctx); 1928 } 1929 1930 // Make sure we aren't just trying to see the value of a persistent 1931 // variable (something like "$0") 1932 lldb::ClangExpressionVariableSP persistent_var_sp; 1933 // Only check for persistent variables the expression starts with a '$' 1934 if (expr_cstr[0] == '$') 1935 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr); 1936 1937 if (persistent_var_sp) 1938 { 1939 result_valobj_sp = persistent_var_sp->GetValueObject (); 1940 execution_results = eExpressionCompleted; 1941 } 1942 else 1943 { 1944 const char *prefix = GetExpressionPrefixContentsAsCString(); 1945 Error error; 1946 execution_results = ClangUserExpression::Evaluate (exe_ctx, 1947 options, 1948 expr_cstr, 1949 prefix, 1950 result_valobj_sp, 1951 error); 1952 } 1953 1954 m_suppress_stop_hooks = old_suppress_value; 1955 1956 return execution_results; 1957 } 1958 1959 lldb::addr_t 1960 Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const 1961 { 1962 addr_t code_addr = load_addr; 1963 switch (m_arch.GetMachine()) 1964 { 1965 case llvm::Triple::arm: 1966 case llvm::Triple::thumb: 1967 switch (addr_class) 1968 { 1969 case eAddressClassData: 1970 case eAddressClassDebug: 1971 return LLDB_INVALID_ADDRESS; 1972 1973 case eAddressClassUnknown: 1974 case eAddressClassInvalid: 1975 case eAddressClassCode: 1976 case eAddressClassCodeAlternateISA: 1977 case eAddressClassRuntime: 1978 // Check if bit zero it no set? 1979 if ((code_addr & 1ull) == 0) 1980 { 1981 // Bit zero isn't set, check if the address is a multiple of 2? 1982 if (code_addr & 2ull) 1983 { 1984 // The address is a multiple of 2 so it must be thumb, set bit zero 1985 code_addr |= 1ull; 1986 } 1987 else if (addr_class == eAddressClassCodeAlternateISA) 1988 { 1989 // We checked the address and the address claims to be the alternate ISA 1990 // which means thumb, so set bit zero. 1991 code_addr |= 1ull; 1992 } 1993 } 1994 break; 1995 } 1996 break; 1997 1998 default: 1999 break; 2000 } 2001 return code_addr; 2002 } 2003 2004 lldb::addr_t 2005 Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const 2006 { 2007 addr_t opcode_addr = load_addr; 2008 switch (m_arch.GetMachine()) 2009 { 2010 case llvm::Triple::arm: 2011 case llvm::Triple::thumb: 2012 switch (addr_class) 2013 { 2014 case eAddressClassData: 2015 case eAddressClassDebug: 2016 return LLDB_INVALID_ADDRESS; 2017 2018 case eAddressClassInvalid: 2019 case eAddressClassUnknown: 2020 case eAddressClassCode: 2021 case eAddressClassCodeAlternateISA: 2022 case eAddressClassRuntime: 2023 opcode_addr &= ~(1ull); 2024 break; 2025 } 2026 break; 2027 2028 default: 2029 break; 2030 } 2031 return opcode_addr; 2032 } 2033 2034 SourceManager & 2035 Target::GetSourceManager () 2036 { 2037 if (m_source_manager_ap.get() == NULL) 2038 m_source_manager_ap.reset (new SourceManager(shared_from_this())); 2039 return *m_source_manager_ap; 2040 } 2041 2042 ClangModulesDeclVendor * 2043 Target::GetClangModulesDeclVendor () 2044 { 2045 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target 2046 2047 { 2048 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex); 2049 2050 if (!m_clang_modules_decl_vendor_ap) 2051 { 2052 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this)); 2053 } 2054 } 2055 2056 return m_clang_modules_decl_vendor_ap.get(); 2057 } 2058 2059 Target::StopHookSP 2060 Target::CreateStopHook () 2061 { 2062 lldb::user_id_t new_uid = ++m_stop_hook_next_id; 2063 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid)); 2064 m_stop_hooks[new_uid] = stop_hook_sp; 2065 return stop_hook_sp; 2066 } 2067 2068 bool 2069 Target::RemoveStopHookByID (lldb::user_id_t user_id) 2070 { 2071 size_t num_removed; 2072 num_removed = m_stop_hooks.erase (user_id); 2073 if (num_removed == 0) 2074 return false; 2075 else 2076 return true; 2077 } 2078 2079 void 2080 Target::RemoveAllStopHooks () 2081 { 2082 m_stop_hooks.clear(); 2083 } 2084 2085 Target::StopHookSP 2086 Target::GetStopHookByID (lldb::user_id_t user_id) 2087 { 2088 StopHookSP found_hook; 2089 2090 StopHookCollection::iterator specified_hook_iter; 2091 specified_hook_iter = m_stop_hooks.find (user_id); 2092 if (specified_hook_iter != m_stop_hooks.end()) 2093 found_hook = (*specified_hook_iter).second; 2094 return found_hook; 2095 } 2096 2097 bool 2098 Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state) 2099 { 2100 StopHookCollection::iterator specified_hook_iter; 2101 specified_hook_iter = m_stop_hooks.find (user_id); 2102 if (specified_hook_iter == m_stop_hooks.end()) 2103 return false; 2104 2105 (*specified_hook_iter).second->SetIsActive (active_state); 2106 return true; 2107 } 2108 2109 void 2110 Target::SetAllStopHooksActiveState (bool active_state) 2111 { 2112 StopHookCollection::iterator pos, end = m_stop_hooks.end(); 2113 for (pos = m_stop_hooks.begin(); pos != end; pos++) 2114 { 2115 (*pos).second->SetIsActive (active_state); 2116 } 2117 } 2118 2119 void 2120 Target::RunStopHooks () 2121 { 2122 if (m_suppress_stop_hooks) 2123 return; 2124 2125 if (!m_process_sp) 2126 return; 2127 2128 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression 2129 // since in that case we do not want to run the stop-hooks 2130 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression()) 2131 return; 2132 2133 if (m_stop_hooks.empty()) 2134 return; 2135 2136 StopHookCollection::iterator pos, end = m_stop_hooks.end(); 2137 2138 // If there aren't any active stop hooks, don't bother either: 2139 bool any_active_hooks = false; 2140 for (pos = m_stop_hooks.begin(); pos != end; pos++) 2141 { 2142 if ((*pos).second->IsActive()) 2143 { 2144 any_active_hooks = true; 2145 break; 2146 } 2147 } 2148 if (!any_active_hooks) 2149 return; 2150 2151 CommandReturnObject result; 2152 2153 std::vector<ExecutionContext> exc_ctx_with_reasons; 2154 std::vector<SymbolContext> sym_ctx_with_reasons; 2155 2156 ThreadList &cur_threadlist = m_process_sp->GetThreadList(); 2157 size_t num_threads = cur_threadlist.GetSize(); 2158 for (size_t i = 0; i < num_threads; i++) 2159 { 2160 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i); 2161 if (cur_thread_sp->ThreadStoppedForAReason()) 2162 { 2163 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0); 2164 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get())); 2165 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything)); 2166 } 2167 } 2168 2169 // If no threads stopped for a reason, don't run the stop-hooks. 2170 size_t num_exe_ctx = exc_ctx_with_reasons.size(); 2171 if (num_exe_ctx == 0) 2172 return; 2173 2174 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream()); 2175 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream()); 2176 2177 bool keep_going = true; 2178 bool hooks_ran = false; 2179 bool print_hook_header; 2180 bool print_thread_header; 2181 2182 if (num_exe_ctx == 1) 2183 print_thread_header = false; 2184 else 2185 print_thread_header = true; 2186 2187 if (m_stop_hooks.size() == 1) 2188 print_hook_header = false; 2189 else 2190 print_hook_header = true; 2191 2192 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++) 2193 { 2194 // result.Clear(); 2195 StopHookSP cur_hook_sp = (*pos).second; 2196 if (!cur_hook_sp->IsActive()) 2197 continue; 2198 2199 bool any_thread_matched = false; 2200 for (size_t i = 0; keep_going && i < num_exe_ctx; i++) 2201 { 2202 if ((cur_hook_sp->GetSpecifier () == NULL 2203 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i])) 2204 && (cur_hook_sp->GetThreadSpecifier() == NULL 2205 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef()))) 2206 { 2207 if (!hooks_ran) 2208 { 2209 hooks_ran = true; 2210 } 2211 if (print_hook_header && !any_thread_matched) 2212 { 2213 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ? 2214 cur_hook_sp->GetCommands().GetStringAtIndex(0) : 2215 NULL); 2216 if (cmd) 2217 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd); 2218 else 2219 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID()); 2220 any_thread_matched = true; 2221 } 2222 2223 if (print_thread_header) 2224 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID()); 2225 2226 CommandInterpreterRunOptions options; 2227 options.SetStopOnContinue (true); 2228 options.SetStopOnError (true); 2229 options.SetEchoCommands (false); 2230 options.SetPrintResults (true); 2231 options.SetAddToHistory (false); 2232 2233 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(), 2234 &exc_ctx_with_reasons[i], 2235 options, 2236 result); 2237 2238 // If the command started the target going again, we should bag out of 2239 // running the stop hooks. 2240 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) || 2241 (result.GetStatus() == eReturnStatusSuccessContinuingResult)) 2242 { 2243 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID()); 2244 keep_going = false; 2245 } 2246 } 2247 } 2248 } 2249 2250 result.GetImmediateOutputStream()->Flush(); 2251 result.GetImmediateErrorStream()->Flush(); 2252 } 2253 2254 const TargetPropertiesSP & 2255 Target::GetGlobalProperties() 2256 { 2257 static TargetPropertiesSP g_settings_sp; 2258 if (!g_settings_sp) 2259 { 2260 g_settings_sp.reset (new TargetProperties (NULL)); 2261 } 2262 return g_settings_sp; 2263 } 2264 2265 Error 2266 Target::Install (ProcessLaunchInfo *launch_info) 2267 { 2268 Error error; 2269 PlatformSP platform_sp (GetPlatform()); 2270 if (platform_sp) 2271 { 2272 if (platform_sp->IsRemote()) 2273 { 2274 if (platform_sp->IsConnected()) 2275 { 2276 // Install all files that have an install path, and always install the 2277 // main executable when connected to a remote platform 2278 const ModuleList& modules = GetImages(); 2279 const size_t num_images = modules.GetSize(); 2280 for (size_t idx = 0; idx < num_images; ++idx) 2281 { 2282 const bool is_main_executable = idx == 0; 2283 ModuleSP module_sp(modules.GetModuleAtIndex(idx)); 2284 if (module_sp) 2285 { 2286 FileSpec local_file (module_sp->GetFileSpec()); 2287 if (local_file) 2288 { 2289 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec()); 2290 if (!remote_file) 2291 { 2292 if (is_main_executable) // TODO: add setting for always installing main executable??? 2293 { 2294 // Always install the main executable 2295 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory(); 2296 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename(); 2297 } 2298 } 2299 if (remote_file) 2300 { 2301 error = platform_sp->Install(local_file, remote_file); 2302 if (error.Success()) 2303 { 2304 module_sp->SetPlatformFileSpec(remote_file); 2305 if (is_main_executable) 2306 { 2307 if (launch_info) 2308 launch_info->SetExecutableFile(remote_file, false); 2309 } 2310 } 2311 else 2312 break; 2313 } 2314 } 2315 } 2316 } 2317 } 2318 } 2319 } 2320 return error; 2321 } 2322 2323 bool 2324 Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id) 2325 { 2326 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr); 2327 } 2328 2329 bool 2330 Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr) 2331 { 2332 return m_images.ResolveFileAddress(file_addr, resolved_addr); 2333 } 2334 2335 bool 2336 Target::SetSectionLoadAddress (const SectionSP §ion_sp, addr_t new_section_load_addr, bool warn_multiple) 2337 { 2338 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp); 2339 if (old_section_load_addr != new_section_load_addr) 2340 { 2341 uint32_t stop_id = 0; 2342 ProcessSP process_sp(GetProcessSP()); 2343 if (process_sp) 2344 stop_id = process_sp->GetStopID(); 2345 else 2346 stop_id = m_section_load_history.GetLastStopID(); 2347 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple)) 2348 return true; // Return true if the section load address was changed... 2349 } 2350 return false; // Return false to indicate nothing changed 2351 2352 } 2353 2354 size_t 2355 Target::UnloadModuleSections (const ModuleList &module_list) 2356 { 2357 size_t section_unload_count = 0; 2358 size_t num_modules = module_list.GetSize(); 2359 for (size_t i=0; i<num_modules; ++i) 2360 { 2361 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i)); 2362 } 2363 return section_unload_count; 2364 } 2365 2366 size_t 2367 Target::UnloadModuleSections (const lldb::ModuleSP &module_sp) 2368 { 2369 uint32_t stop_id = 0; 2370 ProcessSP process_sp(GetProcessSP()); 2371 if (process_sp) 2372 stop_id = process_sp->GetStopID(); 2373 else 2374 stop_id = m_section_load_history.GetLastStopID(); 2375 SectionList *sections = module_sp->GetSectionList(); 2376 size_t section_unload_count = 0; 2377 if (sections) 2378 { 2379 const uint32_t num_sections = sections->GetNumSections(0); 2380 for (uint32_t i = 0; i < num_sections; ++i) 2381 { 2382 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i)); 2383 } 2384 } 2385 return section_unload_count; 2386 } 2387 2388 bool 2389 Target::SetSectionUnloaded (const lldb::SectionSP §ion_sp) 2390 { 2391 uint32_t stop_id = 0; 2392 ProcessSP process_sp(GetProcessSP()); 2393 if (process_sp) 2394 stop_id = process_sp->GetStopID(); 2395 else 2396 stop_id = m_section_load_history.GetLastStopID(); 2397 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp); 2398 } 2399 2400 bool 2401 Target::SetSectionUnloaded (const lldb::SectionSP §ion_sp, addr_t load_addr) 2402 { 2403 uint32_t stop_id = 0; 2404 ProcessSP process_sp(GetProcessSP()); 2405 if (process_sp) 2406 stop_id = process_sp->GetStopID(); 2407 else 2408 stop_id = m_section_load_history.GetLastStopID(); 2409 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr); 2410 } 2411 2412 void 2413 Target::ClearAllLoadedSections () 2414 { 2415 m_section_load_history.Clear(); 2416 } 2417 2418 2419 Error 2420 Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream) 2421 { 2422 Error error; 2423 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET)); 2424 2425 if (log) 2426 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ()); 2427 2428 StateType state = eStateInvalid; 2429 2430 // Scope to temporarily get the process state in case someone has manually 2431 // remotely connected already to a process and we can skip the platform 2432 // launching. 2433 { 2434 ProcessSP process_sp (GetProcessSP()); 2435 2436 if (process_sp) 2437 { 2438 state = process_sp->GetState(); 2439 if (log) 2440 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state)); 2441 } 2442 else 2443 { 2444 if (log) 2445 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__); 2446 } 2447 } 2448 2449 launch_info.GetFlags().Set (eLaunchFlagDebug); 2450 2451 // Get the value of synchronous execution here. If you wait till after you have started to 2452 // run, then you could have hit a breakpoint, whose command might switch the value, and 2453 // then you'll pick up that incorrect value. 2454 Debugger &debugger = GetDebugger(); 2455 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous (); 2456 2457 PlatformSP platform_sp (GetPlatform()); 2458 2459 // Finalize the file actions, and if none were given, default to opening 2460 // up a pseudo terminal 2461 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false; 2462 if (log) 2463 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s", 2464 __FUNCTION__, 2465 platform_sp ? "true" : "false", 2466 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a", 2467 default_to_use_pty ? "true" : "false"); 2468 2469 launch_info.FinalizeFileActions (this, default_to_use_pty); 2470 2471 if (state == eStateConnected) 2472 { 2473 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY)) 2474 { 2475 error.SetErrorString("can't launch in tty when launching through a remote connection"); 2476 return error; 2477 } 2478 } 2479 2480 if (!launch_info.GetArchitecture().IsValid()) 2481 launch_info.GetArchitecture() = GetArchitecture(); 2482 2483 // If we're not already connected to the process, and if we have a platform that can launch a process for debugging, go ahead and do that here. 2484 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ()) 2485 { 2486 if (log) 2487 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__); 2488 2489 m_process_sp = GetPlatform()->DebugProcess (launch_info, 2490 debugger, 2491 this, 2492 error); 2493 } 2494 else 2495 { 2496 if (log) 2497 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__); 2498 2499 if (state == eStateConnected) 2500 { 2501 assert(m_process_sp); 2502 } 2503 else 2504 { 2505 // Use a Process plugin to construct the process. 2506 const char *plugin_name = launch_info.GetProcessPluginName(); 2507 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL); 2508 } 2509 2510 // Since we didn't have a platform launch the process, launch it here. 2511 if (m_process_sp) 2512 error = m_process_sp->Launch (launch_info); 2513 } 2514 2515 if (!m_process_sp) 2516 { 2517 if (error.Success()) 2518 error.SetErrorString("failed to launch or debug process"); 2519 return error; 2520 } 2521 2522 if (error.Success()) 2523 { 2524 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false) 2525 { 2526 ListenerSP hijack_listener_sp (launch_info.GetHijackListener()); 2527 2528 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL); 2529 2530 if (state == eStateStopped) 2531 { 2532 if (!synchronous_execution) 2533 m_process_sp->RestoreProcessEvents (); 2534 2535 error = m_process_sp->PrivateResume(); 2536 2537 if (error.Success()) 2538 { 2539 // there is a race condition where this thread will return up the call stack to the main command 2540 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has 2541 // a chance to call PushProcessIOHandler() 2542 m_process_sp->SyncIOHandler(2000); 2543 2544 if (synchronous_execution) 2545 { 2546 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream); 2547 const bool must_be_alive = false; // eStateExited is ok, so this must be false 2548 if (!StateIsStoppedState(state, must_be_alive)) 2549 { 2550 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state)); 2551 } 2552 } 2553 } 2554 else 2555 { 2556 Error error2; 2557 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString()); 2558 error = error2; 2559 } 2560 } 2561 else if (state == eStateExited) 2562 { 2563 bool with_shell = !!launch_info.GetShell(); 2564 const int exit_status = m_process_sp->GetExitStatus(); 2565 const char *exit_desc = m_process_sp->GetExitDescription(); 2566 #define LAUNCH_SHELL_MESSAGE "\n'r' and 'run' are aliases that default to launching through a shell.\nTry launching without going through a shell by using 'process launch'." 2567 if (exit_desc && exit_desc[0]) 2568 { 2569 if (with_shell) 2570 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc); 2571 else 2572 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc); 2573 } 2574 else 2575 { 2576 if (with_shell) 2577 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status); 2578 else 2579 error.SetErrorStringWithFormat ("process exited with status %i", exit_status); 2580 } 2581 } 2582 else 2583 { 2584 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state)); 2585 } 2586 } 2587 m_process_sp->RestoreProcessEvents (); 2588 } 2589 else 2590 { 2591 Error error2; 2592 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString()); 2593 error = error2; 2594 } 2595 return error; 2596 } 2597 //-------------------------------------------------------------- 2598 // Target::StopHook 2599 //-------------------------------------------------------------- 2600 Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) : 2601 UserID (uid), 2602 m_target_sp (target_sp), 2603 m_commands (), 2604 m_specifier_sp (), 2605 m_thread_spec_ap(), 2606 m_active (true) 2607 { 2608 } 2609 2610 Target::StopHook::StopHook (const StopHook &rhs) : 2611 UserID (rhs.GetID()), 2612 m_target_sp (rhs.m_target_sp), 2613 m_commands (rhs.m_commands), 2614 m_specifier_sp (rhs.m_specifier_sp), 2615 m_thread_spec_ap (), 2616 m_active (rhs.m_active) 2617 { 2618 if (rhs.m_thread_spec_ap.get() != NULL) 2619 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get())); 2620 } 2621 2622 2623 Target::StopHook::~StopHook () 2624 { 2625 } 2626 2627 void 2628 Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier) 2629 { 2630 m_thread_spec_ap.reset (specifier); 2631 } 2632 2633 2634 void 2635 Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const 2636 { 2637 int indent_level = s->GetIndentLevel(); 2638 2639 s->SetIndentLevel(indent_level + 2); 2640 2641 s->Printf ("Hook: %" PRIu64 "\n", GetID()); 2642 if (m_active) 2643 s->Indent ("State: enabled\n"); 2644 else 2645 s->Indent ("State: disabled\n"); 2646 2647 if (m_specifier_sp) 2648 { 2649 s->Indent(); 2650 s->PutCString ("Specifier:\n"); 2651 s->SetIndentLevel (indent_level + 4); 2652 m_specifier_sp->GetDescription (s, level); 2653 s->SetIndentLevel (indent_level + 2); 2654 } 2655 2656 if (m_thread_spec_ap.get() != NULL) 2657 { 2658 StreamString tmp; 2659 s->Indent("Thread:\n"); 2660 m_thread_spec_ap->GetDescription (&tmp, level); 2661 s->SetIndentLevel (indent_level + 4); 2662 s->Indent (tmp.GetData()); 2663 s->PutCString ("\n"); 2664 s->SetIndentLevel (indent_level + 2); 2665 } 2666 2667 s->Indent ("Commands: \n"); 2668 s->SetIndentLevel (indent_level + 4); 2669 uint32_t num_commands = m_commands.GetSize(); 2670 for (uint32_t i = 0; i < num_commands; i++) 2671 { 2672 s->Indent(m_commands.GetStringAtIndex(i)); 2673 s->PutCString ("\n"); 2674 } 2675 s->SetIndentLevel (indent_level); 2676 } 2677 2678 //-------------------------------------------------------------- 2679 // class TargetProperties 2680 //-------------------------------------------------------------- 2681 2682 OptionEnumValueElement 2683 lldb_private::g_dynamic_value_types[] = 2684 { 2685 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"}, 2686 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."}, 2687 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."}, 2688 { 0, NULL, NULL } 2689 }; 2690 2691 static OptionEnumValueElement 2692 g_inline_breakpoint_enums[] = 2693 { 2694 { eInlineBreakpointsNever, "never", "Never look for inline breakpoint locations (fastest). This setting should only be used if you know that no inlining occurs in your programs."}, 2695 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."}, 2696 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."}, 2697 { 0, NULL, NULL } 2698 }; 2699 2700 typedef enum x86DisassemblyFlavor 2701 { 2702 eX86DisFlavorDefault, 2703 eX86DisFlavorIntel, 2704 eX86DisFlavorATT 2705 } x86DisassemblyFlavor; 2706 2707 static OptionEnumValueElement 2708 g_x86_dis_flavor_value_types[] = 2709 { 2710 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."}, 2711 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."}, 2712 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."}, 2713 { 0, NULL, NULL } 2714 }; 2715 2716 static OptionEnumValueElement 2717 g_hex_immediate_style_values[] = 2718 { 2719 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."}, 2720 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."}, 2721 { 0, NULL, NULL } 2722 }; 2723 2724 static OptionEnumValueElement 2725 g_load_script_from_sym_file_values[] = 2726 { 2727 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"}, 2728 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."}, 2729 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."}, 2730 { 0, NULL, NULL } 2731 }; 2732 2733 2734 static OptionEnumValueElement 2735 g_memory_module_load_level_values[] = 2736 { 2737 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."}, 2738 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."}, 2739 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."}, 2740 { 0, NULL, NULL } 2741 }; 2742 2743 static PropertyDefinition 2744 g_properties[] = 2745 { 2746 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." }, 2747 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." }, 2748 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." }, 2749 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." }, 2750 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." }, 2751 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings used to track the change of location between a source file when built, and " 2752 "where it exists on the current system. It consists of an array of duples, the first element of each duple is " 2753 "some part (starting at the root) of the path to the file when it was built, " 2754 "and the second is where the remainder of the original build hierarchy is rooted on the local system. " 2755 "Each element of the array is checked in order and the first one that results in a match wins." }, 2756 { "exec-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "Executable search paths to use when locating executable files whose paths don't match the local file system." }, 2757 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." }, 2758 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." }, 2759 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." }, 2760 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." }, 2761 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." }, 2762 { "arg0" , OptionValue::eTypeString , false, 0 , NULL, NULL, "The first argument passed to the program in the argument array which can be different from the executable itself." }, 2763 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run. Note that this does NOT include the argv[0] which is in target.arg0." }, 2764 { "env-vars" , OptionValue::eTypeDictionary, false, OptionValue::eTypeString , NULL, NULL, "A list of all the environment variables to be passed to the executable's environment, and their values." }, 2765 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." }, 2766 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." }, 2767 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." }, 2768 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." }, 2769 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "debugserver will detach (rather than killing) a process if it loses connection with lldb." }, 2770 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" }, 2771 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" }, 2772 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsAlways , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. " 2773 "Breakpoint locations can end up being inlined by the compiler, so that a compile unit 'a.c' might contain an inlined function from another source file. " 2774 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. " 2775 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. " 2776 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers " 2777 "and find that setting breakpoints is slow, then you can change this setting to headers. " 2778 "This setting allows you to control exactly which strategy is used when setting " 2779 "file and line breakpoints." }, 2780 // FIXME: This is the wrong way to do per-architecture settings, but we don't have a general per architecture settings system in place yet. 2781 { "x86-disassembly-flavor" , OptionValue::eTypeEnum , false, eX86DisFlavorDefault, NULL, g_x86_dis_flavor_value_types, "The default disassembly flavor to use for x86 or x86-64 targets." }, 2782 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." }, 2783 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, NULL, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." }, 2784 { "use-fast-stepping" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Use a fast stepping algorithm based on running from branch to branch rather than instruction single-stepping." }, 2785 { "load-script-from-symbol-file" , OptionValue::eTypeEnum , false, eLoadScriptFromSymFileWarn, NULL, g_load_script_from_sym_file_values, "Allow LLDB to load scripting resources embedded in symbol files when available." }, 2786 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values, 2787 "Loading modules from memory can be slow as reading the symbol tables and other data can take a long time depending on your connection to the debug target. " 2788 "This setting helps users control how much information gets loaded when loading modules from memory." 2789 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). " 2790 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). " 2791 "'minimal' is the fastest setting and will load section data with no symbols, but should rarely be used as stack frames in these memory regions will be inaccurate and not provide any context (fastest). " }, 2792 { "display-expression-in-crashlogs" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "Expressions that crash will show up in crash logs if the host system supports executable specific crash log strings and this setting is set to true." }, 2793 { "trap-handler-names" , OptionValue::eTypeArray , true, OptionValue::eTypeString, NULL, NULL, "A list of trap handler function names, e.g. a common Unix user process one is _sigtramp." }, 2794 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL } 2795 }; 2796 enum 2797 { 2798 ePropertyDefaultArch, 2799 ePropertyExprPrefix, 2800 ePropertyPreferDynamic, 2801 ePropertyEnableSynthetic, 2802 ePropertySkipPrologue, 2803 ePropertySourceMap, 2804 ePropertyExecutableSearchPaths, 2805 ePropertyDebugFileSearchPaths, 2806 ePropertyMaxChildrenCount, 2807 ePropertyMaxSummaryLength, 2808 ePropertyMaxMemReadSize, 2809 ePropertyBreakpointUseAvoidList, 2810 ePropertyArg0, 2811 ePropertyRunArgs, 2812 ePropertyEnvVars, 2813 ePropertyInheritEnv, 2814 ePropertyInputPath, 2815 ePropertyOutputPath, 2816 ePropertyErrorPath, 2817 ePropertyDetachOnError, 2818 ePropertyDisableASLR, 2819 ePropertyDisableSTDIO, 2820 ePropertyInlineStrategy, 2821 ePropertyDisassemblyFlavor, 2822 ePropertyUseHexImmediates, 2823 ePropertyHexImmediateStyle, 2824 ePropertyUseFastStepping, 2825 ePropertyLoadScriptFromSymbolFile, 2826 ePropertyMemoryModuleLoadLevel, 2827 ePropertyDisplayExpressionsInCrashlogs, 2828 ePropertyTrapHandlerNames 2829 }; 2830 2831 2832 class TargetOptionValueProperties : public OptionValueProperties 2833 { 2834 public: 2835 TargetOptionValueProperties (const ConstString &name) : 2836 OptionValueProperties (name), 2837 m_target (NULL), 2838 m_got_host_env (false) 2839 { 2840 } 2841 2842 // This constructor is used when creating TargetOptionValueProperties when it 2843 // is part of a new lldb_private::Target instance. It will copy all current 2844 // global property values as needed 2845 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) : 2846 OptionValueProperties(*target_properties_sp->GetValueProperties()), 2847 m_target (target), 2848 m_got_host_env (false) 2849 { 2850 } 2851 2852 virtual const Property * 2853 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const 2854 { 2855 // When getting the value for a key from the target options, we will always 2856 // try and grab the setting from the current target if there is one. Else we just 2857 // use the one from this instance. 2858 if (idx == ePropertyEnvVars) 2859 GetHostEnvironmentIfNeeded (); 2860 2861 if (exe_ctx) 2862 { 2863 Target *target = exe_ctx->GetTargetPtr(); 2864 if (target) 2865 { 2866 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get()); 2867 if (this != target_properties) 2868 return target_properties->ProtectedGetPropertyAtIndex (idx); 2869 } 2870 } 2871 return ProtectedGetPropertyAtIndex (idx); 2872 } 2873 2874 lldb::TargetSP 2875 GetTargetSP () 2876 { 2877 return m_target->shared_from_this(); 2878 } 2879 2880 protected: 2881 2882 void 2883 GetHostEnvironmentIfNeeded () const 2884 { 2885 if (!m_got_host_env) 2886 { 2887 if (m_target) 2888 { 2889 m_got_host_env = true; 2890 const uint32_t idx = ePropertyInheritEnv; 2891 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0)) 2892 { 2893 PlatformSP platform_sp (m_target->GetPlatform()); 2894 if (platform_sp) 2895 { 2896 StringList env; 2897 if (platform_sp->GetEnvironment(env)) 2898 { 2899 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars); 2900 if (env_dict) 2901 { 2902 const bool can_replace = false; 2903 const size_t envc = env.GetSize(); 2904 for (size_t idx=0; idx<envc; idx++) 2905 { 2906 const char *env_entry = env.GetStringAtIndex (idx); 2907 if (env_entry) 2908 { 2909 const char *equal_pos = ::strchr(env_entry, '='); 2910 ConstString key; 2911 // It is ok to have environment variables with no values 2912 const char *value = NULL; 2913 if (equal_pos) 2914 { 2915 key.SetCStringWithLength(env_entry, equal_pos - env_entry); 2916 if (equal_pos[1]) 2917 value = equal_pos + 1; 2918 } 2919 else 2920 { 2921 key.SetCString(env_entry); 2922 } 2923 // Don't allow existing keys to be replaced with ones we get from the platform environment 2924 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace); 2925 } 2926 } 2927 } 2928 } 2929 } 2930 } 2931 } 2932 } 2933 } 2934 Target *m_target; 2935 mutable bool m_got_host_env; 2936 }; 2937 2938 //---------------------------------------------------------------------- 2939 // TargetProperties 2940 //---------------------------------------------------------------------- 2941 TargetProperties::TargetProperties (Target *target) : 2942 Properties () 2943 { 2944 if (target) 2945 { 2946 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties())); 2947 } 2948 else 2949 { 2950 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target"))); 2951 m_collection_sp->Initialize(g_properties); 2952 m_collection_sp->AppendProperty(ConstString("process"), 2953 ConstString("Settings specify to processes."), 2954 true, 2955 Process::GetGlobalProperties()->GetValueProperties()); 2956 } 2957 } 2958 2959 TargetProperties::~TargetProperties () 2960 { 2961 } 2962 ArchSpec 2963 TargetProperties::GetDefaultArchitecture () const 2964 { 2965 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch); 2966 if (value) 2967 return value->GetCurrentValue(); 2968 return ArchSpec(); 2969 } 2970 2971 void 2972 TargetProperties::SetDefaultArchitecture (const ArchSpec& arch) 2973 { 2974 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch); 2975 if (value) 2976 return value->SetCurrentValue(arch, true); 2977 } 2978 2979 lldb::DynamicValueType 2980 TargetProperties::GetPreferDynamicValue() const 2981 { 2982 const uint32_t idx = ePropertyPreferDynamic; 2983 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 2984 } 2985 2986 bool 2987 TargetProperties::GetDisableASLR () const 2988 { 2989 const uint32_t idx = ePropertyDisableASLR; 2990 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 2991 } 2992 2993 void 2994 TargetProperties::SetDisableASLR (bool b) 2995 { 2996 const uint32_t idx = ePropertyDisableASLR; 2997 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 2998 } 2999 3000 bool 3001 TargetProperties::GetDetachOnError () const 3002 { 3003 const uint32_t idx = ePropertyDetachOnError; 3004 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3005 } 3006 3007 void 3008 TargetProperties::SetDetachOnError (bool b) 3009 { 3010 const uint32_t idx = ePropertyDetachOnError; 3011 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 3012 } 3013 3014 bool 3015 TargetProperties::GetDisableSTDIO () const 3016 { 3017 const uint32_t idx = ePropertyDisableSTDIO; 3018 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3019 } 3020 3021 void 3022 TargetProperties::SetDisableSTDIO (bool b) 3023 { 3024 const uint32_t idx = ePropertyDisableSTDIO; 3025 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 3026 } 3027 3028 const char * 3029 TargetProperties::GetDisassemblyFlavor () const 3030 { 3031 const uint32_t idx = ePropertyDisassemblyFlavor; 3032 const char *return_value; 3033 3034 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 3035 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value; 3036 return return_value; 3037 } 3038 3039 InlineStrategy 3040 TargetProperties::GetInlineStrategy () const 3041 { 3042 const uint32_t idx = ePropertyInlineStrategy; 3043 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 3044 } 3045 3046 const char * 3047 TargetProperties::GetArg0 () const 3048 { 3049 const uint32_t idx = ePropertyArg0; 3050 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL); 3051 } 3052 3053 void 3054 TargetProperties::SetArg0 (const char *arg) 3055 { 3056 const uint32_t idx = ePropertyArg0; 3057 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg); 3058 } 3059 3060 bool 3061 TargetProperties::GetRunArguments (Args &args) const 3062 { 3063 const uint32_t idx = ePropertyRunArgs; 3064 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args); 3065 } 3066 3067 void 3068 TargetProperties::SetRunArguments (const Args &args) 3069 { 3070 const uint32_t idx = ePropertyRunArgs; 3071 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args); 3072 } 3073 3074 size_t 3075 TargetProperties::GetEnvironmentAsArgs (Args &env) const 3076 { 3077 const uint32_t idx = ePropertyEnvVars; 3078 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env); 3079 } 3080 3081 bool 3082 TargetProperties::GetSkipPrologue() const 3083 { 3084 const uint32_t idx = ePropertySkipPrologue; 3085 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3086 } 3087 3088 PathMappingList & 3089 TargetProperties::GetSourcePathMap () const 3090 { 3091 const uint32_t idx = ePropertySourceMap; 3092 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx); 3093 assert(option_value); 3094 return option_value->GetCurrentValue(); 3095 } 3096 3097 FileSpecList & 3098 TargetProperties::GetExecutableSearchPaths () 3099 { 3100 const uint32_t idx = ePropertyExecutableSearchPaths; 3101 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx); 3102 assert(option_value); 3103 return option_value->GetCurrentValue(); 3104 } 3105 3106 FileSpecList & 3107 TargetProperties::GetDebugFileSearchPaths () 3108 { 3109 const uint32_t idx = ePropertyDebugFileSearchPaths; 3110 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx); 3111 assert(option_value); 3112 return option_value->GetCurrentValue(); 3113 } 3114 3115 bool 3116 TargetProperties::GetEnableSyntheticValue () const 3117 { 3118 const uint32_t idx = ePropertyEnableSynthetic; 3119 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3120 } 3121 3122 uint32_t 3123 TargetProperties::GetMaximumNumberOfChildrenToDisplay() const 3124 { 3125 const uint32_t idx = ePropertyMaxChildrenCount; 3126 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 3127 } 3128 3129 uint32_t 3130 TargetProperties::GetMaximumSizeOfStringSummary() const 3131 { 3132 const uint32_t idx = ePropertyMaxSummaryLength; 3133 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 3134 } 3135 3136 uint32_t 3137 TargetProperties::GetMaximumMemReadSize () const 3138 { 3139 const uint32_t idx = ePropertyMaxMemReadSize; 3140 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 3141 } 3142 3143 FileSpec 3144 TargetProperties::GetStandardInputPath () const 3145 { 3146 const uint32_t idx = ePropertyInputPath; 3147 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx); 3148 } 3149 3150 void 3151 TargetProperties::SetStandardInputPath (const char *p) 3152 { 3153 const uint32_t idx = ePropertyInputPath; 3154 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p); 3155 } 3156 3157 FileSpec 3158 TargetProperties::GetStandardOutputPath () const 3159 { 3160 const uint32_t idx = ePropertyOutputPath; 3161 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx); 3162 } 3163 3164 void 3165 TargetProperties::SetStandardOutputPath (const char *p) 3166 { 3167 const uint32_t idx = ePropertyOutputPath; 3168 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p); 3169 } 3170 3171 FileSpec 3172 TargetProperties::GetStandardErrorPath () const 3173 { 3174 const uint32_t idx = ePropertyErrorPath; 3175 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); 3176 } 3177 3178 const char * 3179 TargetProperties::GetExpressionPrefixContentsAsCString () 3180 { 3181 const uint32_t idx = ePropertyExprPrefix; 3182 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx); 3183 if (file) 3184 { 3185 const bool null_terminate = true; 3186 DataBufferSP data_sp(file->GetFileContents(null_terminate)); 3187 if (data_sp) 3188 return (const char *) data_sp->GetBytes(); 3189 } 3190 return NULL; 3191 } 3192 3193 void 3194 TargetProperties::SetStandardErrorPath (const char *p) 3195 { 3196 const uint32_t idx = ePropertyErrorPath; 3197 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p); 3198 } 3199 3200 bool 3201 TargetProperties::GetBreakpointsConsultPlatformAvoidList () 3202 { 3203 const uint32_t idx = ePropertyBreakpointUseAvoidList; 3204 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3205 } 3206 3207 bool 3208 TargetProperties::GetUseHexImmediates () const 3209 { 3210 const uint32_t idx = ePropertyUseHexImmediates; 3211 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3212 } 3213 3214 bool 3215 TargetProperties::GetUseFastStepping () const 3216 { 3217 const uint32_t idx = ePropertyUseFastStepping; 3218 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3219 } 3220 3221 bool 3222 TargetProperties::GetDisplayExpressionsInCrashlogs () const 3223 { 3224 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs; 3225 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 3226 } 3227 3228 LoadScriptFromSymFile 3229 TargetProperties::GetLoadScriptFromSymbolFile () const 3230 { 3231 const uint32_t idx = ePropertyLoadScriptFromSymbolFile; 3232 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value); 3233 } 3234 3235 Disassembler::HexImmediateStyle 3236 TargetProperties::GetHexImmediateStyle () const 3237 { 3238 const uint32_t idx = ePropertyHexImmediateStyle; 3239 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value); 3240 } 3241 3242 MemoryModuleLoadLevel 3243 TargetProperties::GetMemoryModuleLoadLevel() const 3244 { 3245 const uint32_t idx = ePropertyMemoryModuleLoadLevel; 3246 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value); 3247 } 3248 3249 bool 3250 TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const 3251 { 3252 const uint32_t idx = ePropertyTrapHandlerNames; 3253 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args); 3254 } 3255 3256 void 3257 TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args) 3258 { 3259 const uint32_t idx = ePropertyTrapHandlerNames; 3260 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args); 3261 } 3262 3263 //---------------------------------------------------------------------- 3264 // Target::TargetEventData 3265 //---------------------------------------------------------------------- 3266 const ConstString & 3267 Target::TargetEventData::GetFlavorString () 3268 { 3269 static ConstString g_flavor ("Target::TargetEventData"); 3270 return g_flavor; 3271 } 3272 3273 const ConstString & 3274 Target::TargetEventData::GetFlavor () const 3275 { 3276 return TargetEventData::GetFlavorString (); 3277 } 3278 3279 Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) : 3280 EventData(), 3281 m_target_sp (new_target_sp) 3282 { 3283 } 3284 3285 Target::TargetEventData::~TargetEventData() 3286 { 3287 3288 } 3289 3290 void 3291 Target::TargetEventData::Dump (Stream *s) const 3292 { 3293 3294 } 3295 3296 const TargetSP 3297 Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp) 3298 { 3299 TargetSP target_sp; 3300 3301 const TargetEventData *data = GetEventDataFromEvent (event_sp.get()); 3302 if (data) 3303 target_sp = data->m_target_sp; 3304 3305 return target_sp; 3306 } 3307 3308 const Target::TargetEventData * 3309 Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr) 3310 { 3311 if (event_ptr) 3312 { 3313 const EventData *event_data = event_ptr->GetData(); 3314 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString()) 3315 return static_cast <const TargetEventData *> (event_ptr->GetData()); 3316 } 3317 return NULL; 3318 } 3319 3320