2014-12-30 14:23:47 -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 <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-03-05 15:40:47 -05:00
|
|
|
#include <map>
|
2014-12-30 14:23:47 -05:00
|
|
|
#include <string>
|
|
|
|
|
2015-02-04 18:32:42 -05:00
|
|
|
#include "base/files/file_path.h"
|
2015-10-29 14:14:15 -04:00
|
|
|
#include "base/files/scoped_file.h"
|
2015-02-12 15:03:59 -05:00
|
|
|
#include "base/logging.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
#include "base/memory/scoped_ptr.h"
|
2015-09-03 13:31:19 -07:00
|
|
|
#include "build/build_config.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
#include "client/crash_report_database.h"
|
2015-10-06 14:15:18 -04:00
|
|
|
#include "client/crashpad_client.h"
|
2014-12-30 14:23:47 -05:00
|
|
|
#include "tools/tool_support.h"
|
2015-08-18 15:34:10 -07:00
|
|
|
#include "handler/crash_report_upload_thread.h"
|
2015-03-31 14:29:32 -04:00
|
|
|
#include "util/stdlib/map_insert.h"
|
2014-12-30 14:23:47 -05:00
|
|
|
#include "util/stdlib/string_number_conversion.h"
|
2015-03-12 14:28:19 -04:00
|
|
|
#include "util/string/split_string.h"
|
2015-02-12 15:03:59 -05:00
|
|
|
#include "util/synchronization/semaphore.h"
|
2014-12-30 14:23:47 -05:00
|
|
|
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
#include <libgen.h>
|
2015-10-29 18:09:03 -04:00
|
|
|
#include "base/mac/scoped_mach_port.h"
|
2015-09-03 13:31:19 -07:00
|
|
|
#include "handler/mac/crash_report_exception_handler.h"
|
|
|
|
#include "handler/mac/exception_handler_server.h"
|
|
|
|
#include "util/mach/child_port_handshake.h"
|
|
|
|
#include "util/posix/close_stdio.h"
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#include "handler/win/crash_report_exception_handler.h"
|
|
|
|
#include "util/win/exception_handler_server.h"
|
|
|
|
#endif // OS_MACOSX
|
|
|
|
|
2014-12-30 14:23:47 -05:00
|
|
|
namespace crashpad {
|
|
|
|
namespace {
|
|
|
|
|
2015-09-03 13:31:19 -07:00
|
|
|
void Usage(const base::FilePath& me) {
|
2014-12-30 14:23:47 -05:00
|
|
|
fprintf(stderr,
|
2015-09-03 13:31:19 -07:00
|
|
|
"Usage: %" PRFilePath " [OPTION]...\n"
|
2014-12-30 14:23:47 -05:00
|
|
|
"Crashpad's exception handler server.\n"
|
|
|
|
"\n"
|
2015-03-05 15:40:47 -05:00
|
|
|
" --annotation=KEY=VALUE set a process annotation in each crash report\n"
|
|
|
|
" --database=PATH store the crash report database at PATH\n"
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2015-03-05 15:40:47 -05:00
|
|
|
" --handshake-fd=FD establish communication with the client over FD\n"
|
2015-10-06 14:15:18 -04:00
|
|
|
" --reset-own-crash-exception-port-to-system-default\n"
|
|
|
|
" reset the server's exception handler to default\n"
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
" --pipe-name=PIPE communicate with the client over PIPE\n"
|
|
|
|
#endif // OS_MACOSX
|
2015-03-18 17:10:55 -04:00
|
|
|
" --url=URL send crash reports to this Breakpad server URL,\n"
|
|
|
|
" only if uploads are enabled for the database\n"
|
2015-03-05 15:40:47 -05:00
|
|
|
" --help display this help and exit\n"
|
|
|
|
" --version output version information and exit\n",
|
2015-09-03 13:31:19 -07:00
|
|
|
me.value().c_str());
|
2014-12-30 14:23:47 -05:00
|
|
|
ToolSupport::UsageTail(me);
|
|
|
|
}
|
|
|
|
|
|
|
|
int HandlerMain(int argc, char* argv[]) {
|
2015-09-03 13:31:19 -07:00
|
|
|
const base::FilePath argv0(
|
|
|
|
ToolSupport::CommandLineArgumentToFilePathStringType(argv[0]));
|
|
|
|
const base::FilePath me(argv0.BaseName());
|
2014-12-30 14:23:47 -05:00
|
|
|
|
|
|
|
enum OptionFlags {
|
|
|
|
// Long options without short equivalents.
|
|
|
|
kOptionLastChar = 255,
|
2015-03-05 15:40:47 -05:00
|
|
|
kOptionAnnotation,
|
2015-02-04 18:32:42 -05:00
|
|
|
kOptionDatabase,
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2014-12-30 14:23:47 -05:00
|
|
|
kOptionHandshakeFD,
|
2015-10-06 14:15:18 -04:00
|
|
|
kOptionResetOwnCrashExceptionPortToSystemDefault,
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
kOptionPipeName,
|
|
|
|
#endif // OS_MACOSX
|
2015-03-05 15:40:47 -05:00
|
|
|
kOptionURL,
|
2014-12-30 14:23:47 -05:00
|
|
|
|
|
|
|
// Standard options.
|
|
|
|
kOptionHelp = -2,
|
|
|
|
kOptionVersion = -3,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
2015-03-05 15:40:47 -05:00
|
|
|
std::map<std::string, std::string> annotations;
|
|
|
|
std::string url;
|
2015-02-04 18:32:42 -05:00
|
|
|
const char* database;
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2014-12-30 14:23:47 -05:00
|
|
|
int handshake_fd;
|
2015-10-06 14:15:18 -04:00
|
|
|
bool reset_own_crash_exception_port_to_system_default;
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
std::string pipe_name;
|
|
|
|
#endif
|
2014-12-30 14:23:47 -05:00
|
|
|
} options = {};
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2014-12-30 14:23:47 -05:00
|
|
|
options.handshake_fd = -1;
|
2015-10-06 14:15:18 -04:00
|
|
|
options.reset_own_crash_exception_port_to_system_default = false;
|
2015-09-03 13:31:19 -07:00
|
|
|
#endif
|
2014-12-30 14:23:47 -05:00
|
|
|
|
2015-03-19 18:41:01 -04:00
|
|
|
const option long_options[] = {
|
2015-03-05 15:40:47 -05:00
|
|
|
{"annotation", required_argument, nullptr, kOptionAnnotation},
|
2015-02-04 18:32:42 -05:00
|
|
|
{"database", required_argument, nullptr, kOptionDatabase},
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2014-12-30 14:23:47 -05:00
|
|
|
{"handshake-fd", required_argument, nullptr, kOptionHandshakeFD},
|
2015-10-06 14:15:18 -04:00
|
|
|
{"reset-own-crash-exception-port-to-system-default",
|
|
|
|
no_argument,
|
|
|
|
nullptr,
|
|
|
|
kOptionResetOwnCrashExceptionPortToSystemDefault},
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
{"pipe-name", required_argument, nullptr, kOptionPipeName},
|
|
|
|
#endif
|
2015-03-05 15:40:47 -05:00
|
|
|
{"url", required_argument, nullptr, kOptionURL},
|
2014-12-30 14:23:47 -05:00
|
|
|
{"help", no_argument, nullptr, kOptionHelp},
|
|
|
|
{"version", no_argument, nullptr, kOptionVersion},
|
|
|
|
{nullptr, 0, nullptr, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
int opt;
|
|
|
|
while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) {
|
|
|
|
switch (opt) {
|
2015-03-05 15:40:47 -05:00
|
|
|
case kOptionAnnotation: {
|
|
|
|
std::string key;
|
|
|
|
std::string value;
|
2015-03-12 14:28:19 -04:00
|
|
|
if (!SplitString(optarg, '=', &key, &value)) {
|
2015-03-05 15:40:47 -05:00
|
|
|
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-05 15:40:47 -05:00
|
|
|
LOG(WARNING) << "duplicate key " << key << ", discarding value "
|
2015-03-31 14:29:32 -04:00
|
|
|
<< old_value;
|
2015-03-05 15:40:47 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kOptionDatabase: {
|
2015-02-04 18:32:42 -05:00
|
|
|
options.database = optarg;
|
|
|
|
break;
|
2015-03-05 15:40:47 -05:00
|
|
|
}
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2015-03-05 15:40:47 -05:00
|
|
|
case kOptionHandshakeFD: {
|
2014-12-30 14:23:47 -05:00
|
|
|
if (!StringToNumber(optarg, &options.handshake_fd) ||
|
|
|
|
options.handshake_fd < 0) {
|
|
|
|
ToolSupport::UsageHint(me,
|
|
|
|
"--handshake-fd requires a file descriptor");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
2015-03-05 15:40:47 -05:00
|
|
|
}
|
2015-10-06 14:15:18 -04:00
|
|
|
case kOptionResetOwnCrashExceptionPortToSystemDefault: {
|
|
|
|
options.reset_own_crash_exception_port_to_system_default = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
case kOptionPipeName: {
|
|
|
|
options.pipe_name = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // OS_MACOSX
|
2015-03-05 15:40:47 -05:00
|
|
|
case kOptionURL: {
|
|
|
|
options.url = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kOptionHelp: {
|
2014-12-30 14:23:47 -05:00
|
|
|
Usage(me);
|
|
|
|
return EXIT_SUCCESS;
|
2015-03-05 15:40:47 -05:00
|
|
|
}
|
|
|
|
case kOptionVersion: {
|
2014-12-30 14:23:47 -05:00
|
|
|
ToolSupport::Version(me);
|
|
|
|
return EXIT_SUCCESS;
|
2015-03-05 15:40:47 -05:00
|
|
|
}
|
|
|
|
default: {
|
2014-12-30 14:23:47 -05:00
|
|
|
ToolSupport::UsageHint(me, nullptr);
|
|
|
|
return EXIT_FAILURE;
|
2015-03-05 15:40:47 -05:00
|
|
|
}
|
2014-12-30 14:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2014-12-30 14:23:47 -05:00
|
|
|
if (options.handshake_fd < 0) {
|
|
|
|
ToolSupport::UsageHint(me, "--handshake-fd is required");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
if (options.pipe_name.empty()) {
|
|
|
|
ToolSupport::UsageHint(me, "--pipe-name is required");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2014-12-30 14:23:47 -05:00
|
|
|
|
2015-02-04 18:32:42 -05:00
|
|
|
if (!options.database) {
|
|
|
|
ToolSupport::UsageHint(me, "--database is required");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-12-30 14:23:47 -05:00
|
|
|
if (argc) {
|
|
|
|
ToolSupport::UsageHint(me, nullptr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:15:18 -04:00
|
|
|
#if defined(OS_MACOSX)
|
2015-10-29 18:09:03 -04:00
|
|
|
CloseStdinAndStdout();
|
|
|
|
|
2015-10-06 14:15:18 -04:00
|
|
|
if (options.reset_own_crash_exception_port_to_system_default) {
|
|
|
|
CrashpadClient::UseSystemDefaultHandler();
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:09:03 -04:00
|
|
|
base::mac::ScopedMachReceiveRight receive_right(
|
|
|
|
ChildPortHandshake::RunServerForFD(
|
2015-10-29 14:14:15 -04:00
|
|
|
base::ScopedFD(options.handshake_fd),
|
2015-10-29 18:09:03 -04:00
|
|
|
ChildPortHandshake::PortRightType::kReceiveRight));
|
|
|
|
if (!receive_right.is_valid()) {
|
2015-10-29 14:14:15 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2015-10-29 18:09:03 -04:00
|
|
|
|
|
|
|
ExceptionHandlerServer exception_handler_server(receive_right.Pass());
|
2015-10-29 15:12:23 -04:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
ExceptionHandlerServer exception_handler_server(options.pipe_name);
|
2015-09-03 13:31:19 -07:00
|
|
|
#endif // OS_MACOSX
|
2014-12-30 14:23:47 -05:00
|
|
|
|
2015-10-08 13:10:02 -04:00
|
|
|
scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize(
|
|
|
|
base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType(
|
|
|
|
options.database))));
|
2015-02-04 18:32:42 -05:00
|
|
|
if (!database) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-03-05 15:40:47 -05:00
|
|
|
CrashReportUploadThread upload_thread(database.get(), options.url);
|
2015-02-12 15:03:59 -05:00
|
|
|
upload_thread.Start();
|
|
|
|
|
2015-03-05 15:40:47 -05:00
|
|
|
CrashReportExceptionHandler exception_handler(
|
|
|
|
database.get(), &upload_thread, &options.annotations);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
|
|
exception_handler_server.Run(&exception_handler);
|
2014-12-30 14:23:47 -05:00
|
|
|
|
2015-02-12 15:03:59 -05:00
|
|
|
upload_thread.Stop();
|
|
|
|
|
2014-12-30 14:23:47 -05:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace crashpad
|
|
|
|
|
2015-09-03 13:31:19 -07:00
|
|
|
#if defined(OS_MACOSX)
|
2014-12-30 14:23:47 -05:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
return crashpad::HandlerMain(argc, argv);
|
|
|
|
}
|
2015-09-03 13:31:19 -07:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
int wmain(int argc, wchar_t* argv[]) {
|
|
|
|
return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HandlerMain);
|
|
|
|
}
|
|
|
|
#endif // OS_MACOSX
|