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 
16 #include "dfx_ptrace.h"
17 #include <chrono>
18 #include <sys/ptrace.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 #include "dfx_define.h"
22 #include "dfx_log.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 #undef LOG_DOMAIN
28 #undef LOG_TAG
29 #define LOG_DOMAIN 0xD002D11
30 #define LOG_TAG "DfxPtrace"
31 }
32 
Detach(pid_t tid)33 void DfxPtrace::Detach(pid_t tid)
34 {
35     if (tid > 0) {
36         ptrace(PTRACE_CONT, tid, 0, 0);
37         ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
38     }
39 }
40 
Attach(pid_t tid,int timeout)41 bool DfxPtrace::Attach(pid_t tid, int timeout)
42 {
43     if (tid <= 0) {
44         return false;
45     }
46 
47     if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) {
48         LOGW("Failed to seize tid(%d), errno=%d", tid, errno);
49         return false;
50     }
51 
52     if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) {
53         LOGW("Failed to interrupt tid(%d), errno=%d", tid, errno);
54         ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
55         return false;
56     }
57 
58     int64_t startTime = std::chrono::duration_cast<std::chrono::milliseconds>(
59         std::chrono::system_clock::now().time_since_epoch()).count();
60     do {
61         if (waitpid(tid, nullptr, WNOHANG) > 0) {
62             break;
63         }
64         int64_t curTime = std::chrono::duration_cast<std::chrono::milliseconds>(
65             std::chrono::system_clock::now().time_since_epoch()).count();
66         if (curTime - startTime > timeout) {
67             ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
68             LOGW("Failed to wait tid(%d) attached.", tid);
69             return false;
70         }
71         usleep(5); // 5 : sleep 5us
72     } while (true);
73     return true;
74 }
75 } // namespace HiviewDFX
76 } // namespace OHOS
77