Stream: announcements

Topic: dbg upgrade


view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 07:18):

If you own a platform and want even better debug messages, I just changed debug to take an extra arg that is the actual source of the debugged expression. Just add the extra arg and print it in a pretty way. Here is an example in rust and zig:

Requires using the latest version of the compiler (should be in the next nightly).

Can get awesome dbg messages like:

[day3.roc:18] (part1 p1Sample) = 4361
#[no_mangle]
pub unsafe extern "C" fn roc_dbg(loc: *mut RocStr, msg: *mut RocStr, src: *mut RocStr) {
    eprintln!("[{}] {} = {}", &*loc, &*src, &*msg);
}
export fn roc_dbg(loc: *RocStr, msg: *RocStr, src: *RocStr) callconv(.C) void {
    const stderr = std.io.getStdErr().writer();
    stderr.print("[{s}] {s} = {s}\n", .{ loc.asSlice(), src.asSlice(), msg.asSlice() }) catch unreachable;
}

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 07:19):

If you don't update roc_dbg, everything should be backwards compatible and still just work the same. Your function will just be missing and ignore the third arg.

view this post on Zulip Oskar Hahn (Dec 03 2023 at 08:07):

Thank you, that is nice.

Could it be, that there is a off-by-one error? It does not show the line of the dbg statement, but one line above it. For example, if I put dbg on line 10, the output shows line 9.

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 08:11):

That's definitely possible, though it had seemed to be working on my testing (at least in terms of the statements printed). I guess I never checked the actual line number and if it is zero indexed or not.

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 15:15):

@Oskar Hahn do you have more details or a repro file?

view this post on Zulip Oskar Hahn (Dec 03 2023 at 15:26):

Brendan Hansknecht said:

Oskar Hahn do you have more details or a repro file?

I already updated my platform. You can add the dbg statement anywhere here https://github.com/ostcar/aoc2023/blob/main/platform/host.zig

view this post on Zulip Oskar Hahn (Dec 03 2023 at 15:51):

Here is an example: https://github.com/ostcar/aoc2023/blob/dbg_bug/days/dayXX.roc

roc days/dayXX.roc
🔨 Rebuilding platform...
[days/dayXX.roc:13] "this is on line 14" = "this is on line 14"
Part1 in 58.171us:
Not implemented yet

Part2 in 1.956us:
Not implemented yet

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 16:00):

Ok. So yeah just line numbers being zero index in roc. Need to add 1 to them. Thanks

view this post on Zulip Oskar Hahn (Dec 03 2023 at 17:19):

And why is the string being printed two times? Here is the platform code: https://github.com/ostcar/aoc2023/blob/33b645286345c79ea2ae9e757ddb931d09fef475/platform/host.zig#L69

view this post on Zulip Oskar Hahn (Dec 03 2023 at 17:20):

Ah. Because I did not use a variable

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 17:21):

Yeah, it prints the source code and the output.

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 17:21):

Strange when debuging with a constant, but really awesome the rest of the time.

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 17:22):

For example, you can get output like:

[path/my-file.roc:12] (List.len out) = 7

view this post on Zulip Oskar Hahn (Dec 03 2023 at 17:59):

The case, where you just debug a constant was easy to fix in the platform: https://github.com/ostcar/aoc2023/blob/f657d50aa2b393b5dc26b49baeb6f740ef405908/platform/host.zig#L67-L74

But I don't know if this is really necessary. It is not the use case of a debugger to print a constant

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 18:00):

yep. Up to each platform if they want to do that or not

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 19:40):

As an extra note, if you are using dbg and notice weird type issues (probably with number types) that appear when you add a debug statement, please update to the lastest main or wait for the next nightly. Just got a bug fix in.

view this post on Zulip Anton (Dec 04 2023 at 10:39):

Fresh nightlies are up


Last updated: Jul 26 2025 at 12:14 UTC