| Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <windows.h> |
| Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 6 | |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include "base/at_exit.h" |
| 10 | #include "base/command_line.h" |
| Tom Sepez | 315bfe8 | 2025-03-24 18:00:30 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 12 | #include "base/win/scoped_handle.h" |
| Joe Downing | b0fb542 | 2021-12-07 21:26:20 | [diff] [blame] | 13 | #include "remoting/base/logging.h" |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // "--help" or "--?" prints the usage message. |
| 18 | const char kHelpSwitchName[] = "help"; |
| 19 | const char kQuestionSwitchName[] = "?"; |
| 20 | |
| 21 | const char kUsageMessage[] = |
| joedow | 900ad0c | 2017-04-20 21:31:17 | [diff] [blame] | 22 | "\n" |
| 23 | "Usage: %s <pid>\n" |
| 24 | "\n" |
| 25 | " pid - PID of the process to be crashed.\n" |
| 26 | "\n\n" |
| 27 | "Note: You may need to run this tool as SYSTEM\n" |
| 28 | " to prevent access denied errors.\n"; |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 29 | |
| 30 | // Exit codes: |
| 31 | const int kSuccessExitCode = 0; |
| 32 | const int kUsageExitCode = 1; |
| 33 | const int kErrorExitCode = 2; |
| 34 | |
| 35 | void usage(const char* program_name) { |
| Tom Sepez | 315bfe8 | 2025-03-24 18:00:30 | [diff] [blame] | 36 | UNSAFE_TODO(fprintf(stderr, kUsageMessage, program_name)); |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | int main(int argc, char** argv) { |
| [email protected] | b982d51 | 2014-05-23 09:42:02 | [diff] [blame] | 42 | base::CommandLine::Init(argc, argv); |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 43 | |
| 44 | base::AtExitManager exit_manager; |
| 45 | |
| [email protected] | bc825c0b | 2012-10-18 01:59:29 | [diff] [blame] | 46 | remoting::InitHostLogging(); |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 47 | |
| [email protected] | b982d51 | 2014-05-23 09:42:02 | [diff] [blame] | 48 | const base::CommandLine* command_line = |
| 49 | base::CommandLine::ForCurrentProcess(); |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 50 | if (command_line->HasSwitch(kHelpSwitchName) || |
| 51 | command_line->HasSwitch(kQuestionSwitchName)) { |
| 52 | usage(argv[0]); |
| 53 | return kSuccessExitCode; |
| 54 | } |
| 55 | |
| [email protected] | b982d51 | 2014-05-23 09:42:02 | [diff] [blame] | 56 | base::CommandLine::StringVector args = command_line->GetArgs(); |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 57 | if (args.size() != 1) { |
| 58 | usage(argv[0]); |
| 59 | return kUsageExitCode; |
| 60 | } |
| 61 | |
| 62 | int pid = _wtoi(args[0].c_str()); |
| 63 | if (pid == 0) { |
| 64 | LOG(ERROR) << "Invalid process PID: " << args[0]; |
| 65 | return kErrorExitCode; |
| 66 | } |
| 67 | |
| 68 | DWORD desired_access = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | |
| 69 | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ; |
| 70 | base::win::ScopedHandle process; |
| 71 | process.Set(OpenProcess(desired_access, FALSE, pid)); |
| Rafael Cintron | 3ca38fad | 2025-11-10 17:43:47 | [diff] [blame] | 72 | if (!process.is_valid()) { |
| [email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 73 | PLOG(ERROR) << "Failed to open the process " << pid; |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 74 | return kErrorExitCode; |
| 75 | } |
| 76 | |
| 77 | DWORD thread_id; |
| 78 | base::win::ScopedHandle thread; |
| 79 | thread.Set(CreateRemoteThread(process.Get(), NULL, 0, NULL, NULL, 0, |
| 80 | &thread_id)); |
| Rafael Cintron | 3ca38fad | 2025-11-10 17:43:47 | [diff] [blame] | 81 | if (!thread.is_valid()) { |
| [email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 82 | PLOG(ERROR) << "Failed to create a remote thread in " << pid; |
| [email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 83 | return kErrorExitCode; |
| 84 | } |
| 85 | |
| 86 | return kSuccessExitCode; |
| 87 | } |