Files
ark_js_runtime/ecmascript/tooling/interface/js_debugger.cpp
T
wengchangcheng e0b5422f50 enable run ark unittest and use dynamic link
Signed-off-by: wengchangcheng <wengchangcheng@huawei.com>
Change-Id: I6e8e5165b5a4087c6fba45ed4eb11d5c1a5e4000
2021-09-28 10:30:21 +08:00

159 lines
5.8 KiB
C++

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/tooling/interface/js_debugger.h"
#include "ecmascript/class_linker/program_object-inl.h"
#include "ecmascript/js_thread.h"
namespace panda::tooling::ecmascript {
using panda::ecmascript::Program;
std::optional<Error> JSDebugger::SetBreakpoint(const PtLocation &location)
{
JSMethod *method = FindMethod(location);
if (method == nullptr) {
return Error(Error::Type::METHOD_NOT_FOUND,
std::string("Cannot find JSMethod with id ") + std::to_string(location.GetMethodId().GetOffset()) +
" in panda file '" + std::string(location.GetPandaFile()) + "'");
}
if (location.GetBytecodeOffset() >= method->GetCodeSize()) {
return Error(Error::Type::INVALID_BREAKPOINT, std::string("Invalid breakpoint location: bytecode offset (") +
std::to_string(location.GetBytecodeOffset()) +
") >= JSMethod code size (" +
std::to_string(method->GetCodeSize()) + ")");
}
if (!breakpoints_.emplace(method, location.GetBytecodeOffset()).second) {
return Error(Error::Type::BREAKPOINT_ALREADY_EXISTS,
std::string("Breakpoint already exists: bytecode offset ") +
std::to_string(location.GetBytecodeOffset()));
}
return {};
}
std::optional<Error> JSDebugger::RemoveBreakpoint(const PtLocation &location)
{
JSMethod *method = FindMethod(location);
if (method == nullptr) {
return Error(Error::Type::METHOD_NOT_FOUND,
std::string("Cannot find JSMethod with id ") + std::to_string(location.GetMethodId().GetOffset()) +
" in panda file '" + std::string(location.GetPandaFile()) + "'");
}
if (!RemoveBreakpoint(method, location.GetBytecodeOffset())) {
return Error(Error::Type::BREAKPOINT_NOT_FOUND, "Breakpoint not found");
}
return {};
}
void JSDebugger::BytecodePcChanged(ManagedThread *thread, Method *method, uint32_t bcOffset)
{
ASSERT(bcOffset < method->GetCodeSize() && "code size of current JSMethod less then bcOffset");
HandleExceptionThrowEvent(JSThread::Cast(thread), JSMethod::Cast(method), bcOffset);
// Step event is reported before breakpoint, according to the spec.
HandleStep(JSThread::Cast(thread), JSMethod::Cast(method), bcOffset);
HandleBreakpoint(JSThread::Cast(thread), JSMethod::Cast(method), bcOffset);
}
bool JSDebugger::HandleBreakpoint([[maybe_unused]] const JSThread *thread, const JSMethod *method,
uint32_t bcOffset)
{
if (!FindBreakpoint(method, bcOffset)) {
return false;
}
auto *pf = method->GetPandaFile();
PtLocation location {pf->GetFilename().c_str(), method->GetFileId(), bcOffset};
if (hooks_ != nullptr) {
hooks_->Breakpoint(PtThread(ManagedThread::NON_INITIALIZED_THREAD_ID), location);
}
return true;
}
void JSDebugger::HandleExceptionThrowEvent(const JSThread *thread, const JSMethod *method, uint32_t bcOffset)
{
if (!thread->HasPendingException()) {
return;
}
auto *pf = method->GetPandaFile();
PtLocation throwLocation {pf->GetFilename().c_str(), method->GetFileId(), bcOffset};
if (hooks_ != nullptr) {
hooks_->Exception(PtThread(ManagedThread::NON_INITIALIZED_THREAD_ID), throwLocation, PtObject(), throwLocation);
}
}
bool JSDebugger::HandleStep(const JSThread *thread, const JSMethod *method, uint32_t bcOffset)
{
auto *pf = method->GetPandaFile();
PtLocation location {pf->GetFilename().c_str(), method->GetFileId(), bcOffset};
if (hooks_ != nullptr) {
hooks_->SingleStep(PtThread(thread->GetId()), location);
}
return true;
}
bool JSDebugger::FindBreakpoint(const JSMethod *method, uint32_t bcOffset) const
{
for (const auto &bp : breakpoints_) {
if (bp.GetBytecodeOffset() == bcOffset && bp.GetMethod()->GetPandaFile() == method->GetPandaFile() &&
bp.GetMethod()->GetFileId() == method->GetFileId()) {
return true;
}
}
return false;
}
bool JSDebugger::RemoveBreakpoint(const JSMethod *method, uint32_t bcOffset)
{
for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) {
const auto &bp = *it;
if (bp.GetBytecodeOffset() == bcOffset && bp.GetMethod() == method) {
it = breakpoints_.erase(it);
return true;
}
}
return false;
}
JSMethod *JSDebugger::FindMethod(const PtLocation &location) const
{
JSMethod *method = nullptr;
ecmaVm_->EnumeratePandaFiles([&method, location](const Program *pg, const panda_file::File *pf) {
if (std::string(location.GetPandaFile()) == pf->GetFilename()) {
JSMethod *methodsData = pg->GetMethodsData();
uint32_t numberMethods = pg->GetNumberMethods();
for (uint32_t i = 0; i < numberMethods; ++i) {
if (methodsData[i].GetFileId() == location.GetMethodId()) {
method = methodsData + i;
return false;
}
}
}
return true;
});
return method;
}
} // panda::tooling::ecmascript