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 <libgen.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-02-04 18:32:42 -05:00
|
|
|
#include "base/files/file_path.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"
|
|
|
|
#include "client/crash_report_database.h"
|
2014-12-30 14:23:47 -05:00
|
|
|
#include "tools/tool_support.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
#include "handler/mac/crash_report_exception_handler.h"
|
2015-02-12 15:03:59 -05:00
|
|
|
#include "handler/mac/crash_report_upload_thread.h"
|
2014-12-30 14:23:47 -05:00
|
|
|
#include "handler/mac/exception_handler_server.h"
|
|
|
|
#include "util/mach/child_port_handshake.h"
|
|
|
|
#include "util/posix/close_stdio.h"
|
|
|
|
#include "util/stdlib/string_number_conversion.h"
|
2015-02-12 15:03:59 -05:00
|
|
|
#include "util/synchronization/semaphore.h"
|
2014-12-30 14:23:47 -05:00
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Usage(const std::string& me) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %s [OPTION]...\n"
|
|
|
|
"Crashpad's exception handler server.\n"
|
|
|
|
"\n"
|
2015-02-04 18:32:42 -05:00
|
|
|
" --database=PATH store the crash report database at PATH\n"
|
2014-12-30 14:23:47 -05:00
|
|
|
" --handshake-fd=FD establish communication with the client over FD\n"
|
|
|
|
" --help display this help and exit\n"
|
|
|
|
" --version output version information and exit\n",
|
|
|
|
me.c_str());
|
|
|
|
ToolSupport::UsageTail(me);
|
|
|
|
}
|
|
|
|
|
|
|
|
int HandlerMain(int argc, char* argv[]) {
|
|
|
|
const std::string me(basename(argv[0]));
|
|
|
|
|
|
|
|
enum OptionFlags {
|
|
|
|
// Long options without short equivalents.
|
|
|
|
kOptionLastChar = 255,
|
2015-02-04 18:32:42 -05:00
|
|
|
kOptionDatabase,
|
2014-12-30 14:23:47 -05:00
|
|
|
kOptionHandshakeFD,
|
|
|
|
|
|
|
|
// Standard options.
|
|
|
|
kOptionHelp = -2,
|
|
|
|
kOptionVersion = -3,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
2015-02-04 18:32:42 -05:00
|
|
|
const char* database;
|
2014-12-30 14:23:47 -05:00
|
|
|
int handshake_fd;
|
|
|
|
} options = {};
|
|
|
|
options.handshake_fd = -1;
|
|
|
|
|
|
|
|
const struct option long_options[] = {
|
2015-02-04 18:32:42 -05:00
|
|
|
{"database", required_argument, nullptr, kOptionDatabase},
|
2014-12-30 14:23:47 -05:00
|
|
|
{"handshake-fd", required_argument, nullptr, kOptionHandshakeFD},
|
|
|
|
{"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-02-04 18:32:42 -05:00
|
|
|
case kOptionDatabase:
|
|
|
|
options.database = optarg;
|
|
|
|
break;
|
2014-12-30 14:23:47 -05:00
|
|
|
case kOptionHandshakeFD:
|
|
|
|
if (!StringToNumber(optarg, &options.handshake_fd) ||
|
|
|
|
options.handshake_fd < 0) {
|
|
|
|
ToolSupport::UsageHint(me,
|
|
|
|
"--handshake-fd requires a file descriptor");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kOptionHelp:
|
|
|
|
Usage(me);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case kOptionVersion:
|
|
|
|
ToolSupport::Version(me);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
default:
|
|
|
|
ToolSupport::UsageHint(me, nullptr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (options.handshake_fd < 0) {
|
|
|
|
ToolSupport::UsageHint(me, "--handshake-fd is required");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseStdinAndStdout();
|
|
|
|
|
|
|
|
ExceptionHandlerServer exception_handler_server;
|
|
|
|
|
|
|
|
ChildPortHandshake::RunClient(options.handshake_fd,
|
|
|
|
exception_handler_server.receive_port(),
|
|
|
|
MACH_MSG_TYPE_MAKE_SEND);
|
|
|
|
|
2015-02-04 18:32:42 -05:00
|
|
|
scoped_ptr<CrashReportDatabase> database(
|
|
|
|
CrashReportDatabase::Initialize(base::FilePath(options.database)));
|
|
|
|
if (!database) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:03:59 -05:00
|
|
|
CrashReportUploadThread upload_thread(database.get());
|
|
|
|
upload_thread.Start();
|
|
|
|
|
|
|
|
CrashReportExceptionHandler exception_handler(database.get(), &upload_thread);
|
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
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
return crashpad::HandlerMain(argc, argv);
|
|
|
|
}
|