| 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 <mach/mach.h> |
| 35 #include <unistd.h> |
| 36 |
| 37 #include "base/mac/scoped_mach_port.h" |
| 38 #include "snapshot/mac/process_snapshot_mac.h" |
| 33 #include "util/mach/scoped_task_suspend.h" | 39 #include "util/mach/scoped_task_suspend.h" |
| 34 #include "util/mach/task_for_pid.h" | 40 #include "util/mach/task_for_pid.h" |
| 35 #include "util/posix/drop_privileges.h" | 41 #elif defined(OS_WIN) |
| 36 #include "util/stdlib/string_number_conversion.h" | 42 #include "base/strings/utf_string_conversions.h" |
| 43 #include "snapshot/win/process_snapshot_win.h" |
| 44 #endif // OS_MACOSX |
| 37 | 45 |
| 38 namespace crashpad { | 46 namespace crashpad { |
| 39 namespace { | 47 namespace { |
| 40 | 48 |
| 41 struct Options { | 49 struct Options { |
| 42 std::string dump_path; | 50 std::string dump_path; |
| 43 pid_t pid; | 51 pid_t pid; |
| 44 bool suspend; | 52 bool suspend; |
| 45 }; | 53 }; |
| 46 | 54 |
| 47 void Usage(const std::string& me) { | 55 void Usage(const base::FilePath& me) { |
| 48 fprintf(stderr, | 56 fprintf(stderr, |
| 49 "Usage: %s [OPTION]... PID\n" | 57 "Usage: %" PRFilePath " [OPTION]... PID\n" |
| 50 "Generate a minidump file containing a snapshot of a running process.\n" | 58 "Generate a minidump file containing a snapshot of a running process.\n" |
| 51 "\n" | 59 "\n" |
| 52 " -r, --no-suspend don't suspend the target process during dump generation\n" | 60 " -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" | 61 " -o, --output=FILE write the minidump to FILE instead of minidump.PID\n" |
| 54 " --help display this help and exit\n" | 62 " --help display this help and exit\n" |
| 55 " --version output version information and exit\n", | 63 " --version output version information and exit\n", |
| 56 me.c_str()); | 64 me.value().c_str()); |
| 57 ToolSupport::UsageTail(me); | 65 ToolSupport::UsageTail(me); |
| 58 } | 66 } |
| 59 | 67 |
| 60 int GenerateDumpMain(int argc, char* argv[]) { | 68 int GenerateDumpMain(int argc, char* argv[]) { |
| 61 const std::string me(basename(argv[0])); | 69 const base::FilePath argv0( |
| 70 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); |
| 71 const base::FilePath me(argv0.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]); |
| 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): --no-suspend is required for now."; |
| 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 base::FilePath dump_path( |
| 191 ToolSupport::CommandLineArgumentToFilePathStringType( |
| 192 options.dump_path)); |
| 193 if (!file_writer.Open(dump_path, |
| 155 FileWriteMode::kTruncateOrCreate, | 194 FileWriteMode::kTruncateOrCreate, |
| 156 FilePermissions::kWorldReadable)) { | 195 FilePermissions::kWorldReadable)) { |
| 157 return EXIT_FAILURE; | 196 return EXIT_FAILURE; |
| 158 } | 197 } |
| 159 | 198 |
| 160 MinidumpFileWriter minidump; | 199 MinidumpFileWriter minidump; |
| 161 minidump.InitializeFromSnapshot(&process_snapshot); | 200 minidump.InitializeFromSnapshot(&process_snapshot); |
| 162 if (!minidump.WriteEverything(&file_writer)) { | 201 if (!minidump.WriteEverything(&file_writer)) { |
| 163 if (unlink(options.dump_path.c_str()) != 0) { | 202 if (unlink(options.dump_path.c_str()) != 0) { |
| 164 PLOG(ERROR) << "unlink"; | 203 PLOG(ERROR) << "unlink"; |
| 165 } | 204 } |
| 166 return EXIT_FAILURE; | 205 return EXIT_FAILURE; |
| 167 } | 206 } |
| 168 } | 207 } |
| 169 | 208 |
| 170 return EXIT_SUCCESS; | 209 return EXIT_SUCCESS; |
| 171 } | 210 } |
| 172 | 211 |
| 173 } // namespace | 212 } // namespace |
| 174 } // namespace crashpad | 213 } // namespace crashpad |
| 175 | 214 |
| 215 #if defined(OS_POSIX) |
| 176 int main(int argc, char* argv[]) { | 216 int main(int argc, char* argv[]) { |
| 177 return crashpad::GenerateDumpMain(argc, argv); | 217 return crashpad::GenerateDumpMain(argc, argv); |
| 178 } | 218 } |
| 219 #elif defined(OS_WIN) |
| 220 int wmain(int argc, wchar_t* argv[]) { |
| 221 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::GenerateDumpMain); |
| 222 } |
| 223 #endif // OS_POSIX |
| OLD | NEW |