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