DoxigAlpha

dumpHexFallible

Prints a hexadecimal view of the bytes, returning any error that occurs.

Function parameters

Parameters

#
bw:*Writer
ttyconf:std.io.tty.Config
bytes:[]const u8

Type definitions in this namespace

Types

#
SourceLocation
Unresolved source locations can be represented with a single `usize` that

A fully-featured panic handler namespace which lowers all panics to calls to `panicFn`.

Functions

#
FullPanic
A fully-featured panic handler namespace which lowers all panics to calls to `panicFn`.
lockStdErr
Allows the caller to freely write to stderr until `unlockStdErr` is called.
lockStderrWriter
Allows the caller to freely write to stderr until `unlockStdErr` is called.
print
Print to stderr, silently returning on failure.
dumpHex
Tries to print a hexadecimal view of the bytes, unbuffered, and ignores any error returned.
dumpHexFallible
Prints a hexadecimal view of the bytes, returning any error that occurs.
dumpCurrentStackTrace
Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
dumpCurrentStackTraceToWriter
Prints the current stack trace to the provided writer.
copyContext
Copies one context to another, updating any internal pointers
relocateContext
Updates any internal pointers in the context to reflect its current location
getContext
Capture the current context.
dumpStackTraceFromBase
Tries to print the stack trace starting from the supplied base pointer to stderr,
captureStackTrace
Returns a slice with the same pointer as addresses, with a potentially smaller len.
dumpStackTrace
Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.
assert
Invokes detectable illegal behavior when `ok` is `false`.
assertReadable
Invokes detectable illegal behavior when the provided slice is not mapped
assertAligned
Invokes detectable illegal behavior when the provided array is not aligned
panic
Equivalent to `@panic` but with a formatted message.
panicExtra
Equivalent to `@panic` but with a formatted message, and with an explicitly
defaultPanic
Dumps a stack trace to standard error, then aborts.
attachSegfaultHandler
Attaches a global SIGSEGV handler which calls `@panic("segmentation fault");`
inValgrind
Detect whether the program is being executed in the Valgrind virtual machine.

Deprecated because it returns the optimization mode of the standard

Values

#
runtime_safety
Deprecated because it returns the optimization mode of the standard
sys_can_stack_trace
= switch (builtin.cpu.arch) { // Observed to go into an infinite loop. // TODO: Make this work. .mips, .mipsel, .mips64, .mips64el, .s390x, => false, // `@returnAddress()` in LLVM 10 gives // "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address". // On Emscripten, Zig only supports `@returnAddress()` in debug builds // because Emscripten's implementation is very slow. .wasm32, .wasm64, => native_os == .emscripten and builtin.mode == .Debug, // `@returnAddress()` is unsupported in LLVM 13. .bpfel, .bpfeb, => false, else => true, }
have_ucontext
= posix.ucontext_t != void
ThreadContext
Platform-specific thread state.
have_getcontext
= @TypeOf(posix.system.getcontext) != void
have_segfault_handling_support
Whether or not the current target can print useful debug information when a segfault occurs.
default_enable_segfault_handler
= runtime_safety and have_segfault_handling_support

Source

Implementation

#
pub fn dumpHexFallible(bw: *Writer, ttyconf: std.io.tty.Config, bytes: []const u8) !void {
    var chunks = mem.window(u8, bytes, 16, 16);
    while (chunks.next()) |window| {
        // 1. Print the address.
        const address = (@intFromPtr(bytes.ptr) + 0x10 * (std.math.divCeil(usize, chunks.index orelse bytes.len, 16) catch unreachable)) - 0x10;
        try ttyconf.setColor(bw, .dim);
        // We print the address in lowercase and the bytes in uppercase hexadecimal to distinguish them more.
        // Also, make sure all lines are aligned by padding the address.
        try bw.print("{x:0>[1]}  ", .{ address, @sizeOf(usize) * 2 });
        try ttyconf.setColor(bw, .reset);

        // 2. Print the bytes.
        for (window, 0..) |byte, index| {
            try bw.print("{X:0>2} ", .{byte});
            if (index == 7) try bw.writeByte(' ');
        }
        try bw.writeByte(' ');
        if (window.len < 16) {
            var missing_columns = (16 - window.len) * 3;
            if (window.len < 8) missing_columns += 1;
            try bw.splatByteAll(' ', missing_columns);
        }

        // 3. Print the characters.
        for (window) |byte| {
            if (std.ascii.isPrint(byte)) {
                try bw.writeByte(byte);
            } else {
                // Related: https://github.com/ziglang/zig/issues/7600
                if (ttyconf == .windows_api) {
                    try bw.writeByte('.');
                    continue;
                }

                // Let's print some common control codes as graphical Unicode symbols.
                // We don't want to do this for all control codes because most control codes apart from
                // the ones that Zig has escape sequences for are likely not very useful to print as symbols.
                switch (byte) {
                    '\n' => try bw.writeAll("␊"),
                    '\r' => try bw.writeAll("␍"),
                    '\t' => try bw.writeAll("␉"),
                    else => try bw.writeByte('.'),
                }
            }
        }
        try bw.writeByte('\n');
    }
}