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 if (ptrace(PTRACE_DETACH, tid, nullptr, nullptr) == 0) {
37 return;
38 }
39 DFXLOGW("Failed to detach tid(%{public}d), errno=%{public}d", tid, errno);
40 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) {
41 DFXLOGW("Failed to ptrace interrupt tid(%{public}d), errno=%{public}d", tid, errno);
42 return;
43 }
44 if (waitpid(tid, nullptr, 0) < 0) {
45 DFXLOGW("Failed to waitpid tid(%{public}d), errno=%{public}d", tid, errno);
46 return;
47 }
48
49 ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
50 }
51 }
52
Attach(pid_t tid,int timeout)53 bool DfxPtrace::Attach(pid_t tid, int timeout)
54 {
55 if (tid <= 0) {
56 return false;
57 }
58
59 if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) {
60 DFXLOGW("Failed to seize tid(%{public}d), errno=%{public}d", tid, errno);
61 return false;
62 }
63
64 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) {
65 DFXLOGW("Failed to interrupt tid(%{public}d), errno=%{public}d", tid, errno);
66 ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
67 return false;
68 }
69
70 int64_t startTime = std::chrono::duration_cast<std::chrono::milliseconds>(
71 std::chrono::system_clock::now().time_since_epoch()).count();
72 int waitStatus = 0;
73 do {
74 if (waitpid(tid, &waitStatus, WNOHANG) > 0) {
75 if (((waitStatus >> 16U) & 0xffU) == PTRACE_EVENT_STOP) { // 16 : stop event flag
76 break;
77 } else {
78 ptrace(PTRACE_CONT, tid, 0, 0); // clear old event
79 continue;
80 }
81 }
82 int64_t curTime = std::chrono::duration_cast<std::chrono::milliseconds>(
83 std::chrono::system_clock::now().time_since_epoch()).count();
84 if (curTime - startTime > timeout) {
85 ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
86 DFXLOGW("Failed to wait tid(%{public}d) attached.", tid);
87 return false;
88 }
89 usleep(5); // 5 : sleep 5us
90 } while (true);
91 return true;
92 }
93 } // namespace HiviewDFX
94 } // namespace OHOS
95