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