2014-11-14 19:21:43 -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 <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#include <sys/types.h>
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
#include <memory>
|
2014-11-14 19:21:43 -05:00
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
#include "base/strings/stringprintf.h"
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#include "build/build_config.h"
|
2014-11-14 19:21:43 -05:00
|
|
|
|
#include "minidump/minidump_file_writer.h"
|
|
|
|
|
#include "tools/tool_support.h"
|
|
|
|
|
#include "util/file/file_writer.h"
|
|
|
|
|
#include "util/posix/drop_privileges.h"
|
|
|
|
|
#include "util/stdlib/string_number_conversion.h"
|
|
|
|
|
|
2017-12-08 11:40:29 -08:00
|
|
|
|
#if defined(OS_POSIX)
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
|
#include <mach/mach.h>
|
|
|
|
|
|
|
|
|
|
#include "base/mac/scoped_mach_port.h"
|
|
|
|
|
#include "snapshot/mac/process_snapshot_mac.h"
|
|
|
|
|
#include "util/mach/scoped_task_suspend.h"
|
|
|
|
|
#include "util/mach/task_for_pid.h"
|
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
|
#include "snapshot/win/process_snapshot_win.h"
|
2015-09-08 10:09:26 -07:00
|
|
|
|
#include "util/win/scoped_process_suspend.h"
|
2015-09-11 13:16:06 -07:00
|
|
|
|
#include "util/win/xp_compat.h"
|
2017-12-08 11:40:29 -08:00
|
|
|
|
#elif defined(OS_FUCHSIA)
|
2018-07-30 15:50:17 -07:00
|
|
|
|
#include <lib/zx/process.h>
|
|
|
|
|
|
2017-12-08 11:40:29 -08:00
|
|
|
|
#include "snapshot/fuchsia/process_snapshot_fuchsia.h"
|
2018-04-10 14:25:06 -07:00
|
|
|
|
#include "util/fuchsia/koid_utilities.h"
|
2018-04-13 09:42:19 -07:00
|
|
|
|
#include "util/fuchsia/scoped_task_suspend.h"
|
2018-03-28 08:37:22 -07:00
|
|
|
|
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
2018-02-19 11:11:38 -08:00
|
|
|
|
#include "snapshot/linux/process_snapshot_linux.h"
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#endif // OS_MACOSX
|
|
|
|
|
|
2014-11-14 19:21:43 -05:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2015-05-06 11:09:31 -07:00
|
|
|
|
void Usage(const base::FilePath& me) {
|
2014-11-14 19:21:43 -05:00
|
|
|
|
fprintf(stderr,
|
2015-05-06 11:09:31 -07:00
|
|
|
|
"Usage: %" PRFilePath " [OPTION]... PID\n"
|
2014-11-14 19:21:43 -05:00
|
|
|
|
"Generate a minidump file containing a snapshot of a running process.\n"
|
|
|
|
|
"\n"
|
2014-12-04 16:46:12 -05:00
|
|
|
|
" -r, --no-suspend don't suspend the target process during dump generation\n"
|
2014-11-14 19:21:43 -05:00
|
|
|
|
" -o, --output=FILE write the minidump to FILE instead of minidump.PID\n"
|
|
|
|
|
" --help display this help and exit\n"
|
|
|
|
|
" --version output version information and exit\n",
|
2015-05-06 11:09:31 -07:00
|
|
|
|
me.value().c_str());
|
2014-11-14 19:21:43 -05:00
|
|
|
|
ToolSupport::UsageTail(me);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GenerateDumpMain(int argc, char* argv[]) {
|
2015-05-06 11:09:31 -07:00
|
|
|
|
const base::FilePath argv0(
|
|
|
|
|
ToolSupport::CommandLineArgumentToFilePathStringType(argv[0]));
|
|
|
|
|
const base::FilePath me(argv0.BaseName());
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
|
|
|
|
enum OptionFlags {
|
|
|
|
|
// “Short” (single-character) options.
|
|
|
|
|
kOptionOutput = 'o',
|
|
|
|
|
kOptionNoSuspend = 'r',
|
|
|
|
|
|
|
|
|
|
// Long options without short equivalents.
|
|
|
|
|
kOptionLastChar = 255,
|
|
|
|
|
|
|
|
|
|
// Standard options.
|
|
|
|
|
kOptionHelp = -2,
|
|
|
|
|
kOptionVersion = -3,
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-03 15:11:50 -05:00
|
|
|
|
struct {
|
|
|
|
|
std::string dump_path;
|
|
|
|
|
pid_t pid;
|
|
|
|
|
bool suspend;
|
|
|
|
|
} options = {};
|
2014-11-14 19:21:43 -05:00
|
|
|
|
options.suspend = true;
|
|
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
|
static constexpr option long_options[] = {
|
2014-12-04 16:46:12 -05:00
|
|
|
|
{"no-suspend", no_argument, nullptr, kOptionNoSuspend},
|
2014-11-14 19:21:43 -05:00
|
|
|
|
{"output", required_argument, nullptr, kOptionOutput},
|
|
|
|
|
{"help", no_argument, nullptr, kOptionHelp},
|
|
|
|
|
{"version", no_argument, nullptr, kOptionVersion},
|
|
|
|
|
{nullptr, 0, nullptr, 0},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int opt;
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "o:r", long_options, nullptr)) != -1) {
|
|
|
|
|
switch (opt) {
|
|
|
|
|
case kOptionOutput:
|
|
|
|
|
options.dump_path = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case kOptionNoSuspend:
|
|
|
|
|
options.suspend = false;
|
|
|
|
|
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 (argc != 1) {
|
|
|
|
|
ToolSupport::UsageHint(me, "PID is required");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!StringToNumber(argv[0], &options.pid) || options.pid <= 0) {
|
2015-05-06 11:09:31 -07:00
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"%" PRFilePath ": invalid PID: %s\n",
|
|
|
|
|
me.value().c_str(),
|
|
|
|
|
argv[0]);
|
2014-11-14 19:21:43 -05:00
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#if defined(OS_MACOSX)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
task_t task = TaskForPID(options.pid);
|
|
|
|
|
if (task == TASK_NULL) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
base::mac::ScopedMachSendRight task_owner(task);
|
|
|
|
|
|
|
|
|
|
// This tool may have been installed as a setuid binary so that TaskForPID()
|
|
|
|
|
// could succeed. Drop any privileges now that they’re no longer necessary.
|
|
|
|
|
DropPrivileges();
|
|
|
|
|
|
|
|
|
|
if (options.pid == getpid()) {
|
|
|
|
|
if (options.suspend) {
|
|
|
|
|
LOG(ERROR) << "cannot suspend myself";
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
LOG(WARNING) << "operating on myself";
|
|
|
|
|
}
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
ScopedKernelHANDLE process(
|
2015-09-11 13:16:06 -07:00
|
|
|
|
OpenProcess(kXPProcessAllAccess, false, options.pid));
|
2015-05-06 11:09:31 -07:00
|
|
|
|
if (!process.is_valid()) {
|
2015-11-09 15:50:33 -05:00
|
|
|
|
PLOG(ERROR) << "could not open process " << options.pid;
|
2015-05-06 11:09:31 -07:00
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2018-04-10 14:25:06 -07:00
|
|
|
|
#elif defined(OS_FUCHSIA)
|
2018-07-30 15:50:17 -07:00
|
|
|
|
zx::process process = GetProcessFromKoid(options.pid);
|
|
|
|
|
if (!process.is_valid()) {
|
2018-04-10 14:25:06 -07:00
|
|
|
|
LOG(ERROR) << "could not open process " << options.pid;
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#endif // OS_MACOSX
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
|
|
|
|
if (options.dump_path.empty()) {
|
|
|
|
|
options.dump_path = base::StringPrintf("minidump.%d", options.pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#if defined(OS_MACOSX)
|
2016-04-25 12:13:07 -07:00
|
|
|
|
std::unique_ptr<ScopedTaskSuspend> suspend;
|
2014-11-14 19:21:43 -05:00
|
|
|
|
if (options.suspend) {
|
|
|
|
|
suspend.reset(new ScopedTaskSuspend(task));
|
|
|
|
|
}
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#elif defined(OS_WIN)
|
2016-04-25 12:13:07 -07:00
|
|
|
|
std::unique_ptr<ScopedProcessSuspend> suspend;
|
2015-05-06 11:09:31 -07:00
|
|
|
|
if (options.suspend) {
|
2015-09-08 10:09:26 -07:00
|
|
|
|
suspend.reset(new ScopedProcessSuspend(process.get()));
|
2015-05-06 11:09:31 -07:00
|
|
|
|
}
|
2018-04-13 09:42:19 -07:00
|
|
|
|
#elif defined(OS_FUCHSIA)
|
|
|
|
|
std::unique_ptr<ScopedTaskSuspend> suspend;
|
|
|
|
|
if (options.suspend) {
|
2018-07-30 15:50:17 -07:00
|
|
|
|
suspend.reset(new ScopedTaskSuspend(process));
|
2018-04-13 09:42:19 -07:00
|
|
|
|
}
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#endif // OS_MACOSX
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#if defined(OS_MACOSX)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
ProcessSnapshotMac process_snapshot;
|
|
|
|
|
if (!process_snapshot.Initialize(task)) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
ProcessSnapshotWin process_snapshot;
|
2015-09-09 12:29:29 -07:00
|
|
|
|
if (!process_snapshot.Initialize(process.get(),
|
|
|
|
|
options.suspend
|
|
|
|
|
? ProcessSuspensionState::kSuspended
|
2015-10-15 13:18:08 -07:00
|
|
|
|
: ProcessSuspensionState::kRunning,
|
2016-05-02 11:36:41 -07:00
|
|
|
|
0,
|
2015-10-15 13:18:08 -07:00
|
|
|
|
0)) {
|
2015-05-06 11:09:31 -07:00
|
|
|
|
return EXIT_FAILURE;
|
2017-12-08 11:40:29 -08:00
|
|
|
|
}
|
|
|
|
|
#elif defined(OS_FUCHSIA)
|
|
|
|
|
ProcessSnapshotFuchsia process_snapshot;
|
2018-07-30 15:50:17 -07:00
|
|
|
|
if (!process_snapshot.Initialize(process)) {
|
2017-12-08 11:40:29 -08:00
|
|
|
|
return EXIT_FAILURE;
|
2018-02-19 11:11:38 -08:00
|
|
|
|
}
|
2018-03-28 08:37:22 -07:00
|
|
|
|
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
2018-02-19 11:11:38 -08:00
|
|
|
|
// TODO(jperaza): https://crashpad.chromium.org/bug/30.
|
|
|
|
|
ProcessSnapshotLinux process_snapshot;
|
|
|
|
|
if (!process_snapshot.Initialize(nullptr)) {
|
|
|
|
|
return EXIT_FAILURE;
|
2015-05-06 11:09:31 -07:00
|
|
|
|
}
|
|
|
|
|
#endif // OS_MACOSX
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
|
|
|
|
FileWriter file_writer;
|
2015-05-06 11:09:31 -07:00
|
|
|
|
base::FilePath dump_path(
|
|
|
|
|
ToolSupport::CommandLineArgumentToFilePathStringType(
|
|
|
|
|
options.dump_path));
|
|
|
|
|
if (!file_writer.Open(dump_path,
|
2014-12-19 13:33:01 -08:00
|
|
|
|
FileWriteMode::kTruncateOrCreate,
|
2014-12-19 15:35:30 -08:00
|
|
|
|
FilePermissions::kWorldReadable)) {
|
2014-11-14 19:21:43 -05:00
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpFileWriter minidump;
|
|
|
|
|
minidump.InitializeFromSnapshot(&process_snapshot);
|
|
|
|
|
if (!minidump.WriteEverything(&file_writer)) {
|
2015-05-14 17:37:02 -07:00
|
|
|
|
file_writer.Close();
|
2014-11-14 19:21:43 -05:00
|
|
|
|
if (unlink(options.dump_path.c_str()) != 0) {
|
|
|
|
|
PLOG(ERROR) << "unlink";
|
|
|
|
|
}
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
} // namespace crashpad
|
|
|
|
|
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#if defined(OS_POSIX)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
return crashpad::GenerateDumpMain(argc, argv);
|
|
|
|
|
}
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
int wmain(int argc, wchar_t* argv[]) {
|
|
|
|
|
return crashpad::ToolSupport::Wmain(argc, argv, crashpad::GenerateDumpMain);
|
|
|
|
|
}
|
|
|
|
|
#endif // OS_POSIX
|