1 //===-- PlatformNetBSD.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 "PlatformNetBSD.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_netbsd;
38 PlatformNetBSD::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::NetBSD:
59 return PlatformSP(new PlatformNetBSD (is_host));
65 PlatformNetBSD::GetPluginNameStatic(bool is_host)
69 static ConstString g_host_name(Platform::GetHostPlatformName ());
74 static ConstString g_remote_name("remote-netbsd");
80 PlatformNetBSD::GetDescriptionStatic (bool is_host)
83 return "Local NetBSD user platform plug-in.";
85 return "Remote NetBSD user platform plug-in.";
88 static uint32_t g_initialize_count = 0;
91 PlatformNetBSD::Initialize ()
93 Platform::Initialize ();
95 if (g_initialize_count++ == 0)
97 #if defined(__NetBSD__)
98 // Force a host flag to true for the default platform object.
99 PlatformSP default_platform_sp (new PlatformNetBSD(true));
100 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
101 Platform::SetHostPlatform (default_platform_sp);
103 PluginManager::RegisterPlugin(PlatformNetBSD::GetPluginNameStatic(false),
104 PlatformNetBSD::GetDescriptionStatic(false),
105 PlatformNetBSD::CreateInstance);
110 PlatformNetBSD::Terminate ()
112 if (g_initialize_count > 0 && --g_initialize_count == 0)
113 PluginManager::UnregisterPlugin (PlatformNetBSD::CreateInstance);
115 Platform::Terminate ();
119 PlatformNetBSD::GetModuleSpec (const FileSpec& module_file_spec,
120 const ArchSpec& arch,
121 ModuleSpec &module_spec)
123 if (m_remote_platform_sp)
124 return m_remote_platform_sp->GetModuleSpec (module_file_spec, arch, module_spec);
126 return Platform::GetModuleSpec (module_file_spec, arch, module_spec);
130 PlatformNetBSD::RunShellCommand(const char *command,
131 const FileSpec &working_dir,
134 std::string *command_output,
135 uint32_t timeout_sec)
138 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
141 if (m_remote_platform_sp)
142 return m_remote_platform_sp->RunShellCommand(command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
144 return Error("unable to run a remote command without a platform");
149 PlatformNetBSD::ResolveExecutable (const ModuleSpec &module_spec,
150 lldb::ModuleSP &exe_module_sp,
151 const FileSpecList *module_search_paths_ptr)
154 // Nothing special to do here, just use the actual file and architecture
156 char exe_path[PATH_MAX];
157 ModuleSpec resolved_module_spec(module_spec);
161 // If we have "ls" as the module_spec's file, resolve the executable location based on
162 // the current path variables
163 if (!resolved_module_spec.GetFileSpec().Exists())
165 module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
166 resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
169 if (!resolved_module_spec.GetFileSpec().Exists())
170 resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
172 if (resolved_module_spec.GetFileSpec().Exists())
176 error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
181 if (m_remote_platform_sp)
183 error = GetCachedExecutable (resolved_module_spec, exe_module_sp, module_search_paths_ptr, *m_remote_platform_sp);
187 // We may connect to a process and use the provided executable (Don't use local $PATH).
189 // Resolve any executable within a bundle on MacOSX
190 Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
192 if (resolved_module_spec.GetFileSpec().Exists())
198 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", resolved_module_spec.GetFileSpec().GetPath().c_str());
205 if (resolved_module_spec.GetArchitecture().IsValid())
207 error = ModuleList::GetSharedModule (resolved_module_spec,
209 module_search_paths_ptr,
213 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
215 exe_module_sp.reset();
216 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
217 resolved_module_spec.GetFileSpec().GetPath().c_str(),
218 resolved_module_spec.GetArchitecture().GetArchitectureName());
223 // No valid architecture was specified, ask the platform for
224 // the architectures that we should be using (in the correct order)
225 // and see if we can find a match that way
226 StreamString arch_names;
227 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
229 error = ModuleList::GetSharedModule (resolved_module_spec,
231 module_search_paths_ptr,
234 // Did we find an executable using one of the
237 if (exe_module_sp && exe_module_sp->GetObjectFile())
240 error.SetErrorToGenericError();
244 arch_names.PutCString (", ");
245 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
248 if (error.Fail() || !exe_module_sp)
250 if (resolved_module_spec.GetFileSpec().Readable())
252 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
253 resolved_module_spec.GetFileSpec().GetPath().c_str(),
254 GetPluginName().GetCString(),
255 arch_names.GetString().c_str());
259 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
268 // From PlatformMacOSX only
270 PlatformNetBSD::GetFileWithUUID (const FileSpec &platform_file,
271 const UUID *uuid_ptr,
272 FileSpec &local_file)
276 if (m_remote_platform_sp)
277 return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
280 // Default to the local case
281 local_file = platform_file;
286 //------------------------------------------------------------------
287 /// Default Constructor
288 //------------------------------------------------------------------
289 PlatformNetBSD::PlatformNetBSD (bool is_host) :
291 m_remote_platform_sp()
296 PlatformNetBSD::GetRemoteOSVersion ()
298 if (m_remote_platform_sp)
299 return m_remote_platform_sp->GetOSVersion (m_major_os_version,
301 m_update_os_version);
306 PlatformNetBSD::GetRemoteOSBuildString (std::string &s)
308 if (m_remote_platform_sp)
309 return m_remote_platform_sp->GetRemoteOSBuildString (s);
315 PlatformNetBSD::GetRemoteOSKernelDescription (std::string &s)
317 if (m_remote_platform_sp)
318 return m_remote_platform_sp->GetRemoteOSKernelDescription (s);
323 // Remote Platform subclasses need to override this function
325 PlatformNetBSD::GetRemoteSystemArchitecture ()
327 if (m_remote_platform_sp)
328 return m_remote_platform_sp->GetRemoteSystemArchitecture ();
334 PlatformNetBSD::GetHostname ()
337 return Platform::GetHostname();
339 if (m_remote_platform_sp)
340 return m_remote_platform_sp->GetHostname ();
345 PlatformNetBSD::IsConnected () const
349 else if (m_remote_platform_sp)
350 return m_remote_platform_sp->IsConnected();
355 PlatformNetBSD::ConnectRemote (Args& args)
360 error.SetErrorStringWithFormat ("can't connect to the host platform '%s', always connected", GetPluginName().GetCString());
364 if (!m_remote_platform_sp)
365 m_remote_platform_sp = Platform::Create (ConstString("remote-gdb-server"), error);
367 if (m_remote_platform_sp)
371 if (m_remote_platform_sp)
373 error = m_remote_platform_sp->ConnectRemote (args);
377 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>");
382 error.SetErrorString ("failed to create a 'remote-gdb-server' platform");
385 m_remote_platform_sp.reset();
392 PlatformNetBSD::DisconnectRemote ()
398 error.SetErrorStringWithFormat ("can't disconnect from the host platform '%s', always connected", GetPluginName().GetCString());
402 if (m_remote_platform_sp)
403 error = m_remote_platform_sp->DisconnectRemote ();
405 error.SetErrorString ("the platform is not currently connected");
411 PlatformNetBSD::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
413 bool success = false;
416 success = Platform::GetProcessInfo (pid, process_info);
418 else if (m_remote_platform_sp)
420 success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
426 PlatformNetBSD::FindProcesses (const ProcessInstanceInfoMatch &match_info,
427 ProcessInstanceInfoList &process_infos)
429 uint32_t match_count = 0;
432 // Let the base class figure out the host details
433 match_count = Platform::FindProcesses (match_info, process_infos);
437 // If we are remote, we can only return results if we are connected
438 if (m_remote_platform_sp)
439 match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
445 PlatformNetBSD::GetUserName (uint32_t uid)
447 // Check the cache in Platform in case we have already looked this uid up
448 const char *user_name = Platform::GetUserName(uid);
452 if (IsRemote() && m_remote_platform_sp)
453 return m_remote_platform_sp->GetUserName(uid);
458 PlatformNetBSD::GetGroupName (uint32_t gid)
460 const char *group_name = Platform::GetGroupName(gid);
464 if (IsRemote() && m_remote_platform_sp)
465 return m_remote_platform_sp->GetGroupName(gid);
471 PlatformNetBSD::GetSharedModule (const ModuleSpec &module_spec,
474 const FileSpecList *module_search_paths_ptr,
475 ModuleSP *old_module_sp_ptr,
476 bool *did_create_ptr)
483 // If we have a remote platform always, let it try and locate
484 // the shared module first.
485 if (m_remote_platform_sp)
487 error = m_remote_platform_sp->GetSharedModule (module_spec,
490 module_search_paths_ptr,
498 // Fall back to the local platform and find the file locally
499 error = Platform::GetSharedModule (module_spec,
502 module_search_paths_ptr,
507 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
513 PlatformNetBSD::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
517 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
518 if (hostArch.GetTriple().isOSNetBSD())
523 return arch.IsValid();
527 // If the default host architecture is 64-bit, look for a 32-bit variant
528 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
530 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
531 return arch.IsValid();
538 if (m_remote_platform_sp)
539 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
542 // Set the OS to NetBSD
543 triple.setOS(llvm::Triple::NetBSD);
544 // Set the architecture
547 case 0: triple.setArchName("x86_64"); break;
548 case 1: triple.setArchName("i386"); break;
549 default: return false;
551 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the vendor by
552 // calling triple.SetVendorName("unknown") so that it is a "unspecified unknown".
553 // This means when someone calls triple.GetVendorName() it will return an empty string
554 // which indicates that the vendor can be set when two architectures are merged
556 // Now set the triple into "arch" and return true
557 arch.SetTriple(triple);
564 PlatformNetBSD::GetStatus (Stream &strm)
566 #ifndef LLDB_DISABLE_POSIX
571 ::memset(&un, 0, sizeof(utsname));
572 if (::uname(&un) == -1) {
573 strm << "NetBSD" << '\n';
575 strm << un.sysname << ' ' << un.release;
576 if (un.nodename[0] != '\0')
577 strm << " (" << un.nodename << ')';
580 // Dump a common information about the platform status.
581 strm << "Host: " << un.sysname << ' ' << un.release << ' ' << un.version << '\n';
585 Platform::GetStatus(strm);
589 PlatformNetBSD::CalculateTrapHandlerSymbolNames ()
591 m_trap_handlers.push_back (ConstString ("_sigtramp"));
595 PlatformNetBSD::LaunchProcess (ProcessLaunchInfo &launch_info)
600 error = Platform::LaunchProcess (launch_info);
604 if (m_remote_platform_sp)
605 error = m_remote_platform_sp->LaunchProcess (launch_info);
607 error.SetErrorString ("the platform is not currently connected");
613 PlatformNetBSD::Attach(ProcessAttachInfo &attach_info,
618 lldb::ProcessSP process_sp;
623 TargetSP new_target_sp;
624 ArchSpec emptyArchSpec;
626 error = debugger.GetTargetList().CreateTarget (debugger,
630 m_remote_platform_sp,
632 target = new_target_sp.get();
637 if (target && error.Success())
639 debugger.GetTargetList().SetSelectedTarget(target);
640 // The netbsd always currently uses the GDB remote debugger plug-in
641 // so even when debugging locally we are debugging remotely!
642 // Just like the darwin plugin.
643 process_sp = target->CreateProcess (attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
646 error = process_sp->Attach (attach_info);
651 if (m_remote_platform_sp)
652 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, error);
654 error.SetErrorString ("the platform is not currently connected");