ust: continue moving communication stuff to libustcomm and cleanup
[ust.git] / libtracectl / tracectl.c
CommitLineData
68c1021b
PMF
1#include <stdio.h>
2#include <stdint.h>
3#include <signal.h>
4#include <sys/types.h>
5#include <sys/socket.h>
6#include <sys/un.h>
98963de4 7#include <sched.h>
a584bc4e 8#include <fcntl.h>
fbd8191b
PMF
9
10#include "marker.h"
a584bc4e 11#include "tracer.h"
d0b5f2b9
PMF
12#include "localerr.h"
13#include "ustcomm.h"
fbd8191b 14
68c1021b
PMF
15#define UNIX_PATH_MAX 108
16
68c1021b
PMF
17#define SOCKETDIR "/tmp/socks"
18#define SOCKETDIRLEN sizeof(SOCKETDIR)
19#define USTSIGNAL SIGIO
20
98963de4
PMF
21#define MAX_MSG_SIZE (100)
22#define MSG_NOTIF 1
23#define MSG_REGISTER_NOTIF 2
24
a584bc4e
PMF
25char consumer_stack[10000];
26
d0b5f2b9
PMF
27static struct ustcomm_app ustcomm_app;
28
68c1021b
PMF
29struct tracecmd { /* no padding */
30 uint32_t size;
31 uint16_t command;
32};
33
98963de4
PMF
34//struct listener_arg {
35// int pipe_fd;
36//};
37
38struct trctl_msg {
39 /* size: the size of all the fields except size itself */
40 uint32_t size;
41 uint16_t type;
42 /* Only the necessary part of the payload is transferred. It
43 * may even be none of it.
44 */
45 char payload[94];
46};
68c1021b
PMF
47
48pid_t mypid;
49char mysocketfile[UNIX_PATH_MAX] = "";
d0b5f2b9 50//int pfd = -1;
68c1021b 51
a584bc4e
PMF
52struct consumer_channel {
53 int fd;
54 struct ltt_channel_struct *chan;
55};
56
57int consumer(void *arg)
58{
59 int result;
60 int fd;
61 char str[] = "Hello, this is the consumer.\n";
62 struct ltt_trace_struct *trace;
63 struct consumer_channel *consumer_channels;
64 int i;
65 char trace_name[] = "auto";
66
67 ltt_lock_traces();
68 trace = _ltt_trace_find(trace_name);
69 ltt_unlock_traces();
70
71 if(trace == NULL) {
72 CPRINTF("cannot find trace!");
73 return 1;
74 }
75
76 consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel));
77 if(consumer_channels == NULL) {
78 ERR("malloc returned NULL");
79 return 1;
80 }
81
82 CPRINTF("opening trace files");
83 for(i=0; i<trace->nr_channels; i++) {
84 char tmp[100];
85 struct ltt_channel_struct *chan = &trace->channels[i];
86
87 consumer_channels[i].chan = chan;
88
1c184644 89 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
a584bc4e
PMF
90 result = consumer_channels[i].fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 00644);
91 if(result == -1) {
92 perror("open");
93 return -1;
94 }
95 CPRINTF("\topened trace file %s", tmp);
96
97 }
98 CPRINTF("done opening trace files");
99
100 for(;;) {
101 /*wait*/
102
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;
107 long consumed_old;
108
109 result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
110 if(result < 0) {
1c184644 111 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
a584bc4e
PMF
112 }
113 else {
1c184644 114 DBG("success!");
a584bc4e
PMF
115
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);
118 }
119 }
120
121 sleep(1);
122 }
123
124// CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
125//
126// struct ltt_channel_struct *chan = &trace->channels[0];
127//
128// CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
129
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;
134//
135// result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
136// if(result == -1) {
137// perror("open");
138// return -1;
139// }
140
141// for(;;) {
142// write(STDOUT_FILENO, str, sizeof(str));
143//
144// result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
145// if(result < 0) {
146// CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
147// }
148// else {
149// CPRINTF("success!");
150//
151// result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
152// ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
153// }
154//
155// //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(&lttbuf->offset), rbuf->chan) - consumed_old);
156// CPRINTF("Commit count %ld", local_read(&lttbuf->commit_count[0]));
157//
158//
159// sleep(1);
160// }
161}
162
163void start_consumer(void)
164{
165 int result;
166
167 result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
168 if(result == -1) {
169 perror("clone");
170 }
171}
fbd8191b
PMF
172
173static void print_markers(void)
174{
175 struct marker_iter iter;
176
d0b5f2b9 177 lock_markers();
fbd8191b
PMF
178 marker_iter_reset(&iter);
179 marker_iter_start(&iter);
180
181 while(iter.marker) {
182 fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format);
183 marker_iter_next(&iter);
184 }
d0b5f2b9 185 unlock_markers();
fbd8191b
PMF
186}
187
68c1021b
PMF
188void do_command(struct tracecmd *cmd)
189{
190}
191
192void receive_commands()
193{
194}
195
98963de4
PMF
196int fd_notif = -1;
197void notif_cb(void)
198{
199 int result;
200 struct trctl_msg msg;
201
202 /* FIXME: fd_notif should probably be protected by a spinlock */
203
204 if(fd_notif == -1)
205 return;
206
207 msg.type = MSG_NOTIF;
208 msg.size = sizeof(msg.type);
209
210 /* FIXME: don't block here */
211 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
212 if(result == -1) {
213 PERROR("write");
214 return;
215 }
216}
217
d0b5f2b9
PMF
218#define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
219
220static int inform_consumer_daemon(void)
221{
222}
fbd8191b 223
98963de4
PMF
224int listener_main(void *p)
225{
226 int result;
227
98963de4 228 for(;;) {
98963de4 229 uint32_t size;
98963de4
PMF
230 struct sockaddr_un addr;
231 socklen_t addrlen = sizeof(addr);
aafb1650
PMF
232 char trace_name[] = "auto";
233 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
234 char *recvbuf;
235 int len;
98963de4 236
d0b5f2b9
PMF
237 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf);
238 if(result) {
239 WARN("error in ustcomm_app_recv_message");
240 continue;
241 }
98963de4 242
d0b5f2b9
PMF
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';
247 }
98963de4 248
d0b5f2b9
PMF
249 if(!strcmp(recvbuf, "print_markers")) {
250 print_markers();
251 }
252 else if(!strcmp(recvbuf, "trace_setup")) {
253 DBG("trace setup");
fbd8191b 254
d0b5f2b9
PMF
255 result = ltt_trace_setup(trace_name);
256 if(result < 0) {
257 ERR("ltt_trace_setup failed");
258 return;
fbd8191b 259 }
d0b5f2b9
PMF
260
261 result = ltt_trace_set_type(trace_name, trace_type);
262 if(result < 0) {
263 ERR("ltt_trace_set_type failed");
264 return;
fbd8191b 265 }
d0b5f2b9
PMF
266 }
267 else if(!strcmp(recvbuf, "trace_alloc")) {
268 DBG("trace alloc");
269
270 result = ltt_trace_alloc(trace_name);
271 if(result < 0) {
272 ERR("ltt_trace_alloc failed");
273 return;
fbd8191b 274 }
d0b5f2b9
PMF
275 }
276 else if(!strcmp(recvbuf, "trace_start")) {
277 DBG("trace start");
278
279 result = ltt_trace_start(trace_name);
280 if(result < 0) {
281 ERR("ltt_trace_start failed");
282 continue;
fbd8191b 283 }
d0b5f2b9
PMF
284 }
285 else if(!strcmp(recvbuf, "trace_stop")) {
286 DBG("trace stop");
287
288 result = ltt_trace_stop(trace_name);
289 if(result < 0) {
290 ERR("ltt_trace_stop failed");
291 return;
aafb1650 292 }
d0b5f2b9
PMF
293 }
294 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 295
d0b5f2b9 296 DBG("trace destroy");
aafb1650 297
d0b5f2b9
PMF
298 result = ltt_trace_destroy(trace_name);
299 if(result < 0) {
300 ERR("ltt_trace_destroy failed");
301 return;
fbd8191b 302 }
98963de4 303 }
d0b5f2b9
PMF
304
305 free(recvbuf);
98963de4
PMF
306 }
307}
308
309void create_listener(void)
310{
311 int result;
312 static char listener_stack[16384];
313
fbd8191b 314 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
315 if(result == -1) {
316 perror("clone");
317 }
318}
319
d0b5f2b9
PMF
320/* The signal handler itself. Signals must be setup so there cannot be
321 nested signals. */
68c1021b
PMF
322
323void sighandler(int sig)
324{
d0b5f2b9 325 static char have_listener = 0;
68c1021b 326 DBG("sighandler");
d0b5f2b9
PMF
327
328 if(!have_listener) {
329 create_listener();
330 have_listener = 1;
331 }
68c1021b
PMF
332}
333
334/* Called by the app signal handler to chain it to us. */
335
98963de4 336void chain_signal(void)
68c1021b
PMF
337{
338 sighandler(USTSIGNAL);
339}
340
98963de4 341static int init_socket(void)
68c1021b 342{
d0b5f2b9 343 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
344}
345
98963de4 346static void destroy_socket(void)
68c1021b
PMF
347{
348 int result;
349
350 if(mysocketfile[0] == '\0')
351 return;
352
353 result = unlink(mysocketfile);
354 if(result == -1) {
355 PERROR("unlink");
356 }
357}
358
98963de4 359static int init_signal_handler(void)
68c1021b
PMF
360{
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.
364 */
365
366 int result;
367 struct sigaction act;
368
369 result = sigemptyset(&act.sa_mask);
370 if(result == -1) {
371 PERROR("sigemptyset");
372 return -1;
373 }
374
375 act.sa_handler = sighandler;
376 act.sa_flags = SA_RESTART;
377
378 /* Only defer ourselves. Also, try to restart interrupted
379 * syscalls to disturb the traced program as little as possible.
380 */
381 result = sigaction(SIGIO, &act, NULL);
382 if(result == -1) {
383 PERROR("sigaction");
384 return -1;
385 }
386
387 return 0;
388}
389
20b37a31 390static void auto_probe_connect(struct marker *m)
68c1021b
PMF
391{
392 int result;
393
20b37a31
PMF
394 result = ltt_marker_connect(m->channel, m->name, "default");
395 if(result)
396 ERR("ltt_marker_connect");
397
398 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
399}
400
401static void __attribute__((constructor(101))) init0()
402{
403 DBG("UST_AUTOPROBE constructor");
404 if(getenv("UST_AUTOPROBE")) {
405 marker_set_new_marker_cb(auto_probe_connect);
406 }
407}
408
a584bc4e
PMF
409static void fini(void);
410
20b37a31
PMF
411static void __attribute__((constructor(1000))) init()
412{
413 int result;
414
415 DBG("UST_TRACE constructor");
416
68c1021b
PMF
417 mypid = getpid();
418
4db647c5
PMF
419 if(getenv("UST_TRACE")) {
420 char trace_name[] = "auto";
421 char trace_type[] = "ustrelay";
422
423 DBG("starting early tracing");
424
425 /* Ensure marker control is initialized */
426 init_marker_control();
427
428 /* Ensure relay is initialized */
429 init_ustrelay_transport();
430
431 /* Ensure markers are initialized */
432 init_markers();
433
20b37a31
PMF
434 /* In case. */
435 ltt_channels_register("ust");
4db647c5
PMF
436
437 result = ltt_trace_setup(trace_name);
438 if(result < 0) {
439 ERR("ltt_trace_setup failed");
440 return;
441 }
442
443 result = ltt_trace_set_type(trace_name, trace_type);
444 if(result < 0) {
445 ERR("ltt_trace_set_type failed");
446 return;
447 }
448
449 result = ltt_trace_alloc(trace_name);
450 if(result < 0) {
451 ERR("ltt_trace_alloc failed");
452 return;
453 }
454
455 result = ltt_trace_start(trace_name);
456 if(result < 0) {
457 ERR("ltt_trace_start failed");
458 return;
459 }
a584bc4e 460 start_consumer();
4db647c5
PMF
461 }
462
98963de4
PMF
463 /* Must create socket before signal handler to prevent races
464 * on pfd variable.
465 */
68c1021b 466 result = init_socket();
98963de4
PMF
467 if(result == -1) {
468 ERR("init_socket error");
469 return;
470 }
471 result = init_signal_handler();
472 if(result == -1) {
473 ERR("init_signal_handler error");
474 return;
475 }
68c1021b
PMF
476
477 return;
478
479 /* should decrementally destroy stuff if error */
480
481}
482
483/* This is only called if we terminate normally, not with an unhandled signal,
484 * so we cannot rely on it. */
485
98963de4 486static void __attribute__((destructor)) fini()
68c1021b 487{
a584bc4e
PMF
488 int result;
489
490 /* if trace running, finish it */
491
492 DBG("destructor stopping traces");
493
494 result = ltt_trace_stop("auto");
495 if(result == -1) {
496 ERR("ltt_trace_stop error");
497 }
498
499 result = ltt_trace_destroy("auto");
500 if(result == -1) {
501 ERR("ltt_trace_destroy error");
502 }
503
504 /* FIXME: wait for the consumer to be done */
d0b5f2b9 505 sleep(1);
a584bc4e 506
68c1021b
PMF
507 destroy_socket();
508}
This page took 0.043168 seconds and 4 git commands to generate.