| 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, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include <string> | 23 #include <string> |
| 24 #include <vector> | 24 #include <vector> |
| 25 | 25 |
| 26 #include "base/files/scoped_file.h" | 26 #include "base/files/scoped_file.h" |
| 27 #include "base/logging.h" | 27 #include "base/logging.h" |
| 28 #include "base/mac/mach_logging.h" | 28 #include "base/mac/mach_logging.h" |
| 29 #include "tools/tool_support.h" | 29 #include "tools/tool_support.h" |
| 30 #include "util/mach/exc_server_variants.h" | 30 #include "util/mach/exc_server_variants.h" |
| 31 #include "util/mach/exception_behaviors.h" | 31 #include "util/mach/exception_behaviors.h" |
| 32 #include "util/mach/mach_extensions.h" | 32 #include "util/mach/mach_extensions.h" |
| 33 #include "util/mach/mach_message.h" |
| 33 #include "util/mach/mach_message_server.h" | 34 #include "util/mach/mach_message_server.h" |
| 34 #include "util/mach/symbolic_constants_mach.h" | 35 #include "util/mach/symbolic_constants_mach.h" |
| 35 #include "util/posix/symbolic_constants_posix.h" | 36 #include "util/posix/symbolic_constants_posix.h" |
| 36 #include "util/stdlib/string_number_conversion.h" | 37 #include "util/stdlib/string_number_conversion.h" |
| 37 | 38 |
| 38 namespace crashpad { | 39 namespace crashpad { |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 struct Options { | 42 struct Options { |
| 42 std::string file_path; | 43 std::string file_path; |
| 43 std::string mach_service; | 44 std::string mach_service; |
| 44 FILE* file; | 45 FILE* file; |
| 45 int timeout_secs; | 46 int timeout_secs; |
| 46 MachMessageServer::Nonblocking nonblocking; | 47 bool has_timeout; |
| 47 MachMessageServer::Persistent persistent; | 48 MachMessageServer::Persistent persistent; |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 class ExceptionServer : public UniversalMachExcServer::Interface { | 51 class ExceptionServer : public UniversalMachExcServer::Interface { |
| 51 public: | 52 public: |
| 52 ExceptionServer(const Options& options, | 53 ExceptionServer(const Options& options, |
| 53 const std::string& me, | 54 const std::string& me, |
| 54 int* exceptions_handled) | 55 int* exceptions_handled) |
| 55 : UniversalMachExcServer::Interface(), | 56 : UniversalMachExcServer::Interface(), |
| 56 options_(options), | 57 options_(options), |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 int* exceptions_handled_; | 165 int* exceptions_handled_; |
| 165 }; | 166 }; |
| 166 | 167 |
| 167 void Usage(const std::string& me) { | 168 void Usage(const std::string& me) { |
| 168 fprintf(stderr, | 169 fprintf(stderr, |
| 169 "Usage: %s -m SERVICE [OPTION]...\n" | 170 "Usage: %s -m SERVICE [OPTION]...\n" |
| 170 "Catch Mach exceptions and display information about them.\n" | 171 "Catch Mach exceptions and display information about them.\n" |
| 171 "\n" | 172 "\n" |
| 172 " -f, --file=FILE append information to FILE instead of stdout\n" | 173 " -f, --file=FILE append information to FILE instead of stdout\n" |
| 173 " -m, --mach-service=SERVICE register SERVICE with the bootstrap server\n" | 174 " -m, --mach-service=SERVICE register SERVICE with the bootstrap server\n" |
| 174 " -n, --nonblocking don't block waiting for an exception to occur\n" | |
| 175 " -p, --persistent continue processing exceptions after the first\n" | 175 " -p, --persistent continue processing exceptions after the first\n" |
| 176 " -t, --timeout=TIMEOUT run for a maximum of TIMEOUT seconds\n" | 176 " -t, --timeout=TIMEOUT run for a maximum of TIMEOUT seconds\n" |
| 177 " --help display this help and exit\n" | 177 " --help display this help and exit\n" |
| 178 " --version output version information and exit\n", | 178 " --version output version information and exit\n", |
| 179 me.c_str()); | 179 me.c_str()); |
| 180 ToolSupport::UsageTail(me); | 180 ToolSupport::UsageTail(me); |
| 181 } | 181 } |
| 182 | 182 |
| 183 int CatchExceptionToolMain(int argc, char* argv[]) { | 183 int CatchExceptionToolMain(int argc, char* argv[]) { |
| 184 const std::string me(basename(argv[0])); | 184 const std::string me(basename(argv[0])); |
| 185 | 185 |
| 186 enum OptionFlags { | 186 enum OptionFlags { |
| 187 // “Short” (single-character) options. | 187 // “Short” (single-character) options. |
| 188 kOptionFile = 'f', | 188 kOptionFile = 'f', |
| 189 kOptionMachService = 'm', | 189 kOptionMachService = 'm', |
| 190 kOptionNonblocking = 'n', | |
| 191 kOptionPersistent = 'p', | 190 kOptionPersistent = 'p', |
| 192 kOptionTimeout = 't', | 191 kOptionTimeout = 't', |
| 193 | 192 |
| 194 // Long options without short equivalents. | 193 // Long options without short equivalents. |
| 195 kOptionLastChar = 255, | 194 kOptionLastChar = 255, |
| 196 | 195 |
| 197 // Standard options. | 196 // Standard options. |
| 198 kOptionHelp = -2, | 197 kOptionHelp = -2, |
| 199 kOptionVersion = -3, | 198 kOptionVersion = -3, |
| 200 }; | 199 }; |
| 201 | 200 |
| 202 Options options = {}; | 201 Options options = {}; |
| 203 | 202 |
| 204 const struct option long_options[] = { | 203 const struct option long_options[] = { |
| 205 {"file", required_argument, nullptr, kOptionFile}, | 204 {"file", required_argument, nullptr, kOptionFile}, |
| 206 {"mach-service", required_argument, nullptr, kOptionMachService}, | 205 {"mach-service", required_argument, nullptr, kOptionMachService}, |
| 207 {"nonblocking", no_argument, nullptr, kOptionNonblocking}, | |
| 208 {"persistent", no_argument, nullptr, kOptionPersistent}, | 206 {"persistent", no_argument, nullptr, kOptionPersistent}, |
| 209 {"timeout", required_argument, nullptr, kOptionTimeout}, | 207 {"timeout", required_argument, nullptr, kOptionTimeout}, |
| 210 {"help", no_argument, nullptr, kOptionHelp}, | 208 {"help", no_argument, nullptr, kOptionHelp}, |
| 211 {"version", no_argument, nullptr, kOptionVersion}, | 209 {"version", no_argument, nullptr, kOptionVersion}, |
| 212 {nullptr, 0, nullptr, 0}, | 210 {nullptr, 0, nullptr, 0}, |
| 213 }; | 211 }; |
| 214 | 212 |
| 215 int opt; | 213 int opt; |
| 216 while ((opt = getopt_long(argc, argv, "f:m:npt:", long_options, nullptr)) != | 214 while ((opt = getopt_long(argc, argv, "f:m:pt:", long_options, nullptr)) != |
| 217 -1) { | 215 -1) { |
| 218 switch (opt) { | 216 switch (opt) { |
| 219 case kOptionFile: | 217 case kOptionFile: |
| 220 options.file_path = optarg; | 218 options.file_path = optarg; |
| 221 break; | 219 break; |
| 222 case kOptionMachService: | 220 case kOptionMachService: |
| 223 options.mach_service = optarg; | 221 options.mach_service = optarg; |
| 224 break; | 222 break; |
| 225 case kOptionNonblocking: | |
| 226 options.nonblocking = MachMessageServer::kNonblocking; | |
| 227 break; | |
| 228 case kOptionPersistent: | 223 case kOptionPersistent: |
| 229 options.persistent = MachMessageServer::kPersistent; | 224 options.persistent = MachMessageServer::kPersistent; |
| 230 break; | 225 break; |
| 231 case kOptionTimeout: | 226 case kOptionTimeout: |
| 232 if (!StringToNumber(optarg, &options.timeout_secs) || | 227 if (!StringToNumber(optarg, &options.timeout_secs) || |
| 233 options.timeout_secs <= 0) { | 228 options.timeout_secs < 0) { |
| 234 ToolSupport::UsageHint(me, "-t requires a positive TIMEOUT"); | 229 ToolSupport::UsageHint(me, "-t requires a zero or positive TIMEOUT"); |
| 235 return EXIT_FAILURE; | 230 return EXIT_FAILURE; |
| 236 } | 231 } |
| 232 options.has_timeout = true; |
| 237 break; | 233 break; |
| 238 case kOptionHelp: | 234 case kOptionHelp: |
| 239 Usage(me); | 235 Usage(me); |
| 240 return EXIT_SUCCESS; | 236 return EXIT_SUCCESS; |
| 241 case kOptionVersion: | 237 case kOptionVersion: |
| 242 ToolSupport::Version(me); | 238 ToolSupport::Version(me); |
| 243 return EXIT_SUCCESS; | 239 return EXIT_SUCCESS; |
| 244 default: | 240 default: |
| 245 ToolSupport::UsageHint(me, nullptr); | 241 ToolSupport::UsageHint(me, nullptr); |
| 246 return EXIT_FAILURE; | 242 return EXIT_FAILURE; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 ExceptionServer exception_server(options, me, &exceptions_handled); | 274 ExceptionServer exception_server(options, me, &exceptions_handled); |
| 279 UniversalMachExcServer universal_mach_exc_server(&exception_server); | 275 UniversalMachExcServer universal_mach_exc_server(&exception_server); |
| 280 | 276 |
| 281 // Assume that if persistent mode has been requested, it’s desirable to ignore | 277 // Assume that if persistent mode has been requested, it’s desirable to ignore |
| 282 // large messages and keep running. | 278 // large messages and keep running. |
| 283 MachMessageServer::ReceiveLarge receive_large = | 279 MachMessageServer::ReceiveLarge receive_large = |
| 284 (options.persistent == MachMessageServer::kPersistent) | 280 (options.persistent == MachMessageServer::kPersistent) |
| 285 ? MachMessageServer::kReceiveLargeIgnore | 281 ? MachMessageServer::kReceiveLargeIgnore |
| 286 : MachMessageServer::kReceiveLargeError; | 282 : MachMessageServer::kReceiveLargeError; |
| 287 | 283 |
| 288 mach_msg_timeout_t timeout_ms = options.timeout_secs | 284 mach_msg_timeout_t timeout_ms; |
| 289 ? options.timeout_secs * 1000 | 285 if (!options.has_timeout) { |
| 290 : MACH_MSG_TIMEOUT_NONE; | 286 timeout_ms = kMachMessageTimeoutWaitIndefinitely; |
| 287 } else if (options.timeout_secs == 0) { |
| 288 timeout_ms = kMachMessageTimeoutNonblocking; |
| 289 } else { |
| 290 timeout_ms = options.timeout_secs * 1000; |
| 291 } |
| 291 | 292 |
| 292 mach_msg_return_t mr = MachMessageServer::Run(&universal_mach_exc_server, | 293 mach_msg_return_t mr = MachMessageServer::Run(&universal_mach_exc_server, |
| 293 service_port, | 294 service_port, |
| 294 MACH_MSG_OPTION_NONE, | 295 MACH_MSG_OPTION_NONE, |
| 295 options.persistent, | 296 options.persistent, |
| 296 options.nonblocking, | |
| 297 receive_large, | 297 receive_large, |
| 298 timeout_ms); | 298 timeout_ms); |
| 299 if (mr == MACH_RCV_TIMED_OUT && options.timeout_secs && options.persistent && | 299 if (mr == MACH_RCV_TIMED_OUT && options.has_timeout && options.persistent && |
| 300 exceptions_handled) { | 300 exceptions_handled) { |
| 301 // This is not an error: when exiting on timeout during persistent | 301 // This is not an error: when exiting on timeout during persistent |
| 302 // processing and at least one exception was handled, it’s considered a | 302 // processing and at least one exception was handled, it’s considered a |
| 303 // success. | 303 // success. |
| 304 } else if (mr != MACH_MSG_SUCCESS) { | 304 } else if (mr != MACH_MSG_SUCCESS) { |
| 305 MACH_LOG(ERROR, mr) << "MachMessageServer::Run"; | 305 MACH_LOG(ERROR, mr) << "MachMessageServer::Run"; |
| 306 return EXIT_FAILURE; | 306 return EXIT_FAILURE; |
| 307 } | 307 } |
| 308 | 308 |
| 309 return EXIT_SUCCESS; | 309 return EXIT_SUCCESS; |
| 310 } | 310 } |
| 311 | 311 |
| 312 } // namespace | 312 } // namespace |
| 313 } // namespace crashpad | 313 } // namespace crashpad |
| 314 | 314 |
| 315 int main(int argc, char* argv[]) { | 315 int main(int argc, char* argv[]) { |
| 316 return crashpad::CatchExceptionToolMain(argc, argv); | 316 return crashpad::CatchExceptionToolMain(argc, argv); |
| 317 } | 317 } |
| OLD | NEW |