2 * The MIT License (MIT)
4 * Copyright (c) 2019-2021 Samsung Electronics Co., Ltd.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include "createdump.h"
31 #include "dnetmemoryenumlib.h"
33 const int DEFAULT_SIGNAL = 6;
35 bool g_diagnostics = false;
36 bool g_diagnosticsVerbose = false;
38 std::vector<SimpleMemoryRegion> sm_regions;
41 prepare_crash_info(pid_t pid, elf_prstatus **statuses, int statuses_count,
42 DUMP_TYPE minidump_type, ReleaseHolder<CrashInfo> &crash_info)
46 std::vector<elf_prstatus*> stats(statuses, statuses + statuses_count);
48 MINIDUMP_TYPE minidumpType;
50 switch (minidump_type) {
53 minidumpType = MiniDumpNormal;
55 case DT_WITH_PRIV_AND_SHARED_MEM:
56 minidumpType = MiniDumpWithPrivateReadWriteMemory;
59 minidumpType = MiniDumpWithFullMemory;
63 int exitCode = REGERR_OK;
67 crash_info = new CrashInfo(pid, true, pid, DEFAULT_SIGNAL);
69 if (!crash_info->Initialize())
71 return REGERR_CRASHINFO_INITIALIZE_ERROR;
74 if (!crash_info->EnumerateAndSuspendThreads(false))
76 return REGERR_ENUMERATION_ERROR;
78 crash_info->SetThreadsRegisters(stats);
79 if (!crash_info->GatherCrashInfo(minidumpType, false))
81 return REGERR_GATHER_CRASHINFO_ERROR;
83 if (!crash_info->EnumerateMemoryRegionsWithDAC(minidumpType))
85 return REGERR_ENUMERATIONDAC_ERROR;
90 exitCode = REGERR_WRONG_PID;
95 extern "C" DLLEXPORT int
96 DotNetMemoryEnumInit()
98 int exitCode = PAL_InitializeDLL();
102 extern "C" DLLEXPORT void
103 DotNetMemoryEnumFinish()
108 extern "C" DLLEXPORT int
109 DotNetMemoryWriteDump(pid_t pid, elf_prstatus **statuses, int statuses_count,
110 DUMP_TYPE minidump_type, const char *dump_path)
112 ReleaseHolder<CrashInfo> crashInfo;
113 int exitCode = prepare_crash_info(pid, statuses, statuses_count, minidump_type, crashInfo);
115 if (exitCode != REGERR_OK)
118 DumpWriter dumpWriter(*crashInfo);
120 if (!dumpWriter.OpenDump(dump_path))
121 exitCode = REGERR_OPENDUMP_ERROR;
123 if (!dumpWriter.WriteDump())
124 exitCode = REGERR_WRITEDUMP_ERROR;
130 printf_status(const char* format, ...)
133 va_start(args, format);
134 fprintf(stdout, "[createdump] ");
135 vfprintf(stdout, format, args);
141 printf_error(const char* format, ...)
144 va_start(args, format);
145 fprintf(stderr, "[createdump] ");
146 vfprintf(stderr, format, args);
152 trace_printf(const char* format, ...)
157 va_start(args, format);
158 fprintf(stdout, "[createdump] ");
159 vfprintf(stdout, format, args);
166 trace_verbose_printf(const char* format, ...)
168 if (g_diagnosticsVerbose)
171 va_start(args, format);
172 fprintf(stdout, "[createdump] ");
173 vfprintf(stdout, format, args);