Skip to content

Commit 898122a

Browse files
committed
FIXME: handling trace_seq and seq_buffer changes
check struct defintion rather than KERNLE_VERSION_CODE to handle structure changes in kernel v6.8/v3.19/v2.6.33/... Signed-off-by: shenping.matt <shenping.matt@bytedance.com>
1 parent 478196c commit 898122a

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

driver/LKM/Makefile

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ obj-m += $(MODULE_NAME).o
1010
ccflags-y += -I$(MODULE_DIR)/include -I$(MODULE_DIR)
1111

1212
K_I_PATH := .
13+
ifneq ($(wildcard $(srctree)/include/.),)
14+
K_I_PATH += $(srctree)/include
15+
endif
16+
1317
ifneq ($(wildcard /usr/src/kernels/$(KERNEL_HEAD)/include/.),)
1418
K_I_PATH += /usr/src/kernels/$(KERNEL_HEAD)/include
1519
endif
@@ -67,10 +71,22 @@ ccflags-y += -D SMITH_TRACE_EVENTS
6771
endif
6872

6973
TRACE_SEQ_FILES := $(shell find -L $(K_I_PATH) -path \*/linux/trace_seq.h) /dev/null
70-
TRACE_SEQ_SEQ := $(shell sh -c "grep -s trace_seq_used $(TRACE_SEQ_FILES)")
74+
TRACE_SEQ_SEQ := $(shell sh -c "grep -s 'struct trace_seq {' -A5 $(TRACE_SEQ_FILES) | grep 'seq;'")
7175
ifneq ($(TRACE_SEQ_SEQ),)
7276
ccflags-y += -D SMITH_TRACE_SEQ
7377
endif
78+
TRACE_SEQ_READPOS := $(shell sh -c "grep -s 'struct trace_seq {' -A5 $(TRACE_SEQ_FILES) | grep 'readpos;'")
79+
ifneq ($(TRACE_SEQ_READPOS),)
80+
ccflags-y += -D SMITH_TRACE_READPOS
81+
endif
82+
TRACE_SEQ_FULL := $(shell sh -c "grep -s 'struct trace_seq {' -A5 $(TRACE_SEQ_FILES) | grep 'full;'")
83+
ifneq ($(TRACE_SEQ_FULL),)
84+
ccflags-y += -D SMITH_TRACE_FULL
85+
endif
86+
TRACE_SEQ_LEN := $(shell sh -c "grep -s 'struct trace_seq {' -A5 $(TRACE_SEQ_FILES) | grep 'len;'")
87+
ifneq ($(TRACE_SEQ_LEN),)
88+
ccflags-y += -D SMITH_TRACE_LEN
89+
endif
7490

7591
FS_H_FILES := $(shell find -L $(K_I_PATH) -path "*/linux/fs.h") /dev/null
7692
FS_OP_ITERATE := $(shell sh -c "grep -s \(\*iterate\) $(FS_H_FILES)")
@@ -117,7 +133,15 @@ endif
117133
else
118134

119135
MODULE_DIR := $(shell pwd)
120-
KERNEL_DIR := $(if $(wildcard /usr/src/kernels/$(KERNEL_HEAD)),/usr/src/kernels/$(KERNEL_HEAD),/lib/modules/$(KERNEL_HEAD)/build)
136+
ifneq ($(wildcard /lib/modules/$(KERNEL_HEAD)/build),)
137+
KERNEL_DIR := /lib/modules/$(KERNEL_HEAD)/build
138+
else ifneq ($(wildcard /usr/src/kernels/$(KERNEL_HEAD)),)
139+
KERNEL_DIR := /usr/src/kernels/$(KERNEL_HEAD)
140+
else ifneq ($(wildcard /usr/src/$(KERNEL_HEAD)),)
141+
KERNEL_DIR := /usr/src/$(KERNEL_HEAD)
142+
else
143+
$(error Kernel header files related to $(KERNEL_HEAD) not found)
144+
endif
121145

122146
all:
123147
@echo "|-----------------------------------|"

driver/LKM/include/trace.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define SZ_128K 0x00020000
2424

2525

26-
#ifdef SMITH_TRACE_SEQ
26+
#ifdef SMITH_TRACE_SEQ /* from kernel v3.19 */
2727
static inline int __trace_seq_used(struct trace_seq *s)
2828
{
2929
return trace_seq_used(s);
@@ -48,15 +48,16 @@ static inline int __trace_seq_used(struct trace_seq *s)
4848
{
4949
return min(s->len, (unsigned int)(PAGE_SIZE - 1));
5050
}
51-
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0)
51+
52+
#ifdef SMITH_TRACE_FULL /* fromm 2.6.33 */
5253
static inline bool __trace_seq_has_overflowed(struct trace_seq *s)
5354
{
54-
return s->len > PAGE_SIZE - 1;
55+
return s->full || s->len > PAGE_SIZE - 1;
5556
}
5657
#else
5758
static inline bool __trace_seq_has_overflowed(struct trace_seq *s)
5859
{
59-
return s->full || s->len > PAGE_SIZE - 1;
60+
return s->len > PAGE_SIZE - 1;
6061
}
6162
#endif
6263

@@ -70,7 +71,7 @@ static inline enum print_line_t __trace_handle_return(struct trace_seq *s)
7071
return __trace_seq_has_overflowed(s) ?
7172
TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
7273
}
73-
#endif
74+
#endif /* SMITH_TRACE_SEQ */
7475

7576
/*
7677
* The print entry - the most basic unit of tracing.

driver/LKM/src/trace.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static ssize_t(*trace_seq_to_user_sym) (struct trace_seq * s,
4141
#define trace_seq_to_user_sym trace_seq_to_user
4242
#endif
4343

44-
static int kallsyms_lookup_symbols(void)
44+
static int trace_lookup_symbols(void)
4545
{
4646
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
4747
void *ptr = (void *)smith_kallsyms_lookup_name("trace_seq_to_user");
@@ -278,18 +278,18 @@ static ssize_t trace_read_pipe(struct file *filp, char __user * ubuf,
278278
mutex_lock(&access_lock);
279279
while (trace_next_entry_inc(iter) != NULL) {
280280
enum print_line_t ret;
281-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
282-
int save_len = iter->seq.seq.len;
283-
#else
281+
#ifdef SMITH_TRACE_LEN
284282
int save_len = iter->seq.len;
283+
#else
284+
int save_len = iter->seq.seq.len;
285285
#endif
286286
ret = print_trace_fmt_line(iter);
287287
if (ret == TRACE_TYPE_PARTIAL_LINE) {
288288
/* don't print partial lines */
289-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
290-
iter->seq.seq.len = save_len;
291-
#else
289+
#ifdef SMITH_TRACE_LEN
292290
iter->seq.len = save_len;
291+
#else
292+
iter->seq.seq.len = save_len;
293293
#endif
294294
break;
295295
}
@@ -304,18 +304,18 @@ static ssize_t trace_read_pipe(struct file *filp, char __user * ubuf,
304304
* size and we should leave by partial output condition above.
305305
* One of the trace_seq_* functions is not used properly.
306306
*/
307-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
307+
#ifdef SMITH_TRACE_FULL
308308
WARN_ONCE(iter->seq.full, "full flag set for trace id: %d", iter->ent->id);
309309
#endif
310310
}
311311
mutex_unlock(&access_lock);
312312

313313
/* Now copy what we have to the user */
314314
sret = trace_seq_to_user_sym(&iter->seq, ubuf, cnt);
315-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
316-
if (iter->seq.seq.readpos >= __trace_seq_used(&iter->seq))
317-
#else
315+
#ifdef SMITH_TRACE_READPOS
318316
if (iter->seq.readpos >= __trace_seq_used(&iter->seq))
317+
#else
318+
if (iter->seq.seq.readpos >= __trace_seq_used(&iter->seq))
319319
#endif
320320
trace_seq_init(&iter->seq);
321321

@@ -399,7 +399,7 @@ static int __init print_event_init(void)
399399
if (num_class >= PRINT_EVENT_ID_MAX)
400400
return -EINVAL;
401401

402-
if (kallsyms_lookup_symbols())
402+
if (trace_lookup_symbols())
403403
return -ENODEV;
404404

405405
trace_ring = tb_alloc(RB_BUFFER_SIZE, TB_FL_OVERWRITE);

0 commit comments

Comments
 (0)