Skip to content

Commit 500eba1

Browse files
unikzforceanakryiko
authored andcommitted
examples/c: add support for clang-format
Add support for clang-format so the code style would be consistent throughout the whole project for C examples. Signed-off-by: Hosein Bakhtiari <[email protected]>
1 parent 6ee3b62 commit 500eba1

18 files changed

+232
-97
lines changed

.clang-format

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
#
3+
# clang-format configuration file. Intended for clang-format >= 11.
4+
#
5+
# For more information, see:
6+
#
7+
# Documentation/process/clang-format.rst
8+
# https://clang.llvm.org/docs/ClangFormat.html
9+
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
10+
#
11+
---
12+
AccessModifierOffset: -4
13+
AlignAfterOpenBracket: Align
14+
AlignConsecutiveAssignments: false
15+
AlignConsecutiveDeclarations: false
16+
AlignEscapedNewlines: Left
17+
AlignOperands: true
18+
AlignTrailingComments: false
19+
AllowAllParametersOfDeclarationOnNextLine: false
20+
AllowShortBlocksOnASingleLine: false
21+
AllowShortCaseLabelsOnASingleLine: false
22+
AllowShortFunctionsOnASingleLine: None
23+
AllowShortIfStatementsOnASingleLine: false
24+
AllowShortLoopsOnASingleLine: false
25+
AlwaysBreakAfterDefinitionReturnType: None
26+
AlwaysBreakAfterReturnType: None
27+
AlwaysBreakBeforeMultilineStrings: false
28+
AlwaysBreakTemplateDeclarations: false
29+
BinPackArguments: true
30+
BinPackParameters: true
31+
BraceWrapping:
32+
AfterClass: false
33+
AfterControlStatement: false
34+
AfterEnum: false
35+
AfterFunction: true
36+
AfterNamespace: true
37+
AfterObjCDeclaration: false
38+
AfterStruct: false
39+
AfterUnion: false
40+
AfterExternBlock: false
41+
BeforeCatch: false
42+
BeforeElse: false
43+
IndentBraces: false
44+
SplitEmptyFunction: true
45+
SplitEmptyRecord: true
46+
SplitEmptyNamespace: true
47+
BreakBeforeBinaryOperators: None
48+
BreakBeforeBraces: Custom
49+
BreakBeforeInheritanceComma: false
50+
BreakBeforeTernaryOperators: false
51+
BreakConstructorInitializersBeforeComma: false
52+
BreakConstructorInitializers: BeforeComma
53+
BreakAfterJavaFieldAnnotations: false
54+
BreakStringLiterals: false
55+
ColumnLimit: 100
56+
CommentPragmas: '^ IWYU pragma:'
57+
CompactNamespaces: false
58+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
59+
ConstructorInitializerIndentWidth: 8
60+
ContinuationIndentWidth: 8
61+
Cpp11BracedListStyle: false
62+
DerivePointerAlignment: false
63+
DisableFormat: false
64+
ExperimentalAutoDetectBinPacking: false
65+
FixNamespaceComments: false
66+
67+
ForEachMacros:
68+
- 'bpf__perf_for_each_map'
69+
- 'bpf__perf_for_each_map_named'
70+
- 'bpf_for_each_spilled_reg'
71+
- 'bpf_object__for_each_map'
72+
- 'bpf_object__for_each_program'
73+
- 'bpf_object__for_each_safe'
74+
- 'bpf_perf_object__for_each'
75+
- 'for_each_btf_ext_rec'
76+
- 'for_each_btf_ext_sec'
77+
- 'hashmap__for_each_entry'
78+
- 'hashmap__for_each_entry_safe'
79+
- 'hashmap__for_each_key_entry'
80+
- 'hashmap__for_each_key_entry_safe'
81+
- 'list_for_each_entry'
82+
- 'libbpf_nla_for_each_attr'
83+
- 'list_for_each_entry'
84+
- 'list_for_each_entry_continue'
85+
- 'list_for_each_entry_continue_rcu'
86+
- 'list_for_each_entry_continue_reverse'
87+
- 'list_for_each_entry_from'
88+
- 'list_for_each_entry_from_rcu'
89+
- 'list_for_each_entry_from_reverse'
90+
- 'list_for_each_entry_lockless'
91+
- 'list_for_each_entry_rcu'
92+
- 'list_for_each_entry_reverse'
93+
- 'list_for_each_entry_safe'
94+
- 'list_for_each_entry_safe_continue'
95+
- 'list_for_each_entry_safe_from'
96+
- 'list_for_each_entry_safe_reverse'
97+
- 'list_for_each_entry_srcu'
98+
99+
100+
IncludeBlocks: Preserve
101+
IncludeCategories:
102+
- Regex: '.*'
103+
Priority: 1
104+
IncludeIsMainRegex: '(Test)?$'
105+
IndentCaseLabels: false
106+
IndentGotoLabels: false
107+
IndentPPDirectives: None
108+
IndentWidth: 8
109+
IndentWrappedFunctionNames: false
110+
JavaScriptQuotes: Leave
111+
JavaScriptWrapImports: true
112+
KeepEmptyLinesAtTheStartOfBlocks: false
113+
MacroBlockBegin: ''
114+
MacroBlockEnd: ''
115+
MaxEmptyLinesToKeep: 1
116+
NamespaceIndentation: None
117+
ObjCBinPackProtocolList: Auto
118+
ObjCBlockIndentWidth: 8
119+
ObjCSpaceAfterProperty: true
120+
ObjCSpaceBeforeProtocolList: true
121+
122+
# Taken from git's rules
123+
PenaltyBreakAssignment: 10
124+
PenaltyBreakBeforeFirstCallParameter: 30
125+
PenaltyBreakComment: 10
126+
PenaltyBreakFirstLessLess: 0
127+
PenaltyBreakString: 10
128+
PenaltyExcessCharacter: 100
129+
PenaltyReturnTypeOnItsOwnLine: 60
130+
131+
PointerAlignment: Right
132+
ReflowComments: false
133+
SortIncludes: false
134+
SortUsingDeclarations: false
135+
SpaceAfterCStyleCast: false
136+
SpaceAfterTemplateKeyword: true
137+
SpaceBeforeAssignmentOperators: true
138+
SpaceBeforeCtorInitializerColon: true
139+
SpaceBeforeInheritanceColon: true
140+
SpaceBeforeParens: ControlStatementsExceptForEachMacros
141+
SpaceBeforeRangeBasedForLoopColon: true
142+
SpaceInEmptyParentheses: false
143+
SpacesBeforeTrailingComments: 1
144+
SpacesInAngles: false
145+
SpacesInContainerLiterals: false
146+
SpacesInCStyleCastParentheses: false
147+
SpacesInParentheses: false
148+
SpacesInSquareBrackets: false
149+
Standard: Cpp03
150+
TabWidth: 8
151+
UseTab: Always
152+
153+
AlignConsecutiveMacros: AcrossEmptyLines

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/build
22
/examples/c/build
33
/examples/c/.xmake
4+
/.idea

examples/c/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
/sockfilter
1111
/tc
1212
/ksyscall
13+
/cmake-build-debug/
14+
/cmake-build-release/

examples/c/bootstrap.bpf.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ int handle_exec(struct trace_event_raw_sched_process_exec *ctx)
6262
}
6363

6464
SEC("tp/sched/sched_process_exit")
65-
int handle_exit(struct trace_event_raw_sched_process_template* ctx)
65+
int handle_exit(struct trace_event_raw_sched_process_template *ctx)
6666
{
6767
struct task_struct *task;
6868
struct event *e;
6969
pid_t pid, tid;
7070
u64 id, ts, *start_ts, duration_ns = 0;
71-
71+
7272
/* get PID and TID of exiting thread/process */
7373
id = bpf_get_current_pid_tgid();
7474
pid = id >> 32;
@@ -109,4 +109,3 @@ int handle_exit(struct trace_event_raw_sched_process_template* ctx)
109109
bpf_ringbuf_submit(e, 0);
110110
return 0;
111111
}
112-

examples/c/bootstrap.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ static struct env {
1616

1717
const char *argp_program_version = "bootstrap 0.0";
1818
const char *argp_program_bug_address = "<[email protected]>";
19-
const char argp_program_doc[] =
20-
"BPF bootstrap demo application.\n"
21-
"\n"
22-
"It traces process start and exits and shows associated \n"
23-
"information (filename, process duration, PID and PPID, etc).\n"
24-
"\n"
25-
"USAGE: ./bootstrap [-d <min-duration-ms>] [-v]\n";
19+
const char argp_program_doc[] = "BPF bootstrap demo application.\n"
20+
"\n"
21+
"It traces process start and exits and shows associated \n"
22+
"information (filename, process duration, PID and PPID, etc).\n"
23+
"\n"
24+
"USAGE: ./bootstrap [-d <min-duration-ms>] [-v]\n";
2625

2726
static const struct argp_option opts[] = {
2827
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
@@ -85,14 +84,14 @@ static int handle_event(void *ctx, void *data, size_t data_sz)
8584
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
8685

8786
if (e->exit_event) {
88-
printf("%-8s %-5s %-16s %-7d %-7d [%u]",
89-
ts, "EXIT", e->comm, e->pid, e->ppid, e->exit_code);
87+
printf("%-8s %-5s %-16s %-7d %-7d [%u]", ts, "EXIT", e->comm, e->pid, e->ppid,
88+
e->exit_code);
9089
if (e->duration_ns)
9190
printf(" (%llums)", e->duration_ns / 1000000);
9291
printf("\n");
9392
} else {
94-
printf("%-8s %-5s %-16s %-7d %-7d %s\n",
95-
ts, "EXEC", e->comm, e->pid, e->ppid, e->filename);
93+
printf("%-8s %-5s %-16s %-7d %-7d %s\n", ts, "EXEC", e->comm, e->pid, e->ppid,
94+
e->filename);
9695
}
9796

9897
return 0;
@@ -149,8 +148,8 @@ int main(int argc, char **argv)
149148
}
150149

151150
/* Process events */
152-
printf("%-8s %-5s %-16s %-7s %-7s %s\n",
153-
"TIME", "EVENT", "COMM", "PID", "PPID", "FILENAME/EXIT CODE");
151+
printf("%-8s %-5s %-16s %-7s %-7s %s\n", "TIME", "EVENT", "COMM", "PID", "PPID",
152+
"FILENAME/EXIT CODE");
154153
while (!exiting) {
155154
err = ring_buffer__poll(rb, 100 /* timeout, ms */);
156155
/* Ctrl-C will cause -EINTR */

examples/c/bootstrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifndef __BOOTSTRAP_H
44
#define __BOOTSTRAP_H
55

6-
#define TASK_COMM_LEN 16
6+
#define TASK_COMM_LEN 16
77
#define MAX_FILENAME_LEN 127
88

99
struct event {

examples/c/ksyscall.bpf.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define TASK_COMM_LEN 16
77

88
SEC("ksyscall/tgkill")
9-
int BPF_KSYSCALL(tgkill_entry, pid_t tgid, pid_t tid, int sig)
9+
int BPF_KSYSCALL(tgkill_entry, pid_t tgid, pid_t tid, int sig)
1010
{
1111
char comm[TASK_COMM_LEN];
1212
__u32 caller_pid = bpf_get_current_pid_tgid() >> 32;
@@ -22,13 +22,14 @@ int BPF_KSYSCALL(tgkill_entry, pid_t tgid, pid_t tid, int sig)
2222
}
2323

2424
bpf_get_current_comm(&comm, sizeof(comm));
25-
bpf_printk("tgkill syscall called by PID %d (%s) for thread id %d with pid %d and signal %d.",
26-
caller_pid, comm, tid, tgid, sig);
25+
bpf_printk(
26+
"tgkill syscall called by PID %d (%s) for thread id %d with pid %d and signal %d.",
27+
caller_pid, comm, tid, tgid, sig);
2728
return 0;
2829
}
2930

3031
SEC("ksyscall/kill")
31-
int BPF_KSYSCALL(entry_probe, pid_t pid, int sig)
32+
int BPF_KSYSCALL(entry_probe, pid_t pid, int sig)
3233
{
3334
char comm[TASK_COMM_LEN];
3435
__u32 caller_pid = bpf_get_current_pid_tgid() >> 32;
@@ -44,8 +45,8 @@ int BPF_KSYSCALL(entry_probe, pid_t pid, int sig)
4445
}
4546

4647
bpf_get_current_comm(&comm, sizeof(comm));
47-
bpf_printk("KILL syscall called by PID %d (%s) for PID %d with signal %d.",
48-
caller_pid, comm, pid, sig);
48+
bpf_printk("KILL syscall called by PID %d (%s) for PID %d with signal %d.", caller_pid,
49+
comm, pid, sig);
4950
return 0;
5051
}
5152

examples/c/minimal_legacy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ int main(int argc, char **argv)
2929

3030
/* ensure BPF program only handles write() syscalls from our process */
3131
pid = getpid();
32-
err = bpf_map__update_elem(skel->maps.my_pid_map, &index, sizeof(index), &pid, sizeof(pid_t), BPF_ANY);
32+
err = bpf_map__update_elem(skel->maps.my_pid_map, &index, sizeof(index), &pid,
33+
sizeof(pid_t), BPF_ANY);
3334
if (err < 0) {
3435
fprintf(stderr, "Error updating map with pid: %s\n", strerror(err));
3536
goto cleanup;

examples/c/profile.bpf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ int profile(void *ctx)
3434

3535
event->kstack_sz = bpf_get_stack(ctx, event->kstack, sizeof(event->kstack), 0);
3636

37-
event->ustack_sz = bpf_get_stack(ctx, event->ustack, sizeof(event->ustack), BPF_F_USER_STACK);
37+
event->ustack_sz =
38+
bpf_get_stack(ctx, event->ustack, sizeof(event->ustack), BPF_F_USER_STACK);
3839

3940
bpf_ringbuf_submit(event, 0);
4041

examples/c/profile.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*/
2222
extern int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
2323

24-
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
25-
int cpu, int group_fd, unsigned long flags)
24+
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd,
25+
unsigned long flags)
2626
{
2727
int ret;
2828

@@ -59,13 +59,11 @@ static void show_stack_trace(__u64 *stack, int stack_sz, pid_t pid)
5959
if (result->entries[i].size == 1) {
6060
sym = &result->entries[i].syms[0];
6161
if (sym->path && sym->path[0]) {
62-
printf(" %d [<%016llx>] %s+0x%llx %s:%ld\n",
63-
i, stack[i], sym->symbol,
64-
stack[i] - sym->start_address,
65-
sym->path, sym->line_no);
62+
printf(" %d [<%016llx>] %s+0x%llx %s:%ld\n", i, stack[i],
63+
sym->symbol, stack[i] - sym->start_address, sym->path,
64+
sym->line_no);
6665
} else {
67-
printf(" %d [<%016llx>] %s+0x%llx\n",
68-
i, stack[i], sym->symbol,
66+
printf(" %d [<%016llx>] %s+0x%llx\n", i, stack[i], sym->symbol,
6967
stack[i] - sym->start_address);
7068
}
7169
continue;
@@ -75,9 +73,8 @@ static void show_stack_trace(__u64 *stack, int stack_sz, pid_t pid)
7573
for (j = 0; j < result->entries[i].size; j++) {
7674
sym = &result->entries[i].syms[j];
7775
if (sym->path && sym->path[0]) {
78-
printf(" %s+0x%llx %s:%ld\n",
79-
sym->symbol, stack[i] - sym->start_address,
80-
sym->path, sym->line_no);
76+
printf(" %s+0x%llx %s:%ld\n", sym->symbol,
77+
stack[i] - sym->start_address, sym->path, sym->line_no);
8178
} else {
8279
printf(" %s+0x%llx\n", sym->symbol,
8380
stack[i] - sym->start_address);
@@ -121,7 +118,7 @@ static void show_help(const char *progname)
121118
printf("Usage: %s [-f <frequency>] [-h]\n", progname);
122119
}
123120

124-
int main(int argc, char * const argv[])
121+
int main(int argc, char *const argv[])
125122
{
126123
const char *online_cpus_file = "/sys/devices/system/cpu/online";
127124
int freq = 1, pid = -1, cpu;
@@ -218,7 +215,7 @@ int main(int argc, char * const argv[])
218215
goto cleanup;
219216
}
220217
}
221-
218+
222219
/* Wait and receive stack traces */
223220
while (ring_buffer__poll(ring_buf, -1) >= 0) {
224221
}

0 commit comments

Comments
 (0)