• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef STACK_UTIL_H
16 #define STACK_UTIL_H
17 
18 #include <cstdio>
19 #include <csignal>
20 #include <cstring>
21 #include <string>
22 #include <pthread.h>
23 #include "dfx_define.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 static uintptr_t g_stackMapStart = 0;
29 static uintptr_t g_stackMapEnd = 0;
30 static uintptr_t g_arkMapStart = 0;
31 static uintptr_t g_arkMapEnd = 0;
32 
ParseSelfMaps()33 static bool ParseSelfMaps()
34 {
35     FILE *fp = fopen(PROC_SELF_MAPS_PATH, "r");
36     if (fp == NULL) {
37         return false;
38     }
39     bool ret = false;
40     char mapInfo[256] = {0}; // 256: map info size
41     int pos = 0;
42     uint64_t begin = 0;
43     uint64_t end = 0;
44     uint64_t offset = 0;
45     char perms[5] = {0}; // 5:rwxp
46     while (fgets(mapInfo, sizeof(mapInfo), fp) != NULL) {
47         if (strstr(mapInfo, "[stack]") != NULL) {
48             if (sscanf_s(mapInfo, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %*x:%*x %*d%n", &begin, &end,
49                 &perms, sizeof(perms), &offset, &pos) != 4) { // 4:scan size
50                 continue;
51             }
52             g_stackMapStart = static_cast<uintptr_t>(begin);
53             g_stackMapEnd = static_cast<uintptr_t>(end);
54             ret = true;
55         }
56 
57         if (strstr(mapInfo, "stub.an") != NULL) {
58             if (sscanf_s(mapInfo, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %*x:%*x %*d%n", &begin, &end,
59                 &perms, sizeof(perms), &offset, &pos) != 4) { // 4:scan size
60                 continue;
61             }
62             if (strcmp(perms, "r-xp") != 0) {
63                 continue;
64             }
65 
66             g_arkMapStart = static_cast<uintptr_t>(begin);
67             g_arkMapEnd = static_cast<uintptr_t>(end);
68             ret = true;
69         }
70     }
71     (void)fclose(fp);
72     return ret;
73 };
74 }
75 
GetArkStackRange(uintptr_t & start,uintptr_t & end)76 AT_UNUSED inline bool GetArkStackRange(uintptr_t& start, uintptr_t& end)
77 {
78     if (g_arkMapStart == 0 || g_arkMapEnd == 0) {
79         ParseSelfMaps();
80     }
81     start = g_arkMapStart;
82     end = g_arkMapEnd;
83     return (g_arkMapStart != 0 && g_arkMapEnd != 0);
84 }
85 
GetMainStackRange(uintptr_t & stackBottom,uintptr_t & stackTop)86 AT_UNUSED inline bool GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop)
87 {
88     if (g_stackMapStart == 0 || g_stackMapEnd == 0) {
89         ParseSelfMaps();
90     }
91     stackBottom = g_stackMapStart;
92     stackTop = g_stackMapEnd;
93     return (g_stackMapStart != 0 && g_stackMapEnd != 0);
94 }
95 
GetSelfStackRange(uintptr_t & stackBottom,uintptr_t & stackTop)96 AT_UNUSED static bool GetSelfStackRange(uintptr_t& stackBottom, uintptr_t& stackTop)
97 {
98     bool ret = false;
99     pthread_attr_t tattr;
100     void *base = nullptr;
101     size_t size = 0;
102     if (pthread_getattr_np(pthread_self(), &tattr) != 0) {
103         return ret;
104     }
105     if (pthread_attr_getstack(&tattr, &base, &size) == 0) {
106         stackBottom = reinterpret_cast<uintptr_t>(base);
107         stackTop = reinterpret_cast<uintptr_t>(base) + size;
108         ret = true;
109     }
110     pthread_attr_destroy(&tattr);
111     return ret;
112 }
113 
GetSigAltStackRange(uintptr_t & stackBottom,uintptr_t & stackTop)114 AT_UNUSED static bool GetSigAltStackRange(uintptr_t& stackBottom, uintptr_t& stackTop)
115 {
116     bool ret = false;
117     stack_t altStack;
118     if (sigaltstack(nullptr, &altStack) != -1) {
119         if ((static_cast<uint32_t>(altStack.ss_flags) & SS_ONSTACK) != 0) {
120             stackBottom = reinterpret_cast<uintptr_t>(altStack.ss_sp);
121             stackTop = reinterpret_cast<uintptr_t>(altStack.ss_sp) + altStack.ss_size;
122             ret = true;
123         }
124     }
125     return ret;
126 }
127 } // namespace HiviewDFX
128 } // namespace OHOS
129 #endif