diff --git a/demo/cpp/hiperf_example_cmd.cpp b/demo/cpp/hiperf_example_cmd.cpp index 31293245ae6be95ec63135de185e2f436005b835..0183be4f7f54a282a87f4a992c1761d93fb37616 100755 --- a/demo/cpp/hiperf_example_cmd.cpp +++ b/demo/cpp/hiperf_example_cmd.cpp @@ -478,7 +478,9 @@ USED_FUNCTION int main(int argc, char *argv[]) CPU_ZERO(&mask); CPU_SET(option.boundCpu, &mask); 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) { diff --git a/include/debug_logger.h b/include/debug_logger.h index 51e6acc59ea9e17894c2ef74309643b99fe3d3e8..d5b803f4c2e3f6c2cc59e2727bc4979844242561 100755 --- a/include/debug_logger.h +++ b/include/debug_logger.h @@ -159,8 +159,8 @@ private: #define perror(format, ...) \ do { \ std::perror(format); \ - DebugLogger::GetInstance()->Log(LEVEL_STDOUT, HILOG_TAG, format "<%d:%s>\n", \ - ##__VA_ARGS__, errno, strerror(errno)); \ + DebugLogger::GetInstance()->Log(LEVEL_STDOUT, HILOG_TAG, format "<%d>\n", \ + ##__VA_ARGS__, errno); \ } while (0) #endif #endif @@ -263,7 +263,7 @@ private: #ifndef HLOGEP #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 #ifndef HLOGF diff --git a/include/utilities.h b/include/utilities.h index 69fc4b1ddc6a2b15d3ff4a392b41311b7e9f65a1..746cdcfe571451b8614828bb704dd06433cdf82d 100755 --- a/include/utilities.h +++ b/include/utilities.h @@ -87,7 +87,9 @@ constexpr uint64_t KILO = 1024; namespace OHOS { namespace Developtools { namespace HiPerf { +std::string CanonicalizeSpecPath(const char* src); const std::string EMPTY_STRING = ""; +const ssize_t ERRINFOLEN = 512; // string function class MemoryHold { @@ -103,6 +105,9 @@ public: } // for null end char *p = new char[view.size() + 1]; + if (p == nullptr) { + return ""; + } p[view.size()] = '\0'; std::copy(view.data(), view.data() + view.size(), p); holder_.emplace_back(p); @@ -293,7 +298,7 @@ float Percentage(const T &a, const T &b) } bool IsRoot(); -bool PowerOfTwo(int n); +bool PowerOfTwo(uint64_t n); #define INDENT_ONE_LEVEL (indent + 1) #define INDENT_TWO_LEVEL (indent + 2) diff --git a/interfaces/innerkits/native/include/hiperf_client.h b/interfaces/innerkits/native/include/hiperf_client.h index 569dc5de7174f3d84436bf308cf8d5fa60ac9fa5..99f16726dafc15de7dafcd21577d36f26cebc8c9 100644 --- a/interfaces/innerkits/native/include/hiperf_client.h +++ b/interfaces/innerkits/native/include/hiperf_client.h @@ -294,8 +294,8 @@ public: void EnableHilog(); private: - static const int PIPE_READ = 0; - static const int PIPE_WRITE = 1; + static const uint64_t PIPE_READ = 0; + static const uint64_t PIPE_WRITE = 1; static constexpr size_t SIZE_ARGV_TAIL = 1; // nullptr static constexpr int64_t THOUSAND = 1000; diff --git a/interfaces/innerkits/native/src/hiperf_client.cpp b/interfaces/innerkits/native/src/hiperf_client.cpp index 32773cf5f73b0ee6314ad8b07ed4c3445ea6faad..7d8792c9509caf5386fc51553e9ac85e30c93ce9 100644 --- a/interfaces/innerkits/native/src/hiperf_client.cpp +++ b/interfaces/innerkits/native/src/hiperf_client.cpp @@ -35,6 +35,7 @@ namespace OHOS { namespace Developtools { namespace HiPerf { namespace HiperfClient { +const ssize_t ERRINFOLEN = 512; void RecordOption::SetOption(const std::string &name, bool enable) { auto it = std::find(args_.begin(), args_.end(), name); @@ -384,13 +385,17 @@ bool Client::Start(const std::vector &args) int clientToServerFd[2]; int serverToClientFd[2]; 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; } hperfPid_ = fork(); 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; } else if (hperfPid_ == 0) { // child process @@ -411,9 +416,11 @@ bool Client::Start(const std::vector &args) argv[i] = nullptr; execv(argv[0], argv); + char errInfo[ERRINFOLEN] = { 0 }; + strerror_r(errno, errInfo, ERRINFOLEN); HIPERF_HILOGD(MODULE_CPP_API, "failed to call exec: '%" HILOG_PUBLIC "s' %" HILOG_PUBLIC "s\n", - executeCommandPath_.c_str(), strerror(errno)); + executeCommandPath_.c_str(), errInfo); exit(0); } else { // parent process diff --git a/src/debug_logger.cpp b/src/debug_logger.cpp index 4a844a2bb3e3bbdbff381f7ec2af050d053df4a8..1393bebdcbb1e74dba1d2e345ebabf53ada746f6 100755 --- a/src/debug_logger.cpp +++ b/src/debug_logger.cpp @@ -255,17 +255,18 @@ bool DebugLogger::OpenLog(const std::string &tempLogPath, const std::string &fla } if (!tempLogPath.empty()) { 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) { // already open return true; } else { - file_ = fopen(logPath_.c_str(), "w"); + std::string resolvedPath = CanonicalizeSpecPath(logPath_.c_str()); + file_ = fopen(resolvedPath.c_str(), "w"); } if (file_ == nullptr) { - fprintf(stdout, "unable save log file to '%s' because '%d:%s'\n", logPath_.c_str(), errno, - strerror(errno)); + fprintf(stdout, "unable save log file to '%s' because '%d'\n", logPath_.c_str(), errno); return false; } else { fseek(file_, 0, SEEK_SET); diff --git a/src/elf_file.cpp b/src/elf_file.cpp index 3838f4404e9042c402cc6d5f6239c1b92831114c..065572e3d25f274927d1ec5ffcb08f2d0fa609bc 100755 --- a/src/elf_file.cpp +++ b/src/elf_file.cpp @@ -14,6 +14,7 @@ */ #include +#include "utilities.h" using namespace OHOS::Developtools::HiPerf::ELF; namespace OHOS { @@ -22,9 +23,11 @@ namespace HiPerf { ElfFile::ElfFile(const std::string &filename) { #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 - fd_ = open(filename.c_str(), O_RDONLY); + std::string resolvedPath = CanonicalizeSpecPath(filename.c_str()); + fd_ = open(resolvedPath.c_str(), O_RDONLY); #endif if (fd_ != -1) { struct stat sb; diff --git a/src/perf_events.cpp b/src/perf_events.cpp index d389807a2e3fdeffdc1a88c0d3789124fff64393..ecf8a8784ac6b55af62cbb3c16d2c33273dd04da 100755 --- a/src/perf_events.cpp +++ b/src/perf_events.cpp @@ -1023,11 +1023,15 @@ bool PerfEvents::CreateFdEvents(void) } else { // clang-format off 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", - 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", - eventItem.configName.c_str(), cpus_[icpu], errno, strerror(errno)); + eventItem.configName.c_str(), cpus_[icpu], errno, errInfo); // clang-format on break; // jump to next cpu } diff --git a/src/perf_file_reader.cpp b/src/perf_file_reader.cpp index 70bdb451c3bc424e0fd691ff3efbeafa8f75495f..d630fb9055b24377f830bf9504b9c68d5240e3dd 100755 --- a/src/perf_file_reader.cpp +++ b/src/perf_file_reader.cpp @@ -33,7 +33,8 @@ const int SIZE_FETURE_COUNT = 8; std::unique_ptr 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) { HLOGE("fail to open file %s", fileName.c_str()); return nullptr; @@ -78,6 +79,7 @@ end: PerfFileReader::PerfFileReader(const std::string &fileName, FILE *fp) : fp_(fp), fileName_(fileName) { + featureSectionOffset_ = 0; struct stat fileStat; if (fp != nullptr) { 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) { - printf("failed to read file: %s", strerror(errno)); + printf("failed to read file: %d", errno); return false; } return true; @@ -342,7 +344,7 @@ bool PerfFileReader::Read(char *buf, uint64_t offset, size_t len) } if (fread(buf, len, 1, fp_) != 1) { - printf("failed to read file: %s", strerror(errno)); + printf("failed to read file: %d", errno); return false; } HLOGM("offset %" PRIx64 " len %zu buf %x %x %x %x", offset, len, buf[0], buf[1], buf[2], diff --git a/src/perf_file_writer.cpp b/src/perf_file_writer.cpp index 95f44ad41a16c6cc4bfa286a5e6e82fff3a77d81..d68272b377e6787f8428073c01b62907d18fdca6 100755 --- a/src/perf_file_writer.cpp +++ b/src/perf_file_writer.cpp @@ -41,15 +41,19 @@ bool PerfFileWriter::Open(const std::string &fileName, bool compressData) if (access(fileName.c_str(), F_OK) == 0) { // file exists 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, - strerror(errno)); + errInfo); return false; } } - - fp_ = fopen(fileName.c_str(), "web+"); + std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str()); + fp_ = fopen(resolvedPath.c_str(), "web+"); 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; } @@ -90,15 +94,22 @@ bool PerfFileWriter::Close() std::string gzName = fileName_ + ".gz"; if (CompressFile(fileName_, gzName)) { 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) { + 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(), - errno, strerror(errno)); + errno, errInfo); } } else { + char errInfo[ERRINFOLEN] = { 0 }; + strerror_r(errno, errInfo, ERRINFOLEN); printf("failed to compress file(%s). %d:%s\n", fileName_.c_str(), errno, - strerror(errno)); + errInfo); } } diff --git a/src/report.cpp b/src/report.cpp index 32b7dbafcc2b995f632c1e28291aea36eabe20bd..f6bdb34d452de55069d8e736784f41339b35c1e3 100755 --- a/src/report.cpp +++ b/src/report.cpp @@ -378,7 +378,7 @@ void Report::OutputStdHead(ReportEventConfigItem &config, bool diffMode) displayKeyNames_.insert(displayKeyNames_.begin(), "count"); } - int remainingWidth = consoleWidth_; + unsigned int remainingWidth = consoleWidth_; // sort key head for (auto &keyName : displayKeyNames_) { auto &key = reportKeyMap_.at(keyName); @@ -431,7 +431,7 @@ void Report::PrepareConsole() #if is_mingw CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); - consoleWidth_ = static_cast(csbi.srWindow.Right - csbi.srWindow.Left + 1); + consoleWidth_ = static_cast(csbi.srWindow.Right - csbi.srWindow.Left + 1); const auto handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD mode; GetConsoleMode(handle, &mode); @@ -440,7 +440,7 @@ void Report::PrepareConsole() #else struct winsize w = {0, 0, 0, 0}; ioctl(fileno(stdout), TIOCGWINSZ, &w); - consoleWidth_ = static_cast(w.ws_col); + consoleWidth_ = static_cast(w.ws_col); #endif if (consoleWidth_ == 0) { consoleWidth_ = ConsoleDefaultWidth; diff --git a/src/report_json_file.cpp b/src/report_json_file.cpp index 938c1350bdbf168bb879c1a21a79b8f25678b173..a494070d83c8c33d75b6c0d9334f4c3b9f924020 100755 --- a/src/report_json_file.cpp +++ b/src/report_json_file.cpp @@ -37,7 +37,7 @@ void ReportJsonFile::ProcessSymbolsFiles( { auto symbolsFileIt = symbolsFiles.begin(); while (symbolsFileIt != symbolsFiles.end()) { - int libId = libList_.size(); + size_t libId = libList_.size(); libList_.emplace_back(symbolsFileIt->get()->filePath_); const auto &symbols = symbolsFileIt->get()->GetSymbols(); auto symbolIt = symbols.begin(); diff --git a/src/report_protobuf_file.cpp b/src/report_protobuf_file.cpp index 2a002abfab40f542c7e4f09752b06aeb1f0311c9..75c6e3fa1c4103379f221a7787cca63169ab2c2f 100755 --- a/src/report_protobuf_file.cpp +++ b/src/report_protobuf_file.cpp @@ -15,6 +15,7 @@ #define HILOG_TAG "Protobuf" #include "report_protobuf_file.h" +#include "utilities.h" using namespace Proto; namespace OHOS { @@ -71,7 +72,8 @@ bool ReportProtobufFileWriter::Create(std::string fileName) try { protobufFileStream_->exceptions(std::ofstream::failbit | std::ofstream::badbit | 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); protpbufOutputStream_ = std::make_unique(this); @@ -250,7 +252,8 @@ bool ReportProtobufFileReader::Dump(std::string fileName, ProtobufReadBack readB fileName_ = fileName; try { 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"); if (!CheckFileMagic()) { return false; diff --git a/src/subcommand_dump.cpp b/src/subcommand_dump.cpp index cae2b0c60a5db7e2d964307b3640c7c093225816..119c7aa4676129703e01d5f771f041f7eb9fc0d4 100644 --- a/src/subcommand_dump.cpp +++ b/src/subcommand_dump.cpp @@ -355,14 +355,16 @@ void SubCommandDump::ExprotUserStack(const PerfRecordSample &recordSample) std::string userRegs = StringPrintf("hiperf_%d_%d_user_regs_%zu.dump", recordSample.data_.pid, recordSample.data_.tid, exportSampleIndex_); - std::unique_ptr fpUserRegs(fopen(userRegs.c_str(), "wb"), fclose); + std::string resolvedPath = CanonicalizeSpecPath(userRegs.c_str()); + std::unique_ptr fpUserRegs(fopen(resolvedPath.c_str(), "wb"), fclose); fwrite(recordSample.data_.user_regs, sizeof(u64), recordSample.data_.reg_nr, fpUserRegs.get()); std::string userData = StringPrintf("hiperf_%d_%d_user_data_%zu.dump", recordSample.data_.pid, recordSample.data_.tid, exportSampleIndex_); - std::unique_ptr fpUserData(fopen(userData.c_str(), "wb"), fclose); + std::string resolvePath = CanonicalizeSpecPath(userData.c_str()); + std::unique_ptr fpUserData(fopen(resolvePath.c_str(), "wb"), fclose); fwrite(recordSample.data_.stack_data, sizeof(u8), recordSample.data_.dyn_size, fpUserData.get()); } @@ -380,7 +382,8 @@ void SubCommandDump::ExprotUserData(std::unique_ptr &record) std::string userData = StringPrintf("hiperf_%d_%d_sample_record_%zu_%" PRIu64 ".dump", recordSample->data_.pid, recordSample->data_.tid, exportSampleIndex_, recordSample->data_.time); - std::unique_ptr fpUserData(fopen(userData.c_str(), "wb"), fclose); + std::string resolvedPath = CanonicalizeSpecPath(userData.c_str()); + std::unique_ptr fpUserData(fopen(resolvedPath.c_str(), "wb"), fclose); std::vector buf(RECORD_SIZE_LIMIT); if (!recordSample->GetBinary(buf)) { HLOGE("export user sample data failed"); diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index d9cbbb6a0fbd98046162e0c143289c053b56d7a4..2a7c5e1db6ec3188965fb01edc3b9b5184be69c1 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -751,16 +751,20 @@ bool SubCommandRecord::ClientCommandResponse(bool OK) 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, - strerror(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, - strerror(errno)); + errInfo); return false; } return true; @@ -889,20 +893,26 @@ bool SubCommandRecord::CreateFifoServer() remove(CONTROL_FIFO_FILE_S2C.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; } pid_t pid = fork(); 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; } else if (pid == 0) { // child process isFifoServer_ = true; clientPipeOutput_ = open(CONTROL_FIFO_FILE_S2C.c_str(), O_WRONLY); 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, - strerror(errno)); + errInfo); return false; } fclose(stdout); // for XTS, because popen in CmdRun @@ -914,7 +924,9 @@ bool SubCommandRecord::CreateFifoServer() kill(pid, SIGINT); remove(CONTROL_FIFO_FILE_C2S.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; } close(fd); diff --git a/src/subcommand_report.cpp b/src/subcommand_report.cpp index d1ed978a9cb261fe5de852d33d9d3bd00ba7a1d5..9b7b2d03279a49a06977af4bd5d4b54416696701 100644 --- a/src/subcommand_report.cpp +++ b/src/subcommand_report.cpp @@ -537,10 +537,10 @@ bool SubCommandReport::PrepareOutput() } 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) { - printf("unable open file to '%s' because '%d:%s'\n", reportFile_.c_str(), errno, - strerror(errno)); + printf("unable open file to '%s' because '%d'\n", reportFile_.c_str(), errno); return false; } else { printf("report will save at '%s'\n", reportFile_.c_str()); diff --git a/src/symbols_file.cpp b/src/symbols_file.cpp index 64b63e0b7843afbb0d680b258ef1cef9f12c1c96..833fb8312efb08894e83afa1bfffffafdce548a1 100755 --- a/src/symbols_file.cpp +++ b/src/symbols_file.cpp @@ -502,7 +502,8 @@ private: { #ifndef HIPERF_ELF_READ_USE_MMAP 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) { return; } @@ -515,9 +516,11 @@ private: return; } #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 - 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 if (fd_ != -1) { struct stat sb = {}; @@ -542,7 +545,7 @@ private: } } } 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; } #endif diff --git a/src/utilities.cpp b/src/utilities.cpp index 6227bdfc124a0df3ec8fad2197bd45f7c18317fa..8d377f6e34f19c7c389683460f7cd28b07076c61 100755 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -22,6 +22,24 @@ namespace OHOS { namespace Developtools { 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) { return (((x) + (align)-1) / (align)) * (align); @@ -81,7 +99,8 @@ std::vector StringSplit(std::string source, std::string split) StdoutRecord::StdoutRecord(const std::string &tempFile, const std::string &mode) { 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) { HLOGE("tmpfile create failed '%s' with mode '%s'", tempFile.c_str(), mode.c_str()); } else { @@ -102,7 +121,8 @@ bool StdoutRecord::Start() if (recordFile_ == nullptr) { // try second way 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) { HLOGF("tmpfile create failed '%s'", fileName.c_str()); 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) { 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) { struct stat fileStat; if (fstat(fd.Get(), &fileStat) != -1 && fileStat.st_size > 0) { @@ -275,7 +296,7 @@ bool IsRoot() #endif } -bool PowerOfTwo(int n) +bool PowerOfTwo(uint64_t n) { return n && (!(n & (n - 1))); } @@ -300,7 +321,8 @@ bool WriteIntToProcFile(const std::string &path, int value) // compress specified dataFile into gzip file 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) { HLOGE("Fail to open data file %s", dataFile.c_str()); 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 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) { HLOGE("Fail to open data file %s", dataFile.c_str()); perror("Fail to fopen(rb)"); diff --git a/test/unittest/common/native/symbols_file_test.cpp b/test/unittest/common/native/symbols_file_test.cpp index 7f1c41891b05699269b2c26fdeb5940dc6580c3d..50c19e2651ad3ee251bb9d04610d88e102623439 100755 --- a/test/unittest/common/native/symbols_file_test.cpp +++ b/test/unittest/common/native/symbols_file_test.cpp @@ -281,7 +281,10 @@ HWTEST_F(SymbolsFileTest, LoadKernelSymbols, TestSize.Level1) } 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 modulesCount; for (auto &symbol : symbols) { if (symbol.module_.length()) { @@ -674,8 +677,8 @@ HWTEST_F(SymbolsFileTest, ReadRoMemory, TestSize.Level1) std::unique_ptr symbolsFile = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, TEST_FILE_ELF_FULL_PATH); - - std::unique_ptr fp(fopen(TEST_FILE_ELF_FULL_PATH.c_str(), "rb"), + std::string resolvedPath = CanonicalizeSpecPath(TEST_FILE_ELF_FULL_PATH.c_str()); + std::unique_ptr fp(fopen(resolvedPath.c_str(), "rb"), fclose); ASSERT_NE(symbolsFile, nullptr); @@ -892,7 +895,7 @@ HWTEST_F(SymbolsFileTest, CreateSymbolsFile, TestSize.Level1) HWTEST_F(SymbolsFileTest, LoadSymbolsFromSaved, TestSize.Level1) { 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.symbolType_ = type; sfs.textExecVaddrFileOffset_ = rnd_();