• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstdlib>
17 
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/runtime_options.h"
20 #include "runtime/include/runtime_notification.h"
21 #include "runtime/include/panda_vm.h"
22 #include "runtime/include/thread_scopes.h"
23 #include "runtime/mem/gc/reference-processor/reference_processor.h"
24 
25 #include "libpandafile/file.h"
26 
27 namespace panda {
28 namespace {
29 class DefaultDebuggerAgent : public LibraryAgent, public LibraryAgentLoader<DefaultDebuggerAgent, false> {
30 public:
DefaultDebuggerAgent(os::memory::Mutex & mutex)31     explicit DefaultDebuggerAgent(os::memory::Mutex &mutex)
32         : LibraryAgent(mutex, PandaString(Runtime::GetOptions().GetDebuggerLibraryPath()), "StartDebugger",
33                        "StopDebugger")
34     {
35     }
36 
Load()37     bool Load() override
38     {
39         debug_session_ = Runtime::GetCurrent()->StartDebugSession();
40         if (!debug_session_) {
41             LOG(ERROR, RUNTIME) << "Could not start debug session";
42             return false;
43         }
44 
45         if (!LibraryAgent::Load()) {
46             debug_session_.reset();
47             return false;
48         }
49 
50         return true;
51     }
52 
Unload()53     bool Unload() override
54     {
55         auto result = LibraryAgent::Unload();
56         debug_session_.reset();
57         return result;
58     }
59 
60 private:
CallLoadCallback(void * resolvedFunction)61     bool CallLoadCallback(void *resolvedFunction) override
62     {
63         ASSERT(resolvedFunction);
64         ASSERT(debug_session_);
65 
66         using StartDebuggerT = int (*)(uint32_t, tooling::DebugInterface *, void *);
67         uint32_t port = Runtime::GetOptions().GetDebuggerPort();
68         int res = reinterpret_cast<StartDebuggerT>(resolvedFunction)(port, &debug_session_->GetDebugger(), nullptr);
69         if (res != 0) {
70             LOG(ERROR, RUNTIME) << "'StartDebugger' has failed with " << res;
71             return false;
72         }
73 
74         return true;
75     }
76 
CallUnloadCallback(void * resolvedFunction)77     bool CallUnloadCallback(void *resolvedFunction) override
78     {
79         ASSERT(resolvedFunction);
80 
81         using StopDebugger = int (*)();
82         int res = reinterpret_cast<StopDebugger>(resolvedFunction)();
83         if (res != 0) {
84             LOG(ERROR, RUNTIME) << "'StopDebugger' has failed with " << res;
85             return false;
86         }
87 
88         return true;
89     }
90 
91     Runtime::DebugSessionHandle debug_session_;
92 };
93 }  // namespace
94 
95 /* static */
Create(Runtime * runtime,const RuntimeOptions & options,std::string_view runtime_type)96 PandaVM *PandaVM::Create(Runtime *runtime, const RuntimeOptions &options, std::string_view runtime_type)
97 {
98     LanguageContext ctx = runtime->GetLanguageContext(std::string(runtime_type));
99     return ctx.CreateVM(runtime, options);
100 }
101 
InvokeEntrypoint(Method * entrypoint,const std::vector<std::string> & args)102 Expected<int, Runtime::Error> PandaVM::InvokeEntrypoint(Method *entrypoint, const std::vector<std::string> &args)
103 {
104     if (!CheckEntrypointSignature(entrypoint)) {
105         LOG(ERROR, RUNTIME) << "Method '" << entrypoint << "' has invalid signature";
106         return Unexpected(Runtime::Error::INVALID_ENTRY_POINT);
107     }
108     Expected<int, Runtime::Error> ret = InvokeEntrypointImpl(entrypoint, args);
109     ManagedThread *thread = ManagedThread::GetCurrent();
110     ASSERT(thread != nullptr);
111     bool has_exception = false;
112     {
113         ScopedManagedCodeThread s(thread);
114         has_exception = thread->HasPendingException();
115     }
116     if (has_exception) {
117         HandleUncaughtException();
118         ret = EXIT_FAILURE;
119     }
120 
121     return ret;
122 }
123 
HandleLdaStr(Frame * frame,BytecodeId string_id)124 void PandaVM::HandleLdaStr(Frame *frame, BytecodeId string_id)
125 {
126     coretypes::String *str =
127         panda::Runtime::GetCurrent()->ResolveString(this, *frame->GetMethod(), string_id.AsFileId());
128     frame->GetAccAsVReg().SetReference(str);
129 }
130 
OpenPandaFile(std::string_view location)131 std::unique_ptr<const panda_file::File> PandaVM::OpenPandaFile(std::string_view location)
132 {
133     return panda_file::OpenPandaFile(location);
134 }
135 
GetNonMovableString(const panda_file::File & pf,panda_file::File::EntityId id) const136 coretypes::String *PandaVM::GetNonMovableString(const panda_file::File &pf, panda_file::File::EntityId id) const
137 {
138     auto cached_string = GetStringTable()->GetInternalStringFast(pf, id);
139     if (cached_string == nullptr) {
140         return nullptr;
141     }
142 
143     if (!GetHeapManager()->GetObjectAllocator().AsObjectAllocator()->IsObjectInNonMovableSpace(cached_string)) {
144         return nullptr;
145     }
146 
147     return cached_string;
148 }
149 
ShouldEnableDebug()150 bool PandaVM::ShouldEnableDebug()
151 {
152     return !Runtime::GetOptions().GetDebuggerLibraryPath().empty() || Runtime::GetOptions().IsDebuggerEnable();
153 }
154 
CreateDebuggerAgent()155 LoadableAgentHandle PandaVM::CreateDebuggerAgent()
156 {
157     if (!Runtime::GetOptions().GetDebuggerLibraryPath().empty()) {
158         return DefaultDebuggerAgent::LoadInstance();
159     }
160 
161     return {};
162 }
163 }  // namespace panda
164