106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
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 "hidebug_ffi.h"
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include <numeric>
1906f6ba60Sopenharmony_ci#include <sys/types.h>
2006f6ba60Sopenharmony_ci#include <sys/stat.h>
2106f6ba60Sopenharmony_ci#include <fcntl.h>
2206f6ba60Sopenharmony_ci#include <memory>
2306f6ba60Sopenharmony_ci#include <unistd.h>
2406f6ba60Sopenharmony_ci#include <malloc.h>
2506f6ba60Sopenharmony_ci#include <codecvt>
2606f6ba60Sopenharmony_ci#include <string>
2706f6ba60Sopenharmony_ci#include <vector>
2806f6ba60Sopenharmony_ci#include <parameters.h>
2906f6ba60Sopenharmony_ci
3006f6ba60Sopenharmony_ci#include "cpu_collector.h"
3106f6ba60Sopenharmony_ci#include "dump_usage.h"
3206f6ba60Sopenharmony_ci#include "file_ex.h"
3306f6ba60Sopenharmony_ci#include "directory_ex.h"
3406f6ba60Sopenharmony_ci#include "storage_acl.h"
3506f6ba60Sopenharmony_ci#include "hidebug_native_interface.h"
3606f6ba60Sopenharmony_ci#include "memory_collector.h"
3706f6ba60Sopenharmony_ci#include "hilog/log.h"
3806f6ba60Sopenharmony_ci#include "system_ability_definition.h"
3906f6ba60Sopenharmony_ci#include "iservice_registry.h"
4006f6ba60Sopenharmony_ci
4106f6ba60Sopenharmony_cinamespace OHOS::HiviewDFX {
4206f6ba60Sopenharmony_ci
4306f6ba60Sopenharmony_ci#undef LOG_DOMAIN
4406f6ba60Sopenharmony_ci#define LOG_DOMAIN 0xD002D0A
4506f6ba60Sopenharmony_ci#undef LOG_TAG
4606f6ba60Sopenharmony_ci#define LOG_TAG "CJ_HiDebug"
4706f6ba60Sopenharmony_ci
4806f6ba60Sopenharmony_ciconst std::string KEY_HIVIEW_USER_TYPE = "const.logsystem.versiontype";
4906f6ba60Sopenharmony_ciconst std::string KEY_HIVIEW_DEVELOP_TYPE = "persist.hiview.leak_detector";
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_cienum ErrorCode {
5206f6ba60Sopenharmony_ci    MEM_ERROR = 1,
5306f6ba60Sopenharmony_ci    PERMISSION_ERROR = 201,
5406f6ba60Sopenharmony_ci    PARAMETER_ERROR = 401,
5506f6ba60Sopenharmony_ci    VERSION_ERROR = 801,
5606f6ba60Sopenharmony_ci    SYSTEM_ABILITY_NOT_FOUND = 11400101,
5706f6ba60Sopenharmony_ci    HAVA_ALREADY_TRACE = 11400102,
5806f6ba60Sopenharmony_ci    WITHOUT_WRITE_PERMISSON = 11400103,
5906f6ba60Sopenharmony_ci    SYSTEM_STATUS_ABNORMAL = 11400104,
6006f6ba60Sopenharmony_ci    NO_CAPTURE_TRACE_RUNNING = 11400105,
6106f6ba60Sopenharmony_ci};
6206f6ba60Sopenharmony_ci
6306f6ba60Sopenharmony_cistatic bool CheckVersionType(const std::string& type, const std::string& key)
6406f6ba60Sopenharmony_ci{
6506f6ba60Sopenharmony_ci    auto versionType = OHOS::system::GetParameter(key, "unknown");
6606f6ba60Sopenharmony_ci    return (versionType.find(type) != std::string::npos);
6706f6ba60Sopenharmony_ci}
6806f6ba60Sopenharmony_ci
6906f6ba60Sopenharmony_cistatic bool CreateSanBoxDir()
7006f6ba60Sopenharmony_ci{
7106f6ba60Sopenharmony_ci    constexpr mode_t defaultLogDirMode = 0x0770;
7206f6ba60Sopenharmony_ci    const std::string reourceLimitDir = "/data/storage/el2/log/resourcelimit/";
7306f6ba60Sopenharmony_ci    if (!OHOS::FileExists(reourceLimitDir)) {
7406f6ba60Sopenharmony_ci        OHOS::ForceCreateDirectory(reourceLimitDir);
7506f6ba60Sopenharmony_ci        OHOS::ChangeModeDirectory(reourceLimitDir, defaultLogDirMode);
7606f6ba60Sopenharmony_ci    }
7706f6ba60Sopenharmony_ci    if (OHOS::StorageDaemon::AclSetAccess(reourceLimitDir, "g:1201:rwx") != 0) {
7806f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "CreateSanBoxDir Failed to AclSetAccess");
7906f6ba60Sopenharmony_ci        return false;
8006f6ba60Sopenharmony_ci    }
8106f6ba60Sopenharmony_ci    return true;
8206f6ba60Sopenharmony_ci}
8306f6ba60Sopenharmony_ci
8406f6ba60Sopenharmony_ciextern "C" {
8506f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetPss()
8606f6ba60Sopenharmony_ci    {
8706f6ba60Sopenharmony_ci        std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
8806f6ba60Sopenharmony_ci        if (collector != nullptr) {
8906f6ba60Sopenharmony_ci            int pid = getprocpid();
9006f6ba60Sopenharmony_ci            auto collectResult = collector->CollectProcessMemory(pid);
9106f6ba60Sopenharmony_ci            int32_t pssInfo = collectResult.data.pss + collectResult.data.swapPss;
9206f6ba60Sopenharmony_ci            return static_cast<uint64_t>(pssInfo);
9306f6ba60Sopenharmony_ci        } else {
9406f6ba60Sopenharmony_ci            return 0;
9506f6ba60Sopenharmony_ci        }
9606f6ba60Sopenharmony_ci    }
9706f6ba60Sopenharmony_ci
9806f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetVss()
9906f6ba60Sopenharmony_ci    {
10006f6ba60Sopenharmony_ci        std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
10106f6ba60Sopenharmony_ci        if (collector != nullptr) {
10206f6ba60Sopenharmony_ci            pid_t pid = getprocpid();
10306f6ba60Sopenharmony_ci            auto collectResult = collector->CollectProcessVss(pid);
10406f6ba60Sopenharmony_ci            uint64_t vssInfo = collectResult.data;
10506f6ba60Sopenharmony_ci            return vssInfo;
10606f6ba60Sopenharmony_ci        } else {
10706f6ba60Sopenharmony_ci            return 0;
10806f6ba60Sopenharmony_ci        }
10906f6ba60Sopenharmony_ci    }
11006f6ba60Sopenharmony_ci
11106f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetNativeHeapSize()
11206f6ba60Sopenharmony_ci    {
11306f6ba60Sopenharmony_ci        struct mallinfo mi = mallinfo();
11406f6ba60Sopenharmony_ci        return static_cast<uint64_t>(mi.uordblks + mi.fordblks);
11506f6ba60Sopenharmony_ci    }
11606f6ba60Sopenharmony_ci
11706f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetNativeHeapAllocatedSize()
11806f6ba60Sopenharmony_ci    {
11906f6ba60Sopenharmony_ci        struct mallinfo mi = mallinfo();
12006f6ba60Sopenharmony_ci        return static_cast<uint64_t>(mi.uordblks);
12106f6ba60Sopenharmony_ci    }
12206f6ba60Sopenharmony_ci
12306f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetNativeHeapFreeSize()
12406f6ba60Sopenharmony_ci    {
12506f6ba60Sopenharmony_ci        struct mallinfo mi = mallinfo();
12606f6ba60Sopenharmony_ci        return static_cast<uint64_t>(mi.fordblks);
12706f6ba60Sopenharmony_ci    }
12806f6ba60Sopenharmony_ci
12906f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetSharedDirty()
13006f6ba60Sopenharmony_ci    {
13106f6ba60Sopenharmony_ci        std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
13206f6ba60Sopenharmony_ci        if (collector != nullptr) {
13306f6ba60Sopenharmony_ci            int pid = getprocpid();
13406f6ba60Sopenharmony_ci            auto collectResult = collector->CollectProcessMemory(pid);
13506f6ba60Sopenharmony_ci            int32_t sharedDirtyInfo = collectResult.data.sharedDirty;
13606f6ba60Sopenharmony_ci            return static_cast<uint64_t>(sharedDirtyInfo);
13706f6ba60Sopenharmony_ci        } else {
13806f6ba60Sopenharmony_ci            return 0;
13906f6ba60Sopenharmony_ci        }
14006f6ba60Sopenharmony_ci    }
14106f6ba60Sopenharmony_ci
14206f6ba60Sopenharmony_ci    uint64_t FfiHidebugGetPrivateDirty()
14306f6ba60Sopenharmony_ci    {
14406f6ba60Sopenharmony_ci        std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
14506f6ba60Sopenharmony_ci        if (collector != nullptr) {
14606f6ba60Sopenharmony_ci            pid_t pid = getprocpid();
14706f6ba60Sopenharmony_ci            auto collectResult = collector->CollectProcessMemory(pid);
14806f6ba60Sopenharmony_ci            int32_t privateDirty = collectResult.data.privateDirty;
14906f6ba60Sopenharmony_ci            return static_cast<uint64_t>(privateDirty);
15006f6ba60Sopenharmony_ci        } else {
15106f6ba60Sopenharmony_ci            return 0;
15206f6ba60Sopenharmony_ci        }
15306f6ba60Sopenharmony_ci    }
15406f6ba60Sopenharmony_ci
15506f6ba60Sopenharmony_ci    double FfiHidebugGetCpuUsage()
15606f6ba60Sopenharmony_ci    {
15706f6ba60Sopenharmony_ci        std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
15806f6ba60Sopenharmony_ci        pid_t pid = getprocpid();
15906f6ba60Sopenharmony_ci        return dumpUsage->GetCpuUsage(pid);
16006f6ba60Sopenharmony_ci    }
16106f6ba60Sopenharmony_ci
16206f6ba60Sopenharmony_ci    double FfiHidebugGetSystemCpuUsage(int32_t &code)
16306f6ba60Sopenharmony_ci    {
16406f6ba60Sopenharmony_ci        auto cpuUsageOptional = HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage();
16506f6ba60Sopenharmony_ci        if (cpuUsageOptional.has_value()) {
16606f6ba60Sopenharmony_ci            return cpuUsageOptional.value();
16706f6ba60Sopenharmony_ci        }
16806f6ba60Sopenharmony_ci        code = ErrorCode::SYSTEM_STATUS_ABNORMAL;
16906f6ba60Sopenharmony_ci        return 0;
17006f6ba60Sopenharmony_ci    }
17106f6ba60Sopenharmony_ci
17206f6ba60Sopenharmony_ci    ThreadCpuUsageArr FfiHidebugGetAppThreadCpuUsage(int32_t &code)
17306f6ba60Sopenharmony_ci    {
17406f6ba60Sopenharmony_ci        ThreadCpuUsageArr arr{ .head = nullptr, .size = 0};
17506f6ba60Sopenharmony_ci        auto nativeInterface = HidebugNativeInterface::CreateInstance();
17606f6ba60Sopenharmony_ci        if (!nativeInterface) {
17706f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
17806f6ba60Sopenharmony_ci            return arr;
17906f6ba60Sopenharmony_ci        }
18006f6ba60Sopenharmony_ci        std::map<uint32_t, double> threadMap = nativeInterface->GetAppThreadCpuUsage();
18106f6ba60Sopenharmony_ci        auto size = threadMap.size();
18206f6ba60Sopenharmony_ci        if (size <= 0) {
18306f6ba60Sopenharmony_ci            return arr;
18406f6ba60Sopenharmony_ci        }
18506f6ba60Sopenharmony_ci        arr.head = static_cast<CThreadCpuUsage *>(malloc(sizeof(CThreadCpuUsage) * size));
18606f6ba60Sopenharmony_ci        if (arr.head == nullptr) {
18706f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
18806f6ba60Sopenharmony_ci            return arr;
18906f6ba60Sopenharmony_ci        }
19006f6ba60Sopenharmony_ci        size_t idx = 0;
19106f6ba60Sopenharmony_ci        for (const auto[id, usage] : threadMap) {
19206f6ba60Sopenharmony_ci            arr.head[idx] = CThreadCpuUsage{ .threadId = id, .cpuUsage = usage };
19306f6ba60Sopenharmony_ci            idx++;
19406f6ba60Sopenharmony_ci        }
19506f6ba60Sopenharmony_ci        return arr;
19606f6ba60Sopenharmony_ci    }
19706f6ba60Sopenharmony_ci
19806f6ba60Sopenharmony_ci    CSystemMemInfo FfiHidebugGetSystemMemInfo(int32_t &code)
19906f6ba60Sopenharmony_ci    {
20006f6ba60Sopenharmony_ci        CSystemMemInfo info{.totalMem = 0, .freeMem = 0, .availableMem = 0};
20106f6ba60Sopenharmony_ci        auto nativeInterface = HidebugNativeInterface::CreateInstance();
20206f6ba60Sopenharmony_ci        if (!nativeInterface) {
20306f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
20406f6ba60Sopenharmony_ci            return info;
20506f6ba60Sopenharmony_ci        }
20606f6ba60Sopenharmony_ci
20706f6ba60Sopenharmony_ci        auto systemMemInfo = nativeInterface->GetSystemMemInfo();
20806f6ba60Sopenharmony_ci        if (!systemMemInfo) {
20906f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
21006f6ba60Sopenharmony_ci            return info;
21106f6ba60Sopenharmony_ci        }
21206f6ba60Sopenharmony_ci
21306f6ba60Sopenharmony_ci        info.totalMem = systemMemInfo->memTotal;
21406f6ba60Sopenharmony_ci        info.freeMem = systemMemInfo->memFree;
21506f6ba60Sopenharmony_ci        info.availableMem = systemMemInfo->memAvailable;
21606f6ba60Sopenharmony_ci        return info;
21706f6ba60Sopenharmony_ci    }
21806f6ba60Sopenharmony_ci
21906f6ba60Sopenharmony_ci    CNativeMemInfo FfiHidebugGetAppNativeMemInfo(int32_t &code)
22006f6ba60Sopenharmony_ci    {
22106f6ba60Sopenharmony_ci        CNativeMemInfo info{};
22206f6ba60Sopenharmony_ci        auto nativeInterface = HidebugNativeInterface::CreateInstance();
22306f6ba60Sopenharmony_ci        if (!nativeInterface) {
22406f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
22506f6ba60Sopenharmony_ci            return info;
22606f6ba60Sopenharmony_ci        }
22706f6ba60Sopenharmony_ci
22806f6ba60Sopenharmony_ci        auto nativeMemInfo = nativeInterface->GetAppNativeMemInfo();
22906f6ba60Sopenharmony_ci        if (!nativeMemInfo) {
23006f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
23106f6ba60Sopenharmony_ci            return info;
23206f6ba60Sopenharmony_ci        }
23306f6ba60Sopenharmony_ci        info.pss = nativeMemInfo->pss;
23406f6ba60Sopenharmony_ci        info.vss = nativeMemInfo->vss;
23506f6ba60Sopenharmony_ci        info.rss = nativeMemInfo->rss;
23606f6ba60Sopenharmony_ci        info.sharedDirty = nativeMemInfo->sharedDirty;
23706f6ba60Sopenharmony_ci        info.privateDirty = nativeMemInfo->privateDirty;
23806f6ba60Sopenharmony_ci        info.sharedClean = nativeMemInfo->sharedClean;
23906f6ba60Sopenharmony_ci        info.privateClean = nativeMemInfo->privateClean;
24006f6ba60Sopenharmony_ci        return info;
24106f6ba60Sopenharmony_ci    }
24206f6ba60Sopenharmony_ci
24306f6ba60Sopenharmony_ci    CMemoryLimit FfiHidebugGetAppMemoryLimit(int32_t &code)
24406f6ba60Sopenharmony_ci    {
24506f6ba60Sopenharmony_ci        CMemoryLimit limit{.rssLimit = 0, .vssLimit = 0};
24606f6ba60Sopenharmony_ci        auto nativeInterface = HidebugNativeInterface::CreateInstance();
24706f6ba60Sopenharmony_ci        if (!nativeInterface) {
24806f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
24906f6ba60Sopenharmony_ci            return limit;
25006f6ba60Sopenharmony_ci        }
25106f6ba60Sopenharmony_ci
25206f6ba60Sopenharmony_ci        auto memoryLimit = nativeInterface->GetAppMemoryLimit();
25306f6ba60Sopenharmony_ci        if (!memoryLimit) {
25406f6ba60Sopenharmony_ci            code = ErrorCode::MEM_ERROR;
25506f6ba60Sopenharmony_ci            return limit;
25606f6ba60Sopenharmony_ci        }
25706f6ba60Sopenharmony_ci        limit.rssLimit = memoryLimit->rssLimit;
25806f6ba60Sopenharmony_ci        limit.vssLimit = memoryLimit->vssLimit;
25906f6ba60Sopenharmony_ci
26006f6ba60Sopenharmony_ci        return limit;
26106f6ba60Sopenharmony_ci    }
26206f6ba60Sopenharmony_ci
26306f6ba60Sopenharmony_ci    int32_t FfiHidebugGetServiceDump(int32_t serviceId, int32_t fd, CArrString args)
26406f6ba60Sopenharmony_ci    {
26506f6ba60Sopenharmony_ci        sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
26606f6ba60Sopenharmony_ci        if (!sam) {
26706f6ba60Sopenharmony_ci            return ErrorCode::MEM_ERROR;
26806f6ba60Sopenharmony_ci        }
26906f6ba60Sopenharmony_ci        sptr<IRemoteObject> sa = sam->CheckSystemAbility(serviceId);
27006f6ba60Sopenharmony_ci        if (sa == nullptr) {
27106f6ba60Sopenharmony_ci            return ErrorCode::SYSTEM_ABILITY_NOT_FOUND;
27206f6ba60Sopenharmony_ci        }
27306f6ba60Sopenharmony_ci        std::vector<std::u16string> cargs;
27406f6ba60Sopenharmony_ci        std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> strCnv;
27506f6ba60Sopenharmony_ci        for (int64_t i = 0; i < args.size; i++) {
27606f6ba60Sopenharmony_ci            cargs.push_back(strCnv.from_bytes(args.head[i]));
27706f6ba60Sopenharmony_ci        }
27806f6ba60Sopenharmony_ci        int dumpResult = sa->Dump(fd, cargs);
27906f6ba60Sopenharmony_ci        HILOG_INFO(LOG_CORE, "Dump result: %{public}d", dumpResult);
28006f6ba60Sopenharmony_ci        return 0;
28106f6ba60Sopenharmony_ci    }
28206f6ba60Sopenharmony_ci
28306f6ba60Sopenharmony_ci    char *FfiHidebugStartAppTraceCapture(CArrUnit tags, int32_t flag, uint32_t limitSize, int32_t &code)
28406f6ba60Sopenharmony_ci    {
28506f6ba60Sopenharmony_ci        std::vector<uint64_t> taglist;
28606f6ba60Sopenharmony_ci        uint64_t *tagPtr = static_cast<uint64_t *>(tags.head);
28706f6ba60Sopenharmony_ci        for (int64_t i = 0; i < tags.size; i++) {
28806f6ba60Sopenharmony_ci            taglist.push_back(tagPtr[i]);
28906f6ba60Sopenharmony_ci        }
29006f6ba60Sopenharmony_ci        uint64_t tag = std::accumulate(taglist.begin(), taglist.end(), 0ull,
29106f6ba60Sopenharmony_ci            [](uint64_t a, uint64_t b) { return a | b; });
29206f6ba60Sopenharmony_ci        std::string file;
29306f6ba60Sopenharmony_ci        auto nativeInterface = HidebugNativeInterface::CreateInstance();
29406f6ba60Sopenharmony_ci        if (!nativeInterface) {
29506f6ba60Sopenharmony_ci            code = ErrorCode::SYSTEM_STATUS_ABNORMAL;
29606f6ba60Sopenharmony_ci            return nullptr;
29706f6ba60Sopenharmony_ci        }
29806f6ba60Sopenharmony_ci        code = nativeInterface->StartAppTraceCapture(tag, flag, limitSize, file);
29906f6ba60Sopenharmony_ci        if (code != HIDEBUG_SUCCESS || file.empty()) {
30006f6ba60Sopenharmony_ci            return nullptr;
30106f6ba60Sopenharmony_ci        }
30206f6ba60Sopenharmony_ci        auto len = file.length() + 1;
30306f6ba60Sopenharmony_ci        char *res = static_cast<char *>(malloc(sizeof(char) * len));
30406f6ba60Sopenharmony_ci        if (res == nullptr) {
30506f6ba60Sopenharmony_ci            return nullptr;
30606f6ba60Sopenharmony_ci        }
30706f6ba60Sopenharmony_ci        return std::char_traits<char>::copy(res, file.c_str(), len);
30806f6ba60Sopenharmony_ci    }
30906f6ba60Sopenharmony_ci
31006f6ba60Sopenharmony_ci    int32_t FfiHidebugStopAppTraceCapture()
31106f6ba60Sopenharmony_ci    {
31206f6ba60Sopenharmony_ci        auto nativeInterface = HidebugNativeInterface::CreateInstance();
31306f6ba60Sopenharmony_ci        if (!nativeInterface) {
31406f6ba60Sopenharmony_ci            return ErrorCode::MEM_ERROR;
31506f6ba60Sopenharmony_ci        }
31606f6ba60Sopenharmony_ci        return nativeInterface->StopAppTraceCapture();
31706f6ba60Sopenharmony_ci    }
31806f6ba60Sopenharmony_ci
31906f6ba60Sopenharmony_ci    int32_t FfiHidebugSetAppResourceLimit(const char *type, int32_t value, bool enableDebugLog)
32006f6ba60Sopenharmony_ci    {
32106f6ba60Sopenharmony_ci        if (!CheckVersionType("beta", KEY_HIVIEW_USER_TYPE) &&
32206f6ba60Sopenharmony_ci            !CheckVersionType("enable", KEY_HIVIEW_DEVELOP_TYPE)) {
32306f6ba60Sopenharmony_ci            HILOG_ERROR(LOG_CORE, "SetAppResourceLimit failed. Not developer options or beta versions");
32406f6ba60Sopenharmony_ci            return ErrorCode::VERSION_ERROR;
32506f6ba60Sopenharmony_ci        }
32606f6ba60Sopenharmony_ci        auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32706f6ba60Sopenharmony_ci        if (!abilityManager) {
32806f6ba60Sopenharmony_ci            return ErrorCode::MEM_ERROR;
32906f6ba60Sopenharmony_ci        }
33006f6ba60Sopenharmony_ci        sptr<IRemoteObject> remoteObject = abilityManager->CheckSystemAbility(DFX_SYS_HIVIEW_ABILITY_ID);
33106f6ba60Sopenharmony_ci        if (remoteObject == nullptr) {
33206f6ba60Sopenharmony_ci            HILOG_ERROR(LOG_CORE, "SetAppResourceLimit failed. No this system ability.");
33306f6ba60Sopenharmony_ci            return ErrorCode::SYSTEM_STATUS_ABNORMAL;
33406f6ba60Sopenharmony_ci        }
33506f6ba60Sopenharmony_ci        auto result =
33606f6ba60Sopenharmony_ci            HidebugNativeInterface::CreateInstance()->GetMemoryLeakResource(std::string(type), value, enableDebugLog);
33706f6ba60Sopenharmony_ci        if (result == MemoryState::MEMORY_FAILED) {
33806f6ba60Sopenharmony_ci            return 0;
33906f6ba60Sopenharmony_ci        }
34006f6ba60Sopenharmony_ci        CreateSanBoxDir();
34106f6ba60Sopenharmony_ci        return 0;
34206f6ba60Sopenharmony_ci    }
34306f6ba60Sopenharmony_ci
34406f6ba60Sopenharmony_ci    bool FfiHidebugIsDebugState()
34506f6ba60Sopenharmony_ci    {
34606f6ba60Sopenharmony_ci        return HidebugNativeInterface::CreateInstance()->IsDebuggerConnected();
34706f6ba60Sopenharmony_ci    }
34806f6ba60Sopenharmony_ci}
34906f6ba60Sopenharmony_ci}
350