Skip to content

Commit 63baf70

Browse files
authored
Add lint check workflow & format all source files (#76)
* update: add lint script * fix: errors in format script * update: disable `ReflowComments` * update: add linting workflow * fix: disable sorting includes * format all source files * use `clang-format-lint-action` to lint * update: auto commit lint results * fix workflow file syntax * fix: lint * mess a file and test CI * Committing clang-format changes
1 parent af40de8 commit 63baf70

34 files changed

+1515
-1460
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{BasedOnStyle: Chromium, IndentWidth: 4}
1+
{BasedOnStyle: Chromium, IndentWidth: 4, ReflowComments: false, SortIncludes: "Never"}

.github/workflows/c-cpp-lint.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: "c-cpp-lint"
2+
3+
on:
4+
push:
5+
branches: "main"
6+
pull_request:
7+
branches: "*"
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: DoozyX/[email protected]
15+
with:
16+
source: 'examples wasm-sdk runtime/cpp/include runtime/cpp/test runtime/cpp/src'
17+
extensions: 'h,cpp,c,hpp,cxx,hxx'
18+
clangFormatVersion: 14
19+
inplace: True
20+
- uses: EndBug/add-and-commit@v4
21+
with:
22+
message: 'Committing clang-format changes'
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

examples/bootstrap/bootstrap.bpf.c

+87-88
Original file line numberDiff line numberDiff line change
@@ -9,103 +9,102 @@
99
char LICENSE[] SEC("license") = "Dual BSD/GPL";
1010

1111
struct {
12-
__uint(type, BPF_MAP_TYPE_HASH);
13-
__uint(max_entries, 8192);
14-
__type(key, pid_t);
15-
__type(value, u64);
12+
__uint(type, BPF_MAP_TYPE_HASH);
13+
__uint(max_entries, 8192);
14+
__type(key, pid_t);
15+
__type(value, u64);
1616
} exec_start SEC(".maps");
1717

1818
struct {
19-
__uint(type, BPF_MAP_TYPE_RINGBUF);
20-
__uint(max_entries, 256 * 1024);
19+
__uint(type, BPF_MAP_TYPE_RINGBUF);
20+
__uint(max_entries, 256 * 1024);
2121
} rb SEC(".maps");
2222

2323
const volatile unsigned long long min_duration_ns = 0;
2424

2525
SEC("tp/sched/sched_process_exec")
26-
int handle_exec(struct trace_event_raw_sched_process_exec *ctx)
27-
{
28-
struct task_struct *task;
29-
unsigned fname_off;
30-
struct event *e;
31-
pid_t pid;
32-
u64 ts;
33-
34-
/* remember time exec() was executed for this PID */
35-
pid = bpf_get_current_pid_tgid() >> 32;
36-
ts = bpf_ktime_get_ns();
37-
bpf_map_update_elem(&exec_start, &pid, &ts, BPF_ANY);
38-
39-
/* don't emit exec events when minimum duration is specified */
40-
if (min_duration_ns)
41-
return 0;
42-
43-
/* reserve sample from BPF ringbuf */
44-
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
45-
if (!e)
46-
return 0;
47-
48-
/* fill out the sample with data */
49-
task = (struct task_struct *)bpf_get_current_task();
50-
51-
e->exit_event = false;
52-
e->pid = pid;
53-
e->ppid = BPF_CORE_READ(task, real_parent, tgid);
54-
bpf_get_current_comm(&e->comm, sizeof(e->comm));
55-
56-
fname_off = ctx->__data_loc_filename & 0xFFFF;
57-
bpf_probe_read_str(&e->filename, sizeof(e->filename), (void *)ctx + fname_off);
58-
59-
/* successfully submit it to user-space for post-processing */
60-
bpf_ringbuf_submit(e, 0);
61-
return 0;
26+
int handle_exec(struct trace_event_raw_sched_process_exec* ctx) {
27+
struct task_struct* task;
28+
unsigned fname_off;
29+
struct event* e;
30+
pid_t pid;
31+
u64 ts;
32+
33+
/* remember time exec() was executed for this PID */
34+
pid = bpf_get_current_pid_tgid() >> 32;
35+
ts = bpf_ktime_get_ns();
36+
bpf_map_update_elem(&exec_start, &pid, &ts, BPF_ANY);
37+
38+
/* don't emit exec events when minimum duration is specified */
39+
if (min_duration_ns)
40+
return 0;
41+
42+
/* reserve sample from BPF ringbuf */
43+
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
44+
if (!e)
45+
return 0;
46+
47+
/* fill out the sample with data */
48+
task = (struct task_struct*)bpf_get_current_task();
49+
50+
e->exit_event = false;
51+
e->pid = pid;
52+
e->ppid = BPF_CORE_READ(task, real_parent, tgid);
53+
bpf_get_current_comm(&e->comm, sizeof(e->comm));
54+
55+
fname_off = ctx->__data_loc_filename & 0xFFFF;
56+
bpf_probe_read_str(&e->filename, sizeof(e->filename),
57+
(void*)ctx + fname_off);
58+
59+
/* successfully submit it to user-space for post-processing */
60+
bpf_ringbuf_submit(e, 0);
61+
return 0;
6262
}
6363

6464
SEC("tp/sched/sched_process_exit")
65-
int handle_exit(struct trace_event_raw_sched_process_template* ctx)
66-
{
67-
struct task_struct *task;
68-
struct event *e;
69-
pid_t pid, tid;
70-
u64 id, ts, *start_ts, duration_ns = 0;
71-
72-
/* get PID and TID of exiting thread/process */
73-
id = bpf_get_current_pid_tgid();
74-
pid = id >> 32;
75-
tid = (u32)id;
76-
77-
/* ignore thread exits */
78-
if (pid != tid)
79-
return 0;
80-
81-
/* if we recorded start of the process, calculate lifetime duration */
82-
start_ts = bpf_map_lookup_elem(&exec_start, &pid);
83-
if (start_ts)
84-
duration_ns = bpf_ktime_get_ns() - *start_ts;
85-
else if (min_duration_ns)
86-
return 0;
87-
bpf_map_delete_elem(&exec_start, &pid);
88-
89-
/* if process didn't live long enough, return early */
90-
if (min_duration_ns && duration_ns < min_duration_ns)
91-
return 0;
92-
93-
/* reserve sample from BPF ringbuf */
94-
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
95-
if (!e)
96-
return 0;
97-
98-
/* fill out the sample with data */
99-
task = (struct task_struct *)bpf_get_current_task();
100-
101-
e->exit_event = true;
102-
e->duration_ns = duration_ns;
103-
e->pid = pid;
104-
e->ppid = BPF_CORE_READ(task, real_parent, tgid);
105-
e->exit_code = (BPF_CORE_READ(task, exit_code) >> 8) & 0xff;
106-
bpf_get_current_comm(&e->comm, sizeof(e->comm));
107-
108-
/* send data to user-space for post-processing */
109-
bpf_ringbuf_submit(e, 0);
110-
return 0;
65+
int handle_exit(struct trace_event_raw_sched_process_template* ctx) {
66+
struct task_struct* task;
67+
struct event* e;
68+
pid_t pid, tid;
69+
u64 id, ts, *start_ts, duration_ns = 0;
70+
71+
/* get PID and TID of exiting thread/process */
72+
id = bpf_get_current_pid_tgid();
73+
pid = id >> 32;
74+
tid = (u32)id;
75+
76+
/* ignore thread exits */
77+
if (pid != tid)
78+
return 0;
79+
80+
/* if we recorded start of the process, calculate lifetime duration */
81+
start_ts = bpf_map_lookup_elem(&exec_start, &pid);
82+
if (start_ts)
83+
duration_ns = bpf_ktime_get_ns() - *start_ts;
84+
else if (min_duration_ns)
85+
return 0;
86+
bpf_map_delete_elem(&exec_start, &pid);
87+
88+
/* if process didn't live long enough, return early */
89+
if (min_duration_ns && duration_ns < min_duration_ns)
90+
return 0;
91+
92+
/* reserve sample from BPF ringbuf */
93+
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
94+
if (!e)
95+
return 0;
96+
97+
/* fill out the sample with data */
98+
task = (struct task_struct*)bpf_get_current_task();
99+
100+
e->exit_event = true;
101+
e->duration_ns = duration_ns;
102+
e->pid = pid;
103+
e->ppid = BPF_CORE_READ(task, real_parent, tgid);
104+
e->exit_code = (BPF_CORE_READ(task, exit_code) >> 8) & 0xff;
105+
bpf_get_current_comm(&e->comm, sizeof(e->comm));
106+
107+
/* send data to user-space for post-processing */
108+
bpf_ringbuf_submit(e, 0);
109+
return 0;
111110
}

examples/bootstrap/bootstrap.c

+16-22
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ static struct env {
1111
long min_duration_ms;
1212
} env;
1313

14-
const char *argp_program_version = "bootstrap 0.0";
15-
const char *argp_program_bug_address = "<[email protected]>";
14+
const char* argp_program_version = "bootstrap 0.0";
15+
const char* argp_program_bug_address = "<[email protected]>";
1616
const char argp_program_doc[] =
1717
"BPF bootstrap demo application.\n"
1818
"\n"
@@ -21,18 +21,14 @@ const char argp_program_doc[] =
2121
"\n"
2222
"USAGE: ./bootstrap [-d <min-duration-ms>] -v\n";
2323

24-
static void
25-
print_usage(void)
26-
{
24+
static void print_usage(void) {
2725
printf("%s\n", argp_program_version);
2826
printf("%s\n", argp_program_doc);
2927
}
3028

31-
static int
32-
handle_event(void *ctx, void *data, size_t data_sz)
33-
{
34-
const struct event *e = data;
35-
struct tm *tm;
29+
static int handle_event(void* ctx, void* data, size_t data_sz) {
30+
const struct event* e = data;
31+
struct tm* tm;
3632
char ts[32];
3733
time_t t;
3834

@@ -46,8 +42,7 @@ handle_event(void *ctx, void *data, size_t data_sz)
4642
if (e->duration_ns)
4743
printf(" (%llums)", e->duration_ns / 1000000);
4844
printf("\n");
49-
}
50-
else {
45+
} else {
5146
printf("%-8s %-5s %-16s %-7d %-7d %s\n", ts, "EXEC", e->comm, e->pid,
5247
e->ppid, e->filename);
5348
}
@@ -57,20 +52,19 @@ handle_event(void *ctx, void *data, size_t data_sz)
5752

5853
static bool exiting = false;
5954

60-
int
61-
main(int argc, char **argv)
62-
{
63-
struct bpf_buffer *rb = NULL;
64-
struct bootstrap_bpf *skel;
55+
int main(int argc, char** argv) {
56+
struct bpf_buffer* rb = NULL;
57+
struct bootstrap_bpf* skel;
6558
int err;
6659

67-
// parse the args manually for demo purpose
68-
if (argc > 3 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
60+
// parse the args manually for demo purpose
61+
if (argc > 3 || strcmp(argv[1], "-h") == 0 ||
62+
strcmp(argv[1], "--help") == 0) {
6963
print_usage();
7064
return 0;
71-
}
72-
else if ((strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--duration") == 0)
73-
&& argc == 3) {
65+
} else if ((strcmp(argv[1], "-d") == 0 ||
66+
strcmp(argv[1], "--duration") == 0) &&
67+
argc == 3) {
7468
env.min_duration_ms = strtol(argv[2], NULL, 10);
7569
}
7670

examples/bootstrap/bootstrap.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#define MAX_FILENAME_LEN 127
88

99
struct event {
10-
int pid;
11-
int ppid;
12-
unsigned exit_code;
13-
unsigned long long duration_ns;
14-
char comm[TASK_COMM_LEN];
15-
char filename[MAX_FILENAME_LEN];
16-
char exit_event;
10+
int pid;
11+
int ppid;
12+
unsigned exit_code;
13+
unsigned long long duration_ns;
14+
char comm[TASK_COMM_LEN];
15+
char filename[MAX_FILENAME_LEN];
16+
char exit_event;
1717
};
1818

1919
#endif /* __BOOTSTRAP_H */

examples/bootstrap/bootstrap.wasm.h

-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@ struct event {
1919
} __attribute__((packed));
2020
static_assert(sizeof(struct event) == 168, "Size of event is not 168");
2121

22-
2322
#endif /* __VMLINUX_H__ */
24-

examples/execve/execve.bpf.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ struct {
1212
struct execve_args {
1313
struct trace_entry common;
1414
int unused;
15-
char *file;
16-
char *const *argv;
17-
char *const *envp;
15+
char* file;
16+
char* const* argv;
17+
char* const* envp;
1818
};
1919

2020
SEC("tp/syscalls/sys_enter_execve")
21-
int sys_enter_execve(struct execve_args *ctx) {
21+
int sys_enter_execve(struct execve_args* ctx) {
2222
struct comm_event comm;
2323
comm.pid = (int)(bpf_get_current_pid_tgid() & 0xFFFFFFFF);
2424
bpf_get_current_comm(&(comm.parent_proc[0]), sizeof(comm.parent_proc));
@@ -27,7 +27,7 @@ int sys_enter_execve(struct execve_args *ctx) {
2727
int start = 0;
2828
int end = COMM_SIZE - 1;
2929

30-
char *args[MAX_ARG_NUM];
30+
char* args[MAX_ARG_NUM];
3131
int idx = 0;
3232
for (; idx < MAX_ARG_NUM; idx++) {
3333
if (bpf_probe_read_user(&args[idx], sizeof(args[idx]),

examples/execve/execve.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include "execve.h"
66
static int handle_event(void* ctx, void* data, size_t data_sz) {
77
struct comm_event* st = (struct comm_event*)data;
8-
printf("[%d] %s -> %s\n", st->pid, st->parent_proc,
9-
st->command);
8+
printf("[%d] %s -> %s\n", st->pid, st->parent_proc, st->command);
109
return 0;
1110
}
1211

examples/go-execve/execve.bpf.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ struct {
1212
struct execve_args {
1313
struct trace_entry common;
1414
int unused;
15-
char *file;
16-
char *const *argv;
17-
char *const *envp;
15+
char* file;
16+
char* const* argv;
17+
char* const* envp;
1818
};
1919

2020
SEC("tp/syscalls/sys_enter_execve")
21-
int sys_enter_execve(struct execve_args *ctx) {
21+
int sys_enter_execve(struct execve_args* ctx) {
2222
struct comm_event comm;
2323
comm.pid = (int)(bpf_get_current_pid_tgid() & 0xFFFFFFFF);
2424
bpf_get_current_comm(&(comm.parent_proc[0]), sizeof(comm.parent_proc));
@@ -27,7 +27,7 @@ int sys_enter_execve(struct execve_args *ctx) {
2727
int start = 0;
2828
int end = COMM_SIZE - 1;
2929

30-
char *args[MAX_ARG_NUM];
30+
char* args[MAX_ARG_NUM];
3131
int idx = 0;
3232
for (; idx < MAX_ARG_NUM; idx++) {
3333
if (bpf_probe_read_user(&args[idx], sizeof(args[idx]),

0 commit comments

Comments
 (0)