File: hang_crash_dump_win.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (126 lines) | stat: -rw-r--r-- 4,906 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/hang_monitor/hang_crash_dump_win.h"

#include "base/debug/crash_logging.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "chrome/common/chrome_constants.h"
#include "content/public/common/result_codes.h"

namespace {

// How long do we wait for the terminated thread or process to die (in ms)
static const int kTerminateTimeoutMS = 2000;

// How long do we wait for the crash to be generated (in ms).
static const int kGenerateDumpTimeoutMS = 10000;

enum NoCrashKeyReason {
  kNoCrashKeyReasonVirtualAlloc = 1,
  kNoCrashKeyReasonWriteProcessMemory,
  kNoCrashKeyReasonNoKeys
};

}  // namespace

void CrashDumpAndTerminateHungChildProcess(
    HANDLE hprocess,
    const base::StringPairs& additional_child_crash_keys) {
  // Before terminating the process we try collecting a dump. Which
  // a transient thread in the child process will do for us.
  typedef HANDLE(__cdecl * DumpFunction)(HANDLE, void*);
  typedef HANDLE(__cdecl * DumpFunctionNoCrashKeys)(HANDLE, int);
  static DumpFunction request_dump = nullptr;
  static DumpFunctionNoCrashKeys request_dump_no_crash_keys = nullptr;
  if (!request_dump) {
    request_dump = reinterpret_cast<DumpFunction>(GetProcAddress(
        GetModuleHandle(chrome::kChromeElfDllName), "InjectDumpForHungInput"));
    DCHECK(request_dump) << "Failed loading DumpProcessWithoutCrash: error " <<
        GetLastError();
  }
  if (!request_dump_no_crash_keys) {
    request_dump_no_crash_keys = reinterpret_cast<DumpFunctionNoCrashKeys>(
        GetProcAddress(GetModuleHandle(chrome::kChromeElfDllName),
                       "InjectDumpForHungInputNoCrashKeys"));
    DCHECK(request_dump_no_crash_keys)
        << "Failed loading DumpProcessWithoutCrash: error " << GetLastError();
  }

  DWORD crash_key_failure = 0;
  void* remote_memory = nullptr;
  bool send_remote_memory = false;
  std::vector<std::string> keys;
  for (const auto& crash_key : additional_child_crash_keys) {
    DCHECK(base::debug::LookupCrashKey(crash_key.first));
    std::string serialized_key = crash_key.first;
    serialized_key.append(":");
    serialized_key.append(crash_key.second);
    keys.push_back(serialized_key);
  }
  std::string serialized_keys = base::JoinString(keys, ",");

  if (!serialized_keys.empty()) {
    size_t data_length = serialized_keys.length() + 1;
    remote_memory = VirtualAllocEx(hprocess, nullptr, data_length,
                                   MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    if (remote_memory) {
      send_remote_memory =
          !!WriteProcessMemory(hprocess, remote_memory, serialized_keys.c_str(),
                               data_length, nullptr);
      crash_key_failure =
          MAKELPARAM(kNoCrashKeyReasonWriteProcessMemory, GetLastError());
    } else {
      crash_key_failure =
          MAKELPARAM(kNoCrashKeyReasonVirtualAlloc, GetLastError());
    }
  } else {
    crash_key_failure = MAKELPARAM(kNoCrashKeyReasonNoKeys, 0);
  }

  HANDLE remote_thread = nullptr;
  if (request_dump && send_remote_memory) {
    remote_thread = request_dump(hprocess, remote_memory);
  } else if (request_dump_no_crash_keys) {
    remote_thread = request_dump_no_crash_keys(hprocess, crash_key_failure);
  }
  DCHECK(remote_thread) << "Failed creating remote thread: error "
                        << GetLastError();
  if (remote_thread) {
    WaitForSingleObject(remote_thread, kGenerateDumpTimeoutMS);
    CloseHandle(remote_thread);
  }

  TerminateProcess(hprocess, content::RESULT_CODE_HUNG);
  WaitForSingleObject(hprocess, kTerminateTimeoutMS);
}

void CrashDumpForHangDebugging(HANDLE hprocess) {
  if (hprocess == GetCurrentProcess()) {
    typedef void (__cdecl *DumpFunction)();
    DumpFunction request_dump = reinterpret_cast<DumpFunction>(GetProcAddress(
        GetModuleHandle(chrome::kChromeElfDllName), "DumpProcessWithoutCrash"));
    DCHECK(request_dump) << "Failed loading DumpProcessWithoutCrash: error " <<
        GetLastError();
    if (request_dump)
      request_dump();
  } else {
    typedef HANDLE (__cdecl *DumpFunction)(HANDLE);
    DumpFunction request_dump = reinterpret_cast<DumpFunction>(
        GetProcAddress(GetModuleHandle(chrome::kChromeElfDllName),
                       "InjectDumpForHangDebugging"));
    DCHECK(request_dump) << "Failed loading InjectDumpForHangDebugging: error "
                         << GetLastError();
    if (request_dump) {
      HANDLE remote_thread = request_dump(hprocess);
      DCHECK(remote_thread) << "Failed creating remote thread: error " <<
          GetLastError();
      if (remote_thread) {
        WaitForSingleObject(remote_thread, kGenerateDumpTimeoutMS);
        CloseHandle(remote_thread);
      }
    }
  }
}