• 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 #include "hidump_helper.h"
17 
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const int32_t MIN_ARGS_SIZE = 1;
25 const int32_t MAX_ARGS_SIZE = 2;
26 const int32_t FIRST_PARAM = 0;
27 const int32_t SECOND_PARAM = 1;
28 const int32_t NON_ANONYMIZE_LEN = 4;
29 const std::string ARGS_HELP = "-h";
30 const std::string ARGS_ABILITY = "-ability";
31 const std::string ARGS_ABILITY_LIST = "-ability-list";
32 const std::string ARGS_BUNDLE = "-bundle";
33 const std::string ARGS_BUNDLE_LIST = "-bundle-list";
34 const std::string ARGS_DEVICEID = "-device";
35 const std::string ILLEGAL_INFOMATION = "The arguments are illegal and you can enter '-h' for help.\n";
36 const std::string NO_INFOMATION = "no such infomation\n";
37 
38 const std::unordered_map<std::string, HidumpFlag> ARGS_MAP = {
39     { ARGS_HELP, HidumpFlag::GET_HELP },
40     { ARGS_ABILITY, HidumpFlag::GET_ABILITY },
41     { ARGS_ABILITY_LIST, HidumpFlag::GET_ABILITY_LIST },
42     { ARGS_BUNDLE, HidumpFlag::GET_BUNDLE },
43     { ARGS_BUNDLE_LIST, HidumpFlag::GET_BUNDLE_LIST },
44     { ARGS_DEVICEID, HidumpFlag::GET_DEVICEID },
45 };
46 
AnonymizeDeviceId(const std::string & deviceId)47 std::string AnonymizeDeviceId(const std::string &deviceId)
48 {
49     int32_t totalLen = static_cast<int32_t>(strlen(deviceId.c_str()));
50     int32_t anonymizeCharNum = totalLen - NON_ANONYMIZE_LEN - NON_ANONYMIZE_LEN;
51     if (anonymizeCharNum <= 0) {
52         return deviceId;
53     }
54 
55     std::string newDeviceId;
56     newDeviceId
57         .append(deviceId.substr(0, NON_ANONYMIZE_LEN))
58         .append(anonymizeCharNum, '*')
59         .append(deviceId.substr(totalLen - NON_ANONYMIZE_LEN, NON_ANONYMIZE_LEN));
60     return newDeviceId;
61 }
62 }
63 
HidumpHelper(const std::weak_ptr<BundleDataMgr> & dataMgr)64 HidumpHelper::HidumpHelper(const std::weak_ptr<BundleDataMgr> &dataMgr)
65     : dataMgr_(dataMgr) {}
66 
Dump(const std::vector<std::string> & args,std::string & result)67 bool HidumpHelper::Dump(const std::vector<std::string>& args, std::string &result)
68 {
69     result.clear();
70     ErrCode errCode = ERR_OK;
71     int32_t argsSize = static_cast<int32_t>(args.size());
72     switch (argsSize) {
73         case MIN_ARGS_SIZE: {
74             errCode = ProcessOneParam(args[FIRST_PARAM], result);
75             break;
76         }
77         case MAX_ARGS_SIZE: {
78             errCode = ProcessTwoParam(args[FIRST_PARAM], args[SECOND_PARAM], result);
79             break;
80         }
81         default: {
82             errCode = ERR_APPEXECFWK_HIDUMP_INVALID_ARGS;
83             break;
84         }
85     }
86 
87     bool ret = false;
88     switch (errCode) {
89         case ERR_OK: {
90             ret = true;
91             break;
92         }
93         case ERR_APPEXECFWK_HIDUMP_INVALID_ARGS: {
94             ShowIllealInfomation(result);
95             ret = true;
96             break;
97         }
98         case ERR_APPEXECFWK_HIDUMP_UNKONW: {
99             result.append(NO_INFOMATION);
100             ret = true;
101             break;
102         }
103         case ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR: {
104             ret = false;
105             break;
106         }
107         default: {
108             break;
109         }
110     }
111 
112     return ret;
113 }
114 
ProcessOneParam(const std::string & args,std::string & result)115 ErrCode HidumpHelper::ProcessOneParam(const std::string& args, std::string &result)
116 {
117     HidumpParam hidumpParam;
118     auto operatorIter = ARGS_MAP.find(args);
119     if (operatorIter != ARGS_MAP.end()) {
120         hidumpParam.hidumpFlag = operatorIter->second;
121     }
122 
123     if (hidumpParam.hidumpFlag == HidumpFlag::GET_HELP) {
124         ShowHelp(result);
125         return ERR_OK;
126     }
127 
128     return ProcessDump(hidumpParam, result);
129 }
130 
ProcessTwoParam(const std::string & firstParam,const std::string & secondParam,std::string & result)131 ErrCode HidumpHelper::ProcessTwoParam(
132     const std::string& firstParam, const std::string& secondParam, std::string &result)
133 {
134     HidumpParam hidumpParam;
135     hidumpParam.args = secondParam;
136     auto operatorIter = ARGS_MAP.find(firstParam);
137     if (operatorIter != ARGS_MAP.end()) {
138         hidumpParam.hidumpFlag = operatorIter->second;
139     }
140 
141     switch (hidumpParam.hidumpFlag) {
142         case HidumpFlag::GET_ABILITY: {
143             hidumpParam.hidumpFlag = HidumpFlag::GET_ABILITY_BY_NAME;
144             break;
145         }
146         case HidumpFlag::GET_BUNDLE: {
147             hidumpParam.hidumpFlag = HidumpFlag::GET_BUNDLE_BY_NAME;
148             break;
149         }
150         default: {
151             break;
152         }
153     }
154 
155     return ProcessDump(hidumpParam, result);
156 }
157 
ProcessDump(const HidumpParam & hidumpParam,std::string & result)158 ErrCode HidumpHelper::ProcessDump(const HidumpParam& hidumpParam, std::string &result)
159 {
160     result.clear();
161     ErrCode errCode = ERR_APPEXECFWK_HIDUMP_ERROR;
162     switch (hidumpParam.hidumpFlag) {
163         case HidumpFlag::GET_ABILITY: {
164             errCode = GetAllAbilityInfo(result);
165             break;
166         }
167         case HidumpFlag::GET_ABILITY_LIST: {
168             errCode = GetAllAbilityNameList(result);
169             break;
170         }
171         case HidumpFlag::GET_ABILITY_BY_NAME: {
172             errCode = GetAbilityInfoByName(hidumpParam.args, result);
173             break;
174         }
175         case HidumpFlag::GET_BUNDLE: {
176             errCode = GetAllBundleInfo(result);
177             break;
178         }
179         case HidumpFlag::GET_BUNDLE_LIST: {
180             errCode = GetAllBundleNameList(result);
181             break;
182         }
183         case HidumpFlag::GET_BUNDLE_BY_NAME: {
184             errCode = GetBundleInfoByName(hidumpParam.args, result);
185             break;
186         }
187         case HidumpFlag::GET_DEVICEID: {
188             errCode = GetAllDeviced(result);
189             break;
190         }
191         default: {
192             errCode = ERR_APPEXECFWK_HIDUMP_INVALID_ARGS;
193             break;
194         }
195     }
196 
197     return errCode;
198 }
199 
GetAllAbilityInfo(std::string & result)200 ErrCode HidumpHelper::GetAllAbilityInfo(std::string &result)
201 {
202     auto shareDataMgr = dataMgr_.lock();
203     if (shareDataMgr == nullptr) {
204         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
205     }
206 
207     std::vector<BundleInfo> bundleInfos;
208     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
209         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
210         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
211         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
212         bundleInfos, Constants::ANY_USERID)) {
213         APP_LOGE("get bundleInfos failed.");
214         return ERR_APPEXECFWK_HIDUMP_ERROR;
215     }
216 
217     for (auto &bundleInfo : bundleInfos) {
218         for (auto &abilityInfo :  bundleInfo.abilityInfos) {
219             result.append(abilityInfo.name);
220             result.append(":\n");
221             nlohmann::json jsonObject = abilityInfo;
222             result.append(jsonObject.dump(Constants::DUMP_INDENT));
223             result.append("\n");
224         }
225     }
226 
227     APP_LOGD("get all ability info success");
228     return ERR_OK;
229 }
230 
GetAllAbilityNameList(std::string & result)231 ErrCode HidumpHelper::GetAllAbilityNameList(std::string &result)
232 {
233     auto shareDataMgr = dataMgr_.lock();
234     if (shareDataMgr == nullptr) {
235         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
236     }
237 
238     std::vector<BundleInfo> bundleInfos;
239     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
240         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
241         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
242         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
243         bundleInfos, Constants::ANY_USERID)) {
244         APP_LOGE("get bundleInfos failed.");
245         return ERR_APPEXECFWK_HIDUMP_ERROR;
246     }
247 
248     for (const auto &bundleInfo : bundleInfos) {
249         for (auto abilityInfo :  bundleInfo.abilityInfos) {
250             result.append(abilityInfo.name);
251             result.append("\n");
252         }
253     }
254 
255     APP_LOGD("get all ability list info success");
256     return ERR_OK;
257 }
258 
GetAbilityInfoByName(const std::string & name,std::string & result)259 ErrCode HidumpHelper::GetAbilityInfoByName(const std::string &name, std::string &result)
260 {
261     auto shareDataMgr = dataMgr_.lock();
262     if (shareDataMgr == nullptr) {
263         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
264     }
265 
266     std::vector<BundleInfo> bundleInfos;
267     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
268         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
269         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
270         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
271         bundleInfos, Constants::ANY_USERID)) {
272         APP_LOGE("get bundleInfos failed.");
273         return ERR_APPEXECFWK_HIDUMP_ERROR;
274     }
275 
276     nlohmann::json jsonObject;
277     for (const auto &bundleInfo : bundleInfos) {
278         for (auto abilityInfo :  bundleInfo.abilityInfos) {
279             if (abilityInfo.name == name) {
280                 jsonObject[abilityInfo.bundleName][abilityInfo.moduleName] = abilityInfo;
281             }
282         }
283     }
284 
285     if (jsonObject.is_discarded() || jsonObject.empty()) {
286         APP_LOGE("get ability by abilityName failed.");
287         return ERR_APPEXECFWK_HIDUMP_ERROR;
288     }
289 
290     result.append(jsonObject.dump(Constants::DUMP_INDENT));
291     result.append("\n");
292     return ERR_OK;
293 }
294 
GetAllBundleInfo(std::string & result)295 ErrCode HidumpHelper::GetAllBundleInfo(std::string &result)
296 {
297     auto shareDataMgr = dataMgr_.lock();
298     if (shareDataMgr == nullptr) {
299         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
300     }
301 
302     std::vector<BundleInfo> bundleInfos;
303     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
304         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
305         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
306         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
307         bundleInfos, Constants::ANY_USERID)) {
308         APP_LOGE("get bundleInfos failed.");
309         return false;
310     }
311 
312     for (auto &info : bundleInfos) {
313         result.append(info.name);
314         result.append(":\n");
315         nlohmann::json jsonObject = info;
316         jsonObject["hapModuleInfos"] = info.hapModuleInfos;
317         result.append(jsonObject.dump(Constants::DUMP_INDENT));
318         result.append("\n");
319     }
320     APP_LOGD("get all bundle info success");
321     return ERR_OK;
322 }
323 
GetAllBundleNameList(std::string & result)324 ErrCode HidumpHelper::GetAllBundleNameList(std::string &result)
325 {
326     auto shareDataMgr = dataMgr_.lock();
327     if (shareDataMgr == nullptr) {
328         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
329     }
330 
331     std::vector<std::string> bundleNames;
332     if (!shareDataMgr->GetBundleList(bundleNames, Constants::ANY_USERID)) {
333         APP_LOGE("get bundle list failed");
334         return ERR_APPEXECFWK_HIDUMP_ERROR;
335     }
336 
337     for (auto &name : bundleNames) {
338         result.append(name);
339         result.append("\n");
340     }
341 
342     return ERR_OK;
343 }
344 
GetBundleInfoByName(const std::string & name,std::string & result)345 ErrCode HidumpHelper::GetBundleInfoByName(const std::string &name, std::string &result)
346 {
347     APP_LOGD("hidump bundle info begin");
348     auto shareDataMgr = dataMgr_.lock();
349     if (shareDataMgr == nullptr) {
350         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
351     }
352 
353     BundleInfo bundleInfo;
354     if (!shareDataMgr->GetBundleInfo(name,
355         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
356         BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION |
357         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
358         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE, bundleInfo, Constants::ANY_USERID)) {
359         APP_LOGE("get bundleInfo(%{public}s) failed", name.c_str());
360         return ERR_APPEXECFWK_HIDUMP_ERROR;
361     }
362 
363     result.append(name);
364     result.append(":\n");
365     nlohmann::json jsonObject = bundleInfo;
366     jsonObject["hapModuleInfos"] = bundleInfo.hapModuleInfos;
367     result.append(jsonObject.dump(Constants::DUMP_INDENT));
368     result.append("\n");
369     APP_LOGD("get %{public}s bundle info success", name.c_str());
370     return ERR_OK;
371 }
372 
GetAllDeviced(std::string & result)373 ErrCode HidumpHelper::GetAllDeviced(std::string &result)
374 {
375     auto shareDataMgr = dataMgr_.lock();
376     if (shareDataMgr == nullptr) {
377         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
378     }
379 
380     std::vector<std::string> deviceIds;
381     if (!shareDataMgr->QueryAllDeviceIds(deviceIds)) {
382         APP_LOGE("get all deviceId failed");
383         return ERR_APPEXECFWK_HIDUMP_ERROR;
384     }
385 
386     for (auto deviceId : deviceIds) {
387         result.append(AnonymizeDeviceId(deviceId));
388         result.append("\n");
389     }
390 
391     return ERR_OK;
392 }
393 
ShowHelp(std::string & result)394 void HidumpHelper::ShowHelp(std::string &result)
395 {
396     result.append("Usage:dump  <command> [options]\n")
397         .append("Description:\n")
398         .append("-ability          ")
399         .append("dump all ability infomation in the system\n")
400         .append("-ability    [abilityName]\n")
401         .append("                  dump ability list information of the specified name in the system\n")
402         .append("-ability-list     ")
403         .append("dump list of all ability names in the system\n")
404         .append("-bundle           ")
405         .append("dump all bundle infomation in the system\n")
406         .append("-bundle     [bundleName]\n")
407         .append("                  dump bundle list information of the specified name in the system\n")
408         .append("-bundle-list      ")
409         .append("dump list of all bundle names in the system\n")
410         .append("-device           ")
411         .append("dump the list of devices involved in the ability infomation in the system\n");
412 }
413 
ShowIllealInfomation(std::string & result)414 void HidumpHelper::ShowIllealInfomation(std::string &result)
415 {
416     result.append(ILLEGAL_INFOMATION);
417 }
418 }  // namespace AppExecFwk
419 }  // namespace OHOS
420