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" |
28 #include "tools/tool_support.h" | |
29 #include "util/file/basename.h" | |
30 #include "util/file/file_writer.h" | |
31 #include "util/posix/drop_privileges.h" | |
32 #include "util/stdlib/string_number_conversion.h" | |
33 | |
34 #if defined(OS_MACOSX) | |
35 #include <libgen.h> | |
36 #include <mach/mach.h> | |
37 #include <unistd.h> | |
38 | |
39 #include "base/mac/scoped_mach_port.h" | |
30 #include "snapshot/mac/process_snapshot_mac.h" | 40 #include "snapshot/mac/process_snapshot_mac.h" |
31 #include "tools/tool_support.h" | |
32 #include "util/file/file_writer.h" | |
33 #include "util/mach/scoped_task_suspend.h" | 41 #include "util/mach/scoped_task_suspend.h" |
34 #include "util/mach/task_for_pid.h" | 42 #include "util/mach/task_for_pid.h" |
35 #include "util/posix/drop_privileges.h" | 43 #elif defined(OS_WIN) |
36 #include "util/stdlib/string_number_conversion.h" | 44 #include "base/strings/utf_string_conversions.h" |
45 #include "snapshot/win/process_snapshot_win.h" | |
46 #endif // OS_MACOSX | |
37 | 47 |
38 namespace crashpad { | 48 namespace crashpad { |
39 namespace { | 49 namespace { |
40 | 50 |
41 struct Options { | 51 struct Options { |
42 std::string dump_path; | 52 std::string dump_path; |
43 pid_t pid; | 53 pid_t pid; |
44 bool suspend; | 54 bool suspend; |
45 }; | 55 }; |
46 | 56 |
47 void Usage(const std::string& me) { | 57 void Usage(const std::string& me) { |
48 fprintf(stderr, | 58 fprintf(stderr, |
49 "Usage: %s [OPTION]... PID\n" | 59 "Usage: %s [OPTION]... PID\n" |
50 "Generate a minidump file containing a snapshot of a running process.\n" | 60 "Generate a minidump file containing a snapshot of a running process.\n" |
51 "\n" | 61 "\n" |
52 " -r, --no-suspend don't suspend the target process during dump generation\n" | 62 " -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" | 63 " -o, --output=FILE write the minidump to FILE instead of minidump.PID\n" |
54 " --help display this help and exit\n" | 64 " --help display this help and exit\n" |
55 " --version output version information and exit\n", | 65 " --version output version information and exit\n", |
56 me.c_str()); | 66 me.c_str()); |
57 ToolSupport::UsageTail(me); | 67 ToolSupport::UsageTail(me); |
58 } | 68 } |
59 | 69 |
60 int GenerateDumpMain(int argc, char* argv[]) { | 70 int GenerateDumpMain(int argc, char* argv[]) { |
61 const std::string me(basename(argv[0])); | 71 const std::string me(Basename(argv[0])); |
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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, "%s: invalid PID: %s\n", me.c_str(), argv[0]); |
117 return EXIT_FAILURE; | 127 return EXIT_FAILURE; |
118 } | 128 } |
119 | 129 |
130 #if defined(OS_MACOSX) | |
Mark Mentovai
2015/05/04 21:21:58
I hope that over time, we can refactor things well
scottmg
2015/05/05 19:26:22
OK. It seems that having a typedef for task_t/proc
Mark Mentovai
2015/05/05 22:41:35
scottmg wrote:
| |
120 task_t task = TaskForPID(options.pid); | 131 task_t task = TaskForPID(options.pid); |
121 if (task == TASK_NULL) { | 132 if (task == TASK_NULL) { |
122 return EXIT_FAILURE; | 133 return EXIT_FAILURE; |
123 } | 134 } |
124 base::mac::ScopedMachSendRight task_owner(task); | 135 base::mac::ScopedMachSendRight task_owner(task); |
125 | 136 |
126 // This tool may have been installed as a setuid binary so that TaskForPID() | 137 // 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. | 138 // could succeed. Drop any privileges now that they’re no longer necessary. |
128 DropPrivileges(); | 139 DropPrivileges(); |
129 | 140 |
130 if (options.pid == getpid()) { | 141 if (options.pid == getpid()) { |
131 if (options.suspend) { | 142 if (options.suspend) { |
132 LOG(ERROR) << "cannot suspend myself"; | 143 LOG(ERROR) << "cannot suspend myself"; |
133 return EXIT_FAILURE; | 144 return EXIT_FAILURE; |
134 } | 145 } |
135 LOG(WARNING) << "operating on myself"; | 146 LOG(WARNING) << "operating on myself"; |
136 } | 147 } |
148 #elif defined(OS_WIN) | |
149 ScopedKernelHANDLE process( | |
150 OpenProcess(PROCESS_ALL_ACCESS, false, options.pid)); | |
151 if (!process.is_valid()) { | |
152 LOG(ERROR) << "could not open process " << options.pid; | |
153 return EXIT_FAILURE; | |
154 } | |
155 #endif // OS_MACOSX | |
137 | 156 |
138 if (options.dump_path.empty()) { | 157 if (options.dump_path.empty()) { |
139 options.dump_path = base::StringPrintf("minidump.%d", options.pid); | 158 options.dump_path = base::StringPrintf("minidump.%d", options.pid); |
140 } | 159 } |
141 | 160 |
142 { | 161 { |
162 #if defined(OS_MACOSX) | |
143 scoped_ptr<ScopedTaskSuspend> suspend; | 163 scoped_ptr<ScopedTaskSuspend> suspend; |
144 if (options.suspend) { | 164 if (options.suspend) { |
145 suspend.reset(new ScopedTaskSuspend(task)); | 165 suspend.reset(new ScopedTaskSuspend(task)); |
146 } | 166 } |
167 #elif defined(OS_WIN) | |
168 if (options.suspend) { | |
169 LOG(ERROR) << "TODO(scottmg): --suspend not implemented yet."; | |
170 return EXIT_FAILURE; | |
171 } | |
172 #endif // OS_MACOSX | |
147 | 173 |
174 #if defined(OS_MACOSX) | |
148 ProcessSnapshotMac process_snapshot; | 175 ProcessSnapshotMac process_snapshot; |
149 if (!process_snapshot.Initialize(task)) { | 176 if (!process_snapshot.Initialize(task)) { |
150 return EXIT_FAILURE; | 177 return EXIT_FAILURE; |
151 } | 178 } |
179 #elif defined(OS_WIN) | |
180 ProcessSnapshotWin process_snapshot; | |
181 if (!process_snapshot.Initialize(process.get())) { | |
182 return EXIT_FAILURE; | |
183 } | |
184 #endif // OS_MACOSX | |
152 | 185 |
153 FileWriter file_writer; | 186 FileWriter file_writer; |
154 if (!file_writer.Open(base::FilePath(options.dump_path), | 187 #if defined(OS_MACOSX) |
188 base::FilePath dump_path(options.dump_path); | |
189 #elif defined(OS_WIN) | |
190 base::FilePath dump_path(base::UTF8ToUTF16(options.dump_path)); | |
191 #endif // OS_MACOSX | |
192 if (!file_writer.Open(dump_path, | |
155 FileWriteMode::kTruncateOrCreate, | 193 FileWriteMode::kTruncateOrCreate, |
156 FilePermissions::kWorldReadable)) { | 194 FilePermissions::kWorldReadable)) { |
157 return EXIT_FAILURE; | 195 return EXIT_FAILURE; |
158 } | 196 } |
159 | 197 |
160 MinidumpFileWriter minidump; | 198 MinidumpFileWriter minidump; |
161 minidump.InitializeFromSnapshot(&process_snapshot); | 199 minidump.InitializeFromSnapshot(&process_snapshot); |
162 if (!minidump.WriteEverything(&file_writer)) { | 200 if (!minidump.WriteEverything(&file_writer)) { |
163 if (unlink(options.dump_path.c_str()) != 0) { | 201 if (unlink(options.dump_path.c_str()) != 0) { |
164 PLOG(ERROR) << "unlink"; | 202 PLOG(ERROR) << "unlink"; |
165 } | 203 } |
166 return EXIT_FAILURE; | 204 return EXIT_FAILURE; |
167 } | 205 } |
168 } | 206 } |
169 | 207 |
170 return EXIT_SUCCESS; | 208 return EXIT_SUCCESS; |
171 } | 209 } |
172 | 210 |
173 } // namespace | 211 } // namespace |
174 } // namespace crashpad | 212 } // namespace crashpad |
175 | 213 |
176 int main(int argc, char* argv[]) { | 214 int main(int argc, char* argv[]) { |
177 return crashpad::GenerateDumpMain(argc, argv); | 215 return crashpad::GenerateDumpMain(argc, argv); |
178 } | 216 } |
OLD | NEW |