| 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 <stdlib.h> | 16 #include <stdlib.h> |
| 17 | 17 |
| 18 #include <map> | 18 #include <map> |
| 19 #include <string> | 19 #include <string> |
| 20 | 20 |
| 21 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
| 22 #include "base/files/scoped_file.h" | 22 #include "base/files/scoped_file.h" |
| 23 #include "base/logging.h" | 23 #include "base/logging.h" |
| 24 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/strings/utf_string_conversions.h" |
| 25 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 26 #include "client/crash_report_database.h" | 27 #include "client/crash_report_database.h" |
| 27 #include "client/crashpad_client.h" | 28 #include "client/crashpad_client.h" |
| 28 #include "tools/tool_support.h" | 29 #include "tools/tool_support.h" |
| 29 #include "handler/crash_report_upload_thread.h" | 30 #include "handler/crash_report_upload_thread.h" |
| 31 #include "util/file/file_io.h" |
| 30 #include "util/stdlib/map_insert.h" | 32 #include "util/stdlib/map_insert.h" |
| 31 #include "util/stdlib/string_number_conversion.h" | 33 #include "util/stdlib/string_number_conversion.h" |
| 32 #include "util/string/split_string.h" | 34 #include "util/string/split_string.h" |
| 33 #include "util/synchronization/semaphore.h" | 35 #include "util/synchronization/semaphore.h" |
| 34 | 36 |
| 35 #if defined(OS_MACOSX) | 37 #if defined(OS_MACOSX) |
| 36 #include <libgen.h> | 38 #include <libgen.h> |
| 37 #include "base/mac/scoped_mach_port.h" | 39 #include "base/mac/scoped_mach_port.h" |
| 38 #include "handler/mac/crash_report_exception_handler.h" | 40 #include "handler/mac/crash_report_exception_handler.h" |
| 39 #include "handler/mac/exception_handler_server.h" | 41 #include "handler/mac/exception_handler_server.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 55 "Crashpad's exception handler server.\n" | 57 "Crashpad's exception handler server.\n" |
| 56 "\n" | 58 "\n" |
| 57 " --annotation=KEY=VALUE set a process annotation in each crash report\n" | 59 " --annotation=KEY=VALUE set a process annotation in each crash report\n" |
| 58 " --database=PATH store the crash report database at PATH\n" | 60 " --database=PATH store the crash report database at PATH\n" |
| 59 #if defined(OS_MACOSX) | 61 #if defined(OS_MACOSX) |
| 60 " --handshake-fd=FD establish communication with the client over FD\n
" | 62 " --handshake-fd=FD establish communication with the client over FD\n
" |
| 61 " --mach-service=SERVICE register SERVICE with the bootstrap server\n" | 63 " --mach-service=SERVICE register SERVICE with the bootstrap server\n" |
| 62 " --reset-own-crash-exception-port-to-system-default\n" | 64 " --reset-own-crash-exception-port-to-system-default\n" |
| 63 " reset the server's exception handler to default\n
" | 65 " reset the server's exception handler to default\n
" |
| 64 #elif defined(OS_WIN) | 66 #elif defined(OS_WIN) |
| 65 " --persistent continue running after all clients exit\n" | 67 " --handshake-handle=HANDLE\n" |
| 68 " create a new pipe and send its name via HANDLE\n" |
| 66 " --pipe-name=PIPE communicate with the client over PIPE\n" | 69 " --pipe-name=PIPE communicate with the client over PIPE\n" |
| 67 #endif // OS_MACOSX | 70 #endif // OS_MACOSX |
| 68 " --url=URL send crash reports to this Breakpad server URL,\n
" | 71 " --url=URL send crash reports to this Breakpad server URL,\n
" |
| 69 " only if uploads are enabled for the database\n" | 72 " only if uploads are enabled for the database\n" |
| 70 " --help display this help and exit\n" | 73 " --help display this help and exit\n" |
| 71 " --version output version information and exit\n", | 74 " --version output version information and exit\n", |
| 72 me.value().c_str()); | 75 me.value().c_str()); |
| 73 ToolSupport::UsageTail(me); | 76 ToolSupport::UsageTail(me); |
| 74 } | 77 } |
| 75 | 78 |
| 76 int HandlerMain(int argc, char* argv[]) { | 79 int HandlerMain(int argc, char* argv[]) { |
| 77 const base::FilePath argv0( | 80 const base::FilePath argv0( |
| 78 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); | 81 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); |
| 79 const base::FilePath me(argv0.BaseName()); | 82 const base::FilePath me(argv0.BaseName()); |
| 80 | 83 |
| 81 enum OptionFlags { | 84 enum OptionFlags { |
| 82 // Long options without short equivalents. | 85 // Long options without short equivalents. |
| 83 kOptionLastChar = 255, | 86 kOptionLastChar = 255, |
| 84 kOptionAnnotation, | 87 kOptionAnnotation, |
| 85 kOptionDatabase, | 88 kOptionDatabase, |
| 86 #if defined(OS_MACOSX) | 89 #if defined(OS_MACOSX) |
| 87 kOptionHandshakeFD, | 90 kOptionHandshakeFD, |
| 88 kOptionMachService, | 91 kOptionMachService, |
| 89 kOptionResetOwnCrashExceptionPortToSystemDefault, | 92 kOptionResetOwnCrashExceptionPortToSystemDefault, |
| 90 #elif defined(OS_WIN) | 93 #elif defined(OS_WIN) |
| 91 kOptionPersistent, | 94 kOptionHandshakeHandle, |
| 92 kOptionPipeName, | 95 kOptionPipeName, |
| 93 #endif // OS_MACOSX | 96 #endif // OS_MACOSX |
| 94 kOptionURL, | 97 kOptionURL, |
| 95 | 98 |
| 96 // Standard options. | 99 // Standard options. |
| 97 kOptionHelp = -2, | 100 kOptionHelp = -2, |
| 98 kOptionVersion = -3, | 101 kOptionVersion = -3, |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 struct { | 104 struct { |
| 102 std::map<std::string, std::string> annotations; | 105 std::map<std::string, std::string> annotations; |
| 103 std::string url; | 106 std::string url; |
| 104 const char* database; | 107 const char* database; |
| 105 #if defined(OS_MACOSX) | 108 #if defined(OS_MACOSX) |
| 106 int handshake_fd; | 109 int handshake_fd; |
| 107 std::string mach_service; | 110 std::string mach_service; |
| 108 bool reset_own_crash_exception_port_to_system_default; | 111 bool reset_own_crash_exception_port_to_system_default; |
| 109 #elif defined(OS_WIN) | 112 #elif defined(OS_WIN) |
| 110 bool persistent; | 113 HANDLE handshake_handle; |
| 111 std::string pipe_name; | 114 std::string pipe_name; |
| 112 #endif // OS_MACOSX | 115 #endif // OS_MACOSX |
| 113 } options = {}; | 116 } options = {}; |
| 114 #if defined(OS_MACOSX) | 117 #if defined(OS_MACOSX) |
| 115 options.handshake_fd = -1; | 118 options.handshake_fd = -1; |
| 116 options.reset_own_crash_exception_port_to_system_default = false; | 119 #elif defined(OS_WIN) |
| 120 options.handshake_handle = INVALID_HANDLE_VALUE; |
| 117 #endif | 121 #endif |
| 118 | 122 |
| 119 const option long_options[] = { | 123 const option long_options[] = { |
| 120 {"annotation", required_argument, nullptr, kOptionAnnotation}, | 124 {"annotation", required_argument, nullptr, kOptionAnnotation}, |
| 121 {"database", required_argument, nullptr, kOptionDatabase}, | 125 {"database", required_argument, nullptr, kOptionDatabase}, |
| 122 #if defined(OS_MACOSX) | 126 #if defined(OS_MACOSX) |
| 123 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, | 127 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| 124 {"mach-service", required_argument, nullptr, kOptionMachService}, | 128 {"mach-service", required_argument, nullptr, kOptionMachService}, |
| 125 {"reset-own-crash-exception-port-to-system-default", | 129 {"reset-own-crash-exception-port-to-system-default", |
| 126 no_argument, | 130 no_argument, |
| 127 nullptr, | 131 nullptr, |
| 128 kOptionResetOwnCrashExceptionPortToSystemDefault}, | 132 kOptionResetOwnCrashExceptionPortToSystemDefault}, |
| 129 #elif defined(OS_WIN) | 133 #elif defined(OS_WIN) |
| 130 {"persistent", no_argument, nullptr, kOptionPersistent}, | 134 {"handshake-handle", required_argument, nullptr, kOptionHandshakeHandle}, |
| 131 {"pipe-name", required_argument, nullptr, kOptionPipeName}, | 135 {"pipe-name", required_argument, nullptr, kOptionPipeName}, |
| 132 #endif // OS_MACOSX | 136 #endif // OS_MACOSX |
| 133 {"url", required_argument, nullptr, kOptionURL}, | 137 {"url", required_argument, nullptr, kOptionURL}, |
| 134 {"help", no_argument, nullptr, kOptionHelp}, | 138 {"help", no_argument, nullptr, kOptionHelp}, |
| 135 {"version", no_argument, nullptr, kOptionVersion}, | 139 {"version", no_argument, nullptr, kOptionVersion}, |
| 136 {nullptr, 0, nullptr, 0}, | 140 {nullptr, 0, nullptr, 0}, |
| 137 }; | 141 }; |
| 138 | 142 |
| 139 int opt; | 143 int opt; |
| 140 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { | 144 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 169 } | 173 } |
| 170 case kOptionMachService: { | 174 case kOptionMachService: { |
| 171 options.mach_service = optarg; | 175 options.mach_service = optarg; |
| 172 break; | 176 break; |
| 173 } | 177 } |
| 174 case kOptionResetOwnCrashExceptionPortToSystemDefault: { | 178 case kOptionResetOwnCrashExceptionPortToSystemDefault: { |
| 175 options.reset_own_crash_exception_port_to_system_default = true; | 179 options.reset_own_crash_exception_port_to_system_default = true; |
| 176 break; | 180 break; |
| 177 } | 181 } |
| 178 #elif defined(OS_WIN) | 182 #elif defined(OS_WIN) |
| 179 case kOptionPersistent: { | 183 case kOptionHandshakeHandle: { |
| 180 options.persistent = true; | 184 // According to |
| 185 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203, |
| 186 // HANDLEs are always 32 bits. This construction is used to read a full |
| 187 // 32-bit number presented by the client, which uses 0x%x format, and |
| 188 // sign-extend it. |
| 189 unsigned int handle_uint; |
| 190 if (!StringToNumber(optarg, &handle_uint) || |
| 191 (options.handshake_handle = reinterpret_cast<HANDLE>( |
| 192 static_cast<int>(handle_uint))) == INVALID_HANDLE_VALUE) { |
| 193 ToolSupport::UsageHint(me, "--handshake-handle requires a HANDLE"); |
| 194 return EXIT_FAILURE; |
| 195 } |
| 181 break; | 196 break; |
| 182 } | 197 } |
| 183 case kOptionPipeName: { | 198 case kOptionPipeName: { |
| 184 options.pipe_name = optarg; | 199 options.pipe_name = optarg; |
| 185 break; | 200 break; |
| 186 } | 201 } |
| 187 #endif // OS_MACOSX | 202 #endif // OS_MACOSX |
| 188 case kOptionURL: { | 203 case kOptionURL: { |
| 189 options.url = optarg; | 204 options.url = optarg; |
| 190 break; | 205 break; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 210 if (options.handshake_fd < 0 && options.mach_service.empty()) { | 225 if (options.handshake_fd < 0 && options.mach_service.empty()) { |
| 211 ToolSupport::UsageHint(me, "--handshake-fd or --mach-service is required"); | 226 ToolSupport::UsageHint(me, "--handshake-fd or --mach-service is required"); |
| 212 return EXIT_FAILURE; | 227 return EXIT_FAILURE; |
| 213 } | 228 } |
| 214 if (options.handshake_fd >= 0 && !options.mach_service.empty()) { | 229 if (options.handshake_fd >= 0 && !options.mach_service.empty()) { |
| 215 ToolSupport::UsageHint( | 230 ToolSupport::UsageHint( |
| 216 me, "--handshake-fd and --mach-service are incompatible"); | 231 me, "--handshake-fd and --mach-service are incompatible"); |
| 217 return EXIT_FAILURE; | 232 return EXIT_FAILURE; |
| 218 } | 233 } |
| 219 #elif defined(OS_WIN) | 234 #elif defined(OS_WIN) |
| 220 if (options.pipe_name.empty()) { | 235 if (options.handshake_handle == INVALID_HANDLE_VALUE && |
| 221 ToolSupport::UsageHint(me, "--pipe-name is required"); | 236 options.pipe_name.empty()) { |
| 237 ToolSupport::UsageHint(me, "--handshake-handle or --pipe-name is required"); |
| 222 return EXIT_FAILURE; | 238 return EXIT_FAILURE; |
| 223 } | 239 } |
| 224 #endif | 240 if (options.handshake_handle != INVALID_HANDLE_VALUE && |
| 241 !options.pipe_name.empty()) { |
| 242 ToolSupport::UsageHint( |
| 243 me, "--handshake-handle and --pipe-name are incompatible"); |
| 244 return EXIT_FAILURE; |
| 245 } |
| 246 #endif // OS_MACOSX |
| 225 | 247 |
| 226 if (!options.database) { | 248 if (!options.database) { |
| 227 ToolSupport::UsageHint(me, "--database is required"); | 249 ToolSupport::UsageHint(me, "--database is required"); |
| 228 return EXIT_FAILURE; | 250 return EXIT_FAILURE; |
| 229 } | 251 } |
| 230 | 252 |
| 231 if (argc) { | 253 if (argc) { |
| 232 ToolSupport::UsageHint(me, nullptr); | 254 ToolSupport::UsageHint(me, nullptr); |
| 233 return EXIT_FAILURE; | 255 return EXIT_FAILURE; |
| 234 } | 256 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 254 receive_right = BootstrapCheckIn(options.mach_service); | 276 receive_right = BootstrapCheckIn(options.mach_service); |
| 255 } | 277 } |
| 256 | 278 |
| 257 if (!receive_right.is_valid()) { | 279 if (!receive_right.is_valid()) { |
| 258 return EXIT_FAILURE; | 280 return EXIT_FAILURE; |
| 259 } | 281 } |
| 260 | 282 |
| 261 ExceptionHandlerServer exception_handler_server( | 283 ExceptionHandlerServer exception_handler_server( |
| 262 receive_right.Pass(), !options.mach_service.empty()); | 284 receive_right.Pass(), !options.mach_service.empty()); |
| 263 #elif defined(OS_WIN) | 285 #elif defined(OS_WIN) |
| 264 ExceptionHandlerServer exception_handler_server(options.pipe_name, | 286 ExceptionHandlerServer exception_handler_server(!options.pipe_name.empty()); |
| 265 options.persistent); | 287 |
| 288 std::string pipe_name; |
| 289 if (!options.pipe_name.empty()) { |
| 290 exception_handler_server.SetPipeName(base::UTF8ToUTF16(options.pipe_name)); |
| 291 } else if (options.handshake_handle != INVALID_HANDLE_VALUE) { |
| 292 std::wstring pipe_name = exception_handler_server.CreatePipe(); |
| 293 |
| 294 uint32_t pipe_name_length = static_cast<uint32_t>(pipe_name.size()); |
| 295 if (!LoggingWriteFile(options.handshake_handle, |
| 296 &pipe_name_length, |
| 297 sizeof(pipe_name_length))) { |
| 298 return EXIT_FAILURE; |
| 299 } |
| 300 if (!LoggingWriteFile(options.handshake_handle, |
| 301 pipe_name.c_str(), |
| 302 pipe_name.size() * sizeof(pipe_name[0]))) { |
| 303 return EXIT_FAILURE; |
| 304 } |
| 305 } |
| 266 #endif // OS_MACOSX | 306 #endif // OS_MACOSX |
| 267 | 307 |
| 268 scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize( | 308 scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize( |
| 269 base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType( | 309 base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType( |
| 270 options.database)))); | 310 options.database)))); |
| 271 if (!database) { | 311 if (!database) { |
| 272 return EXIT_FAILURE; | 312 return EXIT_FAILURE; |
| 273 } | 313 } |
| 274 | 314 |
| 275 CrashReportUploadThread upload_thread(database.get(), options.url); | 315 CrashReportUploadThread upload_thread(database.get(), options.url); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 290 | 330 |
| 291 #if defined(OS_MACOSX) | 331 #if defined(OS_MACOSX) |
| 292 int main(int argc, char* argv[]) { | 332 int main(int argc, char* argv[]) { |
| 293 return crashpad::HandlerMain(argc, argv); | 333 return crashpad::HandlerMain(argc, argv); |
| 294 } | 334 } |
| 295 #elif defined(OS_WIN) | 335 #elif defined(OS_WIN) |
| 296 int wmain(int argc, wchar_t* argv[]) { | 336 int wmain(int argc, wchar_t* argv[]) { |
| 297 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HandlerMain); | 337 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HandlerMain); |
| 298 } | 338 } |
| 299 #endif // OS_MACOSX | 339 #endif // OS_MACOSX |
| OLD | NEW |