Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include <fcntl.h> | 15 #include <fcntl.h> |
| 16 #include <getopt.h> | 16 #include <getopt.h> |
| 17 #include <libgen.h> | |
| 18 #include <mach/mach.h> | |
| 19 #include <stdio.h> | 17 #include <stdio.h> |
| 20 #include <stdlib.h> | 18 #include <stdlib.h> |
| 21 #include <unistd.h> | 19 #include <sys/types.h> |
| 22 | 20 |
| 23 #include <string> | 21 #include <string> |
| 24 | 22 |
| 25 #include "base/logging.h" | 23 #include "base/logging.h" |
| 26 #include "base/mac/scoped_mach_port.h" | |
| 27 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
| 28 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "build/build_config.h" | |
| 29 #include "minidump/minidump_file_writer.h" | 27 #include "minidump/minidump_file_writer.h" |
| 30 #include "snapshot/mac/process_snapshot_mac.h" | |
| 31 #include "tools/tool_support.h" | 28 #include "tools/tool_support.h" |
| 32 #include "util/file/file_writer.h" | 29 #include "util/file/file_writer.h" |
| 30 #include "util/posix/drop_privileges.h" | |
| 31 #include "util/stdlib/string_number_conversion.h" | |
| 32 | |
| 33 #if defined(OS_MACOSX) | |
| 34 #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.
| |
| 35 #include <mach/mach.h> | |
| 36 #include <unistd.h> | |
| 37 | |
| 38 #include "base/mac/scoped_mach_port.h" | |
| 39 #include "snapshot/mac/process_snapshot_mac.h" | |
| 33 #include "util/mach/scoped_task_suspend.h" | 40 #include "util/mach/scoped_task_suspend.h" |
| 34 #include "util/mach/task_for_pid.h" | 41 #include "util/mach/task_for_pid.h" |
| 35 #include "util/posix/drop_privileges.h" | 42 #elif defined(OS_WIN) |
| 36 #include "util/stdlib/string_number_conversion.h" | 43 #include "base/strings/utf_string_conversions.h" |
| 44 #include "snapshot/win/process_snapshot_win.h" | |
| 45 #endif // OS_MACOSX | |
| 37 | 46 |
| 38 namespace crashpad { | 47 namespace crashpad { |
| 39 namespace { | 48 namespace { |
| 40 | 49 |
| 41 struct Options { | 50 struct Options { |
| 42 std::string dump_path; | 51 std::string dump_path; |
| 43 pid_t pid; | 52 pid_t pid; |
| 44 bool suspend; | 53 bool suspend; |
| 45 }; | 54 }; |
| 46 | 55 |
| 47 void Usage(const std::string& me) { | 56 void Usage(const base::FilePath& me) { |
| 48 fprintf(stderr, | 57 fprintf(stderr, |
| 49 "Usage: %s [OPTION]... PID\n" | 58 "Usage: %" PRFilePath " [OPTION]... PID\n" |
| 50 "Generate a minidump file containing a snapshot of a running process.\n" | 59 "Generate a minidump file containing a snapshot of a running process.\n" |
| 51 "\n" | 60 "\n" |
| 52 " -r, --no-suspend don't suspend the target process during dump generation\n" | 61 " -r, --no-suspend don't suspend the target process during dump generation\n" |
| 53 " -o, --output=FILE write the minidump to FILE instead of minidump.PID\n" | 62 " -o, --output=FILE write the minidump to FILE instead of minidump.PID\n" |
| 54 " --help display this help and exit\n" | 63 " --help display this help and exit\n" |
| 55 " --version output version information and exit\n", | 64 " --version output version information and exit\n", |
| 56 me.c_str()); | 65 me.value().c_str()); |
| 57 ToolSupport::UsageTail(me); | 66 ToolSupport::UsageTail(me); |
| 58 } | 67 } |
| 59 | 68 |
| 60 int GenerateDumpMain(int argc, char* argv[]) { | 69 int GenerateDumpMain(int argc, char* argv[]) { |
| 61 const std::string me(basename(argv[0])); | 70 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
| |
| 71 argv[0])).BaseName()); | |
| 62 | 72 |
| 63 enum OptionFlags { | 73 enum OptionFlags { |
| 64 // “Short” (single-character) options. | 74 // “Short” (single-character) options. |
| 65 kOptionOutput = 'o', | 75 kOptionOutput = 'o', |
| 66 kOptionNoSuspend = 'r', | 76 kOptionNoSuspend = 'r', |
| 67 | 77 |
| 68 // Long options without short equivalents. | 78 // Long options without short equivalents. |
| 69 kOptionLastChar = 255, | 79 kOptionLastChar = 255, |
| 70 | 80 |
| 71 // Standard options. | 81 // Standard options. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 } | 116 } |
| 107 argc -= optind; | 117 argc -= optind; |
| 108 argv += optind; | 118 argv += optind; |
| 109 | 119 |
| 110 if (argc != 1) { | 120 if (argc != 1) { |
| 111 ToolSupport::UsageHint(me, "PID is required"); | 121 ToolSupport::UsageHint(me, "PID is required"); |
| 112 return EXIT_FAILURE; | 122 return EXIT_FAILURE; |
| 113 } | 123 } |
| 114 | 124 |
| 115 if (!StringToNumber(argv[0], &options.pid) || options.pid <= 0) { | 125 if (!StringToNumber(argv[0], &options.pid) || options.pid <= 0) { |
| 116 fprintf(stderr, "%s: invalid PID: %s\n", me.c_str(), argv[0]); | 126 fprintf(stderr, |
| 127 "%" PRFilePath ": invalid PID: %s\n", | |
| 128 me.value().c_str(), | |
| 129 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
| |
| 117 return EXIT_FAILURE; | 130 return EXIT_FAILURE; |
| 118 } | 131 } |
| 119 | 132 |
| 133 #if defined(OS_MACOSX) | |
| 120 task_t task = TaskForPID(options.pid); | 134 task_t task = TaskForPID(options.pid); |
| 121 if (task == TASK_NULL) { | 135 if (task == TASK_NULL) { |
| 122 return EXIT_FAILURE; | 136 return EXIT_FAILURE; |
| 123 } | 137 } |
| 124 base::mac::ScopedMachSendRight task_owner(task); | 138 base::mac::ScopedMachSendRight task_owner(task); |
| 125 | 139 |
| 126 // This tool may have been installed as a setuid binary so that TaskForPID() | 140 // This tool may have been installed as a setuid binary so that TaskForPID() |
| 127 // could succeed. Drop any privileges now that they’re no longer necessary. | 141 // could succeed. Drop any privileges now that they’re no longer necessary. |
| 128 DropPrivileges(); | 142 DropPrivileges(); |
| 129 | 143 |
| 130 if (options.pid == getpid()) { | 144 if (options.pid == getpid()) { |
| 131 if (options.suspend) { | 145 if (options.suspend) { |
| 132 LOG(ERROR) << "cannot suspend myself"; | 146 LOG(ERROR) << "cannot suspend myself"; |
| 133 return EXIT_FAILURE; | 147 return EXIT_FAILURE; |
| 134 } | 148 } |
| 135 LOG(WARNING) << "operating on myself"; | 149 LOG(WARNING) << "operating on myself"; |
| 136 } | 150 } |
| 151 #elif defined(OS_WIN) | |
| 152 ScopedKernelHANDLE process( | |
| 153 OpenProcess(PROCESS_ALL_ACCESS, false, options.pid)); | |
| 154 if (!process.is_valid()) { | |
| 155 LOG(ERROR) << "could not open process " << options.pid; | |
| 156 return EXIT_FAILURE; | |
| 157 } | |
| 158 #endif // OS_MACOSX | |
| 137 | 159 |
| 138 if (options.dump_path.empty()) { | 160 if (options.dump_path.empty()) { |
| 139 options.dump_path = base::StringPrintf("minidump.%d", options.pid); | 161 options.dump_path = base::StringPrintf("minidump.%d", options.pid); |
| 140 } | 162 } |
| 141 | 163 |
| 142 { | 164 { |
| 165 #if defined(OS_MACOSX) | |
| 143 scoped_ptr<ScopedTaskSuspend> suspend; | 166 scoped_ptr<ScopedTaskSuspend> suspend; |
| 144 if (options.suspend) { | 167 if (options.suspend) { |
| 145 suspend.reset(new ScopedTaskSuspend(task)); | 168 suspend.reset(new ScopedTaskSuspend(task)); |
| 146 } | 169 } |
| 170 #elif defined(OS_WIN) | |
| 171 if (options.suspend) { | |
| 172 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.
| |
| 173 return EXIT_FAILURE; | |
| 174 } | |
| 175 #endif // OS_MACOSX | |
| 147 | 176 |
| 177 #if defined(OS_MACOSX) | |
| 148 ProcessSnapshotMac process_snapshot; | 178 ProcessSnapshotMac process_snapshot; |
| 149 if (!process_snapshot.Initialize(task)) { | 179 if (!process_snapshot.Initialize(task)) { |
| 150 return EXIT_FAILURE; | 180 return EXIT_FAILURE; |
| 151 } | 181 } |
| 182 #elif defined(OS_WIN) | |
| 183 ProcessSnapshotWin process_snapshot; | |
| 184 if (!process_snapshot.Initialize(process.get())) { | |
| 185 return EXIT_FAILURE; | |
| 186 } | |
| 187 #endif // OS_MACOSX | |
| 152 | 188 |
| 153 FileWriter file_writer; | 189 FileWriter file_writer; |
| 154 if (!file_writer.Open(base::FilePath(options.dump_path), | 190 #if defined(OS_MACOSX) |
| 191 base::FilePath dump_path(options.dump_path); | |
| 192 #elif defined(OS_WIN) | |
| 193 base::FilePath dump_path(base::UTF8ToUTF16(options.dump_path)); | |
| 194 #endif // OS_MACOSX | |
| 195 if (!file_writer.Open(dump_path, | |
| 155 FileWriteMode::kTruncateOrCreate, | 196 FileWriteMode::kTruncateOrCreate, |
| 156 FilePermissions::kWorldReadable)) { | 197 FilePermissions::kWorldReadable)) { |
| 157 return EXIT_FAILURE; | 198 return EXIT_FAILURE; |
| 158 } | 199 } |
| 159 | 200 |
| 160 MinidumpFileWriter minidump; | 201 MinidumpFileWriter minidump; |
| 161 minidump.InitializeFromSnapshot(&process_snapshot); | 202 minidump.InitializeFromSnapshot(&process_snapshot); |
| 162 if (!minidump.WriteEverything(&file_writer)) { | 203 if (!minidump.WriteEverything(&file_writer)) { |
| 163 if (unlink(options.dump_path.c_str()) != 0) { | 204 if (unlink(options.dump_path.c_str()) != 0) { |
| 164 PLOG(ERROR) << "unlink"; | 205 PLOG(ERROR) << "unlink"; |
| 165 } | 206 } |
| 166 return EXIT_FAILURE; | 207 return EXIT_FAILURE; |
| 167 } | 208 } |
| 168 } | 209 } |
| 169 | 210 |
| 170 return EXIT_SUCCESS; | 211 return EXIT_SUCCESS; |
| 171 } | 212 } |
| 172 | 213 |
| 173 } // namespace | 214 } // namespace |
| 174 } // namespace crashpad | 215 } // namespace crashpad |
| 175 | 216 |
| 217 #if defined(OS_POSIX) | |
| 176 int main(int argc, char* argv[]) { | 218 int main(int argc, char* argv[]) { |
| 177 return crashpad::GenerateDumpMain(argc, argv); | 219 return crashpad::GenerateDumpMain(argc, argv); |
| 178 } | 220 } |
| 221 #elif defined(OS_WIN) | |
| 222 int wmain(int argc, wchar_t* argv[]) { | |
| 223 return crashpad::ToolSupport::Wmain(argc, argv, &crashpad::GenerateDumpMain); | |
| 224 } | |
| 225 #endif // OS_POSIX | |
| OLD | NEW |