Skip to content

Commit 14e6a41

Browse files
author
Mike McLaughlin
authored
Add log to file option to createdump (#67181)
Add log to file option to createdump Add DOTNET_CreateDumpVerboseDiagnostics env var Make the timestamp ms and 8 hex digits Add printf_status and printf_error that all stdout goes through. Prefix "[createdump" to all printf_status/error output.
1 parent 370f160 commit 14e6a41

15 files changed

+211
-101
lines changed

src/coreclr/debug/createdump/crashinfo.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ CrashInfo::~CrashInfo()
6767
kern_return_t result = ::mach_port_deallocate(mach_task_self(), m_task);
6868
if (result != KERN_SUCCESS)
6969
{
70-
fprintf(stderr, "~CrashInfo: mach_port_deallocate FAILED %x %s\n", result, mach_error_string(result));
70+
printf_error("~CrashInfo: mach_port_deallocate FAILED %x %s\n", result, mach_error_string(result));
7171
}
7272
}
7373
#endif
@@ -241,25 +241,25 @@ CrashInfo::InitializeDAC()
241241
m_hdac = LoadLibraryA(dacPath.c_str());
242242
if (m_hdac == nullptr)
243243
{
244-
fprintf(stderr, "LoadLibraryA(%s) FAILED %d\n", dacPath.c_str(), GetLastError());
244+
printf_error("LoadLibraryA(%s) FAILED %d\n", dacPath.c_str(), GetLastError());
245245
goto exit;
246246
}
247247
pfnCLRDataCreateInstance = (PFN_CLRDataCreateInstance)GetProcAddress(m_hdac, "CLRDataCreateInstance");
248248
if (pfnCLRDataCreateInstance == nullptr)
249249
{
250-
fprintf(stderr, "GetProcAddress(CLRDataCreateInstance) FAILED %d\n", GetLastError());
250+
printf_error("GetProcAddress(CLRDataCreateInstance) FAILED %d\n", GetLastError());
251251
goto exit;
252252
}
253253
hr = pfnCLRDataCreateInstance(__uuidof(ICLRDataEnumMemoryRegions), dataTarget, (void**)&m_pClrDataEnumRegions);
254254
if (FAILED(hr))
255255
{
256-
fprintf(stderr, "CLRDataCreateInstance(ICLRDataEnumMemoryRegions) FAILED %08x\n", hr);
256+
printf_error("CLRDataCreateInstance(ICLRDataEnumMemoryRegions) FAILED %08x\n", hr);
257257
goto exit;
258258
}
259259
hr = pfnCLRDataCreateInstance(__uuidof(IXCLRDataProcess), dataTarget, (void**)&m_pClrDataProcess);
260260
if (FAILED(hr))
261261
{
262-
fprintf(stderr, "CLRDataCreateInstance(IXCLRDataProcess) FAILED %08x\n", hr);
262+
printf_error("CLRDataCreateInstance(IXCLRDataProcess) FAILED %08x\n", hr);
263263
goto exit;
264264
}
265265
}
@@ -302,7 +302,7 @@ CrashInfo::EnumerateMemoryRegionsWithDAC(MINIDUMP_TYPE minidumpType)
302302
HRESULT hr = m_pClrDataEnumRegions->EnumMemoryRegions(this, minidumpType, CLRDATA_ENUM_MEM_DEFAULT);
303303
if (FAILED(hr))
304304
{
305-
fprintf(stderr, "EnumMemoryRegions FAILED %08x\n", hr);
305+
printf_error("EnumMemoryRegions FAILED %08x\n", hr);
306306
return false;
307307
}
308308
TRACE("EnumerateMemoryRegionsWithDAC: Memory enumeration FINISHED\n");
@@ -324,7 +324,7 @@ CrashInfo::EnumerateManagedModules()
324324
TRACE("EnumerateManagedModules: Module enumeration STARTED\n");
325325

326326
if (FAILED(hr = m_pClrDataProcess->StartEnumModules(&enumModules))) {
327-
fprintf(stderr, "StartEnumModules FAILED %08x\n", hr);
327+
printf_error("StartEnumModules FAILED %08x\n", hr);
328328
return false;
329329
}
330330

@@ -768,32 +768,6 @@ CrashInfo::SearchMemoryRegions(const std::set<MemoryRegion>& regions, const Memo
768768
return nullptr;
769769
}
770770

771-
void
772-
CrashInfo::Trace(const char* format, ...)
773-
{
774-
if (g_diagnostics)
775-
{
776-
va_list args;
777-
va_start(args, format);
778-
vfprintf(stdout, format, args);
779-
fflush(stdout);
780-
va_end(args);
781-
}
782-
}
783-
784-
void
785-
CrashInfo::TraceVerbose(const char* format, ...)
786-
{
787-
if (g_diagnosticsVerbose)
788-
{
789-
va_list args;
790-
va_start(args, format);
791-
vfprintf(stdout, format, args);
792-
fflush(stdout);
793-
va_end(args);
794-
}
795-
}
796-
797771
//
798772
// Lookup a symbol in a module. The caller needs to call "free()" on symbol returned.
799773
//

src/coreclr/debug/createdump/crashinfomac.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CrashInfo::Initialize()
1212
kern_return_t result = ::task_for_pid(mach_task_self(), m_pid, &m_task);
1313
if (result != KERN_SUCCESS)
1414
{
15-
fprintf(stderr, "task_for_pid(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
15+
printf_error("task_for_pid(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
1616
return false;
1717
}
1818
return true;
@@ -37,14 +37,14 @@ CrashInfo::EnumerateAndSuspendThreads()
3737
kern_return_t result = ::task_suspend(Task());
3838
if (result != KERN_SUCCESS)
3939
{
40-
fprintf(stderr, "task_suspend(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
40+
printf_error("task_suspend(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
4141
return false;
4242
}
4343

4444
result = ::task_threads(Task(), &threadList, &threadCount);
4545
if (result != KERN_SUCCESS)
4646
{
47-
fprintf(stderr, "task_threads(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
47+
printf_error("task_threads(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
4848
return false;
4949
}
5050

src/coreclr/debug/createdump/crashinfounix.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CrashInfo::Initialize()
1414
m_fd = open(memPath, O_RDONLY);
1515
if (m_fd == -1)
1616
{
17-
fprintf(stderr, "open(%s) FAILED %d (%s)\n", memPath, errno, strerror(errno));
17+
printf_error("open(%s) FAILED %d (%s)\n", memPath, errno, strerror(errno));
1818
return false;
1919
}
2020
// Get the process info
@@ -58,7 +58,7 @@ CrashInfo::EnumerateAndSuspendThreads()
5858
DIR* taskDir = opendir(taskPath);
5959
if (taskDir == nullptr)
6060
{
61-
fprintf(stderr, "opendir(%s) FAILED %s\n", taskPath, strerror(errno));
61+
printf_error("opendir(%s) FAILED %s\n", taskPath, strerror(errno));
6262
return false;
6363
}
6464

@@ -76,7 +76,7 @@ CrashInfo::EnumerateAndSuspendThreads()
7676
}
7777
else
7878
{
79-
fprintf(stderr, "ptrace(ATTACH, %d) FAILED %s\n", tid, strerror(errno));
79+
printf_error("ptrace(ATTACH, %d) FAILED %s\n", tid, strerror(errno));
8080
closedir(taskDir);
8181
return false;
8282
}
@@ -102,7 +102,7 @@ CrashInfo::GetAuxvEntries()
102102
int fd = open(auxvPath, O_RDONLY, 0);
103103
if (fd == -1)
104104
{
105-
fprintf(stderr, "open(%s) FAILED %s\n", auxvPath, strerror(errno));
105+
printf_error("open(%s) FAILED %s\n", auxvPath, strerror(errno));
106106
return false;
107107
}
108108
bool result = false;
@@ -159,7 +159,7 @@ CrashInfo::EnumerateModuleMappings()
159159
FILE* mapsFile = fopen(mapPath, "r");
160160
if (mapsFile == nullptr)
161161
{
162-
fprintf(stderr, "fopen(%s) FAILED %s\n", mapPath, strerror(errno));
162+
printf_error("fopen(%s) FAILED %s\n", mapPath, strerror(errno));
163163
return false;
164164
}
165165
// linuxGateAddress is the beginning of the kernel's mapping of
@@ -377,7 +377,7 @@ GetStatus(pid_t pid, pid_t* ppid, pid_t* tgid, std::string* name)
377377
FILE *statusFile = fopen(statusPath, "r");
378378
if (statusFile == nullptr)
379379
{
380-
fprintf(stderr, "GetStatus fopen(%s) FAILED\n", statusPath);
380+
printf_error("GetStatus fopen(%s) FAILED\n", statusPath);
381381
return false;
382382
}
383383

src/coreclr/debug/createdump/crashreportwriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ CrashReportWriter::WriteCrashReport(const std::string& dumpFileName)
3333
{
3434
std::string crashReportFile(dumpFileName);
3535
crashReportFile.append(".crashreport.json");
36-
printf("Writing crash report to file %s\n", crashReportFile.c_str());
36+
printf_status("Writing crash report to file %s\n", crashReportFile.c_str());
3737
try
3838
{
3939
if (!OpenWriter(crashReportFile.c_str())) {
@@ -44,7 +44,7 @@ CrashReportWriter::WriteCrashReport(const std::string& dumpFileName)
4444
}
4545
catch (const std::exception& e)
4646
{
47-
fprintf(stderr, "Writing the crash report file FAILED\n");
47+
printf_error("Writing the crash report file FAILED\n");
4848

4949
// Delete the partial json file on error
5050
remove(crashReportFile.c_str());
@@ -271,7 +271,7 @@ CrashReportWriter::OpenWriter(const char* fileName)
271271
m_fd = open(fileName, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR | S_IRUSR);
272272
if (m_fd == -1)
273273
{
274-
fprintf(stderr, "Could not create json file %s: %d %s\n", fileName, errno, strerror(errno));
274+
printf_error("Could not create json file %s: %d %s\n", fileName, errno, strerror(errno));
275275
return false;
276276
}
277277
Write("{\n");

src/coreclr/debug/createdump/createdump.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#define ___in _SAL1_Source_(__in, (), _In_)
77
#define ___out _SAL1_Source_(__out, (), _Out_)
88

9-
extern void trace_printf(const char* format, ...);
10-
extern void trace_verbose_printf(const char* format, ...);
119
extern bool g_diagnostics;
1210
extern bool g_diagnosticsVerbose;
1311

1412
#ifdef HOST_UNIX
13+
extern void trace_printf(const char* format, ...);
14+
extern void trace_verbose_printf(const char* format, ...);
1515
#define TRACE(args...) trace_printf(args)
1616
#define TRACE_VERBOSE(args...) trace_verbose_printf(args)
1717
#else
@@ -104,6 +104,8 @@ typedef int T_CONTEXT;
104104
#define MAX_LONGPATH 1024
105105
#endif
106106

107-
bool FormatDumpName(std::string& name, const char* pattern, const char* exename, int pid);
108-
bool CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP_TYPE minidumpType, bool crashReport, int crashThread, int signal);
107+
extern bool FormatDumpName(std::string& name, const char* pattern, const char* exename, int pid);
108+
extern bool CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP_TYPE minidumpType, bool crashReport, int crashThread, int signal);
109109

110+
extern void printf_status(const char* format, ...);
111+
extern void printf_error(const char* format, ...);

src/coreclr/debug/createdump/createdumpunix.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP
1919
{
2020
goto exit;
2121
}
22-
printf("Gathering state for process %d %s\n", pid, crashInfo->Name().c_str());
22+
printf_status("Gathering state for process %d %s\n", pid, crashInfo->Name().c_str());
2323

2424
if (signal != 0 || crashThread != 0)
2525
{
26-
printf("Crashing thread %08x signal %08x\n", crashThread, signal);
26+
printf_status("Crashing thread %08x signal %08x\n", crashThread, signal);
2727
}
2828

2929
// Suspend all the threads in the target process and build the list of threads
@@ -52,7 +52,7 @@ CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP
5252
{
5353
goto exit;
5454
}
55-
fprintf(stdout, "Writing %s to file %s\n", dumpType, dumpPath.c_str());
55+
printf_status("Writing %s to file %s\n", dumpType, dumpPath.c_str());
5656

5757
// Write the actual dump file
5858
if (!dumpWriter.OpenDump(dumpPath.c_str()))
@@ -61,7 +61,7 @@ CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP
6161
}
6262
if (!dumpWriter.WriteDump())
6363
{
64-
fprintf(stderr, "Writing dump FAILED\n");
64+
printf_error( "Writing dump FAILED\n");
6565

6666
// Delete the partial dump file on error
6767
remove(dumpPath.c_str());

src/coreclr/debug/createdump/createdumpwindows.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP
2020
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
2121
if (hProcess == NULL)
2222
{
23-
fprintf(stderr, "Invalid process id '%d' error %d\n", pid, GetLastError());
23+
printf_error("Invalid process id '%d' error %d\n", pid, GetLastError());
2424
goto exit;
2525
}
2626
if (GetModuleBaseNameA(hProcess, NULL, pszName, MAX_LONGPATH) <= 0)
2727
{
28-
fprintf(stderr, "Get process name FAILED %d\n", GetLastError());
28+
printf_error("Get process name FAILED %d\n", GetLastError());
2929
goto exit;
3030
}
3131
if (!FormatDumpName(dumpPath, dumpPathTemplate, pszName, pid))
3232
{
3333
goto exit;
3434
}
35-
printf("Writing %s to file %s\n", dumpType, dumpPath.c_str());
35+
printf_status("Writing %s to file %s\n", dumpType, dumpPath.c_str());
3636

3737
hFile = CreateFileA(dumpPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3838
if (hFile == INVALID_HANDLE_VALUE)
3939
{
40-
fprintf(stderr, "Invalid dump path '%s' error %d\n", dumpPath.c_str(), GetLastError());
40+
printf_error("Invalid dump path '%s' error %d\n", dumpPath.c_str(), GetLastError());
4141
goto exit;
4242
}
4343

@@ -54,7 +54,7 @@ CreateDump(const char* dumpPathTemplate, int pid, const char* dumpType, MINIDUMP
5454
int err = GetLastError();
5555
if (err != HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY))
5656
{
57-
fprintf(stderr, "Write dump FAILED 0x%08x\n", err);
57+
printf_error("Write dump FAILED 0x%08x\n", err);
5858
break;
5959
}
6060
}

src/coreclr/debug/createdump/dumpname.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ FormatDumpName(std::string& name, const char* pattern, const char* exename, int
3737
const char* p = pattern;
3838
if (*p == '|')
3939
{
40-
fprintf(stderr, "Pipe syntax in dump name not supported\n");
40+
printf_error("Pipe syntax in dump name not supported\n");
4141
return false;
4242
}
4343

@@ -81,7 +81,7 @@ FormatDumpName(std::string& name, const char* pattern, const char* exename, int
8181
ArrayHolder<char> buffer = new char[MAX_LONGPATH + 1];
8282
if (gethostname(buffer, MAX_LONGPATH) != 0)
8383
{
84-
fprintf(stderr, "Could not get the host name for dump name: %d\n",
84+
printf_error("Could not get the host name for dump name: %d\n",
8585
#ifdef HOST_WINDOWS
8686
WSAGetLastError());
8787
#else
@@ -114,7 +114,7 @@ FormatDumpName(std::string& name, const char* pattern, const char* exename, int
114114
// pid of dumped process
115115
case 'P':
116116
default:
117-
fprintf(stderr, "Invalid dump name format char '%c'\n", *p);
117+
printf_error("Invalid dump name format char '%c'\n", *p);
118118
return false;
119119
}
120120
}

src/coreclr/debug/createdump/dumpwriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ DumpWriter::OpenDump(const char* dumpFileName)
2626
m_fd = open(dumpFileName, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR | S_IRUSR);
2727
if (m_fd == -1)
2828
{
29-
fprintf(stderr, "Could not open output %s: %d %s\n", dumpFileName, errno, strerror(errno));
29+
printf_error("Could not open output %s: %d %s\n", dumpFileName, errno, strerror(errno));
3030
return false;
3131
}
3232
return true;
@@ -46,7 +46,7 @@ DumpWriter::WriteData(int fd, const void* buffer, size_t length)
4646
} while (written == -1 && errno == EINTR);
4747

4848
if (written < 1) {
49-
fprintf(stderr, "WriteData FAILED %d %s\n", errno, strerror(errno));
49+
printf_error("WriteData FAILED %d %s\n", errno, strerror(errno));
5050
return false;
5151
}
5252
done += written;

src/coreclr/debug/createdump/dumpwriterelf.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ DumpWriter::WriteDump()
161161
// and then laydown the memory blocks
162162
if (finalNoteAlignment > 0) {
163163
if (finalNoteAlignment > sizeof(m_tempBuffer)) {
164-
fprintf(stderr, "finalNoteAlignment %zu > sizeof(m_tempBuffer)\n", finalNoteAlignment);
164+
printf_error("finalNoteAlignment %zu > sizeof(m_tempBuffer)\n", finalNoteAlignment);
165165
return false;
166166
}
167167
memset(m_tempBuffer, 0, finalNoteAlignment);
@@ -189,13 +189,13 @@ DumpWriter::WriteDump()
189189
size_t read = 0;
190190

191191
if (!m_crashInfo.ReadProcessMemory((void*)address, m_tempBuffer, bytesToRead, &read)) {
192-
fprintf(stderr, "ReadProcessMemory(%" PRIA PRIx64 ", %08zx) FAILED\n", address, bytesToRead);
192+
printf_error("ReadProcessMemory(%" PRIA PRIx64 ", %08zx) FAILED\n", address, bytesToRead);
193193
return false;
194194
}
195195

196196
// This can happen if the target process dies before createdump is finished
197197
if (read == 0) {
198-
fprintf(stderr, "ReadProcessMemory(%" PRIA PRIx64 ", %08zx) returned 0 bytes read\n", address, bytesToRead);
198+
printf_error("ReadProcessMemory(%" PRIA PRIx64 ", %08zx) returned 0 bytes read\n", address, bytesToRead);
199199
return false;
200200
}
201201

@@ -209,8 +209,7 @@ DumpWriter::WriteDump()
209209
}
210210
}
211211

212-
printf("Written %" PRId64 " bytes (%" PRId64 " pages) to core file\n", total, total / PAGE_SIZE);
213-
212+
printf_status("Written %" PRId64 " bytes (%" PRId64 " pages) to core file\n", total, total / PAGE_SIZE);
214213
return true;
215214
}
216215

0 commit comments

Comments
 (0)