• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "dump_manager_client.h"
16 #include <iservice_registry.h>
17 #include <string_ex.h>
18 #include <unistd.h>
19 #include "common.h"
20 #include "hilog_wrapper.h"
21 #include "dump_errors.h"
22 #include "inner/dump_service_id.h"
23 #include "dump_on_demand_load.h"
24 namespace OHOS {
25 namespace HiviewDFX {
26 static constexpr int32_t SLEEP_DUR = 5 * 1000 * 1000;
27 static constexpr int32_t SLEEP_UNIT = 100 * 1000;
28 
DumpManagerClient()29 DumpManagerClient::DumpManagerClient()
30 {
31 }
32 
~DumpManagerClient()33 DumpManagerClient::~DumpManagerClient()
34 {
35 }
36 
Request(std::vector<std::u16string> & args,int outfd)37 int32_t DumpManagerClient::Request(std::vector<std::u16string> &args, int outfd)
38 {
39     if ((args.size() < 1) || (outfd < 0)) {
40         DUMPER_HILOGE(MODULE_CLIENT, "args or outfd failed.");
41         return DumpStatus::DUMP_FAIL;
42     }
43     for (size_t i = 0; i < args.size(); i++) {
44         std::string trimArg = TrimStr(Str16ToStr8(args[i]));
45         if (strlen(trimArg.c_str()) < 1) {
46             DUMPER_HILOGE(MODULE_CLIENT, "trimArg empty.");
47             return DumpStatus::DUMP_FAIL;
48         }
49     }
50     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51     if (sam == nullptr) {
52         DUMPER_HILOGE(MODULE_CLIENT, "sam is nullptr.");
53         return ERROR_GET_SYSTEM_ABILITY_MANAGER;
54     }
55     sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID);
56     if (remoteObject == nullptr) {
57         ErrCode retStart = OnDemandStart(sam, remoteObject);
58         if (remoteObject == nullptr || retStart != ERR_OK) {
59             DUMPER_HILOGE(MODULE_CLIENT, "remoteObject is nullptr.");
60             return ERROR_GET_DUMPER_SERVICE;
61         }
62     }
63     sptr<IDumpBroker> proxy = iface_cast<IDumpBroker>(remoteObject);
64     if (proxy == nullptr) {
65         DUMPER_HILOGE(MODULE_CLIENT, "proxy == nullptr");
66         return DumpStatus::DUMP_FAIL;
67     }
68     int32_t ret = proxy->Request(args, outfd);
69     DUMPER_HILOGD(MODULE_CLIENT, "debug|ret = %{public}d", ret);
70     return ret;
71 }
72 
OnDemandStart(sptr<ISystemAbilityManager> sam,sptr<IRemoteObject> & remoteObject)73 ErrCode DumpManagerClient::OnDemandStart(sptr<ISystemAbilityManager> sam, sptr<IRemoteObject> &remoteObject)
74 {
75     sptr<OnDemandLoadCallback> loadCallback = new OnDemandLoadCallback();
76     int32_t result = sam->LoadSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID, loadCallback);
77     if (result != ERR_OK) {
78         DUMPER_HILOGE(MODULE_CLIENT, "systemAbilityId:%{public}d load failed, result code:%{public}d",
79             DFX_SYS_HIDUMPER_ABILITY_ID, result);
80         return ERROR_GET_DUMPER_SERVICE;
81     }
82 
83     int32_t loop = SLEEP_DUR / SLEEP_UNIT;
84     while (loop-- > 0) {
85         if (!loadCallback->CheckLoadSystemAbilityStatus()) {
86             usleep(SLEEP_UNIT);
87             continue;
88         }
89         remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID);
90         if (remoteObject != nullptr) {
91             return ERR_OK;
92         } else {
93             usleep(SLEEP_UNIT);
94         }
95     }
96 
97     DUMPER_HILOGD(MODULE_CLIENT, "debug|on demand start fail");
98     return ERROR_GET_DUMPER_SERVICE;
99 }
100 } // namespace HiviewDFX
101 } // namespace OHOS
102