Skip to content

Commit b082f99

Browse files
committed
sigge interrupt
1 parent d709b3e commit b082f99

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

count_nvim.ir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
map $count array 4 8 1
2+
3+
func $main MIT
4+
%value = map_value $count
5+
xadd [%value] 1
6+
ret 0
7+
end
8+
9+
elf $neovim /home/bfredl/dev/neovim/build/bin/nvim
10+
attach $main uprobe $neovim xmalloc
11+
attach $main uprobe $neovim xcalloc
12+
attach $main uprobe $neovim xrealloc

src/eiri.zig

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const ElfSymbols = @import("./ElfSymbols.zig");
55
const FLIR = @import("./FLIR.zig");
66
const RingBuf = @import("./RingBuf.zig");
77
const Parser = @import("./Parser.zig");
8-
const linux = std.os.linux;
9-
const BPF = linux.BPF;
8+
const os = std.os;
9+
const BPF = os.linux.BPF;
1010
const Insn = BPF.Insn;
1111
const io = std.io;
1212
const mem = std.mem;
13-
const fd_t = linux.fd_t;
14-
const errno = linux.getErrno;
13+
const fd_t = os.linux.fd_t;
14+
const errno = os.linux.getErrno;
1515
const print = std.debug.print;
1616

1717
pub var options = struct {
@@ -37,6 +37,14 @@ pub fn usage() void {
3737
, .{});
3838
}
3939

40+
var did_int = false;
41+
fn on_sigint(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) void {
42+
_ = sig;
43+
_ = info;
44+
_ = ctx_ptr;
45+
did_int = true;
46+
}
47+
4048
pub fn main() !void {
4149
const mode = @import("builtin").mode;
4250
var gpa = if (mode == .Debug)
@@ -103,9 +111,17 @@ pub fn main() !void {
103111
}
104112
}
105113

114+
var sa = os.Sigaction{
115+
.handler = .{ .sigaction = on_sigint },
116+
.mask = os.empty_sigset,
117+
.flags = os.SA.SIGINFO,
118+
};
119+
try os.sigaction(os.SIG.INT, &sa, null);
120+
106121
var lastval: u64 = @truncate(u64, -1);
107122
const asBytes = mem.asBytes;
108-
while (true) {
123+
while (!did_int) {
124+
std.time.sleep(1e9);
109125
if (count_map) |map| {
110126
const key: u32 = 0;
111127
var value: u64 = undefined;
@@ -115,40 +131,43 @@ pub fn main() !void {
115131
lastval = value;
116132
}
117133
}
118-
std.time.sleep(1e9);
119134
if (ringbuf) |*rb| {
120135
while (rb.peek_event()) |ev| {
121136
did_read = true;
122137
print("VERY EVENT: {}\n", .{ev});
123138
rb.consume_event(ev);
124139
}
125140
}
141+
}
142+
print("DUN\n", .{});
126143

127-
if (hash_map) |hash| {
128-
var key: u32 = 0;
129-
var next_key: u32 = 0;
130-
print("hashy: \n", .{});
131-
while (try BPF.map_get_next_key(hash.fd, asBytes(&key), asBytes(&next_key))) {
132-
key = next_key;
133-
var value: u64 = 0;
134-
try BPF.map_lookup_elem(hash.fd, asBytes(&key), asBytes(&value));
135-
print("K: {}, V: {}\n", .{ key, value });
136-
if (stack_map) |stack| {
137-
var stack_value: [128]u64 = [1]u64{0xDEADBEEFDEADF00D} ** 128;
138-
BPF.map_lookup_elem(stack.fd, asBytes(&key), asBytes(&stack_value)) catch |e| {
139-
if (e == error.NotFound) continue;
140-
return e;
141-
};
142-
// TODO: we can figure this out dynamically by taking the $rip at some
143-
// know function. then also ASLR of the main binary will be supported.
144-
const off: u64 = 0x555555554000;
145-
print("0x{x}\n0x{x}\n0x{x}\n", .{ stack_value[0] - off, stack_value[1] - off, stack_value[2] - off });
146-
}
144+
if (hash_map) |hash| {
145+
var key: u32 = 0;
146+
var next_key: u32 = 0;
147+
print("hashy: \n", .{});
148+
while (try BPF.map_get_next_key(hash.fd, asBytes(&key), asBytes(&next_key))) {
149+
key = next_key;
150+
var value: u64 = 0;
151+
try BPF.map_lookup_elem(hash.fd, asBytes(&key), asBytes(&value));
152+
print("{}: ", .{value});
153+
if (stack_map) |stack| {
154+
var trace: [128]u64 = [1]u64{0xDEADBEEFDEADF00D} ** 128;
155+
BPF.map_lookup_elem(stack.fd, asBytes(&key), asBytes(&trace)) catch |e| {
156+
if (e == error.NotFound) continue;
157+
return e;
158+
};
159+
print("0x{x} 0x{x} 0x{x}\n", .{ adj(trace[0]), adj(trace[1]), adj(trace[2]) });
147160
}
148161
}
149162
}
150-
151163
// doesn't work on kprobe programs (more context needed?)
152164
// const retval = try bpfUtil.prog_test_run(prog);
153165
// print("RETURNERA: {}\n", .{retval});
154166
}
167+
168+
fn adj(val: u64) u64 {
169+
// TODO: we can figure this out dynamically by taking the $rip at some
170+
// know function. then also ASLR of the main binary will be supported.
171+
const off: u64 = 0x555555554000;
172+
return if (val > off) val - off else val;
173+
}

0 commit comments

Comments
 (0)