1 /* 2 * Copyright (c) 2021 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 #ifndef HOOK_COMMON_H 17 #define HOOK_COMMON_H 18 19 #if defined(HAVE_LIBUNWIND) && HAVE_LIBUNWIND 20 // for libunwind.h empty struct has size 0 in c, size 1 in c++ 21 #define UNW_EMPTY_STRUCT uint8_t unused; 22 #include <libunwind.h> 23 #endif 24 25 #include "register.h" 26 #include "utilities.h" 27 28 #define MAX_THREAD_NAME (32) 29 #define MAX_UNWIND_DEPTH (100) 30 31 namespace OHOS { 32 namespace Developtools { 33 namespace NativeDaemon { 34 const int STACK_DATA_SIZE = 40000; 35 const int SPEED_UP_THRESHOLD = STACK_DATA_SIZE / 2; 36 const int SLOW_DOWN_THRESHOLD = STACK_DATA_SIZE / 4; 37 const int32_t MIN_STACK_DEPTH = 6; 38 // filter two layers of dwarf stack in libnative_hook.z.so 39 const size_t FILTER_STACK_DEPTH = 2; 40 const size_t MAX_CALL_FRAME_UNWIND_SIZE = MAX_UNWIND_DEPTH + FILTER_STACK_DEPTH; 41 // dlopen function minimum stack depth 42 const int32_t DLOPEN_MIN_UNWIND_DEPTH = 5; 43 const uint32_t MMAP_FILE_TYPE = (1u << 8); 44 } 45 } 46 } 47 48 constexpr size_t MAX_REG_SIZE = sizeof(uint64_t) 49 * OHOS::Developtools::NativeDaemon::PERF_REG_ARM64_MAX; 50 51 enum { 52 MALLOCDISABLE = (1u << 0), 53 MMAPDISABLE = (1u << 1), 54 FREEMSGSTACK = (1u << 2), 55 MUNMAPMSGSTACK = (1u << 3), 56 FPUNWIND = (1u << 4), 57 BLOCKED = (1u << 5), 58 MEMTRACE_ENABLE = (1u << 6), 59 }; 60 61 enum { 62 MALLOC_MSG = 0, 63 FREE_MSG, 64 MMAP_MSG, 65 MMAP_FILE_PAGE_MSG, 66 MUNMAP_MSG, 67 MEMORY_USING_MSG, 68 MEMORY_UNUSING_MSG, 69 MEMORY_TAG, 70 THREAD_NAME_MSG, 71 PR_SET_VMA_MSG, 72 }; 73 74 struct alignas(8) MmapFileRawData { // 8 is 8 bit 75 off_t offset; 76 uint32_t flags; 77 }; 78 79 struct alignas(8) BaseStackRawData { // 8 is 8 bit 80 union { 81 struct timespec ts; 82 MmapFileRawData mmapArgs; 83 }; 84 void* addr; 85 size_t mallocSize; 86 uint32_t pid; 87 uint32_t tid; 88 uint16_t type; 89 uint16_t tagId; 90 }; 91 92 struct alignas(8) StackRawData: public BaseStackRawData { // 8 is 8 bit 93 union { 94 char regs[MAX_REG_SIZE]; 95 uint64_t ip[MAX_UNWIND_DEPTH]; 96 char name[PATH_MAX + 1] {0}; 97 }; 98 }; 99 100 struct alignas(8) ClientConfig { // 8 is 8 bit ResetClientConfig101 void Reset() 102 { 103 filterSize = -1; 104 shareMemroySize = 0; 105 clockId = CLOCK_REALTIME; 106 maxStackDepth = 0; 107 mallocDisable = false; 108 mmapDisable = false; 109 freeStackData = false; 110 munmapStackData = false; 111 fpunwind = false; 112 isBlocked = false; 113 memtraceEnable = false; 114 } 115 ToStringClientConfig116 std::string ToString() 117 { 118 std::stringstream ss; 119 ss << "filterSize:" << filterSize << ", shareMemroySize:" << shareMemroySize 120 << ", clockId:" << clockId << ", maxStackDepth:" << std::to_string(maxStackDepth) 121 << ", mallocDisable:" << mallocDisable << ", mmapDisable:" << mmapDisable 122 << ", freeStackData:" << freeStackData << ", munmapStackData:" << munmapStackData 123 << ", fpunwind:" << fpunwind << ", isBlocked:" << isBlocked << ", memtraceEnable:" << memtraceEnable; 124 return ss.str(); 125 } 126 127 int32_t filterSize = -1; 128 uint32_t shareMemroySize = 0; 129 clockid_t clockId = CLOCK_REALTIME; 130 uint8_t maxStackDepth = 0; 131 bool mallocDisable = false; 132 bool mmapDisable = false; 133 bool freeStackData = false; 134 bool munmapStackData = false; 135 bool fpunwind = false; 136 bool isBlocked = false; 137 bool memtraceEnable = false; 138 }; 139 140 struct StandaloneRawStack { 141 BaseStackRawData* stackConext; // points to the foundation type data 142 uint8_t* stackData; 143 int8_t* data; // fp mode data is ip, dwarf mode data is regs 144 uint32_t stackSize; 145 uint8_t fpDepth; // fp mode fpDepth is ip depth, dwarf mode is invalid 146 }; 147 148 #endif // HOOK_COMMON_H