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
17#include "Utility/ARM64_DWARF_Registers.h"
18#include "lldb/Core/Debugger.h"
19#include "lldb/Core/PluginManager.h"
20#include "lldb/Host/HostInfo.h"
21#include "lldb/Symbol/UnwindPlan.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Utility/FileSpec.h"
25#include "lldb/Utility/LLDBLog.h"
26#include "lldb/Utility/Log.h"
27#include "lldb/Utility/State.h"
28#include "lldb/Utility/Status.h"
29#include "lldb/Utility/StreamString.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
40LLDB_PLUGIN_DEFINE(PlatformLinux)
41
42static uint32_t g_initialize_count = 0;
43
44
45PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
46 Log *log = GetLog(mask: 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
84void PlatformLinux::Initialize() {
85 PlatformPOSIX::Initialize();
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
93 PluginManager::RegisterPlugin(
94 name: PlatformLinux::GetPluginNameStatic(is_host: false),
95 description: PlatformLinux::GetPluginDescriptionStatic(is_host: false),
96 create_callback: PlatformLinux::CreateInstance, debugger_init_callback: nullptr);
97 }
98}
99
100void PlatformLinux::Terminate() {
101 if (g_initialize_count > 0) {
102 if (--g_initialize_count == 0) {
103 PluginManager::UnregisterPlugin(create_callback: PlatformLinux::CreateInstance);
104 }
105 }
106
107 PlatformPOSIX::Terminate();
108}
109
110/// Default Constructor
111PlatformLinux::PlatformLinux(bool is_host)
112 : PlatformPOSIX(is_host) // This is the local host platform
113{
114 if (is_host) {
115 ArchSpec hostArch = HostInfo::GetArchitecture(arch_kind: HostInfo::eArchKindDefault);
116 m_supported_architectures.push_back(x: hostArch);
117 if (hostArch.GetTriple().isArch64Bit()) {
118 m_supported_architectures.push_back(
119 x: HostInfo::GetArchitecture(arch_kind: HostInfo::eArchKind32));
120 }
121 } else {
122 m_supported_architectures = CreateArchList(
123 archs: {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 os: llvm::Triple::Linux);
128 }
129}
130
131std::vector<ArchSpec>
132PlatformLinux::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
133 if (m_remote_platform_sp)
134 return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
135 return m_supported_architectures;
136}
137
138void PlatformLinux::GetStatus(Stream &strm) {
139 Platform::GetStatus(strm);
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(name: &un))
149 return;
150
151 strm.Printf(format: " Kernel: %s\n", un.sysname);
152 strm.Printf(format: " Release: %s\n", un.release);
153 strm.Printf(format: " Version: %s\n", un.version);
154 }
155#endif
156}
157
158uint32_t
159PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
160 uint32_t resume_count = 0;
161
162 // Always resume past the initial stop when we use eLaunchFlagDebug
163 if (launch_info.GetFlags().Test(bit: 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(s: shell_string.c_str(), c: '/');
179 if (shell_name == nullptr)
180 shell_name = shell_string.c_str();
181 else
182 shell_name++;
183
184 if (strcmp(s1: shell_name, s2: "csh") == 0 || strcmp(s1: shell_name, s2: "tcsh") == 0 ||
185 strcmp(s1: shell_name, s2: "zsh") == 0 || strcmp(s1: shell_name, s2: "sh") == 0) {
186 // These shells seem to re-exec themselves. Add another resume.
187 ++resume_count;
188 }
189
190 return resume_count;
191}
192
193bool PlatformLinux::CanDebugProcess() {
194 if (IsHost()) {
195 return true;
196 } else {
197 // If we're connected, we can debug.
198 return IsConnected();
199 }
200}
201
202void PlatformLinux::CalculateTrapHandlerSymbolNames() {
203 m_trap_handlers.push_back(x: ConstString("_sigtramp"));
204 m_trap_handlers.push_back(x: ConstString("__kernel_rt_sigreturn"));
205 m_trap_handlers.push_back(x: ConstString("__restore_rt"));
206}
207
208static lldb::UnwindPlanSP GetAArch64TrapHandlerUnwindPlan(ConstString name) {
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(reg_num: arm64_dwarf::sp, offset);
239
240 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x0, offset: 0 * 8, can_replace: false);
241 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x1, offset: 1 * 8, can_replace: false);
242 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x2, offset: 2 * 8, can_replace: false);
243 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x3, offset: 3 * 8, can_replace: false);
244 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x4, offset: 4 * 8, can_replace: false);
245 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x5, offset: 5 * 8, can_replace: false);
246 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x6, offset: 6 * 8, can_replace: false);
247 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x7, offset: 7 * 8, can_replace: false);
248 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x8, offset: 8 * 8, can_replace: false);
249 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x9, offset: 9 * 8, can_replace: false);
250 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x10, offset: 10 * 8, can_replace: false);
251 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x11, offset: 11 * 8, can_replace: false);
252 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x12, offset: 12 * 8, can_replace: false);
253 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x13, offset: 13 * 8, can_replace: false);
254 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x14, offset: 14 * 8, can_replace: false);
255 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x15, offset: 15 * 8, can_replace: false);
256 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x16, offset: 16 * 8, can_replace: false);
257 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x17, offset: 17 * 8, can_replace: false);
258 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x18, offset: 18 * 8, can_replace: false);
259 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x19, offset: 19 * 8, can_replace: false);
260 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x20, offset: 20 * 8, can_replace: false);
261 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x21, offset: 21 * 8, can_replace: false);
262 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x22, offset: 22 * 8, can_replace: false);
263 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x23, offset: 23 * 8, can_replace: false);
264 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x24, offset: 24 * 8, can_replace: false);
265 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x25, offset: 25 * 8, can_replace: false);
266 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x26, offset: 26 * 8, can_replace: false);
267 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x27, offset: 27 * 8, can_replace: false);
268 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x28, offset: 28 * 8, can_replace: false);
269 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::fp, offset: 29 * 8, can_replace: false);
270 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::x30, offset: 30 * 8, can_replace: false);
271 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::sp, offset: 31 * 8, can_replace: false);
272 row->SetRegisterLocationToAtCFAPlusOffset(reg_num: arm64_dwarf::pc, offset: 32 * 8, can_replace: 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>(args: eRegisterKindDWARF);
279 unwind_plan_sp->AppendRow(row_sp: 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
289lldb::UnwindPlanSP
290PlatformLinux::GetTrapHandlerUnwindPlan(const llvm::Triple &triple,
291 ConstString name) {
292 if (triple.isAArch64())
293 return GetAArch64TrapHandlerUnwindPlan(name);
294
295 return {};
296}
297
298MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch,
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>(args: "siginfo", args: 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(type: eBasicTypeInt);
338 CompilerType uint_type = ast->GetBasicType(type: eBasicTypeUnsignedInt);
339 CompilerType short_type = ast->GetBasicType(type: eBasicTypeShort);
340 CompilerType long_type = ast->GetBasicType(type: eBasicTypeLong);
341 CompilerType voidp_type = ast->GetBasicType(type: eBasicTypeVoid).GetPointerType();
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 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "__lldb_sigval_t",
351 kind: llvm::to_underlying(E: clang::TagTypeKind::Union), language: lldb::eLanguageTypeC);
352 ast->StartTagDeclarationDefinition(type: sigval_type);
353 ast->AddFieldToRecordType(type: sigval_type, name: "sival_int", field_type: int_type,
354 access: lldb::eAccessPublic, bitfield_bit_size: 0);
355 ast->AddFieldToRecordType(type: sigval_type, name: "sival_ptr", field_type: voidp_type,
356 access: lldb::eAccessPublic, bitfield_bit_size: 0);
357 ast->CompleteTagDeclarationDefinition(type: sigval_type);
358
359 CompilerType sigfault_bounds_type = ast->CreateRecordType(
360 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "",
361 kind: llvm::to_underlying(E: clang::TagTypeKind::Union), language: lldb::eLanguageTypeC);
362 ast->StartTagDeclarationDefinition(type: sigfault_bounds_type);
363 ast->AddFieldToRecordType(
364 type: sigfault_bounds_type, name: "_addr_bnd",
365 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
366 type_fields: {
367 {"_lower", voidp_type},
368 {"_upper", voidp_type},
369 }),
370 access: lldb::eAccessPublic, bitfield_bit_size: 0);
371 ast->AddFieldToRecordType(type: sigfault_bounds_type, name: "_pkey", field_type: uint_type,
372 access: lldb::eAccessPublic, bitfield_bit_size: 0);
373 ast->CompleteTagDeclarationDefinition(type: sigfault_bounds_type);
374
375 // siginfo_t
376 CompilerType siginfo_type = ast->CreateRecordType(
377 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "__lldb_siginfo_t",
378 kind: llvm::to_underlying(E: clang::TagTypeKind::Struct), language: lldb::eLanguageTypeC);
379 ast->StartTagDeclarationDefinition(type: siginfo_type);
380 ast->AddFieldToRecordType(type: siginfo_type, name: "si_signo", field_type: int_type,
381 access: lldb::eAccessPublic, bitfield_bit_size: 0);
382
383 if (si_errno_then_code) {
384 ast->AddFieldToRecordType(type: siginfo_type, name: "si_errno", field_type: int_type,
385 access: lldb::eAccessPublic, bitfield_bit_size: 0);
386 ast->AddFieldToRecordType(type: siginfo_type, name: "si_code", field_type: int_type,
387 access: lldb::eAccessPublic, bitfield_bit_size: 0);
388 } else {
389 ast->AddFieldToRecordType(type: siginfo_type, name: "si_code", field_type: int_type,
390 access: lldb::eAccessPublic, bitfield_bit_size: 0);
391 ast->AddFieldToRecordType(type: siginfo_type, name: "si_errno", field_type: int_type,
392 access: lldb::eAccessPublic, bitfield_bit_size: 0);
393 }
394
395 // the structure is padded on 64-bit arches to fix alignment
396 if (triple.isArch64Bit())
397 ast->AddFieldToRecordType(type: siginfo_type, name: "__pad0", field_type: int_type,
398 access: lldb::eAccessPublic, bitfield_bit_size: 0);
399
400 // union used to hold the signal data
401 CompilerType union_type = ast->CreateRecordType(
402 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "",
403 kind: llvm::to_underlying(E: clang::TagTypeKind::Union), language: lldb::eLanguageTypeC);
404 ast->StartTagDeclarationDefinition(type: union_type);
405
406 ast->AddFieldToRecordType(
407 type: union_type, name: "_kill",
408 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
409 type_fields: {
410 {"si_pid", pid_type},
411 {"si_uid", uid_type},
412 }),
413 access: lldb::eAccessPublic, bitfield_bit_size: 0);
414
415 ast->AddFieldToRecordType(
416 type: union_type, name: "_timer",
417 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
418 type_fields: {
419 {"si_tid", int_type},
420 {"si_overrun", int_type},
421 {"si_sigval", sigval_type},
422 }),
423 access: lldb::eAccessPublic, bitfield_bit_size: 0);
424
425 ast->AddFieldToRecordType(
426 type: union_type, name: "_rt",
427 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
428 type_fields: {
429 {"si_pid", pid_type},
430 {"si_uid", uid_type},
431 {"si_sigval", sigval_type},
432 }),
433 access: lldb::eAccessPublic, bitfield_bit_size: 0);
434
435 ast->AddFieldToRecordType(
436 type: union_type, name: "_sigchld",
437 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
438 type_fields: {
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 }),
445 access: lldb::eAccessPublic, bitfield_bit_size: 0);
446
447 ast->AddFieldToRecordType(
448 type: union_type, name: "_sigfault",
449 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
450 type_fields: {
451 {"si_addr", voidp_type},
452 {"si_addr_lsb", short_type},
453 {"_bounds", sigfault_bounds_type},
454 }),
455 access: lldb::eAccessPublic, bitfield_bit_size: 0);
456
457 ast->AddFieldToRecordType(
458 type: union_type, name: "_sigpoll",
459 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
460 type_fields: {
461 {"si_band", band_type},
462 {"si_fd", int_type},
463 }),
464 access: lldb::eAccessPublic, bitfield_bit_size: 0);
465
466 // NB: SIGSYS is not present on ia64 but we don't seem to support that
467 ast->AddFieldToRecordType(
468 type: union_type, name: "_sigsys",
469 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
470 type_fields: {
471 {"_call_addr", voidp_type},
472 {"_syscall", int_type},
473 {"_arch", uint_type},
474 }),
475 access: lldb::eAccessPublic, bitfield_bit_size: 0);
476
477 ast->CompleteTagDeclarationDefinition(type: union_type);
478 ast->AddFieldToRecordType(type: siginfo_type, name: "_sifields", field_type: union_type,
479 access: lldb::eAccessPublic, bitfield_bit_size: 0);
480
481 ast->CompleteTagDeclarationDefinition(type: siginfo_type);
482 return siginfo_type;
483}
484

source code of lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp