1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/at_exit.h"
9 #include "base/command_line.h"
10 #include "base/win/scoped_handle.h"
11 #include "remoting/host/logging.h"
15 // "--help" or "--?" prints the usage message.
16 const char kHelpSwitchName
[] = "help";
17 const char kQuestionSwitchName
[] = "?";
19 const char kUsageMessage
[] =
23 " pid - PID of the process to be crashed.\n";
26 const int kSuccessExitCode
= 0;
27 const int kUsageExitCode
= 1;
28 const int kErrorExitCode
= 2;
30 void usage(const char* program_name
) {
31 fprintf(stderr
, kUsageMessage
, program_name
);
36 int main(int argc
, char** argv
) {
37 base::CommandLine::Init(argc
, argv
);
39 base::AtExitManager exit_manager
;
41 remoting::InitHostLogging();
43 const base::CommandLine
* command_line
=
44 base::CommandLine::ForCurrentProcess();
45 if (command_line
->HasSwitch(kHelpSwitchName
) ||
46 command_line
->HasSwitch(kQuestionSwitchName
)) {
48 return kSuccessExitCode
;
51 base::CommandLine::StringVector args
= command_line
->GetArgs();
52 if (args
.size() != 1) {
54 return kUsageExitCode
;
57 int pid
= _wtoi(args
[0].c_str());
59 LOG(ERROR
) << "Invalid process PID: " << args
[0];
60 return kErrorExitCode
;
63 DWORD desired_access
= PROCESS_CREATE_THREAD
| PROCESS_QUERY_INFORMATION
|
64 PROCESS_VM_OPERATION
| PROCESS_VM_WRITE
| PROCESS_VM_READ
;
65 base::win::ScopedHandle process
;
66 process
.Set(OpenProcess(desired_access
, FALSE
, pid
));
67 if (!process
.IsValid()) {
68 PLOG(ERROR
) << "Failed to open the process " << pid
;
69 return kErrorExitCode
;
73 base::win::ScopedHandle thread
;
74 thread
.Set(CreateRemoteThread(process
.Get(), NULL
, 0, NULL
, NULL
, 0,
76 if (!thread
.IsValid()) {
77 PLOG(ERROR
) << "Failed to create a remote thread in " << pid
;
78 return kErrorExitCode
;
81 return kSuccessExitCode
;