3c5ff859c7f5f9b15139e86ca11e2f3c4b2d3184
5 #include <sys/socket.h>
15 #define UNIX_PATH_MAX 108
17 #define SOCKETDIR "/tmp/socks"
18 #define SOCKETDIRLEN sizeof(SOCKETDIR)
19 #define USTSIGNAL SIGIO
21 #define MAX_MSG_SIZE (100)
23 #define MSG_REGISTER_NOTIF 2
25 char consumer_stack
[10000];
27 static struct ustcomm_app ustcomm_app
;
29 struct tracecmd
{ /* no padding */
34 //struct listener_arg {
39 /* size: the size of all the fields except size itself */
42 /* Only the necessary part of the payload is transferred. It
43 * may even be none of it.
49 char mysocketfile
[UNIX_PATH_MAX
] = "";
52 struct consumer_channel
{
54 struct ltt_channel_struct
*chan
;
57 int consumer(void *arg
)
61 char str
[] = "Hello, this is the consumer.\n";
62 struct ltt_trace_struct
*trace
;
63 struct consumer_channel
*consumer_channels
;
65 char trace_name
[] = "auto";
68 trace
= _ltt_trace_find(trace_name
);
72 CPRINTF("cannot find trace!");
76 consumer_channels
= (struct consumer_channel
*) malloc(trace
->nr_channels
* sizeof(struct consumer_channel
));
77 if(consumer_channels
== NULL
) {
78 ERR("malloc returned NULL");
82 CPRINTF("opening trace files");
83 for(i
=0; i
<trace
->nr_channels
; i
++) {
85 struct ltt_channel_struct
*chan
= &trace
->channels
[i
];
87 consumer_channels
[i
].chan
= chan
;
89 snprintf(tmp
, sizeof(tmp
), "trace/%s_0", chan
->channel_name
);
90 result
= consumer_channels
[i
].fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00644);
95 CPRINTF("\topened trace file %s", tmp
);
98 CPRINTF("done opening trace files");
103 for(i
=0; i
<trace
->nr_channels
; i
++) {
104 struct rchan
*rchan
= consumer_channels
[i
].chan
->trans_channel_data
;
105 struct rchan_buf
*rbuf
= rchan
->buf
;
106 struct ltt_channel_buf_struct
*lttbuf
= consumer_channels
[i
].chan
->buf
;
109 result
= ltt_do_get_subbuf(rbuf
, lttbuf
, &consumed_old
);
111 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
116 result
= write(consumer_channels
[i
].fd
, rbuf
->buf_data
+ (consumed_old
& (2 * 4096-1)), 4096);
117 ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
124 // CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
126 // struct ltt_channel_struct *chan = &trace->channels[0];
128 // CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
130 // struct rchan *rchan = chan->trans_channel_data;
131 // struct rchan_buf *rbuf = rchan->buf;
132 // struct ltt_channel_buf_struct *lttbuf = chan->buf;
133 // long consumed_old;
135 // result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
136 // if(result == -1) {
142 // write(STDOUT_FILENO, str, sizeof(str));
144 // result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
146 // CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
149 // CPRINTF("success!");
151 // result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
152 // ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
155 // //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(<tbuf->offset), rbuf->chan) - consumed_old);
156 // CPRINTF("Commit count %ld", local_read(<tbuf->commit_count[0]));
163 void start_consumer(void)
167 result
= clone(consumer
, consumer_stack
+sizeof(consumer_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
173 static void print_markers(void)
175 struct marker_iter iter
;
178 marker_iter_reset(&iter
);
179 marker_iter_start(&iter
);
182 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
183 marker_iter_next(&iter
);
188 void do_command(struct tracecmd
*cmd
)
192 void receive_commands()
200 struct trctl_msg msg
;
202 /* FIXME: fd_notif should probably be protected by a spinlock */
207 msg
.type
= MSG_NOTIF
;
208 msg
.size
= sizeof(msg
.type
);
210 /* FIXME: don't block here */
211 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
218 #define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
220 static int inform_consumer_daemon(void)
224 int listener_main(void *p
)
230 struct sockaddr_un addr
;
231 socklen_t addrlen
= sizeof(addr
);
232 char trace_name
[] = "auto";
233 char trace_type
[] = "ustrelay";
237 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
);
239 WARN("error in ustcomm_app_recv_message");
243 DBG("received a message! it's: %s\n", recvbuf
);
244 len
= strlen(recvbuf
);
245 if(len
&& recvbuf
[len
-1] == '\n') {
246 recvbuf
[len
-1] = '\0';
249 if(!strcmp(recvbuf
, "print_markers")) {
252 else if(!strcmp(recvbuf
, "trace_setup")) {
255 result
= ltt_trace_setup(trace_name
);
257 ERR("ltt_trace_setup failed");
261 result
= ltt_trace_set_type(trace_name
, trace_type
);
263 ERR("ltt_trace_set_type failed");
267 else if(!strcmp(recvbuf
, "trace_alloc")) {
270 result
= ltt_trace_alloc(trace_name
);
272 ERR("ltt_trace_alloc failed");
276 else if(!strcmp(recvbuf
, "trace_start")) {
279 result
= ltt_trace_start(trace_name
);
281 ERR("ltt_trace_start failed");
285 else if(!strcmp(recvbuf
, "trace_stop")) {
288 result
= ltt_trace_stop(trace_name
);
290 ERR("ltt_trace_stop failed");
294 else if(!strcmp(recvbuf
, "trace_destroy")) {
296 DBG("trace destroy");
298 result
= ltt_trace_destroy(trace_name
);
300 ERR("ltt_trace_destroy failed");
309 void create_listener(void)
312 static char listener_stack
[16384];
314 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
320 /* The signal handler itself. Signals must be setup so there cannot be
323 void sighandler(int sig
)
325 static char have_listener
= 0;
334 /* Called by the app signal handler to chain it to us. */
336 void chain_signal(void)
338 sighandler(USTSIGNAL
);
341 static int init_socket(void)
343 return ustcomm_init_app(getpid(), &ustcomm_app
);
346 static void destroy_socket(void)
350 if(mysocketfile
[0] == '\0')
353 result
= unlink(mysocketfile
);
359 static int init_signal_handler(void)
361 /* Attempt to handler SIGIO. If the main program wants to
362 * handle it, fine, it'll override us. They it'll have to
363 * use the chaining function.
367 struct sigaction act
;
369 result
= sigemptyset(&act
.sa_mask
);
371 PERROR("sigemptyset");
375 act
.sa_handler
= sighandler
;
376 act
.sa_flags
= SA_RESTART
;
378 /* Only defer ourselves. Also, try to restart interrupted
379 * syscalls to disturb the traced program as little as possible.
381 result
= sigaction(SIGIO
, &act
, NULL
);
390 static void auto_probe_connect(struct marker
*m
)
394 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
396 ERR("ltt_marker_connect");
398 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
401 static void __attribute__((constructor(101))) init0()
403 DBG("UST_AUTOPROBE constructor");
404 if(getenv("UST_AUTOPROBE")) {
405 marker_set_new_marker_cb(auto_probe_connect
);
409 static void fini(void);
411 static void __attribute__((constructor(1000))) init()
415 DBG("UST_TRACE constructor");
419 if(getenv("UST_TRACE")) {
420 char trace_name
[] = "auto";
421 char trace_type
[] = "ustrelay";
423 DBG("starting early tracing");
425 /* Ensure marker control is initialized */
426 init_marker_control();
428 /* Ensure relay is initialized */
429 init_ustrelay_transport();
431 /* Ensure markers are initialized */
435 ltt_channels_register("ust");
437 result
= ltt_trace_setup(trace_name
);
439 ERR("ltt_trace_setup failed");
443 result
= ltt_trace_set_type(trace_name
, trace_type
);
445 ERR("ltt_trace_set_type failed");
449 result
= ltt_trace_alloc(trace_name
);
451 ERR("ltt_trace_alloc failed");
455 result
= ltt_trace_start(trace_name
);
457 ERR("ltt_trace_start failed");
463 /* Must create socket before signal handler to prevent races
466 result
= init_socket();
468 ERR("init_socket error");
471 result
= init_signal_handler();
473 ERR("init_signal_handler error");
479 /* should decrementally destroy stuff if error */
483 /* This is only called if we terminate normally, not with an unhandled signal,
484 * so we cannot rely on it. */
486 static void __attribute__((destructor
)) fini()
490 /* if trace running, finish it */
492 DBG("destructor stopping traces");
494 result
= ltt_trace_stop("auto");
496 ERR("ltt_trace_stop error");
499 result
= ltt_trace_destroy("auto");
501 ERR("ltt_trace_destroy error");
504 /* FIXME: wait for the consumer to be done */
This page took 0.087249 seconds and 3 git commands to generate.