• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 "default_debugger_agent.h"
17 
18 namespace ark {
DefaultDebuggerAgent(os::memory::Mutex & mutex)19 DefaultDebuggerAgent::DefaultDebuggerAgent(os::memory::Mutex &mutex)
20     : LibraryAgent(mutex, PandaString(Runtime::GetOptions().GetDebuggerLibraryPath()), "StartDebugger", "StopDebugger")
21 {
22 }
23 
Load()24 bool DefaultDebuggerAgent::Load()
25 {
26     debugSession_ = Runtime::GetCurrent()->StartDebugSession();
27     if (!debugSession_) {
28         LOG(ERROR, RUNTIME) << "Could not start debug session";
29         return false;
30     }
31 
32     if (!LibraryAgent::Load()) {
33         debugSession_.reset();
34         return false;
35     }
36 
37     return true;
38 }
39 
Unload()40 bool DefaultDebuggerAgent::Unload()
41 {
42     auto result = LibraryAgent::Unload();
43     debugSession_.reset();
44     return result;
45 }
46 
CallLoadCallback(void * resolvedFunction)47 bool DefaultDebuggerAgent::CallLoadCallback(void *resolvedFunction)
48 {
49     ASSERT(resolvedFunction);
50     ASSERT(debugSession_);
51 
52     using StartDebuggerT = int (*)(uint32_t, bool);
53     uint32_t port = Runtime::GetOptions().GetDebuggerPort();
54     bool breakOnStart = Runtime::GetOptions().IsDebuggerBreakOnStart();
55     int res = reinterpret_cast<StartDebuggerT>(resolvedFunction)(port, breakOnStart);
56     if (res != 0) {
57         LOG(ERROR, RUNTIME) << "'StartDebugger' has failed with " << res;
58         return false;
59     }
60 
61     return true;
62 }
63 
CallUnloadCallback(void * resolvedFunction)64 bool DefaultDebuggerAgent::CallUnloadCallback(void *resolvedFunction)
65 {
66     ASSERT(resolvedFunction);
67 
68     using StopDebugger = int (*)();
69     int res = reinterpret_cast<StopDebugger>(resolvedFunction)();
70     if (res != 0) {
71         LOG(ERROR, RUNTIME) << "'StopDebugger' has failed with " << res;
72         return false;
73     }
74 
75     return true;
76 }
77 }  // namespace ark
78