OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include <fcntl.h> |
| 16 #include <getopt.h> |
| 17 #include <libgen.h> |
| 18 #include <mach/mach.h> |
| 19 #include <stdio.h> |
| 20 #include <stdlib.h> |
| 21 #include <unistd.h> |
| 22 |
| 23 #include <string> |
| 24 |
| 25 #include "base/logging.h" |
| 26 #include "base/mac/scoped_mach_port.h" |
| 27 #include "base/memory/scoped_ptr.h" |
| 28 #include "base/strings/stringprintf.h" |
| 29 #include "minidump/minidump_file_writer.h" |
| 30 #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" |
| 34 #include "util/mach/task_for_pid.h" |
| 35 #include "util/posix/drop_privileges.h" |
| 36 #include "util/stdlib/string_number_conversion.h" |
| 37 |
| 38 namespace crashpad { |
| 39 namespace { |
| 40 |
| 41 struct Options { |
| 42 std::string dump_path; |
| 43 pid_t pid; |
| 44 bool suspend; |
| 45 }; |
| 46 |
| 47 void Usage(const std::string& me) { |
| 48 fprintf(stderr, |
| 49 "Usage: %s [OPTION]... PID\n" |
| 50 "Generate a minidump file containing a snapshot of a running process.\n" |
| 51 "\n" |
| 52 " -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" |
| 54 " --help display this help and exit\n" |
| 55 " --version output version information and exit\n", |
| 56 me.c_str()); |
| 57 ToolSupport::UsageTail(me); |
| 58 } |
| 59 |
| 60 int GenerateDumpMain(int argc, char* argv[]) { |
| 61 const std::string me(basename(argv[0])); |
| 62 |
| 63 enum OptionFlags { |
| 64 // “Short” (single-character) options. |
| 65 kOptionOutput = 'o', |
| 66 kOptionNoSuspend = 'r', |
| 67 |
| 68 // Long options without short equivalents. |
| 69 kOptionLastChar = 255, |
| 70 |
| 71 // Standard options. |
| 72 kOptionHelp = -2, |
| 73 kOptionVersion = -3, |
| 74 }; |
| 75 |
| 76 Options options = {}; |
| 77 options.suspend = true; |
| 78 |
| 79 const struct option long_options[] = { |
| 80 {"no_suspend", no_argument, nullptr, kOptionNoSuspend}, |
| 81 {"output", required_argument, nullptr, kOptionOutput}, |
| 82 {"help", no_argument, nullptr, kOptionHelp}, |
| 83 {"version", no_argument, nullptr, kOptionVersion}, |
| 84 {nullptr, 0, nullptr, 0}, |
| 85 }; |
| 86 |
| 87 int opt; |
| 88 while ((opt = getopt_long(argc, argv, "o:r", long_options, nullptr)) != -1) { |
| 89 switch (opt) { |
| 90 case kOptionOutput: |
| 91 options.dump_path = optarg; |
| 92 break; |
| 93 case kOptionNoSuspend: |
| 94 options.suspend = false; |
| 95 break; |
| 96 case kOptionHelp: |
| 97 Usage(me); |
| 98 return EXIT_SUCCESS; |
| 99 case kOptionVersion: |
| 100 ToolSupport::Version(me); |
| 101 return EXIT_SUCCESS; |
| 102 default: |
| 103 ToolSupport::UsageHint(me, nullptr); |
| 104 return EXIT_FAILURE; |
| 105 } |
| 106 } |
| 107 argc -= optind; |
| 108 argv += optind; |
| 109 |
| 110 if (argc != 1) { |
| 111 ToolSupport::UsageHint(me, "PID is required"); |
| 112 return EXIT_FAILURE; |
| 113 } |
| 114 |
| 115 if (!StringToNumber(argv[0], &options.pid) || options.pid <= 0) { |
| 116 fprintf(stderr, "%s: invalid PID: %s\n", me.c_str(), argv[0]); |
| 117 return EXIT_FAILURE; |
| 118 } |
| 119 |
| 120 task_t task = TaskForPID(options.pid); |
| 121 if (task == TASK_NULL) { |
| 122 return EXIT_FAILURE; |
| 123 } |
| 124 base::mac::ScopedMachSendRight task_owner(task); |
| 125 |
| 126 // 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. |
| 128 DropPrivileges(); |
| 129 |
| 130 if (options.pid == getpid()) { |
| 131 if (options.suspend) { |
| 132 LOG(ERROR) << "cannot suspend myself"; |
| 133 return EXIT_FAILURE; |
| 134 } |
| 135 LOG(WARNING) << "operating on myself"; |
| 136 } |
| 137 |
| 138 if (options.dump_path.empty()) { |
| 139 options.dump_path = base::StringPrintf("minidump.%d", options.pid); |
| 140 } |
| 141 |
| 142 { |
| 143 scoped_ptr<ScopedTaskSuspend> suspend; |
| 144 if (options.suspend) { |
| 145 suspend.reset(new ScopedTaskSuspend(task)); |
| 146 } |
| 147 |
| 148 ProcessSnapshotMac process_snapshot; |
| 149 if (!process_snapshot.Initialize(task)) { |
| 150 return EXIT_FAILURE; |
| 151 } |
| 152 |
| 153 FileWriter file_writer; |
| 154 if (!file_writer.Open(base::FilePath(options.dump_path), |
| 155 O_WRONLY | O_CREAT | O_TRUNC, |
| 156 0644)) { |
| 157 return EXIT_FAILURE; |
| 158 } |
| 159 |
| 160 MinidumpFileWriter minidump; |
| 161 minidump.InitializeFromSnapshot(&process_snapshot); |
| 162 if (!minidump.WriteEverything(&file_writer)) { |
| 163 if (unlink(options.dump_path.c_str()) != 0) { |
| 164 PLOG(ERROR) << "unlink"; |
| 165 } |
| 166 return EXIT_FAILURE; |
| 167 } |
| 168 } |
| 169 |
| 170 return EXIT_SUCCESS; |
| 171 } |
| 172 |
| 173 } // namespace |
| 174 } // namespace crashpad |
| 175 |
| 176 int main(int argc, char* argv[]) { |
| 177 return crashpad::GenerateDumpMain(argc, argv); |
| 178 } |
OLD | NEW |