@@ -19,6 +19,15 @@ struct {
1919 __uint (max_entries , TRACE_MAX );
2020} m_ret SEC (".maps" );
2121
22+ #ifdef STACK_TRACE
23+ struct {
24+ __uint (type , BPF_MAP_TYPE_STACK_TRACE );
25+ __uint (max_entries , 16384 );
26+ __uint (key_size , sizeof (__u32 ));
27+ __uint (value_size , sizeof (stack_trace_t ));
28+ } m_stack SEC (".maps" );
29+ #endif
30+
2231#ifdef KERN_VER
2332__u32 kern_ver SEC ("version" ) = KERN_VER ;
2433#endif
@@ -64,8 +73,38 @@ static try_inline int put_ret(int func)
6473 return 0 ;
6574}
6675
67- static try_inline int handle_entry (void * regs , struct sk_buff * skb , event_t * e ,
68- int size , int func )
76+ #ifdef STACK_TRACE
77+ static try_inline void try_trace_stack (void * regs , bpf_args_t * bpf_args ,
78+ event_t * e , int func )
79+ {
80+ int i = 0 , key ;
81+ u16 * funcs ;
82+
83+ if (!ARGS_GET (stack ))
84+ return ;
85+
86+ funcs = ARGS_GET (stack_funs );
87+
88+ #pragma unroll
89+ for (; i < MAX_FUNC_STACK ; i ++ ) {
90+ if (!funcs [i ])
91+ break ;
92+ if (funcs [i ] == func )
93+ goto do_stack ;
94+ }
95+ return ;
96+
97+ do_stack :
98+ key = bpf_get_stackid (regs , & m_stack , 0 );
99+ e -> stack_id = key ;
100+ }
101+ #else
102+ static try_inline void try_trace_stack (void * regs , bpf_args_t * bpf_args ,
103+ event_t * e , int func ) { }
104+ #endif
105+
106+ static try_inline int handle_entry (void * regs , struct sk_buff * skb ,
107+ event_t * e , int size , int func )
69108{
70109 packet_t * pkt = & e -> pkt ;
71110 bool * matched ;
@@ -111,6 +150,7 @@ static try_inline int handle_entry(void *regs, struct sk_buff *skb, event_t *e,
111150 }
112151
113152out :
153+ try_trace_stack (regs , bpf_args , e , func );
114154 pkt -> ts = bpf_ktime_get_ns ();
115155 e -> key = (u64 )(void * )skb ;
116156
@@ -129,8 +169,8 @@ static try_inline int handle_destroy(struct sk_buff *skb)
129169}
130170
131171static try_inline int default_handle_entry (struct pt_regs * ctx ,
132- struct sk_buff * skb ,
133- int func )
172+ struct sk_buff * skb ,
173+ int func )
134174{
135175 if (ARGS_GET_CONFIG (detail )) {
136176 detail_event_t e = { .func = func };
0 commit comments