mirror of
https://gitee.com/openharmony/developtools_hiperf
synced 2024-11-27 01:30:28 +00:00
fix hiperf codecheck issue
Signed-off-by: wenlong12 <wenlong12@huawei.com> Signed-off-by: wenlong12 <wenlong12@huawei.com>
This commit is contained in:
parent
97cb157d7a
commit
4f8cc24d12
@ -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) {
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<std::string> &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<std::string> &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
|
||||
|
@ -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);
|
||||
|
@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <elf_parser.h>
|
||||
#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;
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -33,7 +33,8 @@ const int SIZE_FETURE_COUNT = 8;
|
||||
|
||||
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) {
|
||||
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],
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<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);
|
||||
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<int>(w.ws_col);
|
||||
consoleWidth_ = static_cast<unsigned int>(w.ws_col);
|
||||
#endif
|
||||
if (consoleWidth_ == 0) {
|
||||
consoleWidth_ = ConsoleDefaultWidth;
|
||||
|
@ -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();
|
||||
|
@ -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<google::protobuf::io::CopyingOutputStreamAdaptor>(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;
|
||||
|
@ -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<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,
|
||||
fpUserRegs.get());
|
||||
|
||||
std::string userData =
|
||||
StringPrintf("hiperf_%d_%d_user_data_%zu.dump", recordSample.data_.pid,
|
||||
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,
|
||||
fpUserData.get());
|
||||
}
|
||||
@ -380,7 +382,8 @@ void SubCommandDump::ExprotUserData(std::unique_ptr<PerfEventRecord> &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<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);
|
||||
if (!recordSample->GetBinary(buf)) {
|
||||
HLOGE("export user sample data failed");
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
|
@ -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
|
||||
|
@ -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<std::string> 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)");
|
||||
|
@ -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<std::string> modulesCount;
|
||||
for (auto &symbol : symbols) {
|
||||
if (symbol.module_.length()) {
|
||||
@ -674,8 +677,8 @@ HWTEST_F(SymbolsFileTest, ReadRoMemory, TestSize.Level1)
|
||||
|
||||
std::unique_ptr<SymbolsFile> symbolsFile =
|
||||
SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, TEST_FILE_ELF_FULL_PATH);
|
||||
|
||||
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(TEST_FILE_ELF_FULL_PATH.c_str(), "rb"),
|
||||
std::string resolvedPath = CanonicalizeSpecPath(TEST_FILE_ELF_FULL_PATH.c_str());
|
||||
std::unique_ptr<FILE, decltype(&fclose)> 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_();
|
||||
|
Loading…
Reference in New Issue
Block a user