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 DBG(fmt, args...) fprintf(stderr, fmt "\n", ## args)
22 #define WARN(fmt, args...) fprintf(stderr, "usertrace: WARNING: " fmt "\n", ## args)
23 #define ERR(fmt, args...) fprintf(stderr, "usertrace: ERROR: " fmt "\n", ## args); fflush(stderr)
24 #define PERROR(call) perror("usertrace: ERROR: " call)
26 #define MAX_MSG_SIZE (100)
28 #define MSG_REGISTER_NOTIF 2
30 char consumer_stack
[10000];
32 struct tracecmd
{ /* no padding */
37 //struct listener_arg {
42 /* size: the size of all the fields except size itself */
45 /* Only the necessary part of the payload is transferred. It
46 * may even be none of it.
52 char mysocketfile
[UNIX_PATH_MAX
] = "";
55 struct consumer_channel
{
57 struct ltt_channel_struct
*chan
;
60 int consumer(void *arg
)
64 char str
[] = "Hello, this is the consumer.\n";
65 struct ltt_trace_struct
*trace
;
66 struct consumer_channel
*consumer_channels
;
68 char trace_name
[] = "auto";
71 trace
= _ltt_trace_find(trace_name
);
75 CPRINTF("cannot find trace!");
79 consumer_channels
= (struct consumer_channel
*) malloc(trace
->nr_channels
* sizeof(struct consumer_channel
));
80 if(consumer_channels
== NULL
) {
81 ERR("malloc returned NULL");
85 CPRINTF("opening trace files");
86 for(i
=0; i
<trace
->nr_channels
; i
++) {
88 struct ltt_channel_struct
*chan
= &trace
->channels
[i
];
90 consumer_channels
[i
].chan
= chan
;
92 snprintf(tmp
, sizeof(tmp
), "trace/%s", chan
->channel_name
);
93 result
= consumer_channels
[i
].fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00644);
98 CPRINTF("\topened trace file %s", tmp
);
101 CPRINTF("done opening trace files");
106 for(i
=0; i
<trace
->nr_channels
; i
++) {
107 struct rchan
*rchan
= consumer_channels
[i
].chan
->trans_channel_data
;
108 struct rchan_buf
*rbuf
= rchan
->buf
;
109 struct ltt_channel_buf_struct
*lttbuf
= consumer_channels
[i
].chan
->buf
;
112 result
= ltt_do_get_subbuf(rbuf
, lttbuf
, &consumed_old
);
114 CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result
));
119 result
= write(consumer_channels
[i
].fd
, rbuf
->buf_data
+ (consumed_old
& (2 * 4096-1)), 4096);
120 ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
127 // CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
129 // struct ltt_channel_struct *chan = &trace->channels[0];
131 // CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
133 // struct rchan *rchan = chan->trans_channel_data;
134 // struct rchan_buf *rbuf = rchan->buf;
135 // struct ltt_channel_buf_struct *lttbuf = chan->buf;
136 // long consumed_old;
138 // result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
139 // if(result == -1) {
145 // write(STDOUT_FILENO, str, sizeof(str));
147 // result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
149 // CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
152 // CPRINTF("success!");
154 // result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
155 // ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
158 // //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(<tbuf->offset), rbuf->chan) - consumed_old);
159 // CPRINTF("Commit count %ld", local_read(<tbuf->commit_count[0]));
166 void start_consumer(void)
170 result
= clone(consumer
, consumer_stack
+sizeof(consumer_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
176 static void print_markers(void)
178 struct marker_iter iter
;
180 marker_iter_reset(&iter
);
181 marker_iter_start(&iter
);
184 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
185 marker_iter_next(&iter
);
189 void do_command(struct tracecmd
*cmd
)
193 void receive_commands()
201 struct trctl_msg msg
;
203 /* FIXME: fd_notif should probably be protected by a spinlock */
208 msg
.type
= MSG_NOTIF
;
209 msg
.size
= sizeof(msg
.type
);
211 /* FIXME: don't block here */
212 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
221 int listener_main(void *p
)
227 struct sockaddr_un addr
;
228 socklen_t addrlen
= sizeof(addr
);
229 char trace_name
[] = "auto";
230 char trace_type
[] = "ustrelay";
233 struct trctl_msg msg
;
236 result
= len
= recvfrom(pfd
, recvbuf
, sizeof(recvbuf
), 0, &addr
, &addrlen
);
242 if(recvbuf
[len
-1] == '\n')
243 recvbuf
[len
-1] = '\0';
245 fprintf(stderr
, "received a message! it's: %s\n", recvbuf
);
248 if(!strcmp(recvbuf
, "print_markers")) {
251 else if(!strcmp(recvbuf
, "trace_setup")) {
254 result
= ltt_trace_setup(trace_name
);
256 ERR("ltt_trace_setup failed");
260 result
= ltt_trace_set_type(trace_name
, trace_type
);
262 ERR("ltt_trace_set_type failed");
266 else if(!strcmp(recvbuf
, "trace_alloc")) {
269 result
= ltt_trace_alloc(trace_name
);
271 ERR("ltt_trace_alloc failed");
275 else if(!strcmp(recvbuf
, "trace_start")) {
278 result
= ltt_trace_start(trace_name
);
280 ERR("ltt_trace_start failed");
284 else if(!strcmp(recvbuf
, "trace_stop")) {
287 result
= ltt_trace_stop(trace_name
);
289 ERR("ltt_trace_stop failed");
293 else if(!strcmp(recvbuf
, "trace_destroy")) {
295 DBG("trace destroy");
297 result
= ltt_trace_destroy(trace_name
);
299 ERR("ltt_trace_destroy failed");
308 void create_listener(void)
311 static char listener_stack
[16384];
313 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
319 /* The signal handler itself. */
321 void sighandler(int sig
)
327 /* Called by the app signal handler to chain it to us. */
329 void chain_signal(void)
331 sighandler(USTSIGNAL
);
334 static int init_socket(void)
341 struct sockaddr_un addr
;
343 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
349 addr
.sun_family
= AF_UNIX
;
351 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s/%d", SOCKETDIR
, mypid
);
352 if(result
>= UNIX_PATH_MAX
) {
353 ERR("string overflow allocating socket name");
356 //DBG("opening socket at %s", addr.sun_path);
358 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
364 strcpy(mysocketfile
, addr
.sun_path
);
375 static void destroy_socket(void)
379 if(mysocketfile
[0] == '\0')
382 result
= unlink(mysocketfile
);
388 static int init_signal_handler(void)
390 /* Attempt to handler SIGIO. If the main program wants to
391 * handle it, fine, it'll override us. They it'll have to
392 * use the chaining function.
396 struct sigaction act
;
398 result
= sigemptyset(&act
.sa_mask
);
400 PERROR("sigemptyset");
404 act
.sa_handler
= sighandler
;
405 act
.sa_flags
= SA_RESTART
;
407 /* Only defer ourselves. Also, try to restart interrupted
408 * syscalls to disturb the traced program as little as possible.
410 result
= sigaction(SIGIO
, &act
, NULL
);
419 static void auto_probe_connect(struct marker
*m
)
423 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
425 ERR("ltt_marker_connect");
427 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
430 static void __attribute__((constructor(101))) init0()
432 DBG("UST_AUTOPROBE constructor");
433 if(getenv("UST_AUTOPROBE")) {
434 marker_set_new_marker_cb(auto_probe_connect
);
438 static void fini(void);
440 static void __attribute__((constructor(1000))) init()
444 DBG("UST_TRACE constructor");
448 if(getenv("UST_TRACE")) {
449 char trace_name
[] = "auto";
450 char trace_type
[] = "ustrelay";
452 DBG("starting early tracing");
454 /* Ensure marker control is initialized */
455 init_marker_control();
457 /* Ensure relay is initialized */
458 init_ustrelay_transport();
460 /* Ensure markers are initialized */
464 ltt_channels_register("ust");
466 result
= ltt_trace_setup(trace_name
);
468 ERR("ltt_trace_setup failed");
472 result
= ltt_trace_set_type(trace_name
, trace_type
);
474 ERR("ltt_trace_set_type failed");
478 result
= ltt_trace_alloc(trace_name
);
480 ERR("ltt_trace_alloc failed");
484 result
= ltt_trace_start(trace_name
);
486 ERR("ltt_trace_start failed");
492 /* Must create socket before signal handler to prevent races
495 result
= init_socket();
497 ERR("init_socket error");
500 result
= init_signal_handler();
502 ERR("init_signal_handler error");
508 /* should decrementally destroy stuff if error */
512 /* This is only called if we terminate normally, not with an unhandled signal,
513 * so we cannot rely on it. */
515 static void __attribute__((destructor
)) fini()
519 /* if trace running, finish it */
521 DBG("destructor stopping traces");
523 result
= ltt_trace_stop("auto");
525 ERR("ltt_trace_stop error");
528 result
= ltt_trace_destroy("auto");
530 ERR("ltt_trace_destroy error");
533 /* FIXME: wait for the consumer to be done */
This page took 0.03872 seconds and 4 git commands to generate.