forked from neo-ai/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tvm::support::hexdump() debug utility (apache#6154)
- Loading branch information
Showing
4 changed files
with
200 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* | ||
* \file support/hexdump.cc | ||
* \brief Print hex representation of BLOBs. | ||
*/ | ||
|
||
#include "hexdump.h" | ||
|
||
namespace tvm { | ||
namespace support { | ||
|
||
void HexDump(const std::string& s, std::ostream& os) { | ||
os << std::hex << std::setfill('0') << std::right; | ||
|
||
int addr_width = 4; | ||
for (size_t addr_bytes = s.size() >> 16; addr_bytes != 0; addr_bytes >>= 4) { | ||
addr_width++; | ||
} | ||
|
||
for (size_t cursor = 0; cursor < s.size(); cursor += 0x10) { | ||
os << std::setw(addr_width) << cursor; | ||
size_t row_end = cursor + 0x10; | ||
if (row_end > s.size()) { | ||
row_end = s.size(); | ||
} | ||
|
||
os << " "; | ||
for (size_t j = cursor; j < row_end; j++) { | ||
os << " " << std::setw(2) << (unsigned int)(s[j] & 0xff); | ||
} | ||
|
||
for (size_t j = row_end; j < cursor + 0x10; j++) { | ||
os << " "; | ||
} | ||
|
||
os << std::setw(1) << " "; | ||
for (size_t j = cursor; j < row_end; j++) { | ||
os << (isprint(s[j]) ? s[j] : '.'); | ||
} | ||
os << std::endl; | ||
} | ||
} | ||
|
||
} // namespace support | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* | ||
* \file support/hexdump.h | ||
* \brief Print hex representation of BLOBs. | ||
*/ | ||
#ifndef TVM_SUPPORT_HEXDUMP_H_ | ||
#define TVM_SUPPORT_HEXDUMP_H_ | ||
|
||
#include <iomanip> | ||
#include <sstream> | ||
#include <string> | ||
|
||
namespace tvm { | ||
namespace support { | ||
|
||
/*! \brief generate a hexdump of some binary data. | ||
* \param s Binary data to print. | ||
* \param os stream that receives the hexdump. | ||
*/ | ||
void HexDump(const std::string& s, std::ostream& os); | ||
|
||
/*! \brief return a string containing a hexdump of the data in s */ | ||
inline std::string HexDump(const std::string& s) { | ||
std::stringstream ss; | ||
HexDump(s, ss); | ||
return ss.str(); | ||
} | ||
|
||
} // namespace support | ||
} // namespace tvm | ||
#endif // TVM_SUPPORT_HEXDUMP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include <dmlc/logging.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "../../src/support/hexdump.h" | ||
|
||
namespace tvm { | ||
namespace test { | ||
|
||
TEST(HexDumpTests, Empty) { EXPECT_EQ("", ::tvm::support::HexDump("")); } | ||
|
||
TEST(HexDumpTests, Aligned) { | ||
EXPECT_EQ( | ||
"0000 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef .#Eg.....#Eg....\n" | ||
"0010 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef .#Eg.....#Eg....\n", | ||
::tvm::support::HexDump("\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89\xab\xcd\xef" | ||
"\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89\xab\xcd\xef")); | ||
} | ||
|
||
TEST(HexDumpTests, Unaligned) { | ||
EXPECT_EQ( | ||
"0000 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef .#Eg.....#Eg....\n" | ||
"0010 01 23 45 67 89 ab cd ef 01 .#Eg.....\n", | ||
::tvm::support::HexDump("\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89\xab\xcd\xef" | ||
"\x01\x23\x45\x67\x89\xab\xcd\xef\x01")); | ||
} | ||
|
||
} // namespace test | ||
} // namespace tvm | ||
|
||
int main(int argc, char** argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
testing::FLAGS_gtest_death_test_style = "threadsafe"; | ||
return RUN_ALL_TESTS(); | ||
} |