Skip to content

Commit

Permalink
feat: add a tflite::hexdump() for printing raw memory
Browse files Browse the repository at this point in the history
Add tflite::hexdump() for printing raw memory to output streams. Copy
the output format of Python's hexdump module.
  • Loading branch information
rkuester committed Aug 7, 2024
1 parent d3475aa commit 5a80cb8
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tensorflow/lite/micro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ cc_library(
],
)

cc_library(
name = "hexdump",
srcs = [
"hexdump.cc",
],
hdrs = [
"hexdump.h",
],
deps = [
":span",
":static_vector"
],
)

cc_library(
name = "recording_allocators",
srcs = [
Expand Down Expand Up @@ -556,6 +570,17 @@ cc_test(
],
)

cc_test(
name = "hexdump_test",
srcs = [
"hexdump_test.cc",
],
deps = [
":hexdump",
],
size = "small",
)

cc_test(
name = "memory_helpers_test",
srcs = [
Expand Down
66 changes: 66 additions & 0 deletions tensorflow/lite/micro/hexdump.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.
Licensed 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 "tensorflow/lite/micro/hexdump.h"

#include <iomanip>
#include <iostream>
#include <ostream>

#include "tensorflow/lite/micro/static_vector.h"

void tflite::hexdump(tflite::Span<const std::byte> region, std::ostream& out) {
auto initial_flags = out.flags();
out << std::hex << std::uppercase << std::setfill('0');

std::size_t byte = 0;
constexpr int per_line = 16;
const int lines = (region.size() + per_line - 1) / per_line; // rounded up
for (int line = 0; line < lines; ++line) {
tflite::StaticVector<char, per_line> ascii;

// print address
out << std::setw(8) << line << ":";

for (int pos = 0; pos < per_line; ++pos) {
if (byte < region.size()) {
// print byte
int as_int = static_cast<int>(region[byte++]);
out << ' ' << std::setw(2) << as_int;

// buffer an ascii printable value
char c{'.'};
if (std::isprint(as_int)) {
c = static_cast<char>(as_int);
}
ascii.push_back(c);
} else {
out << " ";
}

// extra space in the middle
if (pos == per_line / 2 - 1) {
out << " ";
}
}

out << " ";
for (const auto& c : ascii) {
out << c;
}
out << '\n';
}

out.flags(initial_flags);
}
32 changes: 32 additions & 0 deletions tensorflow/lite/micro/hexdump.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.
Licensed 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. */

// A hexdump with an output style matching Python's hexdump module.

#ifndef TENSORFLOW_LITE_MICRO_HEXDUMP_H_
#define TENSORFLOW_LITE_MICRO_HEXDUMP_H_

#include <cstddef>
#include <iostream>
#include <ostream>

#include "tensorflow/lite/micro/span.h"

namespace tflite {

void hexdump(Span<const std::byte> region, std::ostream& = std::cout);

} // end namespace tflite

#endif // TENSORFLOW_LITE_MICRO_HEXDUMP_H_
9 changes: 9 additions & 0 deletions tensorflow/lite/micro/hexdump_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "tensorflow/lite/micro/hexdump.h"

#include <string>

int main(int argc, char* argv[]) {
const std::string s{"This is a really long test string."};
tflite::hexdump({reinterpret_cast<const std::byte*>(s.data()), s.size()});
return 0;
}

0 comments on commit 5a80cb8

Please sign in to comment.