diff --git a/PerformanceAnalysisKit/HiDebugTool/entry/src/main/cpp/test_malloc_dispatch.cpp b/PerformanceAnalysisKit/HiDebugTool/entry/src/main/cpp/test_malloc_dispatch.cpp index c60fd0044e866917a6bc22acd578be10fa5ab3bf..6f1b4d4bdb0e6bcf388f3971e4e419c2baa6897e 100644 --- a/PerformanceAnalysisKit/HiDebugTool/entry/src/main/cpp/test_malloc_dispatch.cpp +++ b/PerformanceAnalysisKit/HiDebugTool/entry/src/main/cpp/test_malloc_dispatch.cpp @@ -13,7 +13,6 @@ * limitations under the License. */ -// [Start MallocDispatchImpl] #include #include #include @@ -26,9 +25,10 @@ #include #include "string.h" #include +// [Start MallocDispatchTableImport] #include "hidebug/hidebug.h" #include "hidebug/hidebug_type.h" -#include "hidebug/hidebug.h" +// [End MallocDispatchTableImport] #include "hilog/log.h" #pragma clang optimize off @@ -54,20 +54,22 @@ static void MyFree(void* ptr) original->free(ptr); } +// [Start DIYMapFunctions] static void* MyMmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset) { HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable(); - printf("test my_mmap----\n"); - OH_LOG_INFO(LOG_APP, "test MyMmap"); - return original->mmap(addr, len, prot, flags, fd, offset); + void* returnAddr = original->mmap(addr, len, prot, flags, fd, offset); + OH_LOG_INFO(LOG_APP, "test MyMmap with len:%{public}d and addr:%{public}p", len, returnAddr); + return returnAddr; } static int MyMunmap(void* addr, size_t len) { HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable(); - printf("test my_munmap----\n"); + OH_LOG_INFO(LOG_APP, "test MyMunmap with len:%{public}d and addr:%{public}p", len, addr); return original->munmap(addr, len); } +// [End DIYMapFunctions] static void* MyCalloc(size_t nmemb, size_t size) { @@ -85,24 +87,35 @@ static void* MyRealloc(void* ptr, size_t size) HiDebug_MallocDispatch* InitCustomMalloc() { + // [Start InitCustomMalloc] + //Obtain default MallocDispatchTable that can allocate memory directly. HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable(); + //Create a MallocDispatchTable struct called current. HiDebug_MallocDispatch* current = (HiDebug_MallocDispatch*)original->malloc(sizeof(HiDebug_MallocDispatch)); memset(current, 0, sizeof(HiDebug_MallocDispatch)); - current->malloc = MyMalloc; - current->free = MyFree; + //replace function pointers of current, from which self-defined functions can be redirected. current->mmap = MyMmap; current->munmap = MyMunmap; + // [End InitCustomMalloc] + current->malloc = MyMalloc; + current->free = MyFree; current->calloc = MyCalloc; current->realloc = MyRealloc; + // [Start SetMallocDispatchTable] OH_HiDebug_SetMallocDispatchTable(current); + // [End SetMallocDispatchTable] return current; } void DesCustomMalloc(HiDebug_MallocDispatch* current) { + // [Start DesCustomMalloc] + //release memory of self-defined MallocDispatchTable struct. HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable(); original->free(current); + //reset MallocDispatchTable strut that libc uses. OH_HiDebug_RestoreMallocDispatchTable(); + // [End DesCustomMalloc] } void TestMalloc() @@ -126,6 +139,7 @@ void TestMalloc() } void TestMmap() { + // [Start TestMallocDispatchMmap] char* mapPtr = nullptr; const size_t bufferSize = 100; // 100 : the size of memory mapPtr = (char*)mmap(nullptr, bufferSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); @@ -134,6 +148,7 @@ void TestMmap() return; } munmap(mapPtr, bufferSize); + // [End TestMallocDispatchMmap] } void TestCalloc() @@ -296,5 +311,4 @@ void AllocateMemoryImpl() TestMmap(); } -#pragma clang optimize on -// [End MallocDispatchImpl] \ No newline at end of file +#pragma clang optimize on \ No newline at end of file diff --git a/PerformanceAnalysisKit/PerformanceAnalysisTool/entry/src/main/ets/pages/Index.ets b/PerformanceAnalysisKit/PerformanceAnalysisTool/entry/src/main/ets/pages/Index.ets index eb41fa38c369b6b0c29931fe8345382788f5d2a4..483954307a6c4e9211f292e672c62e94936d3a64 100644 --- a/PerformanceAnalysisKit/PerformanceAnalysisTool/entry/src/main/ets/pages/Index.ets +++ b/PerformanceAnalysisKit/PerformanceAnalysisTool/entry/src/main/ets/pages/Index.ets @@ -16,9 +16,13 @@ // [Start Project_Header] import { hilog, hiTraceMeter, hiTraceChain, hidebug, hiAppEvent } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; -import jsLeakWatcher from '@ohos.hiviewdfx.jsLeakWatcher'; import testNapi from 'libentry.so'; // [End Project_Header] + +// [Start JsLeakWatcherImport] +import jsLeakWatcher from '@ohos.hiviewdfx.jsLeakWatcher'; +// [End JsLeakWatcherImport] + // [Start testHidebug] function testHidebug(event?: ClickEvent) { try { @@ -29,21 +33,22 @@ function testHidebug(event?: ClickEvent) { } // [End testHidebug] -// [Start testJsLeakWatcher] - function testEnableJsLeakWatcher(event?: ClickEvent) { +// [Start testJsLeakWatcherEnable] let config : Array = []; jsLeakWatcher.enableLeakWatcher(true, config, (filepath: Array) => { console.log('testJsLeakWatcher leakListFileName: ' + filepath[0]); console.log('testJsLeakWatcher heapDumpFileName: ' + filepath[1]); }); +// [End testJsLeakWatcherEnable] } function testDisableJsLeakWatcher(event?: ClickEvent) { +// [Start testJsLeakWatcherDisable] let config : Array = []; jsLeakWatcher.enableLeakWatcher(false, config, () => {}); +// [End testJsLeakWatcherDisable] } -// [End testJsLeakWatcher] @Entry @Component