~kline/geiger

e8d948bd6cab62d04aa95fc2ce8b9a0a041cc14b — Gareth Pulham 1 year, 11 months ago 39894bd master
Make it an LD_PRELOADable library with demo
4 files changed, 82 insertions(+), 33 deletions(-)

M .gitignore
M Makefile
A demo_target/main.c
M geiger.c
M .gitignore => .gitignore +2 -1
@@ 1,2 1,3 @@
*.o
geiger
geiger.so
demo_target/demo

M Makefile => Makefile +13 -7
@@ 1,11 1,11 @@
CC = clang
CFLAGS = -std=c99 -Wall -D_POSIX_C_SOURCE=200809L
LDFLAGS = -lpthread -lasound
CC = gcc
CFLAGS = -std=c99 -Wall -D_POSIX_C_SOURCE=200809L -fPIC
LDFLAGS = -lpthread -lasound -shared
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
EXECUTABLE = geiger
EXECUTABLE = geiger.so

all: $(EXECUTABLE)
all: $(EXECUTABLE) demo_target/demo

%.o: %.c %.h
	$(CC) -c $(CFLAGS) $< -o $@


@@ 13,6 13,12 @@ all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@

.PHONY: clean all
demo_target/demo: demo_target/main.c $(EXECUTABLE)
	gcc -std=c11 $< -o $@

rundemo: demo_target/demo
	LD_PRELOAD=./geiger.so demo_target/demo

.PHONY: clean all rundemo
clean:
	rm -f *.o $(EXECUTABLE)
	rm -f *.o $(EXECUTABLE) demo_target/demo

A demo_target/main.c => demo_target/main.c +17 -0
@@ 0,0 1,17 @@
#define _XOPEN_SOURCE 500

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(){
    for (;;){
        usleep(rand()%100000);
        void* mem = malloc(1000);
        if (mem != NULL){
            //printf("malloc()d\n");
            free(mem);
        }
    }
    return 0;
}

M geiger.c => geiger.c +50 -25
@@ 1,3 1,5 @@
#define _GNU_SOURCE

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>


@@ 5,13 7,15 @@

char stop = 0;
char decay = 0;
char thread_started = 0;
pthread_t worker_thread;

void* worker(void *z){
    // mostly lifted from https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm__min_8c-example.html
    static char *device = "default";

    unsigned char click[16];
    unsigned char blank[16];
    unsigned char click[4];
    unsigned char blank[4];

    int err;
    unsigned int i;


@@ 26,7 30,7 @@ void* worker(void *z){

    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
        pthread_exit(NULL);
    }
    if ((err = snd_pcm_set_params(handle,
                                  SND_PCM_FORMAT_U8,


@@ 36,46 40,67 @@ void* worker(void *z){
                                  1,
                                  125)) < 0) {
        fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
        pthread_exit(NULL);
    }

    fprintf(stderr, "Starting!\n");
    while (!stop) {
        // Frame selection
        if (decay) {
            frames = snd_pcm_writei(handle, click, sizeof(click));
            decay = 0;
        }
        else
        else {
            frames = snd_pcm_writei(handle, blank, sizeof(click));
        if (frames < 0)
        }

        // Handle mis-plays
        if (frames < 0) {
            frames = snd_pcm_recover(handle, frames, 0);
            if (frames < 0) {
                fprintf(stderr, "snd_pcm_writei failed: %s\n", snd_strerror(err));
                break;
            }
            if (frames > 0 && frames < (long)sizeof(click))
                fprintf(stderr, "Short write (expected %li, wrote %li)\n", (long)sizeof(click), frames);
        }
        if (frames < 0) {
            fprintf(stderr, "snd_pcm_writei failed: %s\n", snd_strerror(err));
            break;
        }
        if (frames > 0 && frames < (long)sizeof(click)) {
            fprintf(stderr, "Short write (expected %li, wrote %li)\n", (long)sizeof(click), frames);
        }
    }

    snd_pcm_close(handle);
    return 0;
    fprintf(stderr, "Stopping from workers\n");
    pthread_exit(NULL);
}

int main(){
    pthread_t worker_thread;
void init(void) __attribute__((constructor));
void init(void){
    int rc = pthread_create(&worker_thread, NULL, worker, (void *)NULL);
    if (rc) {
        fprintf(stderr, "Whoops!\n");
        return -1;
    }
    for (;;){
        char input = getchar();
        if (input != 10){
            break;
        }
        decay = 1;
    } else {
        thread_started = 1;
    }
    return;
}

void deinit(void) __attribute__((destructor));
void deinit(void){
    stop = 1;
    fprintf(stderr, "Stopping: %d\n", stop);
    pthread_join(worker_thread, NULL);
    return 0;
    if (thread_started)
        pthread_join(worker_thread, NULL);
    return;
}

#include <dlfcn.h>
void* malloc(size_t size)
{
    static void* (*real_malloc)(size_t) = NULL;
    if (!real_malloc) {
        real_malloc = dlsym(RTLD_NEXT, "malloc");
    }

    void *p = real_malloc(size);
    decay = 1;
    return p;
}