Chromium Code Reviews| Index: tools/generate_dump.cc |
| diff --git a/tools/generate_dump.cc b/tools/generate_dump.cc |
| index ecb1bda08b32836c6ff2732bffe4ccc25c560391..fc0d8e221ee03d6eb0a9126a54316c3c6c72f0dc 100644 |
| --- a/tools/generate_dump.cc |
| +++ b/tools/generate_dump.cc |
| @@ -14,27 +14,36 @@ |
| #include <fcntl.h> |
| #include <getopt.h> |
| -#include <libgen.h> |
| -#include <mach/mach.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| -#include <unistd.h> |
| +#include <sys/types.h> |
| #include <string> |
| #include "base/logging.h" |
| -#include "base/mac/scoped_mach_port.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/strings/stringprintf.h" |
| +#include "build/build_config.h" |
| #include "minidump/minidump_file_writer.h" |
| -#include "snapshot/mac/process_snapshot_mac.h" |
| #include "tools/tool_support.h" |
| #include "util/file/file_writer.h" |
| -#include "util/mach/scoped_task_suspend.h" |
| -#include "util/mach/task_for_pid.h" |
| #include "util/posix/drop_privileges.h" |
| #include "util/stdlib/string_number_conversion.h" |
| +#if defined(OS_MACOSX) |
| +#include <libgen.h> |
|
Mark Mentovai
2015/05/05 22:41:35
This was only present for basename(). You can remo
scottmg
2015/05/05 23:23:52
Done.
|
| +#include <mach/mach.h> |
| +#include <unistd.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" |
| +#endif // OS_MACOSX |
| + |
| namespace crashpad { |
| namespace { |
| @@ -44,21 +53,22 @@ struct Options { |
| bool suspend; |
| }; |
| -void Usage(const std::string& me) { |
| +void Usage(const base::FilePath& me) { |
| fprintf(stderr, |
| -"Usage: %s [OPTION]... PID\n" |
| +"Usage: %" PRFilePath " [OPTION]... PID\n" |
| "Generate a minidump file containing a snapshot of a running process.\n" |
| "\n" |
| " -r, --no-suspend don't suspend the target process during dump generation\n" |
| " -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", |
| - me.c_str()); |
| + me.value().c_str()); |
| ToolSupport::UsageTail(me); |
| } |
| int GenerateDumpMain(int argc, char* argv[]) { |
| - const std::string me(basename(argv[0])); |
| + const base::FilePath me(base::FilePath(ToolSupport::UTF8ToFilePathStringType( |
|
Mark Mentovai
2015/05/05 22:41:35
Not loving what clang-format does with this, but I
scottmg
2015/05/05 23:23:52
I broke them into two statements, as it got even w
|
| + argv[0])).BaseName()); |
| enum OptionFlags { |
| // “Short” (single-character) options. |
| @@ -113,10 +123,14 @@ int GenerateDumpMain(int argc, char* argv[]) { |
| } |
| if (!StringToNumber(argv[0], &options.pid) || options.pid <= 0) { |
| - fprintf(stderr, "%s: invalid PID: %s\n", me.c_str(), argv[0]); |
| + fprintf(stderr, |
| + "%" PRFilePath ": invalid PID: %s\n", |
| + me.value().c_str(), |
| + argv[0]); |
|
Mark Mentovai
2015/05/05 22:41:36
Where this happens, it’s writing UTF-8 to stdout,
scottmg
2015/05/05 23:23:52
Filed: https://code.google.com/p/crashpad/issues/d
Mark Mentovai
2015/05/06 04:44:15
%ls is for me.value().c_str(). %ls says UTF-16, so
scottmg
2015/05/06 18:02:18
Oh, I misunderstood and was looking at `me`, rathe
|
| return EXIT_FAILURE; |
| } |
| +#if defined(OS_MACOSX) |
| task_t task = TaskForPID(options.pid); |
| if (task == TASK_NULL) { |
| return EXIT_FAILURE; |
| @@ -134,24 +148,51 @@ int GenerateDumpMain(int argc, char* argv[]) { |
| } |
| LOG(WARNING) << "operating on myself"; |
| } |
| +#elif defined(OS_WIN) |
| + ScopedKernelHANDLE process( |
| + OpenProcess(PROCESS_ALL_ACCESS, false, options.pid)); |
| + if (!process.is_valid()) { |
| + LOG(ERROR) << "could not open process " << options.pid; |
| + return EXIT_FAILURE; |
| + } |
| +#endif // OS_MACOSX |
| if (options.dump_path.empty()) { |
| options.dump_path = base::StringPrintf("minidump.%d", options.pid); |
| } |
| { |
| +#if defined(OS_MACOSX) |
| scoped_ptr<ScopedTaskSuspend> suspend; |
| if (options.suspend) { |
| suspend.reset(new ScopedTaskSuspend(task)); |
| } |
| +#elif defined(OS_WIN) |
| + if (options.suspend) { |
| + LOG(ERROR) << "TODO(scottmg): --suspend not implemented yet."; |
|
Mark Mentovai
2015/05/05 22:41:36
suspend is the default, so technically, it’s “--no
scottmg
2015/05/05 23:23:52
Done.
|
| + return EXIT_FAILURE; |
| + } |
| +#endif // OS_MACOSX |
| +#if defined(OS_MACOSX) |
| ProcessSnapshotMac process_snapshot; |
| if (!process_snapshot.Initialize(task)) { |
| return EXIT_FAILURE; |
| } |
| +#elif defined(OS_WIN) |
| + ProcessSnapshotWin process_snapshot; |
| + if (!process_snapshot.Initialize(process.get())) { |
| + return EXIT_FAILURE; |
| + } |
| +#endif // OS_MACOSX |
| FileWriter file_writer; |
| - if (!file_writer.Open(base::FilePath(options.dump_path), |
| +#if defined(OS_MACOSX) |
| + base::FilePath dump_path(options.dump_path); |
| +#elif defined(OS_WIN) |
| + base::FilePath dump_path(base::UTF8ToUTF16(options.dump_path)); |
| +#endif // OS_MACOSX |
| + if (!file_writer.Open(dump_path, |
| FileWriteMode::kTruncateOrCreate, |
| FilePermissions::kWorldReadable)) { |
| return EXIT_FAILURE; |
| @@ -173,6 +214,12 @@ int GenerateDumpMain(int argc, char* argv[]) { |
| } // namespace |
| } // namespace crashpad |
| +#if defined(OS_POSIX) |
| int main(int argc, char* argv[]) { |
| return crashpad::GenerateDumpMain(argc, argv); |
| } |
| +#elif defined(OS_WIN) |
| +int wmain(int argc, wchar_t* argv[]) { |
| + return crashpad::ToolSupport::Wmain(argc, argv, &crashpad::GenerateDumpMain); |
| +} |
| +#endif // OS_POSIX |