Skip to content

Instantly share code, notes, and snippets.

@eepp
Forked from simark/Makefile
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eepp/d280a27f5e7d0a64abaa to your computer and use it in GitHub Desktop.
Save eepp/d280a27f5e7d0a64abaa to your computer and use it in GitHub Desktop.
#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;
}
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
#define TRACEPOINT_CREATE_PROBES
/*
* The header containing our TRACEPOINT_EVENTs.
*/
#include "tracepoints.h"
#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>
#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;
}
int func(int a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment