Skip to content

Commit

Permalink
examples/c: add support for clang-format
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
unikzforce authored and anakryiko committed May 17, 2023
1 parent 6ee3b62 commit 500eba1
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 97 deletions.
153 changes: 153 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# SPDX-License-Identifier: GPL-2.0
#
# clang-format configuration file. Intended for clang-format >= 11.
#
# For more information, see:
#
# Documentation/process/clang-format.rst
# https://clang.llvm.org/docs/ClangFormat.html
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false

ForEachMacros:
- 'bpf__perf_for_each_map'
- 'bpf__perf_for_each_map_named'
- 'bpf_for_each_spilled_reg'
- 'bpf_object__for_each_map'
- 'bpf_object__for_each_program'
- 'bpf_object__for_each_safe'
- 'bpf_perf_object__for_each'
- 'for_each_btf_ext_rec'
- 'for_each_btf_ext_sec'
- 'hashmap__for_each_entry'
- 'hashmap__for_each_entry_safe'
- 'hashmap__for_each_key_entry'
- 'hashmap__for_each_key_entry_safe'
- 'list_for_each_entry'
- 'libbpf_nla_for_each_attr'
- 'list_for_each_entry'
- 'list_for_each_entry_continue'
- 'list_for_each_entry_continue_rcu'
- 'list_for_each_entry_continue_reverse'
- 'list_for_each_entry_from'
- 'list_for_each_entry_from_rcu'
- 'list_for_each_entry_from_reverse'
- 'list_for_each_entry_lockless'
- 'list_for_each_entry_rcu'
- 'list_for_each_entry_reverse'
- 'list_for_each_entry_safe'
- 'list_for_each_entry_safe_continue'
- 'list_for_each_entry_safe_from'
- 'list_for_each_entry_safe_reverse'
- 'list_for_each_entry_srcu'


IncludeBlocks: Preserve
IncludeCategories:
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true

# Taken from git's rules
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60

PointerAlignment: Right
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: Always

AlignConsecutiveMacros: AcrossEmptyLines
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build
/examples/c/build
/examples/c/.xmake
/.idea
2 changes: 2 additions & 0 deletions examples/c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
/sockfilter
/tc
/ksyscall
/cmake-build-debug/
/cmake-build-release/
5 changes: 2 additions & 3 deletions examples/c/bootstrap.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ int handle_exec(struct trace_event_raw_sched_process_exec *ctx)
}

SEC("tp/sched/sched_process_exit")
int handle_exit(struct trace_event_raw_sched_process_template* ctx)
int handle_exit(struct trace_event_raw_sched_process_template *ctx)
{
struct task_struct *task;
struct event *e;
pid_t pid, tid;
u64 id, ts, *start_ts, duration_ns = 0;

/* get PID and TID of exiting thread/process */
id = bpf_get_current_pid_tgid();
pid = id >> 32;
Expand Down Expand Up @@ -109,4 +109,3 @@ int handle_exit(struct trace_event_raw_sched_process_template* ctx)
bpf_ringbuf_submit(e, 0);
return 0;
}

25 changes: 12 additions & 13 deletions examples/c/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ static struct env {

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

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

if (e->exit_event) {
printf("%-8s %-5s %-16s %-7d %-7d [%u]",
ts, "EXIT", e->comm, e->pid, e->ppid, e->exit_code);
printf("%-8s %-5s %-16s %-7d %-7d [%u]", ts, "EXIT", e->comm, e->pid, e->ppid,
e->exit_code);
if (e->duration_ns)
printf(" (%llums)", e->duration_ns / 1000000);
printf("\n");
} else {
printf("%-8s %-5s %-16s %-7d %-7d %s\n",
ts, "EXEC", e->comm, e->pid, e->ppid, e->filename);
printf("%-8s %-5s %-16s %-7d %-7d %s\n", ts, "EXEC", e->comm, e->pid, e->ppid,
e->filename);
}

return 0;
Expand Down Expand Up @@ -149,8 +148,8 @@ int main(int argc, char **argv)
}

/* Process events */
printf("%-8s %-5s %-16s %-7s %-7s %s\n",
"TIME", "EVENT", "COMM", "PID", "PPID", "FILENAME/EXIT CODE");
printf("%-8s %-5s %-16s %-7s %-7s %s\n", "TIME", "EVENT", "COMM", "PID", "PPID",
"FILENAME/EXIT CODE");
while (!exiting) {
err = ring_buffer__poll(rb, 100 /* timeout, ms */);
/* Ctrl-C will cause -EINTR */
Expand Down
2 changes: 1 addition & 1 deletion examples/c/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef __BOOTSTRAP_H
#define __BOOTSTRAP_H

#define TASK_COMM_LEN 16
#define TASK_COMM_LEN 16
#define MAX_FILENAME_LEN 127

struct event {
Expand Down
13 changes: 7 additions & 6 deletions examples/c/ksyscall.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define TASK_COMM_LEN 16

SEC("ksyscall/tgkill")
int BPF_KSYSCALL(tgkill_entry, pid_t tgid, pid_t tid, int sig)
int BPF_KSYSCALL(tgkill_entry, pid_t tgid, pid_t tid, int sig)
{
char comm[TASK_COMM_LEN];
__u32 caller_pid = bpf_get_current_pid_tgid() >> 32;
Expand All @@ -22,13 +22,14 @@ int BPF_KSYSCALL(tgkill_entry, pid_t tgid, pid_t tid, int sig)
}

bpf_get_current_comm(&comm, sizeof(comm));
bpf_printk("tgkill syscall called by PID %d (%s) for thread id %d with pid %d and signal %d.",
caller_pid, comm, tid, tgid, sig);
bpf_printk(
"tgkill syscall called by PID %d (%s) for thread id %d with pid %d and signal %d.",
caller_pid, comm, tid, tgid, sig);
return 0;
}

SEC("ksyscall/kill")
int BPF_KSYSCALL(entry_probe, pid_t pid, int sig)
int BPF_KSYSCALL(entry_probe, pid_t pid, int sig)
{
char comm[TASK_COMM_LEN];
__u32 caller_pid = bpf_get_current_pid_tgid() >> 32;
Expand All @@ -44,8 +45,8 @@ int BPF_KSYSCALL(entry_probe, pid_t pid, int sig)
}

bpf_get_current_comm(&comm, sizeof(comm));
bpf_printk("KILL syscall called by PID %d (%s) for PID %d with signal %d.",
caller_pid, comm, pid, sig);
bpf_printk("KILL syscall called by PID %d (%s) for PID %d with signal %d.", caller_pid,
comm, pid, sig);
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion examples/c/minimal_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ int main(int argc, char **argv)

/* ensure BPF program only handles write() syscalls from our process */
pid = getpid();
err = bpf_map__update_elem(skel->maps.my_pid_map, &index, sizeof(index), &pid, sizeof(pid_t), BPF_ANY);
err = bpf_map__update_elem(skel->maps.my_pid_map, &index, sizeof(index), &pid,
sizeof(pid_t), BPF_ANY);
if (err < 0) {
fprintf(stderr, "Error updating map with pid: %s\n", strerror(err));
goto cleanup;
Expand Down
3 changes: 2 additions & 1 deletion examples/c/profile.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ int profile(void *ctx)

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

event->ustack_sz = bpf_get_stack(ctx, event->ustack, sizeof(event->ustack), BPF_F_USER_STACK);
event->ustack_sz =
bpf_get_stack(ctx, event->ustack, sizeof(event->ustack), BPF_F_USER_STACK);

bpf_ringbuf_submit(event, 0);

Expand Down
23 changes: 10 additions & 13 deletions examples/c/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
extern int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);

static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd,
unsigned long flags)
{
int ret;

Expand Down Expand Up @@ -59,13 +59,11 @@ static void show_stack_trace(__u64 *stack, int stack_sz, pid_t pid)
if (result->entries[i].size == 1) {
sym = &result->entries[i].syms[0];
if (sym->path && sym->path[0]) {
printf(" %d [<%016llx>] %s+0x%llx %s:%ld\n",
i, stack[i], sym->symbol,
stack[i] - sym->start_address,
sym->path, sym->line_no);
printf(" %d [<%016llx>] %s+0x%llx %s:%ld\n", i, stack[i],
sym->symbol, stack[i] - sym->start_address, sym->path,
sym->line_no);
} else {
printf(" %d [<%016llx>] %s+0x%llx\n",
i, stack[i], sym->symbol,
printf(" %d [<%016llx>] %s+0x%llx\n", i, stack[i], sym->symbol,
stack[i] - sym->start_address);
}
continue;
Expand All @@ -75,9 +73,8 @@ static void show_stack_trace(__u64 *stack, int stack_sz, pid_t pid)
for (j = 0; j < result->entries[i].size; j++) {
sym = &result->entries[i].syms[j];
if (sym->path && sym->path[0]) {
printf(" %s+0x%llx %s:%ld\n",
sym->symbol, stack[i] - sym->start_address,
sym->path, sym->line_no);
printf(" %s+0x%llx %s:%ld\n", sym->symbol,
stack[i] - sym->start_address, sym->path, sym->line_no);
} else {
printf(" %s+0x%llx\n", sym->symbol,
stack[i] - sym->start_address);
Expand Down Expand Up @@ -121,7 +118,7 @@ static void show_help(const char *progname)
printf("Usage: %s [-f <frequency>] [-h]\n", progname);
}

int main(int argc, char * const argv[])
int main(int argc, char *const argv[])
{
const char *online_cpus_file = "/sys/devices/system/cpu/online";
int freq = 1, pid = -1, cpu;
Expand Down Expand Up @@ -218,7 +215,7 @@ int main(int argc, char * const argv[])
goto cleanup;
}
}

/* Wait and receive stack traces */
while (ring_buffer__poll(ring_buf, -1) >= 0) {
}
Expand Down
2 changes: 1 addition & 1 deletion examples/c/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#endif

#ifndef MAX_STACK_DEPTH
#define MAX_STACK_DEPTH 128
#define MAX_STACK_DEPTH 128
#endif

typedef __u64 stack_trace_t[MAX_STACK_DEPTH];
Expand Down
4 changes: 2 additions & 2 deletions examples/c/sockfilter.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <bpf/bpf_endian.h>
#include "sockfilter.h"

#define IP_MF 0x2000
#define IP_OFFSET 0x1FFF
#define IP_MF 0x2000
#define IP_OFFSET 0x1FFF

char LICENSE[] SEC("license") = "Dual BSD/GPL";

Expand Down
Loading

0 comments on commit 500eba1

Please sign in to comment.