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
3 // found in the LICENSE file.
5 #include "components/nacl/common/nacl_debug_exception_handler_win.h"
8 #include "base/threading/platform_thread.h"
9 #include "base/win/scoped_handle.h"
10 #include "native_client/src/public/win/debug_exception_handler.h"
14 class DebugExceptionHandler
: public base::PlatformThread::Delegate
{
16 DebugExceptionHandler(base::Process nacl_process
,
17 const std::string
& startup_info
,
18 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
19 const base::Callback
<void(bool)>& on_connected
)
20 : nacl_process_(nacl_process
.Pass()),
21 startup_info_(startup_info
),
22 task_runner_(task_runner
),
23 on_connected_(on_connected
) {}
25 void ThreadMain() override
{
26 // In the Windows API, the set of processes being debugged is
27 // thread-local, so we have to attach to the process (using
28 // DebugActiveProcess()) on the same thread on which
29 // NaClDebugExceptionHandlerRun() receives debug events for the
31 bool attached
= false;
32 int pid
= nacl_process_
.Pid();
33 if (nacl_process_
.IsValid()) {
35 if (!DebugActiveProcess(pid
)) {
36 LOG(ERROR
) << "Failed to connect to the process";
41 LOG(ERROR
) << "Invalid process handle";
43 task_runner_
->PostTask(FROM_HERE
, base::Bind(on_connected_
, attached
));
46 NaClDebugExceptionHandlerRun(
47 nacl_process_
.Handle(),
48 reinterpret_cast<const void*>(startup_info_
.data()),
49 startup_info_
.size());
55 base::Process nacl_process_
;
56 std::string startup_info_
;
57 const scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
58 base::Callback
<void(bool)> on_connected_
;
60 DISALLOW_COPY_AND_ASSIGN(DebugExceptionHandler
);
65 void NaClStartDebugExceptionHandlerThread(
66 base::Process nacl_process
,
67 const std::string
& startup_info
,
68 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
69 const base::Callback
<void(bool)>& on_connected
) {
70 // The new PlatformThread will take ownership of the
71 // DebugExceptionHandler object, which will delete itself on exit.
72 DebugExceptionHandler
* handler
= new DebugExceptionHandler(
73 nacl_process
.Pass(), startup_info
, task_runner
, on_connected
);
74 if (!base::PlatformThread::CreateNonJoinable(0, handler
)) {
75 on_connected
.Run(false);