!26 fix hiperf codecheck issue

Merge pull request !26 from wenlong_12/master
This commit is contained in:
openharmony_ci 2022-02-21 13:17:30 +00:00 committed by Gitee
commit bb4085e67c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
13 changed files with 91 additions and 61 deletions

View File

@ -128,9 +128,10 @@ public:
virtual bool GetBinary(char *buf, size_t size) = 0;
virtual size_t GetSize() = 0;
virtual ~PerfFileSection() {};
explicit PerfFileSection(const FEATURE featureId) : featureId_(featureId) {}
explicit PerfFileSection(const FEATURE featureId) : featureId_(featureId)
{
header.size = 0;
}
static std::string GetFeatureName(FEATURE featureId);
protected:

View File

@ -33,7 +33,11 @@ constexpr const int WRITER_BUFFER_SIZE = 4 * 1024 * 1024;
// tools/perf/Documentation/perf.data-file-format.txt
class PerfFileWriter {
public:
PerfFileWriter() {};
PerfFileWriter()
{
attrSection_.offset = 0;
attrSection_.size = 0;
};
~PerfFileWriter();
bool Open(const std::string &fileName, bool compressData = false);

View File

@ -239,7 +239,7 @@ private:
bool SaveRecord(std::unique_ptr<PerfEventRecord>);
// file format like as 0,1-3,4-6,7,8
int GetCountFromFile(const std::string &fileName);
uint32_t GetCountFromFile(const std::string &fileName);
std::string GetCpuDescFromFile();
bool AddCpuFeature();
void AddMemTotalFeature();

View File

@ -98,21 +98,7 @@ public:
{
Clean();
}
const char *HoldStringView(std::string_view view)
{
if (view.size() == 0) {
return "";
}
// 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);
return p;
};
const char *HoldStringView(std::string_view view);
// only use in UT
void Clean()
{

View File

@ -308,8 +308,11 @@ int CallStack::AccessReg([[maybe_unused]] unw_addr_space_t as, unw_regnum_t regn
{
UnwindInfo *unwindInfoPtr = static_cast<UnwindInfo *>(arg);
uint64_t val;
size_t perfRegIndex = LibunwindRegIdToPerfReg(regnum);
int perfRegIndex = LibunwindRegIdToPerfReg(regnum);
if (perfRegIndex < 0) {
HLOGE("can't read reg %d", perfRegIndex);
return perfRegIndex;
}
/* Don't support write, I suspect we don't need it. */
if (writeOperation) {
HLOGE("access_reg %d", regnum);
@ -320,14 +323,14 @@ int CallStack::AccessReg([[maybe_unused]] unw_addr_space_t as, unw_regnum_t regn
return -UNW_EUNSPEC;
}
if (!RegisterGetValue(val, unwindInfoPtr->callStack.regs_, perfRegIndex,
if (!RegisterGetValue(val, unwindInfoPtr->callStack.regs_, static_cast<size_t>(perfRegIndex),
unwindInfoPtr->callStack.regsNum_)) {
HLOGE("can't read reg %zu", perfRegIndex);
HLOGE("can't read reg %d", perfRegIndex);
return -UNW_EUNSPEC;
}
*valuePoint = (unw_word_t)val;
HLOGM("reg %d:%s, val 0x%" UNW_WORD_PFLAG "", regnum, RegisterGetName(perfRegIndex).c_str(),
HLOGM("reg %d:%s, val 0x%" UNW_WORD_PFLAG "", regnum, RegisterGetName(static_cast<size_t>(perfRegIndex)).c_str(),
*valuePoint);
return UNW_ESUCCESS;
}
@ -547,7 +550,7 @@ size_t CallStack::DoExpandCallStack(std::vector<CallFrame> &newCallFrames,
// first frame earch, from called - > caller
// for case 2 it should found B
ssize_t distances = expandLimit - 1;
size_t distances = expandLimit - 1;
auto cachedIt = find(cachedCallFrames.begin(), cachedCallFrames.end(), *newIt);
if (cachedIt == cachedCallFrames.end()) {
HLOGM("not found in first search");

View File

@ -108,12 +108,12 @@ bool ElfFile::ParseElfHeader()
}
HLOG_ASSERT(ret == 0);
unsigned char ehdrBuf[ehdr64Size] {0};
ret = ReadFile(ehdrBuf, ehdr64Size);
if (ret < ehdr64Size) {
HLOGW("file size not enough, try read %zu, only have %zu", ehdr64Size, ret);
size_t readsize = ReadFile(ehdrBuf, ehdr64Size);
if (readsize < ehdr64Size) {
HLOGW("file size not enough, try read %zu, only have %zu", ehdr64Size, readsize);
}
HLOG_ASSERT(ret > 0);
ehdr_ = ElfHeader::MakeUnique(ehdrBuf, ret);
HLOG_ASSERT(readsize > 0);
ehdr_ = ElfHeader::MakeUnique(ehdrBuf, readsize);
return !(ehdr_ == nullptr);
}
@ -184,6 +184,10 @@ bool ElfFile::ParseSecNamesStr()
ret = lseek(fd_, secOffset, SEEK_SET);
HLOG_ASSERT(ret == static_cast<int64_t>(secOffset));
char *secNamesBuf = new (std::nothrow) char[secSize];
if (secNamesBuf == nullptr) {
HLOGE("new secNamesBuf failed");
return false;
}
memset_s(secNamesBuf, secSize, '\0', secSize);
ret = ReadFile(secNamesBuf, secSize);
if (ret != static_cast<int64_t>(secSize)) {

View File

@ -37,18 +37,18 @@ std::unique_ptr<ElfSymbol> ElfSymbol::MakeUnique(char * const symBuf, const std:
bool ElfSymbol::ParseElf32Symbol(char * const symBuf)
{
uint32_t *u4Buf = reinterpret_cast<uint32_t *>(symBuf);
constexpr int nameOffset {0};
constexpr uint32_t nameOffset {0};
nameIndex_ = u4Buf[nameOffset];
constexpr int valueOffset {1};
constexpr uint32_t valueOffset {1};
symValue_ = u4Buf[valueOffset];
constexpr int sizeOffset {2};
constexpr uint32_t sizeOffset {2};
symSize_ = u4Buf[sizeOffset];
constexpr int infoOffset {12};
constexpr uint32_t infoOffset {12};
symInfo_ = symBuf[infoOffset];
constexpr int otherInfoOffset {13};
constexpr uint32_t otherInfoOffset {13};
symOtherInfo_ = symBuf[otherInfoOffset];
uint16_t *u2Buf = reinterpret_cast<uint16_t *>(symBuf);
constexpr int secOffset {7};
constexpr uint32_t secOffset {7};
secIndex_ = u2Buf[secOffset];
return true;
}
@ -56,19 +56,19 @@ bool ElfSymbol::ParseElf32Symbol(char * const symBuf)
bool ElfSymbol::ParseElf64Symbol(char * const symBuf)
{
uint32_t *u4Buf = reinterpret_cast<uint32_t *>(symBuf);
constexpr int nameOffset {0};
constexpr uint32_t nameOffset {0};
nameIndex_ = u4Buf[nameOffset];
constexpr int infoOffset {4};
constexpr uint32_t infoOffset {4};
symInfo_ = symBuf[infoOffset];
constexpr int otherInfoOffset {5};
constexpr uint32_t otherInfoOffset {5};
symOtherInfo_ = symBuf[otherInfoOffset];
uint16_t *u2Buf = reinterpret_cast<uint16_t *>(symBuf);
constexpr int secOffset {3};
constexpr uint32_t secOffset {3};
secIndex_ = u2Buf[secOffset];
uint64_t *u8Buf = reinterpret_cast<uint64_t *>(symBuf);
constexpr int valueOffset {1};
constexpr uint32_t valueOffset {1};
symValue_ = u8Buf[valueOffset];
constexpr int sizeOffset {2};
constexpr uint32_t sizeOffset {2};
symSize_ = u8Buf[sizeOffset];
return true;
}

View File

@ -169,6 +169,10 @@ static std::string CovertByteBufferToHexString(const unsigned char *buffer, size
static std::string ElfGetBuildId(const unsigned char *buffer, size_t size)
{
if (buffer == nullptr) {
HLOGE("buffer is nullptr");
return EMPTY_STRING;
}
const unsigned char *end = buffer + size;
HLOGV("size:%zu", size);
static constexpr const int elfNoteSectionLens = sizeof(uint32_t) * 3;

View File

@ -421,8 +421,9 @@ PerfRecordMmap::PerfRecordMmap(bool inKernel, u32 pid, u32 tid, u64 addr, u64 le
data_.addr = addr;
data_.len = len;
data_.pgoff = pgoff;
strncpy_s(data_.filename, KILO, filename.c_str(), KILO);
if (strncpy_s(data_.filename, KILO, filename.c_str(), KILO) != 0) {
HLOGE("strncpy_s failed");
}
header.size = sizeof(header) + sizeof(data_) - KILO + filename.size() + 1;
}
@ -476,10 +477,12 @@ PerfRecordMmap2::PerfRecordMmap2(bool inKernel, u32 pid, u32 tid, u64 addr, u64
data_.maj = maj;
data_.min = min;
data_.ino = ino;
// ino_generation is ignored?
data_.ino_generation = 0;
data_.prot = prot;
data_.flags = flags;
strncpy_s(data_.filename, KILO, filename.c_str(), KILO);
if (strncpy_s(data_.filename, KILO, filename.c_str(), KILO) != 0) {
HLOGE("strncpy_s failed");
}
header.size = sizeof(header) + sizeof(data_) - KILO + filename.size() + 1;
}
@ -495,10 +498,12 @@ PerfRecordMmap2::PerfRecordMmap2(bool inKernel, u32 pid, u32 tid, const MemMapIt
data_.maj = item.major_;
data_.min = item.minor_;
data_.ino = item.inode;
// ino_generation is ignored?
data_.ino_generation = 0;
data_.prot = item.type_;
data_.flags = item.flags;
strncpy_s(data_.filename, KILO, item.name_.c_str(), KILO);
if (strncpy_s(data_.filename, KILO, item.name_.c_str(), KILO) != 0) {
HLOGE("strncpy_s failed");
}
header.size = sizeof(header) + sizeof(data_) - KILO + item.name_.size() + 1;
}
@ -574,7 +579,9 @@ PerfRecordComm::PerfRecordComm(bool inKernel, u32 pid, u32 tid, const std::strin
{
data_.pid = pid;
data_.tid = tid;
strncpy_s(data_.comm, KILO, comm.c_str(), KILO);
if (strncpy_s(data_.comm, KILO, comm.c_str(), KILO) != 0) {
HLOGE("strncpy_s failed !!!");
}
header.size = sizeof(header) + sizeof(data_) - KILO + comm.size() + 1;
}

View File

@ -354,7 +354,7 @@ bool PerfFileWriter::WriteFeatureData()
}
}
long contentOffset = featureOffset + featureSections_.size() * sizeof(perf_file_section);
unsigned long contentOffset = featureOffset + featureSections_.size() * sizeof(perf_file_section);
HLOGV("features start at file '0x%lx' content at '0x%lx'", featureOffset, contentOffset);
// reorder

View File

@ -82,22 +82,22 @@ bool SectionHeader::ParseSecHeader64(char * const shdrBuf)
uint32_t *u4Buf = reinterpret_cast<uint32_t *>(shdrBuf);
size_t index {0};
nameIndex_ = u4Buf[index];
index = static_cast<int>(NUMBER::ONE);
index = static_cast<size_t>(NUMBER::ONE);
secType_ = u4Buf[index];
secFlags_ = u8Buf[index];
index = static_cast<int>(NUMBER::TEN);
index = static_cast<size_t>(NUMBER::TEN);
link_ = u4Buf[index];
index = static_cast<int>(NUMBER::ELEVEN);
index = static_cast<size_t>(NUMBER::ELEVEN);
info_ = u4Buf[index];
index = static_cast<int>(NUMBER::TWO);
index = static_cast<size_t>(NUMBER::TWO);
secVaddr_ = u8Buf[index];
index = static_cast<int>(NUMBER::THREE);
index = static_cast<size_t>(NUMBER::THREE);
fileOffset_ = u8Buf[index];
index = static_cast<int>(NUMBER::FOUR);
index = static_cast<size_t>(NUMBER::FOUR);
secSize_ = u8Buf[index];
index = static_cast<int>(NUMBER::SIX);
index = static_cast<size_t>(NUMBER::SIX);
secAddrAlign_ = u8Buf[index];
index = static_cast<int>(NUMBER::SEVEN);
index = static_cast<size_t>(NUMBER::SEVEN);
secEntrySize_ = u8Buf[index];
return true;
}

View File

@ -1139,9 +1139,9 @@ bool SubCommandRecord::SaveRecord(std::unique_ptr<PerfEventRecord> record)
return false;
}
int SubCommandRecord::GetCountFromFile(const std::string &fileName)
uint32_t SubCommandRecord::GetCountFromFile(const std::string &fileName)
{
int ret = 0;
uint32_t ret = 0;
std::string str = ReadFileToString(fileName);
std::vector<std::string> subStrs = StringSplit(str);
for (auto subStr : subStrs) {

View File

@ -22,6 +22,27 @@
namespace OHOS {
namespace Developtools {
namespace HiPerf {
const char *MemoryHold::HoldStringView(std::string_view view)
{
if (view.size() == 0) {
return "";
}
try {
// 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);
return p;
} catch (...) {
return "";
}
return "";
}
std::string CanonicalizeSpecPath(const char* src)
{
char resolvedPath[PATH_MAX] = { 0 };