OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/crash/content/app/run_as_crashpad_handler_win.h" | 5 #include "components/crash/content/app/run_as_crashpad_handler_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ptr_util.h" |
13 #include "base/process/memory.h" | 15 #include "base/process/memory.h" |
14 #include "base/strings/string16.h" | |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "components/browser_watcher/stability_report_user_stream_data_source.h" |
17 #include "third_party/crashpad/crashpad/client/crashpad_info.h" | 19 #include "third_party/crashpad/crashpad/client/crashpad_info.h" |
18 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" | 20 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" |
19 #include "third_party/crashpad/crashpad/handler/handler_main.h" | 21 #include "third_party/crashpad/crashpad/handler/handler_main.h" |
| 22 #include "third_party/crashpad/crashpad/handler/user_stream_data_source.h" |
20 | 23 |
21 namespace crash_reporter { | 24 namespace crash_reporter { |
22 | 25 |
23 int RunAsCrashpadHandler(const base::CommandLine& command_line, | 26 int RunAsCrashpadHandler(const base::CommandLine& command_line, |
24 const char* process_type_switch) { | 27 const base::FilePath& user_data_dir, |
| 28 const char* process_type_switch, |
| 29 const char* user_data_dir_switch) { |
25 // Make sure this process terminates on OOM in the same mode as other Chrome | 30 // Make sure this process terminates on OOM in the same mode as other Chrome |
26 // processes. | 31 // processes. |
27 base::EnableTerminationOnOutOfMemory(); | 32 base::EnableTerminationOnOutOfMemory(); |
28 | 33 |
29 // If the handler is started with --monitor-self, it'll need a ptype | 34 // If the handler is started with --monitor-self, it'll need a ptype |
30 // annotation set. It'll normally set one itself by being invoked with | 35 // annotation set. It'll normally set one itself by being invoked with |
31 // --monitor-self-annotation=ptype=crashpad-handler, but that leaves a window | 36 // --monitor-self-annotation=ptype=crashpad-handler, but that leaves a window |
32 // during self-monitoring initialization when the ptype is not set at all, so | 37 // during self-monitoring initialization when the ptype is not set at all, so |
33 // provide one here. | 38 // provide one here. |
34 const std::string process_type = | 39 const std::string process_type = |
35 command_line.GetSwitchValueASCII(process_type_switch); | 40 command_line.GetSwitchValueASCII(process_type_switch); |
36 if (!process_type.empty()) { | 41 if (!process_type.empty()) { |
37 crashpad::SimpleStringDictionary* annotations = | 42 crashpad::SimpleStringDictionary* annotations = |
38 new crashpad::SimpleStringDictionary(); | 43 new crashpad::SimpleStringDictionary(); |
39 annotations->SetKeyValue("ptype", process_type.c_str()); | 44 annotations->SetKeyValue("ptype", process_type.c_str()); |
40 crashpad::CrashpadInfo* crashpad_info = | 45 crashpad::CrashpadInfo* crashpad_info = |
41 crashpad::CrashpadInfo::GetCrashpadInfo(); | 46 crashpad::CrashpadInfo::GetCrashpadInfo(); |
42 DCHECK(!crashpad_info->simple_annotations()); | 47 DCHECK(!crashpad_info->simple_annotations()); |
43 crashpad_info->set_simple_annotations(annotations); | 48 crashpad_info->set_simple_annotations(annotations); |
44 } | 49 } |
45 | 50 |
46 std::vector<base::string16> argv = command_line.argv(); | 51 std::vector<base::string16> argv = command_line.argv(); |
47 const base::string16 process_type_arg_prefix = | 52 const base::string16 process_type_arg_prefix = |
48 base::string16(L"--") + base::UTF8ToUTF16(process_type_switch) + L"="; | 53 base::string16(L"--") + base::UTF8ToUTF16(process_type_switch) + L"="; |
| 54 const base::string16 user_data_dir_arg_prefix = |
| 55 base::string16(L"--") + base::UTF8ToUTF16(user_data_dir_switch) + L"="; |
49 argv.erase( | 56 argv.erase( |
50 std::remove_if(argv.begin(), argv.end(), | 57 std::remove_if(argv.begin(), argv.end(), |
51 [&process_type_arg_prefix](const base::string16& str) { | 58 [&process_type_arg_prefix, |
| 59 &user_data_dir_arg_prefix](const base::string16& str) { |
52 return base::StartsWith(str, process_type_arg_prefix, | 60 return base::StartsWith(str, process_type_arg_prefix, |
53 base::CompareCase::SENSITIVE) || | 61 base::CompareCase::SENSITIVE) || |
| 62 base::StartsWith(str, user_data_dir_arg_prefix, |
| 63 base::CompareCase::SENSITIVE) || |
54 (!str.empty() && str[0] == L'/'); | 64 (!str.empty() && str[0] == L'/'); |
55 }), | 65 }), |
56 argv.end()); | 66 argv.end()); |
57 | 67 |
58 std::unique_ptr<char* []> argv_as_utf8(new char*[argv.size() + 1]); | 68 std::unique_ptr<char* []> argv_as_utf8(new char*[argv.size() + 1]); |
59 std::vector<std::string> storage; | 69 std::vector<std::string> storage; |
60 storage.reserve(argv.size()); | 70 storage.reserve(argv.size()); |
61 for (size_t i = 0; i < argv.size(); ++i) { | 71 for (size_t i = 0; i < argv.size(); ++i) { |
62 storage.push_back(base::UTF16ToUTF8(argv[i])); | 72 storage.push_back(base::UTF16ToUTF8(argv[i])); |
63 argv_as_utf8[i] = &storage[i][0]; | 73 argv_as_utf8[i] = &storage[i][0]; |
64 } | 74 } |
65 argv_as_utf8[argv.size()] = nullptr; | 75 argv_as_utf8[argv.size()] = nullptr; |
66 argv.clear(); | 76 argv.clear(); |
| 77 |
| 78 crashpad::UserStreamDataSources user_stream_data_sources; |
| 79 // Interpret an empty user data directory as a missing value. |
| 80 if (!user_data_dir.empty()) { |
| 81 // Register an extension to collect stability information. The extension |
| 82 // will be invoked for any registered process' crashes, but information only |
| 83 // exists for instrumented browser processes. |
| 84 user_stream_data_sources.push_back( |
| 85 base::MakeUnique<browser_watcher::StabilityReportUserStreamDataSource>( |
| 86 user_data_dir)); |
| 87 } |
| 88 |
67 return crashpad::HandlerMain(static_cast<int>(storage.size()), | 89 return crashpad::HandlerMain(static_cast<int>(storage.size()), |
68 argv_as_utf8.get(), nullptr); | 90 argv_as_utf8.get(), &user_stream_data_sources); |
69 } | 91 } |
70 | 92 |
71 } // namespace crash_reporter | 93 } // namespace crash_reporter |
OLD | NEW |