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 <cstdio>
13#if LLDB_ENABLE_POSIX
14#include <sys/utsname.h>
15#endif
16
18#include "lldb/Core/Debugger.h"
20#include "lldb/Host/HostInfo.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
26#include "lldb/Utility/Log.h"
27#include "lldb/Utility/State.h"
28#include "lldb/Utility/Status.h"
30
31// Define these constants from Linux mman.h for use when targeting remote linux
32// systems even when host has different values.
33#define MAP_PRIVATE 2
34#define MAP_ANON 0x20
35
36using namespace lldb;
37using namespace lldb_private;
38using namespace lldb_private::platform_linux;
39
41
42static uint32_t g_initialize_count = 0;
43
44
45PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
46 Log *log = GetLog(LLDBLog::Platform);
47 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
48 arch ? arch->GetArchitectureName() : "<null>",
49 arch ? arch->GetTriple().getTriple() : "<null>");
50
51 bool create = force;
52 if (!create && arch && arch->IsValid()) {
53 const llvm::Triple &triple = arch->GetTriple();
54 switch (triple.getOS()) {
55 case llvm::Triple::Linux:
56 create = true;
57 break;
58
59#if defined(__linux__)
60 // Only accept "unknown" for the OS if the host is linux and it "unknown"
61 // wasn't specified (it was just returned because it was NOT specified)
62 case llvm::Triple::OSType::UnknownOS:
63 create = !arch->TripleOSWasSpecified();
64 break;
65#endif
66 default:
67 break;
68 }
69 }
70
71 LLDB_LOG(log, "create = {0}", create);
72 if (create) {
73 return PlatformSP(new PlatformLinux(false));
74 }
75 return PlatformSP();
76}
77
78llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
79 if (is_host)
80 return "Local Linux user platform plug-in.";
81 return "Remote Linux user platform plug-in.";
82}
83
86
87 if (g_initialize_count++ == 0) {
88#if defined(__linux__) && !defined(__ANDROID__)
89 PlatformSP default_platform_sp(new PlatformLinux(true));
90 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
91 Platform::SetHostPlatform(default_platform_sp);
92#endif
97 }
98}
99
101 if (g_initialize_count > 0) {
102 if (--g_initialize_count == 0) {
104 }
105 }
106
108}
109
110/// Default Constructor
112 : PlatformPOSIX(is_host) // This is the local host platform
113{
114 if (is_host) {
115 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
116 m_supported_architectures.push_back(hostArch);
117 if (hostArch.GetTriple().isArch64Bit()) {
119 HostInfo::GetArchitecture(HostInfo::eArchKind32));
120 }
121 } else {
123 {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::arm,
124 llvm::Triple::aarch64, llvm::Triple::mips64, llvm::Triple::mips64,
125 llvm::Triple::hexagon, llvm::Triple::mips, llvm::Triple::mips64el,
126 llvm::Triple::mipsel, llvm::Triple::msp430, llvm::Triple::systemz},
127 llvm::Triple::Linux);
128 }
129}
130
131std::vector<ArchSpec>
134 return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
136}
137
140
141#if LLDB_ENABLE_POSIX
142 // Display local kernel information only when we are running in host mode.
143 // Otherwise, we would end up printing non-Linux information (when running on
144 // Mac OS for example).
145 if (IsHost()) {
146 struct utsname un;
147
148 if (uname(&un))
149 return;
150
151 strm.Printf(" Kernel: %s\n", un.sysname);
152 strm.Printf(" Release: %s\n", un.release);
153 strm.Printf(" Version: %s\n", un.version);
154 }
155#endif
156}
157
158uint32_t
160 uint32_t resume_count = 0;
161
162 // Always resume past the initial stop when we use eLaunchFlagDebug
163 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
164 // Resume past the stop for the final exec into the true inferior.
165 ++resume_count;
166 }
167
168 // If we're not launching a shell, we're done.
169 const FileSpec &shell = launch_info.GetShell();
170 if (!shell)
171 return resume_count;
172
173 std::string shell_string = shell.GetPath();
174 // We're in a shell, so for sure we have to resume past the shell exec.
175 ++resume_count;
176
177 // Figure out what shell we're planning on using.
178 const char *shell_name = strrchr(shell_string.c_str(), '/');
179 if (shell_name == nullptr)
180 shell_name = shell_string.c_str();
181 else
182 shell_name++;
183
184 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
185 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
186 // These shells seem to re-exec themselves. Add another resume.
187 ++resume_count;
188 }
189
190 return resume_count;
191}
192
194 if (IsHost()) {
195 return true;
196 } else {
197 // If we're connected, we can debug.
198 return IsConnected();
199 }
200}
201
203 m_trap_handlers.push_back(ConstString("_sigtramp"));
204 m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));
205 m_trap_handlers.push_back(ConstString("__restore_rt"));
206}
207
209 UnwindPlanSP unwind_plan_sp;
210 if (name != "__kernel_rt_sigreturn")
211 return unwind_plan_sp;
212
213 UnwindPlan::RowSP row = std::make_shared<UnwindPlan::Row>();
214 row->SetOffset(0);
215
216 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
217 // - 128-byte siginfo struct
218 // - ucontext struct:
219 // - 8-byte long (uc_flags)
220 // - 8-byte pointer (uc_link)
221 // - 24-byte stack_t
222 // - 128-byte signal set
223 // - 8 bytes of padding because sigcontext has 16-byte alignment
224 // - sigcontext/mcontext_t
225 // [1]
226 // https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
227 int32_t offset = 128 + 8 + 8 + 24 + 128 + 8;
228 // Then sigcontext[2] is:
229 // - 8 byte fault address
230 // - 31 8 byte registers
231 // - 8 byte sp
232 // - 8 byte pc
233 // [2]
234 // https://github.com/torvalds/linux/blob/master/arch/arm64/include/uapi/asm/sigcontext.h
235
236 // Skip fault address
237 offset += 8;
238 row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, offset);
239
240 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x0, 0 * 8, false);
241 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x1, 1 * 8, false);
242 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x2, 2 * 8, false);
243 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x3, 3 * 8, false);
244 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x4, 4 * 8, false);
245 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x5, 5 * 8, false);
246 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x6, 6 * 8, false);
247 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x7, 7 * 8, false);
248 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x8, 8 * 8, false);
249 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x9, 9 * 8, false);
250 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x10, 10 * 8, false);
251 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x11, 11 * 8, false);
252 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x12, 12 * 8, false);
253 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x13, 13 * 8, false);
254 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x14, 14 * 8, false);
255 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x15, 15 * 8, false);
256 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x16, 16 * 8, false);
257 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x17, 17 * 8, false);
258 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x18, 18 * 8, false);
259 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x19, 19 * 8, false);
260 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x20, 20 * 8, false);
261 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x21, 21 * 8, false);
262 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x22, 22 * 8, false);
263 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x23, 23 * 8, false);
264 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x24, 24 * 8, false);
265 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x25, 25 * 8, false);
266 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x26, 26 * 8, false);
267 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x27, 27 * 8, false);
268 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x28, 28 * 8, false);
269 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::fp, 29 * 8, false);
270 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x30, 30 * 8, false);
271 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::sp, 31 * 8, false);
272 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::pc, 32 * 8, false);
273
274 // The sigcontext may also contain floating point and SVE registers.
275 // However this would require a dynamic unwind plan so they are not included
276 // here.
277
278 unwind_plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF);
279 unwind_plan_sp->AppendRow(row);
280 unwind_plan_sp->SetSourceName("AArch64 Linux sigcontext");
281 unwind_plan_sp->SetSourcedFromCompiler(eLazyBoolYes);
282 // Because sp is the same throughout the function
283 unwind_plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
284 unwind_plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolYes);
285
286 return unwind_plan_sp;
287}
288
291 ConstString name) {
292 if (triple.isAArch64())
294
295 return {};
296}
297
299 addr_t addr, addr_t length,
300 unsigned prot, unsigned flags,
301 addr_t fd, addr_t offset) {
302 uint64_t flags_platform = 0;
303 uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON;
304
305 if (flags & eMmapFlagsPrivate)
306 flags_platform |= MAP_PRIVATE;
307 if (flags & eMmapFlagsAnon)
308 flags_platform |= map_anon;
309
310 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
311 return args;
312}
313
314CompilerType PlatformLinux::GetSiginfoType(const llvm::Triple &triple) {
315 {
316 std::lock_guard<std::mutex> guard(m_mutex);
317 if (!m_type_system)
318 m_type_system = std::make_shared<TypeSystemClang>("siginfo", triple);
319 }
320 TypeSystemClang *ast = m_type_system.get();
321
322 bool si_errno_then_code = true;
323
324 switch (triple.getArch()) {
325 case llvm::Triple::mips:
326 case llvm::Triple::mipsel:
327 case llvm::Triple::mips64:
328 case llvm::Triple::mips64el:
329 // mips has si_code and si_errno swapped
330 si_errno_then_code = false;
331 break;
332 default:
333 break;
334 }
335
336 // generic types
337 CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
339 CompilerType short_type = ast->GetBasicType(eBasicTypeShort);
340 CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
342
343 // platform-specific types
344 CompilerType &pid_type = int_type;
345 CompilerType &uid_type = uint_type;
346 CompilerType &clock_type = long_type;
347 CompilerType &band_type = long_type;
348
349 CompilerType sigval_type = ast->CreateRecordType(
350 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
351 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
352 ast->StartTagDeclarationDefinition(sigval_type);
353 ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
355 ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
357 ast->CompleteTagDeclarationDefinition(sigval_type);
358
359 CompilerType sigfault_bounds_type = ast->CreateRecordType(
361 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
362 ast->StartTagDeclarationDefinition(sigfault_bounds_type);
364 sigfault_bounds_type, "_addr_bnd",
365 ast->CreateStructForIdentifier(llvm::StringRef(),
366 {
367 {"_lower", voidp_type},
368 {"_upper", voidp_type},
369 }),
371 ast->AddFieldToRecordType(sigfault_bounds_type, "_pkey", uint_type,
373 ast->CompleteTagDeclarationDefinition(sigfault_bounds_type);
374
375 // siginfo_t
376 CompilerType siginfo_type = ast->CreateRecordType(
377 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
378 llvm::to_underlying(clang::TagTypeKind::Struct), lldb::eLanguageTypeC);
379 ast->StartTagDeclarationDefinition(siginfo_type);
380 ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
382
383 if (si_errno_then_code) {
384 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
386 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
388 } else {
389 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
391 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
393 }
394
395 // the structure is padded on 64-bit arches to fix alignment
396 if (triple.isArch64Bit())
397 ast->AddFieldToRecordType(siginfo_type, "__pad0", int_type,
399
400 // union used to hold the signal data
401 CompilerType union_type = ast->CreateRecordType(
403 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
404 ast->StartTagDeclarationDefinition(union_type);
405
406 ast->AddFieldToRecordType(
407 union_type, "_kill",
408 ast->CreateStructForIdentifier(llvm::StringRef(),
409 {
410 {"si_pid", pid_type},
411 {"si_uid", uid_type},
412 }),
414
415 ast->AddFieldToRecordType(
416 union_type, "_timer",
417 ast->CreateStructForIdentifier(llvm::StringRef(),
418 {
419 {"si_tid", int_type},
420 {"si_overrun", int_type},
421 {"si_sigval", sigval_type},
422 }),
424
425 ast->AddFieldToRecordType(
426 union_type, "_rt",
427 ast->CreateStructForIdentifier(llvm::StringRef(),
428 {
429 {"si_pid", pid_type},
430 {"si_uid", uid_type},
431 {"si_sigval", sigval_type},
432 }),
434
435 ast->AddFieldToRecordType(
436 union_type, "_sigchld",
437 ast->CreateStructForIdentifier(llvm::StringRef(),
438 {
439 {"si_pid", pid_type},
440 {"si_uid", uid_type},
441 {"si_status", int_type},
442 {"si_utime", clock_type},
443 {"si_stime", clock_type},
444 }),
446
447 ast->AddFieldToRecordType(
448 union_type, "_sigfault",
449 ast->CreateStructForIdentifier(llvm::StringRef(),
450 {
451 {"si_addr", voidp_type},
452 {"si_addr_lsb", short_type},
453 {"_bounds", sigfault_bounds_type},
454 }),
456
457 ast->AddFieldToRecordType(
458 union_type, "_sigpoll",
459 ast->CreateStructForIdentifier(llvm::StringRef(),
460 {
461 {"si_band", band_type},
462 {"si_fd", int_type},
463 }),
465
466 // NB: SIGSYS is not present on ia64 but we don't seem to support that
467 ast->AddFieldToRecordType(
468 union_type, "_sigsys",
469 ast->CreateStructForIdentifier(llvm::StringRef(),
470 {
471 {"_call_addr", voidp_type},
472 {"_syscall", int_type},
473 {"_arch", uint_type},
474 }),
476
477 ast->CompleteTagDeclarationDefinition(union_type);
478 ast->AddFieldToRecordType(siginfo_type, "_sifields", union_type,
480
481 ast->CompleteTagDeclarationDefinition(siginfo_type);
482 return siginfo_type;
483}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:369
#define MAP_ANON
#define MAP_PRIVATE
static uint32_t g_initialize_count
static lldb::UnwindPlanSP GetAArch64TrapHandlerUnwindPlan(ConstString name)
static uint32_t g_initialize_count
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
An architecture specification class.
Definition: ArchSpec.h:31
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:461
bool IsMIPS() const
if MIPS architecture return true.
Definition: ArchSpec.cpp:577
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
Definition: ConstString.h:40
A file utility class.
Definition: FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:1021
static void Terminate()
Definition: Platform.cpp:138
static void SetHostPlatform(const lldb::PlatformSP &platform_sp)
Definition: Platform.cpp:145
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:248
static void Initialize()
Definition: Platform.cpp:136
static std::vector< ArchSpec > CreateArchList(llvm::ArrayRef< llvm::Triple::ArchType > archs, llvm::Triple::OSType os)
Create a list of ArchSpecs with the given OS and a architectures.
Definition: Platform.cpp:1085
bool IsHost() const
Definition: Platform.h:502
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
const FileSpec & GetShell() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
A TypeSystem implementation based on Clang.
CompilerType GetBasicType(lldb::BasicType type)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, lldb::AccessType access, uint32_t bitfield_bit_size)
CompilerType CreateStructForIdentifier(llvm::StringRef type_name, const std::initializer_list< std::pair< const char *, CompilerType > > &type_fields, bool packed=false)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef name, int kind, lldb::LanguageType language, std::optional< ClangASTMetadata > metadata=std::nullopt, bool exports_symbols=false)
static bool StartTagDeclarationDefinition(const CompilerType &type)
std::shared_ptr< Row > RowSP
Definition: UnwindPlan.h:429
void GetStatus(Stream &strm) override
Report the current status for this platform.
void CalculateTrapHandlerSymbolNames() override
Ask the Platform subclass to fill in the list of trap handler names.
PlatformLinux(bool is_host)
Default Constructor.
std::vector< ArchSpec > GetSupportedArchitectures(const ArchSpec &process_host_arch) override
Get the platform's supported architectures in the order in which they should be searched.
std::shared_ptr< TypeSystemClang > m_type_system
Definition: PlatformLinux.h:69
static llvm::StringRef GetPluginDescriptionStatic(bool is_host)
static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch)
lldb::UnwindPlanSP GetTrapHandlerUnwindPlan(const llvm::Triple &triple, ConstString name) override
Try to get a specific unwind plan for a named trap handler.
static llvm::StringRef GetPluginNameStatic(bool is_host)
Definition: PlatformLinux.h:29
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
uint32_t GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) override
std::vector< ArchSpec > m_supported_architectures
Definition: PlatformLinux.h:65
bool CanDebugProcess() override
Not all platforms will support debugging a process by spawning somehow halted for a debugger (specifi...
CompilerType GetSiginfoType(const llvm::Triple &triple) override
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:63
@ eMmapFlagsPrivate
Definition: Platform.h:45
@ eMmapFlagsAnon
Definition: Platform.h:45
Definition: SBAddress.h:15
@ eBasicTypeUnsignedInt
std::shared_ptr< lldb_private::Platform > PlatformSP
Definition: lldb-forward.h:388
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
Definition: lldb-forward.h:483
uint64_t addr_t
Definition: lldb-types.h:80
@ eRegisterKindDWARF
the register numbers seen DWARF