also link libjack, just to be sure
[jack_interposer.git] / jack_interposer.c
blobaaac87d5dc869696b2589899fa0ee2e9810f199a
1 // needed to use RTLD_NEXT from dlfcn.h
2 #define _GNU_SOURCE
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdbool.h>
7 #include <dlfcn.h>
8 #include <poll.h>
9 #include <jack/jack.h>
10 #include <stdarg.h>
12 #define ABORT_ON_VIOLATION 1
14 // is set to 'true' when entering the process-callback and to 'false' when
15 // leaving it. When set to 'true', calls to non-realtime functions will
16 // cause warnings/errors.
17 //
18 // This assumes there is only 1 thread running at a time, thus introducing
19 // the limitation that jack_interposer is only usable on single-CPU machines
20 // (or machines configured to run the application under test on only 1 CPU).
21 bool in_rt = false;
23 #include "checkers.c"
25 JackProcessCallback real_process_callback;
27 int fprintf(FILE *stream, const char *format, ...)
29 va_list ap;
30 va_start(ap, format);
31 return vfprintf(stream, format, ap);
34 int printf(const char *format, ...)
36 va_list ap;
37 va_start(ap, format);
38 return vprintf(format, ap);
41 int pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec* abstime)
43 static int (*func)(pthread_cond_t*, pthread_mutex_t*, const struct timespec* abstime);
45 if (in_rt)
47 printf("pthread_cond_timedwait() is called while in rt section\n");
48 #if ABORT_ON_VIOLATION
49 abort();
50 #endif
53 if(!func)
54 //func = (int (*)(pthread_cond_t*, pthread_mutex_t*)) dlsym(RTLD_NEXT, "pthread_cond_wait");
55 // see http://forums.novell.com/novell-product-support-forums/suse-linux-enterprise-server-sles/sles-configure-administer/385705-sles-10-2-java-hung-calling-pthread_cond_timedwait.html
56 func = (int (*)()) dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2");
57 if (func == NULL)
59 fprintf(stderr, "Error dlsym'ing\n");
60 abort();
62 return(func(cond, mutex, abstime));
66 int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
68 static int (*func)(pthread_cond_t*, pthread_mutex_t*);
70 if (in_rt)
72 printf("pthread_cond_wait() is called while in rt section\n");
73 #if ABORT_ON_VIOLATION
74 abort();
75 #endif
78 if(!func)
79 //func = (int (*)(pthread_cond_t*, pthread_mutex_t*)) dlsym(RTLD_NEXT, "pthread_cond_wait");
80 // see http://forums.novell.com/novell-product-support-forums/suse-linux-enterprise-server-sles/sles-configure-administer/385705-sles-10-2-java-hung-calling-pthread_cond_timedwait.html
81 func = (int (*)(pthread_cond_t*, pthread_mutex_t*)) dlvsym(RTLD_NEXT, "pthread_cond_wait", "GLIBC_2.3.2");
82 if (func == NULL)
84 fprintf(stderr, "Error dlsym'ing\n");
85 abort();
87 return(func(cond, mutex));
90 int interposed_process_callback(jack_nframes_t nframes, void* arg)
92 int result;
94 in_rt = true;
96 result = real_process_callback(nframes, arg);
98 in_rt = false;
100 return result;
103 int jack_set_process_callback(jack_client_t* client,
104 JackProcessCallback process_callback, void* arg)
106 printf("hi!\n");
108 static int (*func)() = NULL;
109 int result;
111 if(!func)
112 func = (int(*)()) dlsym(RTLD_NEXT, "jack_set_process_callback");
113 if(!func)
115 fprintf(stderr, "Error dlsym'ing jack_set_process_callback\n");
116 abort();
119 real_process_callback = process_callback;
121 result = func(client, interposed_process_callback, arg);
123 printf("Result is %d\n", result);
125 return result;