OLD | NEW |
(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/files/file_path.h" |
| 24 #include "base/files/scoped_file.h" |
| 25 #include "base/logging.h" |
| 26 #include "base/memory/scoped_ptr.h" |
| 27 #include "base/strings/utf_string_conversions.h" |
| 28 #include "build/build_config.h" |
| 29 #include "client/crash_report_database.h" |
| 30 #include "client/crashpad_client.h" |
| 31 #include "tools/tool_support.h" |
| 32 #include "handler/crash_report_upload_thread.h" |
| 33 #include "util/file/file_io.h" |
| 34 #include "util/stdlib/map_insert.h" |
| 35 #include "util/stdlib/string_number_conversion.h" |
| 36 #include "util/string/split_string.h" |
| 37 #include "util/synchronization/semaphore.h" |
| 38 |
| 39 #if defined(OS_MACOSX) |
| 40 #include <libgen.h> |
| 41 #include "base/mac/scoped_mach_port.h" |
| 42 #include "handler/mac/crash_report_exception_handler.h" |
| 43 #include "handler/mac/exception_handler_server.h" |
| 44 #include "util/mach/child_port_handshake.h" |
| 45 #include "util/mach/mach_extensions.h" |
| 46 #include "util/posix/close_stdio.h" |
| 47 #elif defined(OS_WIN) |
| 48 #include <windows.h> |
| 49 #include "handler/win/crash_report_exception_handler.h" |
| 50 #include "util/win/exception_handler_server.h" |
| 51 #include "util/win/handle.h" |
| 52 #endif // OS_MACOSX |
| 53 |
| 54 namespace crashpad { |
| 55 |
| 56 namespace { |
| 57 |
| 58 void Usage(const base::FilePath& me) { |
| 59 fprintf(stderr, |
| 60 "Usage: %" PRFilePath " [OPTION]...\n" |
| 61 "Crashpad's exception handler server.\n" |
| 62 "\n" |
| 63 " --annotation=KEY=VALUE set a process annotation in each crash report\n" |
| 64 " --database=PATH store the crash report database at PATH\n" |
| 65 #if defined(OS_MACOSX) |
| 66 " --handshake-fd=FD establish communication with the client over FD\n
" |
| 67 " --mach-service=SERVICE register SERVICE with the bootstrap server\n" |
| 68 " --reset-own-crash-exception-port-to-system-default\n" |
| 69 " reset the server's exception handler to default\n
" |
| 70 #elif defined(OS_WIN) |
| 71 " --handshake-handle=HANDLE\n" |
| 72 " create a new pipe and send its name via HANDLE\n" |
| 73 " --pipe-name=PIPE communicate with the client over PIPE\n" |
| 74 #endif // OS_MACOSX |
| 75 " --url=URL send crash reports to this Breakpad server URL,\n
" |
| 76 " only if uploads are enabled for the database\n" |
| 77 " --help display this help and exit\n" |
| 78 " --version output version information and exit\n", |
| 79 me.value().c_str()); |
| 80 ToolSupport::UsageTail(me); |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 int HandlerMain(int argc, char* argv[]) { |
| 86 const base::FilePath argv0( |
| 87 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); |
| 88 const base::FilePath me(argv0.BaseName()); |
| 89 |
| 90 enum OptionFlags { |
| 91 // Long options without short equivalents. |
| 92 kOptionLastChar = 255, |
| 93 kOptionAnnotation, |
| 94 kOptionDatabase, |
| 95 #if defined(OS_MACOSX) |
| 96 kOptionHandshakeFD, |
| 97 kOptionMachService, |
| 98 kOptionResetOwnCrashExceptionPortToSystemDefault, |
| 99 #elif defined(OS_WIN) |
| 100 kOptionHandshakeHandle, |
| 101 kOptionPipeName, |
| 102 #endif // OS_MACOSX |
| 103 kOptionURL, |
| 104 |
| 105 // Standard options. |
| 106 kOptionHelp = -2, |
| 107 kOptionVersion = -3, |
| 108 }; |
| 109 |
| 110 struct { |
| 111 std::map<std::string, std::string> annotations; |
| 112 std::string url; |
| 113 const char* database; |
| 114 #if defined(OS_MACOSX) |
| 115 int handshake_fd; |
| 116 std::string mach_service; |
| 117 bool reset_own_crash_exception_port_to_system_default; |
| 118 #elif defined(OS_WIN) |
| 119 HANDLE handshake_handle; |
| 120 std::string pipe_name; |
| 121 #endif // OS_MACOSX |
| 122 } options = {}; |
| 123 #if defined(OS_MACOSX) |
| 124 options.handshake_fd = -1; |
| 125 #elif defined(OS_WIN) |
| 126 options.handshake_handle = INVALID_HANDLE_VALUE; |
| 127 #endif |
| 128 |
| 129 const option long_options[] = { |
| 130 {"annotation", required_argument, nullptr, kOptionAnnotation}, |
| 131 {"database", required_argument, nullptr, kOptionDatabase}, |
| 132 #if defined(OS_MACOSX) |
| 133 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| 134 {"mach-service", required_argument, nullptr, kOptionMachService}, |
| 135 {"reset-own-crash-exception-port-to-system-default", |
| 136 no_argument, |
| 137 nullptr, |
| 138 kOptionResetOwnCrashExceptionPortToSystemDefault}, |
| 139 #elif defined(OS_WIN) |
| 140 {"handshake-handle", required_argument, nullptr, kOptionHandshakeHandle}, |
| 141 {"pipe-name", required_argument, nullptr, kOptionPipeName}, |
| 142 #endif // OS_MACOSX |
| 143 {"url", required_argument, nullptr, kOptionURL}, |
| 144 {"help", no_argument, nullptr, kOptionHelp}, |
| 145 {"version", no_argument, nullptr, kOptionVersion}, |
| 146 {nullptr, 0, nullptr, 0}, |
| 147 }; |
| 148 |
| 149 int opt; |
| 150 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { |
| 151 switch (opt) { |
| 152 case kOptionAnnotation: { |
| 153 std::string key; |
| 154 std::string value; |
| 155 if (!SplitString(optarg, '=', &key, &value)) { |
| 156 ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE"); |
| 157 return EXIT_FAILURE; |
| 158 } |
| 159 std::string old_value; |
| 160 if (!MapInsertOrReplace(&options.annotations, key, value, &old_value)) { |
| 161 LOG(WARNING) << "duplicate key " << key << ", discarding value " |
| 162 << old_value; |
| 163 } |
| 164 break; |
| 165 } |
| 166 case kOptionDatabase: { |
| 167 options.database = optarg; |
| 168 break; |
| 169 } |
| 170 #if defined(OS_MACOSX) |
| 171 case kOptionHandshakeFD: { |
| 172 if (!StringToNumber(optarg, &options.handshake_fd) || |
| 173 options.handshake_fd < 0) { |
| 174 ToolSupport::UsageHint(me, |
| 175 "--handshake-fd requires a file descriptor"); |
| 176 return EXIT_FAILURE; |
| 177 } |
| 178 break; |
| 179 } |
| 180 case kOptionMachService: { |
| 181 options.mach_service = optarg; |
| 182 break; |
| 183 } |
| 184 case kOptionResetOwnCrashExceptionPortToSystemDefault: { |
| 185 options.reset_own_crash_exception_port_to_system_default = true; |
| 186 break; |
| 187 } |
| 188 #elif defined(OS_WIN) |
| 189 case kOptionHandshakeHandle: { |
| 190 // Use unsigned int, because the handle was presented by the client in |
| 191 // 0x%x format. |
| 192 unsigned int handle_uint; |
| 193 if (!StringToNumber(optarg, &handle_uint) || |
| 194 (options.handshake_handle = IntToHandle(handle_uint)) == |
| 195 INVALID_HANDLE_VALUE) { |
| 196 ToolSupport::UsageHint(me, "--handshake-handle requires a HANDLE"); |
| 197 return EXIT_FAILURE; |
| 198 } |
| 199 break; |
| 200 } |
| 201 case kOptionPipeName: { |
| 202 options.pipe_name = optarg; |
| 203 break; |
| 204 } |
| 205 #endif // OS_MACOSX |
| 206 case kOptionURL: { |
| 207 options.url = optarg; |
| 208 break; |
| 209 } |
| 210 case kOptionHelp: { |
| 211 Usage(me); |
| 212 return EXIT_SUCCESS; |
| 213 } |
| 214 case kOptionVersion: { |
| 215 ToolSupport::Version(me); |
| 216 return EXIT_SUCCESS; |
| 217 } |
| 218 default: { |
| 219 ToolSupport::UsageHint(me, nullptr); |
| 220 return EXIT_FAILURE; |
| 221 } |
| 222 } |
| 223 } |
| 224 argc -= optind; |
| 225 argv += optind; |
| 226 |
| 227 #if defined(OS_MACOSX) |
| 228 if (options.handshake_fd < 0 && options.mach_service.empty()) { |
| 229 ToolSupport::UsageHint(me, "--handshake-fd or --mach-service is required"); |
| 230 return EXIT_FAILURE; |
| 231 } |
| 232 if (options.handshake_fd >= 0 && !options.mach_service.empty()) { |
| 233 ToolSupport::UsageHint( |
| 234 me, "--handshake-fd and --mach-service are incompatible"); |
| 235 return EXIT_FAILURE; |
| 236 } |
| 237 #elif defined(OS_WIN) |
| 238 if (options.handshake_handle == INVALID_HANDLE_VALUE && |
| 239 options.pipe_name.empty()) { |
| 240 ToolSupport::UsageHint(me, "--handshake-handle or --pipe-name is required"); |
| 241 return EXIT_FAILURE; |
| 242 } |
| 243 if (options.handshake_handle != INVALID_HANDLE_VALUE && |
| 244 !options.pipe_name.empty()) { |
| 245 ToolSupport::UsageHint( |
| 246 me, "--handshake-handle and --pipe-name are incompatible"); |
| 247 return EXIT_FAILURE; |
| 248 } |
| 249 #endif // OS_MACOSX |
| 250 |
| 251 if (!options.database) { |
| 252 ToolSupport::UsageHint(me, "--database is required"); |
| 253 return EXIT_FAILURE; |
| 254 } |
| 255 |
| 256 if (argc) { |
| 257 ToolSupport::UsageHint(me, nullptr); |
| 258 return EXIT_FAILURE; |
| 259 } |
| 260 |
| 261 #if defined(OS_MACOSX) |
| 262 if (options.mach_service.empty()) { |
| 263 // Don’t do this when being run by launchd. See launchd.plist(5). |
| 264 CloseStdinAndStdout(); |
| 265 } |
| 266 |
| 267 if (options.reset_own_crash_exception_port_to_system_default) { |
| 268 CrashpadClient::UseSystemDefaultHandler(); |
| 269 } |
| 270 |
| 271 base::mac::ScopedMachReceiveRight receive_right; |
| 272 |
| 273 if (options.handshake_fd >= 0) { |
| 274 receive_right.reset( |
| 275 ChildPortHandshake::RunServerForFD( |
| 276 base::ScopedFD(options.handshake_fd), |
| 277 ChildPortHandshake::PortRightType::kReceiveRight)); |
| 278 } else if (!options.mach_service.empty()) { |
| 279 receive_right = BootstrapCheckIn(options.mach_service); |
| 280 } |
| 281 |
| 282 if (!receive_right.is_valid()) { |
| 283 return EXIT_FAILURE; |
| 284 } |
| 285 |
| 286 ExceptionHandlerServer exception_handler_server( |
| 287 receive_right.Pass(), !options.mach_service.empty()); |
| 288 #elif defined(OS_WIN) |
| 289 ExceptionHandlerServer exception_handler_server(!options.pipe_name.empty()); |
| 290 |
| 291 std::string pipe_name; |
| 292 if (!options.pipe_name.empty()) { |
| 293 exception_handler_server.SetPipeName(base::UTF8ToUTF16(options.pipe_name)); |
| 294 } else if (options.handshake_handle != INVALID_HANDLE_VALUE) { |
| 295 std::wstring pipe_name = exception_handler_server.CreatePipe(); |
| 296 |
| 297 uint32_t pipe_name_length = static_cast<uint32_t>(pipe_name.size()); |
| 298 if (!LoggingWriteFile(options.handshake_handle, |
| 299 &pipe_name_length, |
| 300 sizeof(pipe_name_length))) { |
| 301 return EXIT_FAILURE; |
| 302 } |
| 303 if (!LoggingWriteFile(options.handshake_handle, |
| 304 pipe_name.c_str(), |
| 305 pipe_name.size() * sizeof(pipe_name[0]))) { |
| 306 return EXIT_FAILURE; |
| 307 } |
| 308 } |
| 309 #endif // OS_MACOSX |
| 310 |
| 311 scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize( |
| 312 base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType( |
| 313 options.database)))); |
| 314 if (!database) { |
| 315 return EXIT_FAILURE; |
| 316 } |
| 317 |
| 318 CrashReportUploadThread upload_thread(database.get(), options.url); |
| 319 upload_thread.Start(); |
| 320 |
| 321 CrashReportExceptionHandler exception_handler( |
| 322 database.get(), &upload_thread, &options.annotations); |
| 323 |
| 324 exception_handler_server.Run(&exception_handler); |
| 325 |
| 326 upload_thread.Stop(); |
| 327 |
| 328 return EXIT_SUCCESS; |
| 329 } |
| 330 |
| 331 } // namespace crashpad |
OLD | NEW |