OLD | NEW |
| (Empty) |
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 | |
5 #include "chrome/common/nacl_debug_exception_handler_win.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/process_util.h" | |
9 #include "base/threading/platform_thread.h" | |
10 #include "base/win/scoped_handle.h" | |
11 #include "native_client/src/trusted/service_runtime/win/debug_exception_handler.
h" | |
12 | |
13 namespace { | |
14 | |
15 class DebugExceptionHandler : public base::PlatformThread::Delegate { | |
16 public: | |
17 DebugExceptionHandler(base::ProcessHandle nacl_process, | |
18 const std::string& startup_info, | |
19 base::MessageLoopProxy* message_loop, | |
20 const base::Callback<void(bool)>& on_connected) | |
21 : nacl_process_(nacl_process), | |
22 startup_info_(startup_info), | |
23 message_loop_(message_loop), | |
24 on_connected_(on_connected) { | |
25 } | |
26 | |
27 virtual void ThreadMain() OVERRIDE { | |
28 // In the Windows API, the set of processes being debugged is | |
29 // thread-local, so we have to attach to the process (using | |
30 // DebugActiveProcess()) on the same thread on which | |
31 // NaClDebugExceptionHandlerRun() receives debug events for the | |
32 // process. | |
33 bool attached = false; | |
34 int pid = GetProcessId(nacl_process_); | |
35 if (pid == 0) { | |
36 LOG(ERROR) << "Invalid process handle"; | |
37 } else { | |
38 if (!DebugActiveProcess(pid)) { | |
39 LOG(ERROR) << "Failed to connect to the process"; | |
40 } else { | |
41 attached = true; | |
42 } | |
43 } | |
44 message_loop_->PostTask(FROM_HERE, base::Bind(on_connected_, attached)); | |
45 | |
46 if (attached) { | |
47 NaClDebugExceptionHandlerRun( | |
48 nacl_process_, | |
49 reinterpret_cast<const void*>(startup_info_.data()), | |
50 startup_info_.size()); | |
51 } | |
52 delete this; | |
53 } | |
54 | |
55 private: | |
56 base::win::ScopedHandle nacl_process_; | |
57 std::string startup_info_; | |
58 base::MessageLoopProxy* message_loop_; | |
59 base::Callback<void(bool)> on_connected_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(DebugExceptionHandler); | |
62 }; | |
63 | |
64 } // namespace | |
65 | |
66 void NaClStartDebugExceptionHandlerThread( | |
67 base::ProcessHandle nacl_process, | |
68 const std::string& startup_info, | |
69 base::MessageLoopProxy* message_loop, | |
70 const base::Callback<void(bool)>& on_connected) { | |
71 // The new PlatformThread will take ownership of the | |
72 // DebugExceptionHandler object, which will delete itself on exit. | |
73 DebugExceptionHandler* handler = new DebugExceptionHandler( | |
74 nacl_process, startup_info, message_loop, on_connected); | |
75 if (!base::PlatformThread::CreateNonJoinable(0, handler)) { | |
76 on_connected.Run(false); | |
77 delete handler; | |
78 } | |
79 } | |
OLD | NEW |