OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include <getopt.h> | 15 #include <getopt.h> |
16 #include <libgen.h> | |
17 #include <stdlib.h> | 16 #include <stdlib.h> |
18 | 17 |
19 #include <map> | 18 #include <map> |
20 #include <string> | 19 #include <string> |
21 | 20 |
22 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
23 #include "base/logging.h" | 22 #include "base/logging.h" |
24 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
| 24 #include "base/strings/utf_string_conversions.h" |
| 25 #include "build/build_config.h" |
25 #include "client/crash_report_database.h" | 26 #include "client/crash_report_database.h" |
26 #include "tools/tool_support.h" | 27 #include "tools/tool_support.h" |
27 #include "handler/crash_report_upload_thread.h" | 28 #include "handler/crash_report_upload_thread.h" |
28 #include "handler/mac/crash_report_exception_handler.h" | |
29 #include "handler/mac/exception_handler_server.h" | |
30 #include "util/mach/child_port_handshake.h" | |
31 #include "util/posix/close_stdio.h" | |
32 #include "util/stdlib/map_insert.h" | 29 #include "util/stdlib/map_insert.h" |
33 #include "util/stdlib/string_number_conversion.h" | 30 #include "util/stdlib/string_number_conversion.h" |
34 #include "util/string/split_string.h" | 31 #include "util/string/split_string.h" |
35 #include "util/synchronization/semaphore.h" | 32 #include "util/synchronization/semaphore.h" |
36 | 33 |
| 34 #if defined(OS_MACOSX) |
| 35 #include <libgen.h> |
| 36 #include "handler/mac/crash_report_exception_handler.h" |
| 37 #include "handler/mac/exception_handler_server.h" |
| 38 #include "util/mach/child_port_handshake.h" |
| 39 #include "util/posix/close_stdio.h" |
| 40 #elif defined(OS_WIN) |
| 41 #include <windows.h> |
| 42 #include "handler/win/crash_report_exception_handler.h" |
| 43 #include "util/win/exception_handler_server.h" |
| 44 #endif // OS_MACOSX |
| 45 |
37 namespace crashpad { | 46 namespace crashpad { |
38 namespace { | 47 namespace { |
39 | 48 |
40 void Usage(const std::string& me) { | 49 void Usage(const base::FilePath& me) { |
41 fprintf(stderr, | 50 fprintf(stderr, |
42 "Usage: %s [OPTION]...\n" | 51 "Usage: %" PRFilePath " [OPTION]...\n" |
43 "Crashpad's exception handler server.\n" | 52 "Crashpad's exception handler server.\n" |
44 "\n" | 53 "\n" |
45 " --annotation=KEY=VALUE set a process annotation in each crash report\n" | 54 " --annotation=KEY=VALUE set a process annotation in each crash report\n" |
46 " --database=PATH store the crash report database at PATH\n" | 55 " --database=PATH store the crash report database at PATH\n" |
| 56 #if defined(OS_MACOSX) |
47 " --handshake-fd=FD establish communication with the client over FD\n
" | 57 " --handshake-fd=FD establish communication with the client over FD\n
" |
| 58 #elif defined(OS_WIN) |
| 59 " --pipe-name=PIPE communicate with the client over PIPE\n" |
| 60 #endif // OS_MACOSX |
48 " --url=URL send crash reports to this Breakpad server URL,\n
" | 61 " --url=URL send crash reports to this Breakpad server URL,\n
" |
49 " only if uploads are enabled for the database\n" | 62 " only if uploads are enabled for the database\n" |
50 " --help display this help and exit\n" | 63 " --help display this help and exit\n" |
51 " --version output version information and exit\n", | 64 " --version output version information and exit\n", |
52 me.c_str()); | 65 me.value().c_str()); |
53 ToolSupport::UsageTail(me); | 66 ToolSupport::UsageTail(me); |
54 } | 67 } |
55 | 68 |
56 int HandlerMain(int argc, char* argv[]) { | 69 int HandlerMain(int argc, char* argv[]) { |
57 const std::string me(basename(argv[0])); | 70 const base::FilePath argv0( |
| 71 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); |
| 72 const base::FilePath me(argv0.BaseName()); |
58 | 73 |
59 enum OptionFlags { | 74 enum OptionFlags { |
60 // Long options without short equivalents. | 75 // Long options without short equivalents. |
61 kOptionLastChar = 255, | 76 kOptionLastChar = 255, |
62 kOptionAnnotation, | 77 kOptionAnnotation, |
63 kOptionDatabase, | 78 kOptionDatabase, |
| 79 #if defined(OS_MACOSX) |
64 kOptionHandshakeFD, | 80 kOptionHandshakeFD, |
| 81 #elif defined(OS_WIN) |
| 82 kOptionPipeName, |
| 83 #endif // OS_MACOSX |
65 kOptionURL, | 84 kOptionURL, |
66 | 85 |
67 // Standard options. | 86 // Standard options. |
68 kOptionHelp = -2, | 87 kOptionHelp = -2, |
69 kOptionVersion = -3, | 88 kOptionVersion = -3, |
70 }; | 89 }; |
71 | 90 |
72 struct { | 91 struct { |
73 std::map<std::string, std::string> annotations; | 92 std::map<std::string, std::string> annotations; |
74 std::string url; | 93 std::string url; |
75 const char* database; | 94 const char* database; |
| 95 #if defined(OS_MACOSX) |
76 int handshake_fd; | 96 int handshake_fd; |
| 97 #elif defined(OS_WIN) |
| 98 std::string pipe_name; |
| 99 #endif |
77 } options = {}; | 100 } options = {}; |
| 101 #if defined(OS_MACOSX) |
78 options.handshake_fd = -1; | 102 options.handshake_fd = -1; |
| 103 #endif |
79 | 104 |
80 const option long_options[] = { | 105 const option long_options[] = { |
81 {"annotation", required_argument, nullptr, kOptionAnnotation}, | 106 {"annotation", required_argument, nullptr, kOptionAnnotation}, |
82 {"database", required_argument, nullptr, kOptionDatabase}, | 107 {"database", required_argument, nullptr, kOptionDatabase}, |
| 108 #if defined(OS_MACOSX) |
83 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, | 109 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| 110 #elif defined(OS_WIN) |
| 111 {"pipe-name", required_argument, nullptr, kOptionPipeName}, |
| 112 #endif |
84 {"url", required_argument, nullptr, kOptionURL}, | 113 {"url", required_argument, nullptr, kOptionURL}, |
85 {"help", no_argument, nullptr, kOptionHelp}, | 114 {"help", no_argument, nullptr, kOptionHelp}, |
86 {"version", no_argument, nullptr, kOptionVersion}, | 115 {"version", no_argument, nullptr, kOptionVersion}, |
87 {nullptr, 0, nullptr, 0}, | 116 {nullptr, 0, nullptr, 0}, |
88 }; | 117 }; |
89 | 118 |
90 int opt; | 119 int opt; |
91 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { | 120 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { |
92 switch (opt) { | 121 switch (opt) { |
93 case kOptionAnnotation: { | 122 case kOptionAnnotation: { |
94 std::string key; | 123 std::string key; |
95 std::string value; | 124 std::string value; |
96 if (!SplitString(optarg, '=', &key, &value)) { | 125 if (!SplitString(optarg, '=', &key, &value)) { |
97 ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE"); | 126 ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE"); |
98 return EXIT_FAILURE; | 127 return EXIT_FAILURE; |
99 } | 128 } |
100 std::string old_value; | 129 std::string old_value; |
101 if (!MapInsertOrReplace(&options.annotations, key, value, &old_value)) { | 130 if (!MapInsertOrReplace(&options.annotations, key, value, &old_value)) { |
102 LOG(WARNING) << "duplicate key " << key << ", discarding value " | 131 LOG(WARNING) << "duplicate key " << key << ", discarding value " |
103 << old_value; | 132 << old_value; |
104 } | 133 } |
105 break; | 134 break; |
106 } | 135 } |
107 case kOptionDatabase: { | 136 case kOptionDatabase: { |
108 options.database = optarg; | 137 options.database = optarg; |
109 break; | 138 break; |
110 } | 139 } |
| 140 #if defined(OS_MACOSX) |
111 case kOptionHandshakeFD: { | 141 case kOptionHandshakeFD: { |
112 if (!StringToNumber(optarg, &options.handshake_fd) || | 142 if (!StringToNumber(optarg, &options.handshake_fd) || |
113 options.handshake_fd < 0) { | 143 options.handshake_fd < 0) { |
114 ToolSupport::UsageHint(me, | 144 ToolSupport::UsageHint(me, |
115 "--handshake-fd requires a file descriptor"); | 145 "--handshake-fd requires a file descriptor"); |
116 return EXIT_FAILURE; | 146 return EXIT_FAILURE; |
117 } | 147 } |
118 break; | 148 break; |
119 } | 149 } |
| 150 #elif defined(OS_WIN) |
| 151 case kOptionPipeName: { |
| 152 options.pipe_name = optarg; |
| 153 break; |
| 154 } |
| 155 #endif // OS_MACOSX |
120 case kOptionURL: { | 156 case kOptionURL: { |
121 options.url = optarg; | 157 options.url = optarg; |
122 break; | 158 break; |
123 } | 159 } |
124 case kOptionHelp: { | 160 case kOptionHelp: { |
125 Usage(me); | 161 Usage(me); |
126 return EXIT_SUCCESS; | 162 return EXIT_SUCCESS; |
127 } | 163 } |
128 case kOptionVersion: { | 164 case kOptionVersion: { |
129 ToolSupport::Version(me); | 165 ToolSupport::Version(me); |
130 return EXIT_SUCCESS; | 166 return EXIT_SUCCESS; |
131 } | 167 } |
132 default: { | 168 default: { |
133 ToolSupport::UsageHint(me, nullptr); | 169 ToolSupport::UsageHint(me, nullptr); |
134 return EXIT_FAILURE; | 170 return EXIT_FAILURE; |
135 } | 171 } |
136 } | 172 } |
137 } | 173 } |
138 argc -= optind; | 174 argc -= optind; |
139 argv += optind; | 175 argv += optind; |
140 | 176 |
| 177 #if defined(OS_MACOSX) |
141 if (options.handshake_fd < 0) { | 178 if (options.handshake_fd < 0) { |
142 ToolSupport::UsageHint(me, "--handshake-fd is required"); | 179 ToolSupport::UsageHint(me, "--handshake-fd is required"); |
143 return EXIT_FAILURE; | 180 return EXIT_FAILURE; |
144 } | 181 } |
| 182 #elif defined(OS_WIN) |
| 183 if (options.pipe_name.empty()) { |
| 184 ToolSupport::UsageHint(me, "--pipe-name is required"); |
| 185 return EXIT_FAILURE; |
| 186 } |
| 187 #endif |
145 | 188 |
146 if (!options.database) { | 189 if (!options.database) { |
147 ToolSupport::UsageHint(me, "--database is required"); | 190 ToolSupport::UsageHint(me, "--database is required"); |
148 return EXIT_FAILURE; | 191 return EXIT_FAILURE; |
149 } | 192 } |
150 | 193 |
151 if (argc) { | 194 if (argc) { |
152 ToolSupport::UsageHint(me, nullptr); | 195 ToolSupport::UsageHint(me, nullptr); |
153 return EXIT_FAILURE; | 196 return EXIT_FAILURE; |
154 } | 197 } |
155 | 198 |
156 CloseStdinAndStdout(); | |
157 | |
158 ExceptionHandlerServer exception_handler_server; | 199 ExceptionHandlerServer exception_handler_server; |
159 | 200 |
| 201 #if defined(OS_MACOSX) |
| 202 CloseStdinAndStdout(); |
160 ChildPortHandshake::RunClient(options.handshake_fd, | 203 ChildPortHandshake::RunClient(options.handshake_fd, |
161 exception_handler_server.receive_port(), | 204 exception_handler_server.receive_port(), |
162 MACH_MSG_TYPE_MAKE_SEND); | 205 MACH_MSG_TYPE_MAKE_SEND); |
| 206 #endif // OS_MACOSX |
163 | 207 |
164 scoped_ptr<CrashReportDatabase> database( | 208 scoped_ptr<CrashReportDatabase> database( |
165 CrashReportDatabase::Initialize(base::FilePath(options.database))); | 209 CrashReportDatabase::Initialize(base::FilePath( |
| 210 #if defined(OS_MACOSX) |
| 211 options.database |
| 212 #elif defined(OS_WIN) |
| 213 base::UTF8ToUTF16(options.database) |
| 214 #endif |
| 215 ))); |
166 if (!database) { | 216 if (!database) { |
167 return EXIT_FAILURE; | 217 return EXIT_FAILURE; |
168 } | 218 } |
169 | 219 |
170 CrashReportUploadThread upload_thread(database.get(), options.url); | 220 CrashReportUploadThread upload_thread(database.get(), options.url); |
171 upload_thread.Start(); | 221 upload_thread.Start(); |
172 | 222 |
173 CrashReportExceptionHandler exception_handler( | 223 CrashReportExceptionHandler exception_handler( |
174 database.get(), &upload_thread, &options.annotations); | 224 database.get(), &upload_thread, &options.annotations); |
175 | 225 |
| 226 #if defined(OS_MACOSX) |
176 exception_handler_server.Run(&exception_handler); | 227 exception_handler_server.Run(&exception_handler); |
| 228 #elif defined(OS_WIN) |
| 229 exception_handler_server.Run(&exception_handler, options.pipe_name); |
| 230 #endif // OS_MACOSX |
177 | 231 |
178 upload_thread.Stop(); | 232 upload_thread.Stop(); |
179 | 233 |
180 return EXIT_SUCCESS; | 234 return EXIT_SUCCESS; |
181 } | 235 } |
182 | 236 |
183 } // namespace | 237 } // namespace |
184 } // namespace crashpad | 238 } // namespace crashpad |
185 | 239 |
| 240 #if defined(OS_MACOSX) |
186 int main(int argc, char* argv[]) { | 241 int main(int argc, char* argv[]) { |
187 return crashpad::HandlerMain(argc, argv); | 242 return crashpad::HandlerMain(argc, argv); |
188 } | 243 } |
| 244 #elif defined(OS_WIN) |
| 245 int wmain(int argc, wchar_t* argv[]) { |
| 246 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HandlerMain); |
| 247 } |
| 248 #endif // OS_MACOSX |
OLD | NEW |