ust: improve message parsing
[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
b02e31e5 15//#define USE_CLONE
3847c3ba 16
68c1021b
PMF
17#define UNIX_PATH_MAX 108
18
68c1021b
PMF
19#define SOCKETDIR "/tmp/socks"
20#define SOCKETDIRLEN sizeof(SOCKETDIR)
21#define USTSIGNAL SIGIO
22
98963de4
PMF
23#define MAX_MSG_SIZE (100)
24#define MSG_NOTIF 1
25#define MSG_REGISTER_NOTIF 2
26
a584bc4e
PMF
27char consumer_stack[10000];
28
d0b5f2b9
PMF
29static struct ustcomm_app ustcomm_app;
30
68c1021b
PMF
31struct tracecmd { /* no padding */
32 uint32_t size;
33 uint16_t command;
34};
35
98963de4
PMF
36//struct listener_arg {
37// int pipe_fd;
38//};
39
40struct 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};
68c1021b 49
68c1021b 50char mysocketfile[UNIX_PATH_MAX] = "";
d0b5f2b9 51//int pfd = -1;
68c1021b 52
a584bc4e
PMF
53struct consumer_channel {
54 int fd;
55 struct ltt_channel_struct *chan;
56};
57
58int 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
1c184644 90 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
a584bc4e
PMF
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) {
1c184644 112 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
a584bc4e
PMF
113 }
114 else {
1c184644 115 DBG("success!");
a584bc4e
PMF
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
164void start_consumer(void)
165{
3847c3ba 166#ifdef USE_CLONE
a584bc4e
PMF
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 }
3847c3ba
PMF
173#else
174 pthread_t thread;
175
176 pthread_create(&thread, NULL, consumer, NULL);
177#endif
a584bc4e 178}
fbd8191b
PMF
179
180static void print_markers(void)
181{
182 struct marker_iter iter;
183
d0b5f2b9 184 lock_markers();
fbd8191b
PMF
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 }
d0b5f2b9 192 unlock_markers();
fbd8191b
PMF
193}
194
68c1021b
PMF
195void do_command(struct tracecmd *cmd)
196{
197}
198
199void receive_commands()
200{
201}
202
98963de4
PMF
203int fd_notif = -1;
204void 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
d0b5f2b9
PMF
225#define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
226
227static int inform_consumer_daemon(void)
228{
3847c3ba
PMF
229 ustcomm_request_consumer(getpid(), "metadata");
230 ustcomm_request_consumer(getpid(), "ust");
d0b5f2b9 231}
fbd8191b 232
98963de4
PMF
233int listener_main(void *p)
234{
235 int result;
236
b0540e11
PMF
237 DBG("LISTENER");
238
98963de4 239 for(;;) {
98963de4 240 uint32_t size;
98963de4
PMF
241 struct sockaddr_un addr;
242 socklen_t addrlen = sizeof(addr);
aafb1650
PMF
243 char trace_name[] = "auto";
244 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
245 char *recvbuf;
246 int len;
b02e31e5 247 struct ustcomm_source src;
98963de4 248
b02e31e5 249 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src);
b0540e11 250 DBG("HERE");
d0b5f2b9
PMF
251 if(result) {
252 WARN("error in ustcomm_app_recv_message");
253 continue;
254 }
98963de4 255
d0b5f2b9
PMF
256 DBG("received a message! it's: %s\n", recvbuf);
257 len = strlen(recvbuf);
b0540e11
PMF
258 //if(len && recvbuf[len-1] == '\n') {
259 // recvbuf[len-1] = '\0';
260 //}
98963de4 261
d0b5f2b9
PMF
262 if(!strcmp(recvbuf, "print_markers")) {
263 print_markers();
264 }
265 else if(!strcmp(recvbuf, "trace_setup")) {
266 DBG("trace setup");
fbd8191b 267
d0b5f2b9
PMF
268 result = ltt_trace_setup(trace_name);
269 if(result < 0) {
270 ERR("ltt_trace_setup failed");
271 return;
fbd8191b 272 }
d0b5f2b9
PMF
273
274 result = ltt_trace_set_type(trace_name, trace_type);
275 if(result < 0) {
276 ERR("ltt_trace_set_type failed");
277 return;
fbd8191b 278 }
d0b5f2b9
PMF
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;
fbd8191b 287 }
d0b5f2b9
PMF
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;
fbd8191b 296 }
d0b5f2b9
PMF
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;
aafb1650 305 }
d0b5f2b9
PMF
306 }
307 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 308
d0b5f2b9 309 DBG("trace destroy");
aafb1650 310
d0b5f2b9
PMF
311 result = ltt_trace_destroy(trace_name);
312 if(result < 0) {
313 ERR("ltt_trace_destroy failed");
314 return;
fbd8191b 315 }
98963de4 316 }
b02e31e5 317 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
3847c3ba
PMF
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 }
b02e31e5
PMF
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 }
d0b5f2b9
PMF
348
349 free(recvbuf);
98963de4
PMF
350 }
351}
352
b0540e11
PMF
353static char listener_stack[16384];
354
98963de4
PMF
355void create_listener(void)
356{
357 int result;
358 static char listener_stack[16384];
b0540e11 359 //char *listener_stack = malloc(16384);
98963de4 360
3847c3ba 361#ifdef USE_CLONE
fbd8191b 362 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
363 if(result == -1) {
364 perror("clone");
365 }
3847c3ba
PMF
366#else
367 pthread_t thread;
b0540e11 368
3847c3ba
PMF
369 pthread_create(&thread, NULL, listener_main, NULL);
370#endif
98963de4
PMF
371}
372
d0b5f2b9
PMF
373/* The signal handler itself. Signals must be setup so there cannot be
374 nested signals. */
68c1021b
PMF
375
376void sighandler(int sig)
377{
d0b5f2b9 378 static char have_listener = 0;
68c1021b 379 DBG("sighandler");
d0b5f2b9
PMF
380
381 if(!have_listener) {
382 create_listener();
383 have_listener = 1;
384 }
68c1021b
PMF
385}
386
387/* Called by the app signal handler to chain it to us. */
388
98963de4 389void chain_signal(void)
68c1021b
PMF
390{
391 sighandler(USTSIGNAL);
392}
393
98963de4 394static int init_socket(void)
68c1021b 395{
d0b5f2b9 396 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
397}
398
98963de4 399static void destroy_socket(void)
68c1021b
PMF
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
98963de4 412static int init_signal_handler(void)
68c1021b
PMF
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
20b37a31 443static void auto_probe_connect(struct marker *m)
68c1021b
PMF
444{
445 int result;
446
20b37a31
PMF
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
454static 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
a584bc4e
PMF
462static void fini(void);
463
20b37a31
PMF
464static void __attribute__((constructor(1000))) init()
465{
466 int result;
467
468 DBG("UST_TRACE constructor");
469
3847c3ba
PMF
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 }
68c1021b 482
4db647c5
PMF
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
20b37a31
PMF
498 /* In case. */
499 ltt_channels_register("ust");
4db647c5
PMF
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 }
3847c3ba
PMF
524 //start_consumer();
525 inform_consumer_daemon();
4db647c5
PMF
526 }
527
68c1021b
PMF
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
98963de4 538static void __attribute__((destructor)) fini()
68c1021b 539{
a584bc4e
PMF
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 */
d0b5f2b9 557 sleep(1);
a584bc4e 558
68c1021b
PMF
559 destroy_socket();
560}
This page took 0.046133 seconds and 4 git commands to generate.