From 7f047a50272cf41eb54293c5a1ab6abf9381722c Mon Sep 17 00:00:00 2001 From: lutao Date: Wed, 26 Jan 2022 18:00:03 +0800 Subject: [PATCH 1/2] change ohos.build to bundle.json Signed-off-by: lutao --- OAT.xml | 62 +++++++ README.md | 36 ---- README_zh.md | 8 +- frameworks/native/hichecker.cpp | 18 +- interfaces/js/kits/napi/BUILD.gn | 47 ----- .../js/kits/napi/include/napi_hichecker.h | 38 ---- .../js/kits/napi/src/napi_hichecker.cpp | 166 ------------------ interfaces/native/bundle.json | 45 +++++ .../native/innerkits/include/hichecker.h | 1 + ohos.build | 30 ---- .../common/native/hichecker_native_test.cpp | 48 +++++ 11 files changed, 176 insertions(+), 323 deletions(-) create mode 100644 OAT.xml delete mode 100644 README.md delete mode 100644 interfaces/js/kits/napi/BUILD.gn delete mode 100644 interfaces/js/kits/napi/include/napi_hichecker.h delete mode 100644 interfaces/js/kits/napi/src/napi_hichecker.cpp create mode 100644 interfaces/native/bundle.json delete mode 100644 ohos.build diff --git a/OAT.xml b/OAT.xml new file mode 100644 index 0000000..064f2ce --- /dev/null +++ b/OAT.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + diff --git a/README.md b/README.md deleted file mode 100644 index 3347ef3..0000000 --- a/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# hiviewdfx_hichecker - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README_zh.md b/README_zh.md index d95dc09..78c5680 100644 --- a/README_zh.md +++ b/README_zh.md @@ -27,6 +27,7 @@ - 提供耗时消息检测功能 - 支持应用增加、删除不同的检测规则 - 支持应用增加、删除不同的告警通知规则,目前支持记录流水日志(默认),应用崩溃两种规则 +- 对外接口支持C++/JS形态两种接口 - 相关检测条件满足时,支持Native回栈到关键触发点,暂不支持JS回栈 ## 目录 @@ -65,19 +66,22 @@ | | NotifySlowProcess(std::string) : void | 通知有耗时调用 | | | NotifySlowEvent(std::string) : void | 通知有耗时事件 | | | NotifyAbilityConnectionLeak(Caution caution) : void | 通知有ability泄露 | +| Caution | GetTriggerRule() : BigInt | 获取触发当前告警的检测规则 | +| | GetCustomMessage() : String | 获取更多辅助信息 | +| | GetStackTrace() :String | 获取堆栈信息 | ### 使用说明 1. 添加单条规则 ```js - hichecker.addRule(hichecker.RULE_CAUTION_BY_LOG); + hichecker.addRule(hichecker.RULE_CAUTION_PRINT_LOG ); ``` 2. 添加多条规则 ``` - hichecker.addRule(hichecker.RULE_CAUTION_BY_LOG | hichecker.RULE_CHECK_SLOW_EVENT); + hichecker.addRule(hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CHECK_SLOW_EVENT); ``` ## 涉及仓 diff --git a/frameworks/native/hichecker.cpp b/frameworks/native/hichecker.cpp index 6b3aeae..7a9531a 100644 --- a/frameworks/native/hichecker.cpp +++ b/frameworks/native/hichecker.cpp @@ -34,8 +34,7 @@ static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D0B, "HICHECKER" }; void HiChecker::AddRule(uint64_t rule) { std::lock_guard lock(mutexLock_); - if ((Rule::ALL_RULES & rule) == 0) { - HiLog::Info(LABEL, "input rule is not exist,please check."); + if (!CheckRule(rule)) { return; } if ((Rule::RULE_CHECK_SLOW_EVENT & rule)) { @@ -48,8 +47,7 @@ void HiChecker::AddRule(uint64_t rule) void HiChecker::RemoveRule(uint64_t rule) { std::lock_guard lock(mutexLock_); - if ((Rule::ALL_RULES & rule) == 0) { - HiLog::Info(LABEL, "input rule is not exist,please check."); + if (!CheckRule(rule)) { return; } if ((Rule::RULE_CHECK_SLOW_EVENT & rule)) { @@ -68,6 +66,9 @@ uint64_t HiChecker::GetRule() bool HiChecker::Contains(uint64_t rule) { std::lock_guard lock(mutexLock_); + if (!CheckRule(rule)) { + return false; + } return rule == (rule & (threadLocalRules_ | processRules_)); } @@ -162,5 +163,14 @@ void HiChecker::DumpStackTrace(std::string& msg) HiLog::Info(LABEL, "HiChecker DumpStackTrace fail."); } } + +bool HiChecker::CheckRule(uint64_t rule) +{ + if (rule <= 0 || Rule::ALL_RULES != (Rule::ALL_RULES | rule)) { + HiLog::Info(LABEL, "input rule is not exist,please check."); + return false; + } + return true; +} } // HiviewDFX } // OHOS \ No newline at end of file diff --git a/interfaces/js/kits/napi/BUILD.gn b/interfaces/js/kits/napi/BUILD.gn deleted file mode 100644 index 10c365f..0000000 --- a/interfaces/js/kits/napi/BUILD.gn +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/ohos.gni") -import("//build/ohos/ace/ace.gni") - -config("hichecker_js_source_config") { - visibility = [ ":*" ] - - include_dirs = - [ "//base/hiviewdfx/hichecker/interfaces/native/innerkits/include" ] -} - -ohos_shared_library("hichecker") { - include_dirs = [ - "./", - "include/", - "//third_party/node/src", - "//foundation/ace/napi/interfaces/kits", - ] - - configs = [ ":hichecker_js_source_config" ] - - sources = [ "./src/napi_hichecker.cpp" ] - - deps = [ - "//base/hiviewdfx/hichecker/interfaces/native/innerkits:libhichecker", - "//foundation/ace/napi:ace_napi", - ] - - external_deps = [ "hilog_native:libhilog" ] - - relative_install_dir = "module" - - subsystem_name = "hiviewdfx" - part_name = "hichecker_js" -} diff --git a/interfaces/js/kits/napi/include/napi_hichecker.h b/interfaces/js/kits/napi/include/napi_hichecker.h deleted file mode 100644 index 1536508..0000000 --- a/interfaces/js/kits/napi/include/napi_hichecker.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NAPI_HICHECKER_H -#define NAPI_HICHECKER_H - -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -namespace OHOS { -namespace HiviewDFX { -static napi_value AddRule(napi_env env, napi_callback_info info); -static napi_value RemoveRule(napi_env env, napi_callback_info info); -static napi_value GetRule(napi_env env, napi_callback_info info); -static napi_value Contains(napi_env env, napi_callback_info info); - -static napi_value DeclareHiCheckerInterface(napi_env env, napi_value exports); -static napi_value DeclareHiCheckerRuleEnum(napi_env env, napi_value exports); -static napi_value CreateErrorMessage(napi_env env, std::string msg); -static napi_value CreateUndefined(napi_env env); -static napi_value ToUInt64Value(napi_env env, uint64_t value); -static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); -static uint64_t GetRuleParam(napi_env env, napi_callback_info info); -} // HiviewDFX -} // OHOS -#endif // NAPI_HICHECKER_H \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/napi_hichecker.cpp b/interfaces/js/kits/napi/src/napi_hichecker.cpp deleted file mode 100644 index 7c4433f..0000000 --- a/interfaces/js/kits/napi/src/napi_hichecker.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "napi_hichecker.h" - -#include "hichecker.h" -#include "hilog/log.h" - -namespace OHOS { -namespace HiviewDFX { -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D0B, "HiChecker_NAPI" }; -constexpr int ONE_VALUE_LIMIT = 1; -constexpr int ARRAY_INDEX_FIRST = 0; -constexpr uint64_t GET_RULE_PARAM_FAIL = 0; -} - -napi_value AddRule(napi_env env, napi_callback_info info) -{ - uint64_t rule = GetRuleParam(env, info); - if (rule == GET_RULE_PARAM_FAIL) { - return CreateErrorMessage(env, "invalid input, please check!"); - } - HiChecker::AddRule(rule); - return CreateUndefined(env); -} - -napi_value RemoveRule(napi_env env, napi_callback_info info) -{ - uint64_t rule = GetRuleParam(env, info); - if (rule == GET_RULE_PARAM_FAIL) { - return CreateErrorMessage(env, "invalid input, please check!"); - } - HiChecker::RemoveRule(rule); - return CreateUndefined(env); -} - -napi_value GetRule(napi_env env, napi_callback_info info) -{ - uint64_t rule = HiChecker::GetRule(); - return ToUInt64Value(env, rule); -} - -napi_value Contains(napi_env env, napi_callback_info info) -{ - uint64_t rule = GetRuleParam(env, info); - if (rule == GET_RULE_PARAM_FAIL) { - return CreateErrorMessage(env, "invalid input, please check!"); - } - napi_value result = nullptr; - napi_get_boolean(env, HiChecker::Contains(rule), &result); - return result; -} - -napi_value DeclareHiCheckerInterface(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("addRule", AddRule), - DECLARE_NAPI_FUNCTION("removeRule", RemoveRule), - DECLARE_NAPI_FUNCTION("getRule", GetRule), - DECLARE_NAPI_FUNCTION("contains", Contains), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - DeclareHiCheckerRuleEnum(env, exports); - return exports; -} - -napi_value DeclareHiCheckerRuleEnum(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - DECLARE_NAPI_STATIC_PROPERTY("RULE_CAUTION_PRINT_LOG", ToUInt64Value(env, Rule::RULE_CAUTION_PRINT_LOG)), - DECLARE_NAPI_STATIC_PROPERTY("RULE_CAUTION_TRIGGER_CRASH", - ToUInt64Value(env, Rule::RULE_CAUTION_TRIGGER_CRASH)), - DECLARE_NAPI_STATIC_PROPERTY("RULE_THREAD_CHECK_SLOW_PROCESS", - ToUInt64Value(env, Rule::RULE_THREAD_CHECK_SLOW_PROCESS)), - DECLARE_NAPI_STATIC_PROPERTY("RULE_CHECK_SLOW_EVENT", ToUInt64Value(env, Rule::RULE_CHECK_SLOW_EVENT)), - DECLARE_NAPI_STATIC_PROPERTY("RULE_CHECK_ABILITY_CONNECTION_LEAK", - ToUInt64Value(env, Rule::RULE_CHECK_ABILITY_CONNECTION_LEAK)), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - return exports; -} - -napi_value ToUInt64Value(napi_env env, uint64_t value) -{ - napi_value staticValue = nullptr; - napi_create_bigint_uint64(env, value, &staticValue); - return staticValue; -} - -napi_value CreateUndefined(napi_env env) -{ - napi_value result = nullptr; - napi_get_undefined(env, &result); - return result; -} - -napi_value CreateErrorMessage(napi_env env, std::string msg) -{ - napi_value result = nullptr; - napi_value message = nullptr; - napi_create_string_utf8(env, (char *)msg.data(), msg.size(), &message); - napi_create_error(env, nullptr, message, &result); - return result; -} - -uint64_t GetRuleParam(napi_env env, napi_callback_info info) -{ - size_t argc = ONE_VALUE_LIMIT; - napi_value argv[ONE_VALUE_LIMIT] = { nullptr }; - napi_value thisVar = nullptr; - void *data = nullptr; - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - if (argc != ONE_VALUE_LIMIT) { - HiLog::Error(LABEL, "invalid number=%{public}d of params.", ONE_VALUE_LIMIT); - return GET_RULE_PARAM_FAIL; - } - if (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_bigint)) { - HiLog::Error(LABEL, "Type error, should be bigint type!"); - return GET_RULE_PARAM_FAIL; - } - uint64_t rule = GET_RULE_PARAM_FAIL; - bool lossless = true; - napi_get_value_bigint_uint64(env, argv[ARRAY_INDEX_FIRST], &rule, &lossless); - if (!lossless) { - HiLog::Error(LABEL, "Type error, bigint should be 64!"); - return GET_RULE_PARAM_FAIL; - } - return rule; -} - -bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType) -{ - napi_valuetype valueType = napi_undefined; - napi_typeof(env, value, &valueType); - return valueType == targetType; -} - -static napi_module g_module = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = DeclareHiCheckerInterface, - .nm_modname = "hichecker", - .nm_priv = ((void *)0), - .reserved = {0} -}; - -extern "C" __attribute__((constructor)) void HiCheckerRegisterModule(void) -{ - napi_module_register(&g_module); -} -} // HiviewDFX -} // OHOS \ No newline at end of file diff --git a/interfaces/native/bundle.json b/interfaces/native/bundle.json new file mode 100644 index 0000000..8be5370 --- /dev/null +++ b/interfaces/native/bundle.json @@ -0,0 +1,45 @@ +{ + "name": "@ohos/hichecker_native", + "description": "HiChecker for native application", + "version": "3.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "base/hiviewdfx/hichecker" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "hichecker_native", + "subsystem": "hiviewdfx", + "adapted_system_type": [ + "standard" + ], + "rom": "", + "ram": "", + "deps": { + "components": [ + "faultloggerd", + "hilog_native" + ] + }, + "build": { + "sub_component": [ + "//base/hiviewdfx/hichecker/interfaces/native/innerkits:libhichecker" + ], + "inner_kits": [ + { + "name": "//base/hiviewdfx/hichecker/interfaces/native/innerkits:libhichecker", + "header": { + "header_files": [ + "hichecker.h", + "caution.h" + ], + "header_base": "//base/hiviewdfx/hichecker/interfaces/native/innerkits/include/" + } + } + ], + "test": [ "//base/hiviewdfx/hichecker/test:unittest" ] + } + } +} \ No newline at end of file diff --git a/interfaces/native/innerkits/include/hichecker.h b/interfaces/native/innerkits/include/hichecker.h index 68e20eb..e0f8ffa 100644 --- a/interfaces/native/innerkits/include/hichecker.h +++ b/interfaces/native/innerkits/include/hichecker.h @@ -71,6 +71,7 @@ private: static void TriggerCrash(const CautionDetail& cautionDetail); static bool HasCautionRule(uint64_t rules); static void DumpStackTrace(std::string& msg); + static bool CheckRule(uint64_t rule); static std::mutex mutexLock_; static volatile bool checkMode_; diff --git a/ohos.build b/ohos.build deleted file mode 100644 index 4d1e140..0000000 --- a/ohos.build +++ /dev/null @@ -1,30 +0,0 @@ -{ - "subsystem": "hiviewdfx", - "parts": { - "hichecker_native": { - "module_list": [ - "//base/hiviewdfx/hichecker/interfaces/native/innerkits:libhichecker" - ], - "inner_kits": [ - { - "name": "//base/hiviewdfx/hichecker/interfaces/native/innerkits:libhichecker", - "header": { - "header_files": [ - "hichecker.h", - "caution.h" - ], - "header_base": "//base/hiviewdfx/hichecker/interfaces/native/innerkits/include/" - } - } - ], - "test_list": [ - "//base/hiviewdfx/hichecker/test:unittest" - ] - }, - "hichecker_js": { - "module_list": [ - "//base/hiviewdfx/hichecker/interfaces/js/kits/napi:hichecker" - ] - } - } -} diff --git a/test/unittest/common/native/hichecker_native_test.cpp b/test/unittest/common/native/hichecker_native_test.cpp index ee556ff..f88a4cc 100644 --- a/test/unittest/common/native/hichecker_native_test.cpp +++ b/test/unittest/common/native/hichecker_native_test.cpp @@ -25,6 +25,9 @@ namespace { const int64_t SEC_TO_NS = 1000000000; const int64_t MAX_CALL_DURATION_US = 1000; // 1ms const int LOOP_COUNT = 1000; + const uint64_t RULE_ERROR0 = 0; + const uint64_t RULE_ERROR1 = -1; + const uint64_t RULE_ERROR2 = 999999999; } class HiCheckerNativeTest : public testing::Test { @@ -84,6 +87,21 @@ HWTEST_F(HiCheckerNativeTest, AddRuleTest002, TestSize.Level1) ASSERT_EQ(HiChecker::GetRule(), rule); } +/** + * @tc.name: AddRule003 + * @tc.desc: add invaild rule + * @tc.type: FUNC +*/ +HWTEST_F(HiCheckerNativeTest, AddRuleTest003, TestSize.Level1) +{ + HiChecker::AddRule(RULE_ERROR0); + ASSERT_EQ(HiChecker::GetRule(), 0); + HiChecker::AddRule(RULE_ERROR1); + ASSERT_EQ(HiChecker::GetRule(), 0); + HiChecker::AddRule(RULE_ERROR2); + ASSERT_EQ(HiChecker::GetRule(), 0); +} + /** * @tc.name: AddRulePerf * @tc.desc: test performance for AddRule @@ -132,6 +150,22 @@ HWTEST_F(HiCheckerNativeTest, RemoveRuleTest002, TestSize.Level1) ASSERT_FALSE(HiChecker::Contains(Rule::RULE_CAUTION_PRINT_LOG)); uint64_t rule = Rule::ALL_RULES ^ (Rule::RULE_CAUTION_PRINT_LOG | Rule::RULE_CAUTION_TRIGGER_CRASH); ASSERT_EQ(HiChecker::GetRule(), rule); +} + +/** + * @tc.name: RemoveRule003 + * @tc.desc: remove invaild rule + * @tc.type: FUNC +*/ +HWTEST_F(HiCheckerNativeTest, RemoveRuleTest003, TestSize.Level1) +{ + HiChecker::AddRule(Rule::ALL_RULES); + HiChecker::RemoveRule(RULE_ERROR0); + ASSERT_EQ(HiChecker::GetRule(), Rule::ALL_RULES); + HiChecker::RemoveRule(RULE_ERROR1); + ASSERT_EQ(HiChecker::GetRule(), Rule::ALL_RULES); + HiChecker::RemoveRule(RULE_ERROR2); + ASSERT_EQ(HiChecker::GetRule(), Rule::ALL_RULES); } /** @@ -163,4 +197,18 @@ HWTEST_F(HiCheckerNativeTest, ContainsTest001, TestSize.Level1) HiChecker::AddRule(Rule::RULE_CAUTION_PRINT_LOG); ASSERT_TRUE(HiChecker::Contains(Rule::RULE_CAUTION_PRINT_LOG)); ASSERT_FALSE(HiChecker::Contains(Rule::RULE_CAUTION_TRIGGER_CRASH)); + ASSERT_FALSE(HiChecker::Contains(Rule::RULE_CAUTION_PRINT_LOG | Rule::RULE_CAUTION_TRIGGER_CRASH)); +} + +/** + * @tc.name: Contains002 + * @tc.desc: test Contains with invaild rule + * @tc.type: FUNC +*/ +HWTEST_F(HiCheckerNativeTest, ContainsTest002, TestSize.Level1) +{ + HiChecker::AddRule(Rule::ALL_RULES); + ASSERT_FALSE(HiChecker::Contains(RULE_ERROR0)); + ASSERT_FALSE(HiChecker::Contains(RULE_ERROR1)); + ASSERT_FALSE(HiChecker::Contains(RULE_ERROR2)); } \ No newline at end of file -- Gitee From e7268b134464850499d46e8fa1da8d2e4dd37458 Mon Sep 17 00:00:00 2001 From: lutao Date: Thu, 27 Jan 2022 15:02:02 +0800 Subject: [PATCH 2/2] update README.ZH Signed-off-by: lutao --- README_zh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README_zh.md b/README_zh.md index 78c5680..a8082db 100644 --- a/README_zh.md +++ b/README_zh.md @@ -8,7 +8,7 @@ ## 简介 -​ 本模块主要作用是给基于OpenHarmony的应用(包含系统和三方应用)开发者提供一套检测工具,用来检测应用程序开发过程中容易引人忽略的部分问题,包括应用线程出现耗时调用、应用进程中元能力资源泄露等问题,检测到的问题以日志记录或进程crash等形式展现出来以便开发者发现并修改相关问题,提升应用使用体验 +HiChecker,可以作为应用开发阶段使用的检测工具,用于检测代码运行过程中部分易忽略的问题,如应用线程出现耗时调用、应用进程中元能力资源泄露等问题。开发者可以通过日志记录或进程crash等形式查看具体问题并进行修改,提升应用的使用体验。 ## 架构 -- Gitee