50cb3038882a19b055e4bdd8600ea83d33acf3e9
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 // pthread_create(&thread, NULL, consumer, NULL);
176 static void print_markers(void)
178 struct marker_iter iter
;
181 marker_iter_reset(&iter
);
182 marker_iter_start(&iter
);
185 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
186 marker_iter_next(&iter
);
191 void do_command(struct tracecmd
*cmd
)
195 void receive_commands()
203 struct trctl_msg msg
;
205 /* FIXME: fd_notif should probably be protected by a spinlock */
210 msg
.type
= MSG_NOTIF
;
211 msg
.size
= sizeof(msg
.type
);
213 /* FIXME: don't block here */
214 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
221 #define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
223 static int inform_consumer_daemon(void)
227 int listener_main(void *p
)
235 struct sockaddr_un addr
;
236 socklen_t addrlen
= sizeof(addr
);
237 char trace_name
[] = "auto";
238 char trace_type
[] = "ustrelay";
242 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
);
245 WARN("error in ustcomm_app_recv_message");
249 DBG("received a message! it's: %s\n", recvbuf
);
250 len
= strlen(recvbuf
);
251 //if(len && recvbuf[len-1] == '\n') {
252 // recvbuf[len-1] = '\0';
255 if(!strcmp(recvbuf
, "print_markers")) {
258 else if(!strcmp(recvbuf
, "trace_setup")) {
261 result
= ltt_trace_setup(trace_name
);
263 ERR("ltt_trace_setup failed");
267 result
= ltt_trace_set_type(trace_name
, trace_type
);
269 ERR("ltt_trace_set_type failed");
273 else if(!strcmp(recvbuf
, "trace_alloc")) {
276 result
= ltt_trace_alloc(trace_name
);
278 ERR("ltt_trace_alloc failed");
282 else if(!strcmp(recvbuf
, "trace_start")) {
285 result
= ltt_trace_start(trace_name
);
287 ERR("ltt_trace_start failed");
291 else if(!strcmp(recvbuf
, "trace_stop")) {
294 result
= ltt_trace_stop(trace_name
);
296 ERR("ltt_trace_stop failed");
300 else if(!strcmp(recvbuf
, "trace_destroy")) {
302 DBG("trace destroy");
304 result
= ltt_trace_destroy(trace_name
);
306 ERR("ltt_trace_destroy failed");
315 static char listener_stack
[16384];
317 void create_listener(void)
320 static char listener_stack
[16384];
321 //char *listener_stack = malloc(16384);
323 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
329 //pthread_create(&thread, NULL, listener_main, NULL);
332 /* The signal handler itself. Signals must be setup so there cannot be
335 void sighandler(int sig
)
337 static char have_listener
= 0;
346 /* Called by the app signal handler to chain it to us. */
348 void chain_signal(void)
350 sighandler(USTSIGNAL
);
353 static int init_socket(void)
355 return ustcomm_init_app(getpid(), &ustcomm_app
);
358 static void destroy_socket(void)
362 if(mysocketfile
[0] == '\0')
365 result
= unlink(mysocketfile
);
371 static int init_signal_handler(void)
373 /* Attempt to handler SIGIO. If the main program wants to
374 * handle it, fine, it'll override us. They it'll have to
375 * use the chaining function.
379 struct sigaction act
;
381 result
= sigemptyset(&act
.sa_mask
);
383 PERROR("sigemptyset");
387 act
.sa_handler
= sighandler
;
388 act
.sa_flags
= SA_RESTART
;
390 /* Only defer ourselves. Also, try to restart interrupted
391 * syscalls to disturb the traced program as little as possible.
393 result
= sigaction(SIGIO
, &act
, NULL
);
402 static void auto_probe_connect(struct marker
*m
)
406 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
408 ERR("ltt_marker_connect");
410 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
413 static void __attribute__((constructor(101))) init0()
415 DBG("UST_AUTOPROBE constructor");
416 if(getenv("UST_AUTOPROBE")) {
417 marker_set_new_marker_cb(auto_probe_connect
);
421 static void fini(void);
423 static void __attribute__((constructor(1000))) init()
427 DBG("UST_TRACE constructor");
431 if(getenv("UST_TRACE")) {
432 char trace_name
[] = "auto";
433 char trace_type
[] = "ustrelay";
435 DBG("starting early tracing");
437 /* Ensure marker control is initialized */
438 init_marker_control();
440 /* Ensure relay is initialized */
441 init_ustrelay_transport();
443 /* Ensure markers are initialized */
447 ltt_channels_register("ust");
449 result
= ltt_trace_setup(trace_name
);
451 ERR("ltt_trace_setup failed");
455 result
= ltt_trace_set_type(trace_name
, trace_type
);
457 ERR("ltt_trace_set_type failed");
461 result
= ltt_trace_alloc(trace_name
);
463 ERR("ltt_trace_alloc failed");
467 result
= ltt_trace_start(trace_name
);
469 ERR("ltt_trace_start failed");
475 /* Must create socket before signal handler to prevent races.
477 result
= init_socket();
479 ERR("init_socket error");
482 result
= init_signal_handler();
484 ERR("init_signal_handler error");
490 /* should decrementally destroy stuff if error */
494 /* This is only called if we terminate normally, not with an unhandled signal,
495 * so we cannot rely on it. */
497 static void __attribute__((destructor
)) fini()
501 /* if trace running, finish it */
503 DBG("destructor stopping traces");
505 result
= ltt_trace_stop("auto");
507 ERR("ltt_trace_stop error");
510 result
= ltt_trace_destroy("auto");
512 ERR("ltt_trace_destroy error");
515 /* FIXME: wait for the consumer to be done */
This page took 0.068261 seconds and 3 git commands to generate.