diff --git a/repos/base/include/base/output.h b/repos/base/include/base/output.h index 748e51aa2e..1a8469fb0c 100644 --- a/repos/base/include/base/output.h +++ b/repos/base/include/base/output.h @@ -167,6 +167,25 @@ namespace Genode { */ void print(Output &output, Hex const &); + /** + * Print range as hexadecimal format + * + * This helper is intended for the output for memory-address ranges. For + * brevity, it omits the '0x' prefix from the numbers. The numbers are + * padded with leading zeros to foster the horizontal alignment of + * consecutive outputs (like a table of address ranges). + */ + template + struct Hex_range + { + T const base; + size_t const len; + + Hex_range(T base, size_t len) : base(base), len(len) { } + + void print(Output &out) const; + }; + /** * Helper for the output of an individual character * @@ -208,4 +227,29 @@ namespace Genode { } } + +template +void Genode::Hex_range::print(Output &out) const +{ + using Genode::print; + + Hex const from(base, Hex::OMIT_PREFIX, Hex::PAD); + + T const end = base + len; + + /* if end at integer limit, use ']' as closing delimiter */ + if (base && end == 0) { + Hex const inclusive_to((T)(end - 1), Hex::OMIT_PREFIX, Hex::PAD); + print(out, "[", from, ",", inclusive_to, "]"); + return; + } + + /* use exclusive upper limit for ordinary ranges */ + print(out, "[", from, ",", Hex(end, Hex::OMIT_PREFIX, Hex::PAD), ")"); + + /* output warning on integer-overflowing upper limit or empty range */ + if (base && end < base) print(out, " (overflow!)"); + if (len == 0) print(out, " (empty!)"); +} + #endif /* _INCLUDE__BASE__OUTPUT_H_ */ diff --git a/repos/base/src/test/printf/main.cc b/repos/base/src/test/printf/main.cc index 51b617d356..dc7a257334 100644 --- a/repos/base/src/test/printf/main.cc +++ b/repos/base/src/test/printf/main.cc @@ -12,15 +12,29 @@ * under the terms of the GNU General Public License version 2. */ +#include #include +#include -int main(int argc, char **argv) -{ - /* test that unsupported commands don't crash the printf parser */ - Genode::printf("%#x %s\n", 0x38, "test 1"); - Genode::printf("%#lx %s\n", 0x38L, "test 2"); +namespace Component { - Genode::printf("-1 = %d = %ld\n", -1, -1L); + Genode::size_t stack_size() { return 4*1024*sizeof(long); } - return 0; + void construct(Genode::Env &env); +} + + +void Component::construct(Genode::Env &env) +{ + using namespace Genode; + + log("hex range: ", Hex_range(0xe00, 0x880)); + log("empty hex range: ", Hex_range(0xabc0000, 0)); + log("hex range to limit: ", Hex_range(0xf8, 8)); + log("invalid hex range: ", Hex_range(0xf8, 0x10)); + + /* test that unsupported commands don't crash the printf parser */ + printf("%#x %s\n", 0x38, "test 1"); + printf("%#lx %s\n", 0x38L, "test 2"); + printf("-1 = %d = %ld\n", -1, -1L); }