1 /*
2 * Copyright (c) 2025 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 #include <cstdio>
16 #include <csignal>
17 #include <cstring>
18 #include <memory>
19 #include <pthread.h>
20 #include <securec.h>
21 #include "dfx_define.h"
22 #include "stack_utils.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
StackUtils()26 StackUtils::StackUtils()
27 {
28 ParseSelfMaps();
29 }
30
Instance()31 StackUtils& StackUtils::Instance()
32 {
33 static StackUtils inst;
34 return inst;
35 }
36
GetMainStackRange(uintptr_t & stackBottom,uintptr_t & stackTop) const37 bool StackUtils::GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop) const
38 {
39 if (!mainStack_.IsValid()) {
40 return false;
41 }
42 stackBottom = mainStack_.start;
43 stackTop = mainStack_.end;
44 return true;
45 }
46
GetArkStackRange(uintptr_t & start,uintptr_t & end) const47 bool StackUtils::GetArkStackRange(uintptr_t& start, uintptr_t& end) const
48 {
49 if (!arkCode_.IsValid()) {
50 return false;
51 }
52 start = arkCode_.start;
53 end = arkCode_.end;
54 return true;
55 }
56
GetSelfStackRange(uintptr_t & stackBottom,uintptr_t & stackTop)57 bool StackUtils::GetSelfStackRange(uintptr_t& stackBottom, uintptr_t& stackTop)
58 {
59 pthread_attr_t tattr;
60 if (pthread_getattr_np(pthread_self(), &tattr) != 0) {
61 return false;
62 }
63 void *base = nullptr;
64 size_t size = 0;
65 bool ret = false;
66 if (pthread_attr_getstack(&tattr, &base, &size) == 0) {
67 stackBottom = reinterpret_cast<uintptr_t>(base);
68 stackTop = reinterpret_cast<uintptr_t>(base) + size;
69 ret = true;
70 }
71 pthread_attr_destroy(&tattr);
72 return ret;
73 }
74
ParseSelfMaps()75 void StackUtils::ParseSelfMaps()
76 {
77 std::unique_ptr<FILE, void (*)(FILE*)> fp(fopen(PROC_SELF_MAPS_PATH, "r"), [](FILE* fp) {
78 if (fp != nullptr) {
79 fclose(fp);
80 }
81 });
82 if (fp == nullptr) {
83 return;
84 }
85 auto parser = [] (const char *mapInfo, bool checkExec, MapRange &range) {
86 uint64_t begin = 0;
87 uint64_t end = 0;
88 uint64_t offset = 0;
89 char perms[5] = {0}; // 5:rwxp
90 int pos = 0;
91 if (sscanf_s(mapInfo, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %*x:%*x %*d%n", &begin, &end,
92 &perms, sizeof(perms), &offset, &pos) != 4) { // 4:scan size
93 return;
94 }
95 if (checkExec && strcmp(perms, "r-xp") != 0) {
96 return;
97 }
98 range.start = begin;
99 range.end = end;
100 };
101
102 char mapInfo[256] = {0}; // 256: map info size
103 while (fgets(mapInfo, sizeof(mapInfo), fp.get()) != nullptr) {
104 if (mainStack_.IsValid() && arkCode_.IsValid()) {
105 return;
106 }
107 if (!mainStack_.IsValid() && (strstr(mapInfo, "[stack]") != nullptr)) {
108 parser(mapInfo, false, mainStack_);
109 continue;
110 }
111
112 if (!arkCode_.IsValid() && strstr(mapInfo, "stub.an") != nullptr) {
113 parser(mapInfo, true, arkCode_);
114 }
115 }
116 }
117 } // namespace HiviewDFX
118 } // namespace OHOS
119