1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "bin/crashpad.h" |
6 | |
7 | #if defined(DART_USE_CRASHPAD) |
8 | #include <map> |
9 | #include <string> |
10 | #include <vector> |
11 | |
12 | #include "crashpad/client/crashpad_client.h" |
13 | #include "crashpad/client/crashpad_info.h" |
14 | #endif |
15 | |
16 | #include "bin/error_exit.h" |
17 | #include "bin/platform.h" |
18 | #include "platform/syslog.h" |
19 | |
20 | namespace dart { |
21 | namespace bin { |
22 | |
23 | #if defined(DART_USE_CRASHPAD) |
24 | #if !defined(HOST_OS_WINDOWS) |
25 | #error "Currently we only support Crashpad on Windows" |
26 | #endif |
27 | |
28 | void InitializeCrashpadClient() { |
29 | // DART_CRASHPAD_HANDLER and DART_CRASHPAD_CRASHES_DIR are set by the |
30 | // testing framework. |
31 | wchar_t* handler = _wgetenv(L"DART_CRASHPAD_HANDLER" ); |
32 | wchar_t* crashes_dir = _wgetenv(L"DART_CRASHPAD_CRASHES_DIR" ); |
33 | if (handler == nullptr || crashes_dir == nullptr || wcslen(handler) == 0 || |
34 | wcslen(crashes_dir) == 0) { |
35 | return; |
36 | } |
37 | |
38 | // Crashpad uses STL so we use it here too even though in general we |
39 | // avoid it. |
40 | const base::FilePath handler_path{std::wstring(handler)}; |
41 | const base::FilePath crashes_dir_path{std::wstring(crashes_dir)}; |
42 | const std::string url("" ); |
43 | std::map<std::string, std::string> annotations; |
44 | char* test_name = getenv("DART_TEST_NAME" ); |
45 | if (test_name != nullptr) { |
46 | annotations["dart_test_name" ] = test_name; |
47 | } |
48 | |
49 | std::vector<std::string> arguments; |
50 | |
51 | crashpad::CrashpadClient client; |
52 | |
53 | // Prevent crashpad_handler from inheriting our standard output and error |
54 | // handles. Otherwise we would not be able to close them ourselves making |
55 | // tests that rely on that fail. |
56 | HANDLE original_stdout = GetStdHandle(STD_OUTPUT_HANDLE); |
57 | HANDLE original_stderr = GetStdHandle(STD_ERROR_HANDLE); |
58 | SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE); |
59 | SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE); |
60 | const bool success = |
61 | client.StartHandler(handler_path, crashes_dir_path, crashes_dir_path, url, |
62 | annotations, arguments, |
63 | /*restartable=*/true, |
64 | /*asynchronous_start=*/false); |
65 | SetStdHandle(STD_OUTPUT_HANDLE, original_stdout); |
66 | SetStdHandle(STD_ERROR_HANDLE, original_stderr); |
67 | |
68 | if (!success) { |
69 | Syslog::PrintErr("Failed to start the crash handler!\n" ); |
70 | Platform::Exit(kErrorExitCode); |
71 | } |
72 | crashpad::CrashpadInfo::GetCrashpadInfo() |
73 | ->set_gather_indirectly_referenced_memory(crashpad::TriState::kEnabled, |
74 | /*limit=*/500 * MB); |
75 | } |
76 | #else |
77 | void InitializeCrashpadClient() {} |
78 | #endif // DART_USE_CRASHPAD |
79 | |
80 | } // namespace bin |
81 | } // namespace dart |
82 | |