summaryrefslogtreecommitdiff
path: root/src/cli/util.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/util.zig')
-rw-r--r--src/cli/util.zig16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/cli/util.zig b/src/cli/util.zig
index 1d7ee4c..652956d 100644
--- a/src/cli/util.zig
+++ b/src/cli/util.zig
@@ -62,6 +62,22 @@ pub const Producer = struct {
}
};
+extern fn __cxa_demangle(name: [*:0]const u8, out: ?[*]u8, len: ?*usize, status: *c_int) ?[*:0]u8;
+
+/// What `Symbols.demangle` is set to: the C++ runtime's demangler.
+pub fn demangle(name: []const u8, buf: []u8) ?[]const u8 {
+ if (!std.mem.startsWith(u8, name, "_Z") or name.len >= buf.len) return null;
+ @memcpy(buf[0..name.len], name);
+ buf[name.len] = 0;
+ var status: c_int = 0;
+ const out = __cxa_demangle(buf[0..name.len :0], null, null, &status) orelse return null;
+ defer std.c.free(out);
+ const text = std.mem.span(out);
+ if (text.len > buf.len) return null;
+ @memcpy(buf[0..text.len], text);
+ return buf[0..text.len];
+}
+
/// The arguments after a command's own, as the target's argv.
pub fn targetArgv(arena: std.mem.Allocator, args: []const [:0]const u8) ![:null]?[*:0]const u8 {
const argv = try arena.allocSentinel(?[*:0]const u8, args.len, null);