1 //===-- PlatformFreeBSD.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/lldb-python.h" 11 12 #include "PlatformFreeBSD.h" 13 14 // C Includes 15 #include <stdio.h> 16 #include <sys/utsname.h> 17 18 // C++ Includes 19 // Other libraries and framework includes 20 // Project includes 21 #include "lldb/Core/Error.h" 22 #include "lldb/Core/Debugger.h" 23 #include "lldb/Core/Module.h" 24 #include "lldb/Core/ModuleSpec.h" 25 #include "lldb/Core/PluginManager.h" 26 #include "lldb/Host/Host.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 Platform * 32 PlatformFreeBSD::CreateInstance (bool force, const lldb_private::ArchSpec *arch) 33 { 34 // The only time we create an instance is when we are creating a remote 35 // freebsd platform 36 const bool is_host = false; 37 38 bool create = force; 39 if (create == false && arch && arch->IsValid()) 40 { 41 const llvm::Triple &triple = arch->GetTriple(); 42 switch (triple.getVendor()) 43 { 44 case llvm::Triple::PC: 45 create = true; 46 break; 47 48 #if defined(__FreeBSD__) || defined(__OpenBSD__) 49 // Only accept "unknown" for the vendor if the host is BSD and 50 // it "unknown" wasn't specified (it was just returned becasue it 51 // was NOT specified) 52 case llvm::Triple::UnknownArch: 53 create = !arch->TripleVendorWasSpecified(); 54 break; 55 #endif 56 default: 57 break; 58 } 59 60 if (create) 61 { 62 switch (triple.getOS()) 63 { 64 case llvm::Triple::FreeBSD: 65 case llvm::Triple::KFreeBSD: 66 break; 67 68 #if defined(__FreeBSD__) || defined(__OpenBSD__) 69 // Only accept "unknown" for the OS if the host is BSD and 70 // it "unknown" wasn't specified (it was just returned becasue it 71 // was NOT specified) 72 case llvm::Triple::UnknownOS: 73 create = arch->TripleOSWasSpecified(); 74 break; 75 #endif 76 default: 77 create = false; 78 break; 79 } 80 } 81 } 82 if (create) 83 return new PlatformFreeBSD (is_host); 84 return NULL; 85 86 } 87 88 const char * 89 PlatformFreeBSD::GetPluginNameStatic() 90 { 91 return "plugin.platform.freebsd"; 92 } 93 94 const char * 95 PlatformFreeBSD::GetShortPluginNameStatic (bool is_host) 96 { 97 if (is_host) 98 return Platform::GetHostPlatformName (); 99 else 100 return "remote-freebsd"; 101 } 102 103 const char * 104 PlatformFreeBSD::GetDescriptionStatic (bool is_host) 105 { 106 if (is_host) 107 return "Local FreeBSD user platform plug-in."; 108 else 109 return "Remote FreeBSD user platform plug-in."; 110 } 111 112 static uint32_t g_initialize_count = 0; 113 114 void 115 PlatformFreeBSD::Initialize () 116 { 117 if (g_initialize_count++ == 0) 118 { 119 #if defined (__FreeBSD__) 120 // Force a host flag to true for the default platform object. 121 PlatformSP default_platform_sp (new PlatformFreeBSD(true)); 122 default_platform_sp->SetSystemArchitecture (Host::GetArchitecture()); 123 Platform::SetDefaultPlatform (default_platform_sp); 124 #endif 125 PluginManager::RegisterPlugin(PlatformFreeBSD::GetShortPluginNameStatic(false), 126 PlatformFreeBSD::GetDescriptionStatic(false), 127 PlatformFreeBSD::CreateInstance); 128 } 129 } 130 131 void 132 PlatformFreeBSD::Terminate () 133 { 134 if (g_initialize_count > 0 && --g_initialize_count == 0) 135 PluginManager::UnregisterPlugin (PlatformFreeBSD::CreateInstance); 136 } 137 138 //------------------------------------------------------------------ 139 /// Default Constructor 140 //------------------------------------------------------------------ 141 PlatformFreeBSD::PlatformFreeBSD (bool is_host) : 142 Platform(is_host) 143 { 144 } 145 146 //------------------------------------------------------------------ 147 /// Destructor. 148 /// 149 /// The destructor is virtual since this class is designed to be 150 /// inherited from by the plug-in instance. 151 //------------------------------------------------------------------ 152 PlatformFreeBSD::~PlatformFreeBSD() 153 { 154 } 155 156 157 Error 158 PlatformFreeBSD::ResolveExecutable (const FileSpec &exe_file, 159 const ArchSpec &exe_arch, 160 lldb::ModuleSP &exe_module_sp, 161 const FileSpecList *module_search_paths_ptr) 162 { 163 Error error; 164 // Nothing special to do here, just use the actual file and architecture 165 166 char exe_path[PATH_MAX]; 167 FileSpec resolved_exe_file (exe_file); 168 169 if (IsHost()) 170 { 171 // If we have "ls" as the exe_file, resolve the executable loation based on 172 // the current path variables 173 if (!resolved_exe_file.Exists()) 174 { 175 exe_file.GetPath(exe_path, sizeof(exe_path)); 176 resolved_exe_file.SetFile(exe_path, true); 177 } 178 179 if (!resolved_exe_file.Exists()) 180 resolved_exe_file.ResolveExecutableLocation (); 181 182 // Resolve any executable within a bundle on MacOSX 183 //Host::ResolveExecutableInBundle (resolved_exe_file); 184 185 if (resolved_exe_file.Exists()) 186 error.Clear(); 187 else 188 { 189 exe_file.GetPath(exe_path, sizeof(exe_path)); 190 error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 191 } 192 } 193 else 194 { 195 if (m_remote_platform_sp) 196 { 197 error = m_remote_platform_sp->ResolveExecutable (exe_file, 198 exe_arch, 199 exe_module_sp, 200 module_search_paths_ptr); 201 } 202 else 203 { 204 // We may connect to a process and use the provided executable (Don't use local $PATH). 205 206 // Resolve any executable within a bundle on MacOSX 207 Host::ResolveExecutableInBundle (resolved_exe_file); 208 209 if (resolved_exe_file.Exists()) { 210 error.Clear(); 211 } 212 else 213 { 214 exe_file.GetPath(exe_path, sizeof(exe_path)); 215 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 216 } 217 } 218 } 219 220 221 if (error.Success()) 222 { 223 ModuleSpec module_spec (resolved_exe_file, exe_arch); 224 if (module_spec.GetArchitecture().IsValid()) 225 { 226 error = ModuleList::GetSharedModule (module_spec, 227 exe_module_sp, 228 module_search_paths_ptr, 229 NULL, 230 NULL); 231 232 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 233 { 234 exe_module_sp.reset(); 235 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 236 exe_file.GetPath().c_str(), 237 exe_arch.GetArchitectureName()); 238 } 239 } 240 else 241 { 242 // No valid architecture was specified, ask the platform for 243 // the architectures that we should be using (in the correct order) 244 // and see if we can find a match that way 245 StreamString arch_names; 246 ArchSpec platform_arch; 247 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, platform_arch); ++idx) 248 { 249 error = ModuleList::GetSharedModule (module_spec, 250 exe_module_sp, 251 module_search_paths_ptr, 252 NULL, 253 NULL); 254 // Did we find an executable using one of the 255 if (error.Success()) 256 { 257 if (exe_module_sp && exe_module_sp->GetObjectFile()) 258 break; 259 else 260 error.SetErrorToGenericError(); 261 } 262 263 if (idx > 0) 264 arch_names.PutCString (", "); 265 arch_names.PutCString (platform_arch.GetArchitectureName()); 266 } 267 268 if (error.Fail() || !exe_module_sp) 269 { 270 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 271 exe_file.GetPath().c_str(), 272 GetShortPluginName(), 273 arch_names.GetString().c_str()); 274 } 275 } 276 } 277 else 278 { 279 error.SetErrorStringWithFormat ("'%s' does not exist", 280 exe_file.GetPath().c_str()); 281 } 282 283 return error; 284 } 285 286 size_t 287 PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site) 288 { 289 ArchSpec arch = target.GetArchitecture(); 290 const uint8_t *trap_opcode = NULL; 291 size_t trap_opcode_size = 0; 292 293 switch (arch.GetCore()) 294 { 295 default: 296 assert(false && "Unhandled architecture in PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode()"); 297 break; 298 299 case ArchSpec::eCore_x86_32_i386: 300 case ArchSpec::eCore_x86_64_x86_64: 301 { 302 static const uint8_t g_i386_opcode[] = { 0xCC }; 303 trap_opcode = g_i386_opcode; 304 trap_opcode_size = sizeof(g_i386_opcode); 305 } 306 break; 307 } 308 309 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 310 return trap_opcode_size; 311 312 return 0; 313 } 314 315 bool 316 PlatformFreeBSD::GetRemoteOSVersion () 317 { 318 if (m_remote_platform_sp) 319 return m_remote_platform_sp->GetOSVersion (m_major_os_version, 320 m_minor_os_version, 321 m_update_os_version); 322 return false; 323 } 324 325 bool 326 PlatformFreeBSD::GetRemoteOSBuildString (std::string &s) 327 { 328 if (m_remote_platform_sp) 329 return m_remote_platform_sp->GetRemoteOSBuildString (s); 330 s.clear(); 331 return false; 332 } 333 334 bool 335 PlatformFreeBSD::GetRemoteOSKernelDescription (std::string &s) 336 { 337 if (m_remote_platform_sp) 338 return m_remote_platform_sp->GetRemoteOSKernelDescription (s); 339 s.clear(); 340 return false; 341 } 342 343 // Remote Platform subclasses need to override this function 344 ArchSpec 345 PlatformFreeBSD::GetRemoteSystemArchitecture () 346 { 347 if (m_remote_platform_sp) 348 return m_remote_platform_sp->GetRemoteSystemArchitecture (); 349 return ArchSpec(); 350 } 351 352 353 const char * 354 PlatformFreeBSD::GetHostname () 355 { 356 if (IsHost()) 357 return Platform::GetHostname(); 358 359 if (m_remote_platform_sp) 360 return m_remote_platform_sp->GetHostname (); 361 return NULL; 362 } 363 364 bool 365 PlatformFreeBSD::IsConnected () const 366 { 367 if (IsHost()) 368 return true; 369 else if (m_remote_platform_sp) 370 return m_remote_platform_sp->IsConnected(); 371 return false; 372 } 373 374 Error 375 PlatformFreeBSD::ConnectRemote (Args& args) 376 { 377 Error error; 378 if (IsHost()) 379 { 380 error.SetErrorStringWithFormat ("can't connect to the host platform '%s', always connected", GetShortPluginName()); 381 } 382 else 383 { 384 if (!m_remote_platform_sp) 385 m_remote_platform_sp = Platform::Create ("remote-gdb-server", error); 386 387 if (m_remote_platform_sp) 388 { 389 if (error.Success()) 390 { 391 if (m_remote_platform_sp) 392 { 393 error = m_remote_platform_sp->ConnectRemote (args); 394 } 395 else 396 { 397 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>"); 398 } 399 } 400 } 401 else 402 error.SetErrorString ("failed to create a 'remote-gdb-server' platform"); 403 404 if (error.Fail()) 405 m_remote_platform_sp.reset(); 406 } 407 408 return error; 409 } 410 411 Error 412 PlatformFreeBSD::DisconnectRemote () 413 { 414 Error error; 415 416 if (IsHost()) 417 { 418 error.SetErrorStringWithFormat ("can't disconnect from the host platform '%s', always connected", GetShortPluginName()); 419 } 420 else 421 { 422 if (m_remote_platform_sp) 423 error = m_remote_platform_sp->DisconnectRemote (); 424 else 425 error.SetErrorString ("the platform is not currently connected"); 426 } 427 return error; 428 } 429 430 bool 431 PlatformFreeBSD::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 432 { 433 bool success = false; 434 if (IsHost()) 435 { 436 success = Platform::GetProcessInfo (pid, process_info); 437 } 438 else if (m_remote_platform_sp) 439 { 440 success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 441 } 442 return success; 443 } 444 445 446 447 uint32_t 448 PlatformFreeBSD::FindProcesses (const ProcessInstanceInfoMatch &match_info, 449 ProcessInstanceInfoList &process_infos) 450 { 451 uint32_t match_count = 0; 452 if (IsHost()) 453 { 454 // Let the base class figure out the host details 455 match_count = Platform::FindProcesses (match_info, process_infos); 456 } 457 else 458 { 459 // If we are remote, we can only return results if we are connected 460 if (m_remote_platform_sp) 461 match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos); 462 } 463 return match_count; 464 } 465 466 Error 467 PlatformFreeBSD::LaunchProcess (ProcessLaunchInfo &launch_info) 468 { 469 Error error; 470 if (IsHost()) 471 { 472 error = Platform::LaunchProcess (launch_info); 473 } 474 else 475 { 476 if (m_remote_platform_sp) 477 error = m_remote_platform_sp->LaunchProcess (launch_info); 478 else 479 error.SetErrorString ("the platform is not currently connected"); 480 } 481 return error; 482 } 483 484 lldb::ProcessSP 485 PlatformFreeBSD::Attach(ProcessAttachInfo &attach_info, 486 Debugger &debugger, 487 Target *target, 488 Listener &listener, 489 Error &error) 490 { 491 lldb::ProcessSP process_sp; 492 if (IsHost()) 493 { 494 if (target == NULL) 495 { 496 TargetSP new_target_sp; 497 ArchSpec emptyArchSpec; 498 499 error = debugger.GetTargetList().CreateTarget (debugger, 500 NULL, 501 emptyArchSpec, 502 false, 503 m_remote_platform_sp, 504 new_target_sp); 505 target = new_target_sp.get(); 506 } 507 else 508 error.Clear(); 509 510 if (target && error.Success()) 511 { 512 debugger.GetTargetList().SetSelectedTarget(target); 513 // The freebsd always currently uses the GDB remote debugger plug-in 514 // so even when debugging locally we are debugging remotely! 515 // Just like the darwin plugin. 516 process_sp = target->CreateProcess (listener, "gdb-remote", NULL); 517 518 if (process_sp) 519 error = process_sp->Attach (attach_info); 520 } 521 } 522 else 523 { 524 if (m_remote_platform_sp) 525 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 526 else 527 error.SetErrorString ("the platform is not currently connected"); 528 } 529 return process_sp; 530 } 531 532 const char * 533 PlatformFreeBSD::GetUserName (uint32_t uid) 534 { 535 // Check the cache in Platform in case we have already looked this uid up 536 const char *user_name = Platform::GetUserName(uid); 537 if (user_name) 538 return user_name; 539 540 if (IsRemote() && m_remote_platform_sp) 541 return m_remote_platform_sp->GetUserName(uid); 542 return NULL; 543 } 544 545 const char * 546 PlatformFreeBSD::GetGroupName (uint32_t gid) 547 { 548 const char *group_name = Platform::GetGroupName(gid); 549 if (group_name) 550 return group_name; 551 552 if (IsRemote() && m_remote_platform_sp) 553 return m_remote_platform_sp->GetGroupName(gid); 554 return NULL; 555 } 556 557 558 // From PlatformMacOSX only 559 Error 560 PlatformFreeBSD::GetFile (const FileSpec &platform_file, 561 const UUID *uuid_ptr, 562 FileSpec &local_file) 563 { 564 if (IsRemote()) 565 { 566 if (m_remote_platform_sp) 567 return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file); 568 } 569 570 // Default to the local case 571 local_file = platform_file; 572 return Error(); 573 } 574 575 Error 576 PlatformFreeBSD::GetSharedModule (const ModuleSpec &module_spec, 577 ModuleSP &module_sp, 578 const FileSpecList *module_search_paths_ptr, 579 ModuleSP *old_module_sp_ptr, 580 bool *did_create_ptr) 581 { 582 Error error; 583 module_sp.reset(); 584 585 if (IsRemote()) 586 { 587 // If we have a remote platform always, let it try and locate 588 // the shared module first. 589 if (m_remote_platform_sp) 590 { 591 error = m_remote_platform_sp->GetSharedModule (module_spec, 592 module_sp, 593 module_search_paths_ptr, 594 old_module_sp_ptr, 595 did_create_ptr); 596 } 597 } 598 599 if (!module_sp) 600 { 601 // Fall back to the local platform and find the file locally 602 error = Platform::GetSharedModule (module_spec, 603 module_sp, 604 module_search_paths_ptr, 605 old_module_sp_ptr, 606 did_create_ptr); 607 } 608 if (module_sp) 609 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 610 return error; 611 } 612 613 614 bool 615 PlatformFreeBSD::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 616 { 617 // From macosx;s plugin code. For FreeBSD we may want to support more archs. 618 if (idx == 0) 619 { 620 arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture); 621 return arch.IsValid(); 622 } 623 else if (idx == 1) 624 { 625 ArchSpec platform_arch (Host::GetArchitecture (Host::eSystemDefaultArchitecture)); 626 ArchSpec platform_arch64 (Host::GetArchitecture (Host::eSystemDefaultArchitecture64)); 627 if (platform_arch.IsExactMatch(platform_arch64)) 628 { 629 // This freebsd platform supports both 32 and 64 bit. Since we already 630 // returned the 64 bit arch for idx == 0, return the 32 bit arch 631 // for idx == 1 632 arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32); 633 return arch.IsValid(); 634 } 635 } 636 return false; 637 } 638 639 void 640 PlatformFreeBSD::GetStatus (Stream &strm) 641 { 642 struct utsname un; 643 644 if (uname(&un)) { 645 strm << "FreeBSD"; 646 return; 647 } 648 649 strm << "Host: " << un.sysname << ' ' << un.release << ' ' << un.version << '\n'; 650 Platform::GetStatus(strm); 651 } 652