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