180814287SRaphael Isemann //===-- PlatformNetBSD.cpp ------------------------------------------------===//
2910af4d9SBruce Mitchener //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6910af4d9SBruce Mitchener //
7910af4d9SBruce Mitchener //===----------------------------------------------------------------------===//
8910af4d9SBruce Mitchener
9910af4d9SBruce Mitchener #include "PlatformNetBSD.h"
10910af4d9SBruce Mitchener #include "lldb/Host/Config.h"
11910af4d9SBruce Mitchener
1276e47d48SRaphael Isemann #include <cstdio>
133011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
14910af4d9SBruce Mitchener #include <sys/utsname.h>
15910af4d9SBruce Mitchener #endif
16910af4d9SBruce Mitchener
17910af4d9SBruce Mitchener #include "lldb/Core/Debugger.h"
18910af4d9SBruce Mitchener #include "lldb/Core/PluginManager.h"
19910af4d9SBruce Mitchener #include "lldb/Host/HostInfo.h"
20910af4d9SBruce Mitchener #include "lldb/Target/Process.h"
213202ce6fSKamil Rytarowski #include "lldb/Target/Target.h"
225713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
23c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
246f9e6901SZachary Turner #include "lldb/Utility/Log.h"
25d821c997SPavel Labath #include "lldb/Utility/State.h"
2697206d57SZachary Turner #include "lldb/Utility/Status.h"
273202ce6fSKamil Rytarowski #include "lldb/Utility/StreamString.h"
283202ce6fSKamil Rytarowski
2905097246SAdrian Prantl // Define these constants from NetBSD mman.h for use when targeting remote
3005097246SAdrian Prantl // netbsd systems even when host has different values.
313202ce6fSKamil Rytarowski #define MAP_PRIVATE 0x0002
323202ce6fSKamil Rytarowski #define MAP_ANON 0x1000
33910af4d9SBruce Mitchener
34910af4d9SBruce Mitchener using namespace lldb;
35910af4d9SBruce Mitchener using namespace lldb_private;
36910af4d9SBruce Mitchener using namespace lldb_private::platform_netbsd;
37910af4d9SBruce Mitchener
38bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(PlatformNetBSD)
39fbb4d1e4SJonas Devlieghere
403202ce6fSKamil Rytarowski static uint32_t g_initialize_count = 0;
413202ce6fSKamil Rytarowski
423202ce6fSKamil Rytarowski
CreateInstance(bool force,const ArchSpec * arch)43b9c1b51eSKate Stone PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) {
44a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Platform);
45eb175a00SKamil Rytarowski LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
46eb175a00SKamil Rytarowski arch ? arch->GetArchitectureName() : "<null>",
47eb175a00SKamil Rytarowski arch ? arch->GetTriple().getTriple() : "<null>");
48910af4d9SBruce Mitchener
49910af4d9SBruce Mitchener bool create = force;
50a6682a41SJonas Devlieghere if (!create && arch && arch->IsValid()) {
51910af4d9SBruce Mitchener const llvm::Triple &triple = arch->GetTriple();
52b9c1b51eSKate Stone switch (triple.getOS()) {
53910af4d9SBruce Mitchener case llvm::Triple::NetBSD:
54910af4d9SBruce Mitchener create = true;
55910af4d9SBruce Mitchener break;
56910af4d9SBruce Mitchener
57910af4d9SBruce Mitchener default:
58910af4d9SBruce Mitchener break;
59910af4d9SBruce Mitchener }
60910af4d9SBruce Mitchener }
613202ce6fSKamil Rytarowski
62eb175a00SKamil Rytarowski LLDB_LOG(log, "create = {0}", create);
633202ce6fSKamil Rytarowski if (create) {
643202ce6fSKamil Rytarowski return PlatformSP(new PlatformNetBSD(false));
653202ce6fSKamil Rytarowski }
66910af4d9SBruce Mitchener return PlatformSP();
67910af4d9SBruce Mitchener }
68910af4d9SBruce Mitchener
GetPluginDescriptionStatic(bool is_host)69a458ef4fSPavel Labath llvm::StringRef PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
70910af4d9SBruce Mitchener if (is_host)
71910af4d9SBruce Mitchener return "Local NetBSD user platform plug-in.";
72910af4d9SBruce Mitchener return "Remote NetBSD user platform plug-in.";
73910af4d9SBruce Mitchener }
74910af4d9SBruce Mitchener
Initialize()75b9c1b51eSKate Stone void PlatformNetBSD::Initialize() {
763202ce6fSKamil Rytarowski PlatformPOSIX::Initialize();
77910af4d9SBruce Mitchener
78b9c1b51eSKate Stone if (g_initialize_count++ == 0) {
79d550002cSEd Maste #if defined(__NetBSD__)
80910af4d9SBruce Mitchener PlatformSP default_platform_sp(new PlatformNetBSD(true));
81910af4d9SBruce Mitchener default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
82910af4d9SBruce Mitchener Platform::SetHostPlatform(default_platform_sp);
83d550002cSEd Maste #endif
843202ce6fSKamil Rytarowski PluginManager::RegisterPlugin(
853202ce6fSKamil Rytarowski PlatformNetBSD::GetPluginNameStatic(false),
863202ce6fSKamil Rytarowski PlatformNetBSD::GetPluginDescriptionStatic(false),
873202ce6fSKamil Rytarowski PlatformNetBSD::CreateInstance, nullptr);
88910af4d9SBruce Mitchener }
89910af4d9SBruce Mitchener }
90910af4d9SBruce Mitchener
Terminate()91b9c1b51eSKate Stone void PlatformNetBSD::Terminate() {
923202ce6fSKamil Rytarowski if (g_initialize_count > 0) {
933202ce6fSKamil Rytarowski if (--g_initialize_count == 0) {
94910af4d9SBruce Mitchener PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
953202ce6fSKamil Rytarowski }
96910af4d9SBruce Mitchener }
97910af4d9SBruce Mitchener
983202ce6fSKamil Rytarowski PlatformPOSIX::Terminate();
99910af4d9SBruce Mitchener }
100910af4d9SBruce Mitchener
101910af4d9SBruce Mitchener /// Default Constructor
PlatformNetBSD(bool is_host)102b9c1b51eSKate Stone PlatformNetBSD::PlatformNetBSD(bool is_host)
1033202ce6fSKamil Rytarowski : PlatformPOSIX(is_host) // This is the local host platform
104669e57ebSPavel Labath {
105669e57ebSPavel Labath if (is_host) {
106910af4d9SBruce Mitchener ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
107669e57ebSPavel Labath m_supported_architectures.push_back(hostArch);
108669e57ebSPavel Labath if (hostArch.GetTriple().isArch64Bit()) {
109669e57ebSPavel Labath m_supported_architectures.push_back(
110669e57ebSPavel Labath HostInfo::GetArchitecture(HostInfo::eArchKind32));
111910af4d9SBruce Mitchener }
112b9c1b51eSKate Stone } else {
113669e57ebSPavel Labath m_supported_architectures = CreateArchList(
114669e57ebSPavel Labath {llvm::Triple::x86_64, llvm::Triple::x86}, llvm::Triple::NetBSD);
115669e57ebSPavel Labath }
116669e57ebSPavel Labath }
117669e57ebSPavel Labath
118*dde487e5SJonas Devlieghere std::vector<ArchSpec>
GetSupportedArchitectures(const ArchSpec & process_host_arch)119*dde487e5SJonas Devlieghere PlatformNetBSD::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
120910af4d9SBruce Mitchener if (m_remote_platform_sp)
121*dde487e5SJonas Devlieghere return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
122669e57ebSPavel Labath return m_supported_architectures;
123910af4d9SBruce Mitchener }
124910af4d9SBruce Mitchener
GetStatus(Stream & strm)125b9c1b51eSKate Stone void PlatformNetBSD::GetStatus(Stream &strm) {
1263202ce6fSKamil Rytarowski Platform::GetStatus(strm);
1273202ce6fSKamil Rytarowski
1283011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
1293202ce6fSKamil Rytarowski // Display local kernel information only when we are running in host mode.
1303202ce6fSKamil Rytarowski // Otherwise, we would end up printing non-NetBSD information (when running
1313202ce6fSKamil Rytarowski // on Mac OS for example).
1323202ce6fSKamil Rytarowski if (IsHost()) {
1333202ce6fSKamil Rytarowski struct utsname un;
134910af4d9SBruce Mitchener
1353202ce6fSKamil Rytarowski if (uname(&un))
1363202ce6fSKamil Rytarowski return;
137910af4d9SBruce Mitchener
1383202ce6fSKamil Rytarowski strm.Printf(" Kernel: %s\n", un.sysname);
1393202ce6fSKamil Rytarowski strm.Printf(" Release: %s\n", un.release);
1403202ce6fSKamil Rytarowski strm.Printf(" Version: %s\n", un.version);
141910af4d9SBruce Mitchener }
142910af4d9SBruce Mitchener #endif
1433202ce6fSKamil Rytarowski }
144910af4d9SBruce Mitchener
14545c3fc97SRaphael Isemann uint32_t
GetResumeCountForLaunchInfo(ProcessLaunchInfo & launch_info)1463202ce6fSKamil Rytarowski PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
14745c3fc97SRaphael Isemann uint32_t resume_count = 0;
1483202ce6fSKamil Rytarowski
1493202ce6fSKamil Rytarowski // Always resume past the initial stop when we use eLaunchFlagDebug
1503202ce6fSKamil Rytarowski if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
1513202ce6fSKamil Rytarowski // Resume past the stop for the final exec into the true inferior.
1523202ce6fSKamil Rytarowski ++resume_count;
1533202ce6fSKamil Rytarowski }
1543202ce6fSKamil Rytarowski
1553202ce6fSKamil Rytarowski // If we're not launching a shell, we're done.
1563202ce6fSKamil Rytarowski const FileSpec &shell = launch_info.GetShell();
1573202ce6fSKamil Rytarowski if (!shell)
1583202ce6fSKamil Rytarowski return resume_count;
1593202ce6fSKamil Rytarowski
1603202ce6fSKamil Rytarowski std::string shell_string = shell.GetPath();
1613202ce6fSKamil Rytarowski // We're in a shell, so for sure we have to resume past the shell exec.
1623202ce6fSKamil Rytarowski ++resume_count;
1633202ce6fSKamil Rytarowski
1643202ce6fSKamil Rytarowski // Figure out what shell we're planning on using.
1653202ce6fSKamil Rytarowski const char *shell_name = strrchr(shell_string.c_str(), '/');
166248a1305SKonrad Kleine if (shell_name == nullptr)
1673202ce6fSKamil Rytarowski shell_name = shell_string.c_str();
1683202ce6fSKamil Rytarowski else
1693202ce6fSKamil Rytarowski shell_name++;
1703202ce6fSKamil Rytarowski
1713202ce6fSKamil Rytarowski if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
1723202ce6fSKamil Rytarowski strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
1733202ce6fSKamil Rytarowski // These shells seem to re-exec themselves. Add another resume.
1743202ce6fSKamil Rytarowski ++resume_count;
1753202ce6fSKamil Rytarowski }
1763202ce6fSKamil Rytarowski
1773202ce6fSKamil Rytarowski return resume_count;
1783202ce6fSKamil Rytarowski }
1793202ce6fSKamil Rytarowski
CanDebugProcess()1803202ce6fSKamil Rytarowski bool PlatformNetBSD::CanDebugProcess() {
1813202ce6fSKamil Rytarowski if (IsHost()) {
1823202ce6fSKamil Rytarowski return true;
1833202ce6fSKamil Rytarowski } else {
1843202ce6fSKamil Rytarowski // If we're connected, we can debug.
1853202ce6fSKamil Rytarowski return IsConnected();
1863202ce6fSKamil Rytarowski }
1873202ce6fSKamil Rytarowski }
1883202ce6fSKamil Rytarowski
CalculateTrapHandlerSymbolNames()189b9c1b51eSKate Stone void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
190910af4d9SBruce Mitchener m_trap_handlers.push_back(ConstString("_sigtramp"));
191910af4d9SBruce Mitchener }
192910af4d9SBruce Mitchener
GetMmapArgumentList(const ArchSpec & arch,addr_t addr,addr_t length,unsigned prot,unsigned flags,addr_t fd,addr_t offset)19337c40af7SEd Maste MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
19437c40af7SEd Maste addr_t addr, addr_t length,
19537c40af7SEd Maste unsigned prot, unsigned flags,
19637c40af7SEd Maste addr_t fd, addr_t offset) {
1973202ce6fSKamil Rytarowski uint64_t flags_platform = 0;
198910af4d9SBruce Mitchener
1993202ce6fSKamil Rytarowski if (flags & eMmapFlagsPrivate)
2003202ce6fSKamil Rytarowski flags_platform |= MAP_PRIVATE;
2013202ce6fSKamil Rytarowski if (flags & eMmapFlagsAnon)
2023202ce6fSKamil Rytarowski flags_platform |= MAP_ANON;
20337c40af7SEd Maste
20437c40af7SEd Maste MmapArgList args({addr, length, prot, flags_platform, fd, offset});
20537c40af7SEd Maste return args;
206910af4d9SBruce Mitchener }
2073053e143SMichał Górny
GetSiginfoType(const llvm::Triple & triple)2083053e143SMichał Górny CompilerType PlatformNetBSD::GetSiginfoType(const llvm::Triple &triple) {
2093053e143SMichał Górny if (!m_type_system_up)
2103053e143SMichał Górny m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
2113053e143SMichał Górny TypeSystemClang *ast = m_type_system_up.get();
2123053e143SMichał Górny
2133053e143SMichał Górny // generic types
2143053e143SMichał Górny CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
2153053e143SMichał Górny CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
2163053e143SMichał Górny CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
2173053e143SMichał Górny CompilerType long_long_type = ast->GetBasicType(eBasicTypeLongLong);
2183053e143SMichał Górny CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
2193053e143SMichał Górny
2203053e143SMichał Górny // platform-specific types
2213053e143SMichał Górny CompilerType &pid_type = int_type;
2223053e143SMichał Górny CompilerType &uid_type = uint_type;
2233053e143SMichał Górny CompilerType &clock_type = uint_type;
2243053e143SMichał Górny CompilerType &lwpid_type = int_type;
2253053e143SMichał Górny
2263053e143SMichał Górny CompilerType sigval_type = ast->CreateRecordType(
2273053e143SMichał Górny nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
2283053e143SMichał Górny clang::TTK_Union, lldb::eLanguageTypeC);
2293053e143SMichał Górny ast->StartTagDeclarationDefinition(sigval_type);
2303053e143SMichał Górny ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
2313053e143SMichał Górny lldb::eAccessPublic, 0);
2323053e143SMichał Górny ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
2333053e143SMichał Górny lldb::eAccessPublic, 0);
2343053e143SMichał Górny ast->CompleteTagDeclarationDefinition(sigval_type);
2353053e143SMichał Górny
2363053e143SMichał Górny CompilerType ptrace_option_type = ast->CreateRecordType(
2373053e143SMichał Górny nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
2383053e143SMichał Górny clang::TTK_Union, lldb::eLanguageTypeC);
2393053e143SMichał Górny ast->StartTagDeclarationDefinition(ptrace_option_type);
2403053e143SMichał Górny ast->AddFieldToRecordType(ptrace_option_type, "_pe_other_pid", pid_type,
2413053e143SMichał Górny lldb::eAccessPublic, 0);
2423053e143SMichał Górny ast->AddFieldToRecordType(ptrace_option_type, "_pe_lwp", lwpid_type,
2433053e143SMichał Górny lldb::eAccessPublic, 0);
2443053e143SMichał Górny ast->CompleteTagDeclarationDefinition(ptrace_option_type);
2453053e143SMichał Górny
2463053e143SMichał Górny // siginfo_t
2473053e143SMichał Górny CompilerType siginfo_type = ast->CreateRecordType(
2483053e143SMichał Górny nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
2493053e143SMichał Górny clang::TTK_Union, lldb::eLanguageTypeC);
2503053e143SMichał Górny ast->StartTagDeclarationDefinition(siginfo_type);
2513053e143SMichał Górny
2523053e143SMichał Górny // struct _ksiginfo
2533053e143SMichał Górny CompilerType ksiginfo_type = ast->CreateRecordType(
2543053e143SMichał Górny nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
2553053e143SMichał Górny clang::TTK_Struct, lldb::eLanguageTypeC);
2563053e143SMichał Górny ast->StartTagDeclarationDefinition(ksiginfo_type);
2573053e143SMichał Górny ast->AddFieldToRecordType(ksiginfo_type, "_signo", int_type,
2583053e143SMichał Górny lldb::eAccessPublic, 0);
2593053e143SMichał Górny ast->AddFieldToRecordType(ksiginfo_type, "_code", int_type,
2603053e143SMichał Górny lldb::eAccessPublic, 0);
2613053e143SMichał Górny ast->AddFieldToRecordType(ksiginfo_type, "_errno", int_type,
2623053e143SMichał Górny lldb::eAccessPublic, 0);
2633053e143SMichał Górny
2643053e143SMichał Górny // the structure is padded on 64-bit arches to fix alignment
2653053e143SMichał Górny if (triple.isArch64Bit())
2663053e143SMichał Górny ast->AddFieldToRecordType(ksiginfo_type, "__pad0", int_type,
2673053e143SMichał Górny lldb::eAccessPublic, 0);
2683053e143SMichał Górny
2693053e143SMichał Górny // union used to hold the signal data
2703053e143SMichał Górny CompilerType union_type = ast->CreateRecordType(
2713053e143SMichał Górny nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
2723053e143SMichał Górny clang::TTK_Union, lldb::eLanguageTypeC);
2733053e143SMichał Górny ast->StartTagDeclarationDefinition(union_type);
2743053e143SMichał Górny
2753053e143SMichał Górny ast->AddFieldToRecordType(
2763053e143SMichał Górny union_type, "_rt",
2773053e143SMichał Górny ast->CreateStructForIdentifier(ConstString(),
2783053e143SMichał Górny {
2793053e143SMichał Górny {"_pid", pid_type},
2803053e143SMichał Górny {"_uid", uid_type},
2813053e143SMichał Górny {"_value", sigval_type},
2823053e143SMichał Górny }),
2833053e143SMichał Górny lldb::eAccessPublic, 0);
2843053e143SMichał Górny
2853053e143SMichał Górny ast->AddFieldToRecordType(
2863053e143SMichał Górny union_type, "_child",
2873053e143SMichał Górny ast->CreateStructForIdentifier(ConstString(),
2883053e143SMichał Górny {
2893053e143SMichał Górny {"_pid", pid_type},
2903053e143SMichał Górny {"_uid", uid_type},
2913053e143SMichał Górny {"_status", int_type},
2923053e143SMichał Górny {"_utime", clock_type},
2933053e143SMichał Górny {"_stime", clock_type},
2943053e143SMichał Górny }),
2953053e143SMichał Górny lldb::eAccessPublic, 0);
2963053e143SMichał Górny
2973053e143SMichał Górny ast->AddFieldToRecordType(
2983053e143SMichał Górny union_type, "_fault",
2993053e143SMichał Górny ast->CreateStructForIdentifier(ConstString(),
3003053e143SMichał Górny {
3013053e143SMichał Górny {"_addr", voidp_type},
3023053e143SMichał Górny {"_trap", int_type},
3033053e143SMichał Górny {"_trap2", int_type},
3043053e143SMichał Górny {"_trap3", int_type},
3053053e143SMichał Górny }),
3063053e143SMichał Górny lldb::eAccessPublic, 0);
3073053e143SMichał Górny
3083053e143SMichał Górny ast->AddFieldToRecordType(
3093053e143SMichał Górny union_type, "_poll",
3103053e143SMichał Górny ast->CreateStructForIdentifier(ConstString(),
3113053e143SMichał Górny {
3123053e143SMichał Górny {"_band", long_type},
3133053e143SMichał Górny {"_fd", int_type},
3143053e143SMichał Górny }),
3153053e143SMichał Górny lldb::eAccessPublic, 0);
3163053e143SMichał Górny
3173053e143SMichał Górny ast->AddFieldToRecordType(union_type, "_syscall",
3183053e143SMichał Górny ast->CreateStructForIdentifier(
3193053e143SMichał Górny ConstString(),
3203053e143SMichał Górny {
3213053e143SMichał Górny {"_sysnum", int_type},
3223053e143SMichał Górny {"_retval", int_type.GetArrayType(2)},
3233053e143SMichał Górny {"_error", int_type},
3243053e143SMichał Górny {"_args", long_long_type.GetArrayType(8)},
3253053e143SMichał Górny }),
3263053e143SMichał Górny lldb::eAccessPublic, 0);
3273053e143SMichał Górny
3283053e143SMichał Górny ast->AddFieldToRecordType(
3293053e143SMichał Górny union_type, "_ptrace_state",
3303053e143SMichał Górny ast->CreateStructForIdentifier(ConstString(),
3313053e143SMichał Górny {
3323053e143SMichał Górny {"_pe_report_event", int_type},
3333053e143SMichał Górny {"_option", ptrace_option_type},
3343053e143SMichał Górny }),
3353053e143SMichał Górny lldb::eAccessPublic, 0);
3363053e143SMichał Górny
3373053e143SMichał Górny ast->CompleteTagDeclarationDefinition(union_type);
3383053e143SMichał Górny ast->AddFieldToRecordType(ksiginfo_type, "_reason", union_type,
3393053e143SMichał Górny lldb::eAccessPublic, 0);
3403053e143SMichał Górny
3413053e143SMichał Górny ast->CompleteTagDeclarationDefinition(ksiginfo_type);
3423053e143SMichał Górny ast->AddFieldToRecordType(siginfo_type, "_info", ksiginfo_type,
3433053e143SMichał Górny lldb::eAccessPublic, 0);
3443053e143SMichał Górny
3453053e143SMichał Górny ast->CompleteTagDeclarationDefinition(siginfo_type);
3463053e143SMichał Górny return siginfo_type;
3473053e143SMichał Górny }
348