minidumpwriter.cpp (1942B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* 6 * Given a PID and a path to a target file, write a minidump of the 7 * corresponding process in that file. This is taken more or less 8 * verbatim from mozcrash and translated to C++ to avoid problems 9 * writing a minidump of 64 bit Firefox from a 32 bit python. 10 */ 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <windows.h> 15 #include <dbghelp.h> 16 17 int wmain(int argc, wchar_t** argv) { 18 if (argc != 3) { 19 fprintf(stderr, "Usage: minidumpwriter <PID> <DUMP_FILE>\n"); 20 return 1; 21 } 22 23 DWORD pid = (DWORD)_wtoi(argv[1]); 24 25 if (pid <= 0) { 26 fprintf(stderr, "Usage: minidumpwriter <PID> <DUMP_FILE>\n"); 27 return 1; 28 } 29 30 wchar_t* dumpfile = argv[2]; 31 int rv = 1; 32 HANDLE hProcess = 33 OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid); 34 if (!hProcess) { 35 fprintf(stderr, "Couldn't get handle for %lu\n", pid); 36 return rv; 37 } 38 39 HANDLE file = CreateFileW(dumpfile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 40 FILE_ATTRIBUTE_NORMAL, nullptr); 41 if (file == INVALID_HANDLE_VALUE) { 42 fprintf(stderr, "Couldn't open dump file at %S\n", dumpfile); 43 CloseHandle(hProcess); 44 return rv; 45 } 46 47 rv = 0; 48 if (!MiniDumpWriteDump(hProcess, pid, file, MiniDumpNormal, nullptr, nullptr, 49 nullptr)) { 50 fprintf(stderr, "Error 0x%lX in MiniDumpWriteDump\n", GetLastError()); 51 DWORD status = 0; 52 if (!GetExitCodeProcess(hProcess, &status) || (status == STILL_ACTIVE)) { 53 // We return an error only if the process was still running. If we failed 54 // because the process had already been terminated then don't consider it 55 // an actual error. 56 rv = 1; 57 } 58 } 59 60 CloseHandle(file); 61 CloseHandle(hProcess); 62 return rv; 63 }