mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-18 01:36:08 +03:00
macos: fix signpost API to use proper mach header base addr
This commit is contained in:
@ -5,6 +5,38 @@ const c = @import("c.zig").c;
|
|||||||
const logpkg = @import("log.zig");
|
const logpkg = @import("log.zig");
|
||||||
const Log = logpkg.Log;
|
const Log = logpkg.Log;
|
||||||
|
|
||||||
|
/// This should be called once at the start of the program to intialize
|
||||||
|
/// some required state for signpost logging.
|
||||||
|
///
|
||||||
|
/// This is all to workaround a Zig bug:
|
||||||
|
/// https://github.com/ziglang/zig/issues/24370
|
||||||
|
pub fn init() void {
|
||||||
|
if (__dso_handle != null) return;
|
||||||
|
|
||||||
|
// Since __dso_handle is not automatically populated by the linker,
|
||||||
|
// we populate it by looking up the main function's module address
|
||||||
|
// which should be a mach-o header.
|
||||||
|
var info: DlInfo = undefined;
|
||||||
|
const result = dladdr(@import("root").main, &info);
|
||||||
|
assert(result != 0);
|
||||||
|
__dso_handle = @ptrCast(@alignCast(info.dli_fbase));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This should REALLY be an extern var that is populated by the linker,
|
||||||
|
/// but there is a Zig bug: https://github.com/ziglang/zig/issues/24370
|
||||||
|
var __dso_handle: ?*c.mach_header = null;
|
||||||
|
|
||||||
|
// Import the necessary C functions and types
|
||||||
|
extern "c" fn dladdr(addr: ?*const anyopaque, info: *DlInfo) c_int;
|
||||||
|
|
||||||
|
// Define the Dl_info structure
|
||||||
|
const DlInfo = extern struct {
|
||||||
|
dli_fname: [*:0]const u8, // Pathname of shared object
|
||||||
|
dli_fbase: ?*anyopaque, // Base address of shared object
|
||||||
|
dli_sname: [*:0]const u8, // Name of nearest symbol
|
||||||
|
dli_saddr: ?*anyopaque, // Address of nearest symbol
|
||||||
|
};
|
||||||
|
|
||||||
/// Checks whether signpost logging is enabled for the given log handle.
|
/// Checks whether signpost logging is enabled for the given log handle.
|
||||||
/// Returns true if signposts will be recorded for this log, false otherwise.
|
/// Returns true if signposts will be recorded for this log, false otherwise.
|
||||||
/// This can be used to avoid expensive operations when signpost logging is disabled.
|
/// This can be used to avoid expensive operations when signpost logging is disabled.
|
||||||
@ -48,8 +80,6 @@ pub fn intervalEnd(log: *Log, id: Id, comptime name: [:0]const u8) void {
|
|||||||
emitWithName(log, id, .interval_end, name);
|
emitWithName(log, id, .interval_end, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern var __dso_handle: usize;
|
|
||||||
|
|
||||||
/// The internal function to emit a signpost with a specific name.
|
/// The internal function to emit a signpost with a specific name.
|
||||||
fn emitWithName(
|
fn emitWithName(
|
||||||
log: *Log,
|
log: *Log,
|
||||||
@ -57,14 +87,17 @@ fn emitWithName(
|
|||||||
typ: Type,
|
typ: Type,
|
||||||
comptime name: [:0]const u8,
|
comptime name: [:0]const u8,
|
||||||
) void {
|
) void {
|
||||||
var buf: [64]u8 = @splat(0);
|
// Init must be called by this point.
|
||||||
|
assert(__dso_handle != null);
|
||||||
|
|
||||||
|
var buf: [2]u8 = @splat(0);
|
||||||
c._os_signpost_emit_with_name_impl(
|
c._os_signpost_emit_with_name_impl(
|
||||||
&__dso_handle,
|
__dso_handle,
|
||||||
@ptrCast(log),
|
@ptrCast(log),
|
||||||
@intFromEnum(typ),
|
@intFromEnum(typ),
|
||||||
@intFromEnum(id),
|
@intFromEnum(id),
|
||||||
name.ptr,
|
name.ptr,
|
||||||
null,
|
"".ptr,
|
||||||
&buf,
|
&buf,
|
||||||
buf.len,
|
buf.len,
|
||||||
);
|
);
|
||||||
@ -135,12 +168,12 @@ pub const Category = struct {
|
|||||||
/// Dynamic Tracing category enables runtime-configurable logging.
|
/// Dynamic Tracing category enables runtime-configurable logging.
|
||||||
/// Signposts in this category can be enabled/disabled dynamically
|
/// Signposts in this category can be enabled/disabled dynamically
|
||||||
/// without recompiling.
|
/// without recompiling.
|
||||||
pub const dynamic_tracing: [:0]const u8 = "DynamicTracking";
|
pub const dynamic_tracing: [:0]const u8 = "DynamicTracing";
|
||||||
|
|
||||||
/// Dynamic Stack Tracing category captures call stacks at signpost
|
/// Dynamic Stack Tracing category captures call stacks at signpost
|
||||||
/// events. This provides deeper debugging information but has higher
|
/// events. This provides deeper debugging information but has higher
|
||||||
/// performance overhead.
|
/// performance overhead.
|
||||||
pub const dynamic_stack_tracing: [:0]const u8 = "DynamicStackTracking";
|
pub const dynamic_stack_tracing: [:0]const u8 = "DynamicStackTracing";
|
||||||
};
|
};
|
||||||
|
|
||||||
test {
|
test {
|
||||||
@ -152,6 +185,8 @@ test enabled {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test "intervals" {
|
test "intervals" {
|
||||||
|
init();
|
||||||
|
|
||||||
const log = Log.create("com.mitchellh.ghostty", "test");
|
const log = Log.create("com.mitchellh.ghostty", "test");
|
||||||
defer log.release();
|
defer log.release();
|
||||||
|
|
||||||
|
@ -41,11 +41,12 @@ pub fn run(
|
|||||||
// If we're on macOS, we setup signposts so its easier to find
|
// If we're on macOS, we setup signposts so its easier to find
|
||||||
// the results in Instruments. There's a lot of nasty comptime stuff
|
// the results in Instruments. There's a lot of nasty comptime stuff
|
||||||
// here but its just to ensure this does nothing on other platforms.
|
// here but its just to ensure this does nothing on other platforms.
|
||||||
const signpost_name = "Ghostty Benchmark";
|
const signpost_name = "ghostty";
|
||||||
const signpost: if (builtin.target.os.tag.isDarwin()) struct {
|
const signpost: if (builtin.target.os.tag.isDarwin()) struct {
|
||||||
log: *macos.os.Log,
|
log: *macos.os.Log,
|
||||||
id: macos.os.signpost.Id,
|
id: macos.os.signpost.Id,
|
||||||
} else void = if (comptime builtin.os.tag == .macos) macos: {
|
} else void = if (comptime builtin.os.tag == .macos) macos: {
|
||||||
|
macos.os.signpost.init();
|
||||||
const log = macos.os.Log.create(
|
const log = macos.os.Log.create(
|
||||||
build_config.bundle_id,
|
build_config.bundle_id,
|
||||||
macos.os.signpost.Category.points_of_interest,
|
macos.os.signpost.Category.points_of_interest,
|
||||||
|
Reference in New Issue
Block a user