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