breakpad_tester_win.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #include "base/at_exit.h"
  7. #include "base/command_line.h"
  8. #include "base/win/scoped_handle.h"
  9. #include "remoting/base/logging.h"
  10. namespace {
  11. // "--help" or "--?" prints the usage message.
  12. const char kHelpSwitchName[] = "help";
  13. const char kQuestionSwitchName[] = "?";
  14. const char kUsageMessage[] =
  15. "\n"
  16. "Usage: %s <pid>\n"
  17. "\n"
  18. " pid - PID of the process to be crashed.\n"
  19. "\n\n"
  20. "Note: You may need to run this tool as SYSTEM\n"
  21. " to prevent access denied errors.\n";
  22. // Exit codes:
  23. const int kSuccessExitCode = 0;
  24. const int kUsageExitCode = 1;
  25. const int kErrorExitCode = 2;
  26. void usage(const char* program_name) {
  27. fprintf(stderr, kUsageMessage, program_name);
  28. }
  29. } // namespace
  30. int main(int argc, char** argv) {
  31. base::CommandLine::Init(argc, argv);
  32. base::AtExitManager exit_manager;
  33. remoting::InitHostLogging();
  34. const base::CommandLine* command_line =
  35. base::CommandLine::ForCurrentProcess();
  36. if (command_line->HasSwitch(kHelpSwitchName) ||
  37. command_line->HasSwitch(kQuestionSwitchName)) {
  38. usage(argv[0]);
  39. return kSuccessExitCode;
  40. }
  41. base::CommandLine::StringVector args = command_line->GetArgs();
  42. if (args.size() != 1) {
  43. usage(argv[0]);
  44. return kUsageExitCode;
  45. }
  46. int pid = _wtoi(args[0].c_str());
  47. if (pid == 0) {
  48. LOG(ERROR) << "Invalid process PID: " << args[0];
  49. return kErrorExitCode;
  50. }
  51. DWORD desired_access = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
  52. PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ;
  53. base::win::ScopedHandle process;
  54. process.Set(OpenProcess(desired_access, FALSE, pid));
  55. if (!process.IsValid()) {
  56. PLOG(ERROR) << "Failed to open the process " << pid;
  57. return kErrorExitCode;
  58. }
  59. DWORD thread_id;
  60. base::win::ScopedHandle thread;
  61. thread.Set(CreateRemoteThread(process.Get(), NULL, 0, NULL, NULL, 0,
  62. &thread_id));
  63. if (!thread.IsValid()) {
  64. PLOG(ERROR) << "Failed to create a remote thread in " << pid;
  65. return kErrorExitCode;
  66. }
  67. return kSuccessExitCode;
  68. }