2014-12-30 14:25:58 -05:00
|
|
|
|
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <libgen.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2015-03-12 14:28:19 -04:00
|
|
|
|
#include <map>
|
2014-12-30 14:25:58 -05:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
#include "client/crashpad_client.h"
|
|
|
|
|
#include "tools/tool_support.h"
|
2015-03-31 14:29:32 -04:00
|
|
|
|
#include "util/stdlib/map_insert.h"
|
2015-03-12 14:28:19 -04:00
|
|
|
|
#include "util/string/split_string.h"
|
2014-12-30 14:25:58 -05:00
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
void Usage(const std::string& me) {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Usage: %s [OPTION]... COMMAND [ARG]...\n"
|
|
|
|
|
"Start a Crashpad handler and have it handle crashes from COMMAND.\n"
|
|
|
|
|
"\n"
|
2015-03-12 14:28:19 -04:00
|
|
|
|
" -h, --handler=HANDLER invoke HANDLER instead of crashpad_handler\n"
|
|
|
|
|
" --annotation=KEY=VALUE passed to the handler as an --annotation argument\n"
|
|
|
|
|
" --database=PATH passed to the handler as its --database argument\n"
|
|
|
|
|
" --url=URL passed to the handler as its --url argument\n"
|
|
|
|
|
" -a, --argument=ARGUMENT invoke the handler with ARGUMENT\n"
|
|
|
|
|
" --help display this help and exit\n"
|
|
|
|
|
" --version output version information and exit\n",
|
2014-12-30 14:25:58 -05:00
|
|
|
|
me.c_str());
|
|
|
|
|
ToolSupport::UsageTail(me);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RunWithCrashpadMain(int argc, char* argv[]) {
|
|
|
|
|
const std::string me(basename(argv[0]));
|
|
|
|
|
|
|
|
|
|
enum ExitCode {
|
|
|
|
|
kExitSuccess = EXIT_SUCCESS,
|
|
|
|
|
|
|
|
|
|
// To differentiate this tool’s errors from errors in the programs it execs,
|
|
|
|
|
// use a high exit code for ordinary failures instead of EXIT_FAILURE. This
|
|
|
|
|
// is the same rationale for using the distinct exit codes for exec
|
|
|
|
|
// failures.
|
|
|
|
|
kExitFailure = 125,
|
|
|
|
|
|
|
|
|
|
// Like env, use exit code 126 if the program was found but could not be
|
|
|
|
|
// invoked, and 127 if it could not be found.
|
|
|
|
|
// http://pubs.opengroup.org/onlinepubs/9699919799/utilities/env.html
|
|
|
|
|
kExitExecFailure = 126,
|
|
|
|
|
kExitExecENOENT = 127,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum OptionFlags {
|
|
|
|
|
// “Short” (single-character) options.
|
|
|
|
|
kOptionHandler = 'h',
|
2015-03-12 14:28:19 -04:00
|
|
|
|
kOptionArgument = 'a',
|
2014-12-30 14:25:58 -05:00
|
|
|
|
|
|
|
|
|
// Long options without short equivalents.
|
|
|
|
|
kOptionLastChar = 255,
|
2015-03-12 14:28:19 -04:00
|
|
|
|
kOptionAnnotation,
|
|
|
|
|
kOptionDatabase,
|
|
|
|
|
kOptionURL,
|
2014-12-30 14:25:58 -05:00
|
|
|
|
|
|
|
|
|
// Standard options.
|
|
|
|
|
kOptionHelp = -2,
|
|
|
|
|
kOptionVersion = -3,
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-19 18:41:01 -04:00
|
|
|
|
const option long_options[] = {
|
2014-12-30 14:25:58 -05:00
|
|
|
|
{"handler", required_argument, nullptr, kOptionHandler},
|
2015-03-12 14:28:19 -04:00
|
|
|
|
{"annotation", required_argument, nullptr, kOptionAnnotation},
|
|
|
|
|
{"database", required_argument, nullptr, kOptionDatabase},
|
|
|
|
|
{"url", required_argument, nullptr, kOptionURL},
|
|
|
|
|
{"argument", required_argument, nullptr, kOptionArgument},
|
2014-12-30 14:25:58 -05:00
|
|
|
|
{"help", no_argument, nullptr, kOptionHelp},
|
|
|
|
|
{"version", no_argument, nullptr, kOptionVersion},
|
|
|
|
|
{nullptr, 0, nullptr, 0},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
std::string handler;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
std::map<std::string, std::string> annotations;
|
|
|
|
|
std::string database;
|
|
|
|
|
std::string url;
|
|
|
|
|
std::vector<std::string> arguments;
|
2014-12-30 14:25:58 -05:00
|
|
|
|
} options = {};
|
|
|
|
|
options.handler = "crashpad_handler";
|
|
|
|
|
|
|
|
|
|
int opt;
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "+a:h:", long_options, nullptr)) !=
|
|
|
|
|
-1) {
|
|
|
|
|
switch (opt) {
|
2015-03-12 14:28:19 -04:00
|
|
|
|
case kOptionHandler: {
|
2014-12-30 14:25:58 -05:00
|
|
|
|
options.handler = optarg;
|
|
|
|
|
break;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
}
|
|
|
|
|
case kOptionAnnotation: {
|
|
|
|
|
std::string key;
|
|
|
|
|
std::string value;
|
|
|
|
|
if (!SplitString(optarg, '=', &key, &value)) {
|
|
|
|
|
ToolSupport::UsageHint(me, "--annotation requires KEY=VALUE");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2015-03-31 14:29:32 -04:00
|
|
|
|
std::string old_value;
|
|
|
|
|
if (!MapInsertOrReplace(&options.annotations, key, value, &old_value)) {
|
2015-03-12 14:28:19 -04:00
|
|
|
|
LOG(WARNING) << "duplicate key " << key << ", discarding value "
|
2015-03-31 14:29:32 -04:00
|
|
|
|
<< old_value;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
}
|
2014-12-30 14:25:58 -05:00
|
|
|
|
break;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
}
|
|
|
|
|
case kOptionDatabase: {
|
|
|
|
|
options.database = optarg;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case kOptionURL: {
|
|
|
|
|
options.url = optarg;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case kOptionArgument: {
|
|
|
|
|
options.arguments.push_back(optarg);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case kOptionHelp: {
|
2014-12-30 14:25:58 -05:00
|
|
|
|
Usage(me);
|
|
|
|
|
return kExitSuccess;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
}
|
|
|
|
|
case kOptionVersion: {
|
2014-12-30 14:25:58 -05:00
|
|
|
|
ToolSupport::Version(me);
|
|
|
|
|
return kExitSuccess;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
}
|
|
|
|
|
default: {
|
2014-12-30 14:25:58 -05:00
|
|
|
|
ToolSupport::UsageHint(me, nullptr);
|
|
|
|
|
return kExitFailure;
|
2015-03-12 14:28:19 -04:00
|
|
|
|
}
|
2014-12-30 14:25:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
argc -= optind;
|
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
|
|
if (!argc) {
|
|
|
|
|
ToolSupport::UsageHint(me, "COMMAND is required");
|
|
|
|
|
return kExitFailure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start the handler process and direct exceptions to it.
|
|
|
|
|
CrashpadClient crashpad_client;
|
|
|
|
|
if (!crashpad_client.StartHandler(base::FilePath(options.handler),
|
2015-03-12 14:28:19 -04:00
|
|
|
|
base::FilePath(options.database),
|
2016-09-16 13:31:16 -07:00
|
|
|
|
base::FilePath(),
|
2015-03-12 14:28:19 -04:00
|
|
|
|
options.url,
|
|
|
|
|
options.annotations,
|
2015-11-03 19:13:48 -05:00
|
|
|
|
options.arguments,
|
|
|
|
|
false)) {
|
2014-12-30 14:25:58 -05:00
|
|
|
|
return kExitFailure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!crashpad_client.UseHandler()) {
|
|
|
|
|
return kExitFailure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Using the remaining arguments, start a new program with the new exception
|
|
|
|
|
// port in effect.
|
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
|
PLOG(ERROR) << "execvp " << argv[0];
|
|
|
|
|
return errno == ENOENT ? kExitExecENOENT : kExitExecFailure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
} // namespace crashpad
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
return crashpad::RunWithCrashpadMain(argc, argv);
|
|
|
|
|
}
|