1 /**
2 * Copyright (c) 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 "asio_server.h"
17 #include "inspector.h"
18
19 #include "utils/logger.h"
20
21 #include <cstdint>
22 #include <optional>
23
24 namespace panda::tooling {
25 class DebugInterface;
26 } // namespace panda::tooling
27
28 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
29 static panda::tooling::inspector::AsioServer g_server;
30
31 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
32 static std::optional<panda::tooling::inspector::Inspector> g_inspector;
33
StartDebugger(uint32_t port,panda::tooling::DebugInterface * debugger,void *)34 extern "C" int StartDebugger(uint32_t port, panda::tooling::DebugInterface *debugger, void * /* unused */)
35 {
36 if (g_inspector) {
37 LOG(ERROR, DEBUGGER) << "Debugger has already been started";
38 return 1;
39 }
40
41 if (!g_server.Start(port)) {
42 return 1;
43 }
44
45 g_inspector.emplace(g_server, debugger);
46 return 0;
47 }
48
StopDebugger()49 extern "C" int StopDebugger()
50 {
51 if (!g_inspector) {
52 LOG(ERROR, DEBUGGER) << "Debugger has not been started";
53 return 1;
54 }
55
56 g_inspector.reset();
57 return static_cast<int>(!g_server.Stop());
58 }
59