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
}
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;
}
{
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");
}
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
#include <sys/types.h>
#include <stdio.h>
+#include "marker.h"
+
void *(*plibc_malloc)(size_t size) = NULL;
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
--- /dev/null
+#!/bin/sh
+
+LD_VERBOSE=1 LD_LIBRARY_PATH=.:../libtracing:../libmarkers:../libtracectl LD_PRELOAD=libmallocwrap.so $1
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));\
}
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
#include <sys/socket.h>
#include <sys/un.h>
#include <sched.h>
+
+#include "marker.h"
+
#define UNIX_PATH_MAX 108
//#define SOCKETDIR "/var/run/ust/socks"
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)
{
}
}
}
+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:;
}
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");
}
void sighandler(int sig)
{
DBG("sighandler");
- receive_commands();
+ create_listener();
}
/* Called by the app signal handler to chain it to us. */
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;
--- /dev/null
+#include <unistd.h>
+#include <getopt.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#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;
+}