File: minidumpwriter.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (63 lines) | stat: -rw-r--r-- 1,942 bytes parent folder | download | duplicates (2)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Given a PID and a path to a target file, write a minidump of the
 * corresponding process in that file. This is taken more or less
 * verbatim from mozcrash and translated to C++ to avoid problems
 * writing a minidump of 64 bit Firefox from a 32 bit python.
 */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dbghelp.h>

int wmain(int argc, wchar_t** argv) {
  if (argc != 3) {
    fprintf(stderr, "Usage: minidumpwriter <PID> <DUMP_FILE>\n");
    return 1;
  }

  DWORD pid = (DWORD)_wtoi(argv[1]);

  if (pid <= 0) {
    fprintf(stderr, "Usage: minidumpwriter <PID> <DUMP_FILE>\n");
    return 1;
  }

  wchar_t* dumpfile = argv[2];
  int rv = 1;
  HANDLE hProcess =
      OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid);
  if (!hProcess) {
    fprintf(stderr, "Couldn't get handle for %lu\n", pid);
    return rv;
  }

  HANDLE file = CreateFileW(dumpfile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL, nullptr);
  if (file == INVALID_HANDLE_VALUE) {
    fprintf(stderr, "Couldn't open dump file at %S\n", dumpfile);
    CloseHandle(hProcess);
    return rv;
  }

  rv = 0;
  if (!MiniDumpWriteDump(hProcess, pid, file, MiniDumpNormal, nullptr, nullptr,
                         nullptr)) {
    fprintf(stderr, "Error 0x%lX in MiniDumpWriteDump\n", GetLastError());
    DWORD status = 0;
    if (!GetExitCodeProcess(hProcess, &status) || (status == STILL_ACTIVE)) {
      // We return an error only if the process was still running. If we failed
      // because the process had already been terminated then don't consider it
      // an actual error.
      rv = 1;
    }
  }

  CloseHandle(file);
  CloseHandle(hProcess);
  return rv;
}