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 <errno.h> | 15 #include <errno.h> |
16 #include <getopt.h> | 16 #include <getopt.h> |
17 #include <libgen.h> | 17 #include <libgen.h> |
18 #include <stdio.h> | 18 #include <stdio.h> |
19 #include <stdlib.h> | 19 #include <stdlib.h> |
20 #include <unistd.h> | 20 #include <unistd.h> |
21 | 21 |
| 22 #include <map> |
22 #include <string> | 23 #include <string> |
23 #include <vector> | 24 #include <vector> |
24 | 25 |
25 #include "base/files/file_path.h" | 26 #include "base/files/file_path.h" |
26 #include "base/logging.h" | 27 #include "base/logging.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" |
| 30 #include "util/string/split_string.h" |
29 | 31 |
30 namespace crashpad { | 32 namespace crashpad { |
31 namespace { | 33 namespace { |
32 | 34 |
33 void Usage(const std::string& me) { | 35 void Usage(const std::string& me) { |
34 fprintf(stderr, | 36 fprintf(stderr, |
35 "Usage: %s [OPTION]... COMMAND [ARG]...\n" | 37 "Usage: %s [OPTION]... COMMAND [ARG]...\n" |
36 "Start a Crashpad handler and have it handle crashes from COMMAND.\n" | 38 "Start a Crashpad handler and have it handle crashes from COMMAND.\n" |
37 "\n" | 39 "\n" |
38 " -h, --handler=HANDLER invoke HANDLER instead of crashpad_handler\n
" | 40 " -h, --handler=HANDLER invoke HANDLER instead of crashpad_handler\n" |
39 " -a, --handler-argument=ARGUMENT invoke the handler with ARGUMENT\n" | 41 " --annotation=KEY=VALUE passed to the handler as an --annotation argument
\n" |
40 " --help display this help and exit\n" | 42 " --database=PATH passed to the handler as its --database argument\
n" |
41 " --version output version information and exit\n", | 43 " --url=URL passed to the handler as its --url argument\n" |
| 44 " -a, --argument=ARGUMENT invoke the handler with ARGUMENT\n" |
| 45 " --help display this help and exit\n" |
| 46 " --version output version information and exit\n", |
42 me.c_str()); | 47 me.c_str()); |
43 ToolSupport::UsageTail(me); | 48 ToolSupport::UsageTail(me); |
44 } | 49 } |
45 | 50 |
46 int RunWithCrashpadMain(int argc, char* argv[]) { | 51 int RunWithCrashpadMain(int argc, char* argv[]) { |
47 const std::string me(basename(argv[0])); | 52 const std::string me(basename(argv[0])); |
48 | 53 |
49 enum ExitCode { | 54 enum ExitCode { |
50 kExitSuccess = EXIT_SUCCESS, | 55 kExitSuccess = EXIT_SUCCESS, |
51 | 56 |
52 // To differentiate this tool’s errors from errors in the programs it execs, | 57 // To differentiate this tool’s errors from errors in the programs it execs, |
53 // use a high exit code for ordinary failures instead of EXIT_FAILURE. This | 58 // use a high exit code for ordinary failures instead of EXIT_FAILURE. This |
54 // is the same rationale for using the distinct exit codes for exec | 59 // is the same rationale for using the distinct exit codes for exec |
55 // failures. | 60 // failures. |
56 kExitFailure = 125, | 61 kExitFailure = 125, |
57 | 62 |
58 // Like env, use exit code 126 if the program was found but could not be | 63 // Like env, use exit code 126 if the program was found but could not be |
59 // invoked, and 127 if it could not be found. | 64 // invoked, and 127 if it could not be found. |
60 // http://pubs.opengroup.org/onlinepubs/9699919799/utilities/env.html | 65 // http://pubs.opengroup.org/onlinepubs/9699919799/utilities/env.html |
61 kExitExecFailure = 126, | 66 kExitExecFailure = 126, |
62 kExitExecENOENT = 127, | 67 kExitExecENOENT = 127, |
63 }; | 68 }; |
64 | 69 |
65 enum OptionFlags { | 70 enum OptionFlags { |
66 // “Short” (single-character) options. | 71 // “Short” (single-character) options. |
67 kOptionHandler = 'h', | 72 kOptionHandler = 'h', |
68 kOptionHandlerArgument = 'a', | 73 kOptionArgument = 'a', |
69 | 74 |
70 // Long options without short equivalents. | 75 // Long options without short equivalents. |
71 kOptionLastChar = 255, | 76 kOptionLastChar = 255, |
| 77 kOptionAnnotation, |
| 78 kOptionDatabase, |
| 79 kOptionURL, |
72 | 80 |
73 // Standard options. | 81 // Standard options. |
74 kOptionHelp = -2, | 82 kOptionHelp = -2, |
75 kOptionVersion = -3, | 83 kOptionVersion = -3, |
76 }; | 84 }; |
77 | 85 |
78 const struct option long_options[] = { | 86 const struct option long_options[] = { |
79 {"handler", required_argument, nullptr, kOptionHandler}, | 87 {"handler", required_argument, nullptr, kOptionHandler}, |
80 {"handler-argument", required_argument, nullptr, kOptionHandlerArgument}, | 88 {"annotation", required_argument, nullptr, kOptionAnnotation}, |
| 89 {"database", required_argument, nullptr, kOptionDatabase}, |
| 90 {"url", required_argument, nullptr, kOptionURL}, |
| 91 {"argument", required_argument, nullptr, kOptionArgument}, |
81 {"help", no_argument, nullptr, kOptionHelp}, | 92 {"help", no_argument, nullptr, kOptionHelp}, |
82 {"version", no_argument, nullptr, kOptionVersion}, | 93 {"version", no_argument, nullptr, kOptionVersion}, |
83 {nullptr, 0, nullptr, 0}, | 94 {nullptr, 0, nullptr, 0}, |
84 }; | 95 }; |
85 | 96 |
86 struct { | 97 struct { |
87 std::string handler; | 98 std::string handler; |
88 std::vector<std::string> handler_arguments; | 99 std::map<std::string, std::string> annotations; |
| 100 std::string database; |
| 101 std::string url; |
| 102 std::vector<std::string> arguments; |
89 } options = {}; | 103 } options = {}; |
90 options.handler = "crashpad_handler"; | 104 options.handler = "crashpad_handler"; |
91 | 105 |
92 int opt; | 106 int opt; |
93 while ((opt = getopt_long(argc, argv, "+a:h:", long_options, nullptr)) != | 107 while ((opt = getopt_long(argc, argv, "+a:h:", long_options, nullptr)) != |
94 -1) { | 108 -1) { |
95 switch (opt) { | 109 switch (opt) { |
96 case kOptionHandler: | 110 case kOptionHandler: { |
97 options.handler = optarg; | 111 options.handler = optarg; |
98 break; | 112 break; |
99 case kOptionHandlerArgument: | 113 } |
100 options.handler_arguments.push_back(optarg); | 114 case kOptionAnnotation: { |
| 115 std::string key; |
| 116 std::string value; |
| 117 if (!SplitString(optarg, '=', &key, &value)) { |
| 118 ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE"); |
| 119 return EXIT_FAILURE; |
| 120 } |
| 121 auto it = options.annotations.find(key); |
| 122 if (it != options.annotations.end()) { |
| 123 LOG(WARNING) << "duplicate key " << key << ", discarding value " |
| 124 << it->second; |
| 125 it->second = value; |
| 126 } else { |
| 127 options.annotations.insert(std::make_pair(key, value)); |
| 128 } |
101 break; | 129 break; |
102 case kOptionHelp: | 130 } |
| 131 case kOptionDatabase: { |
| 132 options.database = optarg; |
| 133 break; |
| 134 } |
| 135 case kOptionURL: { |
| 136 options.url = optarg; |
| 137 break; |
| 138 } |
| 139 case kOptionArgument: { |
| 140 options.arguments.push_back(optarg); |
| 141 break; |
| 142 } |
| 143 case kOptionHelp: { |
103 Usage(me); | 144 Usage(me); |
104 return kExitSuccess; | 145 return kExitSuccess; |
105 case kOptionVersion: | 146 } |
| 147 case kOptionVersion: { |
106 ToolSupport::Version(me); | 148 ToolSupport::Version(me); |
107 return kExitSuccess; | 149 return kExitSuccess; |
108 default: | 150 } |
| 151 default: { |
109 ToolSupport::UsageHint(me, nullptr); | 152 ToolSupport::UsageHint(me, nullptr); |
110 return kExitFailure; | 153 return kExitFailure; |
| 154 } |
111 } | 155 } |
112 } | 156 } |
113 argc -= optind; | 157 argc -= optind; |
114 argv += optind; | 158 argv += optind; |
115 | 159 |
116 if (!argc) { | 160 if (!argc) { |
117 ToolSupport::UsageHint(me, "COMMAND is required"); | 161 ToolSupport::UsageHint(me, "COMMAND is required"); |
118 return kExitFailure; | 162 return kExitFailure; |
119 } | 163 } |
120 | 164 |
121 // Start the handler process and direct exceptions to it. | 165 // Start the handler process and direct exceptions to it. |
122 CrashpadClient crashpad_client; | 166 CrashpadClient crashpad_client; |
123 if (!crashpad_client.StartHandler(base::FilePath(options.handler), | 167 if (!crashpad_client.StartHandler(base::FilePath(options.handler), |
124 options.handler_arguments)) { | 168 base::FilePath(options.database), |
| 169 options.url, |
| 170 options.annotations, |
| 171 options.arguments)) { |
125 return kExitFailure; | 172 return kExitFailure; |
126 } | 173 } |
127 | 174 |
128 if (!crashpad_client.UseHandler()) { | 175 if (!crashpad_client.UseHandler()) { |
129 return kExitFailure; | 176 return kExitFailure; |
130 } | 177 } |
131 | 178 |
132 // Using the remaining arguments, start a new program with the new exception | 179 // Using the remaining arguments, start a new program with the new exception |
133 // port in effect. | 180 // port in effect. |
134 execvp(argv[0], argv); | 181 execvp(argv[0], argv); |
135 PLOG(ERROR) << "execvp " << argv[0]; | 182 PLOG(ERROR) << "execvp " << argv[0]; |
136 return errno == ENOENT ? kExitExecENOENT : kExitExecFailure; | 183 return errno == ENOENT ? kExitExecENOENT : kExitExecFailure; |
137 } | 184 } |
138 | 185 |
139 } // namespace | 186 } // namespace |
140 } // namespace crashpad | 187 } // namespace crashpad |
141 | 188 |
142 int main(int argc, char* argv[]) { | 189 int main(int argc, char* argv[]) { |
143 return crashpad::RunWithCrashpadMain(argc, argv); | 190 return crashpad::RunWithCrashpadMain(argc, argv); |
144 } | 191 } |
OLD | NEW |