fix hiperf codecheck issue

Signed-off-by: wenlong12 <wenlong12@huawei.com>

Signed-off-by: wenlong12 <wenlong12@huawei.com>
This commit is contained in:
wenlong12 2022-02-18 15:42:09 +08:00
parent 97cb157d7a
commit 4f8cc24d12
19 changed files with 142 additions and 60 deletions

View File

@ -478,7 +478,9 @@ USED_FUNCTION int main(int argc, char *argv[])
CPU_ZERO(&mask); CPU_ZERO(&mask);
CPU_SET(option.boundCpu, &mask); CPU_SET(option.boundCpu, &mask);
if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) { if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
printf("Set CPU(%d) affinity failue, ERROR:%s\n", option.boundCpu, strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("Set CPU(%d) affinity failue, ERROR:%s\n", option.boundCpu, errInfo);
} }
} }
if (!option.noWait) { if (!option.noWait) {

View File

@ -159,8 +159,8 @@ private:
#define perror(format, ...) \ #define perror(format, ...) \
do { \ do { \
std::perror(format); \ std::perror(format); \
DebugLogger::GetInstance()->Log(LEVEL_STDOUT, HILOG_TAG, format "<%d:%s>\n", \ DebugLogger::GetInstance()->Log(LEVEL_STDOUT, HILOG_TAG, format "<%d>\n", \
##__VA_ARGS__, errno, strerror(errno)); \ ##__VA_ARGS__, errno); \
} while (0) } while (0)
#endif #endif
#endif #endif
@ -263,7 +263,7 @@ private:
#ifndef HLOGEP #ifndef HLOGEP
#define HLOGEP(format, ...) \ #define HLOGEP(format, ...) \
HLOG(LEVEL_ERROR, format "(errno %d:%s)", ##__VA_ARGS__, errno, strerror(errno)) HLOG(LEVEL_ERROR, format "(errno %d)", ##__VA_ARGS__, errno)
#endif #endif
#ifndef HLOGF #ifndef HLOGF

View File

@ -87,7 +87,9 @@ constexpr uint64_t KILO = 1024;
namespace OHOS { namespace OHOS {
namespace Developtools { namespace Developtools {
namespace HiPerf { namespace HiPerf {
std::string CanonicalizeSpecPath(const char* src);
const std::string EMPTY_STRING = ""; const std::string EMPTY_STRING = "";
const ssize_t ERRINFOLEN = 512;
// string function // string function
class MemoryHold { class MemoryHold {
@ -103,6 +105,9 @@ public:
} }
// for null end // for null end
char *p = new char[view.size() + 1]; char *p = new char[view.size() + 1];
if (p == nullptr) {
return "";
}
p[view.size()] = '\0'; p[view.size()] = '\0';
std::copy(view.data(), view.data() + view.size(), p); std::copy(view.data(), view.data() + view.size(), p);
holder_.emplace_back(p); holder_.emplace_back(p);
@ -293,7 +298,7 @@ float Percentage(const T &a, const T &b)
} }
bool IsRoot(); bool IsRoot();
bool PowerOfTwo(int n); bool PowerOfTwo(uint64_t n);
#define INDENT_ONE_LEVEL (indent + 1) #define INDENT_ONE_LEVEL (indent + 1)
#define INDENT_TWO_LEVEL (indent + 2) #define INDENT_TWO_LEVEL (indent + 2)

View File

@ -294,8 +294,8 @@ public:
void EnableHilog(); void EnableHilog();
private: private:
static const int PIPE_READ = 0; static const uint64_t PIPE_READ = 0;
static const int PIPE_WRITE = 1; static const uint64_t PIPE_WRITE = 1;
static constexpr size_t SIZE_ARGV_TAIL = 1; // nullptr static constexpr size_t SIZE_ARGV_TAIL = 1; // nullptr
static constexpr int64_t THOUSAND = 1000; static constexpr int64_t THOUSAND = 1000;

View File

@ -35,6 +35,7 @@ namespace OHOS {
namespace Developtools { namespace Developtools {
namespace HiPerf { namespace HiPerf {
namespace HiperfClient { namespace HiperfClient {
const ssize_t ERRINFOLEN = 512;
void RecordOption::SetOption(const std::string &name, bool enable) void RecordOption::SetOption(const std::string &name, bool enable)
{ {
auto it = std::find(args_.begin(), args_.end(), name); auto it = std::find(args_.begin(), args_.end(), name);
@ -384,13 +385,17 @@ bool Client::Start(const std::vector<std::string> &args)
int clientToServerFd[2]; int clientToServerFd[2];
int serverToClientFd[2]; int serverToClientFd[2];
if (pipe(clientToServerFd) != 0 || pipe(serverToClientFd) != 0) { if (pipe(clientToServerFd) != 0 || pipe(serverToClientFd) != 0) {
HIPERF_HILOGD(MODULE_CPP_API, "failed to create pipe: %" HILOG_PUBLIC "s", strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HIPERF_HILOGD(MODULE_CPP_API, "failed to create pipe: %" HILOG_PUBLIC "s", errInfo);
return false; return false;
} }
hperfPid_ = fork(); hperfPid_ = fork();
if (hperfPid_ == -1) { if (hperfPid_ == -1) {
HIPERF_HILOGD(MODULE_CPP_API, "failed to fork: %" HILOG_PUBLIC "s", strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HIPERF_HILOGD(MODULE_CPP_API, "failed to fork: %" HILOG_PUBLIC "s", errInfo);
return false; return false;
} else if (hperfPid_ == 0) { } else if (hperfPid_ == 0) {
// child process // child process
@ -411,9 +416,11 @@ bool Client::Start(const std::vector<std::string> &args)
argv[i] = nullptr; argv[i] = nullptr;
execv(argv[0], argv); execv(argv[0], argv);
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HIPERF_HILOGD(MODULE_CPP_API, HIPERF_HILOGD(MODULE_CPP_API,
"failed to call exec: '%" HILOG_PUBLIC "s' %" HILOG_PUBLIC "s\n", "failed to call exec: '%" HILOG_PUBLIC "s' %" HILOG_PUBLIC "s\n",
executeCommandPath_.c_str(), strerror(errno)); executeCommandPath_.c_str(), errInfo);
exit(0); exit(0);
} else { } else {
// parent process // parent process

View File

@ -255,17 +255,18 @@ bool DebugLogger::OpenLog(const std::string &tempLogPath, const std::string &fla
} }
if (!tempLogPath.empty()) { if (!tempLogPath.empty()) {
fclose(file_); fclose(file_);
file_ = fopen(tempLogPath.c_str(), flags.c_str()); std::string resolvedPath = CanonicalizeSpecPath(tempLogPath.c_str());
file_ = fopen(resolvedPath.c_str(), flags.c_str());
} }
if (file_ != nullptr) { if (file_ != nullptr) {
// already open // already open
return true; return true;
} else { } else {
file_ = fopen(logPath_.c_str(), "w"); std::string resolvedPath = CanonicalizeSpecPath(logPath_.c_str());
file_ = fopen(resolvedPath.c_str(), "w");
} }
if (file_ == nullptr) { if (file_ == nullptr) {
fprintf(stdout, "unable save log file to '%s' because '%d:%s'\n", logPath_.c_str(), errno, fprintf(stdout, "unable save log file to '%s' because '%d'\n", logPath_.c_str(), errno);
strerror(errno));
return false; return false;
} else { } else {
fseek(file_, 0, SEEK_SET); fseek(file_, 0, SEEK_SET);

View File

@ -14,6 +14,7 @@
*/ */
#include <elf_parser.h> #include <elf_parser.h>
#include "utilities.h"
using namespace OHOS::Developtools::HiPerf::ELF; using namespace OHOS::Developtools::HiPerf::ELF;
namespace OHOS { namespace OHOS {
@ -22,9 +23,11 @@ namespace HiPerf {
ElfFile::ElfFile(const std::string &filename) ElfFile::ElfFile(const std::string &filename)
{ {
#if is_mingw #if is_mingw
fd_ = open(filename.c_str(), O_RDONLY | O_BINARY); std::string resolvedPath = CanonicalizeSpecPath(filename.c_str());
fd_ = open(resolvedPath.c_str(), O_RDONLY | O_BINARY);
#else #else
fd_ = open(filename.c_str(), O_RDONLY); std::string resolvedPath = CanonicalizeSpecPath(filename.c_str());
fd_ = open(resolvedPath.c_str(), O_RDONLY);
#endif #endif
if (fd_ != -1) { if (fd_ != -1) {
struct stat sb; struct stat sb;

View File

@ -1023,11 +1023,15 @@ bool PerfEvents::CreateFdEvents(void)
} else { } else {
// clang-format off // clang-format off
if (verboseReport_) { if (verboseReport_) {
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("%s event is not supported by the kernel on cpu %d. reason: %d:%s\n", printf("%s event is not supported by the kernel on cpu %d. reason: %d:%s\n",
eventItem.configName.c_str(), cpus_[icpu], errno, strerror(errno)); eventItem.configName.c_str(), cpus_[icpu], errno, errInfo);
} }
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HLOGE("%s event is not supported by the kernel on cpu %d. reason: %d:%s\n", HLOGE("%s event is not supported by the kernel on cpu %d. reason: %d:%s\n",
eventItem.configName.c_str(), cpus_[icpu], errno, strerror(errno)); eventItem.configName.c_str(), cpus_[icpu], errno, errInfo);
// clang-format on // clang-format on
break; // jump to next cpu break; // jump to next cpu
} }

View File

@ -33,7 +33,8 @@ const int SIZE_FETURE_COUNT = 8;
std::unique_ptr<PerfFileReader> PerfFileReader::Instance(const std::string &fileName) std::unique_ptr<PerfFileReader> PerfFileReader::Instance(const std::string &fileName)
{ {
FILE *fp = fopen(fileName.c_str(), "rb"); std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str());
FILE *fp = fopen(resolvedPath.c_str(), "rb");
if (fp == nullptr) { if (fp == nullptr) {
HLOGE("fail to open file %s", fileName.c_str()); HLOGE("fail to open file %s", fileName.c_str());
return nullptr; return nullptr;
@ -78,6 +79,7 @@ end:
PerfFileReader::PerfFileReader(const std::string &fileName, FILE *fp) : fp_(fp), fileName_(fileName) PerfFileReader::PerfFileReader(const std::string &fileName, FILE *fp) : fp_(fp), fileName_(fileName)
{ {
featureSectionOffset_ = 0;
struct stat fileStat; struct stat fileStat;
if (fp != nullptr) { if (fp != nullptr) {
if (fstat(fileno(fp), &fileStat) != -1 and fileStat.st_size > 0) { if (fstat(fileno(fp), &fileStat) != -1 and fileStat.st_size > 0) {
@ -320,7 +322,7 @@ bool PerfFileReader::Read(void *buf, size_t len)
} }
if (fread(buf, len, 1, fp_) != 1) { if (fread(buf, len, 1, fp_) != 1) {
printf("failed to read file: %s", strerror(errno)); printf("failed to read file: %d", errno);
return false; return false;
} }
return true; return true;
@ -342,7 +344,7 @@ bool PerfFileReader::Read(char *buf, uint64_t offset, size_t len)
} }
if (fread(buf, len, 1, fp_) != 1) { if (fread(buf, len, 1, fp_) != 1) {
printf("failed to read file: %s", strerror(errno)); printf("failed to read file: %d", errno);
return false; return false;
} }
HLOGM("offset %" PRIx64 " len %zu buf %x %x %x %x", offset, len, buf[0], buf[1], buf[2], HLOGM("offset %" PRIx64 " len %zu buf %x %x %x %x", offset, len, buf[0], buf[1], buf[2],

View File

@ -41,15 +41,19 @@ bool PerfFileWriter::Open(const std::string &fileName, bool compressData)
if (access(fileName.c_str(), F_OK) == 0) { if (access(fileName.c_str(), F_OK) == 0) {
// file exists // file exists
if (remove(fileName.c_str()) != 0) { if (remove(fileName.c_str()) != 0) {
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("can't remove exist file(%s). %d:%s\n", fileName.c_str(), errno, printf("can't remove exist file(%s). %d:%s\n", fileName.c_str(), errno,
strerror(errno)); errInfo);
return false; return false;
} }
} }
std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str());
fp_ = fopen(fileName.c_str(), "web+"); fp_ = fopen(resolvedPath.c_str(), "web+");
if (fp_ == nullptr) { if (fp_ == nullptr) {
printf("can't create file(%s). %d:%s\n", fileName.c_str(), errno, strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("can't create file(%s). %d:%s\n", fileName.c_str(), errno, errInfo);
return false; return false;
} }
@ -90,15 +94,22 @@ bool PerfFileWriter::Close()
std::string gzName = fileName_ + ".gz"; std::string gzName = fileName_ + ".gz";
if (CompressFile(fileName_, gzName)) { if (CompressFile(fileName_, gzName)) {
if (remove(fileName_.c_str()) != 0) { if (remove(fileName_.c_str()) != 0) {
printf("can't remove file(%s). %d:%s\n", fileName_.c_str(), errno, strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("can't remove file(%s). %d:%s\n",
fileName_.c_str(), errno, errInfo);
} }
if (rename(gzName.c_str(), fileName_.c_str()) != 0) { if (rename(gzName.c_str(), fileName_.c_str()) != 0) {
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("can't rename file(%s) to (%s). %d:%s\n", gzName.c_str(), fileName_.c_str(), printf("can't rename file(%s) to (%s). %d:%s\n", gzName.c_str(), fileName_.c_str(),
errno, strerror(errno)); errno, errInfo);
} }
} else { } else {
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("failed to compress file(%s). %d:%s\n", fileName_.c_str(), errno, printf("failed to compress file(%s). %d:%s\n", fileName_.c_str(), errno,
strerror(errno)); errInfo);
} }
} }

View File

@ -378,7 +378,7 @@ void Report::OutputStdHead(ReportEventConfigItem &config, bool diffMode)
displayKeyNames_.insert(displayKeyNames_.begin(), "count"); displayKeyNames_.insert(displayKeyNames_.begin(), "count");
} }
int remainingWidth = consoleWidth_; unsigned int remainingWidth = consoleWidth_;
// sort key head // sort key head
for (auto &keyName : displayKeyNames_) { for (auto &keyName : displayKeyNames_) {
auto &key = reportKeyMap_.at(keyName); auto &key = reportKeyMap_.at(keyName);
@ -431,7 +431,7 @@ void Report::PrepareConsole()
#if is_mingw #if is_mingw
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
consoleWidth_ = static_cast<int>(csbi.srWindow.Right - csbi.srWindow.Left + 1); consoleWidth_ = static_cast<unsigned int>(csbi.srWindow.Right - csbi.srWindow.Left + 1);
const auto handle = GetStdHandle(STD_OUTPUT_HANDLE); const auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode; DWORD mode;
GetConsoleMode(handle, &mode); GetConsoleMode(handle, &mode);
@ -440,7 +440,7 @@ void Report::PrepareConsole()
#else #else
struct winsize w = {0, 0, 0, 0}; struct winsize w = {0, 0, 0, 0};
ioctl(fileno(stdout), TIOCGWINSZ, &w); ioctl(fileno(stdout), TIOCGWINSZ, &w);
consoleWidth_ = static_cast<int>(w.ws_col); consoleWidth_ = static_cast<unsigned int>(w.ws_col);
#endif #endif
if (consoleWidth_ == 0) { if (consoleWidth_ == 0) {
consoleWidth_ = ConsoleDefaultWidth; consoleWidth_ = ConsoleDefaultWidth;

View File

@ -37,7 +37,7 @@ void ReportJsonFile::ProcessSymbolsFiles(
{ {
auto symbolsFileIt = symbolsFiles.begin(); auto symbolsFileIt = symbolsFiles.begin();
while (symbolsFileIt != symbolsFiles.end()) { while (symbolsFileIt != symbolsFiles.end()) {
int libId = libList_.size(); size_t libId = libList_.size();
libList_.emplace_back(symbolsFileIt->get()->filePath_); libList_.emplace_back(symbolsFileIt->get()->filePath_);
const auto &symbols = symbolsFileIt->get()->GetSymbols(); const auto &symbols = symbolsFileIt->get()->GetSymbols();
auto symbolIt = symbols.begin(); auto symbolIt = symbols.begin();

View File

@ -15,6 +15,7 @@
#define HILOG_TAG "Protobuf" #define HILOG_TAG "Protobuf"
#include "report_protobuf_file.h" #include "report_protobuf_file.h"
#include "utilities.h"
using namespace Proto; using namespace Proto;
namespace OHOS { namespace OHOS {
@ -71,7 +72,8 @@ bool ReportProtobufFileWriter::Create(std::string fileName)
try { try {
protobufFileStream_->exceptions(std::ofstream::failbit | std::ofstream::badbit | protobufFileStream_->exceptions(std::ofstream::failbit | std::ofstream::badbit |
std::ofstream::eofbit); std::ofstream::eofbit);
protobufFileStream_->open(fileName_.c_str(), std::string resolvedPath = CanonicalizeSpecPath(fileName_.c_str());
protobufFileStream_->open(resolvedPath.c_str(),
std::fstream::out | std::fstream::trunc | std::fstream::binary); std::fstream::out | std::fstream::trunc | std::fstream::binary);
protpbufOutputStream_ = protpbufOutputStream_ =
std::make_unique<google::protobuf::io::CopyingOutputStreamAdaptor>(this); std::make_unique<google::protobuf::io::CopyingOutputStreamAdaptor>(this);
@ -250,7 +252,8 @@ bool ReportProtobufFileReader::Dump(std::string fileName, ProtobufReadBack readB
fileName_ = fileName; fileName_ = fileName;
try { try {
protobufFileStream_->exceptions(std::ifstream::failbit | std::ifstream::badbit); protobufFileStream_->exceptions(std::ifstream::failbit | std::ifstream::badbit);
protobufFileStream_->open(fileName_.c_str(), std::fstream::in | std::fstream::binary); std::string resolvedPath = CanonicalizeSpecPath(fileName_.c_str());
protobufFileStream_->open(resolvedPath.c_str(), std::fstream::in | std::fstream::binary);
printf("open proto buf file succeed.\n"); printf("open proto buf file succeed.\n");
if (!CheckFileMagic()) { if (!CheckFileMagic()) {
return false; return false;

View File

@ -355,14 +355,16 @@ void SubCommandDump::ExprotUserStack(const PerfRecordSample &recordSample)
std::string userRegs = std::string userRegs =
StringPrintf("hiperf_%d_%d_user_regs_%zu.dump", recordSample.data_.pid, StringPrintf("hiperf_%d_%d_user_regs_%zu.dump", recordSample.data_.pid,
recordSample.data_.tid, exportSampleIndex_); recordSample.data_.tid, exportSampleIndex_);
std::unique_ptr<FILE, decltype(&fclose)> fpUserRegs(fopen(userRegs.c_str(), "wb"), fclose); std::string resolvedPath = CanonicalizeSpecPath(userRegs.c_str());
std::unique_ptr<FILE, decltype(&fclose)> fpUserRegs(fopen(resolvedPath.c_str(), "wb"), fclose);
fwrite(recordSample.data_.user_regs, sizeof(u64), recordSample.data_.reg_nr, fwrite(recordSample.data_.user_regs, sizeof(u64), recordSample.data_.reg_nr,
fpUserRegs.get()); fpUserRegs.get());
std::string userData = std::string userData =
StringPrintf("hiperf_%d_%d_user_data_%zu.dump", recordSample.data_.pid, StringPrintf("hiperf_%d_%d_user_data_%zu.dump", recordSample.data_.pid,
recordSample.data_.tid, exportSampleIndex_); recordSample.data_.tid, exportSampleIndex_);
std::unique_ptr<FILE, decltype(&fclose)> fpUserData(fopen(userData.c_str(), "wb"), fclose); std::string resolvePath = CanonicalizeSpecPath(userData.c_str());
std::unique_ptr<FILE, decltype(&fclose)> fpUserData(fopen(resolvePath.c_str(), "wb"), fclose);
fwrite(recordSample.data_.stack_data, sizeof(u8), recordSample.data_.dyn_size, fwrite(recordSample.data_.stack_data, sizeof(u8), recordSample.data_.dyn_size,
fpUserData.get()); fpUserData.get());
} }
@ -380,7 +382,8 @@ void SubCommandDump::ExprotUserData(std::unique_ptr<PerfEventRecord> &record)
std::string userData = std::string userData =
StringPrintf("hiperf_%d_%d_sample_record_%zu_%" PRIu64 ".dump", recordSample->data_.pid, StringPrintf("hiperf_%d_%d_sample_record_%zu_%" PRIu64 ".dump", recordSample->data_.pid,
recordSample->data_.tid, exportSampleIndex_, recordSample->data_.time); recordSample->data_.tid, exportSampleIndex_, recordSample->data_.time);
std::unique_ptr<FILE, decltype(&fclose)> fpUserData(fopen(userData.c_str(), "wb"), fclose); std::string resolvedPath = CanonicalizeSpecPath(userData.c_str());
std::unique_ptr<FILE, decltype(&fclose)> fpUserData(fopen(resolvedPath.c_str(), "wb"), fclose);
std::vector<u8> buf(RECORD_SIZE_LIMIT); std::vector<u8> buf(RECORD_SIZE_LIMIT);
if (!recordSample->GetBinary(buf)) { if (!recordSample->GetBinary(buf)) {
HLOGE("export user sample data failed"); HLOGE("export user sample data failed");

View File

@ -751,16 +751,20 @@ bool SubCommandRecord::ClientCommandResponse(bool OK)
if (OK) { if (OK) {
size_t size = write(clientPipeOutput_, ReplyOK.c_str(), ReplyOK.size()); size_t size = write(clientPipeOutput_, ReplyOK.c_str(), ReplyOK.size());
if (size != 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, HLOGD("Server:%s -> %d : %zd %d:%s", ReplyOK.c_str(), clientPipeOutput_, size, errno,
strerror(errno)); errInfo);
return false; return false;
} }
return true; return true;
} else { } else {
size_t size = write(clientPipeOutput_, ReplyFAIL.c_str(), ReplyFAIL.size()); size_t size = write(clientPipeOutput_, ReplyFAIL.c_str(), ReplyFAIL.size());
if (size != 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, HLOGD("Server:%s -> %d : %zd %d:%s", ReplyFAIL.c_str(), clientPipeOutput_, size, errno,
strerror(errno)); errInfo);
return false; return false;
} }
return true; return true;
@ -889,20 +893,26 @@ bool SubCommandRecord::CreateFifoServer()
remove(CONTROL_FIFO_FILE_S2C.c_str()); remove(CONTROL_FIFO_FILE_S2C.c_str());
remove(CONTROL_FIFO_FILE_C2S.c_str()); remove(CONTROL_FIFO_FILE_C2S.c_str());
} }
HLOGE("create fifo file failed. %d:%s", errno, strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HLOGE("create fifo file failed. %d:%s", errno, errInfo);
return false; return false;
} }
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) { if (pid == -1) {
HLOGE("fork failed. %d:%s", errno, strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HLOGE("fork failed. %d:%s", errno, errInfo);
return false; return false;
} else if (pid == 0) { // child process } else if (pid == 0) { // child process
isFifoServer_ = true; isFifoServer_ = true;
clientPipeOutput_ = open(CONTROL_FIFO_FILE_S2C.c_str(), O_WRONLY); clientPipeOutput_ = open(CONTROL_FIFO_FILE_S2C.c_str(), O_WRONLY);
if (clientPipeOutput_ == -1) { if (clientPipeOutput_ == -1) {
char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
HLOGE("open fifo file(%s) failed. %d:%s", CONTROL_FIFO_FILE_S2C.c_str(), errno, HLOGE("open fifo file(%s) failed. %d:%s", CONTROL_FIFO_FILE_S2C.c_str(), errno,
strerror(errno)); errInfo);
return false; return false;
} }
fclose(stdout); // for XTS, because popen in CmdRun fclose(stdout); // for XTS, because popen in CmdRun
@ -914,7 +924,9 @@ bool SubCommandRecord::CreateFifoServer()
kill(pid, SIGINT); kill(pid, SIGINT);
remove(CONTROL_FIFO_FILE_C2S.c_str()); remove(CONTROL_FIFO_FILE_C2S.c_str());
remove(CONTROL_FIFO_FILE_S2C.c_str()); remove(CONTROL_FIFO_FILE_S2C.c_str());
printf("create control hiperf sampling failed. %d:%s\n", errno, strerror(errno)); char errInfo[ERRINFOLEN] = { 0 };
strerror_r(errno, errInfo, ERRINFOLEN);
printf("create control hiperf sampling failed. %d:%s\n", errno, errInfo);
return false; return false;
} }
close(fd); close(fd);

View File

@ -537,10 +537,10 @@ bool SubCommandReport::PrepareOutput()
} }
if (!reportFile_.empty()) { if (!reportFile_.empty()) {
output_ = fopen(reportFile_.c_str(), "w"); std::string resolvedPath = CanonicalizeSpecPath(reportFile_.c_str());
output_ = fopen(resolvedPath.c_str(), "w");
if (output_ == nullptr) { if (output_ == nullptr) {
printf("unable open file to '%s' because '%d:%s'\n", reportFile_.c_str(), errno, printf("unable open file to '%s' because '%d'\n", reportFile_.c_str(), errno);
strerror(errno));
return false; return false;
} else { } else {
printf("report will save at '%s'\n", reportFile_.c_str()); printf("report will save at '%s'\n", reportFile_.c_str());

View File

@ -502,7 +502,8 @@ private:
{ {
#ifndef HIPERF_ELF_READ_USE_MMAP #ifndef HIPERF_ELF_READ_USE_MMAP
if (readFd_ == nullptr) { if (readFd_ == nullptr) {
FILE *fp = fopen(loadElfPath.c_str(), "rb"); std::string resolvedPath = CanonicalizeSpecPath(loadElfPath.c_str());
FILE *fp = fopen(resolvedPath.c_str(), "rb");
if (fp == nullptr) { if (fp == nullptr) {
return; return;
} }
@ -515,9 +516,11 @@ private:
return; return;
} }
#if is_mingw #if is_mingw
fd_ = OHOS::UniqueFd(open(loadElfPath.c_str(), O_RDONLY | O_BINARY)); std::string resolvedPath = CanonicalizeSpecPath(loadElfPath.c_str());
fd_ = OHOS::UniqueFd(open(resolvedPath.c_str(), O_RDONLY | O_BINARY));
#else #else
fd_ = OHOS::UniqueFd(open(loadElfPath.c_str(), O_RDONLY)); std::string resolvedPath = CanonicalizeSpecPath(loadElfPath.c_str());
fd_ = OHOS::UniqueFd(open(resolvedPath.c_str(), O_RDONLY));
#endif #endif
if (fd_ != -1) { if (fd_ != -1) {
struct stat sb = {}; struct stat sb = {};
@ -542,7 +545,7 @@ private:
} }
} }
} else { } else {
HLOGD("elf file open failed with %s by %s", loadElfPath.c_str(), strerror(errno)); HLOGD("elf file open failed with %s by %d", loadElfPath.c_str(), errno);
return; return;
} }
#endif #endif

View File

@ -22,6 +22,24 @@
namespace OHOS { namespace OHOS {
namespace Developtools { namespace Developtools {
namespace HiPerf { namespace HiPerf {
std::string CanonicalizeSpecPath(const char* src)
{
char resolvedPath[PATH_MAX] = { 0 };
#if defined(_WIN32)
if (!_fullpath(resolvedPath, src, PATH_MAX)) {
fprintf(stderr, "Error: _fullpath %s failed", src);
return "";
}
#else
if (realpath(src, resolvedPath) == nullptr) {
fprintf(stderr, "Error: _fullpath %s failed", src);
return "";
}
#endif
std::string res(resolvedPath);
return res;
}
uint32_t RoundUp(uint32_t x, const int align) uint32_t RoundUp(uint32_t x, const int align)
{ {
return (((x) + (align)-1) / (align)) * (align); return (((x) + (align)-1) / (align)) * (align);
@ -81,7 +99,8 @@ std::vector<std::string> StringSplit(std::string source, std::string split)
StdoutRecord::StdoutRecord(const std::string &tempFile, const std::string &mode) StdoutRecord::StdoutRecord(const std::string &tempFile, const std::string &mode)
{ {
if (!tempFile.empty()) { if (!tempFile.empty()) {
recordFile_ = fopen(tempFile.c_str(), mode.c_str()); std::string resolvedPath = CanonicalizeSpecPath(tempFile.c_str());
recordFile_ = fopen(resolvedPath.c_str(), mode.c_str());
if (recordFile_ == nullptr) { if (recordFile_ == nullptr) {
HLOGE("tmpfile create failed '%s' with mode '%s'", tempFile.c_str(), mode.c_str()); HLOGE("tmpfile create failed '%s' with mode '%s'", tempFile.c_str(), mode.c_str());
} else { } else {
@ -102,7 +121,8 @@ bool StdoutRecord::Start()
if (recordFile_ == nullptr) { if (recordFile_ == nullptr) {
// try second way // try second way
std::string fileName = "temp.stdout"; std::string fileName = "temp.stdout";
recordFile_ = fopen(fileName.c_str(), "w+"); std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str());
recordFile_ = fopen(resolvedPath.c_str(), "w+");
if (recordFile_ == nullptr) { if (recordFile_ == nullptr) {
HLOGF("tmpfile create failed '%s'", fileName.c_str()); HLOGF("tmpfile create failed '%s'", fileName.c_str());
return false; return false;
@ -236,7 +256,8 @@ std::string ReadFileToString(const std::string &fileName)
bool ReadFileToString(const std::string &fileName, std::string &fileData, size_t fileSize) bool ReadFileToString(const std::string &fileName, std::string &fileData, size_t fileSize)
{ {
fileData.clear(); fileData.clear();
OHOS::UniqueFd fd(open(fileName.c_str(), O_RDONLY | O_BINARY)); std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str());
OHOS::UniqueFd fd(open(resolvedPath.c_str(), O_RDONLY | O_BINARY));
if (fileSize == 0) { if (fileSize == 0) {
struct stat fileStat; struct stat fileStat;
if (fstat(fd.Get(), &fileStat) != -1 && fileStat.st_size > 0) { if (fstat(fd.Get(), &fileStat) != -1 && fileStat.st_size > 0) {
@ -275,7 +296,7 @@ bool IsRoot()
#endif #endif
} }
bool PowerOfTwo(int n) bool PowerOfTwo(uint64_t n)
{ {
return n && (!(n & (n - 1))); return n && (!(n & (n - 1)));
} }
@ -300,7 +321,8 @@ bool WriteIntToProcFile(const std::string &path, int value)
// compress specified dataFile into gzip file // compress specified dataFile into gzip file
bool CompressFile(const std::string &dataFile, const std::string &destFile) bool CompressFile(const std::string &dataFile, const std::string &destFile)
{ {
FILE *fp = fopen(dataFile.c_str(), "rb"); std::string resolvedPath = CanonicalizeSpecPath(dataFile.c_str());
FILE *fp = fopen(resolvedPath.c_str(), "rb");
if (fp == nullptr) { if (fp == nullptr) {
HLOGE("Fail to open data file %s", dataFile.c_str()); HLOGE("Fail to open data file %s", dataFile.c_str());
perror("Fail to fopen(rb)"); perror("Fail to fopen(rb)");
@ -339,7 +361,8 @@ bool CompressFile(const std::string &dataFile, const std::string &destFile)
// uncompress specified gzip file into dataFile // uncompress specified gzip file into dataFile
bool UncompressFile(const std::string &gzipFile, const std::string &dataFile) bool UncompressFile(const std::string &gzipFile, const std::string &dataFile)
{ {
FILE *fp = fopen(dataFile.c_str(), "wb"); std::string resolvedPath = CanonicalizeSpecPath(dataFile.c_str());
FILE *fp = fopen(resolvedPath.c_str(), "wb");
if (fp == nullptr) { if (fp == nullptr) {
HLOGE("Fail to open data file %s", dataFile.c_str()); HLOGE("Fail to open data file %s", dataFile.c_str());
perror("Fail to fopen(rb)"); perror("Fail to fopen(rb)");

View File

@ -281,7 +281,10 @@ HWTEST_F(SymbolsFileTest, LoadKernelSymbols, TestSize.Level1)
} }
std::string modulesMap = ReadFileToString("/proc/modules"); std::string modulesMap = ReadFileToString("/proc/modules");
size_t lines = std::count(modulesMap.begin(), modulesMap.end(), '\n'); int lines = std::count(modulesMap.begin(), modulesMap.end(), '\n');
if (lines < 0) {
return;
}
std::set<std::string> modulesCount; std::set<std::string> modulesCount;
for (auto &symbol : symbols) { for (auto &symbol : symbols) {
if (symbol.module_.length()) { if (symbol.module_.length()) {
@ -674,8 +677,8 @@ HWTEST_F(SymbolsFileTest, ReadRoMemory, TestSize.Level1)
std::unique_ptr<SymbolsFile> symbolsFile = std::unique_ptr<SymbolsFile> symbolsFile =
SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, TEST_FILE_ELF_FULL_PATH); SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, TEST_FILE_ELF_FULL_PATH);
std::string resolvedPath = CanonicalizeSpecPath(TEST_FILE_ELF_FULL_PATH.c_str());
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(TEST_FILE_ELF_FULL_PATH.c_str(), "rb"), std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(resolvedPath.c_str(), "rb"),
fclose); fclose);
ASSERT_NE(symbolsFile, nullptr); ASSERT_NE(symbolsFile, nullptr);
@ -892,7 +895,7 @@ HWTEST_F(SymbolsFileTest, CreateSymbolsFile, TestSize.Level1)
HWTEST_F(SymbolsFileTest, LoadSymbolsFromSaved, TestSize.Level1) HWTEST_F(SymbolsFileTest, LoadSymbolsFromSaved, TestSize.Level1)
{ {
SymbolFileStruct sfs; SymbolFileStruct sfs;
for (int type = 0; type < SYMBOL_UNKNOW_FILE; type++) { for (unsigned int type = 0; type < SYMBOL_UNKNOW_FILE; type++) {
sfs.filePath_ = std::to_string(rnd_()); sfs.filePath_ = std::to_string(rnd_());
sfs.symbolType_ = type; sfs.symbolType_ = type;
sfs.textExecVaddrFileOffset_ = rnd_(); sfs.textExecVaddrFileOffset_ = rnd_();