• 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     if (proxy_ != nullptr) {
36         auto remoteObject = proxy_->AsObject();
37         if (remoteObject != nullptr) {
38             remoteObject->RemoveDeathRecipient(deathRecipient_);
39         }
40     }
41 }
42 
Request(std::vector<std::u16string> & args,int outfd)43 int32_t DumpManagerClient::Request(std::vector<std::u16string> &args, int outfd)
44 {
45     if ((args.size() < 1) || (outfd < 0)) {
46         return DumpStatus::DUMP_FAIL;
47     }
48     for (size_t i = 0; i < args.size(); i++) {
49         std::string trimArg = TrimStr(Str16ToStr8(args[i]));
50         if (strlen(trimArg.c_str()) < 1) {
51             return DumpStatus::DUMP_FAIL;
52         }
53     }
54     if (Connect() != ERR_OK) {
55         return DumpStatus::DUMP_FAIL;
56     }
57     int32_t ret = proxy_->Request(args, outfd);
58     DUMPER_HILOGD(MODULE_CLIENT, "debug|ret=%{public}d", ret);
59     return ret;
60 }
61 
Connect()62 ErrCode DumpManagerClient::Connect()
63 {
64     std::lock_guard<std::mutex> lock(mutex_);
65     if (proxy_ != nullptr) {
66         return ERR_OK;
67     }
68     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
69     if (sam == nullptr) {
70         return ERROR_GET_SYSTEM_ABILITY_MANAGER;
71     }
72     sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID);
73     if (remoteObject == nullptr) {
74         ErrCode retStart = OnDemandStart(sam, remoteObject);
75         if (remoteObject == nullptr || retStart != ERR_OK) {
76             return ERROR_GET_DUMPER_SERVICE;
77         }
78     }
79     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DumpManagerDeathRecipient());
80     if (deathRecipient_ == nullptr) {
81         return ERR_NO_MEMORY;
82     }
83     if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
84         return ERROR_ADD_DEATH_RECIPIENT;
85     }
86     proxy_ = iface_cast<IDumpBroker>(remoteObject);
87     DUMPER_HILOGD(MODULE_CLIENT, "debug|connected");
88     return ERR_OK;
89 }
90 
IsConnected()91 bool DumpManagerClient::IsConnected()
92 {
93     return (proxy_ != nullptr);
94 }
95 
Reset()96 void DumpManagerClient::Reset()
97 {
98     std::lock_guard<std::mutex> lock(mutex_);
99     if (proxy_ == nullptr) {
100         return;
101     }
102     auto serviceRemote = proxy_->AsObject();
103     if (serviceRemote != nullptr) {
104         serviceRemote->RemoveDeathRecipient(deathRecipient_);
105         proxy_ = nullptr;
106         DUMPER_HILOGD(MODULE_CLIENT, "debug|disconnected");
107     }
108 }
109 
ResetProxy(const wptr<IRemoteObject> & remote)110 void DumpManagerClient::ResetProxy(const wptr<IRemoteObject>& remote)
111 {
112     std::lock_guard<std::mutex> lock(mutex_);
113     if (proxy_ == nullptr) {
114         return;
115     }
116     auto serviceRemote = proxy_->AsObject();
117     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
118         serviceRemote->RemoveDeathRecipient(deathRecipient_);
119         proxy_ = nullptr;
120         DUMPER_HILOGD(MODULE_CLIENT, "debug|disconnected");
121     }
122 }
123 
OnRemoteDied(const wptr<IRemoteObject> & remote)124 void DumpManagerClient::DumpManagerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
125 {
126     if (remote == nullptr) {
127         return;
128     }
129     DumpManagerClient::GetInstance().ResetProxy(remote);
130 }
131 
OnDemandStart(sptr<ISystemAbilityManager> sam,sptr<IRemoteObject> & remoteObject)132 ErrCode DumpManagerClient::OnDemandStart(sptr<ISystemAbilityManager> sam, sptr<IRemoteObject> &remoteObject)
133 {
134     sptr<OnDemandLoadCallback> loadCallback = new OnDemandLoadCallback();
135     int32_t result = sam->LoadSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID, loadCallback);
136     if (result != ERR_OK) {
137         DUMPER_HILOGE(MODULE_CLIENT, "systemAbilityId:%{public}d load failed, result code:%{public}d",
138             DFX_SYS_HIDUMPER_ABILITY_ID, result);
139         return ERROR_GET_DUMPER_SERVICE;
140     }
141 
142     int32_t loop = SLEEP_DUR / SLEEP_UNIT;
143     while (loop-- > 0) {
144         if (!loadCallback->CheckLoadSystemAbilityStatus()) {
145             usleep(SLEEP_UNIT);
146             continue;
147         }
148         remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID);
149         if (remoteObject != nullptr) {
150             return ERR_OK;
151         } else {
152             usleep(SLEEP_UNIT);
153         }
154     }
155 
156     DUMPER_HILOGD(MODULE_CLIENT, "debug|on demand start fail");
157     return ERROR_GET_DUMPER_SERVICE;
158 }
159 } // namespace HiviewDFX
160 } // namespace OHOS
161