1 /*
2  * Copyright (c) 2024 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 
17 #include <fstream>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 
22 #include "hiebpf_collector_impl.h"
23 #include "hiebpf_decorator.h"
24 #include "hiview_logger.h"
25 
26 using namespace OHOS::HiviewDFX::UCollect;
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace UCollectUtil {
30 DEFINE_LOG_TAG("UCollectUtil-HiebpfCollectorImpl");
31 
Create()32 std::shared_ptr<HiebpfCollector> HiebpfCollector::Create()
33 {
34     return std::make_shared<HiebpfDecorator>(std::make_shared<HiebpfCollectorImpl>());
35 }
36 
StartHiebpf(int duration,const std::string processName,const std::string outFile)37 CollectResult<bool> HiebpfCollectorImpl::StartHiebpf(int duration,
38     const std::string processName,
39     const std::string outFile)
40 {
41     CollectResult<bool> result;
42     int childPid = fork();
43     if (childPid < 0) {
44         result.data = false;
45         result.retCode = UCollect::UcError::UNSUPPORT;
46         return result;
47     } else if (childPid == 0) {
48         std::string timestr = std::to_string(duration);
49         execl("/system/bin/hmpsf", "hmpsf", "--events", "thread", "--duration", timestr.c_str(),
50             "--target_proc_name", processName.c_str(), "--start", "true", "--outprase_file", outFile.c_str(), NULL);
51     } else {
52         result.retCode = UCollect::UcError::SUCCESS;
53         if (waitpid(childPid, nullptr, 0) != childPid) {
54             HIVIEW_LOGE("waitpid fail, pid: = %{public}d, errno = %{public}d", childPid, errno);
55         } else {
56             HIVIEW_LOGE("waitpid success, pid: = %{public}d", childPid);
57         }
58     }
59     return result;
60 }
61 
StopHiebpf()62 CollectResult<bool> HiebpfCollectorImpl::StopHiebpf()
63 {
64     CollectResult<bool> result;
65     int childPid = fork();
66     if (childPid < 0) {
67         result.data = false;
68         result.retCode = UCollect::UcError::UNSUPPORT;
69         return result;
70     } else if (childPid == 0) {
71         execl("/system/bin/hmpsf", "hmpsf", "--stop", "true", NULL);
72     } else {
73         result.retCode = UCollect::UcError::SUCCESS;
74         if (waitpid(childPid, nullptr, 0) != childPid) {
75             HIVIEW_LOGE("waitpid fail, pid: = %{public}d, errno = %{public}d", childPid, errno);
76         } else {
77             HIVIEW_LOGE("waitpid success, pid: = %{public}d", childPid);
78         }
79     }
80     return result;
81 }
82 } // UCollectUtil
83 } // HivewDFX
84 } // OHOS
85