1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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  * Description: FtraceDataReader implements
16  */
17 #include "ftrace_data_reader.h"
18 #include "logging.h"
19 
20 #include <cerrno>
21 #include <cstring>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <climits>
27 
28 FTRACE_NS_BEGIN
FtraceDataReader(const std::string& path)29 FtraceDataReader::FtraceDataReader(const std::string& path) : path_(path), readFd_(-1)
30 {
31     char realPath[PATH_MAX + 1] = {0};
32 
33     if ((path.length() >= PATH_MAX) || (realpath(path.c_str(), realPath) == nullptr)) {
34         PROFILER_LOG_ERROR(LOG_CORE, "%s:so filename invalid, errno=%d", __func__, errno);
35     }
36     readFd_ = open(realPath, O_CLOEXEC | O_NONBLOCK);
37     CHECK_TRUE(readFd_ >= 0, NO_RETVAL, "open %s failed, %d", realPath, errno);
38 }
39 
~FtraceDataReader()40 FtraceDataReader::~FtraceDataReader()
41 {
42     CHECK_TRUE(close(readFd_) == 0, NO_RETVAL, "close %s failed, %d", path_.c_str(), errno);
43 }
44 
Read(uint8_t data[], uint32_t size)45 long FtraceDataReader::Read(uint8_t data[], uint32_t size)
46 {
47     ssize_t nBytes = TEMP_FAILURE_RETRY(read(readFd_, data, size));
48     return nBytes;
49 }
50 FTRACE_NS_END
51