106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. 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
1606f6ba60Sopenharmony_ci#include <algorithm>
1706f6ba60Sopenharmony_ci#include <cerrno>
1806f6ba60Sopenharmony_ci#include <codecvt>
1906f6ba60Sopenharmony_ci#include <fstream>
2006f6ba60Sopenharmony_ci#include <string>
2106f6ba60Sopenharmony_ci#include <memory>
2206f6ba60Sopenharmony_ci#include <sys/types.h>
2306f6ba60Sopenharmony_ci#include <sys/stat.h>
2406f6ba60Sopenharmony_ci#include <fcntl.h>
2506f6ba60Sopenharmony_ci#include <ctime>
2606f6ba60Sopenharmony_ci#include <malloc.h>
2706f6ba60Sopenharmony_ci#include <parameters.h>
2806f6ba60Sopenharmony_ci#include <unistd.h>
2906f6ba60Sopenharmony_ci
3006f6ba60Sopenharmony_ci#include "application_context.h"
3106f6ba60Sopenharmony_ci#include "context.h"
3206f6ba60Sopenharmony_ci#include "cpu_collector.h"
3306f6ba60Sopenharmony_ci#include "directory_ex.h"
3406f6ba60Sopenharmony_ci#include "dump_usage.h"
3506f6ba60Sopenharmony_ci#include "file_ex.h"
3606f6ba60Sopenharmony_ci#include "hiappevent_util.h"
3706f6ba60Sopenharmony_ci#include "hidebug_native_interface.h"
3806f6ba60Sopenharmony_ci#include "hilog/log.h"
3906f6ba60Sopenharmony_ci#include "iservice_registry.h"
4006f6ba60Sopenharmony_ci#include "memory_collector.h"
4106f6ba60Sopenharmony_ci#include "napi_hidebug_init.h"
4206f6ba60Sopenharmony_ci#include "napi/native_api.h"
4306f6ba60Sopenharmony_ci#include "napi/native_node_api.h"
4406f6ba60Sopenharmony_ci#include "native_engine/native_engine.h"
4506f6ba60Sopenharmony_ci#include "refbase.h"
4606f6ba60Sopenharmony_ci#include "storage_acl.h"
4706f6ba60Sopenharmony_ci#include "system_ability_definition.h"
4806f6ba60Sopenharmony_ci#include "napi_hidebug_gc.h"
4906f6ba60Sopenharmony_ci#include "napi_util.h"
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_cinamespace OHOS {
5206f6ba60Sopenharmony_cinamespace HiviewDFX {
5306f6ba60Sopenharmony_cinamespace {
5406f6ba60Sopenharmony_ci#undef LOG_DOMAIN
5506f6ba60Sopenharmony_ci#define LOG_DOMAIN 0xD002D0A
5606f6ba60Sopenharmony_ci#undef LOG_TAG
5706f6ba60Sopenharmony_ci#define LOG_TAG "HiDebug_NAPI"
5806f6ba60Sopenharmony_ciconstexpr int ONE_VALUE_LIMIT = 1;
5906f6ba60Sopenharmony_ciconstexpr int ARRAY_INDEX_FIRST = 0;
6006f6ba60Sopenharmony_ciconstexpr int ARRAY_INDEX_SECOND = 1;
6106f6ba60Sopenharmony_ciconstexpr int REMOVE_NAPI_WRAP_PARAM_COUNT = 2;
6206f6ba60Sopenharmony_ciconstexpr int NAME_LEN = 128;
6306f6ba60Sopenharmony_ciconstexpr int BYTE_2_KB_SHIFT_BITS = 10;
6406f6ba60Sopenharmony_ciconstexpr int FIRST_POS = 0;
6506f6ba60Sopenharmony_ciconstexpr int SECOND_POS = 1;
6606f6ba60Sopenharmony_ciconstexpr int THIRD_POS = 2;
6706f6ba60Sopenharmony_ciconstexpr int PSS_MIN = 1024;
6806f6ba60Sopenharmony_ciconstexpr int PSS_MAX = 4 * 1024 * 1024;
6906f6ba60Sopenharmony_ciconstexpr int JS_MIN = 85;
7006f6ba60Sopenharmony_ciconstexpr int JS_MAX = 95;
7106f6ba60Sopenharmony_ciconstexpr int FD_MIN = 10;
7206f6ba60Sopenharmony_ciconstexpr int FD_MAX = 10000;
7306f6ba60Sopenharmony_ciconstexpr int THREAD_MIN = 1;
7406f6ba60Sopenharmony_ciconstexpr int THREAD_MAX = 1000;
7506f6ba60Sopenharmony_ciconst std::string SLASH_STR = "/";
7606f6ba60Sopenharmony_ciconst std::string DEFAULT_FILENAME = "undefined";
7706f6ba60Sopenharmony_ciconst std::string JSON_FILE = ".json";
7806f6ba60Sopenharmony_ciconst std::string HEAPSNAPSHOT_FILE = ".heapsnapshot";
7906f6ba60Sopenharmony_ciconst std::string KEY_HIVIEW_USER_TYPE = "const.logsystem.versiontype";
8006f6ba60Sopenharmony_ciconst std::string KEY_HIVIEW_DEVELOP_TYPE = "persist.hiview.leak_detector";
8106f6ba60Sopenharmony_cienum ErrorCode {
8206f6ba60Sopenharmony_ci    PERMISSION_ERROR = 201,
8306f6ba60Sopenharmony_ci    PARAMETER_ERROR = 401,
8406f6ba60Sopenharmony_ci    VERSION_ERROR = 801,
8506f6ba60Sopenharmony_ci    SYSTEM_ABILITY_NOT_FOUND = 11400101,
8606f6ba60Sopenharmony_ci    HAVA_ALREADY_TRACE = 11400102,
8706f6ba60Sopenharmony_ci    WITHOUT_WRITE_PERMISSON = 11400103,
8806f6ba60Sopenharmony_ci    SYSTEM_STATUS_ABNORMAL = 11400104,
8906f6ba60Sopenharmony_ci    NO_CAPTURE_TRACE_RUNNING = 11400105,
9006f6ba60Sopenharmony_ci};
9106f6ba60Sopenharmony_ci
9206f6ba60Sopenharmony_ciconst std::unordered_set<std::string> RESOURCE_TYPE_LIST{
9306f6ba60Sopenharmony_ci    "pss_memory",
9406f6ba60Sopenharmony_ci    "js_heap",
9506f6ba60Sopenharmony_ci    "fd",
9606f6ba60Sopenharmony_ci    "thread"
9706f6ba60Sopenharmony_ci};
9806f6ba60Sopenharmony_cistatic std::map<std::string, std::pair<int, int>> limitResource = {
9906f6ba60Sopenharmony_ci    {{"pss_memory", {PSS_MIN, PSS_MAX}}, {"js_heap", {JS_MIN, JS_MAX}},
10006f6ba60Sopenharmony_ci        {"fd", {FD_MIN, FD_MAX}}, {"thread", {THREAD_MIN, THREAD_MAX}}}
10106f6ba60Sopenharmony_ci};
10206f6ba60Sopenharmony_ci}
10306f6ba60Sopenharmony_ci
10406f6ba60Sopenharmony_cistatic bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
10506f6ba60Sopenharmony_ci{
10606f6ba60Sopenharmony_ci    napi_valuetype valueType = napi_undefined;
10706f6ba60Sopenharmony_ci    napi_typeof(env, value, &valueType);
10806f6ba60Sopenharmony_ci    return valueType == targetType;
10906f6ba60Sopenharmony_ci}
11006f6ba60Sopenharmony_ci
11106f6ba60Sopenharmony_cistatic bool CreateFile(const std::string &path)
11206f6ba60Sopenharmony_ci{
11306f6ba60Sopenharmony_ci    if (access(path.c_str(), F_OK) == 0) {
11406f6ba60Sopenharmony_ci        if (access(path.c_str(), W_OK) == 0) {
11506f6ba60Sopenharmony_ci            return true;
11606f6ba60Sopenharmony_ci        }
11706f6ba60Sopenharmony_ci        return false;
11806f6ba60Sopenharmony_ci    }
11906f6ba60Sopenharmony_ci    const mode_t defaultMode = S_IRUSR | S_IWUSR | S_IRGRP; // -rw-r-----
12006f6ba60Sopenharmony_ci    int fd = creat(path.c_str(), defaultMode);
12106f6ba60Sopenharmony_ci    if (fd == -1) {
12206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "file create failed, errno = %{public}d", errno);
12306f6ba60Sopenharmony_ci        return false;
12406f6ba60Sopenharmony_ci    } else {
12506f6ba60Sopenharmony_ci        close(fd);
12606f6ba60Sopenharmony_ci        return true;
12706f6ba60Sopenharmony_ci    }
12806f6ba60Sopenharmony_ci}
12906f6ba60Sopenharmony_ci
13006f6ba60Sopenharmony_cistatic bool IsLegalPath(const std::string& path)
13106f6ba60Sopenharmony_ci{
13206f6ba60Sopenharmony_ci    if (path.find("./") != std::string::npos ||
13306f6ba60Sopenharmony_ci        path.find("../") != std::string::npos) {
13406f6ba60Sopenharmony_ci        return false;
13506f6ba60Sopenharmony_ci    }
13606f6ba60Sopenharmony_ci    return true;
13706f6ba60Sopenharmony_ci}
13806f6ba60Sopenharmony_ci
13906f6ba60Sopenharmony_cistatic bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arraySize)
14006f6ba60Sopenharmony_ci{
14106f6ba60Sopenharmony_ci    bool isArray = false;
14206f6ba60Sopenharmony_ci    arraySize = 0;
14306f6ba60Sopenharmony_ci    if (napi_is_array(env, param, &isArray) != napi_ok || isArray == false) {
14406f6ba60Sopenharmony_ci        return false;
14506f6ba60Sopenharmony_ci    }
14606f6ba60Sopenharmony_ci    if (napi_get_array_length(env, param, &arraySize) != napi_ok) {
14706f6ba60Sopenharmony_ci        return false;
14806f6ba60Sopenharmony_ci    }
14906f6ba60Sopenharmony_ci    return true;
15006f6ba60Sopenharmony_ci}
15106f6ba60Sopenharmony_ci
15206f6ba60Sopenharmony_cistatic bool GetDumpParam(napi_env env, napi_callback_info info,
15306f6ba60Sopenharmony_ci    int& serviceId, int& fd, std::vector<std::u16string>& args)
15406f6ba60Sopenharmony_ci{
15506f6ba60Sopenharmony_ci    const int valueNum = 3;
15606f6ba60Sopenharmony_ci    size_t argc = valueNum;
15706f6ba60Sopenharmony_ci    napi_value argv[valueNum] = {nullptr};
15806f6ba60Sopenharmony_ci    napi_value thisVar = nullptr;
15906f6ba60Sopenharmony_ci    void *data = nullptr;
16006f6ba60Sopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
16106f6ba60Sopenharmony_ci    if (argc != valueNum) {
16206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
16306f6ba60Sopenharmony_ci        return false;
16406f6ba60Sopenharmony_ci    }
16506f6ba60Sopenharmony_ci    int thirdPos = 2;
16606f6ba60Sopenharmony_ci    if (!MatchValueType(env, argv[0], napi_number) &&
16706f6ba60Sopenharmony_ci        !MatchValueType(env, argv[1], napi_number) &&
16806f6ba60Sopenharmony_ci        !MatchValueType(env, argv[thirdPos], napi_object)) {
16906f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "params type error.");
17006f6ba60Sopenharmony_ci        return false;
17106f6ba60Sopenharmony_ci    }
17206f6ba60Sopenharmony_ci    if (napi_get_value_int32(env, argv[0], &serviceId) != napi_ok) {
17306f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input serviceId failed.");
17406f6ba60Sopenharmony_ci        return false;
17506f6ba60Sopenharmony_ci    }
17606f6ba60Sopenharmony_ci    if (napi_get_value_int32(env, argv[1], &fd) != napi_ok) {
17706f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input fd failed.");
17806f6ba60Sopenharmony_ci        return false;
17906f6ba60Sopenharmony_ci    }
18006f6ba60Sopenharmony_ci    uint32_t arraySize = 0;
18106f6ba60Sopenharmony_ci    if (!IsArrayForNapiValue(env, argv[thirdPos], arraySize)) {
18206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input args failed.");
18306f6ba60Sopenharmony_ci        return false;
18406f6ba60Sopenharmony_ci    }
18506f6ba60Sopenharmony_ci    for (uint32_t i = 0; i < arraySize; i++) {
18606f6ba60Sopenharmony_ci        napi_value jsValue = nullptr;
18706f6ba60Sopenharmony_ci        if (napi_get_element(env, argv[thirdPos], i, &jsValue) != napi_ok) {
18806f6ba60Sopenharmony_ci            HILOG_ERROR(LOG_CORE, "get_element -> Get input args failed.");
18906f6ba60Sopenharmony_ci            return false;
19006f6ba60Sopenharmony_ci        }
19106f6ba60Sopenharmony_ci        const size_t bufSize = 256;
19206f6ba60Sopenharmony_ci        size_t bufLen = 0;
19306f6ba60Sopenharmony_ci        char buf[bufSize] = {0};
19406f6ba60Sopenharmony_ci        if (napi_get_value_string_utf8(env, jsValue, buf, bufSize - 1, &bufLen) != napi_ok) {
19506f6ba60Sopenharmony_ci            HILOG_ERROR(LOG_CORE, "get_value -> Get input args failed.");
19606f6ba60Sopenharmony_ci            return false;
19706f6ba60Sopenharmony_ci        }
19806f6ba60Sopenharmony_ci        std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> strCnv;
19906f6ba60Sopenharmony_ci        args.push_back(strCnv.from_bytes(buf));
20006f6ba60Sopenharmony_ci    }
20106f6ba60Sopenharmony_ci    return true;
20206f6ba60Sopenharmony_ci}
20306f6ba60Sopenharmony_ci
20406f6ba60Sopenharmony_cistatic std::string GetFileNameParam(napi_env env, napi_callback_info info)
20506f6ba60Sopenharmony_ci{
20606f6ba60Sopenharmony_ci    size_t argc = ONE_VALUE_LIMIT;
20706f6ba60Sopenharmony_ci    napi_value argv[ONE_VALUE_LIMIT] = { nullptr };
20806f6ba60Sopenharmony_ci    napi_value thisVar = nullptr;
20906f6ba60Sopenharmony_ci    void *data = nullptr;
21006f6ba60Sopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
21106f6ba60Sopenharmony_ci    if (argc != ONE_VALUE_LIMIT) {
21206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
21306f6ba60Sopenharmony_ci        return DEFAULT_FILENAME;
21406f6ba60Sopenharmony_ci    }
21506f6ba60Sopenharmony_ci    if (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_string)) {
21606f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Type error, should be string type!");
21706f6ba60Sopenharmony_ci        return DEFAULT_FILENAME;
21806f6ba60Sopenharmony_ci    }
21906f6ba60Sopenharmony_ci    size_t bufLen = 0;
22006f6ba60Sopenharmony_ci    napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufLen);
22106f6ba60Sopenharmony_ci    if (status != napi_ok) {
22206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input filename param length failed.");
22306f6ba60Sopenharmony_ci        return DEFAULT_FILENAME;
22406f6ba60Sopenharmony_ci    }
22506f6ba60Sopenharmony_ci    const int bufMax = 128;
22606f6ba60Sopenharmony_ci    if (bufLen > bufMax || bufLen == 0) {
22706f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "input filename param length is illegal.");
22806f6ba60Sopenharmony_ci        return DEFAULT_FILENAME;
22906f6ba60Sopenharmony_ci    }
23006f6ba60Sopenharmony_ci    char buf[bufLen + 1];
23106f6ba60Sopenharmony_ci    napi_get_value_string_utf8(env, argv[0], buf, bufLen + 1, &bufLen);
23206f6ba60Sopenharmony_ci    std::string fileName = buf;
23306f6ba60Sopenharmony_ci    return fileName;
23406f6ba60Sopenharmony_ci}
23506f6ba60Sopenharmony_ci
23606f6ba60Sopenharmony_cistatic bool GetTheOnlyStringParam(napi_env env, napi_callback_info info, std::string &fileName)
23706f6ba60Sopenharmony_ci{
23806f6ba60Sopenharmony_ci    size_t argc = ONE_VALUE_LIMIT;
23906f6ba60Sopenharmony_ci    napi_value argv[ONE_VALUE_LIMIT] = { nullptr };
24006f6ba60Sopenharmony_ci    napi_value thisVar = nullptr;
24106f6ba60Sopenharmony_ci    void *data = nullptr;
24206f6ba60Sopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
24306f6ba60Sopenharmony_ci    if (argc != ONE_VALUE_LIMIT) {
24406f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
24506f6ba60Sopenharmony_ci        return false;
24606f6ba60Sopenharmony_ci    }
24706f6ba60Sopenharmony_ci    if (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_string)) {
24806f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Type error, should be string type!");
24906f6ba60Sopenharmony_ci        return false;
25006f6ba60Sopenharmony_ci    }
25106f6ba60Sopenharmony_ci    size_t bufLen = 0;
25206f6ba60Sopenharmony_ci    napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufLen);
25306f6ba60Sopenharmony_ci    if (status != napi_ok) {
25406f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input filename param length failed.");
25506f6ba60Sopenharmony_ci        return false;
25606f6ba60Sopenharmony_ci    }
25706f6ba60Sopenharmony_ci    const int bufMax = 128;
25806f6ba60Sopenharmony_ci    if (bufLen > bufMax || bufLen == 0) {
25906f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "input filename param length is illegal.");
26006f6ba60Sopenharmony_ci        return false;
26106f6ba60Sopenharmony_ci    }
26206f6ba60Sopenharmony_ci    char buf[bufLen + 1];
26306f6ba60Sopenharmony_ci    napi_get_value_string_utf8(env, argv[0], buf, bufLen + 1, &bufLen);
26406f6ba60Sopenharmony_ci    fileName = buf;
26506f6ba60Sopenharmony_ci    return true;
26606f6ba60Sopenharmony_ci}
26706f6ba60Sopenharmony_ci
26806f6ba60Sopenharmony_cistatic bool GetTraceParam(napi_env env, napi_callback_info info,
26906f6ba60Sopenharmony_ci    uint32_t& traceFlag, uint32_t& limitSize, std::vector<uint64_t>& tags)
27006f6ba60Sopenharmony_ci{
27106f6ba60Sopenharmony_ci    const int valueNum = 3;
27206f6ba60Sopenharmony_ci    size_t argc = valueNum;
27306f6ba60Sopenharmony_ci    napi_value argv[valueNum] = {nullptr};
27406f6ba60Sopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
27506f6ba60Sopenharmony_ci    if (argc != valueNum) {
27606f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "invalid numbers of params!");
27706f6ba60Sopenharmony_ci        return false;
27806f6ba60Sopenharmony_ci    }
27906f6ba60Sopenharmony_ci    if (!MatchValueType(env, argv[FIRST_POS], napi_object) &&
28006f6ba60Sopenharmony_ci        !MatchValueType(env, argv[SECOND_POS], napi_number) &&
28106f6ba60Sopenharmony_ci        !MatchValueType(env, argv[THIRD_POS], napi_number)) {
28206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "params type error.");
28306f6ba60Sopenharmony_ci        return false;
28406f6ba60Sopenharmony_ci    }
28506f6ba60Sopenharmony_ci    uint32_t arraySize = 0;
28606f6ba60Sopenharmony_ci    if (!IsArrayForNapiValue(env, argv[FIRST_POS], arraySize)) {
28706f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input tags failed.");
28806f6ba60Sopenharmony_ci        return false;
28906f6ba60Sopenharmony_ci    }
29006f6ba60Sopenharmony_ci    uint64_t tag = 0;
29106f6ba60Sopenharmony_ci    bool lossless = true;
29206f6ba60Sopenharmony_ci    for (uint32_t i = 0; i < arraySize; ++i) {
29306f6ba60Sopenharmony_ci        napi_value jsValue = nullptr;
29406f6ba60Sopenharmony_ci        if (napi_get_element(env, argv[FIRST_POS], i, &jsValue) != napi_ok) {
29506f6ba60Sopenharmony_ci            HILOG_ERROR(LOG_CORE, "get_element -> Get input tags failed.");
29606f6ba60Sopenharmony_ci            return false;
29706f6ba60Sopenharmony_ci        }
29806f6ba60Sopenharmony_ci        if (napi_get_value_bigint_uint64(env, jsValue, &tag, &lossless) != napi_ok) {
29906f6ba60Sopenharmony_ci            HILOG_ERROR(LOG_CORE, "Get input tags failed.");
30006f6ba60Sopenharmony_ci            return false;
30106f6ba60Sopenharmony_ci        }
30206f6ba60Sopenharmony_ci        tags.push_back(tag);
30306f6ba60Sopenharmony_ci    }
30406f6ba60Sopenharmony_ci    if (napi_get_value_uint32(env, argv[SECOND_POS], &traceFlag) != napi_ok) {
30506f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input traceFlag failed.");
30606f6ba60Sopenharmony_ci        return false;
30706f6ba60Sopenharmony_ci    }
30806f6ba60Sopenharmony_ci    if (napi_get_value_uint32(env, argv[THIRD_POS], &limitSize) != napi_ok) {
30906f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "Get input limitSize failed.");
31006f6ba60Sopenharmony_ci        return false;
31106f6ba60Sopenharmony_ci    }
31206f6ba60Sopenharmony_ci    return true;
31306f6ba60Sopenharmony_ci}
31406f6ba60Sopenharmony_ci
31506f6ba60Sopenharmony_cinapi_value StartProfiling(napi_env env, napi_callback_info info)
31606f6ba60Sopenharmony_ci{
31706f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("startProfiling");
31806f6ba60Sopenharmony_ci    std::string fileName = GetFileNameParam(env, info);
31906f6ba60Sopenharmony_ci    auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
32006f6ba60Sopenharmony_ci    if (context == nullptr) {
32106f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
32206f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get ApplicationContext failed.");
32306f6ba60Sopenharmony_ci    }
32406f6ba60Sopenharmony_ci    std::string filesDir = context->GetFilesDir();
32506f6ba60Sopenharmony_ci    if (filesDir.empty()) {
32606f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
32706f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get App files dir failed.");
32806f6ba60Sopenharmony_ci    }
32906f6ba60Sopenharmony_ci    std::string filePath = filesDir + SLASH_STR + fileName + JSON_FILE;
33006f6ba60Sopenharmony_ci    if (!IsLegalPath(filePath)) {
33106f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
33206f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "input fileName is illegal.");
33306f6ba60Sopenharmony_ci    }
33406f6ba60Sopenharmony_ci    if (!CreateFile(filePath)) {
33506f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
33606f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "file created failed.");
33706f6ba60Sopenharmony_ci    }
33806f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
33906f6ba60Sopenharmony_ci    engine->StartCpuProfiler(filePath);
34006f6ba60Sopenharmony_ci    return CreateUndefined(env);
34106f6ba60Sopenharmony_ci}
34206f6ba60Sopenharmony_ci
34306f6ba60Sopenharmony_cinapi_value StartJsCpuProfiling(napi_env env, napi_callback_info info)
34406f6ba60Sopenharmony_ci{
34506f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("startJsCpuProfiling");
34606f6ba60Sopenharmony_ci    std::string fileName;
34706f6ba60Sopenharmony_ci    if (!GetTheOnlyStringParam(env, info, fileName)) {
34806f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
34906f6ba60Sopenharmony_ci        std::string paramErrorMessage = "Invalid parameter, require a string parameter.";
35006f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
35106f6ba60Sopenharmony_ci        return CreateUndefined(env);
35206f6ba60Sopenharmony_ci    }
35306f6ba60Sopenharmony_ci    HILOG_INFO(LOG_CORE, "filename: %{public}s.", fileName.c_str());
35406f6ba60Sopenharmony_ci    auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
35506f6ba60Sopenharmony_ci    if (context == nullptr) {
35606f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
35706f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get ApplicationContext failed.");
35806f6ba60Sopenharmony_ci    }
35906f6ba60Sopenharmony_ci    std::string filesDir = context->GetFilesDir();
36006f6ba60Sopenharmony_ci    if (filesDir.empty()) {
36106f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
36206f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get App files dir failed.");
36306f6ba60Sopenharmony_ci    }
36406f6ba60Sopenharmony_ci    std::string filePath = filesDir + SLASH_STR + fileName + JSON_FILE;
36506f6ba60Sopenharmony_ci    if (!IsLegalPath(filePath)) {
36606f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
36706f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "input fileName is illegal.");
36806f6ba60Sopenharmony_ci    }
36906f6ba60Sopenharmony_ci    if (!CreateFile(filePath)) {
37006f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
37106f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "file created failed.");
37206f6ba60Sopenharmony_ci    }
37306f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
37406f6ba60Sopenharmony_ci    engine->StartCpuProfiler(filePath);
37506f6ba60Sopenharmony_ci    return CreateUndefined(env);
37606f6ba60Sopenharmony_ci}
37706f6ba60Sopenharmony_ci
37806f6ba60Sopenharmony_cinapi_value StopProfiling(napi_env env, napi_callback_info info)
37906f6ba60Sopenharmony_ci{
38006f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("stopProfiling");
38106f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
38206f6ba60Sopenharmony_ci    engine->StopCpuProfiler();
38306f6ba60Sopenharmony_ci    return CreateUndefined(env);
38406f6ba60Sopenharmony_ci}
38506f6ba60Sopenharmony_ci
38606f6ba60Sopenharmony_cinapi_value StopJsCpuProfiling(napi_env env, napi_callback_info info)
38706f6ba60Sopenharmony_ci{
38806f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("stopJsCpuProfiling");
38906f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
39006f6ba60Sopenharmony_ci    engine->StopCpuProfiler();
39106f6ba60Sopenharmony_ci    return CreateUndefined(env);
39206f6ba60Sopenharmony_ci}
39306f6ba60Sopenharmony_ci
39406f6ba60Sopenharmony_cinapi_value DumpHeapData(napi_env env, napi_callback_info info)
39506f6ba60Sopenharmony_ci{
39606f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("dumpHeapData");
39706f6ba60Sopenharmony_ci    std::string fileName = GetFileNameParam(env, info);
39806f6ba60Sopenharmony_ci    auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
39906f6ba60Sopenharmony_ci    if (context == nullptr) {
40006f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get ApplicationContext failed.");
40106f6ba60Sopenharmony_ci    }
40206f6ba60Sopenharmony_ci    std::string filesDir = context->GetFilesDir();
40306f6ba60Sopenharmony_ci    if (filesDir.empty()) {
40406f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get App files dir failed.");
40506f6ba60Sopenharmony_ci    }
40606f6ba60Sopenharmony_ci    std::string filePath = filesDir + SLASH_STR + fileName + HEAPSNAPSHOT_FILE;
40706f6ba60Sopenharmony_ci    if (!IsLegalPath(filePath)) {
40806f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "input fileName is illegal.");
40906f6ba60Sopenharmony_ci    }
41006f6ba60Sopenharmony_ci    if (!CreateFile(filePath)) {
41106f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "file created failed.");
41206f6ba60Sopenharmony_ci    }
41306f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
41406f6ba60Sopenharmony_ci    engine->DumpHeapSnapshot(filePath, true, DumpFormat::JSON, false, true);
41506f6ba60Sopenharmony_ci    return CreateUndefined(env);
41606f6ba60Sopenharmony_ci}
41706f6ba60Sopenharmony_ci
41806f6ba60Sopenharmony_cinapi_value DumpJsHeapData(napi_env env, napi_callback_info info)
41906f6ba60Sopenharmony_ci{
42006f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("dumpJsHeapData");
42106f6ba60Sopenharmony_ci    std::string fileName;
42206f6ba60Sopenharmony_ci    if (!GetTheOnlyStringParam(env, info, fileName)) {
42306f6ba60Sopenharmony_ci        std::string paramErrorMessage = "Invalid parameter, require a string parameter.";
42406f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
42506f6ba60Sopenharmony_ci        return CreateUndefined(env);
42606f6ba60Sopenharmony_ci    }
42706f6ba60Sopenharmony_ci    HILOG_ERROR(LOG_CORE, "filename: %{public}s.", fileName.c_str());
42806f6ba60Sopenharmony_ci    auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
42906f6ba60Sopenharmony_ci    if (context == nullptr) {
43006f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get ApplicationContext failed.");
43106f6ba60Sopenharmony_ci    }
43206f6ba60Sopenharmony_ci    std::string filesDir = context->GetFilesDir();
43306f6ba60Sopenharmony_ci    if (filesDir.empty()) {
43406f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "Get App files dir failed.");
43506f6ba60Sopenharmony_ci    }
43606f6ba60Sopenharmony_ci    std::string filePath = filesDir + SLASH_STR + fileName + HEAPSNAPSHOT_FILE;
43706f6ba60Sopenharmony_ci    if (!IsLegalPath(filePath)) {
43806f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "input fileName is illegal.");
43906f6ba60Sopenharmony_ci    }
44006f6ba60Sopenharmony_ci    if (!CreateFile(filePath)) {
44106f6ba60Sopenharmony_ci        return CreateErrorMessage(env, "file created failed.");
44206f6ba60Sopenharmony_ci    }
44306f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
44406f6ba60Sopenharmony_ci    engine->DumpHeapSnapshot(filePath, true, DumpFormat::JSON, false, true);
44506f6ba60Sopenharmony_ci    return CreateUndefined(env);
44606f6ba60Sopenharmony_ci}
44706f6ba60Sopenharmony_ci
44806f6ba60Sopenharmony_cinapi_value GetPss(napi_env env, napi_callback_info info)
44906f6ba60Sopenharmony_ci{
45006f6ba60Sopenharmony_ci    napi_value pss;
45106f6ba60Sopenharmony_ci    std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
45206f6ba60Sopenharmony_ci    if (collector != nullptr) {
45306f6ba60Sopenharmony_ci        int pid = getprocpid();
45406f6ba60Sopenharmony_ci        auto collectResult = collector->CollectProcessMemory(pid);
45506f6ba60Sopenharmony_ci        int32_t pssInfo = collectResult.data.pss + collectResult.data.swapPss;
45606f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, pssInfo, &pss);
45706f6ba60Sopenharmony_ci    } else {
45806f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, 0, &pss);
45906f6ba60Sopenharmony_ci    }
46006f6ba60Sopenharmony_ci    return pss;
46106f6ba60Sopenharmony_ci}
46206f6ba60Sopenharmony_ci
46306f6ba60Sopenharmony_cinapi_value GetSharedDirty(napi_env env, napi_callback_info info)
46406f6ba60Sopenharmony_ci{
46506f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getSharedDirty");
46606f6ba60Sopenharmony_ci    napi_value sharedDirty;
46706f6ba60Sopenharmony_ci    std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
46806f6ba60Sopenharmony_ci    if (collector != nullptr) {
46906f6ba60Sopenharmony_ci        int pid = getprocpid();
47006f6ba60Sopenharmony_ci        auto collectResult = collector->CollectProcessMemory(pid);
47106f6ba60Sopenharmony_ci        int32_t sharedDirtyInfo = collectResult.data.sharedDirty;
47206f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, sharedDirtyInfo, &sharedDirty);
47306f6ba60Sopenharmony_ci    } else {
47406f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, 0, &sharedDirty);
47506f6ba60Sopenharmony_ci    }
47606f6ba60Sopenharmony_ci    return sharedDirty;
47706f6ba60Sopenharmony_ci}
47806f6ba60Sopenharmony_ci
47906f6ba60Sopenharmony_cinapi_value GetPrivateDirty(napi_env env, napi_callback_info info)
48006f6ba60Sopenharmony_ci{
48106f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getPrivateDirty");
48206f6ba60Sopenharmony_ci    napi_value privateDirtyValue;
48306f6ba60Sopenharmony_ci    std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
48406f6ba60Sopenharmony_ci    if (collector != nullptr) {
48506f6ba60Sopenharmony_ci        pid_t pid = getprocpid();
48606f6ba60Sopenharmony_ci        auto collectResult = collector->CollectProcessMemory(pid);
48706f6ba60Sopenharmony_ci        int32_t privateDirty = collectResult.data.privateDirty;
48806f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, privateDirty, &privateDirtyValue);
48906f6ba60Sopenharmony_ci    } else {
49006f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, 0, &privateDirtyValue);
49106f6ba60Sopenharmony_ci    }
49206f6ba60Sopenharmony_ci    return privateDirtyValue;
49306f6ba60Sopenharmony_ci}
49406f6ba60Sopenharmony_ci
49506f6ba60Sopenharmony_cinapi_value GetCpuUsage(napi_env env, napi_callback_info info)
49606f6ba60Sopenharmony_ci{
49706f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getCpuUsage");
49806f6ba60Sopenharmony_ci    napi_value cpuUsageValue;
49906f6ba60Sopenharmony_ci    std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
50006f6ba60Sopenharmony_ci    pid_t pid = getprocpid();
50106f6ba60Sopenharmony_ci    double cpuUsage = dumpUsage->GetCpuUsage(pid);
50206f6ba60Sopenharmony_ci    napi_create_double(env, cpuUsage, &cpuUsageValue);
50306f6ba60Sopenharmony_ci    return cpuUsageValue;
50406f6ba60Sopenharmony_ci}
50506f6ba60Sopenharmony_ci
50606f6ba60Sopenharmony_cinapi_value GetNativeHeapSize(napi_env env, napi_callback_info info)
50706f6ba60Sopenharmony_ci{
50806f6ba60Sopenharmony_ci    struct mallinfo mi = mallinfo();
50906f6ba60Sopenharmony_ci    napi_value nativeHeapSize;
51006f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, uint64_t(mi.uordblks + mi.fordblks), &nativeHeapSize);
51106f6ba60Sopenharmony_ci    return nativeHeapSize;
51206f6ba60Sopenharmony_ci}
51306f6ba60Sopenharmony_ci
51406f6ba60Sopenharmony_cinapi_value GetNativeHeapAllocatedSize(napi_env env, napi_callback_info info)
51506f6ba60Sopenharmony_ci{
51606f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getNativeHeapAllocatedSize");
51706f6ba60Sopenharmony_ci    struct mallinfo mi = mallinfo();
51806f6ba60Sopenharmony_ci    napi_value nativeHeapAllocatedSize;
51906f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, uint64_t(mi.uordblks), &nativeHeapAllocatedSize);
52006f6ba60Sopenharmony_ci    return nativeHeapAllocatedSize;
52106f6ba60Sopenharmony_ci}
52206f6ba60Sopenharmony_ci
52306f6ba60Sopenharmony_cinapi_value GetNativeHeapFreeSize(napi_env env, napi_callback_info info)
52406f6ba60Sopenharmony_ci{
52506f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getNativeHeapFreeSize");
52606f6ba60Sopenharmony_ci    struct mallinfo mi = mallinfo();
52706f6ba60Sopenharmony_ci    napi_value nativeHeapFreeSize;
52806f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, uint64_t(mi.fordblks), &nativeHeapFreeSize);
52906f6ba60Sopenharmony_ci    return nativeHeapFreeSize;
53006f6ba60Sopenharmony_ci}
53106f6ba60Sopenharmony_ci
53206f6ba60Sopenharmony_cistatic napi_value GetServiceDump(napi_env env, napi_callback_info info)
53306f6ba60Sopenharmony_ci{
53406f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getServiceDump");
53506f6ba60Sopenharmony_ci    int serviceAbilityId = 0;
53606f6ba60Sopenharmony_ci    int fd = 0;
53706f6ba60Sopenharmony_ci    std::vector<std::u16string> args;
53806f6ba60Sopenharmony_ci    if (!GetDumpParam(env, info, serviceAbilityId, fd, args)) {
53906f6ba60Sopenharmony_ci        std::string paramErrorMessage = "The parameter check failed.";
54006f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
54106f6ba60Sopenharmony_ci        return CreateUndefined(env);
54206f6ba60Sopenharmony_ci    }
54306f6ba60Sopenharmony_ci
54406f6ba60Sopenharmony_ci    sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54506f6ba60Sopenharmony_ci    if (!sam) {
54606f6ba60Sopenharmony_ci        return CreateUndefined(env);
54706f6ba60Sopenharmony_ci    }
54806f6ba60Sopenharmony_ci    sptr<IRemoteObject> sa = sam->CheckSystemAbility(serviceAbilityId);
54906f6ba60Sopenharmony_ci    if (sa == nullptr) {
55006f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "no this system ability.");
55106f6ba60Sopenharmony_ci        std::string idErrorMessage = "ServiceId invalid. The system ability does not exist.";
55206f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_ABILITY_NOT_FOUND).c_str(), idErrorMessage.c_str());
55306f6ba60Sopenharmony_ci        return CreateUndefined(env);
55406f6ba60Sopenharmony_ci    }
55506f6ba60Sopenharmony_ci    int dumpResult = sa->Dump(fd, args);
55606f6ba60Sopenharmony_ci    HILOG_INFO(LOG_CORE, "Dump result: %{public}d", dumpResult);
55706f6ba60Sopenharmony_ci    return CreateUndefined(env);
55806f6ba60Sopenharmony_ci}
55906f6ba60Sopenharmony_ci
56006f6ba60Sopenharmony_cinapi_value GetVss(napi_env env, napi_callback_info info)
56106f6ba60Sopenharmony_ci{
56206f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getVss");
56306f6ba60Sopenharmony_ci    napi_value vss;
56406f6ba60Sopenharmony_ci    std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
56506f6ba60Sopenharmony_ci    if (collector != nullptr) {
56606f6ba60Sopenharmony_ci        pid_t pid = getprocpid();
56706f6ba60Sopenharmony_ci        auto collectResult = collector->CollectProcessVss(pid);
56806f6ba60Sopenharmony_ci        uint64_t vssInfo = collectResult.data;
56906f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, vssInfo, &vss);
57006f6ba60Sopenharmony_ci    } else {
57106f6ba60Sopenharmony_ci        napi_create_bigint_uint64(env, 0, &vss);
57206f6ba60Sopenharmony_ci    }
57306f6ba60Sopenharmony_ci    return vss;
57406f6ba60Sopenharmony_ci}
57506f6ba60Sopenharmony_ci
57606f6ba60Sopenharmony_cistatic napi_value GetSystemCpuUsage(napi_env env, napi_callback_info info)
57706f6ba60Sopenharmony_ci{
57806f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getSystemCpuUsage");
57906f6ba60Sopenharmony_ci    auto cpuUsageOptional = HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage();
58006f6ba60Sopenharmony_ci    if (!cpuUsageOptional.has_value()) {
58106f6ba60Sopenharmony_ci        std::string paramErrorMessage = "The status of the system CPU usage is abnormal.";
58206f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), paramErrorMessage.c_str());
58306f6ba60Sopenharmony_ci        return CreateUndefined(env);
58406f6ba60Sopenharmony_ci    }
58506f6ba60Sopenharmony_ci    napi_value retMsg = nullptr;
58606f6ba60Sopenharmony_ci    napi_create_double(env, cpuUsageOptional.value(),  &retMsg);
58706f6ba60Sopenharmony_ci    return retMsg;
58806f6ba60Sopenharmony_ci}
58906f6ba60Sopenharmony_ci
59006f6ba60Sopenharmony_cistatic napi_value RemoveNapiWrap(napi_env env, napi_callback_info info)
59106f6ba60Sopenharmony_ci{
59206f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("removeNapiWrap");
59306f6ba60Sopenharmony_ci    size_t argc = REMOVE_NAPI_WRAP_PARAM_COUNT;
59406f6ba60Sopenharmony_ci    napi_value argv[REMOVE_NAPI_WRAP_PARAM_COUNT] = {nullptr};
59506f6ba60Sopenharmony_ci    napi_value thisVar = nullptr;
59606f6ba60Sopenharmony_ci    void *data = nullptr;
59706f6ba60Sopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
59806f6ba60Sopenharmony_ci    if (argc != REMOVE_NAPI_WRAP_PARAM_COUNT ||
59906f6ba60Sopenharmony_ci        (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_object) ||
60006f6ba60Sopenharmony_ci        !MatchValueType(env, argv[ARRAY_INDEX_SECOND], napi_boolean))) {
60106f6ba60Sopenharmony_ci        HILOG_DEBUG(LOG_CORE, "RemoveNapiWrap Failed to parse parameters, argc %{public}d", (int)argc);
60206f6ba60Sopenharmony_ci        std::string paramErrorMessage = "The parameter check failed.";
60306f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
60406f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
60506f6ba60Sopenharmony_ci        return CreateUndefined(env);
60606f6ba60Sopenharmony_ci    }
60706f6ba60Sopenharmony_ci
60806f6ba60Sopenharmony_ci    // remove jsObj's wrap
60906f6ba60Sopenharmony_ci    auto jsObj = argv[ARRAY_INDEX_FIRST];
61006f6ba60Sopenharmony_ci    void *nativePtr = nullptr;
61106f6ba60Sopenharmony_ci    napi_remove_wrap(env, jsObj, (void **)&nativePtr);
61206f6ba60Sopenharmony_ci
61306f6ba60Sopenharmony_ci    // remove jsObj's properties wrap
61406f6ba60Sopenharmony_ci    bool needRemoveProperty = false;
61506f6ba60Sopenharmony_ci    napi_get_value_bool(env, argv[ARRAY_INDEX_SECOND], &needRemoveProperty);
61606f6ba60Sopenharmony_ci    if (needRemoveProperty) {
61706f6ba60Sopenharmony_ci        napi_value allPropertyNames = nullptr;
61806f6ba60Sopenharmony_ci        napi_object_get_keys(env, jsObj, &allPropertyNames);
61906f6ba60Sopenharmony_ci        uint32_t nameCount = 0;
62006f6ba60Sopenharmony_ci        napi_get_array_length(env, allPropertyNames, &nameCount);
62106f6ba60Sopenharmony_ci        for (size_t i = 0; i < nameCount; ++i) {
62206f6ba60Sopenharmony_ci            napi_value propertyName = nullptr;
62306f6ba60Sopenharmony_ci            napi_get_element(env, allPropertyNames, i, &propertyName);
62406f6ba60Sopenharmony_ci            char name[NAME_LEN] = {0};
62506f6ba60Sopenharmony_ci            size_t len = 0;
62606f6ba60Sopenharmony_ci            napi_get_value_string_utf8(env, propertyName, name, NAME_LEN, &len);
62706f6ba60Sopenharmony_ci            napi_value propertyObj = nullptr;
62806f6ba60Sopenharmony_ci            napi_get_named_property(env, jsObj, name, &propertyObj);
62906f6ba60Sopenharmony_ci            napi_remove_wrap(env, propertyObj, (void **)&nativePtr);
63006f6ba60Sopenharmony_ci        }
63106f6ba60Sopenharmony_ci    }
63206f6ba60Sopenharmony_ci    return CreateUndefined(env);
63306f6ba60Sopenharmony_ci}
63406f6ba60Sopenharmony_ci
63506f6ba60Sopenharmony_cinapi_value GetAppVMMemoryInfo(napi_env env, napi_callback_info info)
63606f6ba60Sopenharmony_ci{
63706f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getAppVMMemoryInfo");
63806f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
63906f6ba60Sopenharmony_ci    if (engine == nullptr) {
64006f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
64106f6ba60Sopenharmony_ci        return CreateUndefined(env);
64206f6ba60Sopenharmony_ci    }
64306f6ba60Sopenharmony_ci
64406f6ba60Sopenharmony_ci    napi_value vMMemoryInfo;
64506f6ba60Sopenharmony_ci    napi_create_object(env, &vMMemoryInfo);
64606f6ba60Sopenharmony_ci
64706f6ba60Sopenharmony_ci    napi_value totalHeap;
64806f6ba60Sopenharmony_ci    uint64_t totalHeapValue = engine->GetHeapTotalSize();
64906f6ba60Sopenharmony_ci    totalHeapValue = totalHeapValue >> BYTE_2_KB_SHIFT_BITS;
65006f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, totalHeapValue, &totalHeap);
65106f6ba60Sopenharmony_ci    napi_set_named_property(env, vMMemoryInfo, "totalHeap", totalHeap);
65206f6ba60Sopenharmony_ci
65306f6ba60Sopenharmony_ci    napi_value heapUsed;
65406f6ba60Sopenharmony_ci    uint64_t heapUsedValue = engine->GetHeapUsedSize();
65506f6ba60Sopenharmony_ci    heapUsedValue = heapUsedValue >> BYTE_2_KB_SHIFT_BITS;
65606f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, heapUsedValue, &heapUsed);
65706f6ba60Sopenharmony_ci    napi_set_named_property(env, vMMemoryInfo, "heapUsed", heapUsed);
65806f6ba60Sopenharmony_ci
65906f6ba60Sopenharmony_ci    napi_value allArraySize;
66006f6ba60Sopenharmony_ci    uint64_t allArraySizeValue = engine->GetArrayBufferSize();
66106f6ba60Sopenharmony_ci    allArraySizeValue = allArraySizeValue >> BYTE_2_KB_SHIFT_BITS;
66206f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, allArraySizeValue, &allArraySize);
66306f6ba60Sopenharmony_ci    napi_set_named_property(env, vMMemoryInfo, "allArraySize", allArraySize);
66406f6ba60Sopenharmony_ci
66506f6ba60Sopenharmony_ci    return vMMemoryInfo;
66606f6ba60Sopenharmony_ci}
66706f6ba60Sopenharmony_ci
66806f6ba60Sopenharmony_cistatic void ConvertThreadCpuUsageToJs(napi_env env, napi_value &result, uint32_t threadIdValue, double cpuUsageValue)
66906f6ba60Sopenharmony_ci{
67006f6ba60Sopenharmony_ci    napi_create_object(env, &result);
67106f6ba60Sopenharmony_ci
67206f6ba60Sopenharmony_ci    napi_value threadId;
67306f6ba60Sopenharmony_ci    napi_create_uint32(env, threadIdValue, &threadId);
67406f6ba60Sopenharmony_ci    napi_set_named_property(env, result, "threadId", threadId);
67506f6ba60Sopenharmony_ci
67606f6ba60Sopenharmony_ci    napi_value cpuUsage;
67706f6ba60Sopenharmony_ci    napi_create_double(env, cpuUsageValue, &cpuUsage);
67806f6ba60Sopenharmony_ci    napi_set_named_property(env, result, "cpuUsage", cpuUsage);
67906f6ba60Sopenharmony_ci}
68006f6ba60Sopenharmony_ci
68106f6ba60Sopenharmony_cistatic void ConvertThreadCpuUsageMapToJs(napi_env env, napi_value &result, const std::map<uint32_t, double> &threadMap)
68206f6ba60Sopenharmony_ci{
68306f6ba60Sopenharmony_ci    napi_create_array(env, &result);
68406f6ba60Sopenharmony_ci    size_t idx = 0;
68506f6ba60Sopenharmony_ci    for (const auto[threadId, cpuUsage] : threadMap) {
68606f6ba60Sopenharmony_ci        napi_value obj = nullptr;
68706f6ba60Sopenharmony_ci        ConvertThreadCpuUsageToJs(env, obj, threadId, cpuUsage);
68806f6ba60Sopenharmony_ci        napi_set_element(env, result, idx, obj);
68906f6ba60Sopenharmony_ci        idx++;
69006f6ba60Sopenharmony_ci    }
69106f6ba60Sopenharmony_ci}
69206f6ba60Sopenharmony_ci
69306f6ba60Sopenharmony_cinapi_value GetAppThreadCpuUsage(napi_env env, napi_callback_info info)
69406f6ba60Sopenharmony_ci{
69506f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getAppThreadCpuUsage");
69606f6ba60Sopenharmony_ci    napi_value result;
69706f6ba60Sopenharmony_ci    auto nativeInterface = HidebugNativeInterface::CreateInstance();
69806f6ba60Sopenharmony_ci    if (!nativeInterface) {
69906f6ba60Sopenharmony_ci        return CreateUndefined(env);
70006f6ba60Sopenharmony_ci    }
70106f6ba60Sopenharmony_ci    std::map<uint32_t, double> threadMap = nativeInterface->GetAppThreadCpuUsage();
70206f6ba60Sopenharmony_ci    ConvertThreadCpuUsageMapToJs(env, result, threadMap);
70306f6ba60Sopenharmony_ci    return result;
70406f6ba60Sopenharmony_ci}
70506f6ba60Sopenharmony_ci
70606f6ba60Sopenharmony_cinapi_value GetAppMemoryLimit(napi_env env, napi_callback_info info)
70706f6ba60Sopenharmony_ci{
70806f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getAppMemoryLimit");
70906f6ba60Sopenharmony_ci    napi_value appMemoryLimit;
71006f6ba60Sopenharmony_ci    napi_create_object(env, &appMemoryLimit);
71106f6ba60Sopenharmony_ci    auto nativeInterface = HidebugNativeInterface::CreateInstance();
71206f6ba60Sopenharmony_ci    if (!nativeInterface) {
71306f6ba60Sopenharmony_ci        return CreateUndefined(env);
71406f6ba60Sopenharmony_ci    }
71506f6ba60Sopenharmony_ci
71606f6ba60Sopenharmony_ci    auto memoryLimit = nativeInterface->GetAppMemoryLimit();
71706f6ba60Sopenharmony_ci    if (!memoryLimit) {
71806f6ba60Sopenharmony_ci        return CreateUndefined(env);
71906f6ba60Sopenharmony_ci    }
72006f6ba60Sopenharmony_ci    napi_value rssLimit;
72106f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, memoryLimit->rssLimit, &rssLimit);
72206f6ba60Sopenharmony_ci    napi_set_named_property(env, appMemoryLimit, "rssLimit", rssLimit);
72306f6ba60Sopenharmony_ci
72406f6ba60Sopenharmony_ci    napi_value vssLimit;
72506f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, memoryLimit->vssLimit, &vssLimit);
72606f6ba60Sopenharmony_ci    napi_set_named_property(env, appMemoryLimit, "vssLimit", vssLimit);
72706f6ba60Sopenharmony_ci
72806f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine *>(env);
72906f6ba60Sopenharmony_ci    if (engine == nullptr) {
73006f6ba60Sopenharmony_ci        return CreateUndefined(env);
73106f6ba60Sopenharmony_ci    }
73206f6ba60Sopenharmony_ci    napi_value vmHeapLimit;
73306f6ba60Sopenharmony_ci    uint64_t vmHeapLimitValue = engine->GetHeapLimitSize();
73406f6ba60Sopenharmony_ci    vmHeapLimitValue = vmHeapLimitValue >> BYTE_2_KB_SHIFT_BITS;
73506f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, vmHeapLimitValue, &vmHeapLimit);
73606f6ba60Sopenharmony_ci    napi_set_named_property(env, appMemoryLimit, "vmHeapLimit", vmHeapLimit);
73706f6ba60Sopenharmony_ci
73806f6ba60Sopenharmony_ci    napi_value vmTotalHeapSize;
73906f6ba60Sopenharmony_ci    uint64_t vmTotalHeapSizeValue = engine->GetProcessHeapLimitSize();
74006f6ba60Sopenharmony_ci    vmTotalHeapSizeValue = vmTotalHeapSizeValue >> BYTE_2_KB_SHIFT_BITS;
74106f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, vmTotalHeapSizeValue, &vmTotalHeapSize);
74206f6ba60Sopenharmony_ci    napi_set_named_property(env, appMemoryLimit, "vmTotalHeapSize", vmTotalHeapSize);
74306f6ba60Sopenharmony_ci
74406f6ba60Sopenharmony_ci    return appMemoryLimit;
74506f6ba60Sopenharmony_ci}
74606f6ba60Sopenharmony_ci
74706f6ba60Sopenharmony_cinapi_value GetAppNativeMemInfo(napi_env env, napi_callback_info info)
74806f6ba60Sopenharmony_ci{
74906f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getAppNativeMemInfo");
75006f6ba60Sopenharmony_ci    auto nativeInterface = HidebugNativeInterface::CreateInstance();
75106f6ba60Sopenharmony_ci    if (!nativeInterface) {
75206f6ba60Sopenharmony_ci        return CreateUndefined(env);
75306f6ba60Sopenharmony_ci    }
75406f6ba60Sopenharmony_ci
75506f6ba60Sopenharmony_ci    auto nativeMemInfo = nativeInterface->GetAppNativeMemInfo();
75606f6ba60Sopenharmony_ci    if (!nativeMemInfo) {
75706f6ba60Sopenharmony_ci        return CreateUndefined(env);
75806f6ba60Sopenharmony_ci    }
75906f6ba60Sopenharmony_ci
76006f6ba60Sopenharmony_ci    napi_value memInfo;
76106f6ba60Sopenharmony_ci    napi_create_object(env, &memInfo);
76206f6ba60Sopenharmony_ci
76306f6ba60Sopenharmony_ci    napi_value pss;
76406f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->pss, &pss);
76506f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "pss", pss);
76606f6ba60Sopenharmony_ci
76706f6ba60Sopenharmony_ci    napi_value rss;
76806f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->rss, &rss);
76906f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "rss", rss);
77006f6ba60Sopenharmony_ci
77106f6ba60Sopenharmony_ci    napi_value sharedDirty;
77206f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->sharedDirty, &sharedDirty);
77306f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "sharedDirty", sharedDirty);
77406f6ba60Sopenharmony_ci
77506f6ba60Sopenharmony_ci    napi_value privateDirty;
77606f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->privateDirty, &privateDirty);
77706f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "privateDirty", privateDirty);
77806f6ba60Sopenharmony_ci
77906f6ba60Sopenharmony_ci    napi_value sharedClean;
78006f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->sharedClean, &sharedClean);
78106f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "sharedClean", sharedClean);
78206f6ba60Sopenharmony_ci
78306f6ba60Sopenharmony_ci    napi_value privateClean;
78406f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->privateClean, &privateClean);
78506f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "privateClean", privateClean);
78606f6ba60Sopenharmony_ci
78706f6ba60Sopenharmony_ci    napi_value vss;
78806f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, nativeMemInfo->vss, &vss);
78906f6ba60Sopenharmony_ci    napi_set_named_property(env, memInfo, "vss", vss);
79006f6ba60Sopenharmony_ci
79106f6ba60Sopenharmony_ci    return memInfo;
79206f6ba60Sopenharmony_ci}
79306f6ba60Sopenharmony_ci
79406f6ba60Sopenharmony_cinapi_value GetSystemMemInfo(napi_env env, napi_callback_info info)
79506f6ba60Sopenharmony_ci{
79606f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getSystemMemInfo");
79706f6ba60Sopenharmony_ci    auto nativeInterface = HidebugNativeInterface::CreateInstance();
79806f6ba60Sopenharmony_ci    if (!nativeInterface) {
79906f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
80006f6ba60Sopenharmony_ci        return CreateUndefined(env);
80106f6ba60Sopenharmony_ci    }
80206f6ba60Sopenharmony_ci
80306f6ba60Sopenharmony_ci    auto systemMemInfo = nativeInterface->GetSystemMemInfo();
80406f6ba60Sopenharmony_ci    if (!systemMemInfo) {
80506f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
80606f6ba60Sopenharmony_ci        return CreateUndefined(env);
80706f6ba60Sopenharmony_ci    }
80806f6ba60Sopenharmony_ci
80906f6ba60Sopenharmony_ci    napi_value sysMemInfo;
81006f6ba60Sopenharmony_ci    napi_create_object(env, &sysMemInfo);
81106f6ba60Sopenharmony_ci
81206f6ba60Sopenharmony_ci    napi_value totalMem;
81306f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, systemMemInfo->memTotal, &totalMem);
81406f6ba60Sopenharmony_ci    napi_set_named_property(env, sysMemInfo, "totalMem", totalMem);
81506f6ba60Sopenharmony_ci
81606f6ba60Sopenharmony_ci    napi_value freeMem;
81706f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, systemMemInfo->memFree, &freeMem);
81806f6ba60Sopenharmony_ci    napi_set_named_property(env, sysMemInfo, "freeMem", freeMem);
81906f6ba60Sopenharmony_ci
82006f6ba60Sopenharmony_ci    napi_value availableMem;
82106f6ba60Sopenharmony_ci    napi_create_bigint_uint64(env, systemMemInfo->memAvailable, &availableMem);
82206f6ba60Sopenharmony_ci    napi_set_named_property(env, sysMemInfo, "availableMem", availableMem);
82306f6ba60Sopenharmony_ci    return sysMemInfo;
82406f6ba60Sopenharmony_ci}
82506f6ba60Sopenharmony_ci
82606f6ba60Sopenharmony_cinapi_value StartAppTraceCapture(napi_env env, napi_callback_info info)
82706f6ba60Sopenharmony_ci{
82806f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("startAppTraceCapture");
82906f6ba60Sopenharmony_ci    napi_value result;
83006f6ba60Sopenharmony_ci    uint32_t traceFlag = 0;
83106f6ba60Sopenharmony_ci    uint32_t limitSize = 0;
83206f6ba60Sopenharmony_ci    std::vector<uint64_t> tags;
83306f6ba60Sopenharmony_ci    if (!GetTraceParam(env, info, traceFlag, limitSize, tags)) {
83406f6ba60Sopenharmony_ci        std::string paramErrorMessage = "Invalid argument";
83506f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
83606f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
83706f6ba60Sopenharmony_ci    }
83806f6ba60Sopenharmony_ci    uint64_t tag = std::accumulate(tags.begin(), tags.end(), 0ull, [](uint64_t a, uint64_t b) { return a | b; });
83906f6ba60Sopenharmony_ci    std::string file;
84006f6ba60Sopenharmony_ci    auto nativeInterface = HidebugNativeInterface::CreateInstance();
84106f6ba60Sopenharmony_ci    if (!nativeInterface) {
84206f6ba60Sopenharmony_ci        std::string errorMessage = "The status of the trace is abnormal";
84306f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
84406f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), errorMessage.c_str());
84506f6ba60Sopenharmony_ci        return CreateUndefined(env);
84606f6ba60Sopenharmony_ci    }
84706f6ba60Sopenharmony_ci    auto ret = nativeInterface->StartAppTraceCapture(tag, traceFlag, limitSize, file);
84806f6ba60Sopenharmony_ci    if (ret == HIDEBUG_SUCCESS) {
84906f6ba60Sopenharmony_ci        napi_create_string_utf8(env, file.c_str(), NAPI_AUTO_LENGTH, &result);
85006f6ba60Sopenharmony_ci        return result;
85106f6ba60Sopenharmony_ci    }
85206f6ba60Sopenharmony_ci    if (ret == HIDEBUG_INVALID_ARGUMENT) {
85306f6ba60Sopenharmony_ci        std::string errorMessage = "Invalid argument";
85406f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
85506f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), errorMessage.c_str());
85606f6ba60Sopenharmony_ci    }
85706f6ba60Sopenharmony_ci    if (ret == HIDEBUG_TRACE_CAPTURED_ALREADY) {
85806f6ba60Sopenharmony_ci        std::string errorMessage = "Capture trace already enabled.";
85906f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::HAVA_ALREADY_TRACE);
86006f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::HAVA_ALREADY_TRACE).c_str(), errorMessage.c_str());
86106f6ba60Sopenharmony_ci    }
86206f6ba60Sopenharmony_ci    if (ret == HIDEBUG_NO_PERMISSION) {
86306f6ba60Sopenharmony_ci        std::string errorMessage = "No write permission on the file.";
86406f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::WITHOUT_WRITE_PERMISSON);
86506f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::WITHOUT_WRITE_PERMISSON).c_str(), errorMessage.c_str());
86606f6ba60Sopenharmony_ci    }
86706f6ba60Sopenharmony_ci    std::string errorMessage = "Abnormal trace status.";
86806f6ba60Sopenharmony_ci    apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
86906f6ba60Sopenharmony_ci    napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), errorMessage.c_str());
87006f6ba60Sopenharmony_ci    return CreateUndefined(env);
87106f6ba60Sopenharmony_ci}
87206f6ba60Sopenharmony_ci
87306f6ba60Sopenharmony_cinapi_value StopAppTraceCapture(napi_env env, napi_callback_info info)
87406f6ba60Sopenharmony_ci{
87506f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("stopAppTraceCapture");
87606f6ba60Sopenharmony_ci    auto nativeInterface = HidebugNativeInterface::CreateInstance();
87706f6ba60Sopenharmony_ci    if (!nativeInterface) {
87806f6ba60Sopenharmony_ci        return CreateUndefined(env);
87906f6ba60Sopenharmony_ci    }
88006f6ba60Sopenharmony_ci    auto ret = nativeInterface->StopAppTraceCapture();
88106f6ba60Sopenharmony_ci    if (ret == HIDEBUG_TRACE_ABNORMAL) {
88206f6ba60Sopenharmony_ci        std::string errorMessage = "The status of the trace is abnormal";
88306f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
88406f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), errorMessage.c_str());
88506f6ba60Sopenharmony_ci    }
88606f6ba60Sopenharmony_ci    if (ret == HIDEBUG_NO_TRACE_RUNNING) {
88706f6ba60Sopenharmony_ci        std::string errorMessage = "No capture trace running";
88806f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::NO_CAPTURE_TRACE_RUNNING);
88906f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::NO_CAPTURE_TRACE_RUNNING).c_str(), errorMessage.c_str());
89006f6ba60Sopenharmony_ci    }
89106f6ba60Sopenharmony_ci    return CreateUndefined(env);
89206f6ba60Sopenharmony_ci}
89306f6ba60Sopenharmony_ci
89406f6ba60Sopenharmony_cinapi_value GetVMRuntimeStats(napi_env env, napi_callback_info info)
89506f6ba60Sopenharmony_ci{
89606f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getVMRuntimeStats");
89706f6ba60Sopenharmony_ci    napi_value vmRunTimeStats;
89806f6ba60Sopenharmony_ci    napi_create_object(env, &vmRunTimeStats);
89906f6ba60Sopenharmony_ci    for (const auto &[k, v] : GC::vmGcMap_) {
90006f6ba60Sopenharmony_ci        napi_set_named_property(env, vmRunTimeStats, k.c_str(), v(env));
90106f6ba60Sopenharmony_ci    }
90206f6ba60Sopenharmony_ci    return vmRunTimeStats;
90306f6ba60Sopenharmony_ci}
90406f6ba60Sopenharmony_ci
90506f6ba60Sopenharmony_cinapi_value GetVMRuntimeStat(napi_env env, napi_callback_info info)
90606f6ba60Sopenharmony_ci{
90706f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getVMRuntimeStat");
90806f6ba60Sopenharmony_ci    std::string param;
90906f6ba60Sopenharmony_ci    if (!GetTheOnlyStringParam(env, info, param)) {
91006f6ba60Sopenharmony_ci        std::string paramErrorMessage = "Invalid parameter, a string parameter required.";
91106f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
91206f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
91306f6ba60Sopenharmony_ci        return CreateUndefined(env);
91406f6ba60Sopenharmony_ci    }
91506f6ba60Sopenharmony_ci    if (GC::vmGcMap_.find(param) == GC::vmGcMap_.end()) {
91606f6ba60Sopenharmony_ci        std::string paramErrorMessage = "Invalid parameter, unknown property.";
91706f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
91806f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
91906f6ba60Sopenharmony_ci        return CreateUndefined(env);
92006f6ba60Sopenharmony_ci    }
92106f6ba60Sopenharmony_ci    return GC::vmGcMap_.at(param)(env);
92206f6ba60Sopenharmony_ci}
92306f6ba60Sopenharmony_ci
92406f6ba60Sopenharmony_cistatic bool JudgeValueRange(const std::string &type, int32_t value)
92506f6ba60Sopenharmony_ci{
92606f6ba60Sopenharmony_ci    if (limitResource.find(type) != limitResource.end()) {
92706f6ba60Sopenharmony_ci        auto limitValue = limitResource[type];
92806f6ba60Sopenharmony_ci        if (value >= limitValue.first && value <= limitValue.second) {
92906f6ba60Sopenharmony_ci            return true;
93006f6ba60Sopenharmony_ci        }
93106f6ba60Sopenharmony_ci    }
93206f6ba60Sopenharmony_ci    return false;
93306f6ba60Sopenharmony_ci}
93406f6ba60Sopenharmony_ci
93506f6ba60Sopenharmony_cistatic bool GetAppResourceLimitParam(napi_env env, napi_callback_info info, std::string& type,
93606f6ba60Sopenharmony_ci    int32_t& value, bool& enabledDebugLog)
93706f6ba60Sopenharmony_ci{
93806f6ba60Sopenharmony_ci    const int valueNum = 3;
93906f6ba60Sopenharmony_ci    size_t argc = valueNum;
94006f6ba60Sopenharmony_ci    napi_value argv[valueNum] = { nullptr };
94106f6ba60Sopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
94206f6ba60Sopenharmony_ci    if (argc != valueNum) {
94306f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. Invalid numbers of params!");
94406f6ba60Sopenharmony_ci        return false;
94506f6ba60Sopenharmony_ci    }
94606f6ba60Sopenharmony_ci    if (!MatchValueType(env, argv[FIRST_POS], napi_string) &&
94706f6ba60Sopenharmony_ci        !MatchValueType(env, argv[SECOND_POS], napi_number) &&
94806f6ba60Sopenharmony_ci        !MatchValueType(env, argv[THIRD_POS], napi_boolean)) {
94906f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam params type error.");
95006f6ba60Sopenharmony_ci        return false;
95106f6ba60Sopenharmony_ci    }
95206f6ba60Sopenharmony_ci    size_t bufLen = 0;
95306f6ba60Sopenharmony_ci    napi_status status = napi_get_value_string_utf8(env, argv[FIRST_POS], nullptr, 0, &bufLen);
95406f6ba60Sopenharmony_ci    if (status != napi_ok) {
95506f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. Get input filename param length failed.");
95606f6ba60Sopenharmony_ci        return false;
95706f6ba60Sopenharmony_ci    }
95806f6ba60Sopenharmony_ci    const int bufMax = 128;
95906f6ba60Sopenharmony_ci    if (bufLen > bufMax || bufLen == 0) {
96006f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. input filename param length is illegal.");
96106f6ba60Sopenharmony_ci        return false;
96206f6ba60Sopenharmony_ci    }
96306f6ba60Sopenharmony_ci    std::vector<char> buf(bufLen + 1, 0);
96406f6ba60Sopenharmony_ci    napi_get_value_string_utf8(env, argv[FIRST_POS], buf.data(), bufLen + 1, &bufLen);
96506f6ba60Sopenharmony_ci    type = std::string(buf.data());
96606f6ba60Sopenharmony_ci    if (type.empty()) {
96706f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. Resource type is invalid.");
96806f6ba60Sopenharmony_ci        return false;
96906f6ba60Sopenharmony_ci    }
97006f6ba60Sopenharmony_ci    auto findType = std::find(RESOURCE_TYPE_LIST.begin(), RESOURCE_TYPE_LIST.end(), type);
97106f6ba60Sopenharmony_ci    if (findType == RESOURCE_TYPE_LIST.end()) {
97206f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. Resource type is invalid.");
97306f6ba60Sopenharmony_ci        return false;
97406f6ba60Sopenharmony_ci    }
97506f6ba60Sopenharmony_ci    if (napi_get_value_int32(env, argv[SECOND_POS], &value) != napi_ok || value < 0) {
97606f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. Input value error.");
97706f6ba60Sopenharmony_ci        return false;
97806f6ba60Sopenharmony_ci    }
97906f6ba60Sopenharmony_ci    if (!JudgeValueRange(type, value)) {
98006f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. The value range is invalid.");
98106f6ba60Sopenharmony_ci        return false;
98206f6ba60Sopenharmony_ci    }
98306f6ba60Sopenharmony_ci    if (napi_get_value_bool(env, argv[THIRD_POS], &enabledDebugLog) != napi_ok) {
98406f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "GetAppResourceLimitParam failed. Get input enabledDebugLog failed.");
98506f6ba60Sopenharmony_ci        return false;
98606f6ba60Sopenharmony_ci    }
98706f6ba60Sopenharmony_ci    return true;
98806f6ba60Sopenharmony_ci}
98906f6ba60Sopenharmony_ci
99006f6ba60Sopenharmony_cistatic bool CreateSanBoxDir()
99106f6ba60Sopenharmony_ci{
99206f6ba60Sopenharmony_ci    constexpr mode_t defaultLogDirMode = 0x0770;
99306f6ba60Sopenharmony_ci    const std::string reourceLimitDir = "/data/storage/el2/log/resourcelimit/";
99406f6ba60Sopenharmony_ci    if (!OHOS::FileExists(reourceLimitDir)) {
99506f6ba60Sopenharmony_ci        OHOS::ForceCreateDirectory(reourceLimitDir);
99606f6ba60Sopenharmony_ci        OHOS::ChangeModeDirectory(reourceLimitDir, defaultLogDirMode);
99706f6ba60Sopenharmony_ci    }
99806f6ba60Sopenharmony_ci    if (OHOS::StorageDaemon::AclSetAccess(reourceLimitDir, "g:1201:rwx") != 0) {
99906f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "CreateSanBoxDir Failed to AclSetAccess");
100006f6ba60Sopenharmony_ci        return false;
100106f6ba60Sopenharmony_ci    }
100206f6ba60Sopenharmony_ci    return true;
100306f6ba60Sopenharmony_ci}
100406f6ba60Sopenharmony_ci
100506f6ba60Sopenharmony_cistatic bool CheckVersionType(const std::string& type, const std::string& key)
100606f6ba60Sopenharmony_ci{
100706f6ba60Sopenharmony_ci    auto versionType = OHOS::system::GetParameter(key, "unknown");
100806f6ba60Sopenharmony_ci    return (versionType.find(type) != std::string::npos);
100906f6ba60Sopenharmony_ci}
101006f6ba60Sopenharmony_ci
101106f6ba60Sopenharmony_cinapi_value SetAppResourceLimit(napi_env env, napi_callback_info info)
101206f6ba60Sopenharmony_ci{
101306f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("setAppResourceLimit");
101406f6ba60Sopenharmony_ci    if (!CheckVersionType("beta", KEY_HIVIEW_USER_TYPE) &&
101506f6ba60Sopenharmony_ci        !CheckVersionType("enable", KEY_HIVIEW_DEVELOP_TYPE)) {
101606f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "SetAppResourceLimit failed. Not developer options or beta versions");
101706f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::VERSION_ERROR);
101806f6ba60Sopenharmony_ci        return CreateUndefined(env);
101906f6ba60Sopenharmony_ci    }
102006f6ba60Sopenharmony_ci    std::string type = "";
102106f6ba60Sopenharmony_ci    int32_t value = 0;
102206f6ba60Sopenharmony_ci    bool enabledDebugLog = false;
102306f6ba60Sopenharmony_ci    if (!GetAppResourceLimitParam(env, info, type, value, enabledDebugLog)) {
102406f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
102506f6ba60Sopenharmony_ci        return CreateUndefined(env);
102606f6ba60Sopenharmony_ci    }
102706f6ba60Sopenharmony_ci    if (type == "js_heap") { // js_heap set value
102806f6ba60Sopenharmony_ci        NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
102906f6ba60Sopenharmony_ci        engine->SetJsDumpThresholds(value);
103006f6ba60Sopenharmony_ci    }
103106f6ba60Sopenharmony_ci    auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103206f6ba60Sopenharmony_ci    if (!abilityManager) {
103306f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_ABILITY_NOT_FOUND);
103406f6ba60Sopenharmony_ci        return CreateUndefined(env);
103506f6ba60Sopenharmony_ci    }
103606f6ba60Sopenharmony_ci    sptr<IRemoteObject> remoteObject = abilityManager->CheckSystemAbility(DFX_SYS_HIVIEW_ABILITY_ID);
103706f6ba60Sopenharmony_ci    if (remoteObject == nullptr) {
103806f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "SetAppResourceLimit failed. No this system ability.");
103906f6ba60Sopenharmony_ci        std::string idErrorMessage = "system ability is not exist.";
104006f6ba60Sopenharmony_ci        napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), idErrorMessage.c_str());
104106f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
104206f6ba60Sopenharmony_ci        return CreateUndefined(env);
104306f6ba60Sopenharmony_ci    }
104406f6ba60Sopenharmony_ci    auto result = HidebugNativeInterface::CreateInstance()->GetMemoryLeakResource(type, value, enabledDebugLog);
104506f6ba60Sopenharmony_ci    if (result == MemoryState::MEMORY_FAILED) {
104606f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
104706f6ba60Sopenharmony_ci        return CreateUndefined(env);
104806f6ba60Sopenharmony_ci    }
104906f6ba60Sopenharmony_ci    CreateSanBoxDir();
105006f6ba60Sopenharmony_ci    return CreateUndefined(env);
105106f6ba60Sopenharmony_ci}
105206f6ba60Sopenharmony_ci
105306f6ba60Sopenharmony_cinapi_value IsDebugState(napi_env env, napi_callback_info info)
105406f6ba60Sopenharmony_ci{
105506f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("isDebugState");
105606f6ba60Sopenharmony_ci    NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
105706f6ba60Sopenharmony_ci    if (engine == nullptr) {
105806f6ba60Sopenharmony_ci        apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
105906f6ba60Sopenharmony_ci        return CreateUndefined(env);
106006f6ba60Sopenharmony_ci    }
106106f6ba60Sopenharmony_ci
106206f6ba60Sopenharmony_ci    bool debugState = engine->GetIsDebugModeEnabled() ||
106306f6ba60Sopenharmony_ci        HidebugNativeInterface::CreateInstance()->IsDebuggerConnected();
106406f6ba60Sopenharmony_ci
106506f6ba60Sopenharmony_ci    napi_value result = nullptr;
106606f6ba60Sopenharmony_ci    napi_get_boolean(env, debugState, &result);
106706f6ba60Sopenharmony_ci    return result;
106806f6ba60Sopenharmony_ci}
106906f6ba60Sopenharmony_ci
107006f6ba60Sopenharmony_ci
107106f6ba60Sopenharmony_ciclass GraphicAsyncTask : public AsyncTask {
107206f6ba60Sopenharmony_cipublic:
107306f6ba60Sopenharmony_ci    GraphicAsyncTask() : AsyncTask("graphicAsyncTask"), apiInvokeRecorder_("getGraphicsMemory") {}
107406f6ba60Sopenharmony_ci
107506f6ba60Sopenharmony_ciprotected:
107606f6ba60Sopenharmony_ci    void Work(napi_env env) override
107706f6ba60Sopenharmony_ci    {
107806f6ba60Sopenharmony_ci        result_ = HidebugNativeInterface::CreateInstance()->GetGraphicsMemory();
107906f6ba60Sopenharmony_ci    }
108006f6ba60Sopenharmony_ci
108106f6ba60Sopenharmony_ci    void Done(napi_env env, napi_status status) override
108206f6ba60Sopenharmony_ci    {
108306f6ba60Sopenharmony_ci        if (result_.has_value()) {
108406f6ba60Sopenharmony_ci            napi_value ret;
108506f6ba60Sopenharmony_ci            napi_create_int32(env, result_.value(), &ret);
108606f6ba60Sopenharmony_ci            napi_resolve_deferred(env, deferred_, ret);
108706f6ba60Sopenharmony_ci        } else {
108806f6ba60Sopenharmony_ci            constexpr const char* errMsg = "Failed to get the application memory due to a remote exception";
108906f6ba60Sopenharmony_ci            apiInvokeRecorder_.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
109006f6ba60Sopenharmony_ci            napi_reject_deferred(env, deferred_,
109106f6ba60Sopenharmony_ci                CreateErrorMessage(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL), errMsg));
109206f6ba60Sopenharmony_ci        }
109306f6ba60Sopenharmony_ci    }
109406f6ba60Sopenharmony_ci
109506f6ba60Sopenharmony_ciprivate:
109606f6ba60Sopenharmony_ci    std::optional<int> result_{};
109706f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder_;
109806f6ba60Sopenharmony_ci};
109906f6ba60Sopenharmony_ci
110006f6ba60Sopenharmony_cinapi_value GetGraphicsMemory(napi_env env, napi_callback_info info)
110106f6ba60Sopenharmony_ci{
110206f6ba60Sopenharmony_ci    return AsyncTask::GetPromise<GraphicAsyncTask>(env);
110306f6ba60Sopenharmony_ci}
110406f6ba60Sopenharmony_ci
110506f6ba60Sopenharmony_cinapi_value GetGraphicsMemorySync(napi_env env, napi_callback_info info)
110606f6ba60Sopenharmony_ci{
110706f6ba60Sopenharmony_ci    ApiInvokeRecorder apiInvokeRecorder("getGraphicsMemorySync");
110806f6ba60Sopenharmony_ci    std::optional<int32_t> result = HidebugNativeInterface::CreateInstance()->GetGraphicsMemory();
110906f6ba60Sopenharmony_ci    if (result.has_value()) {
111006f6ba60Sopenharmony_ci        napi_value ret;
111106f6ba60Sopenharmony_ci        napi_create_int32(env, result.value(), &ret);
111206f6ba60Sopenharmony_ci        return ret;
111306f6ba60Sopenharmony_ci    }
111406f6ba60Sopenharmony_ci    constexpr const char* errMsg = "Failed to get the application memory due to a remote exception";
111506f6ba60Sopenharmony_ci    napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), errMsg);
111606f6ba60Sopenharmony_ci    apiInvokeRecorder.SetErrorCode(ErrorCode::SYSTEM_STATUS_ABNORMAL);
111706f6ba60Sopenharmony_ci    return CreateUndefined(env);
111806f6ba60Sopenharmony_ci}
111906f6ba60Sopenharmony_ci
112006f6ba60Sopenharmony_cinapi_value DeclareHiDebugInterface(napi_env env, napi_value exports)
112106f6ba60Sopenharmony_ci{
112206f6ba60Sopenharmony_ci    ApiInvokeRecorder::InitProcessor();
112306f6ba60Sopenharmony_ci    napi_property_descriptor desc[] = {
112406f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("startProfiling", StartProfiling),
112506f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("stopProfiling", StopProfiling),
112606f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("dumpHeapData", DumpHeapData),
112706f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("startJsCpuProfiling", StartJsCpuProfiling),
112806f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("stopJsCpuProfiling", StopJsCpuProfiling),
112906f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("dumpJsHeapData", DumpJsHeapData),
113006f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getPss", GetPss),
113106f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getSharedDirty", GetSharedDirty),
113206f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getPrivateDirty", GetPrivateDirty),
113306f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getCpuUsage", GetCpuUsage),
113406f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getServiceDump", GetServiceDump),
113506f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getNativeHeapSize", GetNativeHeapSize),
113606f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getNativeHeapAllocatedSize", GetNativeHeapAllocatedSize),
113706f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getNativeHeapFreeSize", GetNativeHeapFreeSize),
113806f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getVss", GetVss),
113906f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("removeNapiWrap", RemoveNapiWrap),
114006f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getAppVMMemoryInfo", GetAppVMMemoryInfo),
114106f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getAppThreadCpuUsage", GetAppThreadCpuUsage),
114206f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getSystemCpuUsage", GetSystemCpuUsage),
114306f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getAppMemoryLimit", GetAppMemoryLimit),
114406f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getAppNativeMemInfo", GetAppNativeMemInfo),
114506f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getSystemMemInfo", GetSystemMemInfo),
114606f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("startAppTraceCapture", StartAppTraceCapture),
114706f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("stopAppTraceCapture", StopAppTraceCapture),
114806f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getVMRuntimeStats", GetVMRuntimeStats),
114906f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getVMRuntimeStat", GetVMRuntimeStat),
115006f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("setAppResourceLimit", SetAppResourceLimit),
115106f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("isDebugState", IsDebugState),
115206f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getGraphicsMemory", GetGraphicsMemory),
115306f6ba60Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getGraphicsMemorySync", GetGraphicsMemorySync),
115406f6ba60Sopenharmony_ci    };
115506f6ba60Sopenharmony_ci    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
115606f6ba60Sopenharmony_ci    InitNapiClass(env, exports);
115706f6ba60Sopenharmony_ci    return exports;
115806f6ba60Sopenharmony_ci}
115906f6ba60Sopenharmony_ci
116006f6ba60Sopenharmony_cistatic napi_module hidebugModule = {
116106f6ba60Sopenharmony_ci    .nm_version = 1,
116206f6ba60Sopenharmony_ci    .nm_flags = 0,
116306f6ba60Sopenharmony_ci    .nm_filename = nullptr,
116406f6ba60Sopenharmony_ci    .nm_register_func = HiviewDFX::DeclareHiDebugInterface,
116506f6ba60Sopenharmony_ci    .nm_modname = "hidebug",
116606f6ba60Sopenharmony_ci    .nm_priv = ((void *)0),
116706f6ba60Sopenharmony_ci    .reserved = {0}
116806f6ba60Sopenharmony_ci};
116906f6ba60Sopenharmony_ci
117006f6ba60Sopenharmony_ciextern "C" __attribute__((constructor)) void HiDebugRegisterModule(void)
117106f6ba60Sopenharmony_ci{
117206f6ba60Sopenharmony_ci    napi_module_register(&hidebugModule);
117306f6ba60Sopenharmony_ci}
117406f6ba60Sopenharmony_ci} // HiviewDFX
117506f6ba60Sopenharmony_ci} // OHOS
1176