1 //===-- SBTarget.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/API/SBTarget.h" 11 12 #include "lldb/lldb-include.h" 13 14 #include "lldb/API/SBFileSpec.h" 15 #include "lldb/API/SBModule.h" 16 #include "lldb/API/SBStream.h" 17 #include "lldb/Breakpoint/BreakpointID.h" 18 #include "lldb/Breakpoint/BreakpointIDList.h" 19 #include "lldb/Breakpoint/BreakpointList.h" 20 #include "lldb/Breakpoint/BreakpointLocation.h" 21 #include "lldb/Core/Address.h" 22 #include "lldb/Core/AddressResolver.h" 23 #include "lldb/Core/AddressResolverName.h" 24 #include "lldb/Interpreter/Args.h" 25 #include "lldb/Core/ArchSpec.h" 26 #include "lldb/Core/Debugger.h" 27 #include "lldb/Core/Disassembler.h" 28 #include "lldb/Core/FileSpec.h" 29 #include "lldb/Core/Log.h" 30 #include "lldb/Core/RegularExpression.h" 31 #include "lldb/Core/SearchFilter.h" 32 #include "lldb/Core/STLUtils.h" 33 #include "lldb/Target/Process.h" 34 #include "lldb/Target/Target.h" 35 #include "lldb/Target/TargetList.h" 36 37 #include "lldb/Interpreter/CommandReturnObject.h" 38 #include "../source/Commands/CommandObjectBreakpoint.h" 39 40 #include "lldb/API/SBDebugger.h" 41 #include "lldb/API/SBProcess.h" 42 #include "lldb/API/SBListener.h" 43 #include "lldb/API/SBBreakpoint.h" 44 45 using namespace lldb; 46 using namespace lldb_private; 47 48 #define DEFAULT_DISASM_BYTE_SIZE 32 49 50 //---------------------------------------------------------------------- 51 // SBTarget constructor 52 //---------------------------------------------------------------------- 53 SBTarget::SBTarget () : 54 m_opaque_sp () 55 { 56 } 57 58 SBTarget::SBTarget (const SBTarget& rhs) : 59 m_opaque_sp (rhs.m_opaque_sp) 60 { 61 } 62 63 SBTarget::SBTarget(const TargetSP& target_sp) : 64 m_opaque_sp (target_sp) 65 { 66 } 67 68 const SBTarget& 69 SBTarget::operator = (const SBTarget& rhs) 70 { 71 if (this != &rhs) 72 m_opaque_sp = rhs.m_opaque_sp; 73 return *this; 74 } 75 76 //---------------------------------------------------------------------- 77 // Destructor 78 //---------------------------------------------------------------------- 79 SBTarget::~SBTarget() 80 { 81 } 82 83 bool 84 SBTarget::IsValid () const 85 { 86 return m_opaque_sp.get() != NULL; 87 } 88 89 SBProcess 90 SBTarget::GetProcess () 91 { 92 93 SBProcess sb_process; 94 if (m_opaque_sp) 95 { 96 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 97 sb_process.SetProcess (m_opaque_sp->GetProcessSP()); 98 } 99 100 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 101 if (log) 102 { 103 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)", 104 m_opaque_sp.get(), sb_process.get()); 105 } 106 107 return sb_process; 108 } 109 110 SBDebugger 111 SBTarget::GetDebugger () const 112 { 113 SBDebugger debugger; 114 if (m_opaque_sp) 115 debugger.reset (m_opaque_sp->GetDebugger().GetSP()); 116 return debugger; 117 } 118 119 SBProcess 120 SBTarget::LaunchProcess 121 ( 122 char const **argv, 123 char const **envp, 124 const char *tty, 125 uint32_t launch_flags, 126 bool stop_at_entry 127 ) 128 { 129 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 130 131 if (log) 132 log->Printf ("SBTarget(%p)::LaunchProcess (argv=%p, envp=%p, tty=\"%s\", launch_flags=%d, stop_at_entry=%i)", 133 m_opaque_sp.get(), argv, envp, tty, launch_flags, stop_at_entry); 134 135 SBError sb_error; 136 SBProcess sb_process = Launch (argv, envp, tty, launch_flags, stop_at_entry, sb_error); 137 138 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); 139 if (log) 140 { 141 log->Printf ("SBTarget(%p)::LaunchProcess (...) => SBProcess(%p)", 142 m_opaque_sp.get(), sb_process.get()); 143 } 144 145 return sb_process; 146 } 147 148 SBProcess 149 SBTarget::Launch 150 ( 151 char const **argv, 152 char const **envp, 153 const char *tty, 154 uint32_t launch_flags, 155 bool stop_at_entry, 156 SBError &error 157 ) 158 { 159 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 160 161 if (log) 162 { 163 log->Printf ("SBTarget(%p)::Launch (argv=%p, envp=%p, tty=\"%s\", launch_flags=%d, stop_at_entry=%i, &error (%p))...", 164 m_opaque_sp.get(), argv, envp, tty, launch_flags, stop_at_entry, error.get()); 165 } 166 SBProcess sb_process; 167 if (m_opaque_sp) 168 { 169 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 170 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 171 172 if (sb_process.IsValid()) 173 { 174 error.SetError (sb_process->Launch (argv, envp, launch_flags, tty, tty, tty)); 175 if (error.Success()) 176 { 177 // We we are stopping at the entry point, we can return now! 178 if (stop_at_entry) 179 return sb_process; 180 181 // Make sure we are stopped at the entry 182 StateType state = sb_process->WaitForProcessToStop (NULL); 183 if (state == eStateStopped) 184 { 185 // resume the process to skip the entry point 186 error.SetError (sb_process->Resume()); 187 if (error.Success()) 188 { 189 // If we are doing synchronous mode, then wait for the 190 // process to stop yet again! 191 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false) 192 sb_process->WaitForProcessToStop (NULL); 193 } 194 } 195 } 196 } 197 else 198 { 199 error.SetErrorString ("unable to create lldb_private::Process"); 200 } 201 } 202 else 203 { 204 error.SetErrorString ("SBTarget is invalid"); 205 } 206 207 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); 208 if (log) 209 { 210 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)", 211 m_opaque_sp.get(), sb_process.get()); 212 } 213 214 return sb_process; 215 } 216 217 218 lldb::SBProcess 219 SBTarget::AttachToProcessWithID 220 ( 221 lldb::pid_t pid,// The process ID to attach to 222 SBError& error // An error explaining what went wrong if attach fails 223 ) 224 { 225 SBProcess sb_process; 226 if (m_opaque_sp) 227 { 228 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 229 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 230 231 if (sb_process.IsValid()) 232 { 233 error.SetError (sb_process->Attach (pid)); 234 } 235 else 236 { 237 error.SetErrorString ("unable to create lldb_private::Process"); 238 } 239 } 240 else 241 { 242 error.SetErrorString ("SBTarget is invalid"); 243 } 244 return sb_process; 245 246 } 247 248 lldb::SBProcess 249 SBTarget::AttachToProcessWithName 250 ( 251 const char *name, // basename of process to attach to 252 bool wait_for, // if true wait for a new instance of "name" to be launched 253 SBError& error // An error explaining what went wrong if attach fails 254 ) 255 { 256 SBProcess sb_process; 257 if (m_opaque_sp) 258 { 259 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 260 261 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 262 263 if (sb_process.IsValid()) 264 { 265 error.SetError (sb_process->Attach (name, wait_for)); 266 } 267 else 268 { 269 error.SetErrorString ("unable to create lldb_private::Process"); 270 } 271 } 272 else 273 { 274 error.SetErrorString ("SBTarget is invalid"); 275 } 276 return sb_process; 277 278 } 279 280 SBFileSpec 281 SBTarget::GetExecutable () 282 { 283 284 SBFileSpec exe_file_spec; 285 if (m_opaque_sp) 286 { 287 ModuleSP exe_module_sp (m_opaque_sp->GetExecutableModule ()); 288 if (exe_module_sp) 289 exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec()); 290 } 291 292 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 293 if (log) 294 { 295 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)", 296 m_opaque_sp.get(), exe_file_spec.get()); 297 } 298 299 return exe_file_spec; 300 } 301 302 303 bool 304 SBTarget::DeleteTargetFromList (TargetList *list) 305 { 306 if (m_opaque_sp) 307 return list->DeleteTarget (m_opaque_sp); 308 else 309 return false; 310 } 311 312 bool 313 SBTarget::operator == (const SBTarget &rhs) const 314 { 315 return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 316 } 317 318 bool 319 SBTarget::operator != (const SBTarget &rhs) const 320 { 321 return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 322 } 323 324 lldb_private::Target * 325 SBTarget::operator ->() const 326 { 327 return m_opaque_sp.get(); 328 } 329 330 lldb_private::Target * 331 SBTarget::get() const 332 { 333 return m_opaque_sp.get(); 334 } 335 336 void 337 SBTarget::reset (const lldb::TargetSP& target_sp) 338 { 339 m_opaque_sp = target_sp; 340 } 341 342 bool 343 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr, 344 lldb::SBAddress& addr) 345 { 346 if (m_opaque_sp) 347 { 348 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 349 return m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, *addr); 350 } 351 352 addr->Clear(); 353 return false; 354 } 355 356 SBBreakpoint 357 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line) 358 { 359 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line)); 360 } 361 362 SBBreakpoint 363 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line) 364 { 365 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 366 367 SBBreakpoint sb_bp; 368 if (m_opaque_sp.get() && line != 0) 369 { 370 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 371 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false); 372 } 373 374 if (log) 375 { 376 SBStream sstr; 377 sb_bp.GetDescription (sstr); 378 char path[PATH_MAX]; 379 sb_file_spec->GetPath (path, sizeof(path)); 380 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s", 381 m_opaque_sp.get(), 382 path, 383 line, 384 sb_bp.get(), 385 sstr.GetData()); 386 } 387 388 return sb_bp; 389 } 390 391 SBBreakpoint 392 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name) 393 { 394 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 395 396 SBBreakpoint sb_bp; 397 if (m_opaque_sp.get() && symbol_name && symbol_name[0]) 398 { 399 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 400 if (module_name && module_name[0]) 401 { 402 FileSpec module_file_spec(module_name, false); 403 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false); 404 } 405 else 406 { 407 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false); 408 } 409 } 410 411 if (log) 412 { 413 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)", 414 m_opaque_sp.get(), symbol_name, module_name, sb_bp.get()); 415 } 416 417 return sb_bp; 418 } 419 420 SBBreakpoint 421 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name) 422 { 423 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 424 425 SBBreakpoint sb_bp; 426 if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0]) 427 { 428 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 429 RegularExpression regexp(symbol_name_regex); 430 431 if (module_name && module_name[0]) 432 { 433 FileSpec module_file_spec(module_name, false); 434 435 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false); 436 } 437 else 438 { 439 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false); 440 } 441 } 442 443 if (log) 444 { 445 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)", 446 m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get()); 447 } 448 449 return sb_bp; 450 } 451 452 453 454 SBBreakpoint 455 SBTarget::BreakpointCreateByAddress (addr_t address) 456 { 457 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 458 459 SBBreakpoint sb_bp; 460 if (m_opaque_sp.get()) 461 { 462 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 463 *sb_bp = m_opaque_sp->CreateBreakpoint (address, false); 464 } 465 466 if (log) 467 { 468 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (%p, address=%p) => SBBreakpoint(%p)", m_opaque_sp.get(), address, sb_bp.get()); 469 } 470 471 return sb_bp; 472 } 473 474 SBBreakpoint 475 SBTarget::FindBreakpointByID (break_id_t bp_id) 476 { 477 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 478 479 SBBreakpoint sb_breakpoint; 480 if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID) 481 { 482 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 483 *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id); 484 } 485 486 if (log) 487 { 488 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)", 489 m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get()); 490 } 491 492 return sb_breakpoint; 493 } 494 495 uint32_t 496 SBTarget::GetNumBreakpoints () const 497 { 498 if (m_opaque_sp) 499 { 500 // The breakpoint list is thread safe, no need to lock 501 return m_opaque_sp->GetBreakpointList().GetSize(); 502 } 503 return 0; 504 } 505 506 SBBreakpoint 507 SBTarget::GetBreakpointAtIndex (uint32_t idx) const 508 { 509 SBBreakpoint sb_breakpoint; 510 if (m_opaque_sp) 511 { 512 // The breakpoint list is thread safe, no need to lock 513 *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx); 514 } 515 return sb_breakpoint; 516 } 517 518 bool 519 SBTarget::BreakpointDelete (break_id_t bp_id) 520 { 521 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 522 523 bool result = false; 524 if (m_opaque_sp) 525 { 526 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 527 result = m_opaque_sp->RemoveBreakpointByID (bp_id); 528 } 529 530 if (log) 531 { 532 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result); 533 } 534 535 return result; 536 } 537 538 bool 539 SBTarget::EnableAllBreakpoints () 540 { 541 if (m_opaque_sp) 542 { 543 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 544 m_opaque_sp->EnableAllBreakpoints (); 545 return true; 546 } 547 return false; 548 } 549 550 bool 551 SBTarget::DisableAllBreakpoints () 552 { 553 if (m_opaque_sp) 554 { 555 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 556 m_opaque_sp->DisableAllBreakpoints (); 557 return true; 558 } 559 return false; 560 } 561 562 bool 563 SBTarget::DeleteAllBreakpoints () 564 { 565 if (m_opaque_sp) 566 { 567 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex()); 568 m_opaque_sp->RemoveAllBreakpoints (); 569 return true; 570 } 571 return false; 572 } 573 574 575 uint32_t 576 SBTarget::GetNumModules () const 577 { 578 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 579 580 uint32_t num = 0; 581 if (m_opaque_sp) 582 { 583 // The module list is thread safe, no need to lock 584 num = m_opaque_sp->GetImages().GetSize(); 585 } 586 587 if (log) 588 log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num); 589 590 return num; 591 } 592 593 void 594 SBTarget::Clear () 595 { 596 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 597 598 if (log) 599 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get()); 600 601 m_opaque_sp.reset(); 602 } 603 604 605 SBModule 606 SBTarget::FindModule (const SBFileSpec &sb_file_spec) 607 { 608 SBModule sb_module; 609 if (m_opaque_sp && sb_file_spec.IsValid()) 610 { 611 // The module list is thread safe, no need to lock 612 sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL)); 613 } 614 return sb_module; 615 } 616 617 SBModule 618 SBTarget::GetModuleAtIndex (uint32_t idx) 619 { 620 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 621 622 SBModule sb_module; 623 if (m_opaque_sp) 624 { 625 // The module list is thread safe, no need to lock 626 sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx)); 627 } 628 629 if (log) 630 { 631 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)", 632 m_opaque_sp.get(), idx, sb_module.get()); 633 } 634 635 return sb_module; 636 } 637 638 639 SBBroadcaster 640 SBTarget::GetBroadcaster () const 641 { 642 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 643 644 SBBroadcaster broadcaster(m_opaque_sp.get(), false); 645 646 if (log) 647 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)", 648 m_opaque_sp.get(), broadcaster.get()); 649 650 return broadcaster; 651 } 652 653 654 bool 655 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) 656 { 657 if (m_opaque_sp) 658 { 659 description.ref(); 660 m_opaque_sp->Dump (description.get(), description_level); 661 } 662 else 663 description.Printf ("No value"); 664 665 return true; 666 } 667 668 bool 669 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const 670 { 671 if (m_opaque_sp) 672 { 673 description.ref(); 674 m_opaque_sp->Dump (description.get(), description_level); 675 } 676 else 677 description.Printf ("No value"); 678 679 return true; 680 } 681