d041e6bb0ce3caef7ac72bea86fe677c8349df60
5 #include <sys/socket.h>
17 #define UNIX_PATH_MAX 108
19 #define SOCKETDIR "/tmp/socks"
20 #define SOCKETDIRLEN sizeof(SOCKETDIR)
21 #define USTSIGNAL SIGIO
23 #define MAX_MSG_SIZE (100)
25 #define MSG_REGISTER_NOTIF 2
27 char consumer_stack
[10000];
29 static struct ustcomm_app ustcomm_app
;
31 struct tracecmd
{ /* no padding */
36 //struct listener_arg {
41 /* size: the size of all the fields except size itself */
44 /* Only the necessary part of the payload is transferred. It
45 * may even be none of it.
50 char mysocketfile
[UNIX_PATH_MAX
] = "";
53 struct consumer_channel
{
55 struct ltt_channel_struct
*chan
;
58 int consumer(void *arg
)
62 char str
[] = "Hello, this is the consumer.\n";
63 struct ltt_trace_struct
*trace
;
64 struct consumer_channel
*consumer_channels
;
66 char trace_name
[] = "auto";
69 trace
= _ltt_trace_find(trace_name
);
73 CPRINTF("cannot find trace!");
77 consumer_channels
= (struct consumer_channel
*) malloc(trace
->nr_channels
* sizeof(struct consumer_channel
));
78 if(consumer_channels
== NULL
) {
79 ERR("malloc returned NULL");
83 CPRINTF("opening trace files");
84 for(i
=0; i
<trace
->nr_channels
; i
++) {
86 struct ltt_channel_struct
*chan
= &trace
->channels
[i
];
88 consumer_channels
[i
].chan
= chan
;
90 snprintf(tmp
, sizeof(tmp
), "trace/%s_0", chan
->channel_name
);
91 result
= consumer_channels
[i
].fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00644);
96 CPRINTF("\topened trace file %s", tmp
);
99 CPRINTF("done opening trace files");
104 for(i
=0; i
<trace
->nr_channels
; i
++) {
105 struct rchan
*rchan
= consumer_channels
[i
].chan
->trans_channel_data
;
106 struct rchan_buf
*rbuf
= rchan
->buf
;
107 struct ltt_channel_buf_struct
*lttbuf
= consumer_channels
[i
].chan
->buf
;
110 result
= ltt_do_get_subbuf(rbuf
, lttbuf
, &consumed_old
);
112 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
117 result
= write(consumer_channels
[i
].fd
, rbuf
->buf_data
+ (consumed_old
& (2 * 4096-1)), 4096);
118 ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
125 // CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
127 // struct ltt_channel_struct *chan = &trace->channels[0];
129 // CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
131 // struct rchan *rchan = chan->trans_channel_data;
132 // struct rchan_buf *rbuf = rchan->buf;
133 // struct ltt_channel_buf_struct *lttbuf = chan->buf;
134 // long consumed_old;
136 // result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
137 // if(result == -1) {
143 // write(STDOUT_FILENO, str, sizeof(str));
145 // result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
147 // CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
150 // CPRINTF("success!");
152 // result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
153 // ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
156 // //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(<tbuf->offset), rbuf->chan) - consumed_old);
157 // CPRINTF("Commit count %ld", local_read(<tbuf->commit_count[0]));
164 void start_consumer(void)
169 result
= clone(consumer
, consumer_stack
+sizeof(consumer_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
176 pthread_create(&thread
, NULL
, consumer
, NULL
);
180 static void print_markers(void)
182 struct marker_iter iter
;
185 marker_iter_reset(&iter
);
186 marker_iter_start(&iter
);
189 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
190 marker_iter_next(&iter
);
195 void do_command(struct tracecmd
*cmd
)
199 void receive_commands()
207 struct trctl_msg msg
;
209 /* FIXME: fd_notif should probably be protected by a spinlock */
214 msg
.type
= MSG_NOTIF
;
215 msg
.size
= sizeof(msg
.type
);
217 /* FIXME: don't block here */
218 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
225 #define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
227 static int inform_consumer_daemon(void)
229 ustcomm_request_consumer(getpid(), "metadata");
230 ustcomm_request_consumer(getpid(), "ust");
233 int listener_main(void *p
)
241 struct sockaddr_un addr
;
242 socklen_t addrlen
= sizeof(addr
);
243 char trace_name
[] = "auto";
244 char trace_type
[] = "ustrelay";
248 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
);
251 WARN("error in ustcomm_app_recv_message");
255 DBG("received a message! it's: %s\n", recvbuf
);
256 len
= strlen(recvbuf
);
257 //if(len && recvbuf[len-1] == '\n') {
258 // recvbuf[len-1] = '\0';
261 if(!strcmp(recvbuf
, "print_markers")) {
264 else if(!strcmp(recvbuf
, "trace_setup")) {
267 result
= ltt_trace_setup(trace_name
);
269 ERR("ltt_trace_setup failed");
273 result
= ltt_trace_set_type(trace_name
, trace_type
);
275 ERR("ltt_trace_set_type failed");
279 else if(!strcmp(recvbuf
, "trace_alloc")) {
282 result
= ltt_trace_alloc(trace_name
);
284 ERR("ltt_trace_alloc failed");
288 else if(!strcmp(recvbuf
, "trace_start")) {
291 result
= ltt_trace_start(trace_name
);
293 ERR("ltt_trace_start failed");
297 else if(!strcmp(recvbuf
, "trace_stop")) {
300 result
= ltt_trace_stop(trace_name
);
302 ERR("ltt_trace_stop failed");
306 else if(!strcmp(recvbuf
, "trace_destroy")) {
308 DBG("trace destroy");
310 result
= ltt_trace_destroy(trace_name
);
312 ERR("ltt_trace_destroy failed");
316 else if(!strncmp(recvbuf
, "get_shmid ", 10)) {
317 struct ltt_trace_struct
*trace
;
318 char trace_name
[] = "auto";
324 trace
= _ltt_trace_find(trace_name
);
328 CPRINTF("cannot find trace!");
332 for(i
=0; i
<trace
->nr_channels
; i
++) {
333 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
334 struct rchan_buf
*rbuf
= rchan
->buf
;
336 DBG("the shmid is %d", rbuf
->shmid
);
345 static char listener_stack
[16384];
347 void create_listener(void)
350 static char listener_stack
[16384];
351 //char *listener_stack = malloc(16384);
354 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
361 pthread_create(&thread
, NULL
, listener_main
, NULL
);
365 /* The signal handler itself. Signals must be setup so there cannot be
368 void sighandler(int sig
)
370 static char have_listener
= 0;
379 /* Called by the app signal handler to chain it to us. */
381 void chain_signal(void)
383 sighandler(USTSIGNAL
);
386 static int init_socket(void)
388 return ustcomm_init_app(getpid(), &ustcomm_app
);
391 static void destroy_socket(void)
395 if(mysocketfile
[0] == '\0')
398 result
= unlink(mysocketfile
);
404 static int init_signal_handler(void)
406 /* Attempt to handler SIGIO. If the main program wants to
407 * handle it, fine, it'll override us. They it'll have to
408 * use the chaining function.
412 struct sigaction act
;
414 result
= sigemptyset(&act
.sa_mask
);
416 PERROR("sigemptyset");
420 act
.sa_handler
= sighandler
;
421 act
.sa_flags
= SA_RESTART
;
423 /* Only defer ourselves. Also, try to restart interrupted
424 * syscalls to disturb the traced program as little as possible.
426 result
= sigaction(SIGIO
, &act
, NULL
);
435 static void auto_probe_connect(struct marker
*m
)
439 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
441 ERR("ltt_marker_connect");
443 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
446 static void __attribute__((constructor(101))) init0()
448 DBG("UST_AUTOPROBE constructor");
449 if(getenv("UST_AUTOPROBE")) {
450 marker_set_new_marker_cb(auto_probe_connect
);
454 static void fini(void);
456 static void __attribute__((constructor(1000))) init()
460 DBG("UST_TRACE constructor");
462 /* Must create socket before signal handler to prevent races.
464 result
= init_socket();
466 ERR("init_socket error");
469 result
= init_signal_handler();
471 ERR("init_signal_handler error");
475 if(getenv("UST_TRACE")) {
476 char trace_name
[] = "auto";
477 char trace_type
[] = "ustrelay";
479 DBG("starting early tracing");
481 /* Ensure marker control is initialized */
482 init_marker_control();
484 /* Ensure relay is initialized */
485 init_ustrelay_transport();
487 /* Ensure markers are initialized */
491 ltt_channels_register("ust");
493 result
= ltt_trace_setup(trace_name
);
495 ERR("ltt_trace_setup failed");
499 result
= ltt_trace_set_type(trace_name
, trace_type
);
501 ERR("ltt_trace_set_type failed");
505 result
= ltt_trace_alloc(trace_name
);
507 ERR("ltt_trace_alloc failed");
511 result
= ltt_trace_start(trace_name
);
513 ERR("ltt_trace_start failed");
517 inform_consumer_daemon();
523 /* should decrementally destroy stuff if error */
527 /* This is only called if we terminate normally, not with an unhandled signal,
528 * so we cannot rely on it. */
530 static void __attribute__((destructor
)) fini()
534 /* if trace running, finish it */
536 DBG("destructor stopping traces");
538 result
= ltt_trace_stop("auto");
540 ERR("ltt_trace_stop error");
543 result
= ltt_trace_destroy("auto");
545 ERR("ltt_trace_destroy error");
548 /* FIXME: wait for the consumer to be done */
This page took 0.062992 seconds and 3 git commands to generate.