| 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 <libgen.h> | 16 #include <libgen.h> |
| 17 #include <stdlib.h> | 17 #include <stdlib.h> |
| 18 | 18 |
| 19 #include <string> | 19 #include <string> |
| 20 | 20 |
| 21 #include "base/files/file_path.h" |
| 22 #include "base/memory/scoped_ptr.h" |
| 23 #include "client/crash_report_database.h" |
| 21 #include "tools/tool_support.h" | 24 #include "tools/tool_support.h" |
| 25 #include "handler/mac/crash_report_exception_handler.h" |
| 22 #include "handler/mac/exception_handler_server.h" | 26 #include "handler/mac/exception_handler_server.h" |
| 23 #include "util/mach/child_port_handshake.h" | 27 #include "util/mach/child_port_handshake.h" |
| 24 #include "util/posix/close_stdio.h" | 28 #include "util/posix/close_stdio.h" |
| 25 #include "util/stdlib/string_number_conversion.h" | 29 #include "util/stdlib/string_number_conversion.h" |
| 26 | 30 |
| 27 namespace crashpad { | 31 namespace crashpad { |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 void Usage(const std::string& me) { | 34 void Usage(const std::string& me) { |
| 31 fprintf(stderr, | 35 fprintf(stderr, |
| 32 "Usage: %s [OPTION]...\n" | 36 "Usage: %s [OPTION]...\n" |
| 33 "Crashpad's exception handler server.\n" | 37 "Crashpad's exception handler server.\n" |
| 34 "\n" | 38 "\n" |
| 39 " --database=PATH store the crash report database at PATH\n" |
| 35 " --handshake-fd=FD establish communication with the client over FD\n" | 40 " --handshake-fd=FD establish communication with the client over FD\n" |
| 36 " --help display this help and exit\n" | 41 " --help display this help and exit\n" |
| 37 " --version output version information and exit\n", | 42 " --version output version information and exit\n", |
| 38 me.c_str()); | 43 me.c_str()); |
| 39 ToolSupport::UsageTail(me); | 44 ToolSupport::UsageTail(me); |
| 40 } | 45 } |
| 41 | 46 |
| 42 int HandlerMain(int argc, char* argv[]) { | 47 int HandlerMain(int argc, char* argv[]) { |
| 43 const std::string me(basename(argv[0])); | 48 const std::string me(basename(argv[0])); |
| 44 | 49 |
| 45 enum OptionFlags { | 50 enum OptionFlags { |
| 46 // Long options without short equivalents. | 51 // Long options without short equivalents. |
| 47 kOptionLastChar = 255, | 52 kOptionLastChar = 255, |
| 53 kOptionDatabase, |
| 48 kOptionHandshakeFD, | 54 kOptionHandshakeFD, |
| 49 | 55 |
| 50 // Standard options. | 56 // Standard options. |
| 51 kOptionHelp = -2, | 57 kOptionHelp = -2, |
| 52 kOptionVersion = -3, | 58 kOptionVersion = -3, |
| 53 }; | 59 }; |
| 54 | 60 |
| 55 struct { | 61 struct { |
| 62 const char* database; |
| 56 int handshake_fd; | 63 int handshake_fd; |
| 57 } options = {}; | 64 } options = {}; |
| 58 options.handshake_fd = -1; | 65 options.handshake_fd = -1; |
| 59 | 66 |
| 60 const struct option long_options[] = { | 67 const struct option long_options[] = { |
| 68 {"database", required_argument, nullptr, kOptionDatabase}, |
| 61 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, | 69 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| 62 {"help", no_argument, nullptr, kOptionHelp}, | 70 {"help", no_argument, nullptr, kOptionHelp}, |
| 63 {"version", no_argument, nullptr, kOptionVersion}, | 71 {"version", no_argument, nullptr, kOptionVersion}, |
| 64 {nullptr, 0, nullptr, 0}, | 72 {nullptr, 0, nullptr, 0}, |
| 65 }; | 73 }; |
| 66 | 74 |
| 67 int opt; | 75 int opt; |
| 68 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { | 76 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { |
| 69 switch (opt) { | 77 switch (opt) { |
| 78 case kOptionDatabase: |
| 79 options.database = optarg; |
| 80 break; |
| 70 case kOptionHandshakeFD: | 81 case kOptionHandshakeFD: |
| 71 if (!StringToNumber(optarg, &options.handshake_fd) || | 82 if (!StringToNumber(optarg, &options.handshake_fd) || |
| 72 options.handshake_fd < 0) { | 83 options.handshake_fd < 0) { |
| 73 ToolSupport::UsageHint(me, | 84 ToolSupport::UsageHint(me, |
| 74 "--handshake-fd requires a file descriptor"); | 85 "--handshake-fd requires a file descriptor"); |
| 75 return EXIT_FAILURE; | 86 return EXIT_FAILURE; |
| 76 } | 87 } |
| 77 break; | 88 break; |
| 78 case kOptionHelp: | 89 case kOptionHelp: |
| 79 Usage(me); | 90 Usage(me); |
| 80 return EXIT_SUCCESS; | 91 return EXIT_SUCCESS; |
| 81 case kOptionVersion: | 92 case kOptionVersion: |
| 82 ToolSupport::Version(me); | 93 ToolSupport::Version(me); |
| 83 return EXIT_SUCCESS; | 94 return EXIT_SUCCESS; |
| 84 default: | 95 default: |
| 85 ToolSupport::UsageHint(me, nullptr); | 96 ToolSupport::UsageHint(me, nullptr); |
| 86 return EXIT_FAILURE; | 97 return EXIT_FAILURE; |
| 87 } | 98 } |
| 88 } | 99 } |
| 89 argc -= optind; | 100 argc -= optind; |
| 90 argv += optind; | 101 argv += optind; |
| 91 | 102 |
| 92 if (options.handshake_fd < 0) { | 103 if (options.handshake_fd < 0) { |
| 93 ToolSupport::UsageHint(me, "--handshake-fd is required"); | 104 ToolSupport::UsageHint(me, "--handshake-fd is required"); |
| 94 return EXIT_FAILURE; | 105 return EXIT_FAILURE; |
| 95 } | 106 } |
| 96 | 107 |
| 108 if (!options.database) { |
| 109 ToolSupport::UsageHint(me, "--database is required"); |
| 110 return EXIT_FAILURE; |
| 111 } |
| 112 |
| 97 if (argc) { | 113 if (argc) { |
| 98 ToolSupport::UsageHint(me, nullptr); | 114 ToolSupport::UsageHint(me, nullptr); |
| 99 return EXIT_FAILURE; | 115 return EXIT_FAILURE; |
| 100 } | 116 } |
| 101 | 117 |
| 102 CloseStdinAndStdout(); | 118 CloseStdinAndStdout(); |
| 103 | 119 |
| 104 ExceptionHandlerServer exception_handler_server; | 120 ExceptionHandlerServer exception_handler_server; |
| 105 | 121 |
| 106 ChildPortHandshake::RunClient(options.handshake_fd, | 122 ChildPortHandshake::RunClient(options.handshake_fd, |
| 107 exception_handler_server.receive_port(), | 123 exception_handler_server.receive_port(), |
| 108 MACH_MSG_TYPE_MAKE_SEND); | 124 MACH_MSG_TYPE_MAKE_SEND); |
| 109 | 125 |
| 110 exception_handler_server.Run(); | 126 scoped_ptr<CrashReportDatabase> database( |
| 127 CrashReportDatabase::Initialize(base::FilePath(options.database))); |
| 128 if (!database) { |
| 129 return EXIT_FAILURE; |
| 130 } |
| 131 |
| 132 CrashReportExceptionHandler exception_handler(database.get()); |
| 133 |
| 134 exception_handler_server.Run(&exception_handler); |
| 111 | 135 |
| 112 return EXIT_SUCCESS; | 136 return EXIT_SUCCESS; |
| 113 } | 137 } |
| 114 | 138 |
| 115 } // namespace | 139 } // namespace |
| 116 } // namespace crashpad | 140 } // namespace crashpad |
| 117 | 141 |
| 118 int main(int argc, char* argv[]) { | 142 int main(int argc, char* argv[]) { |
| 119 return crashpad::HandlerMain(argc, argv); | 143 return crashpad::HandlerMain(argc, argv); |
| 120 } | 144 } |
| OLD | NEW |