Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
/lsm
/cmake-build-debug/
/cmake-build-release/
compile_commands.json
/app
/libapp.so
/snooper
7 changes: 7 additions & 0 deletions examples/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ find_package(BpfObject REQUIRED)
file(GLOB apps *.bpf.c)
if(NOT CARGO_EXISTS)
list(REMOVE_ITEM apps ${CMAKE_CURRENT_SOURCE_DIR}/profile.bpf.c)
list(REMOVE_ITEM apps ${CMAKE_CURRENT_SOURCE_DIR}/snooper.bpf.c)
endif()
foreach(app ${apps})
get_filename_component(app_stem ${app} NAME_WE)
Expand All @@ -93,4 +94,10 @@ foreach(app ${apps})
target_link_libraries(${app_stem}
${CMAKE_CURRENT_SOURCE_DIR}/../../blazesym/target/release/libblazesym_c.a -lpthread -lrt -ldl)
endif()
if(${app_stem} STREQUAL snooper)
target_include_directories(${app_stem} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../../blazesym/capi/include)
target_link_libraries(${app_stem}
${CMAKE_CURRENT_SOURCE_DIR}/../../blazesym/target/release/libblazesym_c.a -lpthread -lrt -ldl)
endif()
endforeach()
15 changes: 12 additions & 3 deletions examples/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CARGO ?= $(shell which cargo)
ifeq ($(strip $(CARGO)),)
BZS_APPS :=
else
BZS_APPS := profile
BZS_APPS := profile snooper
APPS += $(BZS_APPS)
# Required by libblazesym
ALL_LDFLAGS += -lrt -ldl -lpthread -lm
Expand Down Expand Up @@ -70,12 +70,12 @@ $(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(APPS)
all: $(APPS) app

.PHONY: clean
clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(APPS)
$(Q)rm -rf $(OUTPUT) $(APPS) app libapp.so

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)
Expand Down Expand Up @@ -136,3 +136,12 @@ $(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)

# keep intermediate (.skel.h, .bpf.o, etc) targets
.SECONDARY:

# Build target app and its shared library
libapp.so: app_lib.c app_lib.h
$(call msg,SHLIB,$@)
$(Q)$(CC) $(CFLAGS) -shared -fPIC -o $@ app_lib.c

app: app.c app_lib.h libapp.so
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) -o $@ app.c -L. -lapp -Wl,-rpath,'$$ORIGIN' -lpthread
140 changes: 140 additions & 0 deletions examples/c/app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <elf.h>
#include <pthread.h>
#include "app_lib.h"

static __thread int tls_dont_care; /* just to avoid zero offsets everywhere else */

__thread int tls_exec;
extern __thread int tls_shared;

static __thread int tls_local_exec;

int __attribute__((weak)) get_tls_exec(void)
{
return tls_exec;
}

int __attribute__((weak)) get_tls_shared(void)
{
return tls_shared;
}

int __attribute__((weak)) get_tls_local_exec(void)
{
return tls_local_exec;
}

/* Forward declarations for recursive functions */
void func_a(int depth);
void func_b(int depth);
void func_c(int depth);

static __always_inline void func_mux(int depth)
{
if (depth <= 0)
return;

switch (rand() % 3) {
case 0: func_a(depth - 1); break;
case 1: func_b(depth - 1); break;
case 2: func_c(depth - 1); break;
}
}

void func_a(int depth)
{
volatile char stack_space[120];
stack_space[119] = 'a';
stack_space[0] += 1;

if (depth <= 0)
return;

func_mux(depth - 1);
}

void func_b(int depth)
{
volatile char stack_space[350];
stack_space[349] = 'b';
stack_space[0] += 1;

if (depth <= 0)
return;

func_mux(depth - 1);
}

void func_c(int depth)
{
volatile char stack_space[800];
stack_space[799] = 'c';
stack_space[0] += 1;

if (depth <= 0)
return;

func_mux(depth - 1);
}

static void *thread_func(void *arg)
{
time_t last_print = 0;
(void)arg;

pthread_setname_np(pthread_self(), "app_thread");

while (1) {
time_t now;

errno = 123456789;
func_mux(10);
errno = 987654321;

now = time(NULL);
if (now > last_print) {
tls_exec += 4;
tls_shared += 8;
tls_local_exec += 16;
bump_tls_local_shared();
bump_tls_local_shared();

printf("Hello from thread (exec=%d, shared=%d, local_exec=%d, local_shared=%d)!\n",
get_tls_exec(), get_tls_shared(), get_tls_local_exec(), get_tls_local_shared());
last_print = now;
}
}

return NULL;
}

int main() {
pthread_t thread;

pthread_create(&thread, NULL, thread_func, NULL);

while (1) {
tls_dont_care += 1;
tls_exec += 2;
tls_shared += 4;
tls_local_exec += 8;
bump_tls_local_shared();

printf("Hello from app (exec=%d, shared=%d, local_exec=%d, local_shared=%d)!\n",
get_tls_exec(), get_tls_shared(), get_tls_local_exec(), get_tls_local_shared());
sleep(1);
}

return 0;
}
8 changes: 8 additions & 0 deletions examples/c/app_lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__thread int tls_shared;
__thread int tls_shared2;
static __thread int tls_local_shared;

int get_tls_local_shared(void) { return tls_local_shared; }
int get_tls_shared(void) { return tls_shared; }
int get_tls_shared2(void) { return tls_shared2; }
void bump_tls_local_shared(void) { tls_local_shared += 16; }
7 changes: 7 additions & 0 deletions examples/c/app_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef APP_LIB_H
#define APP_LIB_H

int get_tls_local_shared(void);
void bump_tls_local_shared(void);

#endif /* APP_LIB_H */
Loading
Loading