Target.cpp revision eea264007bc5fb42c8f3239726a9d28ae42e1b7b
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/Event.h" 21#include "lldb/Core/Log.h" 22#include "lldb/Core/Timer.h" 23#include "lldb/Core/StreamString.h" 24#include "lldb/Host/Host.h" 25#include "lldb/lldb-private-log.h" 26#include "lldb/Symbol/ObjectFile.h" 27#include "lldb/Target/Process.h" 28#include "lldb/Core/Debugger.h" 29 30using namespace lldb; 31using namespace lldb_private; 32 33//---------------------------------------------------------------------- 34// Target constructor 35//---------------------------------------------------------------------- 36Target::Target(Debugger &debugger) : 37 Broadcaster("Target"), 38 m_debugger (debugger), 39 m_images(), 40 m_section_load_list (), 41 m_breakpoint_list (false), 42 m_internal_breakpoint_list (true), 43 m_process_sp(), 44 m_triple(), 45 m_search_filter_sp(), 46 m_image_search_paths (ImageSearchPathsChanged, this), 47 m_scratch_ast_context_ap(NULL) 48{ 49 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); 50 if (log) 51 log->Printf ("%p Target::Target()", this); 52} 53 54//---------------------------------------------------------------------- 55// Destructor 56//---------------------------------------------------------------------- 57Target::~Target() 58{ 59 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); 60 if (log) 61 log->Printf ("%p Target::~Target()", this); 62 DeleteCurrentProcess (); 63} 64 65void 66Target::Dump (Stream *s) 67{ 68 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 69 s->Indent(); 70 s->PutCString("Target\n"); 71 s->IndentMore(); 72 m_images.Dump(s); 73 m_breakpoint_list.Dump(s); 74 m_internal_breakpoint_list.Dump(s); 75// if (m_process_sp.get()) 76// m_process_sp->Dump(s); 77 s->IndentLess(); 78} 79 80void 81Target::DeleteCurrentProcess () 82{ 83 if (m_process_sp.get()) 84 { 85 if (m_process_sp->IsAlive()) 86 m_process_sp->Destroy(); 87 else 88 m_process_sp->Finalize(); 89 90 // Do any cleanup of the target we need to do between process instances. 91 // NB It is better to do this before destroying the process in case the 92 // clean up needs some help from the process. 93 m_breakpoint_list.ClearAllBreakpointSites(); 94 m_internal_breakpoint_list.ClearAllBreakpointSites(); 95 m_process_sp.reset(); 96 } 97} 98 99const lldb::ProcessSP & 100Target::CreateProcess (Listener &listener, const char *plugin_name) 101{ 102 DeleteCurrentProcess (); 103 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); 104 return m_process_sp; 105} 106 107const lldb::ProcessSP & 108Target::GetProcessSP () const 109{ 110 return m_process_sp; 111} 112 113lldb::TargetSP 114Target::GetSP() 115{ 116 return m_debugger.GetTargetList().GetTargetSP(this); 117} 118 119BreakpointList & 120Target::GetBreakpointList(bool internal) 121{ 122 if (internal) 123 return m_internal_breakpoint_list; 124 else 125 return m_breakpoint_list; 126} 127 128const BreakpointList & 129Target::GetBreakpointList(bool internal) const 130{ 131 if (internal) 132 return m_internal_breakpoint_list; 133 else 134 return m_breakpoint_list; 135} 136 137BreakpointSP 138Target::GetBreakpointByID (break_id_t break_id) 139{ 140 BreakpointSP bp_sp; 141 142 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 143 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 144 else 145 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 146 147 return bp_sp; 148} 149 150BreakpointSP 151Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) 152{ 153 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 154 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); 155 return CreateBreakpoint (filter_sp, resolver_sp, internal); 156} 157 158 159BreakpointSP 160Target::CreateBreakpoint (lldb::addr_t addr, bool internal) 161{ 162 Address so_addr; 163 // Attempt to resolve our load address if possible, though it is ok if 164 // it doesn't resolve to section/offset. 165 166 // Try and resolve as a load address if possible 167 m_section_load_list.ResolveLoadAddress(addr, so_addr); 168 if (!so_addr.IsValid()) 169 { 170 // The address didn't resolve, so just set this as an absolute address 171 so_addr.SetOffset (addr); 172 } 173 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); 174 return bp_sp; 175} 176 177BreakpointSP 178Target::CreateBreakpoint (Address &addr, bool internal) 179{ 180 TargetSP target_sp = this->GetSP(); 181 SearchFilterSP filter_sp(new SearchFilter (target_sp)); 182 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); 183 return CreateBreakpoint (filter_sp, resolver_sp, internal); 184} 185 186BreakpointSP 187Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) 188{ 189 BreakpointSP bp_sp; 190 if (func_name) 191 { 192 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 193 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); 194 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); 195 } 196 return bp_sp; 197} 198 199 200SearchFilterSP 201Target::GetSearchFilterForModule (const FileSpec *containingModule) 202{ 203 SearchFilterSP filter_sp; 204 lldb::TargetSP target_sp = this->GetSP(); 205 if (containingModule != NULL) 206 { 207 // TODO: We should look into sharing module based search filters 208 // across many breakpoints like we do for the simple target based one 209 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); 210 } 211 else 212 { 213 if (m_search_filter_sp.get() == NULL) 214 m_search_filter_sp.reset (new SearchFilter (target_sp)); 215 filter_sp = m_search_filter_sp; 216 } 217 return filter_sp; 218} 219 220BreakpointSP 221Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) 222{ 223 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 224 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); 225 226 return CreateBreakpoint (filter_sp, resolver_sp, internal); 227} 228 229BreakpointSP 230Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) 231{ 232 BreakpointSP bp_sp; 233 if (filter_sp && resolver_sp) 234 { 235 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); 236 resolver_sp->SetBreakpoint (bp_sp.get()); 237 238 if (internal) 239 m_internal_breakpoint_list.Add (bp_sp, false); 240 else 241 m_breakpoint_list.Add (bp_sp, true); 242 243 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 244 if (log) 245 { 246 StreamString s; 247 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 248 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); 249 } 250 251 bp_sp->ResolveBreakpoint(); 252 } 253 return bp_sp; 254} 255 256void 257Target::RemoveAllBreakpoints (bool internal_also) 258{ 259 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 260 if (log) 261 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 262 263 m_breakpoint_list.RemoveAll (true); 264 if (internal_also) 265 m_internal_breakpoint_list.RemoveAll (false); 266} 267 268void 269Target::DisableAllBreakpoints (bool internal_also) 270{ 271 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 272 if (log) 273 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 274 275 m_breakpoint_list.SetEnabledAll (false); 276 if (internal_also) 277 m_internal_breakpoint_list.SetEnabledAll (false); 278} 279 280void 281Target::EnableAllBreakpoints (bool internal_also) 282{ 283 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 284 if (log) 285 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 286 287 m_breakpoint_list.SetEnabledAll (true); 288 if (internal_also) 289 m_internal_breakpoint_list.SetEnabledAll (true); 290} 291 292bool 293Target::RemoveBreakpointByID (break_id_t break_id) 294{ 295 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 296 if (log) 297 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 298 299 if (DisableBreakpointByID (break_id)) 300 { 301 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 302 m_internal_breakpoint_list.Remove(break_id, false); 303 else 304 m_breakpoint_list.Remove(break_id, true); 305 return true; 306 } 307 return false; 308} 309 310bool 311Target::DisableBreakpointByID (break_id_t break_id) 312{ 313 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 314 if (log) 315 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 316 317 BreakpointSP bp_sp; 318 319 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 320 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 321 else 322 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 323 if (bp_sp) 324 { 325 bp_sp->SetEnabled (false); 326 return true; 327 } 328 return false; 329} 330 331bool 332Target::EnableBreakpointByID (break_id_t break_id) 333{ 334 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 335 if (log) 336 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", 337 __FUNCTION__, 338 break_id, 339 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 340 341 BreakpointSP bp_sp; 342 343 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 344 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 345 else 346 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 347 348 if (bp_sp) 349 { 350 bp_sp->SetEnabled (true); 351 return true; 352 } 353 return false; 354} 355 356ModuleSP 357Target::GetExecutableModule () 358{ 359 ModuleSP executable_sp; 360 if (m_images.GetSize() > 0) 361 executable_sp = m_images.GetModuleAtIndex(0); 362 return executable_sp; 363} 364 365void 366Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) 367{ 368 m_images.Clear(); 369 m_scratch_ast_context_ap.reset(); 370 371 if (executable_sp.get()) 372 { 373 Timer scoped_timer (__PRETTY_FUNCTION__, 374 "Target::SetExecutableModule (executable = '%s/%s')", 375 executable_sp->GetFileSpec().GetDirectory().AsCString(), 376 executable_sp->GetFileSpec().GetFilename().AsCString()); 377 378 m_images.Append(executable_sp); // The first image is our exectuable file 379 380 ArchSpec exe_arch = executable_sp->GetArchitecture(); 381 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. 382 if (!m_arch_spec.IsValid()) 383 m_arch_spec = exe_arch; 384 385 FileSpecList dependent_files; 386 ObjectFile * executable_objfile = executable_sp->GetObjectFile(); 387 if (executable_objfile == NULL) 388 { 389 390 FileSpec bundle_executable(executable_sp->GetFileSpec()); 391 if (Host::ResolveExecutableInBundle (&bundle_executable)) 392 { 393 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, 394 exe_arch)); 395 SetExecutableModule (bundle_exe_module_sp, get_dependent_files); 396 if (bundle_exe_module_sp->GetObjectFile() != NULL) 397 executable_sp = bundle_exe_module_sp; 398 return; 399 } 400 } 401 402 if (executable_objfile) 403 { 404 executable_objfile->GetDependentModules(dependent_files); 405 for (uint32_t i=0; i<dependent_files.GetSize(); i++) 406 { 407 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), 408 exe_arch)); 409 if (image_module_sp.get()) 410 { 411 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY 412 ObjectFile *objfile = image_module_sp->GetObjectFile(); 413 if (objfile) 414 objfile->GetDependentModules(dependent_files); 415 } 416 } 417 } 418 419 // Now see if we know the target triple, and if so, create our scratch AST context: 420 ConstString target_triple; 421 if (GetTargetTriple(target_triple)) 422 { 423 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString())); 424 } 425 } 426} 427 428 429ModuleList& 430Target::GetImages () 431{ 432 return m_images; 433} 434 435ArchSpec 436Target::GetArchitecture () const 437{ 438 return m_arch_spec; 439} 440 441bool 442Target::SetArchitecture (const ArchSpec &arch_spec) 443{ 444 if (m_arch_spec == arch_spec) 445 { 446 // If we're setting the architecture to our current architecture, we 447 // don't need to do anything. 448 return true; 449 } 450 else if (!m_arch_spec.IsValid()) 451 { 452 // If we haven't got a valid arch spec, then we just need to set it. 453 m_arch_spec = arch_spec; 454 return true; 455 } 456 else 457 { 458 // If we have an executable file, try to reset the executable to the desired architecture 459 m_arch_spec = arch_spec; 460 ModuleSP executable_sp = GetExecutableModule (); 461 m_images.Clear(); 462 m_scratch_ast_context_ap.reset(); 463 m_triple.Clear(); 464 // Need to do something about unsetting breakpoints. 465 466 if (executable_sp) 467 { 468 FileSpec exec_file_spec = executable_sp->GetFileSpec(); 469 Error error = ModuleList::GetSharedModule(exec_file_spec, 470 arch_spec, 471 NULL, 472 NULL, 473 0, 474 executable_sp, 475 NULL, 476 NULL); 477 478 if (!error.Fail() && executable_sp) 479 { 480 SetExecutableModule (executable_sp, true); 481 return true; 482 } 483 else 484 { 485 return false; 486 } 487 } 488 else 489 { 490 return false; 491 } 492 } 493} 494 495bool 496Target::GetTargetTriple(ConstString &triple) 497{ 498 triple.Clear(); 499 500 if (m_triple) 501 { 502 triple = m_triple; 503 } 504 else 505 { 506 Module *exe_module = GetExecutableModule().get(); 507 if (exe_module) 508 { 509 ObjectFile *objfile = exe_module->GetObjectFile(); 510 if (objfile) 511 { 512 objfile->GetTargetTriple(m_triple); 513 triple = m_triple; 514 } 515 } 516 } 517 return !triple.IsEmpty(); 518} 519 520void 521Target::ModuleAdded (ModuleSP &module_sp) 522{ 523 // A module is being added to this target for the first time 524 ModuleList module_list; 525 module_list.Append(module_sp); 526 ModulesDidLoad (module_list); 527} 528 529void 530Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) 531{ 532 // A module is being added to this target for the first time 533 ModuleList module_list; 534 module_list.Append (old_module_sp); 535 ModulesDidUnload (module_list); 536 module_list.Clear (); 537 module_list.Append (new_module_sp); 538 ModulesDidLoad (module_list); 539} 540 541void 542Target::ModulesDidLoad (ModuleList &module_list) 543{ 544 m_breakpoint_list.UpdateBreakpoints (module_list, true); 545 // TODO: make event data that packages up the module_list 546 BroadcastEvent (eBroadcastBitModulesLoaded, NULL); 547} 548 549void 550Target::ModulesDidUnload (ModuleList &module_list) 551{ 552 m_breakpoint_list.UpdateBreakpoints (module_list, false); 553 // TODO: make event data that packages up the module_list 554 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); 555} 556 557size_t 558Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error) 559{ 560 error.Clear(); 561 562 bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); 563 564 Address resolved_addr(addr); 565 if (!resolved_addr.IsSectionOffset()) 566 { 567 if (process_is_valid) 568 { 569 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr); 570 } 571 else 572 { 573 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); 574 } 575 } 576 577 578 if (process_is_valid) 579 { 580 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this); 581 if (load_addr == LLDB_INVALID_ADDRESS) 582 { 583 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) 584 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", 585 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), 586 resolved_addr.GetFileAddress()); 587 else 588 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); 589 } 590 else 591 { 592 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); 593 if (bytes_read != dst_len) 594 { 595 if (error.Success()) 596 { 597 if (bytes_read == 0) 598 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); 599 else 600 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); 601 } 602 } 603 if (bytes_read) 604 return bytes_read; 605 // If the address is not section offset we have an address that 606 // doesn't resolve to any address in any currently loaded shared 607 // libaries and we failed to read memory so there isn't anything 608 // more we can do. If it is section offset, we might be able to 609 // read cached memory from the object file. 610 if (!resolved_addr.IsSectionOffset()) 611 return 0; 612 } 613 } 614 615 const Section *section = resolved_addr.GetSection(); 616 if (section && section->GetModule()) 617 { 618 ObjectFile *objfile = section->GetModule()->GetObjectFile(); 619 return section->ReadSectionDataFromObjectFile (objfile, 620 resolved_addr.GetOffset(), 621 dst, 622 dst_len); 623 } 624 return 0; 625} 626 627 628ModuleSP 629Target::GetSharedModule 630( 631 const FileSpec& file_spec, 632 const ArchSpec& arch, 633 const UUID *uuid_ptr, 634 const ConstString *object_name, 635 off_t object_offset, 636 Error *error_ptr 637) 638{ 639 // Don't pass in the UUID so we can tell if we have a stale value in our list 640 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library 641 bool did_create_module = false; 642 ModuleSP module_sp; 643 644 // If there are image search path entries, try to use them first to acquire a suitable image. 645 646 Error error; 647 648 if (m_image_search_paths.GetSize()) 649 { 650 FileSpec transformed_spec; 651 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) 652 { 653 transformed_spec.GetFilename() = file_spec.GetFilename(); 654 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); 655 } 656 } 657 658 // If a module hasn't been found yet, use the unmodified path. 659 660 if (!module_sp) 661 { 662 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); 663 } 664 665 if (module_sp) 666 { 667 m_images.Append (module_sp); 668 if (did_create_module) 669 { 670 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) 671 ModuleUpdated(old_module_sp, module_sp); 672 else 673 ModuleAdded(module_sp); 674 } 675 } 676 if (error_ptr) 677 *error_ptr = error; 678 return module_sp; 679} 680 681 682Target * 683Target::CalculateTarget () 684{ 685 return this; 686} 687 688Process * 689Target::CalculateProcess () 690{ 691 return NULL; 692} 693 694Thread * 695Target::CalculateThread () 696{ 697 return NULL; 698} 699 700StackFrame * 701Target::CalculateStackFrame () 702{ 703 return NULL; 704} 705 706void 707Target::Calculate (ExecutionContext &exe_ctx) 708{ 709 exe_ctx.target = this; 710 exe_ctx.process = NULL; // Do NOT fill in process... 711 exe_ctx.thread = NULL; 712 exe_ctx.frame = NULL; 713} 714 715PathMappingList & 716Target::GetImageSearchPathList () 717{ 718 return m_image_search_paths; 719} 720 721void 722Target::ImageSearchPathsChanged 723( 724 const PathMappingList &path_list, 725 void *baton 726) 727{ 728 Target *target = (Target *)baton; 729 if (target->m_images.GetSize() > 1) 730 { 731 ModuleSP exe_module_sp (target->GetExecutableModule()); 732 if (exe_module_sp) 733 { 734 target->m_images.Clear(); 735 target->SetExecutableModule (exe_module_sp, true); 736 } 737 } 738} 739 740ClangASTContext * 741Target::GetScratchClangASTContext() 742{ 743 return m_scratch_ast_context_ap.get(); 744} 745