2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-11-14 19:21:43 -05:00
|
|
|
|
//
|
|
|
|
|
// 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"
|
2019-04-24 11:22:35 -04:00
|
|
|
|
#include "util/process/process_id.h"
|
2014-11-14 19:21:43 -05:00
|
|
|
|
#include "util/stdlib/string_number_conversion.h"
|
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2017-12-08 11:40:29 -08:00
|
|
|
|
#include <unistd.h>
|
2019-01-28 10:19:44 -05:00
|
|
|
|
|
|
|
|
|
#include "util/posix/drop_privileges.h"
|
2017-12-08 11:40:29 -08:00
|
|
|
|
#endif
|
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#include <mach/mach.h>
|
|
|
|
|
|
2023-08-16 16:30:40 -04:00
|
|
|
|
#include "base/apple/scoped_mach_port.h"
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#include "snapshot/mac/process_snapshot_mac.h"
|
|
|
|
|
#include "util/mach/scoped_task_suspend.h"
|
|
|
|
|
#include "util/mach/task_for_pid.h"
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2015-05-06 11:09:31 -07:00
|
|
|
|
#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"
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
|
2018-02-19 11:11:38 -08:00
|
|
|
|
#include "snapshot/linux/process_snapshot_linux.h"
|
2021-11-12 18:55:21 +01:00
|
|
|
|
#include "util/linux/direct_ptrace_connection.h"
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2015-05-06 11:09:31 -07:00
|
|
|
|
|
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) {
|
2022-01-19 15:00:24 -05:00
|
|
|
|
// clang-format off
|
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());
|
2022-01-19 15:00:24 -05:00
|
|
|
|
// clang-format on
|
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;
|
2019-04-24 11:22:35 -04:00
|
|
|
|
ProcessID pid;
|
2017-03-03 15:11:50 -05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
task_t task = TaskForPID(options.pid);
|
|
|
|
|
if (task == TASK_NULL) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2023-08-16 16:30:40 -04:00
|
|
|
|
base::apple::ScopedMachSendRight task_owner(task);
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
|
|
|
|
// 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";
|
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2015-05-06 11:09:31 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
|
|
|
|
if (options.dump_path.empty()) {
|
2019-04-24 11:22:35 -04:00
|
|
|
|
options.dump_path = base::StringPrintf("minidump.%" PRI_PROCESS_ID,
|
|
|
|
|
options.pid);
|
2014-11-14 19:21:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
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));
|
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_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
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
ProcessSnapshotMac process_snapshot;
|
|
|
|
|
if (!process_snapshot.Initialize(task)) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2015-05-06 11:09:31 -07:00
|
|
|
|
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
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
|
2018-02-19 11:11:38 -08:00
|
|
|
|
// TODO(jperaza): https://crashpad.chromium.org/bug/30.
|
2021-11-12 18:55:21 +01:00
|
|
|
|
DirectPtraceConnection task;
|
|
|
|
|
if (!task.Initialize(options.pid)) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2018-02-19 11:11:38 -08:00
|
|
|
|
ProcessSnapshotLinux process_snapshot;
|
2021-11-12 18:55:21 +01:00
|
|
|
|
if (!process_snapshot.Initialize(&task)) {
|
2018-02-19 11:11:38 -08:00
|
|
|
|
return EXIT_FAILURE;
|
2015-05-06 11:09:31 -07:00
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
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
|
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2014-11-14 19:21:43 -05:00
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
return crashpad::GenerateDumpMain(argc, argv);
|
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2015-05-06 11:09:31 -07:00
|
|
|
|
int wmain(int argc, wchar_t* argv[]) {
|
|
|
|
|
return crashpad::ToolSupport::Wmain(argc, argv, crashpad::GenerateDumpMain);
|
|
|
|
|
}
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_POSIX)
|