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";
247 struct ustcomm_source src
;
249 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
);
252 WARN("error in ustcomm_app_recv_message");
256 DBG("received a message! it's: %s\n", recvbuf
);
257 len
= strlen(recvbuf
);
258 //if(len && recvbuf[len-1] == '\n') {
259 // recvbuf[len-1] = '\0';
262 if(!strcmp(recvbuf
, "print_markers")) {
265 else if(!strcmp(recvbuf
, "trace_setup")) {
268 result
= ltt_trace_setup(trace_name
);
270 ERR("ltt_trace_setup failed");
274 result
= ltt_trace_set_type(trace_name
, trace_type
);
276 ERR("ltt_trace_set_type failed");
280 else if(!strcmp(recvbuf
, "trace_alloc")) {
283 result
= ltt_trace_alloc(trace_name
);
285 ERR("ltt_trace_alloc failed");
289 else if(!strcmp(recvbuf
, "trace_start")) {
292 result
= ltt_trace_start(trace_name
);
294 ERR("ltt_trace_start failed");
298 else if(!strcmp(recvbuf
, "trace_stop")) {
301 result
= ltt_trace_stop(trace_name
);
303 ERR("ltt_trace_stop failed");
307 else if(!strcmp(recvbuf
, "trace_destroy")) {
309 DBG("trace destroy");
311 result
= ltt_trace_destroy(trace_name
);
313 ERR("ltt_trace_destroy failed");
317 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
318 struct ltt_trace_struct
*trace
;
319 char trace_name
[] = "auto";
325 trace
= _ltt_trace_find(trace_name
);
329 CPRINTF("cannot find trace!");
333 for(i
=0; i
<trace
->nr_channels
; i
++) {
334 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
335 struct rchan_buf
*rbuf
= rchan
->buf
;
337 DBG("the shmid is %d", rbuf
->shmid
);
341 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
344 libfile
= nth_token(recvbuf
, 1);
346 DBG("load_probe_lib loading %s", libfile
);
353 static char listener_stack
[16384];
355 void create_listener(void)
358 static char listener_stack
[16384];
359 //char *listener_stack = malloc(16384);
362 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
369 pthread_create(&thread
, NULL
, listener_main
, NULL
);
373 /* The signal handler itself. Signals must be setup so there cannot be
376 void sighandler(int sig
)
378 static char have_listener
= 0;
387 /* Called by the app signal handler to chain it to us. */
389 void chain_signal(void)
391 sighandler(USTSIGNAL
);
394 static int init_socket(void)
396 return ustcomm_init_app(getpid(), &ustcomm_app
);
399 static void destroy_socket(void)
403 if(mysocketfile
[0] == '\0')
406 result
= unlink(mysocketfile
);
412 static int init_signal_handler(void)
414 /* Attempt to handler SIGIO. If the main program wants to
415 * handle it, fine, it'll override us. They it'll have to
416 * use the chaining function.
420 struct sigaction act
;
422 result
= sigemptyset(&act
.sa_mask
);
424 PERROR("sigemptyset");
428 act
.sa_handler
= sighandler
;
429 act
.sa_flags
= SA_RESTART
;
431 /* Only defer ourselves. Also, try to restart interrupted
432 * syscalls to disturb the traced program as little as possible.
434 result
= sigaction(SIGIO
, &act
, NULL
);
443 static void auto_probe_connect(struct marker
*m
)
447 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
449 ERR("ltt_marker_connect");
451 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
454 static void __attribute__((constructor(101))) init0()
456 DBG("UST_AUTOPROBE constructor");
457 if(getenv("UST_AUTOPROBE")) {
458 marker_set_new_marker_cb(auto_probe_connect
);
462 static void fini(void);
464 static void __attribute__((constructor(1000))) init()
468 DBG("UST_TRACE constructor");
470 /* Must create socket before signal handler to prevent races.
472 result
= init_socket();
474 ERR("init_socket error");
477 result
= init_signal_handler();
479 ERR("init_signal_handler error");
483 if(getenv("UST_TRACE")) {
484 char trace_name
[] = "auto";
485 char trace_type
[] = "ustrelay";
487 DBG("starting early tracing");
489 /* Ensure marker control is initialized */
490 init_marker_control();
492 /* Ensure relay is initialized */
493 init_ustrelay_transport();
495 /* Ensure markers are initialized */
499 ltt_channels_register("ust");
501 result
= ltt_trace_setup(trace_name
);
503 ERR("ltt_trace_setup failed");
507 result
= ltt_trace_set_type(trace_name
, trace_type
);
509 ERR("ltt_trace_set_type failed");
513 result
= ltt_trace_alloc(trace_name
);
515 ERR("ltt_trace_alloc failed");
519 result
= ltt_trace_start(trace_name
);
521 ERR("ltt_trace_start failed");
525 inform_consumer_daemon();
531 /* should decrementally destroy stuff if error */
535 /* This is only called if we terminate normally, not with an unhandled signal,
536 * so we cannot rely on it. */
538 static void __attribute__((destructor
)) fini()
542 /* if trace running, finish it */
544 DBG("destructor stopping traces");
546 result
= ltt_trace_stop("auto");
548 ERR("ltt_trace_stop error");
551 result
= ltt_trace_destroy("auto");
553 ERR("ltt_trace_destroy error");
556 /* FIXME: wait for the consumer to be done */
This page took 0.043485 seconds and 4 git commands to generate.