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/Target/Target.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Breakpoint/BreakpointResolver.h" 17 #include "lldb/Breakpoint/BreakpointResolverAddress.h" 18 #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 19 #include "lldb/Breakpoint/BreakpointResolverName.h" 20 #include "lldb/Core/DataBufferMemoryMap.h" 21 #include "lldb/Core/Debugger.h" 22 #include "lldb/Core/Event.h" 23 #include "lldb/Core/Log.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Core/Timer.h" 26 #include "lldb/Core/ValueObject.h" 27 #include "lldb/Host/Host.h" 28 #include "lldb/lldb-private-log.h" 29 #include "lldb/Symbol/ObjectFile.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/StackFrame.h" 32 33 using namespace lldb; 34 using namespace lldb_private; 35 36 //---------------------------------------------------------------------- 37 // Target constructor 38 //---------------------------------------------------------------------- 39 Target::Target(Debugger &debugger) : 40 Broadcaster("lldb.target"), 41 TargetInstanceSettings (*GetSettingsController()), 42 m_debugger (debugger), 43 m_mutex (Mutex::eMutexTypeRecursive), 44 m_images(), 45 m_section_load_list (), 46 m_breakpoint_list (false), 47 m_internal_breakpoint_list (true), 48 m_process_sp(), 49 m_triple(), 50 m_search_filter_sp(), 51 m_image_search_paths (ImageSearchPathsChanged, this), 52 m_scratch_ast_context_ap (NULL), 53 m_persistent_variables () 54 { 55 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed"); 56 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded"); 57 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded"); 58 59 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 60 if (log) 61 log->Printf ("%p Target::Target()", this); 62 } 63 64 //---------------------------------------------------------------------- 65 // Destructor 66 //---------------------------------------------------------------------- 67 Target::~Target() 68 { 69 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 70 if (log) 71 log->Printf ("%p Target::~Target()", this); 72 DeleteCurrentProcess (); 73 } 74 75 void 76 Target::Dump (Stream *s, lldb::DescriptionLevel description_level) 77 { 78 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 79 if (description_level != lldb::eDescriptionLevelBrief) 80 { 81 s->Indent(); 82 s->PutCString("Target\n"); 83 s->IndentMore(); 84 m_images.Dump(s); 85 m_breakpoint_list.Dump(s); 86 m_internal_breakpoint_list.Dump(s); 87 s->IndentLess(); 88 } 89 else 90 { 91 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString()); 92 } 93 } 94 95 void 96 Target::DeleteCurrentProcess () 97 { 98 if (m_process_sp.get()) 99 { 100 m_section_load_list.Clear(); 101 if (m_process_sp->IsAlive()) 102 m_process_sp->Destroy(); 103 else 104 m_process_sp->Finalize(); 105 106 // Do any cleanup of the target we need to do between process instances. 107 // NB It is better to do this before destroying the process in case the 108 // clean up needs some help from the process. 109 m_breakpoint_list.ClearAllBreakpointSites(); 110 m_internal_breakpoint_list.ClearAllBreakpointSites(); 111 m_process_sp.reset(); 112 } 113 } 114 115 const lldb::ProcessSP & 116 Target::CreateProcess (Listener &listener, const char *plugin_name) 117 { 118 DeleteCurrentProcess (); 119 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); 120 return m_process_sp; 121 } 122 123 const lldb::ProcessSP & 124 Target::GetProcessSP () const 125 { 126 return m_process_sp; 127 } 128 129 lldb::TargetSP 130 Target::GetSP() 131 { 132 return m_debugger.GetTargetList().GetTargetSP(this); 133 } 134 135 BreakpointList & 136 Target::GetBreakpointList(bool internal) 137 { 138 if (internal) 139 return m_internal_breakpoint_list; 140 else 141 return m_breakpoint_list; 142 } 143 144 const BreakpointList & 145 Target::GetBreakpointList(bool internal) const 146 { 147 if (internal) 148 return m_internal_breakpoint_list; 149 else 150 return m_breakpoint_list; 151 } 152 153 BreakpointSP 154 Target::GetBreakpointByID (break_id_t break_id) 155 { 156 BreakpointSP bp_sp; 157 158 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 159 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 160 else 161 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 162 163 return bp_sp; 164 } 165 166 BreakpointSP 167 Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) 168 { 169 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 170 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); 171 return CreateBreakpoint (filter_sp, resolver_sp, internal); 172 } 173 174 175 BreakpointSP 176 Target::CreateBreakpoint (lldb::addr_t addr, bool internal) 177 { 178 Address so_addr; 179 // Attempt to resolve our load address if possible, though it is ok if 180 // it doesn't resolve to section/offset. 181 182 // Try and resolve as a load address if possible 183 m_section_load_list.ResolveLoadAddress(addr, so_addr); 184 if (!so_addr.IsValid()) 185 { 186 // The address didn't resolve, so just set this as an absolute address 187 so_addr.SetOffset (addr); 188 } 189 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); 190 return bp_sp; 191 } 192 193 BreakpointSP 194 Target::CreateBreakpoint (Address &addr, bool internal) 195 { 196 TargetSP target_sp = this->GetSP(); 197 SearchFilterSP filter_sp(new SearchFilter (target_sp)); 198 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); 199 return CreateBreakpoint (filter_sp, resolver_sp, internal); 200 } 201 202 BreakpointSP 203 Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) 204 { 205 BreakpointSP bp_sp; 206 if (func_name) 207 { 208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 209 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); 210 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); 211 } 212 return bp_sp; 213 } 214 215 216 SearchFilterSP 217 Target::GetSearchFilterForModule (const FileSpec *containingModule) 218 { 219 SearchFilterSP filter_sp; 220 lldb::TargetSP target_sp = this->GetSP(); 221 if (containingModule != NULL) 222 { 223 // TODO: We should look into sharing module based search filters 224 // across many breakpoints like we do for the simple target based one 225 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); 226 } 227 else 228 { 229 if (m_search_filter_sp.get() == NULL) 230 m_search_filter_sp.reset (new SearchFilter (target_sp)); 231 filter_sp = m_search_filter_sp; 232 } 233 return filter_sp; 234 } 235 236 BreakpointSP 237 Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) 238 { 239 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 240 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); 241 242 return CreateBreakpoint (filter_sp, resolver_sp, internal); 243 } 244 245 BreakpointSP 246 Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) 247 { 248 BreakpointSP bp_sp; 249 if (filter_sp && resolver_sp) 250 { 251 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); 252 resolver_sp->SetBreakpoint (bp_sp.get()); 253 254 if (internal) 255 m_internal_breakpoint_list.Add (bp_sp, false); 256 else 257 m_breakpoint_list.Add (bp_sp, true); 258 259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 260 if (log) 261 { 262 StreamString s; 263 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 264 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); 265 } 266 267 bp_sp->ResolveBreakpoint(); 268 } 269 270 if (!internal && bp_sp) 271 { 272 m_last_created_breakpoint = bp_sp; 273 } 274 275 return bp_sp; 276 } 277 278 void 279 Target::RemoveAllBreakpoints (bool internal_also) 280 { 281 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 282 if (log) 283 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 284 285 m_breakpoint_list.RemoveAll (true); 286 if (internal_also) 287 m_internal_breakpoint_list.RemoveAll (false); 288 289 m_last_created_breakpoint.reset(); 290 } 291 292 void 293 Target::DisableAllBreakpoints (bool internal_also) 294 { 295 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 296 if (log) 297 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 298 299 m_breakpoint_list.SetEnabledAll (false); 300 if (internal_also) 301 m_internal_breakpoint_list.SetEnabledAll (false); 302 } 303 304 void 305 Target::EnableAllBreakpoints (bool internal_also) 306 { 307 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 308 if (log) 309 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 310 311 m_breakpoint_list.SetEnabledAll (true); 312 if (internal_also) 313 m_internal_breakpoint_list.SetEnabledAll (true); 314 } 315 316 bool 317 Target::RemoveBreakpointByID (break_id_t break_id) 318 { 319 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 320 if (log) 321 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 322 323 if (DisableBreakpointByID (break_id)) 324 { 325 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 326 m_internal_breakpoint_list.Remove(break_id, false); 327 else 328 { 329 if (m_last_created_breakpoint) 330 { 331 if (m_last_created_breakpoint->GetID() == break_id) 332 m_last_created_breakpoint.reset(); 333 } 334 m_breakpoint_list.Remove(break_id, true); 335 } 336 return true; 337 } 338 return false; 339 } 340 341 bool 342 Target::DisableBreakpointByID (break_id_t break_id) 343 { 344 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 345 if (log) 346 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 347 348 BreakpointSP bp_sp; 349 350 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 351 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 352 else 353 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 354 if (bp_sp) 355 { 356 bp_sp->SetEnabled (false); 357 return true; 358 } 359 return false; 360 } 361 362 bool 363 Target::EnableBreakpointByID (break_id_t break_id) 364 { 365 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 366 if (log) 367 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", 368 __FUNCTION__, 369 break_id, 370 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 371 372 BreakpointSP bp_sp; 373 374 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 375 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 376 else 377 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 378 379 if (bp_sp) 380 { 381 bp_sp->SetEnabled (true); 382 return true; 383 } 384 return false; 385 } 386 387 ModuleSP 388 Target::GetExecutableModule () 389 { 390 ModuleSP executable_sp; 391 if (m_images.GetSize() > 0) 392 executable_sp = m_images.GetModuleAtIndex(0); 393 return executable_sp; 394 } 395 396 void 397 Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) 398 { 399 m_images.Clear(); 400 m_scratch_ast_context_ap.reset(); 401 402 if (executable_sp.get()) 403 { 404 Timer scoped_timer (__PRETTY_FUNCTION__, 405 "Target::SetExecutableModule (executable = '%s/%s')", 406 executable_sp->GetFileSpec().GetDirectory().AsCString(), 407 executable_sp->GetFileSpec().GetFilename().AsCString()); 408 409 m_images.Append(executable_sp); // The first image is our exectuable file 410 411 ArchSpec exe_arch = executable_sp->GetArchitecture(); 412 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. 413 if (!m_arch_spec.IsValid()) 414 m_arch_spec = exe_arch; 415 416 FileSpecList dependent_files; 417 ObjectFile * executable_objfile = executable_sp->GetObjectFile(); 418 if (executable_objfile == NULL) 419 { 420 421 FileSpec bundle_executable(executable_sp->GetFileSpec()); 422 if (Host::ResolveExecutableInBundle (bundle_executable)) 423 { 424 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, 425 exe_arch)); 426 SetExecutableModule (bundle_exe_module_sp, get_dependent_files); 427 if (bundle_exe_module_sp->GetObjectFile() != NULL) 428 executable_sp = bundle_exe_module_sp; 429 return; 430 } 431 } 432 433 if (executable_objfile) 434 { 435 executable_objfile->GetDependentModules(dependent_files); 436 for (uint32_t i=0; i<dependent_files.GetSize(); i++) 437 { 438 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), 439 exe_arch)); 440 if (image_module_sp.get()) 441 { 442 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY 443 ObjectFile *objfile = image_module_sp->GetObjectFile(); 444 if (objfile) 445 objfile->GetDependentModules(dependent_files); 446 } 447 } 448 } 449 450 // Now see if we know the target triple, and if so, create our scratch AST context: 451 ConstString target_triple; 452 if (GetTargetTriple(target_triple)) 453 { 454 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString())); 455 } 456 } 457 458 UpdateInstanceName(); 459 } 460 461 462 ModuleList& 463 Target::GetImages () 464 { 465 return m_images; 466 } 467 468 ArchSpec 469 Target::GetArchitecture () const 470 { 471 return m_arch_spec; 472 } 473 474 bool 475 Target::SetArchitecture (const ArchSpec &arch_spec) 476 { 477 if (m_arch_spec == arch_spec) 478 { 479 // If we're setting the architecture to our current architecture, we 480 // don't need to do anything. 481 return true; 482 } 483 else if (!m_arch_spec.IsValid()) 484 { 485 // If we haven't got a valid arch spec, then we just need to set it. 486 m_arch_spec = arch_spec; 487 return true; 488 } 489 else 490 { 491 // If we have an executable file, try to reset the executable to the desired architecture 492 m_arch_spec = arch_spec; 493 ModuleSP executable_sp = GetExecutableModule (); 494 m_images.Clear(); 495 m_scratch_ast_context_ap.reset(); 496 m_triple.Clear(); 497 // Need to do something about unsetting breakpoints. 498 499 if (executable_sp) 500 { 501 FileSpec exec_file_spec = executable_sp->GetFileSpec(); 502 Error error = ModuleList::GetSharedModule(exec_file_spec, 503 arch_spec, 504 NULL, 505 NULL, 506 0, 507 executable_sp, 508 NULL, 509 NULL); 510 511 if (!error.Fail() && executable_sp) 512 { 513 SetExecutableModule (executable_sp, true); 514 return true; 515 } 516 else 517 { 518 return false; 519 } 520 } 521 else 522 { 523 return false; 524 } 525 } 526 } 527 528 bool 529 Target::GetTargetTriple(ConstString &triple) 530 { 531 triple.Clear(); 532 533 if (m_triple) 534 { 535 triple = m_triple; 536 } 537 else 538 { 539 Module *exe_module = GetExecutableModule().get(); 540 if (exe_module) 541 { 542 ObjectFile *objfile = exe_module->GetObjectFile(); 543 if (objfile) 544 { 545 objfile->GetTargetTriple(m_triple); 546 triple = m_triple; 547 } 548 } 549 } 550 return !triple.IsEmpty(); 551 } 552 553 void 554 Target::ModuleAdded (ModuleSP &module_sp) 555 { 556 // A module is being added to this target for the first time 557 ModuleList module_list; 558 module_list.Append(module_sp); 559 ModulesDidLoad (module_list); 560 } 561 562 void 563 Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) 564 { 565 // A module is being added to this target for the first time 566 ModuleList module_list; 567 module_list.Append (old_module_sp); 568 ModulesDidUnload (module_list); 569 module_list.Clear (); 570 module_list.Append (new_module_sp); 571 ModulesDidLoad (module_list); 572 } 573 574 void 575 Target::ModulesDidLoad (ModuleList &module_list) 576 { 577 m_breakpoint_list.UpdateBreakpoints (module_list, true); 578 // TODO: make event data that packages up the module_list 579 BroadcastEvent (eBroadcastBitModulesLoaded, NULL); 580 } 581 582 void 583 Target::ModulesDidUnload (ModuleList &module_list) 584 { 585 m_breakpoint_list.UpdateBreakpoints (module_list, false); 586 587 // Remove the images from the target image list 588 m_images.Remove(module_list); 589 590 // TODO: make event data that packages up the module_list 591 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); 592 } 593 594 size_t 595 Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error) 596 { 597 const Section *section = addr.GetSection(); 598 if (section && section->GetModule()) 599 { 600 ObjectFile *objfile = section->GetModule()->GetObjectFile(); 601 if (objfile) 602 { 603 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile, 604 addr.GetOffset(), 605 dst, 606 dst_len); 607 if (bytes_read > 0) 608 return bytes_read; 609 else 610 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString()); 611 } 612 else 613 { 614 error.SetErrorString("address isn't from a object file"); 615 } 616 } 617 else 618 { 619 error.SetErrorString("address doesn't contain a section that points to a section in a object file"); 620 } 621 return 0; 622 } 623 624 size_t 625 Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error) 626 { 627 error.Clear(); 628 629 bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); 630 631 size_t bytes_read = 0; 632 Address resolved_addr(addr); 633 if (!resolved_addr.IsSectionOffset()) 634 { 635 if (process_is_valid) 636 { 637 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr); 638 } 639 else 640 { 641 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); 642 } 643 } 644 645 if (prefer_file_cache) 646 { 647 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 648 if (bytes_read > 0) 649 return bytes_read; 650 } 651 652 if (process_is_valid) 653 { 654 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this); 655 if (load_addr == LLDB_INVALID_ADDRESS) 656 { 657 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) 658 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", 659 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), 660 resolved_addr.GetFileAddress()); 661 else 662 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); 663 } 664 else 665 { 666 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); 667 if (bytes_read != dst_len) 668 { 669 if (error.Success()) 670 { 671 if (bytes_read == 0) 672 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); 673 else 674 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); 675 } 676 } 677 if (bytes_read) 678 return bytes_read; 679 // If the address is not section offset we have an address that 680 // doesn't resolve to any address in any currently loaded shared 681 // libaries and we failed to read memory so there isn't anything 682 // more we can do. If it is section offset, we might be able to 683 // read cached memory from the object file. 684 if (!resolved_addr.IsSectionOffset()) 685 return 0; 686 } 687 } 688 689 if (!prefer_file_cache) 690 { 691 // If we didn't already try and read from the object file cache, then 692 // try it after failing to read from the process. 693 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 694 } 695 return 0; 696 } 697 698 699 ModuleSP 700 Target::GetSharedModule 701 ( 702 const FileSpec& file_spec, 703 const ArchSpec& arch, 704 const UUID *uuid_ptr, 705 const ConstString *object_name, 706 off_t object_offset, 707 Error *error_ptr 708 ) 709 { 710 // Don't pass in the UUID so we can tell if we have a stale value in our list 711 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library 712 bool did_create_module = false; 713 ModuleSP module_sp; 714 715 // If there are image search path entries, try to use them first to acquire a suitable image. 716 717 Error error; 718 719 if (m_image_search_paths.GetSize()) 720 { 721 FileSpec transformed_spec; 722 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) 723 { 724 transformed_spec.GetFilename() = file_spec.GetFilename(); 725 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); 726 } 727 } 728 729 // If a module hasn't been found yet, use the unmodified path. 730 731 if (!module_sp) 732 { 733 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); 734 } 735 736 if (module_sp) 737 { 738 m_images.Append (module_sp); 739 if (did_create_module) 740 { 741 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) 742 ModuleUpdated(old_module_sp, module_sp); 743 else 744 ModuleAdded(module_sp); 745 } 746 } 747 if (error_ptr) 748 *error_ptr = error; 749 return module_sp; 750 } 751 752 753 Target * 754 Target::CalculateTarget () 755 { 756 return this; 757 } 758 759 Process * 760 Target::CalculateProcess () 761 { 762 return NULL; 763 } 764 765 Thread * 766 Target::CalculateThread () 767 { 768 return NULL; 769 } 770 771 StackFrame * 772 Target::CalculateStackFrame () 773 { 774 return NULL; 775 } 776 777 void 778 Target::CalculateExecutionContext (ExecutionContext &exe_ctx) 779 { 780 exe_ctx.target = this; 781 exe_ctx.process = NULL; // Do NOT fill in process... 782 exe_ctx.thread = NULL; 783 exe_ctx.frame = NULL; 784 } 785 786 PathMappingList & 787 Target::GetImageSearchPathList () 788 { 789 return m_image_search_paths; 790 } 791 792 void 793 Target::ImageSearchPathsChanged 794 ( 795 const PathMappingList &path_list, 796 void *baton 797 ) 798 { 799 Target *target = (Target *)baton; 800 if (target->m_images.GetSize() > 1) 801 { 802 ModuleSP exe_module_sp (target->GetExecutableModule()); 803 if (exe_module_sp) 804 { 805 target->m_images.Clear(); 806 target->SetExecutableModule (exe_module_sp, true); 807 } 808 } 809 } 810 811 ClangASTContext * 812 Target::GetScratchClangASTContext() 813 { 814 return m_scratch_ast_context_ap.get(); 815 } 816 817 void 818 Target::Initialize () 819 { 820 UserSettingsControllerSP &usc = GetSettingsController(); 821 usc.reset (new SettingsController); 822 UserSettingsController::InitializeSettingsController (usc, 823 SettingsController::global_settings_table, 824 SettingsController::instance_settings_table); 825 } 826 827 void 828 Target::Terminate () 829 { 830 UserSettingsControllerSP &usc = GetSettingsController(); 831 UserSettingsController::FinalizeSettingsController (usc); 832 usc.reset(); 833 } 834 835 UserSettingsControllerSP & 836 Target::GetSettingsController () 837 { 838 static UserSettingsControllerSP g_settings_controller; 839 return g_settings_controller; 840 } 841 842 ArchSpec 843 Target::GetDefaultArchitecture () 844 { 845 lldb::UserSettingsControllerSP &settings_controller = GetSettingsController(); 846 lldb::SettableVariableType var_type; 847 Error err; 848 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err); 849 850 const char *default_name = ""; 851 if (result.GetSize() == 1 && err.Success()) 852 default_name = result.GetStringAtIndex (0); 853 854 ArchSpec default_arch (default_name); 855 return default_arch; 856 } 857 858 void 859 Target::SetDefaultArchitecture (ArchSpec new_arch) 860 { 861 if (new_arch.IsValid()) 862 GetSettingsController ()->SetVariable ("target.default-arch", 863 new_arch.AsCString(), 864 lldb::eVarSetOperationAssign, 865 false, 866 "[]"); 867 } 868 869 Target * 870 Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) 871 { 872 // The target can either exist in the "process" of ExecutionContext, or in 873 // the "target_sp" member of SymbolContext. This accessor helper function 874 // will get the target from one of these locations. 875 876 Target *target = NULL; 877 if (sc_ptr != NULL) 878 target = sc_ptr->target_sp.get(); 879 if (target == NULL) 880 { 881 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL) 882 target = &exe_ctx_ptr->process->GetTarget(); 883 } 884 return target; 885 } 886 887 888 void 889 Target::UpdateInstanceName () 890 { 891 StreamString sstr; 892 893 ModuleSP module_sp = GetExecutableModule(); 894 if (module_sp) 895 { 896 sstr.Printf ("%s_%s", 897 module_sp->GetFileSpec().GetFilename().AsCString(), 898 module_sp->GetArchitecture().AsCString()); 899 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), 900 sstr.GetData()); 901 } 902 } 903 904 const char * 905 Target::GetExpressionPrefixContentsAsCString () 906 { 907 return m_expr_prefix_contents.c_str(); 908 } 909 910 ExecutionResults 911 Target::EvaluateExpression 912 ( 913 const char *expr_cstr, 914 StackFrame *frame, 915 bool unwind_on_error, 916 bool keep_in_memory, 917 lldb::ValueObjectSP &result_valobj_sp 918 ) 919 { 920 ExecutionResults execution_results = eExecutionSetupError; 921 922 result_valobj_sp.reset(); 923 924 ExecutionContext exe_ctx; 925 if (frame) 926 { 927 frame->CalculateExecutionContext(exe_ctx); 928 Error error; 929 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember | 930 StackFrame::eExpressionPathOptionsNoFragileObjcIvar; 931 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error); 932 } 933 else if (m_process_sp) 934 { 935 m_process_sp->CalculateExecutionContext(exe_ctx); 936 } 937 else 938 { 939 CalculateExecutionContext(exe_ctx); 940 } 941 942 if (result_valobj_sp) 943 { 944 execution_results = eExecutionCompleted; 945 // We got a result from the frame variable expression path above... 946 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName()); 947 948 lldb::ValueObjectSP const_valobj_sp; 949 950 // Check in case our value is already a constant value 951 if (result_valobj_sp->GetIsConstant()) 952 { 953 const_valobj_sp = result_valobj_sp; 954 const_valobj_sp->SetName (persistent_variable_name); 955 } 956 else 957 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(), 958 persistent_variable_name); 959 960 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp; 961 962 result_valobj_sp = const_valobj_sp; 963 964 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp)); 965 assert (clang_expr_variable_sp.get()); 966 967 // Set flags and live data as appropriate 968 969 const Value &result_value = live_valobj_sp->GetValue(); 970 971 switch (result_value.GetValueType()) 972 { 973 case Value::eValueTypeHostAddress: 974 case Value::eValueTypeFileAddress: 975 // we don't do anything with these for now 976 break; 977 case Value::eValueTypeScalar: 978 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; 979 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation; 980 break; 981 case Value::eValueTypeLoadAddress: 982 clang_expr_variable_sp->m_live_sp = live_valobj_sp; 983 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference; 984 break; 985 } 986 } 987 else 988 { 989 // Make sure we aren't just trying to see the value of a persistent 990 // variable (something like "$0") 991 lldb::ClangExpressionVariableSP persistent_var_sp; 992 // Only check for persistent variables the expression starts with a '$' 993 if (expr_cstr[0] == '$') 994 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr); 995 996 if (persistent_var_sp) 997 { 998 result_valobj_sp = persistent_var_sp->GetValueObject (); 999 execution_results = eExecutionCompleted; 1000 } 1001 else 1002 { 1003 const char *prefix = GetExpressionPrefixContentsAsCString(); 1004 1005 execution_results = ClangUserExpression::Evaluate (exe_ctx, 1006 unwind_on_error, 1007 keep_in_memory, 1008 expr_cstr, 1009 prefix, 1010 result_valobj_sp); 1011 } 1012 } 1013 return execution_results; 1014 } 1015 1016 //-------------------------------------------------------------- 1017 // class Target::SettingsController 1018 //-------------------------------------------------------------- 1019 1020 Target::SettingsController::SettingsController () : 1021 UserSettingsController ("target", Debugger::GetSettingsController()), 1022 m_default_architecture () 1023 { 1024 m_default_settings.reset (new TargetInstanceSettings (*this, false, 1025 InstanceSettings::GetDefaultName().AsCString())); 1026 } 1027 1028 Target::SettingsController::~SettingsController () 1029 { 1030 } 1031 1032 lldb::InstanceSettingsSP 1033 Target::SettingsController::CreateInstanceSettings (const char *instance_name) 1034 { 1035 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(), 1036 false, 1037 instance_name); 1038 lldb::InstanceSettingsSP new_settings_sp (new_settings); 1039 return new_settings_sp; 1040 } 1041 1042 const ConstString & 1043 Target::SettingsController::DefArchVarName () 1044 { 1045 static ConstString def_arch_var_name ("default-arch"); 1046 1047 return def_arch_var_name; 1048 } 1049 1050 bool 1051 Target::SettingsController::SetGlobalVariable (const ConstString &var_name, 1052 const char *index_value, 1053 const char *value, 1054 const SettingEntry &entry, 1055 const lldb::VarSetOperationType op, 1056 Error&err) 1057 { 1058 if (var_name == DefArchVarName()) 1059 { 1060 ArchSpec tmp_spec (value); 1061 if (tmp_spec.IsValid()) 1062 m_default_architecture = tmp_spec; 1063 else 1064 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value); 1065 } 1066 return true; 1067 } 1068 1069 1070 bool 1071 Target::SettingsController::GetGlobalVariable (const ConstString &var_name, 1072 StringList &value, 1073 Error &err) 1074 { 1075 if (var_name == DefArchVarName()) 1076 { 1077 // If the arch is invalid (the default), don't show a string for it 1078 if (m_default_architecture.IsValid()) 1079 value.AppendString (m_default_architecture.AsCString()); 1080 return true; 1081 } 1082 else 1083 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1084 1085 return false; 1086 } 1087 1088 //-------------------------------------------------------------- 1089 // class TargetInstanceSettings 1090 //-------------------------------------------------------------- 1091 1092 TargetInstanceSettings::TargetInstanceSettings 1093 ( 1094 UserSettingsController &owner, 1095 bool live_instance, 1096 const char *name 1097 ) : 1098 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance) 1099 { 1100 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called 1101 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers. 1102 // For this reason it has to be called here, rather than in the initializer or in the parent constructor. 1103 // This is true for CreateInstanceName() too. 1104 1105 if (GetInstanceName () == InstanceSettings::InvalidName()) 1106 { 1107 ChangeInstanceName (std::string (CreateInstanceName().AsCString())); 1108 m_owner.RegisterInstanceSettings (this); 1109 } 1110 1111 if (live_instance) 1112 { 1113 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1114 CopyInstanceSettings (pending_settings,false); 1115 //m_owner.RemovePendingSettings (m_instance_name); 1116 } 1117 } 1118 1119 TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) : 1120 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()) 1121 { 1122 if (m_instance_name != InstanceSettings::GetDefaultName()) 1123 { 1124 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1125 CopyInstanceSettings (pending_settings,false); 1126 //m_owner.RemovePendingSettings (m_instance_name); 1127 } 1128 } 1129 1130 TargetInstanceSettings::~TargetInstanceSettings () 1131 { 1132 } 1133 1134 TargetInstanceSettings& 1135 TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs) 1136 { 1137 if (this != &rhs) 1138 { 1139 } 1140 1141 return *this; 1142 } 1143 1144 #define EXPR_PREFIX_STRING "expr-prefix" 1145 1146 void 1147 TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, 1148 const char *index_value, 1149 const char *value, 1150 const ConstString &instance_name, 1151 const SettingEntry &entry, 1152 lldb::VarSetOperationType op, 1153 Error &err, 1154 bool pending) 1155 { 1156 static ConstString expr_prefix_str (EXPR_PREFIX_STRING); 1157 1158 if (var_name == expr_prefix_str) 1159 { 1160 switch (op) 1161 { 1162 default: 1163 err.SetErrorToGenericError (); 1164 err.SetErrorString ("Unrecognized operation. Cannot update value.\n"); 1165 return; 1166 case lldb::eVarSetOperationAssign: 1167 { 1168 FileSpec file_spec(value, true); 1169 1170 if (!file_spec.Exists()) 1171 { 1172 err.SetErrorToGenericError (); 1173 err.SetErrorStringWithFormat ("%s does not exist.\n", value); 1174 return; 1175 } 1176 1177 DataBufferMemoryMap buf; 1178 1179 if (!buf.MemoryMapFromFileSpec(&file_spec) && 1180 buf.GetError().Fail()) 1181 { 1182 err.SetErrorToGenericError (); 1183 err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString()); 1184 return; 1185 } 1186 1187 m_expr_prefix_path = value; 1188 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize()); 1189 } 1190 return; 1191 case lldb::eVarSetOperationAppend: 1192 err.SetErrorToGenericError (); 1193 err.SetErrorString ("Cannot append to a path.\n"); 1194 return; 1195 case lldb::eVarSetOperationClear: 1196 m_expr_prefix_path.clear (); 1197 m_expr_prefix_contents.clear (); 1198 return; 1199 } 1200 } 1201 } 1202 1203 void 1204 TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 1205 bool pending) 1206 { 1207 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get()); 1208 1209 if (!new_settings_ptr) 1210 return; 1211 1212 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path; 1213 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents; 1214 } 1215 1216 bool 1217 TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, 1218 const ConstString &var_name, 1219 StringList &value, 1220 Error *err) 1221 { 1222 static ConstString expr_prefix_str (EXPR_PREFIX_STRING); 1223 1224 if (var_name == expr_prefix_str) 1225 { 1226 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size()); 1227 } 1228 else 1229 { 1230 if (err) 1231 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1232 return false; 1233 } 1234 1235 return true; 1236 } 1237 1238 const ConstString 1239 TargetInstanceSettings::CreateInstanceName () 1240 { 1241 StreamString sstr; 1242 static int instance_count = 1; 1243 1244 sstr.Printf ("target_%d", instance_count); 1245 ++instance_count; 1246 1247 const ConstString ret_val (sstr.GetData()); 1248 return ret_val; 1249 } 1250 1251 //-------------------------------------------------- 1252 // Target::SettingsController Variable Tables 1253 //-------------------------------------------------- 1254 1255 SettingEntry 1256 Target::SettingsController::global_settings_table[] = 1257 { 1258 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 1259 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." }, 1260 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1261 }; 1262 1263 SettingEntry 1264 Target::SettingsController::instance_settings_table[] = 1265 { 1266 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 1267 { EXPR_PREFIX_STRING, eSetVarTypeString, NULL, NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." }, 1268 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1269 }; 1270