1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifndef DFX_TEST_UTIL_H
16800b99b8Sopenharmony_ci#define DFX_TEST_UTIL_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <string>
19800b99b8Sopenharmony_ci#include <vector>
20800b99b8Sopenharmony_ci#include <csignal>
21800b99b8Sopenharmony_ci#include <sys/wait.h>
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_ci#define NOINLINE __attribute__((noinline))
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_ci#define ACCOUNTMGR_NAME           "accountmgr"
26800b99b8Sopenharmony_ci#define FOUNDATION_NAME           "foundation"
27800b99b8Sopenharmony_ci#define APPSPAWN_NAME             "appspawn"
28800b99b8Sopenharmony_ci#define TEST_BUNDLE_NAME          "com.example.myapplication"
29800b99b8Sopenharmony_ci#define TRUNCATE_TEST_BUNDLE_NAME "e.myapplication"
30800b99b8Sopenharmony_ci
31800b99b8Sopenharmony_ci
32800b99b8Sopenharmony_ci#if defined(__arm__)
33800b99b8Sopenharmony_ci#define REGISTERS           "r0:","r1:","r2:","r3:","r4:","r5:","r6:",\
34800b99b8Sopenharmony_ci                            "r7:","r8:","r9:","r10:","fp:","ip:","sp:","lr:","pc:"
35800b99b8Sopenharmony_ci#define REGISTERS_NUM       16
36800b99b8Sopenharmony_ci#define REGISTER_FORMAT_LENGTH    8
37800b99b8Sopenharmony_ci#elif defined(__aarch64__)
38800b99b8Sopenharmony_ci#define REGISTERS           "x0:","x1:","x2:","x3:","x4:","x5:","x6:","x7:","x8:",\
39800b99b8Sopenharmony_ci                            "x9:","x10:","x11:","x12:","x13:","x14:","x15:","x16:",\
40800b99b8Sopenharmony_ci                            "x17:","x18:","x19:","x20:","x21:","x22:","x23:","x24:",\
41800b99b8Sopenharmony_ci                            "x25:","x26:","x27:","x28:","x29:","lr:","sp:","pc:"
42800b99b8Sopenharmony_ci#define REGISTERS_NUM       33
43800b99b8Sopenharmony_ci#define REGISTER_FORMAT_LENGTH    16
44800b99b8Sopenharmony_ci#elif defined(__x86_64__)
45800b99b8Sopenharmony_ci#define REGISTERS           "rax:","rdx:","rcx:","rbx:","rsi:","rdi:","rbp:","rsp:",\
46800b99b8Sopenharmony_ci                            "r8:","r9:","r10:","r11:","r12:","r13:","r14:","r15:","rip:"
47800b99b8Sopenharmony_ci#define REGISTERS_NUM       17
48800b99b8Sopenharmony_ci#define REGISTER_FORMAT_LENGTH    16
49800b99b8Sopenharmony_ci#endif
50800b99b8Sopenharmony_ciconst char* const TEMP_DIR = "/data/log/faultlog/temp/";
51800b99b8Sopenharmony_ci
52800b99b8Sopenharmony_cinamespace OHOS {
53800b99b8Sopenharmony_cinamespace HiviewDFX {
54800b99b8Sopenharmony_ciclass TestScopedPidReaper {
55800b99b8Sopenharmony_cipublic:
56800b99b8Sopenharmony_ci    explicit TestScopedPidReaper(pid_t pid) : pid_(pid) {}
57800b99b8Sopenharmony_ci    ~TestScopedPidReaper() { Kill(pid_); }
58800b99b8Sopenharmony_ci
59800b99b8Sopenharmony_ci    static void Kill(pid_t pid)
60800b99b8Sopenharmony_ci    {
61800b99b8Sopenharmony_ci        if (pid <= 0) {
62800b99b8Sopenharmony_ci            return;
63800b99b8Sopenharmony_ci        }
64800b99b8Sopenharmony_ci        kill(pid, SIGKILL);
65800b99b8Sopenharmony_ci        waitpid(pid, nullptr, 0);
66800b99b8Sopenharmony_ci    }
67800b99b8Sopenharmony_ciprivate:
68800b99b8Sopenharmony_ci    pid_t pid_;
69800b99b8Sopenharmony_ci};
70800b99b8Sopenharmony_ci
71800b99b8Sopenharmony_cienum CrasherType {
72800b99b8Sopenharmony_ci    CRASHER_C,
73800b99b8Sopenharmony_ci    CRASHER_CPP
74800b99b8Sopenharmony_ci};
75800b99b8Sopenharmony_cistd::string ExecuteCommands(const std::string& cmds);
76800b99b8Sopenharmony_cibool ExecuteCommands(const std::string& cmds, std::vector<std::string>& ress);
77800b99b8Sopenharmony_ciint GetProcessPid(const std::string& processName);
78800b99b8Sopenharmony_ciint LaunchTestHap(const std::string& abilityName, const std::string& bundleName);
79800b99b8Sopenharmony_civoid StopTestHap(const std::string& bundleName);
80800b99b8Sopenharmony_civoid InstallTestHap(const std::string& hapName);
81800b99b8Sopenharmony_civoid UninstallTestHap(const std::string& bundleName);
82800b99b8Sopenharmony_ciint CountLines(const std::string& fileName);
83800b99b8Sopenharmony_cibool CheckProcessComm(int pid, const std::string& name);
84800b99b8Sopenharmony_ciint CheckKeyWords(const std::string& filePath, std::string *keywords, int length, int minRegIdx);
85800b99b8Sopenharmony_cibool CheckContent(const std::string& content, const std::string& keyContent, bool checkExist);
86800b99b8Sopenharmony_ciint GetKeywordsNum(const std::string& msg, std::string *keywords, int length);
87800b99b8Sopenharmony_cistd::string GetCppCrashFileName(const pid_t pid, const std::string& tempPath = TEMP_DIR);
88800b99b8Sopenharmony_ciuint32_t GetSelfFdCount();
89800b99b8Sopenharmony_ciuint32_t GetSelfMapsCount();
90800b99b8Sopenharmony_ciuint64_t GetSelfMemoryCount();
91800b99b8Sopenharmony_civoid CheckResourceUsage(uint32_t fdCount, uint32_t mapsCount, uint64_t memCount);
92800b99b8Sopenharmony_ci} // namespace HiviewDFX
93800b99b8Sopenharmony_ci} // namespace OHOS
94800b99b8Sopenharmony_ci#endif // DFX_TEST_UTIL