• 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 #ifndef DFX_EXTRACTOR_UTILS_H
16 #define DFX_EXTRACTOR_UTILS_H
17 
18 #include <memory>
19 #include <vector>
20 #include "dfx_define.h"
21 #ifdef ENABLE_HAP_EXTRACTOR
22 #include "dfx_log.h"
23 #include "extractor.h"
24 #include "string_util.h"
25 #include "zip_file.h"
26 #endif
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 #ifdef ENABLE_HAP_EXTRACTOR
32 #undef LOG_DOMAIN
33 #undef LOG_TAG
34 #define LOG_DOMAIN 0xD002D11
35 #define LOG_TAG "DfxExtractor"
36 #endif
37 }
38 
39 struct HapExtractorInfo {
40     uintptr_t loadOffset;
41     uint8_t* abcData;
42     size_t abcDataSize;
43 };
44 
45 class DfxExtractor final {
46 public:
DfxExtractor(const std::string & file)47     explicit DfxExtractor(const std::string& file) { Init(file); }
~DfxExtractor()48     ~DfxExtractor() { Clear(); }
49 
GetHapAbcInfo(uintptr_t & loadOffset,std::unique_ptr<uint8_t[]> & abcDataPtr,size_t & abcDataSize)50     bool GetHapAbcInfo(uintptr_t& loadOffset, std::unique_ptr<uint8_t[]>& abcDataPtr, size_t& abcDataSize)
51     {
52         bool ret = false;
53 #ifdef ENABLE_HAP_EXTRACTOR
54         if (zipFile_ == nullptr) {
55             return false;
56         }
57 
58         auto &entrys = zipFile_->GetAllEntries();
59         for (const auto &entry : entrys) {
60             std::string fileName = entry.first;
61             if (!EndsWith(fileName, "modules.abc")) {
62                 continue;
63             }
64 
65             AbilityBase::ZipPos offset = 0;
66             uint32_t length = 0;
67             if (!zipFile_->GetDataOffsetRelative(entry.second, offset, length)) {
68                 break;
69             }
70 
71             DFXLOGU("[%{public}d]: Hap entry file: %{public}s, offset: 0x%{public}016" PRIx64 "", __LINE__,
72                 fileName.c_str(), (uint64_t)offset);
73             loadOffset = static_cast<uintptr_t>(offset);
74             if (zipFile_->ExtractToBufByName(fileName, abcDataPtr, abcDataSize)) {
75                 DFXLOGU("Hap abc: %{public}s, size: %{public}zu", fileName.c_str(), abcDataSize);
76                 ret = true;
77                 break;
78             }
79         }
80 #endif
81         return ret;
82     }
83 
GetHapSourceMapInfo(uintptr_t & loadOffset,std::unique_ptr<uint8_t[]> & sourceMapPtr,size_t & sourceMapSize)84     bool GetHapSourceMapInfo(uintptr_t& loadOffset, std::unique_ptr<uint8_t[]>& sourceMapPtr, size_t& sourceMapSize)
85     {
86         bool ret = false;
87 #ifdef ENABLE_HAP_EXTRACTOR
88         if (zipFile_ == nullptr) {
89             return false;
90         }
91 
92         auto &entrys = zipFile_->GetAllEntries();
93         for (const auto &entry : entrys) {
94             std::string fileName = entry.first;
95             if (!EndsWith(fileName, ".map")) {
96                 continue;
97             }
98 
99             AbilityBase::ZipPos offset = 0;
100             uint32_t length = 0;
101             if (!zipFile_->GetDataOffsetRelative(entry.second, offset, length)) {
102                 break;
103             }
104 
105             DFXLOGU("[%{public}d]: Hap entry file: %{public}s, offset: 0x%{public}016" PRIx64 "", __LINE__,
106                 fileName.c_str(), (uint64_t)offset);
107             loadOffset = static_cast<uintptr_t>(offset);
108             if (zipFile_->ExtractToBufByName(fileName, sourceMapPtr, sourceMapSize)) {
109                 DFXLOGU("Hap sourcemap: %{public}s, size: %{public}zu", fileName.c_str(), sourceMapSize);
110                 ret = true;
111                 break;
112             }
113         }
114 #endif
115         return ret;
116     }
117 
118 protected:
Init(const std::string & file)119     bool Init(const std::string& file)
120     {
121 #ifdef ENABLE_HAP_EXTRACTOR
122         if (zipFile_ == nullptr) {
123             zipFile_ = std::make_shared<AbilityBase::ZipFile>(file);
124         }
125         if ((zipFile_ == nullptr) || (!zipFile_->Open())) {
126             DFXLOGE("Failed to open zip file(%{public}s)", file.c_str());
127             zipFile_ = nullptr;
128             return false;
129         }
130         DFXLOGU("Done load zip file %{public}s", file.c_str());
131         return true;
132 #endif
133         return false;
134     }
135 
Clear()136     void Clear()
137     {
138 #ifdef ENABLE_HAP_EXTRACTOR
139         if (zipFile_ == nullptr) {
140             return;
141         }
142         zipFile_ = nullptr;
143 #endif
144     }
145 
146 private:
147 #ifdef ENABLE_HAP_EXTRACTOR
148     std::shared_ptr<AbilityBase::ZipFile> zipFile_ = nullptr;
149 #endif
150 };
151 }   // namespace HiviewDFX
152 }   // namespace OHOS
153 #endif