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