1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#include "backend/js_pt_hooks.h"
17e509ee18Sopenharmony_ci
18e509ee18Sopenharmony_ci#include "agent/debugger_impl.h"
19e509ee18Sopenharmony_ci
20e509ee18Sopenharmony_cinamespace panda::ecmascript::tooling {
21e509ee18Sopenharmony_civoid JSPtHooks::DebuggerStmt([[maybe_unused]] const JSPtLocation &location)
22e509ee18Sopenharmony_ci{
23e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPHooks: Debugger Statement";
24e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
25e509ee18Sopenharmony_ci    debugger_->NotifyPaused({}, DEBUGGERSTMT);
26e509ee18Sopenharmony_ci}
27e509ee18Sopenharmony_ci
28e509ee18Sopenharmony_civoid JSPtHooks::Breakpoint(const JSPtLocation &location)
29e509ee18Sopenharmony_ci{
30e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPtHooks: Breakpoint => " << location.GetMethodId() << ": "
31e509ee18Sopenharmony_ci                         << location.GetBytecodeOffset();
32e509ee18Sopenharmony_ci
33e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
34e509ee18Sopenharmony_ci    debugger_->NotifyPaused(location, OTHER);
35e509ee18Sopenharmony_ci}
36e509ee18Sopenharmony_ci
37e509ee18Sopenharmony_civoid JSPtHooks::Exception([[maybe_unused]] const JSPtLocation &location)
38e509ee18Sopenharmony_ci{
39e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPtHooks: Exception";
40e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
41e509ee18Sopenharmony_ci
42e509ee18Sopenharmony_ci    debugger_->NotifyPaused({}, EXCEPTION);
43e509ee18Sopenharmony_ci}
44e509ee18Sopenharmony_ci
45e509ee18Sopenharmony_cibool JSPtHooks::SingleStep(const JSPtLocation &location)
46e509ee18Sopenharmony_ci{
47e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPtHooks: SingleStep => " << location.GetBytecodeOffset();
48e509ee18Sopenharmony_ci
49e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
50e509ee18Sopenharmony_ci    if (UNLIKELY(firstTime_)) {
51e509ee18Sopenharmony_ci        firstTime_ = false;
52e509ee18Sopenharmony_ci
53e509ee18Sopenharmony_ci        debugger_->NotifyPaused({}, BREAK_ON_START);
54e509ee18Sopenharmony_ci        return false;
55e509ee18Sopenharmony_ci    }
56e509ee18Sopenharmony_ci
57e509ee18Sopenharmony_ci    // pause or step complete
58e509ee18Sopenharmony_ci    if (debugger_->NotifySingleStep(location)) {
59e509ee18Sopenharmony_ci        debugger_->NotifyPaused({}, OTHER);
60e509ee18Sopenharmony_ci        return true;
61e509ee18Sopenharmony_ci    }
62e509ee18Sopenharmony_ci
63e509ee18Sopenharmony_ci    // temporary "safepoint" to handle possible protocol command
64e509ee18Sopenharmony_ci    debugger_->NotifyHandleProtocolCommand();
65e509ee18Sopenharmony_ci
66e509ee18Sopenharmony_ci    return false;
67e509ee18Sopenharmony_ci}
68e509ee18Sopenharmony_ci
69e509ee18Sopenharmony_cibool JSPtHooks::NativeOut()
70e509ee18Sopenharmony_ci{
71e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
72e509ee18Sopenharmony_ci    if (debugger_->NotifyNativeOut()) {
73e509ee18Sopenharmony_ci        debugger_->NotifyPaused({}, NATIVE_OUT);
74e509ee18Sopenharmony_ci        return true;
75e509ee18Sopenharmony_ci    }
76e509ee18Sopenharmony_ci
77e509ee18Sopenharmony_ci    return false;
78e509ee18Sopenharmony_ci}
79e509ee18Sopenharmony_ci
80e509ee18Sopenharmony_civoid JSPtHooks::LoadModule(std::string_view pandaFileName, std::string_view entryPoint)
81e509ee18Sopenharmony_ci{
82e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPtHooks: LoadModule: " << pandaFileName;
83e509ee18Sopenharmony_ci
84e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
85e509ee18Sopenharmony_ci
86e509ee18Sopenharmony_ci    if (debugger_->NotifyScriptParsed(pandaFileName.data(), entryPoint)) {
87e509ee18Sopenharmony_ci        firstTime_ = true;
88e509ee18Sopenharmony_ci    }
89e509ee18Sopenharmony_ci}
90e509ee18Sopenharmony_ci
91e509ee18Sopenharmony_civoid JSPtHooks::NativeCalling(const void *nativeAddress)
92e509ee18Sopenharmony_ci{
93e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPtHooks: NativeCalling, addr = " << nativeAddress;
94e509ee18Sopenharmony_ci
95e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
96e509ee18Sopenharmony_ci
97e509ee18Sopenharmony_ci    debugger_->NotifyNativeCalling(nativeAddress);
98e509ee18Sopenharmony_ci}
99e509ee18Sopenharmony_ci
100e509ee18Sopenharmony_civoid JSPtHooks::NativeReturn(const void *nativeAddress)
101e509ee18Sopenharmony_ci{
102e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
103e509ee18Sopenharmony_ci
104e509ee18Sopenharmony_ci    debugger_->NotifyNativeReturn(nativeAddress);
105e509ee18Sopenharmony_ci}
106e509ee18Sopenharmony_ci
107e509ee18Sopenharmony_civoid JSPtHooks::SendableMethodEntry(JSHandle<Method> method)
108e509ee18Sopenharmony_ci{
109e509ee18Sopenharmony_ci    LOG_DEBUGGER(VERBOSE) << "JSPtHooks: MethodEntry";
110e509ee18Sopenharmony_ci
111e509ee18Sopenharmony_ci    [[maybe_unused]] LocalScope scope(debugger_->vm_);
112e509ee18Sopenharmony_ci
113e509ee18Sopenharmony_ci    if (debugger_->SendableMethodEntry(method)) {
114e509ee18Sopenharmony_ci        firstTime_ = true;
115e509ee18Sopenharmony_ci    };
116e509ee18Sopenharmony_ci}
117e509ee18Sopenharmony_ci}  // namespace panda::ecmascript::tooling
118