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 <getopt.h> | |
16 #include <libgen.h> | |
17 #include <stdlib.h> | |
18 | |
19 #include <map> | |
20 #include <string> | |
21 | |
22 #include "base/files/file_path.h" | |
23 #include "base/logging.h" | |
24 #include "base/memory/scoped_ptr.h" | |
25 #include "client/crash_report_database.h" | |
26 #include "tools/tool_support.h" | |
27 #include "handler/crash_report_upload_thread.h" | |
28 #include "handler/mac/crash_report_exception_handler.h" | |
29 #include "handler/mac/exception_handler_server.h" | |
30 #include "util/mach/child_port_handshake.h" | |
31 #include "util/posix/close_stdio.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 namespace crashpad { | |
38 namespace { | |
39 | |
40 void Usage(const std::string& me) { | |
41 fprintf(stderr, | |
42 "Usage: %s [OPTION]...\n" | |
43 "Crashpad's exception handler server.\n" | |
44 "\n" | |
45 " --annotation=KEY=VALUE set a process annotation in each crash report\n" | |
46 " --database=PATH store the crash report database at PATH\n" | |
47 " --handshake-fd=FD establish communication with the client over FD\n
" | |
48 " --url=URL send crash reports to this Breakpad server URL,\n
" | |
49 " only if uploads are enabled for the database\n" | |
50 " --help display this help and exit\n" | |
51 " --version output version information and exit\n", | |
52 me.c_str()); | |
53 ToolSupport::UsageTail(me); | |
54 } | |
55 | |
56 int HandlerMain(int argc, char* argv[]) { | |
57 const std::string me(basename(argv[0])); | |
58 | |
59 enum OptionFlags { | |
60 // Long options without short equivalents. | |
61 kOptionLastChar = 255, | |
62 kOptionAnnotation, | |
63 kOptionDatabase, | |
64 kOptionHandshakeFD, | |
65 kOptionURL, | |
66 | |
67 // Standard options. | |
68 kOptionHelp = -2, | |
69 kOptionVersion = -3, | |
70 }; | |
71 | |
72 struct { | |
73 std::map<std::string, std::string> annotations; | |
74 std::string url; | |
75 const char* database; | |
76 int handshake_fd; | |
77 } options = {}; | |
78 options.handshake_fd = -1; | |
79 | |
80 const option long_options[] = { | |
81 {"annotation", required_argument, nullptr, kOptionAnnotation}, | |
82 {"database", required_argument, nullptr, kOptionDatabase}, | |
83 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, | |
84 {"url", required_argument, nullptr, kOptionURL}, | |
85 {"help", no_argument, nullptr, kOptionHelp}, | |
86 {"version", no_argument, nullptr, kOptionVersion}, | |
87 {nullptr, 0, nullptr, 0}, | |
88 }; | |
89 | |
90 int opt; | |
91 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { | |
92 switch (opt) { | |
93 case kOptionAnnotation: { | |
94 std::string key; | |
95 std::string value; | |
96 if (!SplitString(optarg, '=', &key, &value)) { | |
97 ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE"); | |
98 return EXIT_FAILURE; | |
99 } | |
100 std::string old_value; | |
101 if (!MapInsertOrReplace(&options.annotations, key, value, &old_value)) { | |
102 LOG(WARNING) << "duplicate key " << key << ", discarding value " | |
103 << old_value; | |
104 } | |
105 break; | |
106 } | |
107 case kOptionDatabase: { | |
108 options.database = optarg; | |
109 break; | |
110 } | |
111 case kOptionHandshakeFD: { | |
112 if (!StringToNumber(optarg, &options.handshake_fd) || | |
113 options.handshake_fd < 0) { | |
114 ToolSupport::UsageHint(me, | |
115 "--handshake-fd requires a file descriptor"); | |
116 return EXIT_FAILURE; | |
117 } | |
118 break; | |
119 } | |
120 case kOptionURL: { | |
121 options.url = optarg; | |
122 break; | |
123 } | |
124 case kOptionHelp: { | |
125 Usage(me); | |
126 return EXIT_SUCCESS; | |
127 } | |
128 case kOptionVersion: { | |
129 ToolSupport::Version(me); | |
130 return EXIT_SUCCESS; | |
131 } | |
132 default: { | |
133 ToolSupport::UsageHint(me, nullptr); | |
134 return EXIT_FAILURE; | |
135 } | |
136 } | |
137 } | |
138 argc -= optind; | |
139 argv += optind; | |
140 | |
141 if (options.handshake_fd < 0) { | |
142 ToolSupport::UsageHint(me, "--handshake-fd is required"); | |
143 return EXIT_FAILURE; | |
144 } | |
145 | |
146 if (!options.database) { | |
147 ToolSupport::UsageHint(me, "--database is required"); | |
148 return EXIT_FAILURE; | |
149 } | |
150 | |
151 if (argc) { | |
152 ToolSupport::UsageHint(me, nullptr); | |
153 return EXIT_FAILURE; | |
154 } | |
155 | |
156 CloseStdinAndStdout(); | |
157 | |
158 ExceptionHandlerServer exception_handler_server; | |
159 | |
160 ChildPortHandshake::RunClient(options.handshake_fd, | |
161 exception_handler_server.receive_port(), | |
162 MACH_MSG_TYPE_MAKE_SEND); | |
163 | |
164 scoped_ptr<CrashReportDatabase> database( | |
165 CrashReportDatabase::Initialize(base::FilePath(options.database))); | |
166 if (!database) { | |
167 return EXIT_FAILURE; | |
168 } | |
169 | |
170 CrashReportUploadThread upload_thread(database.get(), options.url); | |
171 upload_thread.Start(); | |
172 | |
173 CrashReportExceptionHandler exception_handler( | |
174 database.get(), &upload_thread, &options.annotations); | |
175 | |
176 exception_handler_server.Run(&exception_handler); | |
177 | |
178 upload_thread.Stop(); | |
179 | |
180 return EXIT_SUCCESS; | |
181 } | |
182 | |
183 } // namespace | |
184 } // namespace crashpad | |
185 | |
186 int main(int argc, char* argv[]) { | |
187 return crashpad::HandlerMain(argc, argv); | |
188 } | |
OLD | NEW |