From a9bf27a2df5a93ed0458ff79ca786ba14d946e02 Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Tue, 28 Jun 2022 23:29:59 +0800 Subject: [PATCH] =?UTF-8?q?0628=E6=B7=BB=E5=8A=A0dumper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyb0625 --- .../service/include/device_manager_service.h | 3 + .../include/ipc/standard/ipc_server_stub.h | 7 + .../service/src/device_manager_service.cpp | 18 +++ .../src/ipc/standard/ipc_server_stub.cpp | 21 +++ utils/BUILD.gn | 3 + utils/include/dfx/dm_dfx_constants.h | 17 +++ utils/include/dfx/lite/dm_hidumper.h | 44 ++++++ utils/include/dfx/standard/dm_hidumper.h | 49 +++++++ utils/src/dfx/lite/dm_hidumper.cpp | 46 +++++++ utils/src/dfx/standard/dm_hidumper.cpp | 126 ++++++++++++++++++ 10 files changed, 334 insertions(+) create mode 100644 utils/include/dfx/lite/dm_hidumper.h create mode 100644 utils/include/dfx/standard/dm_hidumper.h create mode 100644 utils/src/dfx/lite/dm_hidumper.cpp create mode 100644 utils/src/dfx/standard/dm_hidumper.cpp diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 1531c0ec..4dc49f7d 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -28,6 +28,8 @@ #include "idevice_manager_service_impl.h" #include "single_instance.h" #include "softbus_listener.h" +#include "dm_dfx_constants.h" +#include "dm_hidumper.h" namespace OHOS { namespace DistributedHardware { @@ -81,6 +83,7 @@ public: bool IsDMServiceImplReady(); + int32_t DmHiDumper(const std::vector& args, std::string &result); private: bool isImplsoLoaded_ = false; std::shared_ptr softbusListener_; diff --git a/services/service/include/ipc/standard/ipc_server_stub.h b/services/service/include/ipc/standard/ipc_server_stub.h index ee8ed98c..d8939b5b 100644 --- a/services/service/include/ipc/standard/ipc_server_stub.h +++ b/services/service/include/ipc/standard/ipc_server_stub.h @@ -113,6 +113,13 @@ public: */ const sptr GetDmListener(std::string pkgName) const; + /** + * @tc.name: IpcServerStub::Dump + * @tc.desc: Dump of the Device Manager Service + * @tc.type: FUNC + */ + int32_t Dump(int32_t fd, const std::vector& args) override; + private: IpcServerStub(); ~IpcServerStub() = default; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index d8d7f734..926d8086 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -269,5 +269,23 @@ bool DeviceManagerService::IsDMServiceImplReady() isImplsoLoaded_ = true; return true; } + +int32_t DeviceManagerService::DmHiDumper(const std::vector& args, std::string &result) +{ + std::vector dumpflag; + HidumpHelper::GetInstance().GetArgsType(args, dumpflag); + for (unsigned int i = 0; i < dumpflag.size(); i++) { + if (dumpflag[i] == HidumperFlag::HIDUMPER_GET_TRUSTED_LIST) { + std::vector deviceList; + std::string extra; + GetTrustedDeviceList(DM_PKG_NAME, extra, deviceList); + for (unsigned int j = 0; j < deviceList.size(); j++) { + HidumpHelper::GetInstance().SetNodeInfo(deviceList[j]); + } + } + } + HidumpHelper::GetInstance().HiDump(args, result); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 63d6e117..e688c9e8 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -22,6 +22,7 @@ #include "device_manager_service.h" #include "dm_constants.h" #include "dm_log.h" +#include "dm_hidumper.h" #include "if_system_ability_manager.h" #include "ipc_cmd_register.h" #include "ipc_skeleton.h" @@ -186,6 +187,26 @@ const sptr IpcServerStub::GetDmListener(std::string pkgName) co return dmListener; } +int32_t IpcServerStub::Dump(int32_t fd, const std::vector& args) +{ + LOGI("DistributedHardwareService Dump."); + std::vector argsStr {}; + for (auto item : args) { + argsStr.emplace_back(Str16ToStr8(item)); + } + std::string result(""); + int ret = DeviceManagerService::GetInstance().DmHiDumper(argsStr, result); + if (ret != DM_OK) { + LOGE("Dump error, ret = %d", ret); + } + ret = dprintf(fd, "%s\n", result.c_str()); + if (ret < 0) { + LOGE("HiDumper dprintf error"); + ret = ERR_DM_FAILED; + } + return ret; +} + void AppDeathRecipient::OnRemoteDied(const wptr &remote) { LOGW("AppDeathRecipient: OnRemoteDied"); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index c411fc3b..7e9e9e65 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -42,6 +42,7 @@ if (defined(ohos_lite)) { ] sources = [ + "${utils_path}/src/dfx/lite/dm_hidumper.cpp", "${utils_path}/src/dfx/lite/dm_hisysevent.cpp", "${utils_path}/src/dfx/lite/dm_hitrace.cpp", "${utils_path}/src/dm_anonymous.cpp", @@ -98,6 +99,7 @@ if (defined(ohos_lite)) { "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", ] sources = [ + "src/dfx/lite/dm_hidumper.cpp", "src/dfx/lite/dm_hisysevent.cpp", "src/dfx/lite/dm_hitrace.cpp", "src/dm_anonymous.cpp", @@ -144,6 +146,7 @@ if (defined(ohos_lite)) { ohos_shared_library("devicemanagerutils") { sources = [ + "src/dfx/standard/dm_hidumper.cpp", "src/dfx/standard/dm_hisysevent.cpp", "src/dfx/standard/dm_hitrace.cpp", "src/dm_anonymous.cpp", diff --git a/utils/include/dfx/dm_dfx_constants.h b/utils/include/dfx/dm_dfx_constants.h index 3d4f1768..9d761ef4 100644 --- a/utils/include/dfx/dm_dfx_constants.h +++ b/utils/include/dfx/dm_dfx_constants.h @@ -16,6 +16,7 @@ #define OHOS_DM_DFX_CONSTANTS_H #include +#include namespace OHOS { namespace DistributedHardware { @@ -64,6 +65,22 @@ const std::string DM_HITRACE_AUTH_TO_CONSULT = "DM_HITRACE_AUTH_TO_CONSULT"; const std::string DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION"; const std::string DM_HITRACE_DEVICE_ONLINE = "DM_HITRACE_DEVICE_ONLINE"; const std::string DM_HITRACE_INIT = "DM_HITRACE_INIT"; + +// HiDumper Flag +enum class HidumperFlag { + HIDUMPER_UNKNOWN = 0, + HIDUMPER_GET_HELP, + HIDUMPER_GET_TRUSTED_LIST, +}; + +// HiDumper info +const std::string ARGS_HELP = "-help"; +const std::string HIDUMPER_GET_TRUSTED_LIST = "-getTrustlist"; + +const std::unordered_map MAP_ARGS = { + { ARGS_HELP, HidumperFlag::HIDUMPER_GET_HELP }, + { HIDUMPER_GET_TRUSTED_LIST, HidumperFlag::HIDUMPER_GET_TRUSTED_LIST }, +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DFX_CONSTANTS_H diff --git a/utils/include/dfx/lite/dm_hidumper.h b/utils/include/dfx/lite/dm_hidumper.h new file mode 100644 index 00000000..21425580 --- /dev/null +++ b/utils/include/dfx/lite/dm_hidumper.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_HIDUMPER_H +#define OHOS_DM_HIDUMPER_H + +#include +#include +#include +#include +#include + +#include "dm_constants.h" +#include "dm_dfx_constants.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class HidumpHelper { +IMPLEMENT_SINGLE_INSTANCE(HidumpHelper); +public: + int32_t HiDump(const std::vector& args, std::string &result); + +private: + int32_t ProcessDump(const HidumperFlag &flag, std::string &result); + int32_t ShowAllLoadTrustedList(std::string &result); + int32_t ShowHelp(std::string &result); + int32_t ShowIllealInfomation(std::string &result); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_HIDUMPER_H diff --git a/utils/include/dfx/standard/dm_hidumper.h b/utils/include/dfx/standard/dm_hidumper.h new file mode 100644 index 00000000..b4176bbf --- /dev/null +++ b/utils/include/dfx/standard/dm_hidumper.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_HIDUMPER_H +#define OHOS_DM_HIDUMPER_H + +#include +#include +#include +#include + +#include "dm_constants.h" +#include "dm_dfx_constants.h" +#include "single_instance.h" +#include "dm_log.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class HidumpHelper { +DECLARE_SINGLE_INSTANCE(HidumpHelper); +public: + int32_t HiDump(const std::vector& args, std::string &result); + void GetArgsType(const std::vector& args, std::vector &Flag); + void SetNodeInfo(const DmDeviceInfo& deviceInfo); +private: + int32_t ProcessDump(const HidumperFlag &flag, std::string &result); + int32_t ShowAllLoadTrustedList(std::string &result); + int32_t ShowHelp(std::string &result); + int32_t ShowIllealInfomation(std::string &result); + +private: + std::vector nodeInfos_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_HIDUMPER_H diff --git a/utils/src/dfx/lite/dm_hidumper.cpp b/utils/src/dfx/lite/dm_hidumper.cpp new file mode 100644 index 00000000..933c16d8 --- /dev/null +++ b/utils/src/dfx/lite/dm_hidumper.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dm_hidumper.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(HidumpHelper); +int32_t HidumpHelper::HiDump(const std::vector& args, std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ShowAllLoadTrustedList(std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ShowHelp(std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ShowIllealInfomation(std::string &result) +{ + return ERR_DM_FAILED; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/utils/src/dfx/standard/dm_hidumper.cpp b/utils/src/dfx/standard/dm_hidumper.cpp new file mode 100644 index 00000000..9341e158 --- /dev/null +++ b/utils/src/dfx/standard/dm_hidumper.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "dm_hidumper.h" +#include "dm_anonymous.h" +#include "dm_dfx_constants.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(HidumpHelper); +int32_t HidumpHelper::HiDump(const std::vector& args, std::string &result) +{ + LOGI("HidumpHelper hidumper start."); + result.clear(); + int32_t errCode = ERR_DM_FAILED; + if (args.empty()) { + return ProcessDump(HidumperFlag::HIDUMPER_GET_HELP, result); + } + auto flag = MAP_ARGS.find(args[0]); + if ((args.size() > 1) || (flag == MAP_ARGS.end())) { + errCode = ProcessDump(HidumperFlag::HIDUMPER_UNKNOWN, result); + } else { + errCode = ProcessDump(flag->second, result); + } + return errCode; +} + +void HidumpHelper::SetNodeInfo(const DmDeviceInfo& deviceInfo) +{ + nodeInfos_.push_back(deviceInfo); +} + +int32_t HidumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result) +{ + LOGI("Process Dump."); + int32_t ret = ERR_DM_FAILED; + switch (flag) { + case HidumperFlag::HIDUMPER_GET_HELP: { + ret = ShowHelp(result); + break; + } + case HidumperFlag::HIDUMPER_GET_TRUSTED_LIST: { + ret = ShowAllLoadTrustedList(result); + break; + } + default: { + ret = ShowIllealInfomation(result); + break; + } + } + return ret; +} + +int32_t HidumpHelper::ShowAllLoadTrustedList(std::string &result) +{ + LOGI("Dump Show All Load Trust List."); + int32_t ret = DM_OK; + + if (nodeInfos_.size() == 0) { + LOGE("Hidumper get trusted list is 0"); + } + for (unsigned int i = 0; i < nodeInfos_.size(); ++i) { + result.append("\n{\n deviceId : ").append(GetAnonyString(nodeInfos_[i].deviceId).c_str()); + result.append("\n{\n deviceName : ").append(nodeInfos_[i].deviceName); + result.append("\n{\n networkId : ").append(GetAnonyString(nodeInfos_[i].networkId).c_str()); + } + nodeInfos_.clear(); + result.replace(result.size() - 1, 1, "\n"); + + LOGI("HidumpHelper ShowAllLoadTrustedList %s", result.c_str()); + return ret; +} + +int32_t HidumpHelper::ShowHelp(std::string &result) +{ + LOGI("Show hidumper help."); + result.append("DistributedHardwareDeviceManager hidumper options:\n"); + result.append(" -help "); + result.append(": Show help\n"); + result.append(" -getTrustlist "); + result.append(": Show all get trusted list:\n"); + result.append(" -getOnlineDeviceInfo "); + result.append(": Show all get online device list\n"); + result.append(" -getOfflineDeviceInfo "); + result.append(": Show all get offline device list\n\n"); + LOGI("result is %s", result.c_str()); + return DM_OK; +} + +int32_t HidumpHelper::ShowIllealInfomation(std::string &result) +{ + LOGI("ShowIllealInfomation Dump."); + result.clear(); + result.append("Unrecognized option, -h for help."); + return ERR_DM_FAILED; +} + +void HidumpHelper::GetArgsType(const std::vector& args, std::vector &Flag) +{ + LOGI("HidumpHelper::GetArgsType."); + if (args.empty()) { + Flag.push_back(HidumperFlag::HIDUMPER_GET_HELP); + } + + auto flag = MAP_ARGS.find(args[0]); + if ((args.size() > 1) || (flag == MAP_ARGS.end())) { + Flag.push_back(flag->second); + } +} +} // namespace DistributedHardware +} // namespace OHOS