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 #ifndef DFX_TEST_UTIL_H 16 #define DFX_TEST_UTIL_H 17 18 #include <string> 19 #include <vector> 20 #include <csignal> 21 #include <sys/wait.h> 22 23 #define NOINLINE __attribute__((noinline)) 24 25 #define ACCOUNTMGR_NAME "accountmgr" 26 #define FOUNDATION_NAME "foundation" 27 #define APPSPAWN_NAME "appspawn" 28 #define TEST_BUNDLE_NAME "com.example.myapplication" 29 #define TRUNCATE_TEST_BUNDLE_NAME "e.myapplication" 30 31 32 #if defined(__arm__) 33 #define REGISTERS "r0:","r1:","r2:","r3:","r4:","r5:","r6:",\ 34 "r7:","r8:","r9:","r10:","fp:","ip:","sp:","lr:","pc:" 35 #define REGISTERS_NUM 16 36 #define REGISTER_FORMAT_LENGTH 8 37 #elif defined(__aarch64__) 38 #define REGISTERS "x0:","x1:","x2:","x3:","x4:","x5:","x6:","x7:","x8:",\ 39 "x9:","x10:","x11:","x12:","x13:","x14:","x15:","x16:",\ 40 "x17:","x18:","x19:","x20:","x21:","x22:","x23:","x24:",\ 41 "x25:","x26:","x27:","x28:","x29:","lr:","sp:","pc:" 42 #define REGISTERS_NUM 33 43 #define REGISTER_FORMAT_LENGTH 16 44 #elif defined(__x86_64__) 45 #define REGISTERS "rax:","rdx:","rcx:","rbx:","rsi:","rdi:","rbp:","rsp:",\ 46 "r8:","r9:","r10:","r11:","r12:","r13:","r14:","r15:","rip:" 47 #define REGISTERS_NUM 17 48 #define REGISTER_FORMAT_LENGTH 16 49 #endif 50 const char* const TEMP_DIR = "/data/log/faultlog/temp/"; 51 52 namespace OHOS { 53 namespace HiviewDFX { 54 class TestScopedPidReaper { 55 public: TestScopedPidReaper(pid_t pid)56 explicit TestScopedPidReaper(pid_t pid) : pid_(pid) {} ~TestScopedPidReaper()57 ~TestScopedPidReaper() { Kill(pid_); } 58 Kill(pid_t pid)59 static void Kill(pid_t pid) 60 { 61 if (pid <= 0) { 62 return; 63 } 64 kill(pid, SIGKILL); 65 waitpid(pid, nullptr, 0); 66 } 67 private: 68 pid_t pid_; 69 }; 70 71 enum CrasherType { 72 CRASHER_C, 73 CRASHER_CPP 74 }; 75 std::string ExecuteCommands(const std::string& cmds); 76 bool ExecuteCommands(const std::string& cmds, std::vector<std::string>& ress); 77 int GetProcessPid(const std::string& processName); 78 int LaunchTestHap(const std::string& abilityName, const std::string& bundleName); 79 void StopTestHap(const std::string& bundleName); 80 void InstallTestHap(const std::string& hapName); 81 void UninstallTestHap(const std::string& bundleName); 82 int CountLines(const std::string& fileName); 83 bool CheckProcessComm(int pid, const std::string& name); 84 int CheckKeyWords(const std::string& filePath, std::string *keywords, int length, int minRegIdx); 85 bool CheckContent(const std::string& content, const std::string& keyContent, bool checkExist); 86 int GetKeywordsNum(const std::string& msg, std::string *keywords, int length); 87 std::string GetCppCrashFileName(const pid_t pid, const std::string& tempPath = TEMP_DIR); 88 uint32_t GetSelfFdCount(); 89 uint32_t GetSelfMapsCount(); 90 uint64_t GetSelfMemoryCount(); 91 void CheckResourceUsage(uint32_t fdCount, uint32_t mapsCount, uint64_t memCount); 92 } // namespace HiviewDFX 93 } // namespace OHOS 94 #endif // DFX_TEST_UTIL