1 /*
2 * Copyright (c) 2021-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 #include <fstream>
16 #include <sstream>
17 #include <iomanip>
18 #include <memory>
19 #include <algorithm>
20 #include <unwind.h>
21
22 #include "stacktrace.h"
23 #include "os/mutex.h"
24
25 #include <cxxabi.h>
26 #include <dlfcn.h>
27 #include "debug_info.h"
28
29 namespace ark {
30
31 struct VmaEntry {
32 enum DebugInfoStatus { NOT_READ, VALID, BAD };
33
34 // NOLINTNEXTLINE(modernize-pass-by-value)
VmaEntryark::VmaEntry35 VmaEntry(uintptr_t paramStartAddr, uintptr_t paramEndAddr, uintptr_t paramOffset, const std::string &fname)
36 : startAddr(paramStartAddr), endAddr(paramEndAddr), offset(paramOffset), filename(fname)
37 {
38 }
39
40 ~VmaEntry() = default;
41
42 uintptr_t startAddr; // NOLINT(misc-non-private-member-variables-in-classes)
43 uintptr_t endAddr; // NOLINT(misc-non-private-member-variables-in-classes)
44 uintptr_t offset; // NOLINT(misc-non-private-member-variables-in-classes)
45 std::string filename; // NOLINT(misc-non-private-member-variables-in-classes)
46 DebugInfoStatus status {NOT_READ}; // NOLINT(misc-non-private-member-variables-in-classes)
47 DebugInfo debugInfo; // NOLINT(misc-non-private-member-variables-in-classes)
48
49 DEFAULT_MOVE_SEMANTIC(VmaEntry);
50 NO_COPY_SEMANTIC(VmaEntry);
51 };
52
53 class Tokenizer {
54 public:
55 // NOLINTNEXTLINE(modernize-pass-by-value)
Tokenizer(const std::string & str)56 explicit Tokenizer(const std::string &str) : str_(str) {}
57
Next(char delim=' ')58 std::string Next(char delim = ' ')
59 {
60 while (pos_ < str_.length() && str_[pos_] == ' ') {
61 ++pos_;
62 }
63 size_t pos = str_.find(delim, pos_);
64 std::string token;
65 if (pos == std::string::npos) {
66 token = str_.substr(pos_);
67 pos_ = str_.length();
68 } else {
69 token = str_.substr(pos_, pos - pos_);
70 pos_ = pos + 1; // skip delimiter
71 }
72 return token;
73 }
74
75 private:
76 std::string str_;
77 size_t pos_ {0};
78 };
79
80 class StackPrinter {
81 public:
GetInstance()82 static StackPrinter &GetInstance()
83 {
84 static StackPrinter printer;
85 return printer;
86 }
87
Print(const std::vector<uintptr_t> & stacktrace,std::ostream & out)88 std::ostream &Print(const std::vector<uintptr_t> &stacktrace, std::ostream &out)
89 {
90 os::memory::LockHolder lock(mutex_);
91 ScanVma();
92 for (size_t frameNum = 0; frameNum < stacktrace.size(); ++frameNum) {
93 PrintFrame(frameNum, stacktrace[frameNum], out);
94 }
95 return out;
96 }
97
98 NO_MOVE_SEMANTIC(StackPrinter);
99 NO_COPY_SEMANTIC(StackPrinter);
100
101 private:
102 explicit StackPrinter() = default;
103 ~StackPrinter() = default;
104
PrintFrame(size_t frameNum,uintptr_t pc,std::ostream & out)105 void PrintFrame(size_t frameNum, uintptr_t pc, std::ostream &out)
106 {
107 std::ios_base::fmtflags f = out.flags();
108 auto w = out.width();
109 out << "#" << std::setw(2U) << std::left << frameNum << ": 0x" << std::hex << pc << " ";
110 out.flags(f);
111 out.width(w);
112
113 VmaEntry *vma = FindVma(pc);
114 if (vma == nullptr) {
115 vmas_.clear();
116 ScanVma();
117 vma = FindVma(pc);
118 }
119 if (vma != nullptr) {
120 uintptr_t pcOffset = pc - vma->startAddr + vma->offset;
121 // pc points to the instruction after the call
122 // Decrement pc to get source line number pointing to the function call
123 --pcOffset;
124 std::string function;
125 std::string srcFile;
126 unsigned int line = 0;
127 if (ReadDebugInfo(vma) && vma->debugInfo.GetSrcLocation(pcOffset, &function, &srcFile, &line)) {
128 PrintFrame(function, srcFile, line, out);
129 return;
130 }
131 uintptr_t offset = 0;
132 if (ReadSymbol(pc, &function, &offset)) {
133 PrintFrame(function, offset, out);
134 return;
135 }
136 }
137 out << "??:??\n";
138 }
139
PrintFrame(const std::string & function,const std::string & srcFile,unsigned int line,std::ostream & out)140 void PrintFrame(const std::string &function, const std::string &srcFile, unsigned int line, std::ostream &out)
141 {
142 if (function.empty()) {
143 out << "??";
144 } else {
145 Demangle(function, out);
146 }
147 out << "\n at ";
148 if (srcFile.empty()) {
149 out << "??";
150 } else {
151 out << srcFile;
152 }
153 out << ":";
154 if (line == 0) {
155 out << "??";
156 } else {
157 out << line;
158 }
159
160 out << "\n";
161 }
162
PrintFrame(const std::string & function,uintptr_t offset,std::ostream & out)163 void PrintFrame(const std::string &function, uintptr_t offset, std::ostream &out)
164 {
165 std::ios_base::fmtflags f = out.flags();
166 Demangle(function, out);
167 out << std::hex << "+0x" << offset << "\n";
168 out.flags(f);
169 }
170
ReadSymbol(uintptr_t pc,std::string * function,uintptr_t * offset)171 bool ReadSymbol(uintptr_t pc, std::string *function, uintptr_t *offset)
172 {
173 Dl_info info {};
174 if (dladdr(reinterpret_cast<void *>(pc), &info) != 0 && info.dli_sname != nullptr) {
175 *function = info.dli_sname;
176 *offset = pc - reinterpret_cast<uintptr_t>(info.dli_saddr);
177 return true;
178 }
179 return false;
180 }
181
Demangle(const std::string & function,std::ostream & out)182 void Demangle(const std::string &function, std::ostream &out)
183 {
184 size_t length = 0;
185 int status = 0;
186 char *demangledFunction = abi::__cxa_demangle(function.c_str(), nullptr, &length, &status);
187 if (status == 0) {
188 out << demangledFunction;
189 free(demangledFunction); // NOLINT(cppcoreguidelines-no-malloc)
190 } else {
191 out << function;
192 }
193 }
194
FindVma(uintptr_t pc)195 VmaEntry *FindVma(uintptr_t pc)
196 {
197 VmaEntry el(pc, pc, 0, "");
198 auto it = std::upper_bound(vmas_.begin(), vmas_.end(), el,
199 [](const VmaEntry &e1, const VmaEntry &e2) { return e1.endAddr < e2.endAddr; });
200 if (it != vmas_.end() && (it->startAddr <= pc && pc < it->endAddr)) {
201 return &(*it);
202 }
203 return nullptr;
204 }
205
ReadDebugInfo(VmaEntry * vma)206 bool ReadDebugInfo(VmaEntry *vma)
207 {
208 if (vma->status == VmaEntry::VALID) {
209 return true;
210 }
211 if (vma->status == VmaEntry::BAD) {
212 return false;
213 }
214 if (!vma->filename.empty() && vma->debugInfo.ReadFromFile(vma->filename.c_str()) == DebugInfo::SUCCESS) {
215 vma->status = VmaEntry::VALID;
216 return true;
217 }
218 vma->status = VmaEntry::BAD;
219 return false;
220 }
221
ScanVma()222 void ScanVma()
223 {
224 static const int HEX_RADIX = 16;
225 static const size_t MODE_FIELD_LEN = 4;
226 static const size_t XMODE_POS = 2;
227
228 if (!vmas_.empty()) {
229 return;
230 }
231
232 std::stringstream fname;
233 fname << "/proc/self/maps";
234 std::string filename = fname.str();
235 std::ifstream maps(filename.c_str());
236
237 while (maps) {
238 std::string line;
239 std::getline(maps, line);
240 Tokenizer tokenizer(line);
241 std::string startAddr = tokenizer.Next('-');
242 std::string endAddr = tokenizer.Next();
243 std::string rights = tokenizer.Next();
244 if (rights.length() == MODE_FIELD_LEN && rights[XMODE_POS] == 'x') {
245 std::string offset = tokenizer.Next();
246 tokenizer.Next();
247 tokenizer.Next();
248 std::string objFilename = tokenizer.Next();
249 vmas_.emplace_back(stoul(startAddr, nullptr, HEX_RADIX), stoul(endAddr, nullptr, HEX_RADIX),
250 stoul(offset, nullptr, HEX_RADIX), objFilename);
251 }
252 }
253 }
254
255 private:
256 std::vector<VmaEntry> vmas_;
257 os::memory::Mutex mutex_;
258 };
259
260 class Buf {
261 public:
Buf(uintptr_t * buf,size_t skip,size_t capacity)262 Buf(uintptr_t *buf, size_t skip, size_t capacity) : buf_(buf), skip_(skip), capacity_(capacity) {}
263
Append(uintptr_t pc)264 void Append(uintptr_t pc)
265 {
266 if (skip_ > 0) {
267 // Skip the element
268 --skip_;
269 return;
270 }
271 if (size_ >= capacity_) {
272 return;
273 }
274 buf_[size_++] = pc; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
275 }
276
Size() const277 int Size() const
278 {
279 return static_cast<int>(size_);
280 }
281
282 private:
283 uintptr_t *buf_;
284 size_t skip_;
285 size_t size_ {0};
286 size_t capacity_;
287 };
288
FrameHandler(struct _Unwind_Context * ctx,void * arg)289 static _Unwind_Reason_Code FrameHandler(struct _Unwind_Context *ctx, [[maybe_unused]] void *arg)
290 {
291 Buf *buf = reinterpret_cast<Buf *>(arg);
292 uintptr_t pc = _Unwind_GetIP(ctx);
293 // _Unwind_GetIP returns 0 pc at the end of the stack. Ignore it
294 if (pc != 0) {
295 buf->Append(pc);
296 }
297 return _URC_NO_REASON;
298 }
299
GetStacktrace(uintptr_t * buf,size_t size)300 static size_t GetStacktrace(uintptr_t *buf, size_t size)
301 {
302 static constexpr int SKIP_FRAMES = 2; // backtrace
303 Buf bufWrapper(buf, SKIP_FRAMES, size);
304 _Unwind_Reason_Code res = _Unwind_Backtrace(FrameHandler, &bufWrapper);
305 if (res != _URC_END_OF_STACK || bufWrapper.Size() < 0) {
306 return 0;
307 }
308
309 return bufWrapper.Size();
310 }
311
GetStacktrace()312 std::vector<uintptr_t> GetStacktrace()
313 {
314 static constexpr size_t BUF_SIZE = 100;
315 std::vector<uintptr_t> buf;
316 buf.resize(BUF_SIZE);
317 size_t size = GetStacktrace(buf.data(), buf.size());
318 buf.resize(size);
319 return buf;
320 }
321
PrintStack(const std::vector<uintptr_t> & stacktrace,std::ostream & out)322 std::ostream &PrintStack(const std::vector<uintptr_t> &stacktrace, std::ostream &out)
323 {
324 return StackPrinter::GetInstance().Print(stacktrace, out);
325 }
326
327 } // namespace ark
328