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_cpu_client.h"
16 #include <iservice_registry.h>
17 #include <string_ex.h>
18 #include <unistd.h>
19 #include "hilog_wrapper.h"
20 #include "dump_errors.h"
21 #include "inner/dump_service_id.h"
22 #include "dump_on_demand_load.h"
23 #include "dump_broker_cpu_proxy.h"
24 namespace OHOS {
25 namespace HiviewDFX {
DumpManagerCpuClient()26 DumpManagerCpuClient::DumpManagerCpuClient()
27 {
28 }
29
~DumpManagerCpuClient()30 DumpManagerCpuClient::~DumpManagerCpuClient()
31 {
32 if (proxy_ != nullptr) {
33 auto remoteObject = proxy_->AsObject();
34 if (remoteObject != nullptr) {
35 remoteObject->RemoveDeathRecipient(deathRecipient_);
36 }
37 }
38 }
39
Request(DumpCpuData & dumpCpuData)40 int32_t DumpManagerCpuClient::Request(DumpCpuData &dumpCpuData)
41 {
42 if (Connect() != ERR_OK) {
43 DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|cpu connect error");
44 return DumpStatus::DUMP_FAIL;
45 }
46 int32_t ret = proxy_->Request(dumpCpuData);
47 return ret;
48 }
49
Connect()50 ErrCode DumpManagerCpuClient::Connect()
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (proxy_ != nullptr) {
54 return ERR_OK;
55 }
56 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 if (sam == nullptr) {
58 DUMPER_HILOGI(MODULE_CPU_CLIENT, "sam is null");
59 return ERROR_GET_SYSTEM_ABILITY_MANAGER;
60 }
61 sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_CPU_ABILITY_ID);
62 if (remoteObject == nullptr) {
63 DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu remoteobject is null");
64 return ERROR_GET_DUMPER_SERVICE;
65 }
66 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DumpManagerCpuDeathRecipient());
67 if (deathRecipient_ == nullptr) {
68 DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu deathRecipient_ is null");
69 return ERR_NO_MEMORY;
70 }
71 if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
72 DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu IsProxyObject is null");
73 return ERROR_ADD_DEATH_RECIPIENT;
74 }
75 proxy_ = iface_cast<IDumpCpuBroker>(remoteObject);
76 return ERR_OK;
77 }
78
Reset()79 void DumpManagerCpuClient::Reset()
80 {
81 std::lock_guard<std::mutex> lock(mutex_);
82 if (proxy_ == nullptr) {
83 return;
84 }
85 auto serviceRemote = proxy_->AsObject();
86 if (serviceRemote != nullptr) {
87 serviceRemote->RemoveDeathRecipient(deathRecipient_);
88 proxy_ = nullptr;
89 DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|disconnected");
90 }
91 }
92
ResetProxy(const wptr<IRemoteObject> & remote)93 void DumpManagerCpuClient::ResetProxy(const wptr<IRemoteObject>& remote)
94 {
95 std::lock_guard<std::mutex> lock(mutex_);
96 if (proxy_ == nullptr) {
97 return;
98 }
99 auto serviceRemote = proxy_->AsObject();
100 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
101 serviceRemote->RemoveDeathRecipient(deathRecipient_);
102 proxy_ = nullptr;
103 DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|disconnected");
104 }
105 }
106
OnRemoteDied(const wptr<IRemoteObject> & remote)107 void DumpManagerCpuClient::DumpManagerCpuDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
108 {
109 if (remote == nullptr) {
110 return;
111 }
112 DumpManagerCpuClient::GetInstance().ResetProxy(remote);
113 }
114 } // namespace HiviewDFX
115 } // namespace OHOS
116