-
-
Save eepp/d280a27f5e7d0a64abaa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <dlfcn.h> | |
#include <stdlib.h> | |
#include <link.h> | |
static void *lib_truc = NULL; | |
static int (*func_ptr) (int a) = NULL; | |
static void* load_lib(char *name) { | |
void *handle; | |
struct link_map *lm; | |
handle = dlopen(name, RTLD_NOW); | |
if (!handle) { | |
printf("Can't load %s: %s\n", name, dlerror()); | |
abort(); | |
} | |
lm = handle; | |
printf("lib %s is loaded at 0x%lx\n", name, lm->l_addr); | |
return handle; | |
} | |
static void* lookup_sym(void *lib, char *sym) { | |
char *error; | |
void *ret; | |
// First, clear the error flag. | |
dlerror(); | |
ret = dlsym(lib, sym); | |
error = dlerror(); | |
if (error) { | |
printf("Can't lookup %s: %s\n", sym, error); | |
abort(); | |
} | |
return ret; | |
} | |
int main() { | |
int result; | |
lib_truc = load_lib("./libtruc.so"); | |
func_ptr = lookup_sym(lib_truc, "func"); | |
result = func_ptr(4); | |
printf("Result is %d\n", result); | |
dlclose(lib_truc); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: libtruc.so app tracepoints.so | |
app: app.o | |
gcc -o app app.o -ldl | |
app.o: app.c | |
gcc -c app.c -g3 -O0 -Wall | |
libtruc.so: truc.o tracepoints.o | |
gcc -shared -o libtruc.so truc.o -ldl | |
truc.o: truc.c | |
gcc -c truc.c -g3 -O0 -Wall -fPIC | |
tracepoints.o: tracepoints.c | |
gcc -c tracepoints.c -I. -fPIC | |
tracepoints.so: tracepoints.o | |
gcc -shared -Wl,--no-as-needed -o $@ -llttng-ust $< | |
clean: | |
rm -f *.o *.so app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define TRACEPOINT_CREATE_PROBES | |
/* | |
* The header containing our TRACEPOINT_EVENTs. | |
*/ | |
#include "tracepoints.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#undef TRACEPOINT_PROVIDER | |
#define TRACEPOINT_PROVIDER sample_tracepoint | |
#undef TRACEPOINT_INCLUDE | |
#define TRACEPOINT_INCLUDE "./tracepoints.h" | |
#if !defined(TRACEPOINTS_H) || defined(TRACEPOINT_HEADER_MULTI_READ) | |
#define TRACEPOINTS_H | |
#include <lttng/tracepoint.h> | |
TRACEPOINT_EVENT( | |
sample_tracepoint, | |
message, | |
TP_ARGS(int, voila), | |
TP_FIELDS( | |
ctf_integer(int, intfield, voila) | |
) | |
) | |
#endif /* TRACEPOINTS_H */ | |
#include <lttng/tracepoint-event.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define TRACEPOINT_DEFINE | |
#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE | |
#include "truc.h" | |
#include "tracepoints.h" | |
int func(int a) { | |
tracepoint(sample_tracepoint, message, a); | |
return a + 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int func(int a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment