1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
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 <cerrno>
17 #include <codecvt>
18 #include <fstream>
19 #include <string>
20 #include <memory>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <ctime>
25 #include <malloc.h>
26 #include <unistd.h>
27
28 #include "application_context.h"
29 #include "context.h"
30 #include "cpu_collector.h"
31 #include "directory_ex.h"
32 #include "dump_usage.h"
33 #include "hilog/log.h"
34 #include "iservice_registry.h"
35 #include "memory_collector.h"
36 #include "napi/native_api.h"
37 #include "napi/native_node_api.h"
38 #include "native_engine/native_engine.h"
39 #include "refbase.h"
40
41 namespace OHOS {
42 namespace HiviewDFX {
43 namespace {
44 #undef LOG_DOMAIN
45 #define LOG_DOMAIN 0xD002D0A
46 #undef LOG_TAG
47 #define LOG_TAG "HiDebug_NAPI"
48 constexpr int ONE_VALUE_LIMIT = 1;
49 constexpr int ARRAY_INDEX_FIRST = 0;
50 constexpr int ARRAY_INDEX_SECOND = 1;
51 constexpr int REMOVE_NAPI_WRAP_PARAM_COUNT = 2;
52 const std::string SLASH_STR = "/";
53 const std::string DEFAULT_FILENAME = "undefined";
54 const std::string JSON_FILE = ".json";
55 const std::string HEAPSNAPSHOT_FILE = ".heapsnapshot";
56 enum ErrorCode {
57 PERMISSION_ERROR = 201,
58 PARAMETER_ERROR = 401,
59 VERSION_ERROR = 801,
60 SYSTEM_ABILITY_NOT_FOUND = 11400101
61 };
62 }
63
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)64 static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
65 {
66 napi_valuetype valueType = napi_undefined;
67 napi_typeof(env, value, &valueType);
68 return valueType == targetType;
69 }
70
CreateFile(const std::string & path)71 static bool CreateFile(const std::string &path)
72 {
73 if (access(path.c_str(), F_OK) == 0) {
74 if (access(path.c_str(), W_OK) == 0) {
75 return true;
76 }
77 return false;
78 }
79 const mode_t defaultMode = S_IRUSR | S_IWUSR | S_IRGRP; // -rw-r-----
80 int fd = creat(path.c_str(), defaultMode);
81 if (fd == -1) {
82 HILOG_ERROR(LOG_CORE, "file create failed, errno = %{public}d", errno);
83 return false;
84 } else {
85 close(fd);
86 return true;
87 }
88 }
89
IsLegalPath(const std::string & path)90 static bool IsLegalPath(const std::string& path)
91 {
92 if (path.find("./") != std::string::npos ||
93 path.find("../") != std::string::npos) {
94 return false;
95 }
96 return true;
97 }
98
IsArrayForNapiValue(napi_env env,napi_value param,uint32_t & arraySize)99 static bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arraySize)
100 {
101 bool isArray = false;
102 arraySize = 0;
103 if (napi_is_array(env, param, &isArray) != napi_ok || isArray == false) {
104 return false;
105 }
106 if (napi_get_array_length(env, param, &arraySize) != napi_ok) {
107 return false;
108 }
109 return true;
110 }
111
GetDumpParam(napi_env env,napi_callback_info info,int & serviceId,int & fd,std::vector<std::u16string> & args)112 static bool GetDumpParam(napi_env env, napi_callback_info info,
113 int& serviceId, int& fd, std::vector<std::u16string>& args)
114 {
115 const int valueNum = 3;
116 size_t argc = valueNum;
117 napi_value argv[valueNum] = {nullptr};
118 napi_value thisVar = nullptr;
119 void *data = nullptr;
120 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
121 if (argc != valueNum) {
122 HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
123 return false;
124 }
125 int thirdPos = 2;
126 if (!MatchValueType(env, argv[0], napi_number) &&
127 !MatchValueType(env, argv[1], napi_number) &&
128 !MatchValueType(env, argv[thirdPos], napi_object)) {
129 HILOG_ERROR(LOG_CORE, "params type error.");
130 return false;
131 }
132 if (napi_get_value_int32(env, argv[0], &serviceId) != napi_ok) {
133 HILOG_ERROR(LOG_CORE, "Get input serviceId failed.");
134 return false;
135 }
136 if (napi_get_value_int32(env, argv[1], &fd) != napi_ok) {
137 HILOG_ERROR(LOG_CORE, "Get input fd failed.");
138 return false;
139 }
140 uint32_t arraySize = 0;
141 if (!IsArrayForNapiValue(env, argv[thirdPos], arraySize)) {
142 HILOG_ERROR(LOG_CORE, "Get input args failed.");
143 return false;
144 }
145 for (uint32_t i = 0; i < arraySize; i++) {
146 napi_value jsValue = nullptr;
147 if (napi_get_element(env, argv[thirdPos], i, &jsValue) != napi_ok) {
148 HILOG_ERROR(LOG_CORE, "get_element -> Get input args failed.");
149 return false;
150 }
151 const size_t bufSize = 256;
152 size_t bufLen = 0;
153 char buf[bufSize] = {0};
154 if (napi_get_value_string_utf8(env, jsValue, buf, bufSize - 1, &bufLen) != napi_ok) {
155 HILOG_ERROR(LOG_CORE, "get_value -> Get input args failed.");
156 return false;
157 }
158 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> strCnv;
159 args.push_back(strCnv.from_bytes(buf));
160 }
161 return true;
162 }
163
GetFileNameParam(napi_env env,napi_callback_info info)164 static std::string GetFileNameParam(napi_env env, napi_callback_info info)
165 {
166 size_t argc = ONE_VALUE_LIMIT;
167 napi_value argv[ONE_VALUE_LIMIT] = { nullptr };
168 napi_value thisVar = nullptr;
169 void *data = nullptr;
170 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
171 if (argc != ONE_VALUE_LIMIT) {
172 HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
173 return DEFAULT_FILENAME;
174 }
175 if (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_string)) {
176 HILOG_ERROR(LOG_CORE, "Type error, should be string type!");
177 return DEFAULT_FILENAME;
178 }
179 size_t bufLen = 0;
180 napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufLen);
181 if (status != napi_ok) {
182 HILOG_ERROR(LOG_CORE, "Get input filename param length failed.");
183 return DEFAULT_FILENAME;
184 }
185 const int bufMax = 128;
186 if (bufLen > bufMax || bufLen == 0) {
187 HILOG_ERROR(LOG_CORE, "input filename param length is illegal.");
188 return DEFAULT_FILENAME;
189 }
190 char buf[bufLen + 1];
191 napi_get_value_string_utf8(env, argv[0], buf, bufLen + 1, &bufLen);
192 std::string fileName = buf;
193 return fileName;
194 }
195
GetFileNameParamThrowErrorVersion(napi_env env,napi_callback_info info,std::string & fileName)196 static bool GetFileNameParamThrowErrorVersion(napi_env env, napi_callback_info info, std::string &fileName)
197 {
198 size_t argc = ONE_VALUE_LIMIT;
199 napi_value argv[ONE_VALUE_LIMIT] = { nullptr };
200 napi_value thisVar = nullptr;
201 void *data = nullptr;
202 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
203 if (argc != ONE_VALUE_LIMIT) {
204 HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
205 return false;
206 }
207 if (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_string)) {
208 HILOG_ERROR(LOG_CORE, "Type error, should be string type!");
209 return false;
210 }
211 size_t bufLen = 0;
212 napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufLen);
213 if (status != napi_ok) {
214 HILOG_ERROR(LOG_CORE, "Get input filename param length failed.");
215 return false;
216 }
217 const int bufMax = 128;
218 if (bufLen > bufMax || bufLen == 0) {
219 HILOG_ERROR(LOG_CORE, "input filename param length is illegal.");
220 return false;
221 }
222 char buf[bufLen + 1];
223 napi_get_value_string_utf8(env, argv[0], buf, bufLen + 1, &bufLen);
224 fileName = buf;
225 return true;
226 }
227
CreateUndefined(napi_env env)228 static napi_value CreateUndefined(napi_env env)
229 {
230 napi_value res = nullptr;
231 napi_get_undefined(env, &res);
232 return res;
233 }
234
CreateErrorMessage(napi_env env,std::string msg)235 static napi_value CreateErrorMessage(napi_env env, std::string msg)
236 {
237 napi_value result = nullptr;
238 napi_value message = nullptr;
239 napi_create_string_utf8(env, (char *)msg.data(), msg.size(), &message);
240 napi_create_error(env, nullptr, message, &result);
241 return result;
242 }
243
StartProfiling(napi_env env,napi_callback_info info)244 napi_value StartProfiling(napi_env env, napi_callback_info info)
245 {
246 std::string fileName = GetFileNameParam(env, info);
247 auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
248 if (context == nullptr) {
249 return CreateErrorMessage(env, "Get ApplicationContext failed.");
250 }
251 std::string filesDir = context->GetFilesDir();
252 if (filesDir.empty()) {
253 return CreateErrorMessage(env, "Get App files dir failed.");
254 }
255 std::string filePath = filesDir + SLASH_STR + fileName + JSON_FILE;
256 if (!IsLegalPath(filePath)) {
257 return CreateErrorMessage(env, "input fileName is illegal.");
258 }
259 if (!CreateFile(filePath)) {
260 return CreateErrorMessage(env, "file created failed.");
261 }
262 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
263 engine->StartCpuProfiler(filePath);
264 return CreateUndefined(env);
265 }
266
StartJsCpuProfiling(napi_env env,napi_callback_info info)267 napi_value StartJsCpuProfiling(napi_env env, napi_callback_info info)
268 {
269 std::string fileName;
270 if (!GetFileNameParamThrowErrorVersion(env, info, fileName)) {
271 std::string paramErrorMessage = "Invalid parameter, require a string parameter.";
272 napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
273 return CreateUndefined(env);
274 }
275 HILOG_INFO(LOG_CORE, "filename: %{public}s.", fileName.c_str());
276 auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
277 if (context == nullptr) {
278 return CreateErrorMessage(env, "Get ApplicationContext failed.");
279 }
280 std::string filesDir = context->GetFilesDir();
281 if (filesDir.empty()) {
282 return CreateErrorMessage(env, "Get App files dir failed.");
283 }
284 std::string filePath = filesDir + SLASH_STR + fileName + JSON_FILE;
285 if (!IsLegalPath(filePath)) {
286 return CreateErrorMessage(env, "input fileName is illegal.");
287 }
288 if (!CreateFile(filePath)) {
289 return CreateErrorMessage(env, "file created failed.");
290 }
291 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
292 engine->StartCpuProfiler(filePath);
293 return CreateUndefined(env);
294 }
295
StopProfiling(napi_env env,napi_callback_info info)296 napi_value StopProfiling(napi_env env, napi_callback_info info)
297 {
298 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
299 engine->StopCpuProfiler();
300 return CreateUndefined(env);
301 }
302
StopJsCpuProfiling(napi_env env,napi_callback_info info)303 napi_value StopJsCpuProfiling(napi_env env, napi_callback_info info)
304 {
305 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
306 engine->StopCpuProfiler();
307 return CreateUndefined(env);
308 }
309
DumpHeapData(napi_env env,napi_callback_info info)310 napi_value DumpHeapData(napi_env env, napi_callback_info info)
311 {
312 std::string fileName = GetFileNameParam(env, info);
313 auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
314 if (context == nullptr) {
315 return CreateErrorMessage(env, "Get ApplicationContext failed.");
316 }
317 std::string filesDir = context->GetFilesDir();
318 if (filesDir.empty()) {
319 return CreateErrorMessage(env, "Get App files dir failed.");
320 }
321 std::string filePath = filesDir + SLASH_STR + fileName + HEAPSNAPSHOT_FILE;
322 if (!IsLegalPath(filePath)) {
323 return CreateErrorMessage(env, "input fileName is illegal.");
324 }
325 if (!CreateFile(filePath)) {
326 return CreateErrorMessage(env, "file created failed.");
327 }
328 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
329 engine->DumpHeapSnapshot(filePath);
330 return CreateUndefined(env);
331 }
332
DumpJsHeapData(napi_env env,napi_callback_info info)333 napi_value DumpJsHeapData(napi_env env, napi_callback_info info)
334 {
335 std::string fileName;
336 if (!GetFileNameParamThrowErrorVersion(env, info, fileName)) {
337 std::string paramErrorMessage = "Invalid parameter, require a string parameter.";
338 napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
339 return CreateUndefined(env);
340 }
341 HILOG_ERROR(LOG_CORE, "filename: %{public}s.", fileName.c_str());
342 auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
343 if (context == nullptr) {
344 return CreateErrorMessage(env, "Get ApplicationContext failed.");
345 }
346 std::string filesDir = context->GetFilesDir();
347 if (filesDir.empty()) {
348 return CreateErrorMessage(env, "Get App files dir failed.");
349 }
350 std::string filePath = filesDir + SLASH_STR + fileName + HEAPSNAPSHOT_FILE;
351 if (!IsLegalPath(filePath)) {
352 return CreateErrorMessage(env, "input fileName is illegal.");
353 }
354 if (!CreateFile(filePath)) {
355 return CreateErrorMessage(env, "file created failed.");
356 }
357 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
358 engine->DumpHeapSnapshot(filePath);
359 return CreateUndefined(env);
360 }
361
GetPss(napi_env env,napi_callback_info info)362 napi_value GetPss(napi_env env, napi_callback_info info)
363 {
364 napi_value pss;
365 std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
366 if (collector != nullptr) {
367 int pid = getpid();
368 auto collectResult = collector->CollectProcessMemory(pid);
369 int32_t pssInfo = collectResult.data.pss + collectResult.data.swapPss;
370 napi_create_bigint_uint64(env, pssInfo, &pss);
371 } else {
372 napi_create_bigint_uint64(env, 0, &pss);
373 }
374 return pss;
375 }
376
GetSharedDirty(napi_env env,napi_callback_info info)377 napi_value GetSharedDirty(napi_env env, napi_callback_info info)
378 {
379 napi_value sharedDirty;
380 std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
381 if (collector != nullptr) {
382 int pid = getpid();
383 auto collectResult = collector->CollectProcessMemory(pid);
384 int32_t sharedDirtyInfo = collectResult.data.sharedDirty;
385 napi_create_bigint_uint64(env, sharedDirtyInfo, &sharedDirty);
386 } else {
387 napi_create_bigint_uint64(env, 0, &sharedDirty);
388 }
389 return sharedDirty;
390 }
391
GetPrivateDirty(napi_env env,napi_callback_info info)392 napi_value GetPrivateDirty(napi_env env, napi_callback_info info)
393 {
394 napi_value privateDirtyValue;
395 std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
396 if (collector != nullptr) {
397 pid_t pid = getpid();
398 auto collectResult = collector->CollectProcessMemory(pid);
399 int32_t privateDirty = collectResult.data.privateDirty;
400 napi_create_bigint_uint64(env, privateDirty, &privateDirtyValue);
401 } else {
402 napi_create_bigint_uint64(env, 0, &privateDirtyValue);
403 }
404 return privateDirtyValue;
405 }
406
GetCpuUsage(napi_env env,napi_callback_info info)407 napi_value GetCpuUsage(napi_env env, napi_callback_info info)
408 {
409 napi_value cpuUsageValue;
410 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
411 pid_t pid = getpid();
412 double cpuUsage = dumpUsage->GetCpuUsage(pid);
413 napi_create_double(env, cpuUsage, &cpuUsageValue);
414 return cpuUsageValue;
415 }
416
GetNativeHeapSize(napi_env env,napi_callback_info info)417 napi_value GetNativeHeapSize(napi_env env, napi_callback_info info)
418 {
419 struct mallinfo mi = mallinfo();
420 napi_value nativeHeapSize;
421 napi_create_bigint_uint64(env, uint64_t(mi.uordblks + mi.fordblks), &nativeHeapSize);
422 return nativeHeapSize;
423 }
424
GetNativeHeapAllocatedSize(napi_env env,napi_callback_info info)425 napi_value GetNativeHeapAllocatedSize(napi_env env, napi_callback_info info)
426 {
427 struct mallinfo mi = mallinfo();
428 napi_value nativeHeapAllocatedSize;
429 napi_create_bigint_uint64(env, uint64_t(mi.uordblks), &nativeHeapAllocatedSize);
430 return nativeHeapAllocatedSize;
431 }
432
GetNativeHeapFreeSize(napi_env env,napi_callback_info info)433 napi_value GetNativeHeapFreeSize(napi_env env, napi_callback_info info)
434 {
435 struct mallinfo mi = mallinfo();
436 napi_value nativeHeapFreeSize;
437 napi_create_bigint_uint64(env, uint64_t(mi.fordblks), &nativeHeapFreeSize);
438 return nativeHeapFreeSize;
439 }
440
GetServiceDump(napi_env env,napi_callback_info info)441 static napi_value GetServiceDump(napi_env env, napi_callback_info info)
442 {
443 int serviceAbilityId = 0;
444 int fd = 0;
445 std::vector<std::u16string> args;
446 if (!GetDumpParam(env, info, serviceAbilityId, fd, args)) {
447 std::string paramErrorMessage = "The parameter check failed.";
448 napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
449 return CreateUndefined(env);
450 }
451
452 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
453 if (!sam) {
454 return CreateUndefined(env);
455 }
456 sptr<IRemoteObject> sa = sam->CheckSystemAbility(serviceAbilityId);
457 if (sa == nullptr) {
458 HILOG_ERROR(LOG_CORE, "no this system ability.");
459 std::string idErrorMessage = "service id is invalid, system ability is not exist.";
460 napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_ABILITY_NOT_FOUND).c_str(), idErrorMessage.c_str());
461 return CreateUndefined(env);
462 }
463 int dumpResult = sa->Dump(fd, args);
464 HILOG_INFO(LOG_CORE, "Dump result: %{public}d", dumpResult);
465 return CreateUndefined(env);
466 }
467
GetVss(napi_env env,napi_callback_info info)468 napi_value GetVss(napi_env env, napi_callback_info info)
469 {
470 napi_value vss;
471 std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
472 if (collector != nullptr) {
473 pid_t pid = getpid();
474 auto collectResult = collector->CollectProcessVss(pid);
475 uint64_t vssInfo = collectResult.data;
476 napi_create_bigint_uint64(env, vssInfo, &vss);
477 } else {
478 napi_create_bigint_uint64(env, 0, &vss);
479 }
480 return vss;
481 }
482
RemoveNapiWrap(napi_env env,napi_callback_info info)483 static napi_value RemoveNapiWrap(napi_env env, napi_callback_info info)
484 {
485 size_t argc = REMOVE_NAPI_WRAP_PARAM_COUNT;
486 napi_value argv[REMOVE_NAPI_WRAP_PARAM_COUNT] = {nullptr};
487 napi_value thisVar = nullptr;
488 void *data = nullptr;
489 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
490 if (argc != REMOVE_NAPI_WRAP_PARAM_COUNT ||
491 (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_object) ||
492 !MatchValueType(env, argv[ARRAY_INDEX_SECOND], napi_boolean))) {
493 HILOG_ERROR(LOG_CORE, "RemoveNapiWrap Failed to parse parameters, argc %{public}d", (int)argc);
494 std::string paramErrorMessage = "The parameter check failed.";
495 napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
496 return CreateUndefined(env);
497 }
498
499 // remove jsObj's wrap
500 auto jsObj = argv[ARRAY_INDEX_FIRST];
501 void *nativePtr = nullptr;
502 napi_remove_wrap(env, jsObj, (void **)&nativePtr);
503
504 // remove jsObj's properties wrap
505 bool needRemoveProperty = false;
506 napi_get_value_bool(env, argv[ARRAY_INDEX_SECOND], &needRemoveProperty);
507 if (needRemoveProperty) {
508 napi_value allPropertyNames = nullptr;
509 napi_object_get_keys(env, jsObj, &allPropertyNames);
510 uint32_t nameCount = 0;
511 napi_get_array_length(env, allPropertyNames, &nameCount);
512 for (size_t i = 0; i < nameCount; ++i) {
513 napi_value propertyName = nullptr;
514 napi_get_element(env, allPropertyNames, i, &propertyName);
515 char name[128] = {0};
516 size_t len = 0;
517 napi_get_value_string_utf8(env, propertyName, name, 64, &len);
518 napi_value propertyObj = nullptr;
519 napi_get_named_property(env, jsObj, name, &propertyObj);
520 napi_remove_wrap(env, propertyObj, (void **)&nativePtr);
521 }
522 }
523 return CreateUndefined(env);
524 }
525
DeclareHiDebugInterface(napi_env env,napi_value exports)526 napi_value DeclareHiDebugInterface(napi_env env, napi_value exports)
527 {
528 napi_property_descriptor desc[] = {
529 DECLARE_NAPI_FUNCTION("startProfiling", StartProfiling),
530 DECLARE_NAPI_FUNCTION("stopProfiling", StopProfiling),
531 DECLARE_NAPI_FUNCTION("dumpHeapData", DumpHeapData),
532 DECLARE_NAPI_FUNCTION("startJsCpuProfiling", StartJsCpuProfiling),
533 DECLARE_NAPI_FUNCTION("stopJsCpuProfiling", StopJsCpuProfiling),
534 DECLARE_NAPI_FUNCTION("dumpJsHeapData", DumpJsHeapData),
535 DECLARE_NAPI_FUNCTION("getPss", GetPss),
536 DECLARE_NAPI_FUNCTION("getSharedDirty", GetSharedDirty),
537 DECLARE_NAPI_FUNCTION("getPrivateDirty", GetPrivateDirty),
538 DECLARE_NAPI_FUNCTION("getCpuUsage", GetCpuUsage),
539 DECLARE_NAPI_FUNCTION("getServiceDump", GetServiceDump),
540 DECLARE_NAPI_FUNCTION("getNativeHeapSize", GetNativeHeapSize),
541 DECLARE_NAPI_FUNCTION("getNativeHeapAllocatedSize", GetNativeHeapAllocatedSize),
542 DECLARE_NAPI_FUNCTION("getNativeHeapFreeSize", GetNativeHeapFreeSize),
543 DECLARE_NAPI_FUNCTION("getVss", GetVss),
544 DECLARE_NAPI_FUNCTION("removeNapiWrap", RemoveNapiWrap)
545 };
546 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
547 return exports;
548 }
549
550 static napi_module hidebugModule = {
551 .nm_version = 1,
552 .nm_flags = 0,
553 .nm_filename = nullptr,
554 .nm_register_func = HiviewDFX::DeclareHiDebugInterface,
555 .nm_modname = "hidebug",
556 .nm_priv = ((void *)0),
557 .reserved = {0}
558 };
559
HiDebugRegisterModule(void)560 extern "C" __attribute__((constructor)) void HiDebugRegisterModule(void)
561 {
562 napi_module_register(&hidebugModule);
563 }
564 } // HiviewDFX
565 } // OHOS
566