/* * 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 "xcollie_utils.h" #include #include #include #include #include "parameter.h" namespace OHOS { namespace HiviewDFX { uint64_t GetCurrentTickMillseconds() { struct timespec t; t.tv_sec = 0; t.tv_nsec = 0; clock_gettime(CLOCK_MONOTONIC, &t); return (int64_t)((t.tv_sec) * SEC_TO_MANOSEC + t.tv_nsec) / SEC_TO_MICROSEC; } bool IsFileNameFormat(char c) { if (c >= '0' && c <= '9') { return false; } if (c >= 'a' && c <= 'z') { return false; } if (c >= 'A' && c <= 'Z') { return false; } if (c == '.' || c == '-' || c == '_') { return false; } return true; } std::string GetSelfProcName() { constexpr uint16_t READ_SIZE = 128; std::ifstream fin; fin.open("/proc/self/comm", std::ifstream::in); if (!fin.is_open()) { XCOLLIE_LOGE("fin.is_open() false"); return ""; } char readStr[READ_SIZE] = {'\0'}; fin.getline(readStr, READ_SIZE - 1); fin.close(); std::string ret = std::string(readStr); ret.erase(std::remove_if(ret.begin(), ret.end(), IsFileNameFormat), ret.end()); return ret; } std::string GetFirstLine(const std::string& path) { char checkPath[PATH_MAX] = {0}; if (realpath(path.c_str(), checkPath) == nullptr) { XCOLLIE_LOGE("canonicalize failed. path is %{public}s", path.c_str()); return ""; } std::ifstream inFile(checkPath); if (!inFile) { return ""; } std::string firstLine; getline(inFile, firstLine); inFile.close(); return firstLine; } std::string GetProcessNameFromProcCmdline(int32_t pid) { std::string procCmdlinePath = "/proc/" + std::to_string(pid) + "/cmdline"; std::string procCmdlineContent = GetFirstLine(procCmdlinePath); if (procCmdlineContent.empty()) { return ""; } size_t procNameStartPos = 0; size_t procNameEndPos = procCmdlineContent.size(); for (size_t i = 0; i < procCmdlineContent.size(); i++) { if (procCmdlineContent[i] == '/') { procNameStartPos = i + 1; } else if (procCmdlineContent[i] == '\0') { procNameEndPos = i; break; } } return procCmdlineContent.substr(procNameStartPos, procNameEndPos - procNameStartPos); } std::string GetLimitedSizeName(std::string name) { const size_t nameStartPos = 0; if (name.size() > MAX_NAME_SIZE) { return name.substr(nameStartPos, MAX_NAME_SIZE); } return name; } bool IsProcessDebug(int32_t pid) { const int buffSize = 128; char param[buffSize] = {0}; std::string filter = "hiviewdfx.freeze.filter." + GetProcessNameFromProcCmdline(pid); GetParameter(filter.c_str(), "", param, buffSize - 1); int32_t debugPid = atoi(param); if (debugPid == pid) { return true; } return false; } void DelayBeforeExit(unsigned int leftTime) { while (leftTime > 0) { leftTime = sleep(leftTime); } } } // end of HiviewDFX } // end of OHOS