@@ -5,13 +5,13 @@ const ElfSymbols = @import("./ElfSymbols.zig");
5
5
const FLIR = @import ("./FLIR.zig" );
6
6
const RingBuf = @import ("./RingBuf.zig" );
7
7
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 ;
10
10
const Insn = BPF .Insn ;
11
11
const io = std .io ;
12
12
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 ;
15
15
const print = std .debug .print ;
16
16
17
17
pub var options = struct {
@@ -37,6 +37,14 @@ pub fn usage() void {
37
37
, .{});
38
38
}
39
39
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
+
40
48
pub fn main () ! void {
41
49
const mode = @import ("builtin" ).mode ;
42
50
var gpa = if (mode == .Debug )
@@ -103,9 +111,17 @@ pub fn main() !void {
103
111
}
104
112
}
105
113
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
+
106
121
var lastval : u64 = @truncate (u64 , -1 );
107
122
const asBytes = mem .asBytes ;
108
- while (true ) {
123
+ while (! did_int ) {
124
+ std .time .sleep (1e9 );
109
125
if (count_map ) | map | {
110
126
const key : u32 = 0 ;
111
127
var value : u64 = undefined ;
@@ -115,40 +131,43 @@ pub fn main() !void {
115
131
lastval = value ;
116
132
}
117
133
}
118
- std .time .sleep (1e9 );
119
134
if (ringbuf ) | * rb | {
120
135
while (rb .peek_event ()) | ev | {
121
136
did_read = true ;
122
137
print ("VERY EVENT: {}\n " , .{ev });
123
138
rb .consume_event (ev );
124
139
}
125
140
}
141
+ }
142
+ print ("DUN\n " , .{});
126
143
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}\n 0x{x}\n 0x{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 ]) });
147
160
}
148
161
}
149
162
}
150
-
151
163
// doesn't work on kprobe programs (more context needed?)
152
164
// const retval = try bpfUtil.prog_test_run(prog);
153
165
// print("RETURNERA: {}\n", .{retval});
154
166
}
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