LLDB  mainline
PlatformLinux.cpp
Go to the documentation of this file.
1 //===-- PlatformLinux.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "PlatformLinux.h"
10 #include "lldb/Host/Config.h"
11 
12 #include <stdio.h>
13 #if LLDB_ENABLE_POSIX
14 #include <sys/utsname.h>
15 #endif
16 
17 #include "lldb/Core/Debugger.h"
19 #include "lldb/Host/HostInfo.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/State.h"
25 #include "lldb/Utility/Status.h"
27 
28 // Define these constants from Linux mman.h for use when targeting remote linux
29 // systems even when host has different values.
30 #define MAP_PRIVATE 2
31 #define MAP_ANON 0x20
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 using namespace lldb_private::platform_linux;
36 
38 
40 
41 
42 PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
44  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
45  arch ? arch->GetArchitectureName() : "<null>",
46  arch ? arch->GetTriple().getTriple() : "<null>");
47 
48  bool create = force;
49  if (!create && arch && arch->IsValid()) {
50  const llvm::Triple &triple = arch->GetTriple();
51  switch (triple.getOS()) {
52  case llvm::Triple::Linux:
53  create = true;
54  break;
55 
56 #if defined(__linux__)
57  // Only accept "unknown" for the OS if the host is linux and it "unknown"
58  // wasn't specified (it was just returned because it was NOT specified)
59  case llvm::Triple::OSType::UnknownOS:
60  create = !arch->TripleOSWasSpecified();
61  break;
62 #endif
63  default:
64  break;
65  }
66  }
67 
68  LLDB_LOG(log, "create = {0}", create);
69  if (create) {
70  return PlatformSP(new PlatformLinux(false));
71  }
72  return PlatformSP();
73 }
74 
75 ConstString PlatformLinux::GetPluginNameStatic(bool is_host) {
76  if (is_host) {
77  static ConstString g_host_name(Platform::GetHostPlatformName());
78  return g_host_name;
79  } else {
80  static ConstString g_remote_name("remote-linux");
81  return g_remote_name;
82  }
83 }
84 
85 const char *PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
86  if (is_host)
87  return "Local Linux user platform plug-in.";
88  else
89  return "Remote Linux user platform plug-in.";
90 }
91 
92 ConstString PlatformLinux::GetPluginName() {
93  return GetPluginNameStatic(IsHost());
94 }
95 
96 void PlatformLinux::Initialize() {
98 
99  if (g_initialize_count++ == 0) {
100 #if defined(__linux__) && !defined(__ANDROID__)
101  PlatformSP default_platform_sp(new PlatformLinux(true));
102  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
103  Platform::SetHostPlatform(default_platform_sp);
104 #endif
105  PluginManager::RegisterPlugin(
106  PlatformLinux::GetPluginNameStatic(false),
107  PlatformLinux::GetPluginDescriptionStatic(false),
108  PlatformLinux::CreateInstance, nullptr);
109  }
110 }
111 
112 void PlatformLinux::Terminate() {
113  if (g_initialize_count > 0) {
114  if (--g_initialize_count == 0) {
115  PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
116  }
117  }
118 
120 }
121 
122 /// Default Constructor
123 PlatformLinux::PlatformLinux(bool is_host)
124  : PlatformPOSIX(is_host) // This is the local host platform
125 {}
126 
128  ArchSpec &arch) {
129  if (IsHost()) {
130  ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
131  if (hostArch.GetTriple().isOSLinux()) {
132  if (idx == 0) {
133  arch = hostArch;
134  return arch.IsValid();
135  } else if (idx == 1) {
136  // If the default host architecture is 64-bit, look for a 32-bit
137  // variant
138  if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
139  arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
140  return arch.IsValid();
141  }
142  }
143  }
144  } else {
146  return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
147 
148  llvm::Triple triple;
149  // Set the OS to linux
150  triple.setOS(llvm::Triple::Linux);
151  // Set the architecture
152  switch (idx) {
153  case 0:
154  triple.setArchName("x86_64");
155  break;
156  case 1:
157  triple.setArchName("i386");
158  break;
159  case 2:
160  triple.setArchName("arm");
161  break;
162  case 3:
163  triple.setArchName("aarch64");
164  break;
165  case 4:
166  triple.setArchName("mips64");
167  break;
168  case 5:
169  triple.setArchName("hexagon");
170  break;
171  case 6:
172  triple.setArchName("mips");
173  break;
174  case 7:
175  triple.setArchName("mips64el");
176  break;
177  case 8:
178  triple.setArchName("mipsel");
179  break;
180  case 9:
181  triple.setArchName("s390x");
182  break;
183  default:
184  return false;
185  }
186  // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
187  // vendor by calling triple.SetVendorName("unknown") so that it is a
188  // "unspecified unknown". This means when someone calls
189  // triple.GetVendorName() it will return an empty string which indicates
190  // that the vendor can be set when two architectures are merged
191 
192  // Now set the triple into "arch" and return true
193  arch.SetTriple(triple);
194  return true;
195  }
196  return false;
197 }
198 
200  Platform::GetStatus(strm);
201 
202 #if LLDB_ENABLE_POSIX
203  // Display local kernel information only when we are running in host mode.
204  // Otherwise, we would end up printing non-Linux information (when running on
205  // Mac OS for example).
206  if (IsHost()) {
207  struct utsname un;
208 
209  if (uname(&un))
210  return;
211 
212  strm.Printf(" Kernel: %s\n", un.sysname);
213  strm.Printf(" Release: %s\n", un.release);
214  strm.Printf(" Version: %s\n", un.version);
215  }
216 #endif
217 }
218 
219 uint32_t
221  uint32_t resume_count = 0;
222 
223  // Always resume past the initial stop when we use eLaunchFlagDebug
224  if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
225  // Resume past the stop for the final exec into the true inferior.
226  ++resume_count;
227  }
228 
229  // If we're not launching a shell, we're done.
230  const FileSpec &shell = launch_info.GetShell();
231  if (!shell)
232  return resume_count;
233 
234  std::string shell_string = shell.GetPath();
235  // We're in a shell, so for sure we have to resume past the shell exec.
236  ++resume_count;
237 
238  // Figure out what shell we're planning on using.
239  const char *shell_name = strrchr(shell_string.c_str(), '/');
240  if (shell_name == nullptr)
241  shell_name = shell_string.c_str();
242  else
243  shell_name++;
244 
245  if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
246  strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
247  // These shells seem to re-exec themselves. Add another resume.
248  ++resume_count;
249  }
250 
251  return resume_count;
252 }
253 
255  if (IsHost()) {
256  return true;
257  } else {
258  // If we're connected, we can debug.
259  return IsConnected();
260  }
261 }
262 
264  m_trap_handlers.push_back(ConstString("_sigtramp"));
265  m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));
266  m_trap_handlers.push_back(ConstString("__restore_rt"));
267 }
268 
270  addr_t addr, addr_t length,
271  unsigned prot, unsigned flags,
272  addr_t fd, addr_t offset) {
273  uint64_t flags_platform = 0;
274  uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON;
275 
276  if (flags & eMmapFlagsPrivate)
277  flags_platform |= MAP_PRIVATE;
278  if (flags & eMmapFlagsAnon)
279  flags_platform |= map_anon;
280 
281  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
282  return args;
283 }
284 
lldb_private::RemoteAwarePlatform::IsConnected
bool IsConnected() const override
Definition: RemoteAwarePlatform.cpp:380
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::ArchSpec::IsMIPS
bool IsMIPS() const
if MIPS architecture return true.
Definition: ArchSpec.cpp:545
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:59
MAP_ANON
#define MAP_ANON
Definition: PlatformLinux.cpp:31
lldb_private::platform_linux::PlatformLinux::GetStatus
void GetStatus(Stream &strm) override
Report the current status for this platform.
Definition: PlatformLinux.cpp:199
lldb_private::platform_linux::PlatformLinux::CalculateTrapHandlerSymbolNames
void CalculateTrapHandlerSymbolNames() override
Ask the Platform subclass to fill in the list of trap handler names.
Definition: PlatformLinux.cpp:263
LIBLLDB_LOG_PLATFORM
#define LIBLLDB_LOG_PLATFORM
Definition: Logging.h:39
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:905
lldb_private::Flags::Test
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
lldb_private::ProcessLaunchInfo::GetFlags
Flags & GetFlags()
Definition: ProcessLaunchInfo.h:64
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
Debugger.h
Process.h
Target.h
lldb_private::FileSpec
Definition: FileSpec.h:56
PlatformPOSIX
Definition: PlatformPOSIX.h:18
lldb_private::ProcessLaunchInfo
Definition: ProcessLaunchInfo.h:31
Log.h
lldb_private::ArchSpec::SetTriple
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:739
lldb_private::RemoteAwarePlatform::m_remote_platform_sp
lldb::PlatformSP m_remote_platform_sp
Definition: RemoteAwarePlatform.h:104
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::platform_linux::PlatformLinux::GetResumeCountForLaunchInfo
uint32_t GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) override
Definition: PlatformLinux.cpp:220
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:149
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
HostInfo.h
lldb_private::platform_linux::PlatformLinux
Definition: PlatformLinux.h:17
MAP_PRIVATE
#define MAP_PRIVATE
Definition: PlatformLinux.cpp:30
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
lldb_private::Platform::Initialize
static void Initialize()
Definition: Platform.cpp:147
PlatformLinux.h
lldb_private::platform_linux::PlatformLinux::GetMmapArgumentList
MmapArgList GetMmapArgumentList(const ArchSpec &arch, lldb::addr_t addr, lldb::addr_t length, unsigned prot, unsigned flags, lldb::addr_t fd, lldb::addr_t offset) override
Definition: PlatformLinux.cpp:269
uint32_t
lldb_private::platform_linux::PlatformLinux::CanDebugProcess
bool CanDebugProcess() override
Not all platforms will support debugging a process by spawning somehow halted for a debugger (specifi...
Definition: PlatformLinux.cpp:254
lldb_private::ProcessLaunchInfo::GetShell
const FileSpec & GetShell() const
Definition: ProcessLaunchInfo.cpp:141
PluginManager.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::eMmapFlagsAnon
@ eMmapFlagsAnon
Definition: Platform.h:40
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformLinux.cpp:39
FileSpec.h
State.h
lldb_private::Platform::GetStatus
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:407
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:49
lldb_private::Platform::IsHost
bool IsHost() const
Definition: Platform.h:442
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb_private::eMmapFlagsPrivate
@ eMmapFlagsPrivate
Definition: Platform.h:40
lldb
Definition: SBAddress.h:15
lldb_private::platform_linux::PlatformLinux::GetSupportedArchitectureAtIndex
bool GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformLinux.cpp:127
lldb_private::platform_linux
Definition: PlatformLinux.h:15