| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/nacl/common/nacl_debug_exception_handler_win.h" | 5 #include "components/nacl/common/nacl_debug_exception_handler_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
| 9 #include "base/win/scoped_handle.h" | 9 #include "base/win/scoped_handle.h" |
| 10 #include "native_client/src/public/win/debug_exception_handler.h" | 10 #include "native_client/src/public/win/debug_exception_handler.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 class DebugExceptionHandler : public base::PlatformThread::Delegate { | 14 class DebugExceptionHandler : public base::PlatformThread::Delegate { |
| 15 public: | 15 public: |
| 16 DebugExceptionHandler( | 16 DebugExceptionHandler(base::Process nacl_process, |
| 17 base::Process nacl_process, | 17 const std::string& startup_info, |
| 18 const std::string& startup_info, | 18 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 19 const scoped_refptr<base::MessageLoopProxy>& message_loop, | 19 const base::Callback<void(bool)>& on_connected) |
| 20 const base::Callback<void(bool)>& on_connected) | |
| 21 : nacl_process_(nacl_process.Pass()), | 20 : nacl_process_(nacl_process.Pass()), |
| 22 startup_info_(startup_info), | 21 startup_info_(startup_info), |
| 23 message_loop_(message_loop), | 22 task_runner_(task_runner), |
| 24 on_connected_(on_connected) {} | 23 on_connected_(on_connected) {} |
| 25 | 24 |
| 26 void ThreadMain() override { | 25 void ThreadMain() override { |
| 27 // In the Windows API, the set of processes being debugged is | 26 // In the Windows API, the set of processes being debugged is |
| 28 // thread-local, so we have to attach to the process (using | 27 // thread-local, so we have to attach to the process (using |
| 29 // DebugActiveProcess()) on the same thread on which | 28 // DebugActiveProcess()) on the same thread on which |
| 30 // NaClDebugExceptionHandlerRun() receives debug events for the | 29 // NaClDebugExceptionHandlerRun() receives debug events for the |
| 31 // process. | 30 // process. |
| 32 bool attached = false; | 31 bool attached = false; |
| 33 int pid = nacl_process_.Pid(); | 32 int pid = nacl_process_.Pid(); |
| 34 if (nacl_process_.IsValid()) { | 33 if (nacl_process_.IsValid()) { |
| 35 DCHECK(pid); | 34 DCHECK(pid); |
| 36 if (!DebugActiveProcess(pid)) { | 35 if (!DebugActiveProcess(pid)) { |
| 37 LOG(ERROR) << "Failed to connect to the process"; | 36 LOG(ERROR) << "Failed to connect to the process"; |
| 38 } else { | 37 } else { |
| 39 attached = true; | 38 attached = true; |
| 40 } | 39 } |
| 41 } else { | 40 } else { |
| 42 LOG(ERROR) << "Invalid process handle"; | 41 LOG(ERROR) << "Invalid process handle"; |
| 43 } | 42 } |
| 44 message_loop_->PostTask(FROM_HERE, base::Bind(on_connected_, attached)); | 43 task_runner_->PostTask(FROM_HERE, base::Bind(on_connected_, attached)); |
| 45 | 44 |
| 46 if (attached) { | 45 if (attached) { |
| 47 NaClDebugExceptionHandlerRun( | 46 NaClDebugExceptionHandlerRun( |
| 48 nacl_process_.Handle(), | 47 nacl_process_.Handle(), |
| 49 reinterpret_cast<const void*>(startup_info_.data()), | 48 reinterpret_cast<const void*>(startup_info_.data()), |
| 50 startup_info_.size()); | 49 startup_info_.size()); |
| 51 } | 50 } |
| 52 delete this; | 51 delete this; |
| 53 } | 52 } |
| 54 | 53 |
| 55 private: | 54 private: |
| 56 base::Process nacl_process_; | 55 base::Process nacl_process_; |
| 57 std::string startup_info_; | 56 std::string startup_info_; |
| 58 const scoped_refptr<base::MessageLoopProxy> message_loop_; | 57 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 59 base::Callback<void(bool)> on_connected_; | 58 base::Callback<void(bool)> on_connected_; |
| 60 | 59 |
| 61 DISALLOW_COPY_AND_ASSIGN(DebugExceptionHandler); | 60 DISALLOW_COPY_AND_ASSIGN(DebugExceptionHandler); |
| 62 }; | 61 }; |
| 63 | 62 |
| 64 } // namespace | 63 } // namespace |
| 65 | 64 |
| 66 void NaClStartDebugExceptionHandlerThread( | 65 void NaClStartDebugExceptionHandlerThread( |
| 67 base::Process nacl_process, | 66 base::Process nacl_process, |
| 68 const std::string& startup_info, | 67 const std::string& startup_info, |
| 69 const scoped_refptr<base::MessageLoopProxy>& message_loop, | 68 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 70 const base::Callback<void(bool)>& on_connected) { | 69 const base::Callback<void(bool)>& on_connected) { |
| 71 // The new PlatformThread will take ownership of the | 70 // The new PlatformThread will take ownership of the |
| 72 // DebugExceptionHandler object, which will delete itself on exit. | 71 // DebugExceptionHandler object, which will delete itself on exit. |
| 73 DebugExceptionHandler* handler = new DebugExceptionHandler( | 72 DebugExceptionHandler* handler = new DebugExceptionHandler( |
| 74 nacl_process.Pass(), startup_info, message_loop, on_connected); | 73 nacl_process.Pass(), startup_info, task_runner, on_connected); |
| 75 if (!base::PlatformThread::CreateNonJoinable(0, handler)) { | 74 if (!base::PlatformThread::CreateNonJoinable(0, handler)) { |
| 76 on_connected.Run(false); | 75 on_connected.Run(false); |
| 77 delete handler; | 76 delete handler; |
| 78 } | 77 } |
| 79 } | 78 } |
| OLD | NEW |