authorgravatar for 61841960+Arnau478@users.noreply.github.comArnau Camprubí <61841960+Arnau478@users.noreply.github.com> 2025-03-25 23:20:56+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-26 15:40:56+01:00
log9c857bb32d838a14fdf9fd09d88d74bb9ccfc72e
tree756ec049773898fcbc477cf9aa39f31a615e1238
parent38ececf0a76867ff0e56f38879bb72344f9f9f17
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Fix std.debug.dumpHex address offsets


1 files changed, 23 insertions(+), 1 deletions(-)

lib/std/debug.zig+23-1
...@@ -244,10 +244,14 @@ pub fn dumpHexFallible(bytes: []const u8) !void {...@@ -244,10 +244,14 @@ pub fn dumpHexFallible(bytes: []const u8) !void {
244 const stderr = std.io.getStdErr();244 const stderr = std.io.getStdErr();
245 const ttyconf = std.io.tty.detectConfig(stderr);245 const ttyconf = std.io.tty.detectConfig(stderr);
246 const writer = stderr.writer();246 const writer = stderr.writer();
247 try dumpHexInternal(bytes, ttyconf, writer);
248}
249
250fn dumpHexInternal(bytes: []const u8, ttyconf: std.io.tty.Config, writer: anytype) !void {
247 var chunks = mem.window(u8, bytes, 16, 16);251 var chunks = mem.window(u8, bytes, 16, 16);
248 while (chunks.next()) |window| {252 while (chunks.next()) |window| {
249 // 1. Print the address.253 // 1. Print the address.
250 const address = (@intFromPtr(bytes.ptr) + 0x10 * (chunks.index orelse 0) / 16) - 0x10;254 const address = (@intFromPtr(bytes.ptr) + 0x10 * (std.math.divCeil(usize, chunks.index orelse bytes.len, 16) catch unreachable)) - 0x10;
251 try ttyconf.setColor(writer, .dim);255 try ttyconf.setColor(writer, .dim);
252 // We print the address in lowercase and the bytes in uppercase hexadecimal to distinguish them more.256 // We print the address in lowercase and the bytes in uppercase hexadecimal to distinguish them more.
253 // Also, make sure all lines are aligned by padding the address.257 // Also, make sure all lines are aligned by padding the address.
...@@ -292,6 +296,24 @@ pub fn dumpHexFallible(bytes: []const u8) !void {...@@ -292,6 +296,24 @@ pub fn dumpHexFallible(bytes: []const u8) !void {
292 }296 }
293}297}
294298
299test dumpHexInternal {
300 const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 };
301 var output = std.ArrayList(u8).init(std.testing.allocator);
302 defer output.deinit();
303 try dumpHexInternal(bytes, .no_color, output.writer());
304 const expected = try std.fmt.allocPrint(std.testing.allocator,
305 \\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........
306 \\{x:0>[2]} 01 12 13 ...
307 \\
308 , .{
309 @intFromPtr(bytes.ptr),
310 @intFromPtr(bytes.ptr) + 16,
311 @sizeOf(usize) * 2,
312 });
313 defer std.testing.allocator.free(expected);
314 try std.testing.expectEqualStrings(expected, output.items);
315}
316
295/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.317/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
296/// TODO multithreaded awareness318/// TODO multithreaded awareness
297pub fn dumpCurrentStackTrace(start_addr: ?usize) void {319pub fn dumpCurrentStackTrace(start_addr: ?usize) void {