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/process/memory.h" | 13 #include "base/process/memory.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "third_party/crashpad/crashpad/client/crashpad_info.h" |
| 18 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" |
17 #include "third_party/crashpad/crashpad/handler/handler_main.h" | 19 #include "third_party/crashpad/crashpad/handler/handler_main.h" |
18 | 20 |
19 namespace crash_reporter { | 21 namespace crash_reporter { |
20 | 22 |
21 int RunAsCrashpadHandler(const base::CommandLine& command_line) { | 23 int RunAsCrashpadHandler(const base::CommandLine& command_line, |
| 24 const char* process_type_switch) { |
22 // Make sure this process terminates on OOM in the same mode as other Chrome | 25 // Make sure this process terminates on OOM in the same mode as other Chrome |
23 // processes. | 26 // processes. |
24 base::EnableTerminationOnOutOfMemory(); | 27 base::EnableTerminationOnOutOfMemory(); |
25 | 28 |
| 29 // 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 |
| 31 // --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 |
| 33 // provide one here. |
| 34 const std::string process_type = |
| 35 command_line.GetSwitchValueASCII(process_type_switch); |
| 36 if (!process_type.empty()) { |
| 37 crashpad::SimpleStringDictionary* annotations = |
| 38 new crashpad::SimpleStringDictionary(); |
| 39 annotations->SetKeyValue("ptype", process_type.c_str()); |
| 40 crashpad::CrashpadInfo* crashpad_info = |
| 41 crashpad::CrashpadInfo::GetCrashpadInfo(); |
| 42 crashpad_info->set_simple_annotations(annotations); |
| 43 } |
| 44 |
26 std::vector<base::string16> argv = command_line.argv(); | 45 std::vector<base::string16> argv = command_line.argv(); |
27 const base::string16 process_type = L"--type="; | 46 const base::string16 process_type_arg_prefix = |
28 argv.erase(std::remove_if(argv.begin(), argv.end(), | 47 base::string16(L"--") + base::UTF8ToUTF16(process_type_switch) + L"="; |
29 [&process_type](const base::string16& str) { | 48 argv.erase( |
30 return base::StartsWith( | 49 std::remove_if(argv.begin(), argv.end(), |
31 str, process_type, | 50 [&process_type_arg_prefix](const base::string16& str) { |
32 base::CompareCase::SENSITIVE) || | 51 return base::StartsWith(str, process_type_arg_prefix, |
33 (!str.empty() && str[0] == L'/'); | 52 base::CompareCase::SENSITIVE) || |
34 }), | 53 (!str.empty() && str[0] == L'/'); |
35 argv.end()); | 54 }), |
| 55 argv.end()); |
36 | 56 |
37 std::unique_ptr<char* []> argv_as_utf8(new char*[argv.size() + 1]); | 57 std::unique_ptr<char* []> argv_as_utf8(new char*[argv.size() + 1]); |
38 std::vector<std::string> storage; | 58 std::vector<std::string> storage; |
39 storage.reserve(argv.size()); | 59 storage.reserve(argv.size()); |
40 for (size_t i = 0; i < argv.size(); ++i) { | 60 for (size_t i = 0; i < argv.size(); ++i) { |
41 storage.push_back(base::UTF16ToUTF8(argv[i])); | 61 storage.push_back(base::UTF16ToUTF8(argv[i])); |
42 argv_as_utf8[i] = &storage[i][0]; | 62 argv_as_utf8[i] = &storage[i][0]; |
43 } | 63 } |
44 argv_as_utf8[argv.size()] = nullptr; | 64 argv_as_utf8[argv.size()] = nullptr; |
45 argv.clear(); | 65 argv.clear(); |
46 return crashpad::HandlerMain(static_cast<int>(storage.size()), | 66 return crashpad::HandlerMain(static_cast<int>(storage.size()), |
47 argv_as_utf8.get()); | 67 argv_as_utf8.get()); |
48 } | 68 } |
49 | 69 |
50 } // namespace crash_reporter | 70 } // namespace crash_reporter |
OLD | NEW |