-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
createdumpunix.cpp
118 lines (106 loc) · 3.48 KB
/
createdumpunix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "createdump.h"
#if defined(__arm__) || defined(__aarch64__) || defined(__loongarch64) || defined(__riscv)
long g_pageSize = 0;
#endif
//
// The Linux/MacOS create dump code
//
bool
CreateDump(const CreateDumpOptions& options)
{
ReleaseHolder<CrashInfo> crashInfo = new CrashInfo(options);
DumpWriter dumpWriter(*crashInfo);
std::string dumpPath;
bool result = false;
// Initialize PAGE_SIZE
#if defined(__arm__) || defined(__aarch64__) || defined(__loongarch64) || defined(__riscv)
g_pageSize = sysconf(_SC_PAGESIZE);
#endif
TRACE("PAGE_SIZE %d\n", PAGE_SIZE);
if (options.CrashReport && (options.AppModel == AppModelType::SingleFile || options.AppModel == AppModelType::NativeAOT))
{
printf_error("The app model does not support crash report generation\n");
goto exit;
}
if (options.DumpType != DumpType::Full && options.AppModel == AppModelType::NativeAOT)
{
printf_error("The app model only supports full dump generation\n");
goto exit;
}
// Initialize the crash info
if (!crashInfo->Initialize())
{
goto exit;
}
printf_status("Gathering state for process %d %s\n", options.Pid, crashInfo->Name().c_str());
if (options.Signal != 0 || options.CrashThread != 0)
{
printf_status("Crashing thread %04x signal %d (%04x)\n", options.CrashThread, options.Signal, options.Signal);
}
// Suspend all the threads in the target process and build the list of threads
if (!crashInfo->EnumerateAndSuspendThreads())
{
goto exit;
}
// Gather all the info about the process, threads (registers, etc.) and memory regions
if (!crashInfo->GatherCrashInfo(options.DumpType))
{
goto exit;
}
// Format the dump pattern template now that the process name on MacOS has been obtained
if (!FormatDumpName(dumpPath, options.DumpPathTemplate, crashInfo->Name().c_str(), options.Pid))
{
goto exit;
}
// Write the crash report json file if enabled
if (options.CrashReport)
{
CrashReportWriter crashReportWriter(*crashInfo);
crashReportWriter.WriteCrashReport(dumpPath);
}
if (options.CreateDump)
{
// Gather all the useful memory regions from the DAC
if (!crashInfo->EnumerateMemoryRegionsWithDAC(options.DumpType))
{
goto exit;
}
// Join all adjacent memory regions
crashInfo->CombineMemoryRegions();
printf_status("Writing %s to file %s\n", GetDumpTypeString(options.DumpType), dumpPath.c_str());
// Write the actual dump file
if (!dumpWriter.OpenDump(dumpPath.c_str()))
{
goto exit;
}
if (!dumpWriter.WriteDump())
{
printf_error("Writing dump FAILED\n");
// Delete the partial dump file on error
remove(dumpPath.c_str());
goto exit;
}
}
result = true;
exit:
if (kill(options.Pid, 0) == 0)
{
printf_status("Target process is alive\n");
}
else
{
int err = errno;
if (err == ESRCH)
{
printf_error("Target process terminated\n");
}
else
{
printf_error("kill(%d, 0) FAILED %s (%d)\n", options.Pid, strerror(err), err);
}
}
crashInfo->CleanupAndResumeProcess();
return result;
}