106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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#ifndef HOOK_COMMON_H 1706f6ba60Sopenharmony_ci#define HOOK_COMMON_H 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#if defined(HAVE_LIBUNWIND) && HAVE_LIBUNWIND 2006f6ba60Sopenharmony_ci// for libunwind.h empty struct has size 0 in c, size 1 in c++ 2106f6ba60Sopenharmony_ci#define UNW_EMPTY_STRUCT uint8_t unused; 2206f6ba60Sopenharmony_ci#include <libunwind.h> 2306f6ba60Sopenharmony_ci#endif 2406f6ba60Sopenharmony_ci 2506f6ba60Sopenharmony_ci#include "register.h" 2606f6ba60Sopenharmony_ci#include "utilities.h" 2706f6ba60Sopenharmony_ci 2806f6ba60Sopenharmony_ci#define MAX_THREAD_NAME (32) 2906f6ba60Sopenharmony_ci#define MAX_UNWIND_DEPTH (100) 3006f6ba60Sopenharmony_ci 3106f6ba60Sopenharmony_cinamespace OHOS { 3206f6ba60Sopenharmony_cinamespace Developtools { 3306f6ba60Sopenharmony_cinamespace NativeDaemon { 3406f6ba60Sopenharmony_ciconst int STACK_DATA_SIZE = 40000; 3506f6ba60Sopenharmony_ciconst int SPEED_UP_THRESHOLD = STACK_DATA_SIZE / 2; 3606f6ba60Sopenharmony_ciconst int SLOW_DOWN_THRESHOLD = STACK_DATA_SIZE / 4; 3706f6ba60Sopenharmony_ciconst int32_t MIN_STACK_DEPTH = 6; 3806f6ba60Sopenharmony_ci// filter two layers of dwarf stack in libnative_hook.z.so 3906f6ba60Sopenharmony_ciconst size_t FILTER_STACK_DEPTH = 2; 4006f6ba60Sopenharmony_ciconst size_t MAX_CALL_FRAME_UNWIND_SIZE = MAX_UNWIND_DEPTH + FILTER_STACK_DEPTH; 4106f6ba60Sopenharmony_ci// dlopen function minimum stack depth 4206f6ba60Sopenharmony_ciconst int32_t DLOPEN_MIN_UNWIND_DEPTH = 5; 4306f6ba60Sopenharmony_ciconst uint32_t MMAP_FILE_TYPE = (1u << 8); 4406f6ba60Sopenharmony_ci// default max js stack depth 4506f6ba60Sopenharmony_ciconst int32_t DEFAULT_MAX_JS_STACK_DEPTH = 10; 4606f6ba60Sopenharmony_ci} 4706f6ba60Sopenharmony_ci} 4806f6ba60Sopenharmony_ci} 4906f6ba60Sopenharmony_ci 5006f6ba60Sopenharmony_ciconstexpr size_t MAX_REG_SIZE = sizeof(uint64_t) 5106f6ba60Sopenharmony_ci * OHOS::Developtools::NativeDaemon::PERF_REG_ARM64_MAX; 5206f6ba60Sopenharmony_ci 5306f6ba60Sopenharmony_cienum { 5406f6ba60Sopenharmony_ci MALLOCDISABLE = (1u << 0), 5506f6ba60Sopenharmony_ci MMAPDISABLE = (1u << 1), 5606f6ba60Sopenharmony_ci FREEMSGSTACK = (1u << 2), 5706f6ba60Sopenharmony_ci MUNMAPMSGSTACK = (1u << 3), 5806f6ba60Sopenharmony_ci FPUNWIND = (1u << 4), 5906f6ba60Sopenharmony_ci BLOCKED = (1u << 5), 6006f6ba60Sopenharmony_ci MEMTRACE_ENABLE = (1u << 6), 6106f6ba60Sopenharmony_ci}; 6206f6ba60Sopenharmony_ci 6306f6ba60Sopenharmony_cienum { 6406f6ba60Sopenharmony_ci MALLOC_MSG = 0, 6506f6ba60Sopenharmony_ci FREE_MSG, 6606f6ba60Sopenharmony_ci MMAP_MSG, 6706f6ba60Sopenharmony_ci MMAP_FILE_PAGE_MSG, 6806f6ba60Sopenharmony_ci MUNMAP_MSG, 6906f6ba60Sopenharmony_ci MEMORY_USING_MSG, 7006f6ba60Sopenharmony_ci MEMORY_UNUSING_MSG, 7106f6ba60Sopenharmony_ci MEMORY_TAG, 7206f6ba60Sopenharmony_ci THREAD_NAME_MSG, 7306f6ba60Sopenharmony_ci PR_SET_VMA_MSG, 7406f6ba60Sopenharmony_ci JS_STACK_MSG, 7506f6ba60Sopenharmony_ci NMD_MSG, 7606f6ba60Sopenharmony_ci END_MSG, 7706f6ba60Sopenharmony_ci}; 7806f6ba60Sopenharmony_ci 7906f6ba60Sopenharmony_cistruct alignas(8) MmapFileRawData { // 8 is 8 bit 8006f6ba60Sopenharmony_ci off_t offset; 8106f6ba60Sopenharmony_ci uint32_t flags; 8206f6ba60Sopenharmony_ci}; 8306f6ba60Sopenharmony_ci 8406f6ba60Sopenharmony_cistruct alignas(8) BaseStackRawData { // 8 is 8 bit 8506f6ba60Sopenharmony_ci union { 8606f6ba60Sopenharmony_ci struct timespec ts; 8706f6ba60Sopenharmony_ci MmapFileRawData mmapArgs; 8806f6ba60Sopenharmony_ci }; 8906f6ba60Sopenharmony_ci void* addr; 9006f6ba60Sopenharmony_ci size_t mallocSize; 9106f6ba60Sopenharmony_ci uint64_t jsChainId; 9206f6ba60Sopenharmony_ci uint32_t pid; 9306f6ba60Sopenharmony_ci uint32_t tid; 9406f6ba60Sopenharmony_ci uint16_t type; 9506f6ba60Sopenharmony_ci uint16_t tagId; 9606f6ba60Sopenharmony_ci}; 9706f6ba60Sopenharmony_ci 9806f6ba60Sopenharmony_cistruct alignas(8) StackRawData: public BaseStackRawData { // 8 is 8 bit 9906f6ba60Sopenharmony_ci union { 10006f6ba60Sopenharmony_ci char regs[MAX_REG_SIZE]; 10106f6ba60Sopenharmony_ci uint64_t ip[MAX_UNWIND_DEPTH]; 10206f6ba60Sopenharmony_ci char name[PATH_MAX + 1] {0}; 10306f6ba60Sopenharmony_ci }; 10406f6ba60Sopenharmony_ci}; 10506f6ba60Sopenharmony_ci 10606f6ba60Sopenharmony_cistruct alignas(8) ReportEventBaseData { // 8 is 8 bit 10706f6ba60Sopenharmony_ci ReportEventBaseData(const BaseStackRawData* baseData, uint32_t id = 0) 10806f6ba60Sopenharmony_ci { 10906f6ba60Sopenharmony_ci ts = baseData->ts; 11006f6ba60Sopenharmony_ci addr = reinterpret_cast<uint64_t>(baseData->addr); 11106f6ba60Sopenharmony_ci type = baseData->type; 11206f6ba60Sopenharmony_ci tagId = baseData->tagId; 11306f6ba60Sopenharmony_ci mallocSize = baseData->mallocSize; 11406f6ba60Sopenharmony_ci tid = baseData->tid; 11506f6ba60Sopenharmony_ci stackMapId = id; 11606f6ba60Sopenharmony_ci } 11706f6ba60Sopenharmony_ci struct timespec ts; 11806f6ba60Sopenharmony_ci uint64_t addr : 40; 11906f6ba60Sopenharmony_ci uint64_t type : 8; 12006f6ba60Sopenharmony_ci uint64_t tagId : 16; 12106f6ba60Sopenharmony_ci uint32_t mallocSize; 12206f6ba60Sopenharmony_ci uint32_t tid; 12306f6ba60Sopenharmony_ci uint32_t stackMapId; 12406f6ba60Sopenharmony_ci}; 12506f6ba60Sopenharmony_ci 12606f6ba60Sopenharmony_cistruct alignas(8) ArkTsClientConfig { // 8 is 8 bit 12706f6ba60Sopenharmony_ci int32_t jsStackReport = 0; 12806f6ba60Sopenharmony_ci uint8_t maxJsStackDepth = 0; 12906f6ba60Sopenharmony_ci bool jsFpunwind = false; 13006f6ba60Sopenharmony_ci char filterNapiName[64] = {""}; 13106f6ba60Sopenharmony_ci}; 13206f6ba60Sopenharmony_ci 13306f6ba60Sopenharmony_cistruct alignas(8) ClientConfig { // 8 is 8 bit 13406f6ba60Sopenharmony_ci void Reset() 13506f6ba60Sopenharmony_ci { 13606f6ba60Sopenharmony_ci filterSize = -1; 13706f6ba60Sopenharmony_ci shareMemorySize = 0; 13806f6ba60Sopenharmony_ci clockId = CLOCK_REALTIME; 13906f6ba60Sopenharmony_ci maxStackDepth = 0; 14006f6ba60Sopenharmony_ci statisticsInterval = 0; 14106f6ba60Sopenharmony_ci sampleInterval = 0; 14206f6ba60Sopenharmony_ci mallocDisable = false; 14306f6ba60Sopenharmony_ci mmapDisable = false; 14406f6ba60Sopenharmony_ci freeStackData = false; 14506f6ba60Sopenharmony_ci munmapStackData = false; 14606f6ba60Sopenharmony_ci fpunwind = false; 14706f6ba60Sopenharmony_ci isBlocked = false; 14806f6ba60Sopenharmony_ci memtraceEnable = false; 14906f6ba60Sopenharmony_ci responseLibraryMode = false; 15006f6ba60Sopenharmony_ci freeEventOnlyAddrEnable = false; 15106f6ba60Sopenharmony_ci printNmd = false; 15206f6ba60Sopenharmony_ci nmdType = -1; 15306f6ba60Sopenharmony_ci arktsConfig.jsStackReport = 0; 15406f6ba60Sopenharmony_ci arktsConfig.maxJsStackDepth = 0; 15506f6ba60Sopenharmony_ci arktsConfig.jsFpunwind = false; 15606f6ba60Sopenharmony_ci arktsConfig.filterNapiName[0] = '\0'; 15706f6ba60Sopenharmony_ci } 15806f6ba60Sopenharmony_ci 15906f6ba60Sopenharmony_ci std::string ToString() 16006f6ba60Sopenharmony_ci { 16106f6ba60Sopenharmony_ci std::stringstream ss; 16206f6ba60Sopenharmony_ci ss << "filterSize:" << filterSize << ", shareMemorySize:" << shareMemorySize 16306f6ba60Sopenharmony_ci << ", clockId:" << clockId << ", maxStackDepth:" << std::to_string(maxStackDepth) 16406f6ba60Sopenharmony_ci << ", mallocDisable:" << mallocDisable << ", mmapDisable:" << mmapDisable 16506f6ba60Sopenharmony_ci << ", freeStackData:" << freeStackData << ", munmapStackData:" << munmapStackData 16606f6ba60Sopenharmony_ci << ", fpunwind:" << fpunwind << ", isBlocked:" << isBlocked << ", memtraceEnable:" << memtraceEnable 16706f6ba60Sopenharmony_ci << ", sampleInterval: " << sampleInterval << ", responseLibraryMode: " << responseLibraryMode 16806f6ba60Sopenharmony_ci << ", freeEventOnlyAddrEnable: " << freeEventOnlyAddrEnable << ", jsStackReport: " 16906f6ba60Sopenharmony_ci << arktsConfig.jsStackReport << ", maxJsStackDepth: " 17006f6ba60Sopenharmony_ci << std::to_string(arktsConfig.maxJsStackDepth) << ", filterNapiName: " 17106f6ba60Sopenharmony_ci << arktsConfig.filterNapiName << ", jsFpunwind: " << arktsConfig.jsFpunwind; 17206f6ba60Sopenharmony_ci return ss.str(); 17306f6ba60Sopenharmony_ci } 17406f6ba60Sopenharmony_ci 17506f6ba60Sopenharmony_ci int32_t filterSize = -1; 17606f6ba60Sopenharmony_ci uint32_t shareMemorySize = 0; 17706f6ba60Sopenharmony_ci uint32_t sampleInterval = 0; 17806f6ba60Sopenharmony_ci uint32_t statisticsInterval = 0; 17906f6ba60Sopenharmony_ci clockid_t clockId = CLOCK_REALTIME; 18006f6ba60Sopenharmony_ci uint8_t maxStackDepth = 0; 18106f6ba60Sopenharmony_ci bool mallocDisable = false; 18206f6ba60Sopenharmony_ci bool mmapDisable = false; 18306f6ba60Sopenharmony_ci bool freeStackData = false; 18406f6ba60Sopenharmony_ci bool munmapStackData = false; 18506f6ba60Sopenharmony_ci bool fpunwind = false; 18606f6ba60Sopenharmony_ci bool isBlocked = false; 18706f6ba60Sopenharmony_ci bool memtraceEnable = false; 18806f6ba60Sopenharmony_ci bool responseLibraryMode = false; 18906f6ba60Sopenharmony_ci bool freeEventOnlyAddrEnable = false; 19006f6ba60Sopenharmony_ci bool printNmd = false; 19106f6ba60Sopenharmony_ci int nmdType = -1; 19206f6ba60Sopenharmony_ci ArkTsClientConfig arktsConfig = {0}; 19306f6ba60Sopenharmony_ci}; 19406f6ba60Sopenharmony_ci#endif // HOOK_COMMON_H