• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 /* This files contains process dump header. */
17 #include "dfx_symbols_cache.h"
18 
19 #include <algorithm>
20 #include <cstdlib>
21 #include <cstring>
22 #include <cxxabi.h>
23 #include <string>
24 #include <vector>
25 #include "dfx_define.h"
26 #include "libunwind_i-ohos.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
SymbolComparator(DfxSymbol s1,DfxSymbol s2)31 bool SymbolComparator(DfxSymbol s1, DfxSymbol s2)
32 {
33     return (s1.start < s2.start);
34 };
35 #ifdef __cplusplus
36 };
37 #endif
38 
39 namespace OHOS {
40 namespace HiviewDFX {
GetNameAndOffsetByPc(struct unw_addr_space * as,uint64_t pc,std::string & name,uint64_t & offset)41 bool DfxSymbolsCache::GetNameAndOffsetByPc(struct unw_addr_space *as,
42     uint64_t pc, std::string& name, uint64_t& offset)
43 {
44     if (GetNameAndOffsetByPc(pc, name, offset)) {
45         return true;
46     }
47 
48     char buf[LOG_BUF_LEN] { 0 };
49     DfxSymbol symbol;
50     if (unw_get_symbol_info_by_pc(as, pc, LOG_BUF_LEN, buf, &symbol.start, &symbol.end) != 0) {
51         return false;
52     }
53 
54     if (strlen(buf) >= LOG_BUF_LEN - 1) {
55         return false;
56     }
57 
58     int status = 0;
59     auto funcName = abi::__cxa_demangle(buf, nullptr, nullptr, &status);
60     if (funcName != nullptr) {
61         symbol.funcName = std::string(funcName);
62         std::free(funcName);
63     } else {
64         symbol.funcName = std::string(buf, strlen(buf));
65     }
66     offset = pc - symbol.start;
67     name = symbol.funcName;
68     cachedSymbols_.push_back(symbol);
69     std::sort(cachedSymbols_.begin(), cachedSymbols_.end(), SymbolComparator);
70     return true;
71 }
72 
GetNameAndOffsetByPc(uint64_t pc,std::string & name,uint64_t & offset)73 bool DfxSymbolsCache::GetNameAndOffsetByPc(uint64_t pc, std::string& name, uint64_t& offset)
74 {
75     size_t begin = 0;
76     size_t end = cachedSymbols_.size();
77     while (begin < end) {
78         size_t mid = begin + (end - begin) / 2;
79         DfxSymbol& symbol = cachedSymbols_[mid];
80         if (pc < symbol.start) {
81             end = mid;
82         } else if (pc <= symbol.end) {
83             offset = pc - symbol.start;
84             name = symbol.funcName;
85             return true;
86         } else {
87             begin = mid + 1;
88         }
89     }
90     return false;
91 }
92 } // namespace HiviewDFX
93 } // namespace OHOS
94