1 //===-- ProcessFreeBSD.cpp ----------------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 // C Includes 12 #include <errno.h> 13 14 // C++ Includes 15 #include <mutex> 16 17 // Other libraries and framework includes 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Core/State.h" 20 #include "lldb/Host/Host.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Target/DynamicLoader.h" 23 #include "lldb/Target/Target.h" 24 25 #include "FreeBSDThread.h" 26 #include "Plugins/Process/Utility/FreeBSDSignals.h" 27 #include "Plugins/Process/Utility/InferiorCallPOSIX.h" 28 #include "ProcessFreeBSD.h" 29 #include "ProcessMonitor.h" 30 #include "ProcessPOSIXLog.h" 31 32 // Other libraries and framework includes 33 #include "lldb/Breakpoint/BreakpointLocation.h" 34 #include "lldb/Breakpoint/Watchpoint.h" 35 #include "lldb/Core/Module.h" 36 #include "lldb/Core/ModuleSpec.h" 37 #include "lldb/Core/PluginManager.h" 38 #include "lldb/Core/State.h" 39 #include "lldb/Host/FileSpec.h" 40 #include "lldb/Host/Host.h" 41 #include "lldb/Symbol/ObjectFile.h" 42 #include "lldb/Target/DynamicLoader.h" 43 #include "lldb/Target/Platform.h" 44 #include "lldb/Target/Target.h" 45 46 #include "lldb/Host/posix/Fcntl.h" 47 48 using namespace lldb; 49 using namespace lldb_private; 50 51 namespace { 52 UnixSignalsSP &GetFreeBSDSignals() { 53 static UnixSignalsSP s_freebsd_signals_sp(new FreeBSDSignals()); 54 return s_freebsd_signals_sp; 55 } 56 } 57 58 //------------------------------------------------------------------------------ 59 // Static functions. 60 61 lldb::ProcessSP 62 ProcessFreeBSD::CreateInstance(lldb::TargetSP target_sp, 63 lldb::ListenerSP listener_sp, 64 const FileSpec *crash_file_path) { 65 lldb::ProcessSP process_sp; 66 if (crash_file_path == NULL) 67 process_sp.reset( 68 new ProcessFreeBSD(target_sp, listener_sp, GetFreeBSDSignals())); 69 return process_sp; 70 } 71 72 void ProcessFreeBSD::Initialize() { 73 static std::once_flag g_once_flag; 74 75 std::call_once(g_once_flag, []() { 76 PluginManager::RegisterPlugin(GetPluginNameStatic(), 77 GetPluginDescriptionStatic(), CreateInstance); 78 ProcessPOSIXLog::Initialize(GetPluginNameStatic()); 79 }); 80 } 81 82 lldb_private::ConstString ProcessFreeBSD::GetPluginNameStatic() { 83 static ConstString g_name("freebsd"); 84 return g_name; 85 } 86 87 const char *ProcessFreeBSD::GetPluginDescriptionStatic() { 88 return "Process plugin for FreeBSD"; 89 } 90 91 //------------------------------------------------------------------------------ 92 // ProcessInterface protocol. 93 94 lldb_private::ConstString ProcessFreeBSD::GetPluginName() { 95 return GetPluginNameStatic(); 96 } 97 98 uint32_t ProcessFreeBSD::GetPluginVersion() { return 1; } 99 100 void ProcessFreeBSD::Terminate() {} 101 102 Error ProcessFreeBSD::DoDetach(bool keep_stopped) { 103 Error error; 104 if (keep_stopped) { 105 error.SetErrorString("Detaching with keep_stopped true is not currently " 106 "supported on FreeBSD."); 107 return error; 108 } 109 110 error = m_monitor->Detach(GetID()); 111 112 if (error.Success()) 113 SetPrivateState(eStateDetached); 114 115 return error; 116 } 117 118 Error ProcessFreeBSD::DoResume() { 119 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 120 121 SetPrivateState(eStateRunning); 122 123 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 124 bool do_step = false; 125 126 for (tid_collection::const_iterator t_pos = m_run_tids.begin(), 127 t_end = m_run_tids.end(); 128 t_pos != t_end; ++t_pos) { 129 m_monitor->ThreadSuspend(*t_pos, false); 130 } 131 for (tid_collection::const_iterator t_pos = m_step_tids.begin(), 132 t_end = m_step_tids.end(); 133 t_pos != t_end; ++t_pos) { 134 m_monitor->ThreadSuspend(*t_pos, false); 135 do_step = true; 136 } 137 for (tid_collection::const_iterator t_pos = m_suspend_tids.begin(), 138 t_end = m_suspend_tids.end(); 139 t_pos != t_end; ++t_pos) { 140 m_monitor->ThreadSuspend(*t_pos, true); 141 // XXX Cannot PT_CONTINUE properly with suspended threads. 142 do_step = true; 143 } 144 145 if (log) 146 log->Printf("process %" PRIu64 " resuming (%s)", GetID(), 147 do_step ? "step" : "continue"); 148 if (do_step) 149 m_monitor->SingleStep(GetID(), m_resume_signo); 150 else 151 m_monitor->Resume(GetID(), m_resume_signo); 152 153 return Error(); 154 } 155 156 bool ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, 157 ThreadList &new_thread_list) { 158 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 159 if (log) 160 log->Printf("ProcessFreeBSD::%s (pid = %" PRIu64 ")", __FUNCTION__, 161 GetID()); 162 163 std::vector<lldb::pid_t> tds; 164 if (!GetMonitor().GetCurrentThreadIDs(tds)) { 165 return false; 166 } 167 168 ThreadList old_thread_list_copy(old_thread_list); 169 for (size_t i = 0; i < tds.size(); ++i) { 170 tid_t tid = tds[i]; 171 ThreadSP thread_sp(old_thread_list_copy.RemoveThreadByID(tid, false)); 172 if (!thread_sp) { 173 thread_sp.reset(new FreeBSDThread(*this, tid)); 174 if (log) 175 log->Printf("ProcessFreeBSD::%s new tid = %" PRIu64, __FUNCTION__, tid); 176 } else { 177 if (log) 178 log->Printf("ProcessFreeBSD::%s existing tid = %" PRIu64, __FUNCTION__, 179 tid); 180 } 181 new_thread_list.AddThread(thread_sp); 182 } 183 for (size_t i = 0; i < old_thread_list_copy.GetSize(false); ++i) { 184 ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false)); 185 if (old_thread_sp) { 186 if (log) 187 log->Printf("ProcessFreeBSD::%s remove tid", __FUNCTION__); 188 } 189 } 190 191 return true; 192 } 193 194 Error ProcessFreeBSD::WillResume() { 195 m_resume_signo = 0; 196 m_suspend_tids.clear(); 197 m_run_tids.clear(); 198 m_step_tids.clear(); 199 return Process::WillResume(); 200 } 201 202 void ProcessFreeBSD::SendMessage(const ProcessMessage &message) { 203 std::lock_guard<std::recursive_mutex> guard(m_message_mutex); 204 205 switch (message.GetKind()) { 206 case ProcessMessage::eInvalidMessage: 207 return; 208 209 case ProcessMessage::eAttachMessage: 210 SetPrivateState(eStateStopped); 211 return; 212 213 case ProcessMessage::eLimboMessage: 214 case ProcessMessage::eExitMessage: 215 SetExitStatus(message.GetExitStatus(), NULL); 216 break; 217 218 case ProcessMessage::eSignalMessage: 219 case ProcessMessage::eSignalDeliveredMessage: 220 case ProcessMessage::eBreakpointMessage: 221 case ProcessMessage::eTraceMessage: 222 case ProcessMessage::eWatchpointMessage: 223 case ProcessMessage::eCrashMessage: 224 SetPrivateState(eStateStopped); 225 break; 226 227 case ProcessMessage::eNewThreadMessage: 228 llvm_unreachable("eNewThreadMessage unexpected on FreeBSD"); 229 break; 230 231 case ProcessMessage::eExecMessage: 232 SetPrivateState(eStateStopped); 233 break; 234 } 235 236 m_message_queue.push(message); 237 } 238 239 //------------------------------------------------------------------------------ 240 // Constructors and destructors. 241 242 ProcessFreeBSD::ProcessFreeBSD(lldb::TargetSP target_sp, 243 lldb::ListenerSP listener_sp, 244 UnixSignalsSP &unix_signals_sp) 245 : Process(target_sp, listener_sp, unix_signals_sp), 246 m_byte_order(endian::InlHostByteOrder()), m_monitor(NULL), m_module(NULL), 247 m_message_mutex(), m_exit_now(false), m_seen_initial_stop(), 248 m_resume_signo(0) { 249 // FIXME: Putting this code in the ctor and saving the byte order in a 250 // member variable is a hack to avoid const qual issues in GetByteOrder. 251 lldb::ModuleSP module = GetTarget().GetExecutableModule(); 252 if (module && module->GetObjectFile()) 253 m_byte_order = module->GetObjectFile()->GetByteOrder(); 254 } 255 256 ProcessFreeBSD::~ProcessFreeBSD() { delete m_monitor; } 257 258 //------------------------------------------------------------------------------ 259 // Process protocol. 260 void ProcessFreeBSD::Finalize() { 261 Process::Finalize(); 262 263 if (m_monitor) 264 m_monitor->StopMonitor(); 265 } 266 267 bool ProcessFreeBSD::CanDebug(lldb::TargetSP target_sp, 268 bool plugin_specified_by_name) { 269 // For now we are just making sure the file exists for a given module 270 ModuleSP exe_module_sp(target_sp->GetExecutableModule()); 271 if (exe_module_sp.get()) 272 return exe_module_sp->GetFileSpec().Exists(); 273 // If there is no executable module, we return true since we might be 274 // preparing to attach. 275 return true; 276 } 277 278 Error ProcessFreeBSD::DoAttachToProcessWithID( 279 lldb::pid_t pid, const ProcessAttachInfo &attach_info) { 280 Error error; 281 assert(m_monitor == NULL); 282 283 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 284 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) 285 log->Printf("ProcessFreeBSD::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID()); 286 287 m_monitor = new ProcessMonitor(this, pid, error); 288 289 if (!error.Success()) 290 return error; 291 292 PlatformSP platform_sp(GetTarget().GetPlatform()); 293 assert(platform_sp.get()); 294 if (!platform_sp) 295 return error; // FIXME: Detatch? 296 297 // Find out what we can about this process 298 ProcessInstanceInfo process_info; 299 platform_sp->GetProcessInfo(pid, process_info); 300 301 // Resolve the executable module 302 ModuleSP exe_module_sp; 303 FileSpecList executable_search_paths( 304 Target::GetDefaultExecutableSearchPaths()); 305 ModuleSpec exe_module_spec(process_info.GetExecutableFile(), 306 GetTarget().GetArchitecture()); 307 error = platform_sp->ResolveExecutable( 308 exe_module_spec, exe_module_sp, 309 executable_search_paths.GetSize() ? &executable_search_paths : NULL); 310 if (!error.Success()) 311 return error; 312 313 // Fix the target architecture if necessary 314 const ArchSpec &module_arch = exe_module_sp->GetArchitecture(); 315 if (module_arch.IsValid() && 316 !GetTarget().GetArchitecture().IsExactMatch(module_arch)) 317 GetTarget().SetArchitecture(module_arch); 318 319 // Initialize the target module list 320 GetTarget().SetExecutableModule(exe_module_sp, true); 321 322 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); 323 324 SetID(pid); 325 326 return error; 327 } 328 329 Error ProcessFreeBSD::WillLaunch(Module *module) { 330 Error error; 331 return error; 332 } 333 334 FileSpec 335 ProcessFreeBSD::GetFileSpec(const lldb_private::FileAction *file_action, 336 const FileSpec &default_file_spec, 337 const FileSpec &dbg_pts_file_spec) { 338 FileSpec file_spec{}; 339 340 if (file_action && file_action->GetAction() == FileAction::eFileActionOpen) { 341 file_spec = file_action->GetFileSpec(); 342 // By default the stdio paths passed in will be pseudo-terminal 343 // (/dev/pts). If so, convert to using a different default path 344 // instead to redirect I/O to the debugger console. This should 345 // also handle user overrides to /dev/null or a different file. 346 if (!file_spec || file_spec == dbg_pts_file_spec) 347 file_spec = default_file_spec; 348 } 349 return file_spec; 350 } 351 352 Error ProcessFreeBSD::DoLaunch(Module *module, ProcessLaunchInfo &launch_info) { 353 Error error; 354 assert(m_monitor == NULL); 355 356 FileSpec working_dir = launch_info.GetWorkingDirectory(); 357 if (working_dir && 358 (!working_dir.ResolvePath() || 359 working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) { 360 error.SetErrorStringWithFormat("No such file or directory: %s", 361 working_dir.GetCString()); 362 return error; 363 } 364 365 SetPrivateState(eStateLaunching); 366 367 const lldb_private::FileAction *file_action; 368 369 // Default of empty will mean to use existing open file descriptors 370 FileSpec stdin_file_spec{}; 371 FileSpec stdout_file_spec{}; 372 FileSpec stderr_file_spec{}; 373 374 const FileSpec dbg_pts_file_spec{launch_info.GetPTY().GetSlaveName(NULL, 0), 375 false}; 376 377 file_action = launch_info.GetFileActionForFD(STDIN_FILENO); 378 stdin_file_spec = 379 GetFileSpec(file_action, stdin_file_spec, dbg_pts_file_spec); 380 381 file_action = launch_info.GetFileActionForFD(STDOUT_FILENO); 382 stdout_file_spec = 383 GetFileSpec(file_action, stdout_file_spec, dbg_pts_file_spec); 384 385 file_action = launch_info.GetFileActionForFD(STDERR_FILENO); 386 stderr_file_spec = 387 GetFileSpec(file_action, stderr_file_spec, dbg_pts_file_spec); 388 389 m_monitor = new ProcessMonitor( 390 this, module, launch_info.GetArguments().GetConstArgumentVector(), 391 launch_info.GetEnvironmentEntries().GetConstArgumentVector(), 392 stdin_file_spec, stdout_file_spec, stderr_file_spec, working_dir, 393 launch_info, error); 394 395 m_module = module; 396 397 if (!error.Success()) 398 return error; 399 400 int terminal = m_monitor->GetTerminalFD(); 401 if (terminal >= 0) { 402 // The reader thread will close the file descriptor when done, so we pass it a 403 // copy. 404 #ifdef F_DUPFD_CLOEXEC 405 int stdio = fcntl(terminal, F_DUPFD_CLOEXEC, 0); 406 if (stdio == -1) { 407 error.SetErrorToErrno(); 408 return error; 409 } 410 #else 411 // Special case when F_DUPFD_CLOEXEC does not exist (Debian kFreeBSD) 412 int stdio = fcntl(terminal, F_DUPFD, 0); 413 if (stdio == -1) { 414 error.SetErrorToErrno(); 415 return error; 416 } 417 stdio = fcntl(terminal, F_SETFD, FD_CLOEXEC); 418 if (stdio == -1) { 419 error.SetErrorToErrno(); 420 return error; 421 } 422 #endif 423 SetSTDIOFileDescriptor(stdio); 424 } 425 426 SetID(m_monitor->GetPID()); 427 return error; 428 } 429 430 void ProcessFreeBSD::DidLaunch() {} 431 432 addr_t ProcessFreeBSD::GetImageInfoAddress() { 433 Target *target = &GetTarget(); 434 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); 435 Address addr = obj_file->GetImageInfoAddress(target); 436 437 if (addr.IsValid()) 438 return addr.GetLoadAddress(target); 439 return LLDB_INVALID_ADDRESS; 440 } 441 442 Error ProcessFreeBSD::DoHalt(bool &caused_stop) { 443 Error error; 444 445 if (IsStopped()) { 446 caused_stop = false; 447 } else if (kill(GetID(), SIGSTOP)) { 448 caused_stop = false; 449 error.SetErrorToErrno(); 450 } else { 451 caused_stop = true; 452 } 453 return error; 454 } 455 456 Error ProcessFreeBSD::DoSignal(int signal) { 457 Error error; 458 459 if (kill(GetID(), signal)) 460 error.SetErrorToErrno(); 461 462 return error; 463 } 464 465 Error ProcessFreeBSD::DoDestroy() { 466 Error error; 467 468 if (!HasExited()) { 469 assert(m_monitor); 470 m_exit_now = true; 471 if (GetID() == LLDB_INVALID_PROCESS_ID) { 472 error.SetErrorString("invalid process id"); 473 return error; 474 } 475 if (!m_monitor->Kill()) { 476 error.SetErrorToErrno(); 477 return error; 478 } 479 480 SetPrivateState(eStateExited); 481 } 482 483 return error; 484 } 485 486 void ProcessFreeBSD::DoDidExec() { 487 Target *target = &GetTarget(); 488 if (target) { 489 PlatformSP platform_sp(target->GetPlatform()); 490 assert(platform_sp.get()); 491 if (platform_sp) { 492 ProcessInstanceInfo process_info; 493 platform_sp->GetProcessInfo(GetID(), process_info); 494 ModuleSP exe_module_sp; 495 ModuleSpec exe_module_spec(process_info.GetExecutableFile(), 496 target->GetArchitecture()); 497 FileSpecList executable_search_paths( 498 Target::GetDefaultExecutableSearchPaths()); 499 Error error = platform_sp->ResolveExecutable( 500 exe_module_spec, exe_module_sp, 501 executable_search_paths.GetSize() ? &executable_search_paths : NULL); 502 if (!error.Success()) 503 return; 504 target->SetExecutableModule(exe_module_sp, true); 505 } 506 } 507 } 508 509 bool ProcessFreeBSD::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) { 510 bool added_to_set = false; 511 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid); 512 if (it == m_seen_initial_stop.end()) { 513 m_seen_initial_stop.insert(stop_tid); 514 added_to_set = true; 515 } 516 return added_to_set; 517 } 518 519 bool ProcessFreeBSD::WaitingForInitialStop(lldb::tid_t stop_tid) { 520 return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end()); 521 } 522 523 FreeBSDThread * 524 ProcessFreeBSD::CreateNewFreeBSDThread(lldb_private::Process &process, 525 lldb::tid_t tid) { 526 return new FreeBSDThread(process, tid); 527 } 528 529 void ProcessFreeBSD::RefreshStateAfterStop() { 530 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 531 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) 532 log->Printf("ProcessFreeBSD::%s(), message_queue size = %d", __FUNCTION__, 533 (int)m_message_queue.size()); 534 535 std::lock_guard<std::recursive_mutex> guard(m_message_mutex); 536 537 // This method used to only handle one message. Changing it to loop allows 538 // it to handle the case where we hit a breakpoint while handling a different 539 // breakpoint. 540 while (!m_message_queue.empty()) { 541 ProcessMessage &message = m_message_queue.front(); 542 543 // Resolve the thread this message corresponds to and pass it along. 544 lldb::tid_t tid = message.GetTID(); 545 if (log) 546 log->Printf( 547 "ProcessFreeBSD::%s(), message_queue size = %d, pid = %" PRIi64, 548 __FUNCTION__, (int)m_message_queue.size(), tid); 549 550 m_thread_list.RefreshStateAfterStop(); 551 552 FreeBSDThread *thread = static_cast<FreeBSDThread *>( 553 GetThreadList().FindThreadByID(tid, false).get()); 554 if (thread) 555 thread->Notify(message); 556 557 if (message.GetKind() == ProcessMessage::eExitMessage) { 558 // FIXME: We should tell the user about this, but the limbo message is 559 // probably better for that. 560 if (log) 561 log->Printf("ProcessFreeBSD::%s() removing thread, tid = %" PRIi64, 562 __FUNCTION__, tid); 563 564 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 565 566 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false); 567 thread_sp.reset(); 568 m_seen_initial_stop.erase(tid); 569 } 570 571 m_message_queue.pop(); 572 } 573 } 574 575 bool ProcessFreeBSD::IsAlive() { 576 StateType state = GetPrivateState(); 577 return state != eStateDetached && state != eStateExited && 578 state != eStateInvalid && state != eStateUnloaded; 579 } 580 581 size_t ProcessFreeBSD::DoReadMemory(addr_t vm_addr, void *buf, size_t size, 582 Error &error) { 583 assert(m_monitor); 584 return m_monitor->ReadMemory(vm_addr, buf, size, error); 585 } 586 587 size_t ProcessFreeBSD::DoWriteMemory(addr_t vm_addr, const void *buf, 588 size_t size, Error &error) { 589 assert(m_monitor); 590 return m_monitor->WriteMemory(vm_addr, buf, size, error); 591 } 592 593 addr_t ProcessFreeBSD::DoAllocateMemory(size_t size, uint32_t permissions, 594 Error &error) { 595 addr_t allocated_addr = LLDB_INVALID_ADDRESS; 596 597 unsigned prot = 0; 598 if (permissions & lldb::ePermissionsReadable) 599 prot |= eMmapProtRead; 600 if (permissions & lldb::ePermissionsWritable) 601 prot |= eMmapProtWrite; 602 if (permissions & lldb::ePermissionsExecutable) 603 prot |= eMmapProtExec; 604 605 if (InferiorCallMmap(this, allocated_addr, 0, size, prot, 606 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 607 m_addr_to_mmap_size[allocated_addr] = size; 608 error.Clear(); 609 } else { 610 allocated_addr = LLDB_INVALID_ADDRESS; 611 error.SetErrorStringWithFormat( 612 "unable to allocate %zu bytes of memory with permissions %s", size, 613 GetPermissionsAsCString(permissions)); 614 } 615 616 return allocated_addr; 617 } 618 619 Error ProcessFreeBSD::DoDeallocateMemory(lldb::addr_t addr) { 620 Error error; 621 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); 622 if (pos != m_addr_to_mmap_size.end() && 623 InferiorCallMunmap(this, addr, pos->second)) 624 m_addr_to_mmap_size.erase(pos); 625 else 626 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, 627 addr); 628 629 return error; 630 } 631 632 size_t 633 ProcessFreeBSD::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) { 634 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xD4}; 635 static const uint8_t g_i386_opcode[] = {0xCC}; 636 637 ArchSpec arch = GetTarget().GetArchitecture(); 638 const uint8_t *opcode = NULL; 639 size_t opcode_size = 0; 640 641 switch (arch.GetMachine()) { 642 default: 643 assert(false && "CPU type not supported!"); 644 break; 645 646 case llvm::Triple::arm: { 647 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe 648 // but the linux kernel does otherwise. 649 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 650 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 651 652 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0)); 653 AddressClass addr_class = eAddressClassUnknown; 654 655 if (bp_loc_sp) 656 addr_class = bp_loc_sp->GetAddress().GetAddressClass(); 657 658 if (addr_class == eAddressClassCodeAlternateISA || 659 (addr_class == eAddressClassUnknown && 660 bp_loc_sp->GetAddress().GetOffset() & 1)) { 661 opcode = g_thumb_breakpoint_opcode; 662 opcode_size = sizeof(g_thumb_breakpoint_opcode); 663 } else { 664 opcode = g_arm_breakpoint_opcode; 665 opcode_size = sizeof(g_arm_breakpoint_opcode); 666 } 667 } break; 668 case llvm::Triple::aarch64: 669 opcode = g_aarch64_opcode; 670 opcode_size = sizeof(g_aarch64_opcode); 671 break; 672 673 case llvm::Triple::x86: 674 case llvm::Triple::x86_64: 675 opcode = g_i386_opcode; 676 opcode_size = sizeof(g_i386_opcode); 677 break; 678 } 679 680 bp_site->SetTrapOpcode(opcode, opcode_size); 681 return opcode_size; 682 } 683 684 Error ProcessFreeBSD::EnableBreakpointSite(BreakpointSite *bp_site) { 685 return EnableSoftwareBreakpoint(bp_site); 686 } 687 688 Error ProcessFreeBSD::DisableBreakpointSite(BreakpointSite *bp_site) { 689 return DisableSoftwareBreakpoint(bp_site); 690 } 691 692 Error ProcessFreeBSD::EnableWatchpoint(Watchpoint *wp, bool notify) { 693 Error error; 694 if (wp) { 695 user_id_t watchID = wp->GetID(); 696 addr_t addr = wp->GetLoadAddress(); 697 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 698 if (log) 699 log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 ")", 700 watchID); 701 if (wp->IsEnabled()) { 702 if (log) 703 log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 704 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", 705 watchID, (uint64_t)addr); 706 return error; 707 } 708 709 // Try to find a vacant watchpoint slot in the inferiors' main thread 710 uint32_t wp_hw_index = LLDB_INVALID_INDEX32; 711 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 712 FreeBSDThread *thread = static_cast<FreeBSDThread *>( 713 m_thread_list.GetThreadAtIndex(0, false).get()); 714 715 if (thread) 716 wp_hw_index = thread->FindVacantWatchpointIndex(); 717 718 if (wp_hw_index == LLDB_INVALID_INDEX32) { 719 error.SetErrorString("Setting hardware watchpoint failed."); 720 } else { 721 wp->SetHardwareIndex(wp_hw_index); 722 bool wp_enabled = true; 723 uint32_t thread_count = m_thread_list.GetSize(false); 724 for (uint32_t i = 0; i < thread_count; ++i) { 725 thread = static_cast<FreeBSDThread *>( 726 m_thread_list.GetThreadAtIndex(i, false).get()); 727 if (thread) 728 wp_enabled &= thread->EnableHardwareWatchpoint(wp); 729 else 730 wp_enabled = false; 731 } 732 if (wp_enabled) { 733 wp->SetEnabled(true, notify); 734 return error; 735 } else { 736 // Watchpoint enabling failed on at least one 737 // of the threads so roll back all of them 738 DisableWatchpoint(wp, false); 739 error.SetErrorString("Setting hardware watchpoint failed"); 740 } 741 } 742 } else 743 error.SetErrorString("Watchpoint argument was NULL."); 744 return error; 745 } 746 747 Error ProcessFreeBSD::DisableWatchpoint(Watchpoint *wp, bool notify) { 748 Error error; 749 if (wp) { 750 user_id_t watchID = wp->GetID(); 751 addr_t addr = wp->GetLoadAddress(); 752 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); 753 if (log) 754 log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 ")", 755 watchID); 756 if (!wp->IsEnabled()) { 757 if (log) 758 log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 759 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.", 760 watchID, (uint64_t)addr); 761 // This is needed (for now) to keep watchpoints disabled correctly 762 wp->SetEnabled(false, notify); 763 return error; 764 } 765 766 if (wp->IsHardware()) { 767 bool wp_disabled = true; 768 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 769 uint32_t thread_count = m_thread_list.GetSize(false); 770 for (uint32_t i = 0; i < thread_count; ++i) { 771 FreeBSDThread *thread = static_cast<FreeBSDThread *>( 772 m_thread_list.GetThreadAtIndex(i, false).get()); 773 if (thread) 774 wp_disabled &= thread->DisableHardwareWatchpoint(wp); 775 else 776 wp_disabled = false; 777 } 778 if (wp_disabled) { 779 wp->SetHardwareIndex(LLDB_INVALID_INDEX32); 780 wp->SetEnabled(false, notify); 781 return error; 782 } else 783 error.SetErrorString("Disabling hardware watchpoint failed"); 784 } 785 } else 786 error.SetErrorString("Watchpoint argument was NULL."); 787 return error; 788 } 789 790 Error ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num) { 791 Error error; 792 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 793 FreeBSDThread *thread = static_cast<FreeBSDThread *>( 794 m_thread_list.GetThreadAtIndex(0, false).get()); 795 if (thread) 796 num = thread->NumSupportedHardwareWatchpoints(); 797 else 798 error.SetErrorString("Process does not exist."); 799 return error; 800 } 801 802 Error ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num, bool &after) { 803 Error error = GetWatchpointSupportInfo(num); 804 // Watchpoints trigger and halt the inferior after 805 // the corresponding instruction has been executed. 806 after = true; 807 return error; 808 } 809 810 uint32_t ProcessFreeBSD::UpdateThreadListIfNeeded() { 811 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 812 // Do not allow recursive updates. 813 return m_thread_list.GetSize(false); 814 } 815 816 #if 0 817 bool 818 ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) 819 { 820 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); 821 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) 822 log->Printf ("ProcessFreeBSD::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID()); 823 824 bool has_updated = false; 825 // Update the process thread list with this new thread. 826 // FIXME: We should be using tid, not pid. 827 assert(m_monitor); 828 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); 829 if (!thread_sp) { 830 thread_sp.reset(CreateNewFreeBSDThread(*this, GetID())); 831 has_updated = true; 832 } 833 834 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) 835 log->Printf ("ProcessFreeBSD::%s() updated pid = %" PRIi64, __FUNCTION__, GetID()); 836 new_thread_list.AddThread(thread_sp); 837 838 return has_updated; // the list has been updated 839 } 840 #endif 841 842 ByteOrder ProcessFreeBSD::GetByteOrder() const { 843 // FIXME: We should be able to extract this value directly. See comment in 844 // ProcessFreeBSD(). 845 return m_byte_order; 846 } 847 848 size_t ProcessFreeBSD::PutSTDIN(const char *buf, size_t len, Error &error) { 849 ssize_t status; 850 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) { 851 error.SetErrorToErrno(); 852 return 0; 853 } 854 return status; 855 } 856 857 //------------------------------------------------------------------------------ 858 // Utility functions. 859 860 bool ProcessFreeBSD::HasExited() { 861 switch (GetPrivateState()) { 862 default: 863 break; 864 865 case eStateDetached: 866 case eStateExited: 867 return true; 868 } 869 870 return false; 871 } 872 873 bool ProcessFreeBSD::IsStopped() { 874 switch (GetPrivateState()) { 875 default: 876 break; 877 878 case eStateStopped: 879 case eStateCrashed: 880 case eStateSuspended: 881 return true; 882 } 883 884 return false; 885 } 886 887 bool ProcessFreeBSD::IsAThreadRunning() { 888 bool is_running = false; 889 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); 890 uint32_t thread_count = m_thread_list.GetSize(false); 891 for (uint32_t i = 0; i < thread_count; ++i) { 892 FreeBSDThread *thread = static_cast<FreeBSDThread *>( 893 m_thread_list.GetThreadAtIndex(i, false).get()); 894 StateType thread_state = thread->GetState(); 895 if (thread_state == eStateRunning || thread_state == eStateStepping) { 896 is_running = true; 897 break; 898 } 899 } 900 return is_running; 901 } 902 903 const DataBufferSP ProcessFreeBSD::GetAuxvData() { 904 // If we're the local platform, we can ask the host for auxv data. 905 PlatformSP platform_sp = GetTarget().GetPlatform(); 906 if (platform_sp && platform_sp->IsHost()) 907 return lldb_private::Host::GetAuxvData(this); 908 909 // Somewhat unexpected - the process is not running locally or we don't have a 910 // platform. 911 assert( 912 false && 913 "no platform or not the host - how did we get here with ProcessFreeBSD?"); 914 return DataBufferSP(); 915 } 916