LLDB mainline
MinidumpFileBuilder.cpp
Go to the documentation of this file.
1//===-- MinidumpFileBuilder.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
10
13
14#include "lldb/Core/Module.h"
16#include "lldb/Core/Section.h"
18#include "lldb/Target/Process.h"
24#include "lldb/Utility/Log.h"
26
27#include "llvm/ADT/StringRef.h"
28#include "llvm/BinaryFormat/Minidump.h"
29#include "llvm/Support/ConvertUTF.h"
30#include "llvm/Support/Error.h"
31
33
34#include <cinttypes>
35
36using namespace lldb;
37using namespace lldb_private;
38using namespace llvm::minidump;
39
40void MinidumpFileBuilder::AddDirectory(StreamType type, size_t stream_size) {
41 LocationDescriptor loc;
42 loc.DataSize = static_cast<llvm::support::ulittle32_t>(stream_size);
43 // Stream will begin at the current end of data section
44 loc.RVA = static_cast<llvm::support::ulittle32_t>(GetCurrentDataEndOffset());
45
46 Directory dir;
47 dir.Type = static_cast<llvm::support::little_t<StreamType>>(type);
48 dir.Location = loc;
49
50 m_directories.push_back(dir);
51}
52
53Status MinidumpFileBuilder::AddSystemInfo(const llvm::Triple &target_triple) {
55 AddDirectory(StreamType::SystemInfo, sizeof(llvm::minidump::SystemInfo));
56
57 llvm::minidump::ProcessorArchitecture arch;
58 switch (target_triple.getArch()) {
59 case llvm::Triple::ArchType::x86_64:
60 arch = ProcessorArchitecture::AMD64;
61 break;
62 case llvm::Triple::ArchType::x86:
63 arch = ProcessorArchitecture::X86;
64 break;
65 case llvm::Triple::ArchType::arm:
66 arch = ProcessorArchitecture::ARM;
67 break;
68 case llvm::Triple::ArchType::aarch64:
69 arch = ProcessorArchitecture::ARM64;
70 break;
71 case llvm::Triple::ArchType::mips64:
72 case llvm::Triple::ArchType::mips64el:
73 case llvm::Triple::ArchType::mips:
74 case llvm::Triple::ArchType::mipsel:
75 arch = ProcessorArchitecture::MIPS;
76 break;
77 case llvm::Triple::ArchType::ppc64:
78 case llvm::Triple::ArchType::ppc:
79 case llvm::Triple::ArchType::ppc64le:
80 arch = ProcessorArchitecture::PPC;
81 break;
82 default:
83 error.SetErrorStringWithFormat("Architecture %s not supported.",
84 target_triple.getArchName().str().c_str());
85 return error;
86 };
87
88 llvm::support::little_t<OSPlatform> platform_id;
89 switch (target_triple.getOS()) {
90 case llvm::Triple::OSType::Linux:
91 if (target_triple.getEnvironment() ==
92 llvm::Triple::EnvironmentType::Android)
93 platform_id = OSPlatform::Android;
94 else
95 platform_id = OSPlatform::Linux;
96 break;
97 case llvm::Triple::OSType::Win32:
98 platform_id = OSPlatform::Win32NT;
99 break;
100 case llvm::Triple::OSType::MacOSX:
101 platform_id = OSPlatform::MacOSX;
102 break;
103 case llvm::Triple::OSType::IOS:
104 platform_id = OSPlatform::IOS;
105 break;
106 default:
107 error.SetErrorStringWithFormat("OS %s not supported.",
108 target_triple.getOSName().str().c_str());
109 return error;
110 };
111
112 llvm::minidump::SystemInfo sys_info;
113 sys_info.ProcessorArch =
114 static_cast<llvm::support::little_t<ProcessorArchitecture>>(arch);
115 // Global offset to beginning of a csd_string in a data section
116 sys_info.CSDVersionRVA = static_cast<llvm::support::ulittle32_t>(
117 GetCurrentDataEndOffset() + sizeof(llvm::minidump::SystemInfo));
118 sys_info.PlatformId = platform_id;
119 m_data.AppendData(&sys_info, sizeof(llvm::minidump::SystemInfo));
120
121 std::string csd_string;
122
123 error = WriteString(csd_string, &m_data);
124 if (error.Fail()) {
125 error.SetErrorString("Unable to convert the csd string to UTF16.");
126 return error;
127 }
128
129 return error;
130}
131
132Status WriteString(const std::string &to_write,
135 // let the StringRef eat also null termination char
136 llvm::StringRef to_write_ref(to_write.c_str(), to_write.size() + 1);
137 llvm::SmallVector<llvm::UTF16, 128> to_write_utf16;
138
139 bool converted = convertUTF8ToUTF16String(to_write_ref, to_write_utf16);
140 if (!converted) {
141 error.SetErrorStringWithFormat(
142 "Unable to convert the string to UTF16. Failed to convert %s",
143 to_write.c_str());
144 return error;
145 }
146
147 // size of the UTF16 string should be written without the null termination
148 // character that is stored in 2 bytes
149 llvm::support::ulittle32_t to_write_size(to_write_utf16.size_in_bytes() - 2);
150
151 buffer->AppendData(&to_write_size, sizeof(llvm::support::ulittle32_t));
152 buffer->AppendData(to_write_utf16.data(), to_write_utf16.size_in_bytes());
153
154 return error;
155}
156
157llvm::Expected<uint64_t> getModuleFileSize(Target &target,
158 const ModuleSP &mod) {
159 // JIT module has the same vm and file size.
160 uint64_t SizeOfImage = 0;
161 if (mod->GetObjectFile()->CalculateType() == ObjectFile::Type::eTypeJIT) {
162 for (const auto &section : *mod->GetObjectFile()->GetSectionList()) {
163 SizeOfImage += section->GetByteSize();
164 }
165 return SizeOfImage;
166 }
167
168 SectionSP sect_sp = mod->GetObjectFile()->GetBaseAddress().GetSection();
169
170 if (!sect_sp) {
171 return llvm::createStringError(std::errc::operation_not_supported,
172 "Couldn't obtain the section information.");
173 }
174 lldb::addr_t sect_addr = sect_sp->GetLoadBaseAddress(&target);
175 // Use memory size since zero fill sections, like ".bss", will be smaller on
176 // disk.
177 lldb::addr_t sect_size = sect_sp->GetByteSize();
178 // This will usually be zero, but make sure to calculate the BaseOfImage
179 // offset.
180 const lldb::addr_t base_sect_offset =
181 mod->GetObjectFile()->GetBaseAddress().GetLoadAddress(&target) -
182 sect_addr;
183 SizeOfImage = sect_size - base_sect_offset;
184 lldb::addr_t next_sect_addr = sect_addr + sect_size;
185 Address sect_so_addr;
186 target.ResolveLoadAddress(next_sect_addr, sect_so_addr);
187 lldb::SectionSP next_sect_sp = sect_so_addr.GetSection();
188 while (next_sect_sp &&
189 next_sect_sp->GetLoadBaseAddress(&target) == next_sect_addr) {
190 sect_size = sect_sp->GetByteSize();
191 SizeOfImage += sect_size;
192 next_sect_addr += sect_size;
193 target.ResolveLoadAddress(next_sect_addr, sect_so_addr);
194 next_sect_sp = sect_so_addr.GetSection();
195 }
196
197 return SizeOfImage;
198}
199
200// ModuleList stream consists of a number of modules, followed by an array
201// of llvm::minidump::Module's structures. Every structure informs about a
202// single module. Additional data of variable length, such as module's names,
203// are stored just after the ModuleList stream. The llvm::minidump::Module
204// structures point to this helper data by global offset.
206 constexpr size_t minidump_module_size = sizeof(llvm::minidump::Module);
208
209 const ModuleList &modules = target.GetImages();
210 llvm::support::ulittle32_t modules_count =
211 static_cast<llvm::support::ulittle32_t>(modules.GetSize());
212
213 // This helps us with getting the correct global offset in minidump
214 // file later, when we will be setting up offsets from the
215 // the llvm::minidump::Module's structures into helper data
216 size_t size_before = GetCurrentDataEndOffset();
217
218 // This is the size of the main part of the ModuleList stream.
219 // It consists of a module number and corresponding number of
220 // structs describing individual modules
221 size_t module_stream_size =
222 sizeof(llvm::support::ulittle32_t) + modules_count * minidump_module_size;
223
224 // Adding directory describing this stream.
225 AddDirectory(StreamType::ModuleList, module_stream_size);
226
227 m_data.AppendData(&modules_count, sizeof(llvm::support::ulittle32_t));
228
229 // Temporary storage for the helper data (of variable length)
230 // as these cannot be dumped to m_data before dumping entire
231 // array of module structures.
232 DataBufferHeap helper_data;
233
234 for (size_t i = 0; i < modules_count; ++i) {
235 ModuleSP mod = modules.GetModuleAtIndex(i);
236 std::string module_name = mod->GetSpecificationDescription();
237 auto maybe_mod_size = getModuleFileSize(target, mod);
238 if (!maybe_mod_size) {
239 llvm::Error mod_size_err = maybe_mod_size.takeError();
240 llvm::handleAllErrors(std::move(mod_size_err),
241 [&](const llvm::ErrorInfoBase &E) {
242 error.SetErrorStringWithFormat(
243 "Unable to get the size of module %s: %s.",
244 module_name.c_str(), E.message().c_str());
245 });
246 return error;
247 }
248
249 uint64_t mod_size = std::move(*maybe_mod_size);
250
251 llvm::support::ulittle32_t signature =
252 static_cast<llvm::support::ulittle32_t>(
253 static_cast<uint32_t>(minidump::CvSignature::ElfBuildId));
254 auto uuid = mod->GetUUID().GetBytes();
255
256 VSFixedFileInfo info;
257 info.Signature = static_cast<llvm::support::ulittle32_t>(0u);
258 info.StructVersion = static_cast<llvm::support::ulittle32_t>(0u);
259 info.FileVersionHigh = static_cast<llvm::support::ulittle32_t>(0u);
260 info.FileVersionLow = static_cast<llvm::support::ulittle32_t>(0u);
261 info.ProductVersionHigh = static_cast<llvm::support::ulittle32_t>(0u);
262 info.ProductVersionLow = static_cast<llvm::support::ulittle32_t>(0u);
263 info.FileFlagsMask = static_cast<llvm::support::ulittle32_t>(0u);
264 info.FileFlags = static_cast<llvm::support::ulittle32_t>(0u);
265 info.FileOS = static_cast<llvm::support::ulittle32_t>(0u);
266 info.FileType = static_cast<llvm::support::ulittle32_t>(0u);
267 info.FileSubtype = static_cast<llvm::support::ulittle32_t>(0u);
268 info.FileDateHigh = static_cast<llvm::support::ulittle32_t>(0u);
269 info.FileDateLow = static_cast<llvm::support::ulittle32_t>(0u);
270
271 LocationDescriptor ld;
272 ld.DataSize = static_cast<llvm::support::ulittle32_t>(0u);
273 ld.RVA = static_cast<llvm::support::ulittle32_t>(0u);
274
275 // Setting up LocationDescriptor for uuid string. The global offset into
276 // minidump file is calculated.
277 LocationDescriptor ld_cv;
278 ld_cv.DataSize = static_cast<llvm::support::ulittle32_t>(
279 sizeof(llvm::support::ulittle32_t) + uuid.size());
280 ld_cv.RVA = static_cast<llvm::support::ulittle32_t>(
281 size_before + module_stream_size + helper_data.GetByteSize());
282
283 helper_data.AppendData(&signature, sizeof(llvm::support::ulittle32_t));
284 helper_data.AppendData(uuid.begin(), uuid.size());
285
286 llvm::minidump::Module m;
287 m.BaseOfImage = static_cast<llvm::support::ulittle64_t>(
288 mod->GetObjectFile()->GetBaseAddress().GetLoadAddress(&target));
289 m.SizeOfImage = static_cast<llvm::support::ulittle32_t>(mod_size);
290 m.Checksum = static_cast<llvm::support::ulittle32_t>(0);
291 m.TimeDateStamp =
292 static_cast<llvm::support::ulittle32_t>(std::time(nullptr));
293 m.ModuleNameRVA = static_cast<llvm::support::ulittle32_t>(
294 size_before + module_stream_size + helper_data.GetByteSize());
295 m.VersionInfo = info;
296 m.CvRecord = ld_cv;
297 m.MiscRecord = ld;
298
299 error = WriteString(module_name, &helper_data);
300
301 if (error.Fail())
302 return error;
303
304 m_data.AppendData(&m, sizeof(llvm::minidump::Module));
305 }
306
307 m_data.AppendData(helper_data.GetBytes(), helper_data.GetByteSize());
308 return error;
309}
310
312 llvm::StringRef reg_name) {
313 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name);
314 if (!reg_info)
315 return 0;
317 bool success = reg_ctx->ReadRegister(reg_info, reg_value);
318 if (!success)
319 return 0;
320 return reg_value.GetAsUInt16();
321}
322
324 llvm::StringRef reg_name) {
325 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name);
326 if (!reg_info)
327 return 0;
329 bool success = reg_ctx->ReadRegister(reg_info, reg_value);
330 if (!success)
331 return 0;
332 return reg_value.GetAsUInt32();
333}
334
336 llvm::StringRef reg_name) {
337 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name);
338 if (!reg_info)
339 return 0;
341 bool success = reg_ctx->ReadRegister(reg_info, reg_value);
342 if (!success)
343 return 0;
344 return reg_value.GetAsUInt64();
345}
346
347llvm::support::ulittle16_t read_register_u16(RegisterContext *reg_ctx,
348 llvm::StringRef reg_name) {
349 return static_cast<llvm::support::ulittle16_t>(
350 read_register_u16_raw(reg_ctx, reg_name));
351}
352
353llvm::support::ulittle32_t read_register_u32(RegisterContext *reg_ctx,
354 llvm::StringRef reg_name) {
355 return static_cast<llvm::support::ulittle32_t>(
356 read_register_u32_raw(reg_ctx, reg_name));
357}
358
359llvm::support::ulittle64_t read_register_u64(RegisterContext *reg_ctx,
360 llvm::StringRef reg_name) {
361 return static_cast<llvm::support::ulittle64_t>(
362 read_register_u64_raw(reg_ctx, reg_name));
363}
364
365void read_register_u128(RegisterContext *reg_ctx, llvm::StringRef reg_name,
366 uint8_t *dst) {
367 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name);
368 if (reg_info) {
370 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
372 uint32_t bytes_copied = reg_value.GetAsMemoryData(
373 *reg_info, dst, 16, lldb::ByteOrder::eByteOrderLittle, error);
374 if (bytes_copied == 16)
375 return;
376 }
377 }
378 // If anything goes wrong, then zero out the register value.
379 memset(dst, 0, 16);
380}
381
385 thread_context.p1_home = {};
386 thread_context.context_flags = static_cast<uint32_t>(
391 thread_context.rax = read_register_u64(reg_ctx, "rax");
392 thread_context.rbx = read_register_u64(reg_ctx, "rbx");
393 thread_context.rcx = read_register_u64(reg_ctx, "rcx");
394 thread_context.rdx = read_register_u64(reg_ctx, "rdx");
395 thread_context.rdi = read_register_u64(reg_ctx, "rdi");
396 thread_context.rsi = read_register_u64(reg_ctx, "rsi");
397 thread_context.rbp = read_register_u64(reg_ctx, "rbp");
398 thread_context.rsp = read_register_u64(reg_ctx, "rsp");
399 thread_context.r8 = read_register_u64(reg_ctx, "r8");
400 thread_context.r9 = read_register_u64(reg_ctx, "r9");
401 thread_context.r10 = read_register_u64(reg_ctx, "r10");
402 thread_context.r11 = read_register_u64(reg_ctx, "r11");
403 thread_context.r12 = read_register_u64(reg_ctx, "r12");
404 thread_context.r13 = read_register_u64(reg_ctx, "r13");
405 thread_context.r14 = read_register_u64(reg_ctx, "r14");
406 thread_context.r15 = read_register_u64(reg_ctx, "r15");
407 thread_context.rip = read_register_u64(reg_ctx, "rip");
408 thread_context.eflags = read_register_u32(reg_ctx, "rflags");
409 thread_context.cs = read_register_u16(reg_ctx, "cs");
410 thread_context.fs = read_register_u16(reg_ctx, "fs");
411 thread_context.gs = read_register_u16(reg_ctx, "gs");
412 thread_context.ss = read_register_u16(reg_ctx, "ss");
413 thread_context.ds = read_register_u16(reg_ctx, "ds");
414 return thread_context;
415}
416
420 thread_context.context_flags = static_cast<uint32_t>(
421 minidump::RegisterContextMinidump_ARM64::Flags::ARM64_Flag |
422 minidump::RegisterContextMinidump_ARM64::Flags::Integer |
423 minidump::RegisterContextMinidump_ARM64::Flags::FloatingPoint);
424 char reg_name[16];
425 for (uint32_t i = 0; i < 31; ++i) {
426 snprintf(reg_name, sizeof(reg_name), "x%u", i);
427 thread_context.x[i] = read_register_u64(reg_ctx, reg_name);
428 }
429 // Work around a bug in debugserver where "sp" on arm64 doesn't have the alt
430 // name set to "x31"
431 thread_context.x[31] = read_register_u64(reg_ctx, "sp");
432 thread_context.pc = read_register_u64(reg_ctx, "pc");
433 thread_context.cpsr = read_register_u32(reg_ctx, "cpsr");
434 thread_context.fpsr = read_register_u32(reg_ctx, "fpsr");
435 thread_context.fpcr = read_register_u32(reg_ctx, "fpcr");
436 for (uint32_t i = 0; i < 32; ++i) {
437 snprintf(reg_name, sizeof(reg_name), "v%u", i);
438 read_register_u128(reg_ctx, reg_name, &thread_context.v[i * 16]);
439 }
440 return thread_context;
441}
442
444 llvm::Triple::ArchType m_arch;
445 union {
448 };
449
450public:
451 ArchThreadContexts(llvm::Triple::ArchType arch) : m_arch(arch) {}
452
454 switch (m_arch) {
455 case llvm::Triple::ArchType::x86_64:
457 return true;
458 case llvm::Triple::ArchType::aarch64:
459 arm64 = GetThreadContext_ARM64(reg_ctx);
460 return true;
461 default:
462 break;
463 }
464 return false;
465 }
466
467 const void *data() const { return &x86_64; }
468
469 size_t size() const {
470 switch (m_arch) {
471 case llvm::Triple::ArchType::x86_64:
472 return sizeof(x86_64);
473 case llvm::Triple::ArchType::aarch64:
474 return sizeof(arm64);
475 default:
476 break;
477 }
478 return 0;
479 }
480};
481
482// Function returns start and size of the memory region that contains
483// memory location pointed to by the current stack pointer.
484llvm::Expected<std::pair<addr_t, addr_t>>
485findStackHelper(const lldb::ProcessSP &process_sp, uint64_t rsp) {
486 MemoryRegionInfo range_info;
487 Status error = process_sp->GetMemoryRegionInfo(rsp, range_info);
488 // Skip failed memory region requests or any regions with no permissions.
489 if (error.Fail() || range_info.GetLLDBPermissions() == 0)
490 return llvm::createStringError(
491 std::errc::not_supported,
492 "unable to load stack segment of the process");
493
494 const addr_t addr = range_info.GetRange().GetRangeBase();
495 const addr_t size = range_info.GetRange().GetByteSize();
496
497 if (size == 0)
498 return llvm::createStringError(std::errc::not_supported,
499 "stack segment of the process is empty");
500
501 return std::make_pair(addr, size);
502}
503
505 constexpr size_t minidump_thread_size = sizeof(llvm::minidump::Thread);
506 lldb_private::ThreadList thread_list = process_sp->GetThreadList();
507
508 // size of the entire thread stream consists of:
509 // number of threads and threads array
510 size_t thread_stream_size = sizeof(llvm::support::ulittle32_t) +
511 thread_list.GetSize() * minidump_thread_size;
512 // save for the ability to set up RVA
513 size_t size_before = GetCurrentDataEndOffset();
514
515 AddDirectory(StreamType::ThreadList, thread_stream_size);
516
517 llvm::support::ulittle32_t thread_count =
518 static_cast<llvm::support::ulittle32_t>(thread_list.GetSize());
519 m_data.AppendData(&thread_count, sizeof(llvm::support::ulittle32_t));
520
521 DataBufferHeap helper_data;
522
523 const uint32_t num_threads = thread_list.GetSize();
524
525 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
526 ThreadSP thread_sp(thread_list.GetThreadAtIndex(thread_idx));
527 RegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
529
530 if (!reg_ctx_sp) {
531 error.SetErrorString("Unable to get the register context.");
532 return error;
533 }
534 RegisterContext *reg_ctx = reg_ctx_sp.get();
535 Target &target = process_sp->GetTarget();
536 const ArchSpec &arch = target.GetArchitecture();
537 ArchThreadContexts thread_context(arch.GetMachine());
538 if (!thread_context.prepareRegisterContext(reg_ctx)) {
539 error.SetErrorStringWithFormat(
540 "architecture %s not supported.",
541 arch.GetTriple().getArchName().str().c_str());
542 return error;
543 }
544 uint64_t sp = reg_ctx->GetSP();
545 auto expected_address_range = findStackHelper(process_sp, sp);
546
547 if (!expected_address_range) {
548 consumeError(expected_address_range.takeError());
549 error.SetErrorString("Unable to get the stack address.");
550 return error;
551 }
552
553 std::pair<uint64_t, uint64_t> range = std::move(*expected_address_range);
554 uint64_t addr = range.first;
555 uint64_t size = range.second;
556
557 auto data_up = std::make_unique<DataBufferHeap>(size, 0);
558 const size_t stack_bytes_read =
559 process_sp->ReadMemory(addr, data_up->GetBytes(), size, error);
560
561 if (error.Fail())
562 return error;
563
564 LocationDescriptor stack_memory;
565 stack_memory.DataSize =
566 static_cast<llvm::support::ulittle32_t>(stack_bytes_read);
567 stack_memory.RVA = static_cast<llvm::support::ulittle32_t>(
568 size_before + thread_stream_size + helper_data.GetByteSize());
569
570 MemoryDescriptor stack;
571 stack.StartOfMemoryRange = static_cast<llvm::support::ulittle64_t>(addr);
572 stack.Memory = stack_memory;
573
574 helper_data.AppendData(data_up->GetBytes(), stack_bytes_read);
575
576 LocationDescriptor thread_context_memory_locator;
577 thread_context_memory_locator.DataSize =
578 static_cast<llvm::support::ulittle32_t>(thread_context.size());
579 thread_context_memory_locator.RVA = static_cast<llvm::support::ulittle32_t>(
580 size_before + thread_stream_size + helper_data.GetByteSize());
581 // Cache thie thread context memory so we can reuse for exceptions.
582 m_tid_to_reg_ctx[thread_sp->GetID()] = thread_context_memory_locator;
583
584 helper_data.AppendData(thread_context.data(), thread_context.size());
585
586 llvm::minidump::Thread t;
587 t.ThreadId = static_cast<llvm::support::ulittle32_t>(thread_sp->GetID());
588 t.SuspendCount = static_cast<llvm::support::ulittle32_t>(
589 (thread_sp->GetState() == StateType::eStateSuspended) ? 1 : 0);
590 t.PriorityClass = static_cast<llvm::support::ulittle32_t>(0);
591 t.Priority = static_cast<llvm::support::ulittle32_t>(0);
592 t.EnvironmentBlock = static_cast<llvm::support::ulittle64_t>(0);
593 t.Stack = stack, t.Context = thread_context_memory_locator;
594
595 m_data.AppendData(&t, sizeof(llvm::minidump::Thread));
596 }
597
598 m_data.AppendData(helper_data.GetBytes(), helper_data.GetByteSize());
599 return Status();
600}
601
603 lldb_private::ThreadList thread_list = process_sp->GetThreadList();
604
605 const uint32_t num_threads = thread_list.GetSize();
606 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
607 ThreadSP thread_sp(thread_list.GetThreadAtIndex(thread_idx));
608 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
609 bool add_exception = false;
610 if (stop_info_sp) {
611 switch (stop_info_sp->GetStopReason()) {
614 add_exception = true;
615 break;
616 default:
617 break;
618 }
619 }
620 if (add_exception) {
621 constexpr size_t minidump_exception_size =
622 sizeof(llvm::minidump::ExceptionStream);
623 AddDirectory(StreamType::Exception, minidump_exception_size);
624 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
625 RegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
626 Exception exp_record = {};
627 exp_record.ExceptionCode =
628 static_cast<llvm::support::ulittle32_t>(stop_info_sp->GetValue());
629 exp_record.ExceptionFlags = static_cast<llvm::support::ulittle32_t>(0);
630 exp_record.ExceptionRecord = static_cast<llvm::support::ulittle64_t>(0);
631 exp_record.ExceptionAddress = reg_ctx_sp->GetPC();
632 exp_record.NumberParameters = static_cast<llvm::support::ulittle32_t>(0);
633 exp_record.UnusedAlignment = static_cast<llvm::support::ulittle32_t>(0);
634 // exp_record.ExceptionInformation;
635
636 ExceptionStream exp_stream;
637 exp_stream.ThreadId =
638 static_cast<llvm::support::ulittle32_t>(thread_sp->GetID());
639 exp_stream.UnusedAlignment = static_cast<llvm::support::ulittle32_t>(0);
640 exp_stream.ExceptionRecord = exp_record;
641 auto Iter = m_tid_to_reg_ctx.find(thread_sp->GetID());
642 if (Iter != m_tid_to_reg_ctx.end()) {
643 exp_stream.ThreadContext = Iter->second;
644 } else {
645 exp_stream.ThreadContext.DataSize = 0;
646 exp_stream.ThreadContext.RVA = 0;
647 }
648 m_data.AppendData(&exp_stream, minidump_exception_size);
649 }
650 }
651}
652
655 lldb::SaveCoreStyle core_style) {
658 error = process_sp->CalculateCoreFileSaveRanges(core_style, core_ranges);
659 if (error.Fail()) {
660 error.SetErrorString("Process doesn't support getting memory region info.");
661 return error;
662 }
663
664 DataBufferHeap helper_data;
665 std::vector<MemoryDescriptor> mem_descriptors;
666 for (const auto &core_range : core_ranges) {
667 // Skip empty memory regions.
668 if (core_range.range.empty())
669 continue;
670 const addr_t addr = core_range.range.start();
671 const addr_t size = core_range.range.size();
672 auto data_up = std::make_unique<DataBufferHeap>(size, 0);
673 const size_t bytes_read =
674 process_sp->ReadMemory(addr, data_up->GetBytes(), size, error);
675 if (error.Fail()) {
676 Log *log = GetLog(LLDBLog::Object);
677 LLDB_LOGF(log, "Failed to read memory region. Bytes read: %zu, error: %s",
678 bytes_read, error.AsCString());
679 error.Clear();
680 }
681 if (bytes_read == 0)
682 continue;
683 // We have a good memory region with valid bytes to store.
684 LocationDescriptor memory_dump;
685 memory_dump.DataSize = static_cast<llvm::support::ulittle32_t>(bytes_read);
686 memory_dump.RVA =
687 static_cast<llvm::support::ulittle32_t>(GetCurrentDataEndOffset());
688 MemoryDescriptor memory_desc;
689 memory_desc.StartOfMemoryRange =
690 static_cast<llvm::support::ulittle64_t>(addr);
691 memory_desc.Memory = memory_dump;
692 mem_descriptors.push_back(memory_desc);
693 m_data.AppendData(data_up->GetBytes(), bytes_read);
694 }
695
696 AddDirectory(StreamType::MemoryList,
697 sizeof(llvm::support::ulittle32_t) +
698 mem_descriptors.size() *
699 sizeof(llvm::minidump::MemoryDescriptor));
700 llvm::support::ulittle32_t memory_ranges_num(mem_descriptors.size());
701
702 m_data.AppendData(&memory_ranges_num, sizeof(llvm::support::ulittle32_t));
703 for (auto memory_descriptor : mem_descriptors) {
704 m_data.AppendData(&memory_descriptor,
705 sizeof(llvm::minidump::MemoryDescriptor));
706 }
707
708 return error;
709}
710
712 AddDirectory(StreamType::MiscInfo,
714
716 misc_info.size = static_cast<llvm::support::ulittle32_t>(
718 // Default set flags1 to 0, in case that we will not be able to
719 // get any information
720 misc_info.flags1 = static_cast<llvm::support::ulittle32_t>(0);
721
723 process_sp->GetProcessInfo(process_info);
724 if (process_info.ProcessIDIsValid()) {
725 // Set flags1 to reflect that PID is filled in
726 misc_info.flags1 =
727 static_cast<llvm::support::ulittle32_t>(static_cast<uint32_t>(
729 misc_info.process_id =
730 static_cast<llvm::support::ulittle32_t>(process_info.GetProcessID());
731 }
732
733 m_data.AppendData(&misc_info,
735}
736
737std::unique_ptr<llvm::MemoryBuffer>
738getFileStreamHelper(const std::string &path) {
739 auto maybe_stream = llvm::MemoryBuffer::getFileAsStream(path);
740 if (!maybe_stream)
741 return nullptr;
742 return std::move(maybe_stream.get());
743}
744
746 const lldb::ProcessSP &process_sp) {
747 std::vector<std::pair<StreamType, std::string>> files_with_stream_types = {
748 {StreamType::LinuxCPUInfo, "/proc/cpuinfo"},
749 {StreamType::LinuxLSBRelease, "/etc/lsb-release"},
750 };
751
753 process_sp->GetProcessInfo(process_info);
754 if (process_info.ProcessIDIsValid()) {
755 lldb::pid_t pid = process_info.GetProcessID();
756 std::string pid_str = std::to_string(pid);
757 files_with_stream_types.push_back(
758 {StreamType::LinuxProcStatus, "/proc/" + pid_str + "/status"});
759 files_with_stream_types.push_back(
760 {StreamType::LinuxCMDLine, "/proc/" + pid_str + "/cmdline"});
761 files_with_stream_types.push_back(
762 {StreamType::LinuxEnviron, "/proc/" + pid_str + "/environ"});
763 files_with_stream_types.push_back(
764 {StreamType::LinuxAuxv, "/proc/" + pid_str + "/auxv"});
765 files_with_stream_types.push_back(
766 {StreamType::LinuxMaps, "/proc/" + pid_str + "/maps"});
767 files_with_stream_types.push_back(
768 {StreamType::LinuxProcStat, "/proc/" + pid_str + "/stat"});
769 files_with_stream_types.push_back(
770 {StreamType::LinuxProcFD, "/proc/" + pid_str + "/fd"});
771 }
772
773 for (const auto &entry : files_with_stream_types) {
774 StreamType stream = entry.first;
775 std::string path = entry.second;
776 auto memory_buffer = getFileStreamHelper(path);
777
778 if (memory_buffer) {
779 size_t size = memory_buffer->getBufferSize();
780 if (size == 0)
781 continue;
782 AddDirectory(stream, size);
783 m_data.AppendData(memory_buffer->getBufferStart(), size);
784 }
785 }
786}
787
789 constexpr size_t header_size = sizeof(llvm::minidump::Header);
790 constexpr size_t directory_size = sizeof(llvm::minidump::Directory);
791
792 // write header
793 llvm::minidump::Header header;
794 header.Signature = static_cast<llvm::support::ulittle32_t>(
795 llvm::minidump::Header::MagicSignature);
796 header.Version = static_cast<llvm::support::ulittle32_t>(
797 llvm::minidump::Header::MagicVersion);
798 header.NumberOfStreams =
799 static_cast<llvm::support::ulittle32_t>(GetDirectoriesNum());
800 header.StreamDirectoryRVA =
801 static_cast<llvm::support::ulittle32_t>(GetCurrentDataEndOffset());
802 header.Checksum = static_cast<llvm::support::ulittle32_t>(
803 0u), // not used in most of the writers
804 header.TimeDateStamp =
805 static_cast<llvm::support::ulittle32_t>(std::time(nullptr));
806 header.Flags =
807 static_cast<llvm::support::ulittle64_t>(0u); // minidump normal flag
808
810 size_t bytes_written;
811
812 bytes_written = header_size;
813 error = core_file->Write(&header, bytes_written);
814 if (error.Fail() || bytes_written != header_size) {
815 if (bytes_written != header_size)
816 error.SetErrorStringWithFormat(
817 "unable to write the header (written %zd/%zd)", bytes_written,
818 header_size);
819 return error;
820 }
821
822 // write data
823 bytes_written = m_data.GetByteSize();
824 error = core_file->Write(m_data.GetBytes(), bytes_written);
825 if (error.Fail() || bytes_written != m_data.GetByteSize()) {
826 if (bytes_written != m_data.GetByteSize())
827 error.SetErrorStringWithFormat(
828 "unable to write the data (written %zd/%" PRIu64 ")", bytes_written,
830 return error;
831 }
832
833 // write directories
834 for (const Directory &dir : m_directories) {
835 bytes_written = directory_size;
836 error = core_file->Write(&dir, bytes_written);
837 if (error.Fail() || bytes_written != directory_size) {
838 if (bytes_written != directory_size)
839 error.SetErrorStringWithFormat(
840 "unable to write the directory (written %zd/%zd)", bytes_written,
841 directory_size);
842 return error;
843 }
844 }
845
846 return error;
847}
848
850 return m_directories.size();
851}
852
854 return sizeof(llvm::minidump::Header) + m_data.GetByteSize();
855}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:349
llvm::support::ulittle16_t read_register_u16(RegisterContext *reg_ctx, llvm::StringRef reg_name)
uint64_t read_register_u64_raw(RegisterContext *reg_ctx, llvm::StringRef reg_name)
Status WriteString(const std::string &to_write, lldb_private::DataBufferHeap *buffer)
lldb_private::minidump::MinidumpContext_x86_64 GetThreadContext_x86_64(RegisterContext *reg_ctx)
llvm::Expected< uint64_t > getModuleFileSize(Target &target, const ModuleSP &mod)
uint16_t read_register_u16_raw(RegisterContext *reg_ctx, llvm::StringRef reg_name)
llvm::support::ulittle32_t read_register_u32(RegisterContext *reg_ctx, llvm::StringRef reg_name)
void read_register_u128(RegisterContext *reg_ctx, llvm::StringRef reg_name, uint8_t *dst)
std::unique_ptr< llvm::MemoryBuffer > getFileStreamHelper(const std::string &path)
llvm::support::ulittle64_t read_register_u64(RegisterContext *reg_ctx, llvm::StringRef reg_name)
minidump::RegisterContextMinidump_ARM64::Context GetThreadContext_ARM64(RegisterContext *reg_ctx)
llvm::Expected< std::pair< addr_t, addr_t > > findStackHelper(const lldb::ProcessSP &process_sp, uint64_t rsp)
uint32_t read_register_u32_raw(RegisterContext *reg_ctx, llvm::StringRef reg_name)
Structure holding data neccessary for minidump file creation.
lldb_private::Status WriteString(const std::string &to_write, lldb_private::DataBufferHeap *buffer)
const void * data() const
llvm::Triple::ArchType m_arch
lldb_private::minidump::MinidumpContext_x86_64 x86_64
lldb_private::minidump::RegisterContextMinidump_ARM64::Context arm64
bool prepareRegisterContext(RegisterContext *reg_ctx)
ArchThreadContexts(llvm::Triple::ArchType arch)
lldb_private::Status Dump(lldb::FileUP &core_file) const
size_t GetCurrentDataEndOffset() const
lldb_private::Status AddModuleList(lldb_private::Target &target)
void AddExceptions(const lldb::ProcessSP &process_sp)
lldb_private::DataBufferHeap m_data
void AddLinuxFileStreams(const lldb::ProcessSP &process_sp)
void AddMiscInfo(const lldb::ProcessSP &process_sp)
size_t GetDirectoriesNum() const
lldb_private::Status AddSystemInfo(const llvm::Triple &target_triple)
lldb_private::Status AddMemoryList(const lldb::ProcessSP &process_sp, lldb::SaveCoreStyle core_style)
std::map< lldb::tid_t, llvm::minidump::LocationDescriptor > m_tid_to_reg_ctx
std::vector< llvm::minidump::Directory > m_directories
lldb_private::Status AddThreadList(const lldb::ProcessSP &process_sp)
void AddDirectory(llvm::minidump::StreamType type, size_t stream_size)
A section + offset based address class.
Definition: Address.h:62
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:439
An architecture specification class.
Definition: ArchSpec.h:31
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:450
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:683
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
void AppendData(const void *src, uint64_t src_len)
A collection class for Module objects.
Definition: ModuleList.h:103
lldb::ModuleSP GetModuleAtIndex(size_t idx) const
Get the module shared pointer for the module at index idx.
Definition: ModuleList.cpp:429
size_t GetSize() const
Gets the size of the module list.
Definition: ModuleList.cpp:638
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:71
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:67
std::vector< CoreFileMemoryRange > CoreFileMemoryRanges
Definition: Process.h:734
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
uint16_t GetAsUInt16(uint16_t fail_value=UINT16_MAX, bool *success_ptr=nullptr) const
uint32_t GetAsMemoryData(const RegisterInfo &reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
An error handling class.
Definition: Status.h:44
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow)
Definition: Target.cpp:3111
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:972
const ArchSpec & GetArchitecture() const
Definition: Target.h:1014
uint32_t GetSize(bool can_update=true)
Definition: ThreadList.cpp:83
lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update=true)
Definition: ThreadList.cpp:91
uint8_t * GetBytes()
Get a pointer to the data.
Definition: DataBuffer.h:108
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:438
std::unique_ptr< lldb_private::File > FileUP
Definition: lldb-forward.h:344
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:381
uint64_t pid_t
Definition: lldb-types.h:81
@ eByteOrderLittle
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:419
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:406
uint64_t addr_t
Definition: lldb-types.h:79
@ eStopReasonException
@ eStopReasonSignal
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:386
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365
BaseType GetRangeBase() const
Definition: RangeMap.h:45
SizeType GetByteSize() const
Definition: RangeMap.h:87
Every register is described in detail including its name, alternate name (optional),...
llvm::support::ulittle32_t process_id
Definition: MinidumpTypes.h:79
llvm::support::ulittle32_t flags1
Definition: MinidumpTypes.h:78
llvm::support::ulittle32_t size
Definition: MinidumpTypes.h:76