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