a25bd3b214dd0018b2765f3363ec2296e7d6a4de
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
-1), 0, &addr
, &addrlen
);
237 if(recvbuf
[len
-1] == '\n')
238 recvbuf
[len
-1] = '\0';
242 fprintf(stderr
, "received a message! it's: %s\n", recvbuf
);
245 if(!strcmp(recvbuf
, "print_markers")) {
248 else if(!strcmp(recvbuf
, "trace_setup")) {
251 result
= ltt_trace_setup(trace_name
);
253 ERR("ltt_trace_setup failed");
257 result
= ltt_trace_set_type(trace_name
, trace_type
);
259 ERR("ltt_trace_set_type failed");
263 else if(!strcmp(recvbuf
, "trace_alloc")) {
266 result
= ltt_trace_alloc(trace_name
);
268 ERR("ltt_trace_alloc failed");
272 else if(!strcmp(recvbuf
, "trace_start")) {
275 result
= ltt_trace_start(trace_name
);
277 ERR("ltt_trace_start failed");
281 else if(!strcmp(recvbuf
, "trace_stop")) {
284 result
= ltt_trace_stop(trace_name
);
286 ERR("ltt_trace_stop failed");
290 else if(!strcmp(recvbuf
, "trace_destroy")) {
292 DBG("trace destroy");
294 result
= ltt_trace_destroy(trace_name
);
296 ERR("ltt_trace_destroy failed");
305 void create_listener(void)
308 static char listener_stack
[16384];
310 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
316 /* The signal handler itself. */
318 void sighandler(int sig
)
324 /* Called by the app signal handler to chain it to us. */
326 void chain_signal(void)
328 sighandler(USTSIGNAL
);
331 static int init_socket(void)
338 struct sockaddr_un addr
;
340 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
346 addr
.sun_family
= AF_UNIX
;
348 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s/%d", SOCKETDIR
, mypid
);
349 if(result
>= UNIX_PATH_MAX
) {
350 ERR("string overflow allocating socket name");
353 //DBG("opening socket at %s", addr.sun_path);
355 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
361 strcpy(mysocketfile
, addr
.sun_path
);
372 static void destroy_socket(void)
376 if(mysocketfile
[0] == '\0')
379 result
= unlink(mysocketfile
);
385 static int init_signal_handler(void)
387 /* Attempt to handler SIGIO. If the main program wants to
388 * handle it, fine, it'll override us. They it'll have to
389 * use the chaining function.
393 struct sigaction act
;
395 result
= sigemptyset(&act
.sa_mask
);
397 PERROR("sigemptyset");
401 act
.sa_handler
= sighandler
;
402 act
.sa_flags
= SA_RESTART
;
404 /* Only defer ourselves. Also, try to restart interrupted
405 * syscalls to disturb the traced program as little as possible.
407 result
= sigaction(SIGIO
, &act
, NULL
);
416 static void auto_probe_connect(struct marker
*m
)
420 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
422 ERR("ltt_marker_connect");
424 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
427 static void __attribute__((constructor(101))) init0()
429 DBG("UST_AUTOPROBE constructor");
430 if(getenv("UST_AUTOPROBE")) {
431 marker_set_new_marker_cb(auto_probe_connect
);
435 static void fini(void);
437 static void __attribute__((constructor(1000))) init()
441 DBG("UST_TRACE constructor");
445 if(getenv("UST_TRACE")) {
446 char trace_name
[] = "auto";
447 char trace_type
[] = "ustrelay";
449 DBG("starting early tracing");
451 /* Ensure marker control is initialized */
452 init_marker_control();
454 /* Ensure relay is initialized */
455 init_ustrelay_transport();
457 /* Ensure markers are initialized */
461 ltt_channels_register("ust");
463 result
= ltt_trace_setup(trace_name
);
465 ERR("ltt_trace_setup failed");
469 result
= ltt_trace_set_type(trace_name
, trace_type
);
471 ERR("ltt_trace_set_type failed");
475 result
= ltt_trace_alloc(trace_name
);
477 ERR("ltt_trace_alloc failed");
481 result
= ltt_trace_start(trace_name
);
483 ERR("ltt_trace_start failed");
489 /* Must create socket before signal handler to prevent races
492 result
= init_socket();
494 ERR("init_socket error");
497 result
= init_signal_handler();
499 ERR("init_signal_handler error");
505 /* should decrementally destroy stuff if error */
509 /* This is only called if we terminate normally, not with an unhandled signal,
510 * so we cannot rely on it. */
512 static void __attribute__((destructor
)) fini()
516 /* if trace running, finish it */
518 DBG("destructor stopping traces");
520 result
= ltt_trace_stop("auto");
522 ERR("ltt_trace_stop error");
525 result
= ltt_trace_destroy("auto");
527 ERR("ltt_trace_destroy error");
530 /* FIXME: wait for the consumer to be done */
This page took 0.093079 seconds and 4 git commands to generate.