Skip to content

Commit 7d26bb1

Browse files
hideyukn88Hideyuki Nagase
andauthored
enable core dumps for WSLGd and its children (#280)
* enable core dumps for WSLGd and its children * misc changes from feedbacks * more from feedbacks Co-authored-by: Hideyuki Nagase <[email protected]>
1 parent b05968c commit 7d26bb1

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

WSLGd/main.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ constexpr auto c_shareDocsDir = "/usr/share/doc";
1818
constexpr auto c_shareDocsMount = SHARE_PATH "/doc";
1919
constexpr auto c_x11RuntimeDir = SHARE_PATH "/.X11-unix";
2020
constexpr auto c_xdgRuntimeDir = SHARE_PATH "/runtime-dir";
21-
constexpr auto c_stdErrLogMount = SHARE_PATH "/stderr.log";
21+
constexpr auto c_stdErrLogFile = SHARE_PATH "/stderr.log";
22+
23+
constexpr auto c_coreDir = SHARE_PATH "/dumps";
24+
constexpr auto c_corePatternDefault = "core.%e";
25+
constexpr auto c_corePatternFile = "/proc/sys/kernel/core_pattern";
26+
constexpr auto c_corePatternEnv = "WSL2_WSLG_CORE_PATTERN";
2227

2328
constexpr auto c_sharedMemoryMountPoint = "/mnt/shared_memory";
2429
constexpr auto c_sharedMemoryMountPointEnv = "WSL2_SHARED_MEMORY_MOUNT_POINT";
@@ -79,9 +84,9 @@ try {
7984

8085
// Open a file for logging errors and set it to stderr for WSLGd as well as any child process.
8186
{
82-
wil::unique_fd fd_stdErrLog(open(c_stdErrLogMount, (O_RDWR | O_CREAT), (S_IRUSR | S_IRGRP | S_IROTH)));
83-
if (fd_stdErrLog && (fd_stdErrLog.get() != STDERR_FILENO)) {
84-
dup2(fd_stdErrLog.get(), STDERR_FILENO);
87+
wil::unique_fd stdErrLogFd(open(c_stdErrLogFile, (O_RDWR | O_CREAT), (S_IRUSR | S_IRGRP | S_IROTH)));
88+
if (stdErrLogFd && (stdErrLogFd.get() != STDERR_FILENO)) {
89+
dup2(stdErrLogFd.get(), STDERR_FILENO);
8590
}
8691
}
8792

@@ -186,6 +191,34 @@ try {
186191

187192
SetupOptionalEnv();
188193

194+
// "ulimits -c unlimited" for core dumps.
195+
struct rlimit limit;
196+
limit.rlim_cur = RLIM_INFINITY;
197+
limit.rlim_max = RLIM_INFINITY;
198+
THROW_LAST_ERROR_IF(setrlimit(RLIMIT_CORE, &limit) < 0);
199+
200+
// create folder to store core files.
201+
std::filesystem::create_directories(c_coreDir);
202+
THROW_LAST_ERROR_IF(chmod(c_coreDir, 0777) < 0);
203+
204+
// update core_pattern.
205+
{
206+
wil::unique_file corePatternFile(fopen(c_corePatternFile, "w"));
207+
if (corePatternFile.get()) {
208+
// combine folder path and core pattern.
209+
std::string corePatternFullPath(c_coreDir);
210+
corePatternFullPath += "/";
211+
auto corePattern = getenv(c_corePatternEnv);
212+
if (corePattern) {
213+
corePatternFullPath += corePattern;
214+
} else {
215+
corePatternFullPath += c_corePatternDefault; // set to default core_pattern.
216+
}
217+
// write to core_pattern file.
218+
THROW_LAST_ERROR_IF(fprintf(corePatternFile.get(), "%s", corePatternFullPath.c_str()) < 0);
219+
}
220+
}
221+
189222
// Set shared memory mount point to env when available.
190223
if (!is_shared_memory_mounted ||
191224
(setenv(c_sharedMemoryMountPointEnv, c_sharedMemoryMountPoint, true) < 0)) {

WSLGd/precomp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
#include <sys/capability.h>
4+
#include <sys/time.h>
5+
#include <sys/resource.h>
46
#include <sys/prctl.h>
57
#include <sys/mount.h>
68
#include <sys/socket.h>

0 commit comments

Comments
 (0)