5 #include <sys/socket.h>
14 #define UNIX_PATH_MAX 108
16 //#define SOCKETDIR "/var/run/ust/socks"
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 struct tracecmd
{ /* no padding */
32 //struct listener_arg {
37 /* size: the size of all the fields except size itself */
40 /* Only the necessary part of the payload is transferred. It
41 * may even be none of it.
47 char mysocketfile
[UNIX_PATH_MAX
] = "";
50 struct consumer_channel
{
52 struct ltt_channel_struct
*chan
;
55 int consumer(void *arg
)
59 char str
[] = "Hello, this is the consumer.\n";
60 struct ltt_trace_struct
*trace
;
61 struct consumer_channel
*consumer_channels
;
63 char trace_name
[] = "auto";
66 trace
= _ltt_trace_find(trace_name
);
70 CPRINTF("cannot find trace!");
74 consumer_channels
= (struct consumer_channel
*) malloc(trace
->nr_channels
* sizeof(struct consumer_channel
));
75 if(consumer_channels
== NULL
) {
76 ERR("malloc returned NULL");
80 CPRINTF("opening trace files");
81 for(i
=0; i
<trace
->nr_channels
; i
++) {
83 struct ltt_channel_struct
*chan
= &trace
->channels
[i
];
85 consumer_channels
[i
].chan
= chan
;
87 snprintf(tmp
, sizeof(tmp
), "trace/%s_0", chan
->channel_name
);
88 result
= consumer_channels
[i
].fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00644);
93 CPRINTF("\topened trace file %s", tmp
);
96 CPRINTF("done opening trace files");
101 for(i
=0; i
<trace
->nr_channels
; i
++) {
102 struct rchan
*rchan
= consumer_channels
[i
].chan
->trans_channel_data
;
103 struct rchan_buf
*rbuf
= rchan
->buf
;
104 struct ltt_channel_buf_struct
*lttbuf
= consumer_channels
[i
].chan
->buf
;
107 result
= ltt_do_get_subbuf(rbuf
, lttbuf
, &consumed_old
);
109 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
114 result
= write(consumer_channels
[i
].fd
, rbuf
->buf_data
+ (consumed_old
& (2 * 4096-1)), 4096);
115 ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
122 // CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
124 // struct ltt_channel_struct *chan = &trace->channels[0];
126 // CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
128 // struct rchan *rchan = chan->trans_channel_data;
129 // struct rchan_buf *rbuf = rchan->buf;
130 // struct ltt_channel_buf_struct *lttbuf = chan->buf;
131 // long consumed_old;
133 // result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
134 // if(result == -1) {
140 // write(STDOUT_FILENO, str, sizeof(str));
142 // result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
144 // CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
147 // CPRINTF("success!");
149 // result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
150 // ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
153 // //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(<tbuf->offset), rbuf->chan) - consumed_old);
154 // CPRINTF("Commit count %ld", local_read(<tbuf->commit_count[0]));
161 void start_consumer(void)
165 result
= clone(consumer
, consumer_stack
+sizeof(consumer_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
171 static void print_markers(void)
173 struct marker_iter iter
;
175 marker_iter_reset(&iter
);
176 marker_iter_start(&iter
);
179 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
180 marker_iter_next(&iter
);
184 void do_command(struct tracecmd
*cmd
)
188 void receive_commands()
196 struct trctl_msg msg
;
198 /* FIXME: fd_notif should probably be protected by a spinlock */
203 msg
.type
= MSG_NOTIF
;
204 msg
.size
= sizeof(msg
.type
);
206 /* FIXME: don't block here */
207 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
216 int listener_main(void *p
)
222 struct sockaddr_un addr
;
223 socklen_t addrlen
= sizeof(addr
);
224 char trace_name
[] = "auto";
225 char trace_type
[] = "ustrelay";
228 struct trctl_msg msg
;
231 result
= len
= recvfrom(pfd
, recvbuf
, sizeof(recvbuf
), 0, &addr
, &addrlen
);
237 if(recvbuf
[len
-1] == '\n')
238 recvbuf
[len
-1] = '\0';
240 fprintf(stderr
, "received a message! it's: %s\n", recvbuf
);
243 if(!strcmp(recvbuf
, "print_markers")) {
246 else if(!strcmp(recvbuf
, "trace_setup")) {
249 result
= ltt_trace_setup(trace_name
);
251 ERR("ltt_trace_setup failed");
255 result
= ltt_trace_set_type(trace_name
, trace_type
);
257 ERR("ltt_trace_set_type failed");
261 else if(!strcmp(recvbuf
, "trace_alloc")) {
264 result
= ltt_trace_alloc(trace_name
);
266 ERR("ltt_trace_alloc failed");
270 else if(!strcmp(recvbuf
, "trace_start")) {
273 result
= ltt_trace_start(trace_name
);
275 ERR("ltt_trace_start failed");
279 else if(!strcmp(recvbuf
, "trace_stop")) {
282 result
= ltt_trace_stop(trace_name
);
284 ERR("ltt_trace_stop failed");
288 else if(!strcmp(recvbuf
, "trace_destroy")) {
290 DBG("trace destroy");
292 result
= ltt_trace_destroy(trace_name
);
294 ERR("ltt_trace_destroy failed");
303 void create_listener(void)
306 static char listener_stack
[16384];
308 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
314 /* The signal handler itself. */
316 void sighandler(int sig
)
322 /* Called by the app signal handler to chain it to us. */
324 void chain_signal(void)
326 sighandler(USTSIGNAL
);
329 static int init_socket(void)
336 struct sockaddr_un addr
;
338 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
344 addr
.sun_family
= AF_UNIX
;
346 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s/%d", SOCKETDIR
, mypid
);
347 if(result
>= UNIX_PATH_MAX
) {
348 ERR("string overflow allocating socket name");
351 //DBG("opening socket at %s", addr.sun_path);
353 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
359 strcpy(mysocketfile
, addr
.sun_path
);
370 static void destroy_socket(void)
374 if(mysocketfile
[0] == '\0')
377 result
= unlink(mysocketfile
);
383 static int init_signal_handler(void)
385 /* Attempt to handler SIGIO. If the main program wants to
386 * handle it, fine, it'll override us. They it'll have to
387 * use the chaining function.
391 struct sigaction act
;
393 result
= sigemptyset(&act
.sa_mask
);
395 PERROR("sigemptyset");
399 act
.sa_handler
= sighandler
;
400 act
.sa_flags
= SA_RESTART
;
402 /* Only defer ourselves. Also, try to restart interrupted
403 * syscalls to disturb the traced program as little as possible.
405 result
= sigaction(SIGIO
, &act
, NULL
);
414 static void auto_probe_connect(struct marker
*m
)
418 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
420 ERR("ltt_marker_connect");
422 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
425 static void __attribute__((constructor(101))) init0()
427 DBG("UST_AUTOPROBE constructor");
428 if(getenv("UST_AUTOPROBE")) {
429 marker_set_new_marker_cb(auto_probe_connect
);
433 static void fini(void);
435 static void __attribute__((constructor(1000))) init()
439 DBG("UST_TRACE constructor");
443 if(getenv("UST_TRACE")) {
444 char trace_name
[] = "auto";
445 char trace_type
[] = "ustrelay";
447 DBG("starting early tracing");
449 /* Ensure marker control is initialized */
450 init_marker_control();
452 /* Ensure relay is initialized */
453 init_ustrelay_transport();
455 /* Ensure markers are initialized */
459 ltt_channels_register("ust");
461 result
= ltt_trace_setup(trace_name
);
463 ERR("ltt_trace_setup failed");
467 result
= ltt_trace_set_type(trace_name
, trace_type
);
469 ERR("ltt_trace_set_type failed");
473 result
= ltt_trace_alloc(trace_name
);
475 ERR("ltt_trace_alloc failed");
479 result
= ltt_trace_start(trace_name
);
481 ERR("ltt_trace_start failed");
487 /* Must create socket before signal handler to prevent races
490 result
= init_socket();
492 ERR("init_socket error");
495 result
= init_signal_handler();
497 ERR("init_signal_handler error");
503 /* should decrementally destroy stuff if error */
507 /* This is only called if we terminate normally, not with an unhandled signal,
508 * so we cannot rely on it. */
510 static void __attribute__((destructor
)) fini()
514 /* if trace running, finish it */
516 DBG("destructor stopping traces");
518 result
= ltt_trace_stop("auto");
520 ERR("ltt_trace_stop error");
523 result
= ltt_trace_destroy("auto");
525 ERR("ltt_trace_destroy error");
528 /* FIXME: wait for the consumer to be done */
This page took 0.039645 seconds and 4 git commands to generate.