Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: third_party/crashpad/crashpad/handler/handler_main.cc

Issue 1505213004: Copy Crashpad into the Chrome tree instead of importing it via DEPS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments, update README.chromium Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "handler/handler_main.h"
16
17 #include <getopt.h>
18 #include <stdlib.h>
19
20 #include <map>
21 #include <string>
22
23 #include "base/auto_reset.h"
24 #include "base/files/file_path.h"
25 #include "base/files/scoped_file.h"
26 #include "base/logging.h"
27 #include "base/memory/scoped_ptr.h"
28 #include "base/scoped_generic.h"
29 #include "base/strings/utf_string_conversions.h"
30 #include "build/build_config.h"
31 #include "client/crash_report_database.h"
32 #include "client/crashpad_client.h"
33 #include "tools/tool_support.h"
34 #include "handler/crash_report_upload_thread.h"
35 #include "util/file/file_io.h"
36 #include "util/stdlib/move.h"
37 #include "util/stdlib/map_insert.h"
38 #include "util/stdlib/string_number_conversion.h"
39 #include "util/string/split_string.h"
40 #include "util/synchronization/semaphore.h"
41
42 #if defined(OS_MACOSX)
43 #include <libgen.h>
44 #include <signal.h>
45
46 #include "base/mac/scoped_mach_port.h"
47 #include "handler/mac/crash_report_exception_handler.h"
48 #include "handler/mac/exception_handler_server.h"
49 #include "util/mach/child_port_handshake.h"
50 #include "util/mach/mach_extensions.h"
51 #include "util/posix/close_stdio.h"
52 #elif defined(OS_WIN)
53 #include <windows.h>
54
55 #include "handler/win/crash_report_exception_handler.h"
56 #include "util/win/exception_handler_server.h"
57 #include "util/win/handle.h"
58 #endif // OS_MACOSX
59
60 namespace crashpad {
61
62 namespace {
63
64 void Usage(const base::FilePath& me) {
65 fprintf(stderr,
66 "Usage: %" PRFilePath " [OPTION]...\n"
67 "Crashpad's exception handler server.\n"
68 "\n"
69 " --annotation=KEY=VALUE set a process annotation in each crash report\n"
70 " --database=PATH store the crash report database at PATH\n"
71 #if defined(OS_MACOSX)
72 " --handshake-fd=FD establish communication with the client over FD\n "
73 " --mach-service=SERVICE register SERVICE with the bootstrap server\n"
74 " --reset-own-crash-exception-port-to-system-default\n"
75 " reset the server's exception handler to default\n "
76 #elif defined(OS_WIN)
77 " --handshake-handle=HANDLE\n"
78 " create a new pipe and send its name via HANDLE\n"
79 " --pipe-name=PIPE communicate with the client over PIPE\n"
80 #endif // OS_MACOSX
81 " --url=URL send crash reports to this Breakpad server URL,\n "
82 " only if uploads are enabled for the database\n"
83 " --help display this help and exit\n"
84 " --version output version information and exit\n",
85 me.value().c_str());
86 ToolSupport::UsageTail(me);
87 }
88
89 #if defined(OS_MACOSX)
90
91 struct ResetSIGTERMTraits {
92 static struct sigaction* InvalidValue() {
93 return nullptr;
94 }
95
96 static void Free(struct sigaction* sa) {
97 int rv = sigaction(SIGTERM, sa, nullptr);
98 PLOG_IF(ERROR, rv != 0) << "sigaction";
99 }
100 };
101 using ScopedResetSIGTERM =
102 base::ScopedGeneric<struct sigaction*, ResetSIGTERMTraits>;
103
104 ExceptionHandlerServer* g_exception_handler_server;
105
106 // This signal handler is only operative when being run from launchd.
107 void HandleSIGTERM(int sig, siginfo_t* siginfo, void* context) {
108 DCHECK(g_exception_handler_server);
109 g_exception_handler_server->Stop();
110 }
111
112 #endif // OS_MACOSX
113
114 } // namespace
115
116 int HandlerMain(int argc, char* argv[]) {
117 const base::FilePath argv0(
118 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0]));
119 const base::FilePath me(argv0.BaseName());
120
121 enum OptionFlags {
122 // Long options without short equivalents.
123 kOptionLastChar = 255,
124 kOptionAnnotation,
125 kOptionDatabase,
126 #if defined(OS_MACOSX)
127 kOptionHandshakeFD,
128 kOptionMachService,
129 kOptionResetOwnCrashExceptionPortToSystemDefault,
130 #elif defined(OS_WIN)
131 kOptionHandshakeHandle,
132 kOptionPipeName,
133 #endif // OS_MACOSX
134 kOptionURL,
135
136 // Standard options.
137 kOptionHelp = -2,
138 kOptionVersion = -3,
139 };
140
141 struct {
142 std::map<std::string, std::string> annotations;
143 std::string url;
144 const char* database;
145 #if defined(OS_MACOSX)
146 int handshake_fd;
147 std::string mach_service;
148 bool reset_own_crash_exception_port_to_system_default;
149 #elif defined(OS_WIN)
150 HANDLE handshake_handle;
151 std::string pipe_name;
152 #endif // OS_MACOSX
153 } options = {};
154 #if defined(OS_MACOSX)
155 options.handshake_fd = -1;
156 #elif defined(OS_WIN)
157 options.handshake_handle = INVALID_HANDLE_VALUE;
158 #endif
159
160 const option long_options[] = {
161 {"annotation", required_argument, nullptr, kOptionAnnotation},
162 {"database", required_argument, nullptr, kOptionDatabase},
163 #if defined(OS_MACOSX)
164 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD},
165 {"mach-service", required_argument, nullptr, kOptionMachService},
166 {"reset-own-crash-exception-port-to-system-default",
167 no_argument,
168 nullptr,
169 kOptionResetOwnCrashExceptionPortToSystemDefault},
170 #elif defined(OS_WIN)
171 {"handshake-handle", required_argument, nullptr, kOptionHandshakeHandle},
172 {"pipe-name", required_argument, nullptr, kOptionPipeName},
173 #endif // OS_MACOSX
174 {"url", required_argument, nullptr, kOptionURL},
175 {"help", no_argument, nullptr, kOptionHelp},
176 {"version", no_argument, nullptr, kOptionVersion},
177 {nullptr, 0, nullptr, 0},
178 };
179
180 int opt;
181 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) {
182 switch (opt) {
183 case kOptionAnnotation: {
184 std::string key;
185 std::string value;
186 if (!SplitString(optarg, '=', &key, &value)) {
187 ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE");
188 return EXIT_FAILURE;
189 }
190 std::string old_value;
191 if (!MapInsertOrReplace(&options.annotations, key, value, &old_value)) {
192 LOG(WARNING) << "duplicate key " << key << ", discarding value "
193 << old_value;
194 }
195 break;
196 }
197 case kOptionDatabase: {
198 options.database = optarg;
199 break;
200 }
201 #if defined(OS_MACOSX)
202 case kOptionHandshakeFD: {
203 if (!StringToNumber(optarg, &options.handshake_fd) ||
204 options.handshake_fd < 0) {
205 ToolSupport::UsageHint(me,
206 "--handshake-fd requires a file descriptor");
207 return EXIT_FAILURE;
208 }
209 break;
210 }
211 case kOptionMachService: {
212 options.mach_service = optarg;
213 break;
214 }
215 case kOptionResetOwnCrashExceptionPortToSystemDefault: {
216 options.reset_own_crash_exception_port_to_system_default = true;
217 break;
218 }
219 #elif defined(OS_WIN)
220 case kOptionHandshakeHandle: {
221 // Use unsigned int, because the handle was presented by the client in
222 // 0x%x format.
223 unsigned int handle_uint;
224 if (!StringToNumber(optarg, &handle_uint) ||
225 (options.handshake_handle = IntToHandle(handle_uint)) ==
226 INVALID_HANDLE_VALUE) {
227 ToolSupport::UsageHint(me, "--handshake-handle requires a HANDLE");
228 return EXIT_FAILURE;
229 }
230 break;
231 }
232 case kOptionPipeName: {
233 options.pipe_name = optarg;
234 break;
235 }
236 #endif // OS_MACOSX
237 case kOptionURL: {
238 options.url = optarg;
239 break;
240 }
241 case kOptionHelp: {
242 Usage(me);
243 return EXIT_SUCCESS;
244 }
245 case kOptionVersion: {
246 ToolSupport::Version(me);
247 return EXIT_SUCCESS;
248 }
249 default: {
250 ToolSupport::UsageHint(me, nullptr);
251 return EXIT_FAILURE;
252 }
253 }
254 }
255 argc -= optind;
256 argv += optind;
257
258 #if defined(OS_MACOSX)
259 if (options.handshake_fd < 0 && options.mach_service.empty()) {
260 ToolSupport::UsageHint(me, "--handshake-fd or --mach-service is required");
261 return EXIT_FAILURE;
262 }
263 if (options.handshake_fd >= 0 && !options.mach_service.empty()) {
264 ToolSupport::UsageHint(
265 me, "--handshake-fd and --mach-service are incompatible");
266 return EXIT_FAILURE;
267 }
268 #elif defined(OS_WIN)
269 if (options.handshake_handle == INVALID_HANDLE_VALUE &&
270 options.pipe_name.empty()) {
271 ToolSupport::UsageHint(me, "--handshake-handle or --pipe-name is required");
272 return EXIT_FAILURE;
273 }
274 if (options.handshake_handle != INVALID_HANDLE_VALUE &&
275 !options.pipe_name.empty()) {
276 ToolSupport::UsageHint(
277 me, "--handshake-handle and --pipe-name are incompatible");
278 return EXIT_FAILURE;
279 }
280 #endif // OS_MACOSX
281
282 if (!options.database) {
283 ToolSupport::UsageHint(me, "--database is required");
284 return EXIT_FAILURE;
285 }
286
287 if (argc) {
288 ToolSupport::UsageHint(me, nullptr);
289 return EXIT_FAILURE;
290 }
291
292 #if defined(OS_MACOSX)
293 if (options.mach_service.empty()) {
294 // Don’t do this when being run by launchd. See launchd.plist(5).
295 CloseStdinAndStdout();
296 }
297
298 if (options.reset_own_crash_exception_port_to_system_default) {
299 CrashpadClient::UseSystemDefaultHandler();
300 }
301
302 base::mac::ScopedMachReceiveRight receive_right;
303
304 if (options.handshake_fd >= 0) {
305 receive_right.reset(
306 ChildPortHandshake::RunServerForFD(
307 base::ScopedFD(options.handshake_fd),
308 ChildPortHandshake::PortRightType::kReceiveRight));
309 } else if (!options.mach_service.empty()) {
310 receive_right = BootstrapCheckIn(options.mach_service);
311 }
312
313 if (!receive_right.is_valid()) {
314 return EXIT_FAILURE;
315 }
316
317 ExceptionHandlerServer exception_handler_server(
318 crashpad::move(receive_right), !options.mach_service.empty());
319 base::AutoReset<ExceptionHandlerServer*> reset_g_exception_handler_server(
320 &g_exception_handler_server, &exception_handler_server);
321
322 struct sigaction old_sa;
323 ScopedResetSIGTERM reset_sigterm;
324 if (!options.mach_service.empty()) {
325 // When running from launchd, no no-senders notification could ever be
326 // triggered, because launchd maintains a send right to the service. When
327 // launchd wants the job to exit, it will send a SIGTERM. See
328 // launchd.plist(5).
329 //
330 // Set up a SIGTERM handler that will call exception_handler_server.Stop().
331 struct sigaction sa = {};
332 sigemptyset(&sa.sa_mask);
333 sa.sa_flags = SA_SIGINFO;
334 sa.sa_sigaction = HandleSIGTERM;
335 int rv = sigaction(SIGTERM, &sa, &old_sa);
336 PCHECK(rv == 0) << "sigaction";
337 reset_sigterm.reset(&old_sa);
338 }
339 #elif defined(OS_WIN)
340 ExceptionHandlerServer exception_handler_server(!options.pipe_name.empty());
341
342 std::string pipe_name;
343 if (!options.pipe_name.empty()) {
344 exception_handler_server.SetPipeName(base::UTF8ToUTF16(options.pipe_name));
345 } else if (options.handshake_handle != INVALID_HANDLE_VALUE) {
346 std::wstring pipe_name = exception_handler_server.CreatePipe();
347
348 uint32_t pipe_name_length = static_cast<uint32_t>(pipe_name.size());
349 if (!LoggingWriteFile(options.handshake_handle,
350 &pipe_name_length,
351 sizeof(pipe_name_length))) {
352 return EXIT_FAILURE;
353 }
354 if (!LoggingWriteFile(options.handshake_handle,
355 pipe_name.c_str(),
356 pipe_name.size() * sizeof(pipe_name[0]))) {
357 return EXIT_FAILURE;
358 }
359 }
360 #endif // OS_MACOSX
361
362 scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize(
363 base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType(
364 options.database))));
365 if (!database) {
366 return EXIT_FAILURE;
367 }
368
369 CrashReportUploadThread upload_thread(database.get(), options.url);
370 upload_thread.Start();
371
372 CrashReportExceptionHandler exception_handler(
373 database.get(), &upload_thread, &options.annotations);
374
375 exception_handler_server.Run(&exception_handler);
376
377 upload_thread.Stop();
378
379 return EXIT_SUCCESS;
380 }
381
382 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698