From d146b4f2ed7c83826a3e9619e673ac2460cdcd0a Mon Sep 17 00:00:00 2001 From: yuanye Date: Fri, 1 Nov 2024 15:01:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9fork=E5=92=8Cipc=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=97=B6=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- BUILD.gn | 27 +++++------ demo/cpp/hiperf_demo.cpp | 2 +- include/ipc_utilities.h | 29 ++++++++++++ include/subcommand_record.h | 6 ++- include/utilities.h | 6 +-- src/ipc_utilities.cpp | 90 ++++++++++++++++++++++++++++++++++++ src/main.cpp | 3 +- src/subcommand_record.cpp | 92 +++++++++++++++++++++++-------------- src/subcommand_stat.cpp | 5 +- src/symbols_file.cpp | 1 + src/tracked_command.cpp | 2 + src/utilities.cpp | 74 +++++------------------------ 12 files changed, 218 insertions(+), 119 deletions(-) create mode 100644 include/ipc_utilities.h create mode 100644 src/ipc_utilities.cpp diff --git a/BUILD.gn b/BUILD.gn index 385b07b..b7cecba 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -115,24 +115,25 @@ config("hiperf_inner_config") { } sources_platform_common = [ - "./src/perf_file_format.cpp", "./src/command.cpp", - "./src/subcommand.cpp", - "./src/option.cpp", - "./src/utilities.cpp", - "./src/symbols_file.cpp", - "./src/virtual_runtime.cpp", - "./src/virtual_thread.cpp", - "./src/perf_file_reader.cpp", - "./src/perf_event_record.cpp", "./src/dwarf_encoding.cpp", - "./src/subcommand_help.cpp", - "./src/subcommand_dump.cpp", - "./src/subcommand_report.cpp", + "./src/ipc_utilities.cpp", + "./src/option.cpp", + "./src/perf_event_record.cpp", + "./src/perf_file_format.cpp", + "./src/perf_file_reader.cpp", + "./src/register.cpp", "./src/report.cpp", "./src/report_json_file.cpp", - "./src/register.cpp", + "./src/subcommand.cpp", + "./src/subcommand_dump.cpp", + "./src/subcommand_help.cpp", + "./src/subcommand_report.cpp", + "./src/symbols_file.cpp", "./src/unique_stack_table.cpp", + "./src/utilities.cpp", + "./src/virtual_runtime.cpp", + "./src/virtual_thread.cpp", ] if (is_ohos) { diff --git a/demo/cpp/hiperf_demo.cpp b/demo/cpp/hiperf_demo.cpp index 5bdb8d3..6dc9b98 100644 --- a/demo/cpp/hiperf_demo.cpp +++ b/demo/cpp/hiperf_demo.cpp @@ -77,7 +77,7 @@ int main() } auto it = []() { TestCodeThread(0); - } + }; std::thread workload(it); sleep(waitTime); // try for each thread times diff --git a/include/ipc_utilities.h b/include/ipc_utilities.h new file mode 100644 index 0000000..28dc5ae --- /dev/null +++ b/include/ipc_utilities.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024-2024 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 HIPERF_IPC_UTILITIES_H_ +#define HIPERF_IPC_UTILITIES_H_ + +#include + +namespace OHOS::Developtools::HiPerf { + +bool IsDebugableApp(const std::string& bundleName); +bool IsApplicationEncryped(const int pid); + +void CheckIpcBeforeFork(); + +} // namespace OHOS::Developtools::HiPerf + +#endif // HIPERF_IPC_UTILITIES_H_ \ No newline at end of file diff --git a/include/subcommand_record.h b/include/subcommand_record.h index f7e1115..6d7d760 100644 --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -271,7 +271,8 @@ private: std::thread clientCommandHanle_; bool clientExit_ = false; void ClientCommandHandle(); - bool ClientCommandResponse(bool OK); + bool ClientCommandResponse(bool ok); + bool ClientCommandResponse(const std::string& str); bool IsSamplingRunning(); // for cmdline client std::string controlCmd_ = {}; @@ -283,7 +284,10 @@ private: bool CreateFifoServer(); bool SendFifoAndWaitReply(const std::string &cmd, const std::chrono::milliseconds &timeOut); bool WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut); + void WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut, std::string& reply); void CloseClientThread(); + bool allowIpc_ = true; + std::string HandleAppInfo(); bool PreparePerfEvent(); bool PrepareSysKernel(); diff --git a/include/utilities.h b/include/utilities.h index 7cdb85d..0783de1 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -335,9 +335,8 @@ pid_t GetAppPackagePid(const std::string &appPackage, const pid_t oldPid, const bool IsRestarted(const std::string &appPackage); void CollectPidsByAppname(std::set &pids, const std::string &appPackage); bool CheckAppIsRunning (std::vector &selectPids, const std::string &appPackage, int checkAppMs); -bool IsExistDebugByApp(const std::string& bundleName); -bool IsExistDebugByPid(const std::vector &pids); -bool IsDebugableApp(const std::string& bundleName); +bool IsExistDebugByApp(const std::string& bundleName, std::string& err); +bool IsExistDebugByPid(const std::vector &pids, std::string& err); bool IsSupportNonDebuggableApp(); const std::string GetUserType(); bool GetDeveloperMode(); @@ -345,7 +344,6 @@ bool IsArkJsFile(const std::string& filepath); std::string GetProcessName(int pid); bool NeedAdaptSandboxPath(char *filename, int pid, u16 &headerSize); bool NeedAdaptHMBundlePath(std::string& filename, const std::string& threadname); -bool IsApplicationEncryped(const int pid); template class ScopeGuard { diff --git a/src/ipc_utilities.cpp b/src/ipc_utilities.cpp new file mode 100644 index 0000000..57a8c33 --- /dev/null +++ b/src/ipc_utilities.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2024-2024 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 "ipc_utilities.h" + +#include + +#include "debug_logger.h" +#include "hiperf_hilog.h" +#include "utilities.h" +#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) +#include "application_info.h" +#include "bundle_mgr_proxy.h" +#endif +#if defined(is_ohos) && is_ohos +#include "iservice_registry.h" +#include "system_ability_definition.h" +#endif + +namespace OHOS::Developtools::HiPerf { + +static std::atomic haveIpc = false; + +bool IsDebugableApp(const std::string& bundleName) +{ +#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) + CHECK_TRUE(bundleName.empty(), false, LOG_TYPE_PRINTF, "bundleName is empty!\n"); + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n"); + sptr remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + CHECK_TRUE(remoteObject == nullptr, false, LOG_TYPE_PRINTF, "Get BundleMgr SA failed!\n"); + sptr proxy = iface_cast(remoteObject); + CHECK_TRUE(proxy == nullptr, false, LOG_TYPE_PRINTF, "iface_cast failed!\n"); + AppExecFwk::ApplicationInfo appInfo; + bool ret = proxy->GetApplicationInfo(bundleName, AppExecFwk::GET_APPLICATION_INFO_WITH_DISABLE, + AppExecFwk::Constants::ANY_USERID, appInfo); + CHECK_TRUE(!ret, false, 1, "%s GetApplicationInfo failed!", bundleName.c_str()); + HLOGD("bundleName is %s,appProvisionType: %s", bundleName.c_str(), appInfo.appProvisionType.c_str()); + return appInfo.appProvisionType == AppExecFwk::Constants::APP_PROVISION_TYPE_DEBUG; +#else + return false; +#endif +} + +bool IsApplicationEncryped(const int pid) +{ +#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) + CHECK_TRUE(pid <= 0, false, LOG_TYPE_PRINTF, "Invalid -p value '%d', the pid should be larger than 0\n", pid); + std::string bundleName = GetProcessName(pid); + CHECK_TRUE(bundleName.empty(), false, 1, "bundleName is empty,pid is %d", pid); + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n"); + sptr remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + CHECK_TRUE(remoteObject == nullptr, false, LOG_TYPE_PRINTF, "Get BundleMgr SA failed!\n"); + sptr proxy = iface_cast(remoteObject); + CHECK_TRUE(proxy == nullptr, false, LOG_TYPE_PRINTF, "iface_cast failed!\n"); + + AppExecFwk::ApplicationInfo appInfo; + bool ret = proxy->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, + AppExecFwk::Constants::ANY_USERID, appInfo); + CHECK_TRUE(!ret, false, 1, "%s:%s GetApplicationInfo failed!", __func__, bundleName.c_str()); + bool isEncrypted = (appInfo.applicationReservedFlag & + static_cast(AppExecFwk::ApplicationReservedFlag::ENCRYPTED_APPLICATION)) != 0; + HLOGD("check application encryped.%d : %s, pid:%d", isEncrypted, bundleName.c_str(), pid); + return isEncrypted; +#else + return false; +#endif +} + +void CheckIpcBeforeFork() +{ + if (haveIpc.load()) { + HIPERF_HILOGW(MODULE_DEFAULT, "fork after ipc!"); + } +} + +} // namespace OHOS::Developtools::HiPerf diff --git a/src/main.cpp b/src/main.cpp index 31e4e1f..b7c827f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -112,6 +112,5 @@ int main(const int argc, const char *argv[]) #endif HLOGD("normal exit."); - fflush(stdout); - _exit(0); + return 0; } diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 85f3fd8..237a663 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -32,6 +32,7 @@ #include "command.h" #include "debug_logger.h" #include "hiperf_client.h" +#include "ipc_utilities.h" #if defined(is_ohos) && is_ohos #include "hiperf_hilog.h" #endif @@ -212,6 +213,10 @@ bool SubCommandRecord::GetOptions(std::vector &args) if (!Option::GetOptionValue(args, "--exclude-hiperf", excludeHiperf_)) { return false; } + if (!Option::GetOptionValue(args, "--control", controlCmd_)) { + return false; + } + allowIpc_ = controlCmd_ != CONTROL_CMD_PREPARE; if (!Option::GetOptionValue(args, "-z", compressData_)) { return false; } @@ -257,7 +262,8 @@ bool SubCommandRecord::GetOptions(std::vector &args) if (!Option::GetOptionValue(args, "--app", appPackage_)) { return false; } - if (!IsExistDebugByApp(appPackage_)) { + std::string err = ""; + if (allowIpc_ && !IsExistDebugByApp(appPackage_, err)) { return false; } if (!Option::GetOptionValue(args, "--chkms", checkAppMs_)) { @@ -272,7 +278,7 @@ bool SubCommandRecord::GetOptions(std::vector &args) if (!Option::GetOptionValue(args, "-p", selectPids_)) { return false; } - if (!IsExistDebugByPid(selectPids_)) { + if (allowIpc_ && !IsExistDebugByPid(selectPids_, err)) { return false; } if (!Option::GetOptionValue(args, "-t", selectTids_)) { @@ -330,9 +336,6 @@ bool SubCommandRecord::GetOptions(std::vector &args) if (!Option::GetOptionValue(args, "--pipe_output", clientPipeOutput_)) { return false; } - if (!Option::GetOptionValue(args, "--control", controlCmd_)) { - return false; - } if (!Option::GetOptionValue(args, "--dedup_stack", dedupStack_)) { return false; } @@ -950,30 +953,22 @@ void SubCommandRecord::WriteCommEventBeforeSampling() } } -bool SubCommandRecord::ClientCommandResponse(bool OK) +bool SubCommandRecord::ClientCommandResponse(bool ok) { - using namespace HiperfClient; - if (OK) { - size_t size = write(clientPipeOutput_, ReplyOK.c_str(), ReplyOK.size()); - if (size != ReplyOK.size()) { - char errInfo[ERRINFOLEN] = { 0 }; - strerror_r(errno, errInfo, ERRINFOLEN); - HLOGD("Server:%s -> %d : %zd %d:%s", ReplyOK.c_str(), clientPipeOutput_, size, errno, - errInfo); - return false; - } - return true; - } else { - size_t size = write(clientPipeOutput_, ReplyFAIL.c_str(), ReplyFAIL.size()); - if (size != ReplyFAIL.size()) { - char errInfo[ERRINFOLEN] = { 0 }; - strerror_r(errno, errInfo, ERRINFOLEN); - HLOGD("Server:%s -> %d : %zd %d:%s", ReplyFAIL.c_str(), clientPipeOutput_, size, errno, - errInfo); - return false; - } - return true; + return ClientCommandResponse(ok ? HiperfClient::ReplyOK : HiperfClient::ReplyFAIL); +} + +bool SubCommandRecord::ClientCommandResponse(const std::string& str) +{ + size_t size = write(clientPipeOutput_, str.c_str(), str.size()); + if (size != str.size()) { + char errInfo[ERRINFOLEN] = { 0 }; + strerror_r(errno, errInfo, ERRINFOLEN); + HLOGD("Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, + errInfo); + return false; } + return true; } bool SubCommandRecord::IsSamplingRunning() @@ -1103,7 +1098,10 @@ bool SubCommandRecord::CreateFifoServer() return false; } + CheckIpcBeforeFork(); pid_t pid = fork(); + allowIpc_ = true; + if (pid == -1) { strerror_r(errno, errInfo, ERRINFOLEN); HLOGE("fork failed. %d:%s", errno, errInfo); @@ -1118,12 +1116,24 @@ bool SubCommandRecord::CreateFifoServer() HLOGE("open fifo file(%s) failed. %d:%s", CONTROL_FIFO_FILE_S2C.c_str(), errno, errInfo); return false; } + std::string err = HandleAppInfo(); + if (!err.empty()) { + ClientCommandResponse(err); + return false; + } nullFd_ = open("/dev/null", O_WRONLY); (void)dup2(nullFd_, STDOUT_FILENO); // redirect stdout to /dev/null } else { // parent process isFifoClient_ = true; int fd = open(CONTROL_FIFO_FILE_S2C.c_str(), O_RDONLY | O_NONBLOCK); - if (fd == -1 or !WaitFifoReply(fd, CONTROL_WAITREPY_TOMEOUT)) { + std::string reply = ""; + if (fd != -1) { + WaitFifoReply(fd, CONTROL_WAITREPY_TOMEOUT, reply); + } + if (fd == -1 or reply != HiperfClient::ReplyOK) { + if (reply != HiperfClient::ReplyOK) { + printf("%s\n", reply.c_str()); + } close(fd); kill(pid, SIGKILL); remove(CONTROL_FIFO_FILE_C2S.c_str()); @@ -1168,12 +1178,19 @@ bool SubCommandRecord::SendFifoAndWaitReply(const std::string &cmd, const std::c } bool SubCommandRecord::WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut) +{ + std::string reply; + WaitFifoReply(fd, timeOut, reply); + return reply == HiperfClient::ReplyOK; +} + +void SubCommandRecord::WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut, std::string& reply) { struct pollfd pollFd { fd, POLLIN, 0 }; int polled = poll(&pollFd, 1, timeOut.count()); - std::string reply; + reply.clear(); if (polled > 0) { while (true) { char c; @@ -1192,11 +1209,6 @@ bool SubCommandRecord::WaitFifoReply(int fd, const std::chrono::milliseconds &ti } else { HLOGD("wait fifo file(%s) failed", CONTROL_FIFO_FILE_S2C.c_str()); } - - if (reply == HiperfClient::ReplyOK) { - return true; - } - return false; } bool SubCommandRecord::OnSubCommand(std::vector &args) @@ -1907,6 +1919,18 @@ bool SubCommandRecord::OnlineReportData() __FUNCTION__, ret ? "success" : "fail"); return ret; } + +std::string SubCommandRecord::HandleAppInfo() +{ + std::string err = ""; + if (!IsExistDebugByApp(appPackage_, err)) { + return err; + } + if (!IsExistDebugByPid(selectPids_, err)) { + return err; + } + return err; +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp index 0fb4b03..e8823e4 100644 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -99,7 +99,8 @@ bool SubCommandStat::ParseOption(std::vector &args) HLOGD("get option --app failed"); return false; } - if (!IsExistDebugByApp(appPackage_)) { + std::string err = ""; + if (!IsExistDebugByApp(appPackage_, err)) { return false; } if (!Option::GetOptionValue(args, "--chkms", checkAppMs_)) { @@ -109,7 +110,7 @@ bool SubCommandStat::ParseOption(std::vector &args) HLOGD("get option -p failed"); return false; } - if (!IsExistDebugByPid(selectPids_)) { + if (!IsExistDebugByPid(selectPids_, err)) { return false; } if (!Option::GetOptionValue(args, "-t", selectTids_)) { diff --git a/src/symbols_file.cpp b/src/symbols_file.cpp index a3843e7..0a5208b 100644 --- a/src/symbols_file.cpp +++ b/src/symbols_file.cpp @@ -41,6 +41,7 @@ #include "hiperf_hilog.h" #include "unwinder_config.h" #include "utilities.h" +#include "ipc_utilities.h" using namespace OHOS::HiviewDFX; using namespace std::chrono; diff --git a/src/tracked_command.cpp b/src/tracked_command.cpp index 155b2b0..79dbda7 100644 --- a/src/tracked_command.cpp +++ b/src/tracked_command.cpp @@ -19,6 +19,7 @@ #include #include #include "debug_logger.h" +#include "ipc_utilities.h" namespace OHOS { namespace Developtools { @@ -78,6 +79,7 @@ bool TrackedCommand::CreateChildProcess() if (!InitSignalPipes(startFd, ackFd)) { return false; } + CheckIpcBeforeFork(); pid_t pid = fork(); if (pid == -1) { HLOGE("fork() failed in TrackedCommand::CreateChildProcess()"); diff --git a/src/utilities.cpp b/src/utilities.cpp index a08da39..db8b6c9 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -25,16 +25,7 @@ #endif #include "hiperf_hilog.h" -#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) -#include "application_info.h" -#include "bundle_mgr_proxy.h" -#endif -#if defined(is_ohos) && is_ohos -#include "iservice_registry.h" -#include "system_ability_definition.h" -using namespace OHOS; -using namespace OHOS::AppExecFwk; -#endif +#include "ipc_utilities.h" using namespace std::chrono; namespace OHOS { @@ -661,7 +652,7 @@ bool CheckAppIsRunning (std::vector &selectPids, const std::string &appPa return true; } -bool IsExistDebugByApp(const std::string& bundleName) +bool IsExistDebugByApp(const std::string& bundleName, std::string& err) { std::string bundleNameTmp = bundleName; auto pos = bundleNameTmp.find(":"); @@ -670,17 +661,22 @@ bool IsExistDebugByApp(const std::string& bundleName) } if (!IsSupportNonDebuggableApp() && !bundleNameTmp.empty() && !IsDebugableApp(bundleNameTmp)) { HLOGE("--app option only support debug application."); - printf("--app option only support debug application\n"); + err = "--app option only support debug application"; + printf("%s\n", err.c_str()); return false; } return true; } -bool IsExistDebugByPid(const std::vector &pids) +bool IsExistDebugByPid(const std::vector &pids, std::string& err) { CHECK_TRUE(pids.empty(), true, 1, "IsExistDebugByPid: pids is empty."); for (auto pid : pids) { - CHECK_TRUE(pid <= 0, false, LOG_TYPE_PRINTF, "Invalid -p value '%d', the pid should be larger than 0\n", pid); + if (pid <= 0) { + err = "Invalid -p value '" + std::to_string(pid) + "', the pid should be larger than 0"; + printf("%s\n", err.c_str()); + return false; + } std::string bundleName = GetProcessName(pid); auto pos = bundleName.find(":"); if (pos != std::string::npos) { @@ -688,34 +684,14 @@ bool IsExistDebugByPid(const std::vector &pids) } if (!IsSupportNonDebuggableApp() && !IsDebugableApp(bundleName)) { HLOGE("-p option only support debug application for %s", bundleName.c_str()); - printf("-p option only support debug application\n"); + err = "-p option only support debug application"; + printf("%s\n", err.c_str()); return false; } } return true; } -bool IsDebugableApp(const std::string& bundleName) -{ -#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) - CHECK_TRUE(bundleName.empty(), false, LOG_TYPE_PRINTF, "bundleName is empty!\n"); - sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n"); - sptr remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - CHECK_TRUE(remoteObject == nullptr, false, LOG_TYPE_PRINTF, "Get BundleMgr SA failed!\n"); - sptr proxy = iface_cast(remoteObject); - CHECK_TRUE(proxy == nullptr, false, LOG_TYPE_PRINTF, "iface_cast failed!\n"); - AppExecFwk::ApplicationInfo appInfo; - bool ret = proxy->GetApplicationInfo(bundleName, AppExecFwk::GET_APPLICATION_INFO_WITH_DISABLE, - AppExecFwk::Constants::ANY_USERID, appInfo); - CHECK_TRUE(!ret, false, 1, "%s GetApplicationInfo failed!", bundleName.c_str()); - HLOGD("bundleName is %s,appProvisionType: %s", bundleName.c_str(), appInfo.appProvisionType.c_str()); - return appInfo.appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG; -#else - return false; -#endif -} - bool IsSupportNonDebuggableApp() { // root first @@ -866,32 +842,6 @@ bool IsHiviewCall() return false; #endif } - -bool IsApplicationEncryped(const int pid) -{ -#if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) - CHECK_TRUE(pid <= 0, false, LOG_TYPE_PRINTF, "Invalid -p value '%d', the pid should be larger than 0\n", pid); - std::string bundleName = GetProcessName(pid); - CHECK_TRUE(bundleName.empty(), false, 1, "bundleName is empty,pid is %d", pid); - sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n"); - sptr remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - CHECK_TRUE(remoteObject == nullptr, false, LOG_TYPE_PRINTF, "Get BundleMgr SA failed!\n"); - sptr proxy = iface_cast(remoteObject); - CHECK_TRUE(proxy == nullptr, false, LOG_TYPE_PRINTF, "iface_cast failed!\n"); - - AppExecFwk::ApplicationInfo appInfo; - bool ret = proxy->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, - AppExecFwk::Constants::ANY_USERID, appInfo); - CHECK_TRUE(!ret, false, 1, "%s:%s GetApplicationInfo failed!", __func__, bundleName.c_str()); - bool isEncrypted = (appInfo.applicationReservedFlag & - static_cast(AppExecFwk::ApplicationReservedFlag::ENCRYPTED_APPLICATION)) != 0; - HLOGD("check application encryped.%d : %s, pid:%d", isEncrypted, bundleName.c_str(), pid); - return isEncrypted; -#else - return false; -#endif -} } // namespace HiPerf } // namespace Developtools } // namespace OHOS From c5f19813c3e547a58ce46152ceb4d928ad3a8f4f Mon Sep 17 00:00:00 2001 From: yuanye Date: Fri, 1 Nov 2024 15:07:20 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- src/subcommand_record.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 237a663..467b605 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -964,8 +964,7 @@ bool SubCommandRecord::ClientCommandResponse(const std::string& str) if (size != str.size()) { char errInfo[ERRINFOLEN] = { 0 }; strerror_r(errno, errInfo, ERRINFOLEN); - HLOGD("Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, - errInfo); + HLOGD("Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, errInfo); return false; } return true; From 39aa4495f464c3b05c0d0ba752f884f2f95ad9fa Mon Sep 17 00:00:00 2001 From: yuanye Date: Fri, 1 Nov 2024 17:23:44 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0haveIpc=E8=B5=8B=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- src/ipc_utilities.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ipc_utilities.cpp b/src/ipc_utilities.cpp index 57a8c33..15272c6 100644 --- a/src/ipc_utilities.cpp +++ b/src/ipc_utilities.cpp @@ -36,6 +36,7 @@ static std::atomic haveIpc = false; bool IsDebugableApp(const std::string& bundleName) { #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) + haveIpc.store(true); CHECK_TRUE(bundleName.empty(), false, LOG_TYPE_PRINTF, "bundleName is empty!\n"); sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n"); @@ -57,6 +58,7 @@ bool IsDebugableApp(const std::string& bundleName) bool IsApplicationEncryped(const int pid) { #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) + haveIpc.store(true); CHECK_TRUE(pid <= 0, false, LOG_TYPE_PRINTF, "Invalid -p value '%d', the pid should be larger than 0\n", pid); std::string bundleName = GetProcessName(pid); CHECK_TRUE(bundleName.empty(), false, 1, "bundleName is empty,pid is %d", pid); From 0b745b2ec52ebf66e966557db9f7082c00f2e489 Mon Sep 17 00:00:00 2001 From: yuanye Date: Fri, 1 Nov 2024 17:35:17 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- include/subcommand_record.h | 4 ++-- src/ipc_utilities.cpp | 8 ++++---- src/subcommand_record.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/subcommand_record.h b/include/subcommand_record.h index 6d7d760..7ec6aa6 100644 --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -271,10 +271,11 @@ private: std::thread clientCommandHanle_; bool clientExit_ = false; void ClientCommandHandle(); - bool ClientCommandResponse(bool ok); + bool ClientCommandResponse(bool response); bool ClientCommandResponse(const std::string& str); bool IsSamplingRunning(); // for cmdline client + bool allowIpc_ = true; std::string controlCmd_ = {}; bool isFifoServer_ = false; bool isFifoClient_ = false; @@ -286,7 +287,6 @@ private: bool WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut); void WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut, std::string& reply); void CloseClientThread(); - bool allowIpc_ = true; std::string HandleAppInfo(); bool PreparePerfEvent(); diff --git a/src/ipc_utilities.cpp b/src/ipc_utilities.cpp index 15272c6..0492c85 100644 --- a/src/ipc_utilities.cpp +++ b/src/ipc_utilities.cpp @@ -31,12 +31,12 @@ namespace OHOS::Developtools::HiPerf { -static std::atomic haveIpc = false; +static std::atomic g_haveIpc = false; bool IsDebugableApp(const std::string& bundleName) { #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) - haveIpc.store(true); + g_haveIpc.store(true); CHECK_TRUE(bundleName.empty(), false, LOG_TYPE_PRINTF, "bundleName is empty!\n"); sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n"); @@ -58,7 +58,7 @@ bool IsDebugableApp(const std::string& bundleName) bool IsApplicationEncryped(const int pid) { #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE) - haveIpc.store(true); + g_haveIpc.store(true); CHECK_TRUE(pid <= 0, false, LOG_TYPE_PRINTF, "Invalid -p value '%d', the pid should be larger than 0\n", pid); std::string bundleName = GetProcessName(pid); CHECK_TRUE(bundleName.empty(), false, 1, "bundleName is empty,pid is %d", pid); @@ -84,7 +84,7 @@ bool IsApplicationEncryped(const int pid) void CheckIpcBeforeFork() { - if (haveIpc.load()) { + if (g_haveIpc.load()) { HIPERF_HILOGW(MODULE_DEFAULT, "fork after ipc!"); } } diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 467b605..dca44ca 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -953,9 +953,9 @@ void SubCommandRecord::WriteCommEventBeforeSampling() } } -bool SubCommandRecord::ClientCommandResponse(bool ok) +bool SubCommandRecord::ClientCommandResponse(bool response) { - return ClientCommandResponse(ok ? HiperfClient::ReplyOK : HiperfClient::ReplyFAIL); + return ClientCommandResponse(response ? HiperfClient::ReplyOK : HiperfClient::ReplyFAIL); } bool SubCommandRecord::ClientCommandResponse(const std::string& str)