106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include <thread>
1606f6ba60Sopenharmony_ci#include <sys/file.h>
1706f6ba60Sopenharmony_ci#include "common.h"
1806f6ba60Sopenharmony_ci#include "command_poller.h"
1906f6ba60Sopenharmony_ci#include "hook_manager.h"
2006f6ba60Sopenharmony_ci#include "logging.h"
2106f6ba60Sopenharmony_ci#include "plugin_service_types.pb.h"
2206f6ba60Sopenharmony_ci#include "writer_adapter.h"
2306f6ba60Sopenharmony_ci#include "hook_standalone.h"
2406f6ba60Sopenharmony_ci#include "hook_common.h"
2506f6ba60Sopenharmony_ci#include "native_memory_profiler_sa_service.h"
2606f6ba60Sopenharmony_ci
2706f6ba60Sopenharmony_ciusing namespace OHOS::Developtools::NativeDaemon;
2806f6ba60Sopenharmony_ci
2906f6ba60Sopenharmony_cinamespace {
3006f6ba60Sopenharmony_ciconst int SLEEP_ONE_SECOND = 1000;
3106f6ba60Sopenharmony_ciconst int VC_ARG_TWAIN = 2;
3206f6ba60Sopenharmony_ciconst int VC_ARG_STEP_SIZE = 2;
3306f6ba60Sopenharmony_ciconst int SMBSIZE_BASE = 4096;
3406f6ba60Sopenharmony_ci
3506f6ba60Sopenharmony_cibool ProcessExist(const std::string pid)
3606f6ba60Sopenharmony_ci{
3706f6ba60Sopenharmony_ci    std::string pid_path = "";
3806f6ba60Sopenharmony_ci    struct stat stat_buf;
3906f6ba60Sopenharmony_ci    if (pid.size() == 0) {
4006f6ba60Sopenharmony_ci        return false;
4106f6ba60Sopenharmony_ci    }
4206f6ba60Sopenharmony_ci    pid_path = "/proc/" + pid + "/status";
4306f6ba60Sopenharmony_ci    if (stat(pid_path.c_str(), &stat_buf) != 0) {
4406f6ba60Sopenharmony_ci        return false;
4506f6ba60Sopenharmony_ci    }
4606f6ba60Sopenharmony_ci    return true;
4706f6ba60Sopenharmony_ci}
4806f6ba60Sopenharmony_ci
4906f6ba60Sopenharmony_cibool ParseCommand(const std::vector<std::string>& args, HookData& hookData)
5006f6ba60Sopenharmony_ci{
5106f6ba60Sopenharmony_ci    size_t idx = 0;
5206f6ba60Sopenharmony_ci    while (idx < args.size()) {
5306f6ba60Sopenharmony_ci        if (args[idx] == "-o") {
5406f6ba60Sopenharmony_ci            hookData.fileName = args[idx + 1].c_str();
5506f6ba60Sopenharmony_ci        } else if (args[idx] == "-p") {
5606f6ba60Sopenharmony_ci            std::vector<std::string> pids = StringSplit(args[idx + 1], ",");
5706f6ba60Sopenharmony_ci            hookData.pids.insert(pids.begin(), pids.end());
5806f6ba60Sopenharmony_ci            for (auto iter = hookData.pids.begin(); iter != hookData.pids.end();) {
5906f6ba60Sopenharmony_ci                if (!ProcessExist(*iter)) {
6006f6ba60Sopenharmony_ci                    iter = hookData.pids.erase(iter);
6106f6ba60Sopenharmony_ci                    printf("process does not exist %s\n", iter->c_str());
6206f6ba60Sopenharmony_ci                } else {
6306f6ba60Sopenharmony_ci                    ++iter;
6406f6ba60Sopenharmony_ci                }
6506f6ba60Sopenharmony_ci            }
6606f6ba60Sopenharmony_ci            if (hookData.pids.empty()) {
6706f6ba60Sopenharmony_ci                printf("all process does not exist\n");
6806f6ba60Sopenharmony_ci                return false;
6906f6ba60Sopenharmony_ci            }
7006f6ba60Sopenharmony_ci        } else if (args[idx] == "-n") {
7106f6ba60Sopenharmony_ci            hookData.processName = args[idx + 1];
7206f6ba60Sopenharmony_ci        } else if (args[idx] == "-s") {
7306f6ba60Sopenharmony_ci            hookData.smbSize = static_cast<uint32_t>(IsDigits(args[idx + 1]) ? std::stoi(args[idx + 1]) : 0);
7406f6ba60Sopenharmony_ci            if (std::to_string(hookData.smbSize) != args[idx + 1]) {
7506f6ba60Sopenharmony_ci                return false;
7606f6ba60Sopenharmony_ci            }
7706f6ba60Sopenharmony_ci        } else if (args[idx] == "-f") {
7806f6ba60Sopenharmony_ci            hookData.filterSize = static_cast<uint32_t>(IsDigits(args[idx + 1]) ? std::stoi(args[idx + 1]) : 0);
7906f6ba60Sopenharmony_ci            if (std::to_string(hookData.filterSize) != args[idx + 1]) {
8006f6ba60Sopenharmony_ci                return false;
8106f6ba60Sopenharmony_ci            }
8206f6ba60Sopenharmony_ci            if (hookData.filterSize > MAX_UNWIND_DEPTH) {
8306f6ba60Sopenharmony_ci                printf("set max depth = %d\n", MAX_UNWIND_DEPTH);
8406f6ba60Sopenharmony_ci            }
8506f6ba60Sopenharmony_ci        } else if (args[idx] == "-d") {
8606f6ba60Sopenharmony_ci            hookData.maxStackDepth = static_cast<uint32_t>(IsDigits(args[idx + 1]) ? std::stoi(args[idx + 1]) : 0);
8706f6ba60Sopenharmony_ci            if (std::to_string(hookData.maxStackDepth) != args[idx + 1]) {
8806f6ba60Sopenharmony_ci                return false;
8906f6ba60Sopenharmony_ci            }
9006f6ba60Sopenharmony_ci        } else if (args[idx] == "-L") {
9106f6ba60Sopenharmony_ci            if (idx + 1 < args.size()) {
9206f6ba60Sopenharmony_ci                hookData.duration = std::stoull(args[idx + 1]);
9306f6ba60Sopenharmony_ci            }
9406f6ba60Sopenharmony_ci        } else if (args[idx] == "-F") {
9506f6ba60Sopenharmony_ci            if (idx + 1 < args.size()) {
9606f6ba60Sopenharmony_ci                hookData.performanceFilename = args[idx + 1];
9706f6ba60Sopenharmony_ci            }
9806f6ba60Sopenharmony_ci        } else if (args[idx] == "-u") {
9906f6ba60Sopenharmony_ci            std::string unwind = args[idx + 1];
10006f6ba60Sopenharmony_ci            if (unwind == "dwarf") {
10106f6ba60Sopenharmony_ci                hookData.fpUnwind = false;
10206f6ba60Sopenharmony_ci            } else if (unwind == "fp") {
10306f6ba60Sopenharmony_ci                hookData.fpUnwind = true;
10406f6ba60Sopenharmony_ci            } else {
10506f6ba60Sopenharmony_ci                return false;
10606f6ba60Sopenharmony_ci            }
10706f6ba60Sopenharmony_ci            printf("set unwind mode:%s\n", unwind.c_str());
10806f6ba60Sopenharmony_ci        } else if (args[idx] == "-S") {
10906f6ba60Sopenharmony_ci            hookData.statisticsInterval = static_cast<uint32_t>(IsDigits(args[idx + 1]) ?
11006f6ba60Sopenharmony_ci                                                                std::stoi(args[idx + 1]) : 0);
11106f6ba60Sopenharmony_ci            if (std::to_string(hookData.statisticsInterval) != args[idx + 1]) {
11206f6ba60Sopenharmony_ci                return false;
11306f6ba60Sopenharmony_ci            }
11406f6ba60Sopenharmony_ci        } else if (args[idx] == "-i") {
11506f6ba60Sopenharmony_ci            hookData.sampleInterval = static_cast<uint32_t>(IsDigits(args[idx + 1]) ? std::stoi(args[idx + 1]) : 0);
11606f6ba60Sopenharmony_ci            if (std::to_string(hookData.sampleInterval) != args[idx + 1]) {
11706f6ba60Sopenharmony_ci                return false;
11806f6ba60Sopenharmony_ci            }
11906f6ba60Sopenharmony_ci        } else if (args[idx] == "-O") {
12006f6ba60Sopenharmony_ci            std::string offline = args[idx + 1];
12106f6ba60Sopenharmony_ci            if (offline == "false") {
12206f6ba60Sopenharmony_ci                hookData.offlineSymbolization = false;
12306f6ba60Sopenharmony_ci            } else if (offline == "true") {
12406f6ba60Sopenharmony_ci                hookData.offlineSymbolization = true;
12506f6ba60Sopenharmony_ci            } else {
12606f6ba60Sopenharmony_ci                return false;
12706f6ba60Sopenharmony_ci            }
12806f6ba60Sopenharmony_ci            printf("set offlineSymbolization mode:%s\n", offline.c_str());
12906f6ba60Sopenharmony_ci        } else if (args[idx] == "-C") {
13006f6ba60Sopenharmony_ci            std::string callframeCompress = args[idx + 1];
13106f6ba60Sopenharmony_ci            if (callframeCompress == "false") {
13206f6ba60Sopenharmony_ci                hookData.callframeCompress = false;
13306f6ba60Sopenharmony_ci            } else if (callframeCompress == "true") {
13406f6ba60Sopenharmony_ci                hookData.callframeCompress = true;
13506f6ba60Sopenharmony_ci            } else {
13606f6ba60Sopenharmony_ci                return false;
13706f6ba60Sopenharmony_ci            }
13806f6ba60Sopenharmony_ci            printf("set callframeCompress mode:%s\n", callframeCompress.c_str());
13906f6ba60Sopenharmony_ci        } else if (args[idx] == "-c") {
14006f6ba60Sopenharmony_ci            std::string stringCompressed = args[idx + 1];
14106f6ba60Sopenharmony_ci            if (stringCompressed == "false") {
14206f6ba60Sopenharmony_ci                hookData.stringCompressed = false;
14306f6ba60Sopenharmony_ci            } else if (stringCompressed == "true") {
14406f6ba60Sopenharmony_ci                hookData.stringCompressed = true;
14506f6ba60Sopenharmony_ci            } else {
14606f6ba60Sopenharmony_ci                return false;
14706f6ba60Sopenharmony_ci            }
14806f6ba60Sopenharmony_ci            printf("set stringCompressed mode:%s\n", stringCompressed.c_str());
14906f6ba60Sopenharmony_ci        } else if (args[idx] == "-r") {
15006f6ba60Sopenharmony_ci            std::string rawString = args[idx + 1];
15106f6ba60Sopenharmony_ci            if (rawString == "false") {
15206f6ba60Sopenharmony_ci                hookData.rawString = false;
15306f6ba60Sopenharmony_ci            } else if (rawString == "true") {
15406f6ba60Sopenharmony_ci                hookData.rawString = true;
15506f6ba60Sopenharmony_ci            } else {
15606f6ba60Sopenharmony_ci                return false;
15706f6ba60Sopenharmony_ci            }
15806f6ba60Sopenharmony_ci            printf("set rawString mode:%s\n", rawString.c_str());
15906f6ba60Sopenharmony_ci        } else if (args[idx] == "-so") {
16006f6ba60Sopenharmony_ci            std::string rawString = args[idx + 1];
16106f6ba60Sopenharmony_ci            if (rawString == "false") {
16206f6ba60Sopenharmony_ci                hookData.responseLibraryMode = false;
16306f6ba60Sopenharmony_ci            } else if (rawString == "true") {
16406f6ba60Sopenharmony_ci                hookData.responseLibraryMode = true;
16506f6ba60Sopenharmony_ci            } else {
16606f6ba60Sopenharmony_ci                return false;
16706f6ba60Sopenharmony_ci            }
16806f6ba60Sopenharmony_ci            printf("set responseLibraryMode mode:%s\n", rawString.c_str());
16906f6ba60Sopenharmony_ci        } else if (args[idx] == "-js") {
17006f6ba60Sopenharmony_ci            hookData.jsStackReport = IsDigits(args[idx + 1]) ? std::stoi(args[idx + 1]) : 0;
17106f6ba60Sopenharmony_ci            if (std::to_string(hookData.jsStackReport) != args[idx + 1]) {
17206f6ba60Sopenharmony_ci                return false;
17306f6ba60Sopenharmony_ci            }
17406f6ba60Sopenharmony_ci        } else if (args[idx] == "-jsd") {
17506f6ba60Sopenharmony_ci            hookData.maxJsStackdepth = static_cast<uint32_t>(IsDigits(args[idx + 1]) ? std::stoi(args[idx + 1]) : 0);
17606f6ba60Sopenharmony_ci            if (std::to_string(hookData.maxJsStackdepth) != args[idx + 1]) {
17706f6ba60Sopenharmony_ci                return false;
17806f6ba60Sopenharmony_ci            }
17906f6ba60Sopenharmony_ci        } else if (args[idx] == "-jn") {
18006f6ba60Sopenharmony_ci            hookData.filterNapiName = args[idx + 1];
18106f6ba60Sopenharmony_ci        } else if (args[idx] == "-mfm") {
18206f6ba60Sopenharmony_ci            hookData.mallocFreeMatchingInterval = static_cast<uint32_t>(IsDigits(args[idx + 1]) ?
18306f6ba60Sopenharmony_ci                                                                        std::stoi(args[idx + 1]) : 0);
18406f6ba60Sopenharmony_ci            if (std::to_string(hookData.mallocFreeMatchingInterval) != args[idx + 1]) {
18506f6ba60Sopenharmony_ci                return false;
18606f6ba60Sopenharmony_ci            }
18706f6ba60Sopenharmony_ci        } else {
18806f6ba60Sopenharmony_ci            printf("args[%zu] = %s\n", idx, args[idx].c_str());
18906f6ba60Sopenharmony_ci            return false;
19006f6ba60Sopenharmony_ci        }
19106f6ba60Sopenharmony_ci        idx += VC_ARG_STEP_SIZE;
19206f6ba60Sopenharmony_ci    }
19306f6ba60Sopenharmony_ci    return true;
19406f6ba60Sopenharmony_ci}
19506f6ba60Sopenharmony_ci
19606f6ba60Sopenharmony_cibool VerifyCommand(const std::vector<std::string>& args, HookData& hookData)
19706f6ba60Sopenharmony_ci{
19806f6ba60Sopenharmony_ci    if ((args.size() % VC_ARG_TWAIN) != 0) {
19906f6ba60Sopenharmony_ci        return false;
20006f6ba60Sopenharmony_ci    }
20106f6ba60Sopenharmony_ci    if (!ParseCommand(args, hookData)) {
20206f6ba60Sopenharmony_ci        return false;
20306f6ba60Sopenharmony_ci    }
20406f6ba60Sopenharmony_ci    if ((hookData.smbSize % SMBSIZE_BASE) != 0) {
20506f6ba60Sopenharmony_ci        printf("Please configure a multiple of 4096 for the shared memory size\n");
20606f6ba60Sopenharmony_ci        return false;
20706f6ba60Sopenharmony_ci    }
20806f6ba60Sopenharmony_ci    if (!hookData.fileName.empty() && (!hookData.processName.empty() || hookData.pids.size() > 0)) {
20906f6ba60Sopenharmony_ci        return true;
21006f6ba60Sopenharmony_ci    }
21106f6ba60Sopenharmony_ci    return false;
21206f6ba60Sopenharmony_ci}
21306f6ba60Sopenharmony_ci
21406f6ba60Sopenharmony_civolatile sig_atomic_t g_isRunning = true;
21506f6ba60Sopenharmony_civoid SignalSigintHandler(int sig)
21606f6ba60Sopenharmony_ci{
21706f6ba60Sopenharmony_ci    g_isRunning = false;
21806f6ba60Sopenharmony_ci}
21906f6ba60Sopenharmony_ci
22006f6ba60Sopenharmony_civoid GetHookedProceInfo(HookData& hookData)
22106f6ba60Sopenharmony_ci{
22206f6ba60Sopenharmony_ci    printf("Record file = %s, apply sharememory size = %u\n", hookData.fileName.c_str(), hookData.smbSize);
22306f6ba60Sopenharmony_ci    if (hookData.pids.size() > 0) {
22406f6ba60Sopenharmony_ci        for (const auto& pid : hookData.pids) {
22506f6ba60Sopenharmony_ci            printf("hook target process %s start\n", pid.c_str());
22606f6ba60Sopenharmony_ci        }
22706f6ba60Sopenharmony_ci    } else if (!hookData.processName.empty()) {
22806f6ba60Sopenharmony_ci        int pidValue = -1;
22906f6ba60Sopenharmony_ci        const std::string processName = hookData.processName;
23006f6ba60Sopenharmony_ci        bool isExist = COMMON::IsProcessExist(processName, pidValue);
23106f6ba60Sopenharmony_ci        if (!isExist) {
23206f6ba60Sopenharmony_ci            hookData.startupMode = true;
23306f6ba60Sopenharmony_ci            printf("startup mode ,Please start process %s\n", hookData.processName.c_str());
23406f6ba60Sopenharmony_ci        } else {
23506f6ba60Sopenharmony_ci            hookData.pids.emplace(std::to_string(pidValue));
23606f6ba60Sopenharmony_ci        }
23706f6ba60Sopenharmony_ci    }
23806f6ba60Sopenharmony_ci
23906f6ba60Sopenharmony_ci    if (hookData.maxStackDepth > 0) {
24006f6ba60Sopenharmony_ci        printf("depth greater than %u will not display\n", hookData.maxStackDepth);
24106f6ba60Sopenharmony_ci    }
24206f6ba60Sopenharmony_ci    if (hookData.filterSize > 0) {
24306f6ba60Sopenharmony_ci        printf("malloc size smaller than %u will not record\n", hookData.filterSize);
24406f6ba60Sopenharmony_ci    }
24506f6ba60Sopenharmony_ci
24606f6ba60Sopenharmony_ci    if (!OHOS::Developtools::Profiler::Hook::StartHook(hookData)) {
24706f6ba60Sopenharmony_ci        return;
24806f6ba60Sopenharmony_ci    }
24906f6ba60Sopenharmony_ci    while (g_isRunning) {
25006f6ba60Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
25106f6ba60Sopenharmony_ci    }
25206f6ba60Sopenharmony_ci    OHOS::Developtools::Profiler::Hook::EndHook();
25306f6ba60Sopenharmony_ci}
25406f6ba60Sopenharmony_ci} // namespace
25506f6ba60Sopenharmony_ci
25606f6ba60Sopenharmony_ciint main(int argc, char* argv[])
25706f6ba60Sopenharmony_ci{
25806f6ba60Sopenharmony_ci    int lockFileFd = -1;
25906f6ba60Sopenharmony_ci    if (COMMON::IsProcessRunning(lockFileFd)) { // process is running
26006f6ba60Sopenharmony_ci        return 0;
26106f6ba60Sopenharmony_ci    }
26206f6ba60Sopenharmony_ci
26306f6ba60Sopenharmony_ci    if (argc > 1) {
26406f6ba60Sopenharmony_ci        if (argc == 2 && strcmp(argv[1], "sa") == 0) { // 2: argc size
26506f6ba60Sopenharmony_ci            if (!OHOS::Developtools::NativeDaemon::NativeMemoryProfilerSaService::StartServiceAbility()) {
26606f6ba60Sopenharmony_ci                if (lockFileFd > 0) {
26706f6ba60Sopenharmony_ci                    flock(lockFileFd, LOCK_UN);
26806f6ba60Sopenharmony_ci                    close(lockFileFd);
26906f6ba60Sopenharmony_ci                }
27006f6ba60Sopenharmony_ci                return 0;
27106f6ba60Sopenharmony_ci            }
27206f6ba60Sopenharmony_ci            while (true) {
27306f6ba60Sopenharmony_ci                std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
27406f6ba60Sopenharmony_ci            }
27506f6ba60Sopenharmony_ci        } else {
27606f6ba60Sopenharmony_ci            if (!COMMON::IsBetaVersion()) {
27706f6ba60Sopenharmony_ci                printf("memory profiler only support in beta version\n");
27806f6ba60Sopenharmony_ci                if (lockFileFd > 0) {
27906f6ba60Sopenharmony_ci                    flock(lockFileFd, LOCK_UN);
28006f6ba60Sopenharmony_ci                    close(lockFileFd);
28106f6ba60Sopenharmony_ci                }
28206f6ba60Sopenharmony_ci                return 0;
28306f6ba60Sopenharmony_ci            }
28406f6ba60Sopenharmony_ci            std::vector<std::string> args;
28506f6ba60Sopenharmony_ci            for (int i = 1; i < argc; i++) {
28606f6ba60Sopenharmony_ci                args.push_back(argv[i]);
28706f6ba60Sopenharmony_ci            }
28806f6ba60Sopenharmony_ci            HookData hookData;
28906f6ba60Sopenharmony_ci            if (VerifyCommand(args, hookData)) {
29006f6ba60Sopenharmony_ci                signal(SIGINT, SignalSigintHandler);
29106f6ba60Sopenharmony_ci                GetHookedProceInfo(hookData);
29206f6ba60Sopenharmony_ci            } else {
29306f6ba60Sopenharmony_ci                std::string help = R"(Usage: native_daemon
29406f6ba60Sopenharmony_ci                [-o file]
29506f6ba60Sopenharmony_ci                [-s smb_size]
29606f6ba60Sopenharmony_ci                <-n process_name>
29706f6ba60Sopenharmony_ci                <-p pids>
29806f6ba60Sopenharmony_ci                <-f filter_size>
29906f6ba60Sopenharmony_ci                <-d max_stack_depth>
30006f6ba60Sopenharmony_ci                <-i sample_interval>
30106f6ba60Sopenharmony_ci                <-u fp|dwarf>
30206f6ba60Sopenharmony_ci                <-S statistics_interval>
30306f6ba60Sopenharmony_ci                <-O offline_symbolization true|false>
30406f6ba60Sopenharmony_ci                <-C callframe_compress true|false>
30506f6ba60Sopenharmony_ci                <-c string_compressed true|false>
30606f6ba60Sopenharmony_ci                <-r raw_string true|false>
30706f6ba60Sopenharmony_ci                <-so responseLibraryMode true|false>
30806f6ba60Sopenharmony_ci                <-js jsStackReport>
30906f6ba60Sopenharmony_ci                <-jsd maxJsStackDepth>
31006f6ba60Sopenharmony_ci                <-jn filterNapiName>
31106f6ba60Sopenharmony_ci                <-mfm mallocFreeMatchingInterval_>
31206f6ba60Sopenharmony_ci                )";
31306f6ba60Sopenharmony_ci                printf("%s\n", help.c_str());
31406f6ba60Sopenharmony_ci                if (lockFileFd > 0) {
31506f6ba60Sopenharmony_ci                    flock(lockFileFd, LOCK_UN);
31606f6ba60Sopenharmony_ci                    close(lockFileFd);
31706f6ba60Sopenharmony_ci                }
31806f6ba60Sopenharmony_ci                return 0;
31906f6ba60Sopenharmony_ci            }
32006f6ba60Sopenharmony_ci        }
32106f6ba60Sopenharmony_ci    } else {
32206f6ba60Sopenharmony_ci        auto hookManager = std::make_shared<HookManager>();
32306f6ba60Sopenharmony_ci        if (hookManager == nullptr) {
32406f6ba60Sopenharmony_ci            if (lockFileFd > 0) {
32506f6ba60Sopenharmony_ci                flock(lockFileFd, LOCK_UN);
32606f6ba60Sopenharmony_ci                close(lockFileFd);
32706f6ba60Sopenharmony_ci                PROFILER_LOG_INFO(LOG_CORE, "create PluginManager FAILED!");
32806f6ba60Sopenharmony_ci                return 1;
32906f6ba60Sopenharmony_ci            }
33006f6ba60Sopenharmony_ci            return 0;
33106f6ba60Sopenharmony_ci        }
33206f6ba60Sopenharmony_ci        auto commandPoller = std::make_shared<CommandPoller>(hookManager);
33306f6ba60Sopenharmony_ci        if (commandPoller == nullptr) {
33406f6ba60Sopenharmony_ci            if (lockFileFd > 0) {
33506f6ba60Sopenharmony_ci                flock(lockFileFd, LOCK_UN);
33606f6ba60Sopenharmony_ci                close(lockFileFd);
33706f6ba60Sopenharmony_ci                PROFILER_LOG_INFO(LOG_CORE, "create CommandPoller FAILED!");
33806f6ba60Sopenharmony_ci                return 1;
33906f6ba60Sopenharmony_ci            }
34006f6ba60Sopenharmony_ci            return 0;
34106f6ba60Sopenharmony_ci        }
34206f6ba60Sopenharmony_ci        if (!commandPoller->OnConnect()) {
34306f6ba60Sopenharmony_ci            if (lockFileFd > 0) {
34406f6ba60Sopenharmony_ci                flock(lockFileFd, LOCK_UN);
34506f6ba60Sopenharmony_ci                close(lockFileFd);
34606f6ba60Sopenharmony_ci                PROFILER_LOG_INFO(LOG_CORE, "connect FAILED");
34706f6ba60Sopenharmony_ci                return 1;
34806f6ba60Sopenharmony_ci            }
34906f6ba60Sopenharmony_ci            return 0;
35006f6ba60Sopenharmony_ci        }
35106f6ba60Sopenharmony_ci        hookManager->SetCommandPoller(commandPoller);
35206f6ba60Sopenharmony_ci        hookManager->RegisterAgentPlugin("nativehook");
35306f6ba60Sopenharmony_ci
35406f6ba60Sopenharmony_ci        while (true) {
35506f6ba60Sopenharmony_ci            std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
35606f6ba60Sopenharmony_ci        }
35706f6ba60Sopenharmony_ci    }
35806f6ba60Sopenharmony_ci    if (lockFileFd > 0) {
35906f6ba60Sopenharmony_ci        flock(lockFileFd, LOCK_UN);
36006f6ba60Sopenharmony_ci        close(lockFileFd);
36106f6ba60Sopenharmony_ci    }
36206f6ba60Sopenharmony_ci    return 0;
36306f6ba60Sopenharmony_ci}