From fbd8191b00521bbe7e52a6d1ff4bb814708f06ba Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Tue, 17 Feb 2009 18:35:04 -0500 Subject: [PATCH] ust: be able to list markers using ust --- hello/Makefile | 3 +- hello/hello.c | 4 +- libmallocwrap/Makefile | 4 +- libmallocwrap/mallocwrap.c | 7 ++ libmallocwrap/run | 3 + libmarkers/marker.h | 12 ++-- libtracectl/Makefile | 2 +- libtracectl/tracectl.c | 79 +++++++++++++---------- ust/ust.c | 128 +++++++++++++++++++++++++++++++++++++ 9 files changed, 196 insertions(+), 46 deletions(-) create mode 100644 libmallocwrap/run create mode 100644 ust/ust.c diff --git a/hello/Makefile b/hello/Makefile index 57cc8f2..4168e6e 100644 --- a/hello/Makefile +++ b/hello/Makefile @@ -2,7 +2,8 @@ all: hello hello: hello.c #dynamic version - gcc -g -Wl,--print-map -I../libmarkers -I../share -I../libtracing -L../libmarkers -lmarkers -L../libtracectl -ltracectl -L../libtracing -ltracing -o hello hello.c marker-control.c serialize.c + gcc -g -I../libmarkers -I../share -I../libtracing -L../libmarkers -lmarkers -L../libtracectl -ltracectl -L../libtracing -ltracing -o hello hello.c marker-control.c serialize.c + # -Wl,--print-map #static version # gcc -g -I../libmarkers -I../share -I../libtracing -L../libmarkers -o hello hello.c marker-control.c serialize.c ../libtracing/*.c ../libmarkers/*.c ../libtracectl/*.c ../share/*.c diff --git a/hello/hello.c b/hello/hello.c index ee7992b..b4f5216 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -58,7 +58,7 @@ int consumer(void *arg) } consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel)); - if(consumer_channels == NULL) { +if(consumer_channels == NULL) { ERR("malloc returned NULL"); return 1; } @@ -148,7 +148,7 @@ void start_consumer(void) { int result; - result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM, NULL); + result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); if(result == -1) { perror("clone"); } diff --git a/libmallocwrap/Makefile b/libmallocwrap/Makefile index ac7ff69..6f12740 100644 --- a/libmallocwrap/Makefile +++ b/libmallocwrap/Makefile @@ -1,4 +1,6 @@ all: libmallocwrap.so libmallocwrap.so: mallocwrap.c - gcc -shared -fPIC -g -ldl -o libmallocwrap.so mallocwrap.c + gcc -shared -fPIC -g -I../share -I../libmarkers -I../libtracing -I../libtracectl -L../libmarkers -L../libtracing -L../libtracectl -ldl -lmarkers -o libmallocwrap.so mallocwrap.c + +.PHONY: libmallocwrap.so diff --git a/libmallocwrap/mallocwrap.c b/libmallocwrap/mallocwrap.c index 48dab0a..de1d0f1 100644 --- a/libmallocwrap/mallocwrap.c +++ b/libmallocwrap/mallocwrap.c @@ -3,6 +3,8 @@ #include #include +#include "marker.h" + void *(*plibc_malloc)(size_t size) = NULL; void *malloc(size_t size) @@ -14,6 +16,11 @@ void *malloc(size_t size) return NULL; } } + + trace_mark(ust, malloc, "%d", (int)size); + fprintf(stderr, "mallocating size %d\n", size); return plibc_malloc(size); } + +MARKER_LIB diff --git a/libmallocwrap/run b/libmallocwrap/run new file mode 100644 index 0000000..da398d3 --- /dev/null +++ b/libmallocwrap/run @@ -0,0 +1,3 @@ +#!/bin/sh + +LD_VERBOSE=1 LD_LIBRARY_PATH=.:../libtracing:../libmarkers:../libtracectl LD_PRELOAD=libmallocwrap.so $1 diff --git a/libmarkers/marker.h b/libmarkers/marker.h index b83ff65..aebe27a 100644 --- a/libmarkers/marker.h +++ b/libmarkers/marker.h @@ -284,12 +284,12 @@ struct lib { int marker_register_lib(struct marker *markers_start, int markers_count); -#define MARKER_LIB \ -extern struct marker __start___markers[] __attribute__((visibility("hidden"))); \ -extern struct marker __stop___markers[] __attribute__((visibility("hidden"))); \ - \ -static void __attribute__((constructor)) __markers__init(void) \ -{ \ +#define MARKER_LIB \ +extern struct marker __start___markers[] __attribute__((visibility("hidden"))); \ +extern struct marker __stop___markers[] __attribute__((visibility("hidden"))); \ + \ +static void __attribute__((constructor)) __markers__init(void) \ +{ \ marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker));\ } diff --git a/libtracectl/Makefile b/libtracectl/Makefile index 754e8ff..6aa41e4 100644 --- a/libtracectl/Makefile +++ b/libtracectl/Makefile @@ -1,7 +1,7 @@ all: libtracectl.so libtracectl.so: tracectl.c - gcc -g -shared -fPIC -o libtracectl.so tracectl.c + gcc -g -shared -fPIC -I../libmarkers -I../share -o libtracectl.so tracectl.c clean: rm -rf *.so *.o diff --git a/libtracectl/tracectl.c b/libtracectl/tracectl.c index 46874d4..04de9f3 100644 --- a/libtracectl/tracectl.c +++ b/libtracectl/tracectl.c @@ -5,6 +5,9 @@ #include #include #include + +#include "marker.h" + #define UNIX_PATH_MAX 108 //#define SOCKETDIR "/var/run/ust/socks" @@ -44,6 +47,20 @@ pid_t mypid; char mysocketfile[UNIX_PATH_MAX] = ""; int pfd = -1; + +static void print_markers(void) +{ + struct marker_iter iter; + + marker_iter_reset(&iter); + marker_iter_start(&iter); + + while(iter.marker) { + fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format); + marker_iter_next(&iter); + } +} + void do_command(struct tracecmd *cmd) { } @@ -74,56 +91,48 @@ void notif_cb(void) } } +char recvbuf[10000]; + int listener_main(void *p) { int result; - /* Allowing only 1 connection for now. */ - result = listen(pfd, 1); - if(result == -1) { - PERROR("listen"); - return 1; - } - for(;;) { - uint32_t size; - int fd; struct sockaddr_un addr; socklen_t addrlen = sizeof(addr); - result = fd = accept(pfd, (struct sockaddr *)&addr, &addrlen); - if(result == -1) { - PERROR("accept"); - continue; - } - for(;;) { struct trctl_msg msg; - unsigned char dontclose=0; + int len; - result = read(fd, &msg.size, sizeof(msg.size)); + result = len = recvfrom(pfd, recvbuf, sizeof(recvbuf), 0, &addr, &addrlen); if(result == -1) { - PERROR("read"); + PERROR("recvfrom"); continue; } - if(size > MAX_MSG_SIZE) { - ERR("trctl message too big"); - break; - } + if(recvbuf[len-2] == '\n') + recvbuf[len-2] = '\0'; - result = read(fd, &msg.type, sizeof(msg.type)); - if(result == -1) { - PERROR("read"); - continue; - } + fprintf(stderr, "received a message! it's: %s\n", recvbuf); - switch(msg.type) { - case MSG_REGISTER_NOTIF: - /* TODO: put it in notif mode */ - goto next_conn; - }; + + if(!strcmp(recvbuf, "print_markers")) { + print_markers(); + } + else if(!strcmp(recvbuf, "trace_setup")) { + DBG("trace setup"); + } + else if(!strcmp(recvbuf, "trace_alloc")) { + DBG("trace alloc"); + } + else if(!strcmp(recvbuf, "trace_start")) { + DBG("trace start"); + } + else if(!strcmp(recvbuf, "trace_stop")) { + DBG("trace stop"); + } } next_conn:; } @@ -134,7 +143,7 @@ void create_listener(void) int result; static char listener_stack[16384]; - result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM, NULL); + result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); if(result == -1) { perror("clone"); } @@ -145,7 +154,7 @@ void create_listener(void) void sighandler(int sig) { DBG("sighandler"); - receive_commands(); + create_listener(); } /* Called by the app signal handler to chain it to us. */ @@ -164,7 +173,7 @@ static int init_socket(void) struct sockaddr_un addr; - result = fd = socket(PF_UNIX, SOCK_SEQPACKET, 0); + result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); if(result == -1) { PERROR("socket"); return -1; diff --git a/ust/ust.c b/ust/ust.c new file mode 100644 index 0000000..1fb7d09 --- /dev/null +++ b/ust/ust.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define UNIX_PATH_MAX 108 +#define SOCK_DIR "/tmp/socks" +#define UST_SIGNAL SIGIO + +struct ust_msg { + char *raw; +}; + +void parse_opts(int argc, char **argv) +{ + int flags, opt; + int nsecs, tfnd; + + nsecs = 0; + tfnd = 0; + flags = 0; + while ((opt = getopt(argc, argv, "nt:")) != -1) { + switch (opt) { + case 'n': + flags = 1; + break; + case 't': + nsecs = atoi(optarg); + tfnd = 1; + break; + default: /* '?' */ + fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", + argv[0]); + exit(EXIT_FAILURE); + } + } + + printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind); + + if (optind >= argc) { + fprintf(stderr, "Expected argument after options\n"); + exit(EXIT_FAILURE); + } + + printf("name argument = %s\n", argv[optind]); + + /* Other code omitted */ + + exit(EXIT_SUCCESS); + +} + +void signal_process(pid_t pid) +{ + int result; + + result = kill(pid, UST_SIGNAL); + if(result == -1) { + perror("kill"); + return; + } + + sleep(1); +} + +int send_message(pid_t pid, const char *msg) +{ + int fd; + int result; + struct sockaddr_un addr; + + result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); + if(result == -1) { + perror("socket"); + return 1; + } + + addr.sun_family = AF_UNIX; + + result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); + if(result >= UNIX_PATH_MAX) { + fprintf(stderr, "string overflow allocating socket name"); + return 1; + } + + char buf[] = "print_markers\n"; + + signal_process(pid); + + result = sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); + if(result == -1) { + perror("sendto"); + return 1; + } + +// result = fd = open(sockfile, O_RDWR); +// if(result == -1 && errno == ENXIO) { +// fprintf(stderr, "signalling process\n"); +// +// result = fd = open(sockfile, O_RDWR); +// if(result == -1) { +// perror("open"); +// return 1; +// } +// } +// else if(result == -1) { +// perror("open"); +// return 1; +// } + +} + +int main(int argc, char *argv[]) +{ + pid_t pid = atoi(argv[1]); + + char *msg = argv[2]; + + send_message(pid, msg); + + return 0; +} -- 2.34.1