• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include "dfx_hap.h"
17 
18 #include "dfx_define.h"
19 #include "dfx_log.h"
20 #include "dfx_maps.h"
21 #include "dfx_memory.h"
22 #include "dfx_util.h"
23 #include "string_util.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 #undef LOG_DOMAIN
29 #undef LOG_TAG
30 #define LOG_DOMAIN 0xD002D11
31 #define LOG_TAG "DfxHap"
32 }
33 
~DfxHap()34 DfxHap::~DfxHap()
35 {
36 #if is_ohos && !is_mingw
37     if (arkSymbolExtractorPtr_ != 0) {
38         DfxArk::Instance().ArkDestoryJsSymbolExtractor(arkSymbolExtractorPtr_);
39         arkSymbolExtractorPtr_ = 0;
40     }
41 #endif
42 }
43 
ParseHapInfo(pid_t pid,uint64_t pc,std::shared_ptr<DfxMap> map,JsFunction * jsFunction)44 bool DfxHap::ParseHapInfo(pid_t pid, uint64_t pc, std::shared_ptr<DfxMap> map, JsFunction *jsFunction)
45 {
46 #if is_ohos && !is_mingw
47     if (jsFunction == nullptr || map == nullptr) {
48         return false;
49     }
50     if (!DfxMaps::IsArkHapMapItem(map->name) && !DfxMaps::IsArkCodeMapItem(map->name)) {
51         return false;
52     }
53     if (arkSymbolExtractorPtr_ == 0) {
54         if (DfxArk::Instance().ArkCreateJsSymbolExtractor(&arkSymbolExtractorPtr_) < 0) {
55             DFXLOGU("Failed to create ark js symbol extractor");
56         }
57     }
58 
59     if (DfxMaps::IsArkHapMapItem(map->name)) {
60         if (!ParseHapFileInfo(pc, map, jsFunction)) {
61             DFXLOGW("Failed to parse hap file info");
62             return false;
63         }
64     } else if (DfxMaps::IsArkCodeMapItem(map->name)) {
65         if (!ParseHapMemInfo(pid, pc, map, jsFunction)) {
66             DFXLOGW("Failed to parse hap mem info");
67             return false;
68         }
69     }
70     return true;
71 #else
72     return false;
73 #endif
74 }
75 
ParseHapFileInfo(uint64_t pc,std::shared_ptr<DfxMap> map,JsFunction * jsFunction)76 bool DfxHap::ParseHapFileInfo(uint64_t pc, std::shared_ptr<DfxMap> map, JsFunction *jsFunction)
77 {
78 #if is_ohos && !is_mingw
79     if (jsFunction == nullptr || map == nullptr || map->name.empty()) {
80         return false;
81     }
82 
83     if (DfxArk::Instance().ParseArkFileInfo(static_cast<uintptr_t>(pc), static_cast<uintptr_t>(map->begin),
84         map->name.c_str(), arkSymbolExtractorPtr_, jsFunction) < 0) {
85         DFXLOGW("Failed to parse ark file info, pc: %{private}p, begin: %{private}p",
86             reinterpret_cast<void *>(pc), reinterpret_cast<void *>(map->begin));
87         return false;
88     }
89     return true;
90 #endif
91     return false;
92 }
93 
ParseHapMemInfo(pid_t pid,uint64_t pc,std::shared_ptr<DfxMap> map,JsFunction * jsFunction)94 bool DfxHap::ParseHapMemInfo(pid_t pid, uint64_t pc, std::shared_ptr<DfxMap> map,
95     JsFunction *jsFunction)
96 {
97 #if is_ohos && !is_mingw
98     if (pid < 0 || jsFunction == nullptr || map == nullptr) {
99         return false;
100     }
101 
102     if (!ParseHapMemData(pid, map)) {
103         DFXLOGW("Failed to parse hap mem data, pid: %{public}d", pid);
104         return false;
105     }
106     if (DfxArk::Instance().ParseArkFrameInfo(static_cast<uintptr_t>(pc), static_cast<uintptr_t>(map->begin),
107         abcLoadOffset_, abcDataPtr_.get(), abcDataSize_, arkSymbolExtractorPtr_, jsFunction) < 0) {
108         DFXLOGW("Failed to parse ark frame info, pc: %{private}p, begin: %{private}p",
109             reinterpret_cast<void *>(pc), reinterpret_cast<void *>(map->begin));
110         return false;
111     }
112     return true;
113 #endif
114     return false;
115 }
116 
ParseHapFileData(const std::string & name)117 bool DfxHap::ParseHapFileData(const std::string& name)
118 {
119 #ifndef is_ohos_lite
120     if (name.empty()) {
121         return false;
122     }
123     if (abcDataPtr_ != nullptr) {
124         return true;
125     }
126     DFXLOGU("name: %{public}s", name.c_str());
127     if (extractor_ == nullptr) {
128         extractor_ = std::make_unique<DfxExtractor>(name);
129     }
130     if (!extractor_->GetHapAbcInfo(abcLoadOffset_, abcDataPtr_, abcDataSize_)) {
131         DFXLOGW("Failed to get hap abc info: %{public}s", name.c_str());
132         abcDataPtr_ = nullptr;
133         return false;
134     }
135 
136     if (!extractor_->GetHapSourceMapInfo(srcMapLoadOffset_, srcMapDataPtr_, srcMapDataSize_)) {
137         DFXLOGU("Failed to get hap source map info: %{public}s", name.c_str());
138     }
139     return true;
140 #endif
141     return false;
142 }
143 
ParseHapMemData(const pid_t pid,std::shared_ptr<DfxMap> map)144 bool DfxHap::ParseHapMemData(const pid_t pid, std::shared_ptr<DfxMap> map)
145 {
146 #if is_ohos && !is_mingw
147 #ifndef is_ohos_lite
148     if (pid < 0 || map == nullptr) {
149         return false;
150     }
151     if (abcDataPtr_ != nullptr) {
152         return true;
153     }
154     DFXLOGU("[%{public}d]: pid: %{public}d", __LINE__, pid);
155     abcLoadOffset_ = map->offset;
156     abcDataSize_ = map->end - map->begin;
157     abcDataPtr_ = std::make_unique<uint8_t[]>(abcDataSize_);
158     auto size = ReadProcMemByPid(pid, map->begin, abcDataPtr_.get(), abcDataSize_);
159     if (size != abcDataSize_) {
160         DFXLOGW("ReadProcMemByPid(%{public}d) return size(%{public}zu), real size(%{public}zu)",
161             pid, size, abcDataSize_);
162         abcDataPtr_ = nullptr;
163         return false;
164     }
165     return true;
166 #endif
167 #endif
168     return false;
169 }
170 } // namespace HiviewDFX
171 } // namespace OHOS
172