If you really want to replace the function you can compile your own shared library with pthread_create function, from within you can dynamically load and call original phtread_create function.
Library code pthread.c
:
#include <dlfcn.h>
#include <stdio.h>
#include <pthread.h>
int (*original_pthread_create)(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) = NULL;
void load_original_pthread_create() {
void *handle = dlopen("libpthread-2.15.so", RTLD_LAZY);
char *err = dlerror();
if (err) {
printf("%s\n", err);
}
original_pthread_create = dlsym(handle, "pthread_create");
err = dlerror();
if (err) {
printf("%s\n", err);
}
}
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) {
if (original_pthread_create == NULL) {
load_original_pthread_create();
}
printf("I am creating thread from my pthread_create\n");
return original_pthread_create(thread, attr, start_routine, arg);
}
Compilation:
gcc pthread.c -o libmypthread.so -shared -fpic -ldl
Usage:
LD_PRELOAD=./libmypthread.so some_program_using_pthread_create
some_program_using_pthread_create should work as usual, but with each pthread_create function call it should print additional line.
Note: place correct name of your pthread library in dlopen function.