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