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