summaryrefslogtreecommitdiffstats
path: root/chrome/browser/hang_monitor/hang_crash_dump_win.cc
blob: 456aa389cff48f8e0b0df454eb3cfeefb9338987 (plain)
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
// 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/logging.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;

}  // namespace

void CrashDumpAndTerminateHungChildProcess(HANDLE hprocess) {
  // 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);
  static DumpFunction request_dump = NULL;
  if (!request_dump) {
    request_dump = reinterpret_cast<DumpFunction>(GetProcAddress(
        GetModuleHandle(chrome::kBrowserProcessExecutableName),
            "InjectDumpProcessWithoutCrash"));
    DCHECK(request_dump) << "Failed loading DumpProcessWithoutCrash: 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);
    }
  }

  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::kBrowserProcessExecutableName),
        "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::kBrowserProcessExecutableName),
        "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);
      }
    }
  }
}