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