1 //===-- PlatformFreeBSD.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PlatformFreeBSD.h"
11 #include "lldb/Host/Config.h"
15 #ifndef LLDB_DISABLE_POSIX
16 #include <sys/utsname.h>
20 // Other libraries and framework includes
22 #include "lldb/Breakpoint/BreakpointLocation.h"
23 #include "lldb/Breakpoint/BreakpointSite.h"
24 #include "lldb/Core/Error.h"
25 #include "lldb/Core/Debugger.h"
26 #include "lldb/Core/Module.h"
27 #include "lldb/Core/ModuleSpec.h"
28 #include "lldb/Core/PluginManager.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Target/Process.h"
34 using namespace lldb_private;
35 using namespace lldb_private::platform_freebsd;
38 PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch)
40 // The only time we create an instance is when we are creating a remote
42 const bool is_host = false;
45 if (create == false && arch && arch->IsValid())
47 const llvm::Triple &triple = arch->GetTriple();
48 switch (triple.getOS())
50 case llvm::Triple::FreeBSD:
54 #if defined(__FreeBSD__) || defined(__OpenBSD__)
55 // Only accept "unknown" for the OS if the host is BSD and
56 // it "unknown" wasn't specified (it was just returned because it
58 case llvm::Triple::OSType::UnknownOS:
59 create = !arch->TripleOSWasSpecified();
67 return PlatformSP(new PlatformFreeBSD (is_host));
73 PlatformFreeBSD::GetPluginNameStatic(bool is_host)
77 static ConstString g_host_name(Platform::GetHostPlatformName ());
82 static ConstString g_remote_name("remote-freebsd");
88 PlatformFreeBSD::GetDescriptionStatic (bool is_host)
91 return "Local FreeBSD user platform plug-in.";
93 return "Remote FreeBSD user platform plug-in.";
96 static uint32_t g_initialize_count = 0;
99 PlatformFreeBSD::Initialize ()
101 Platform::Initialize ();
103 if (g_initialize_count++ == 0)
105 #if defined (__FreeBSD__)
106 // Force a host flag to true for the default platform object.
107 PlatformSP default_platform_sp (new PlatformFreeBSD(true));
108 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
109 Platform::SetHostPlatform (default_platform_sp);
111 PluginManager::RegisterPlugin(PlatformFreeBSD::GetPluginNameStatic(false),
112 PlatformFreeBSD::GetDescriptionStatic(false),
113 PlatformFreeBSD::CreateInstance);
118 PlatformFreeBSD::Terminate ()
120 if (g_initialize_count > 0 && --g_initialize_count == 0)
121 PluginManager::UnregisterPlugin (PlatformFreeBSD::CreateInstance);
123 Platform::Terminate ();
127 PlatformFreeBSD::GetModuleSpec (const FileSpec& module_file_spec,
128 const ArchSpec& arch,
129 ModuleSpec &module_spec)
131 if (m_remote_platform_sp)
132 return m_remote_platform_sp->GetModuleSpec (module_file_spec, arch, module_spec);
134 return Platform::GetModuleSpec (module_file_spec, arch, module_spec);
138 PlatformFreeBSD::RunShellCommand(const char *command,
139 const FileSpec &working_dir,
142 std::string *command_output,
143 uint32_t timeout_sec)
146 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
149 if (m_remote_platform_sp)
150 return m_remote_platform_sp->RunShellCommand(command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
152 return Error("unable to run a remote command without a platform");
157 PlatformFreeBSD::ResolveExecutable (const ModuleSpec &module_spec,
158 lldb::ModuleSP &exe_module_sp,
159 const FileSpecList *module_search_paths_ptr)
162 // Nothing special to do here, just use the actual file and architecture
164 char exe_path[PATH_MAX];
165 ModuleSpec resolved_module_spec(module_spec);
169 // If we have "ls" as the module_spec's file, resolve the executable location based on
170 // the current path variables
171 if (!resolved_module_spec.GetFileSpec().Exists())
173 module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
174 resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
177 if (!resolved_module_spec.GetFileSpec().Exists())
178 resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
180 if (resolved_module_spec.GetFileSpec().Exists())
184 error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
189 if (m_remote_platform_sp)
191 error = GetCachedExecutable (resolved_module_spec, exe_module_sp, module_search_paths_ptr, *m_remote_platform_sp);
195 // We may connect to a process and use the provided executable (Don't use local $PATH).
197 // Resolve any executable within a bundle on MacOSX
198 Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
200 if (resolved_module_spec.GetFileSpec().Exists())
206 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", resolved_module_spec.GetFileSpec().GetPath().c_str());
213 if (resolved_module_spec.GetArchitecture().IsValid())
215 error = ModuleList::GetSharedModule (resolved_module_spec,
217 module_search_paths_ptr,
221 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
223 exe_module_sp.reset();
224 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
225 resolved_module_spec.GetFileSpec().GetPath().c_str(),
226 resolved_module_spec.GetArchitecture().GetArchitectureName());
231 // No valid architecture was specified, ask the platform for
232 // the architectures that we should be using (in the correct order)
233 // and see if we can find a match that way
234 StreamString arch_names;
235 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
237 error = ModuleList::GetSharedModule (resolved_module_spec,
239 module_search_paths_ptr,
242 // Did we find an executable using one of the
245 if (exe_module_sp && exe_module_sp->GetObjectFile())
248 error.SetErrorToGenericError();
252 arch_names.PutCString (", ");
253 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
256 if (error.Fail() || !exe_module_sp)
258 if (resolved_module_spec.GetFileSpec().Readable())
260 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
261 resolved_module_spec.GetFileSpec().GetPath().c_str(),
262 GetPluginName().GetCString(),
263 arch_names.GetString().c_str());
267 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
276 // From PlatformMacOSX only
278 PlatformFreeBSD::GetFileWithUUID (const FileSpec &platform_file,
279 const UUID *uuid_ptr,
280 FileSpec &local_file)
284 if (m_remote_platform_sp)
285 return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
288 // Default to the local case
289 local_file = platform_file;
294 //------------------------------------------------------------------
295 /// Default Constructor
296 //------------------------------------------------------------------
297 PlatformFreeBSD::PlatformFreeBSD (bool is_host) :
299 m_remote_platform_sp()
303 //------------------------------------------------------------------
306 /// The destructor is virtual since this class is designed to be
307 /// inherited from by the plug-in instance.
308 //------------------------------------------------------------------
309 PlatformFreeBSD::~PlatformFreeBSD()
313 //TODO:VK: inherit PlatformPOSIX
317 PlatformFreeBSD::GetRemoteOSVersion ()
319 if (m_remote_platform_sp)
320 return m_remote_platform_sp->GetOSVersion (m_major_os_version,
322 m_update_os_version);
327 PlatformFreeBSD::GetRemoteOSBuildString (std::string &s)
329 if (m_remote_platform_sp)
330 return m_remote_platform_sp->GetRemoteOSBuildString (s);
336 PlatformFreeBSD::GetRemoteOSKernelDescription (std::string &s)
338 if (m_remote_platform_sp)
339 return m_remote_platform_sp->GetRemoteOSKernelDescription (s);
344 // Remote Platform subclasses need to override this function
346 PlatformFreeBSD::GetRemoteSystemArchitecture ()
348 if (m_remote_platform_sp)
349 return m_remote_platform_sp->GetRemoteSystemArchitecture ();
355 PlatformFreeBSD::GetHostname ()
358 return Platform::GetHostname();
360 if (m_remote_platform_sp)
361 return m_remote_platform_sp->GetHostname ();
366 PlatformFreeBSD::IsConnected () const
370 else if (m_remote_platform_sp)
371 return m_remote_platform_sp->IsConnected();
376 PlatformFreeBSD::ConnectRemote (Args& args)
381 error.SetErrorStringWithFormat ("can't connect to the host platform '%s', always connected", GetPluginName().GetCString());
385 if (!m_remote_platform_sp)
386 m_remote_platform_sp = Platform::Create (ConstString("remote-gdb-server"), error);
388 if (m_remote_platform_sp)
392 if (m_remote_platform_sp)
394 error = m_remote_platform_sp->ConnectRemote (args);
398 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>");
403 error.SetErrorString ("failed to create a 'remote-gdb-server' platform");
406 m_remote_platform_sp.reset();
413 PlatformFreeBSD::DisconnectRemote ()
419 error.SetErrorStringWithFormat ("can't disconnect from the host platform '%s', always connected", GetPluginName().GetCString());
423 if (m_remote_platform_sp)
424 error = m_remote_platform_sp->DisconnectRemote ();
426 error.SetErrorString ("the platform is not currently connected");
432 PlatformFreeBSD::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
434 bool success = false;
437 success = Platform::GetProcessInfo (pid, process_info);
439 else if (m_remote_platform_sp)
441 success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
447 PlatformFreeBSD::FindProcesses (const ProcessInstanceInfoMatch &match_info,
448 ProcessInstanceInfoList &process_infos)
450 uint32_t match_count = 0;
453 // Let the base class figure out the host details
454 match_count = Platform::FindProcesses (match_info, process_infos);
458 // If we are remote, we can only return results if we are connected
459 if (m_remote_platform_sp)
460 match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
466 PlatformFreeBSD::GetUserName (uint32_t uid)
468 // Check the cache in Platform in case we have already looked this uid up
469 const char *user_name = Platform::GetUserName(uid);
473 if (IsRemote() && m_remote_platform_sp)
474 return m_remote_platform_sp->GetUserName(uid);
479 PlatformFreeBSD::GetGroupName (uint32_t gid)
481 const char *group_name = Platform::GetGroupName(gid);
485 if (IsRemote() && m_remote_platform_sp)
486 return m_remote_platform_sp->GetGroupName(gid);
492 PlatformFreeBSD::GetSharedModule (const ModuleSpec &module_spec,
495 const FileSpecList *module_search_paths_ptr,
496 ModuleSP *old_module_sp_ptr,
497 bool *did_create_ptr)
504 // If we have a remote platform always, let it try and locate
505 // the shared module first.
506 if (m_remote_platform_sp)
508 error = m_remote_platform_sp->GetSharedModule (module_spec,
511 module_search_paths_ptr,
519 // Fall back to the local platform and find the file locally
520 error = Platform::GetSharedModule (module_spec,
523 module_search_paths_ptr,
528 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
534 PlatformFreeBSD::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
538 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
539 if (hostArch.GetTriple().isOSFreeBSD())
544 return arch.IsValid();
548 // If the default host architecture is 64-bit, look for a 32-bit variant
549 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
551 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
552 return arch.IsValid();
559 if (m_remote_platform_sp)
560 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
563 // Set the OS to FreeBSD
564 triple.setOS(llvm::Triple::FreeBSD);
565 // Set the architecture
568 case 0: triple.setArchName("x86_64"); break;
569 case 1: triple.setArchName("i386"); break;
570 case 2: triple.setArchName("aarch64"); break;
571 case 3: triple.setArchName("arm"); break;
572 case 4: triple.setArchName("mips64"); break;
573 case 5: triple.setArchName("mips"); break;
574 case 6: triple.setArchName("ppc64"); break;
575 case 7: triple.setArchName("ppc"); break;
576 default: return false;
578 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the vendor by
579 // calling triple.SetVendorName("unknown") so that it is a "unspecified unknown".
580 // This means when someone calls triple.GetVendorName() it will return an empty string
581 // which indicates that the vendor can be set when two architectures are merged
583 // Now set the triple into "arch" and return true
584 arch.SetTriple(triple);
591 PlatformFreeBSD::GetStatus (Stream &strm)
593 #ifndef LLDB_DISABLE_POSIX
598 ::memset(&un, 0, sizeof(utsname));
599 if (uname(&un) == -1)
600 strm << "FreeBSD" << '\n';
602 strm << un.sysname << ' ' << un.release;
603 if (un.nodename[0] != '\0')
604 strm << " (" << un.nodename << ')';
607 // Dump a common information about the platform status.
608 strm << "Host: " << un.sysname << ' ' << un.release << ' ' << un.version << '\n';
611 Platform::GetStatus(strm);
615 PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
617 switch (target.GetArchitecture().GetMachine())
619 case llvm::Triple::arm:
621 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
622 AddressClass addr_class = eAddressClassUnknown;
626 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
627 if (addr_class == eAddressClassUnknown && (bp_loc_sp->GetAddress().GetFileAddress() & 1))
628 addr_class = eAddressClassCodeAlternateISA;
631 if (addr_class == eAddressClassCodeAlternateISA)
633 // TODO: Enable when FreeBSD supports thumb breakpoints.
634 // FreeBSD kernel as of 10.x, does not support thumb breakpoints
640 return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site);
646 PlatformFreeBSD::CalculateTrapHandlerSymbolNames ()
648 m_trap_handlers.push_back (ConstString ("_sigtramp"));
652 PlatformFreeBSD::LaunchProcess (ProcessLaunchInfo &launch_info)
657 error = Platform::LaunchProcess (launch_info);
661 if (m_remote_platform_sp)
662 error = m_remote_platform_sp->LaunchProcess (launch_info);
664 error.SetErrorString ("the platform is not currently connected");
670 PlatformFreeBSD::Attach(ProcessAttachInfo &attach_info,
675 lldb::ProcessSP process_sp;
680 TargetSP new_target_sp;
681 ArchSpec emptyArchSpec;
683 error = debugger.GetTargetList().CreateTarget (debugger,
687 m_remote_platform_sp,
689 target = new_target_sp.get();
694 if (target && error.Success())
696 debugger.GetTargetList().SetSelectedTarget(target);
697 // The freebsd always currently uses the GDB remote debugger plug-in
698 // so even when debugging locally we are debugging remotely!
699 // Just like the darwin plugin.
700 process_sp = target->CreateProcess (attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
703 error = process_sp->Attach (attach_info);
708 if (m_remote_platform_sp)
709 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, error);
711 error.SetErrorString ("the platform is not currently connected");