blob: 0e3daec3ef01f53553ada0b76368eb2197e7ef8d [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2012 The Chromium Authors
[email protected]e89298d2012-07-27 05:01:292// 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 Ikutac8d6b16f2024-04-15 16:59:196
[email protected]e89298d2012-07-27 05:01:297#include <stdlib.h>
8
9#include "base/at_exit.h"
10#include "base/command_line.h"
Tom Sepez315bfe82025-03-24 18:00:3011#include "base/compiler_specific.h"
[email protected]e89298d2012-07-27 05:01:2912#include "base/win/scoped_handle.h"
Joe Downingb0fb5422021-12-07 21:26:2013#include "remoting/base/logging.h"
[email protected]e89298d2012-07-27 05:01:2914
15namespace {
16
17// "--help" or "--?" prints the usage message.
18const char kHelpSwitchName[] = "help";
19const char kQuestionSwitchName[] = "?";
20
21const char kUsageMessage[] =
joedow900ad0c2017-04-20 21:31:1722 "\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]e89298d2012-07-27 05:01:2929
30// Exit codes:
31const int kSuccessExitCode = 0;
32const int kUsageExitCode = 1;
33const int kErrorExitCode = 2;
34
35void usage(const char* program_name) {
Tom Sepez315bfe82025-03-24 18:00:3036 UNSAFE_TODO(fprintf(stderr, kUsageMessage, program_name));
[email protected]e89298d2012-07-27 05:01:2937}
38
39} // namespace
40
41int main(int argc, char** argv) {
[email protected]b982d512014-05-23 09:42:0242 base::CommandLine::Init(argc, argv);
[email protected]e89298d2012-07-27 05:01:2943
44 base::AtExitManager exit_manager;
45
[email protected]bc825c0b2012-10-18 01:59:2946 remoting::InitHostLogging();
[email protected]e89298d2012-07-27 05:01:2947
[email protected]b982d512014-05-23 09:42:0248 const base::CommandLine* command_line =
49 base::CommandLine::ForCurrentProcess();
[email protected]e89298d2012-07-27 05:01:2950 if (command_line->HasSwitch(kHelpSwitchName) ||
51 command_line->HasSwitch(kQuestionSwitchName)) {
52 usage(argv[0]);
53 return kSuccessExitCode;
54 }
55
[email protected]b982d512014-05-23 09:42:0256 base::CommandLine::StringVector args = command_line->GetArgs();
[email protected]e89298d2012-07-27 05:01:2957 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 Cintron3ca38fad2025-11-10 17:43:4772 if (!process.is_valid()) {
[email protected]ad8cfa92014-05-21 20:06:2373 PLOG(ERROR) << "Failed to open the process " << pid;
[email protected]e89298d2012-07-27 05:01:2974 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 Cintron3ca38fad2025-11-10 17:43:4781 if (!thread.is_valid()) {
[email protected]ad8cfa92014-05-21 20:06:2382 PLOG(ERROR) << "Failed to create a remote thread in " << pid;
[email protected]e89298d2012-07-27 05:01:2983 return kErrorExitCode;
84 }
85
86 return kSuccessExitCode;
87}