1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "xpower_event_js.h"
17 #include "xpower_event_jsvm.h"
18 #include "xpower_event_common.h"
19 #include <parameters.h>
20 #include "hisysevent.h"
21 #include "native_engine/native_engine.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 
ReportXPowerJsStackSysEvent(napi_env env,const std::string & tagName,const std::string & info)26 int ReportXPowerJsStackSysEvent(napi_env env, const std::string &tagName, const std::string &info)
27 {
28     auto engine = reinterpret_cast<NativeEngine*>(env);
29     return ReportXPowerJsStackSysEvent(engine, tagName, info);
30 }
31 
ReportXPowerJsStackSysEvent(NativeEngine * engine,const std::string & tagName,const std::string & info)32 int ReportXPowerJsStackSysEvent(NativeEngine *engine, const std::string &tagName, const std::string &info)
33 {
34     if (engine == nullptr) {
35         return ERR_PARAM_INVALID;
36     }
37     if ((OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0) != 1)) {
38         return ERR_PROP_NOT_ENABLE;
39     }
40     std::string stack = "";
41     bool succ = engine->BuildJsStackTrace(stack);
42     if (!succ) {
43         return ERR_DUMP_STACK_FAILED;
44     }
45     int ret = HiSysEventWrite(HiSysEvent::Domain::HIVIEWDFX, TAG_XPOWER_STACKTRACE,
46         HiSysEvent::EventType::STATISTIC, "TAGNAME", tagName, "INFO", info, "STACKTRACE", stack);
47     return ret; // 0 success or error code in HiSysEventWrite
48 }
49 
ReportXPowerJsStackSysEvent(EcmaVM * vm,const std::string & tagName,const std::string & info)50 int ReportXPowerJsStackSysEvent(EcmaVM *vm, const std::string &tagName, const std::string &info)
51 {
52     if (vm == nullptr) {
53         return ERR_PARAM_INVALID;
54     }
55     if ((OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0) != 1)) {
56         return ERR_PROP_NOT_ENABLE;
57     }
58     std::string stack = "";
59     bool succ = panda::DFXJSNApi::BuildJsStackTrace(vm, stack);
60     if (!succ) {
61         return ERR_DUMP_STACK_FAILED;
62     }
63     int ret = HiSysEventWrite(HiSysEvent::Domain::HIVIEWDFX, TAG_XPOWER_STACKTRACE,
64         HiSysEvent::EventType::STATISTIC, "TAGNAME", tagName, "INFO", info, "STACKTRACE", stack);
65     return ret; // 0 success or error code in HiSysEventWrite
66 }
67 
68 }
69 }