106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci * 1506f6ba60Sopenharmony_ci * Description: FtraceDataReader implements 1606f6ba60Sopenharmony_ci */ 1706f6ba60Sopenharmony_ci#include "ftrace_data_reader.h" 1806f6ba60Sopenharmony_ci#include "logging.h" 1906f6ba60Sopenharmony_ci 2006f6ba60Sopenharmony_ci#include <cerrno> 2106f6ba60Sopenharmony_ci#include <cstring> 2206f6ba60Sopenharmony_ci#include <fcntl.h> 2306f6ba60Sopenharmony_ci#include <poll.h> 2406f6ba60Sopenharmony_ci#include <sys/stat.h> 2506f6ba60Sopenharmony_ci#include <unistd.h> 2606f6ba60Sopenharmony_ci#include <climits> 2706f6ba60Sopenharmony_ci 2806f6ba60Sopenharmony_ciFTRACE_NS_BEGIN 2906f6ba60Sopenharmony_ciFtraceDataReader::FtraceDataReader(const std::string& path) : path_(path), readFd_(-1) 3006f6ba60Sopenharmony_ci{ 3106f6ba60Sopenharmony_ci char realPath[PATH_MAX + 1] = {0}; 3206f6ba60Sopenharmony_ci 3306f6ba60Sopenharmony_ci if ((path.length() >= PATH_MAX) || (realpath(path.c_str(), realPath) == nullptr)) { 3406f6ba60Sopenharmony_ci PROFILER_LOG_ERROR(LOG_CORE, "%s:so filename invalid, errno=%d", __func__, errno); 3506f6ba60Sopenharmony_ci } 3606f6ba60Sopenharmony_ci readFd_ = open(realPath, O_CLOEXEC | O_NONBLOCK); 3706f6ba60Sopenharmony_ci CHECK_TRUE(readFd_ >= 0, NO_RETVAL, "open %s failed, %d", realPath, errno); 3806f6ba60Sopenharmony_ci} 3906f6ba60Sopenharmony_ci 4006f6ba60Sopenharmony_ciFtraceDataReader::~FtraceDataReader() 4106f6ba60Sopenharmony_ci{ 4206f6ba60Sopenharmony_ci CHECK_TRUE(close(readFd_) == 0, NO_RETVAL, "close %s failed, %d", path_.c_str(), errno); 4306f6ba60Sopenharmony_ci} 4406f6ba60Sopenharmony_ci 4506f6ba60Sopenharmony_cilong FtraceDataReader::Read(uint8_t data[], uint32_t size) 4606f6ba60Sopenharmony_ci{ 4706f6ba60Sopenharmony_ci ssize_t nBytes = TEMP_FAILURE_RETRY(read(readFd_, data, size)); 4806f6ba60Sopenharmony_ci return nBytes; 4906f6ba60Sopenharmony_ci} 5006f6ba60Sopenharmony_ciFTRACE_NS_END 51