ust: continue implementation of ustd
[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
3847c3ba
PMF
15#define USE_CLONE
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;
98963de4 247
d0b5f2b9 248 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf);
b0540e11 249 DBG("HERE");
d0b5f2b9
PMF
250 if(result) {
251 WARN("error in ustcomm_app_recv_message");
252 continue;
253 }
98963de4 254
d0b5f2b9
PMF
255 DBG("received a message! it's: %s\n", recvbuf);
256 len = strlen(recvbuf);
b0540e11
PMF
257 //if(len && recvbuf[len-1] == '\n') {
258 // recvbuf[len-1] = '\0';
259 //}
98963de4 260
d0b5f2b9
PMF
261 if(!strcmp(recvbuf, "print_markers")) {
262 print_markers();
263 }
264 else if(!strcmp(recvbuf, "trace_setup")) {
265 DBG("trace setup");
fbd8191b 266
d0b5f2b9
PMF
267 result = ltt_trace_setup(trace_name);
268 if(result < 0) {
269 ERR("ltt_trace_setup failed");
270 return;
fbd8191b 271 }
d0b5f2b9
PMF
272
273 result = ltt_trace_set_type(trace_name, trace_type);
274 if(result < 0) {
275 ERR("ltt_trace_set_type failed");
276 return;
fbd8191b 277 }
d0b5f2b9
PMF
278 }
279 else if(!strcmp(recvbuf, "trace_alloc")) {
280 DBG("trace alloc");
281
282 result = ltt_trace_alloc(trace_name);
283 if(result < 0) {
284 ERR("ltt_trace_alloc failed");
285 return;
fbd8191b 286 }
d0b5f2b9
PMF
287 }
288 else if(!strcmp(recvbuf, "trace_start")) {
289 DBG("trace start");
290
291 result = ltt_trace_start(trace_name);
292 if(result < 0) {
293 ERR("ltt_trace_start failed");
294 continue;
fbd8191b 295 }
d0b5f2b9
PMF
296 }
297 else if(!strcmp(recvbuf, "trace_stop")) {
298 DBG("trace stop");
299
300 result = ltt_trace_stop(trace_name);
301 if(result < 0) {
302 ERR("ltt_trace_stop failed");
303 return;
aafb1650 304 }
d0b5f2b9
PMF
305 }
306 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 307
d0b5f2b9 308 DBG("trace destroy");
aafb1650 309
d0b5f2b9
PMF
310 result = ltt_trace_destroy(trace_name);
311 if(result < 0) {
312 ERR("ltt_trace_destroy failed");
313 return;
fbd8191b 314 }
98963de4 315 }
3847c3ba
PMF
316 else if(!strncmp(recvbuf, "get_shmid ", 10)) {
317 struct ltt_trace_struct *trace;
318 char trace_name[] = "auto";
319 int i;
320
321 DBG("get_shmid");
322
323 ltt_lock_traces();
324 trace = _ltt_trace_find(trace_name);
325 ltt_unlock_traces();
326
327 if(trace == NULL) {
328 CPRINTF("cannot find trace!");
329 return 1;
330 }
331
332 for(i=0; i<trace->nr_channels; i++) {
333 struct rchan *rchan = trace->channels[i].trans_channel_data;
334 struct rchan_buf *rbuf = rchan->buf;
335
336 DBG("the shmid is %d", rbuf->shmid);
337
338 }
339 }
d0b5f2b9
PMF
340
341 free(recvbuf);
98963de4
PMF
342 }
343}
344
b0540e11
PMF
345static char listener_stack[16384];
346
98963de4
PMF
347void create_listener(void)
348{
349 int result;
350 static char listener_stack[16384];
b0540e11 351 //char *listener_stack = malloc(16384);
98963de4 352
3847c3ba 353#ifdef USE_CLONE
fbd8191b 354 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
355 if(result == -1) {
356 perror("clone");
357 }
3847c3ba
PMF
358#else
359 pthread_t thread;
b0540e11 360
3847c3ba
PMF
361 pthread_create(&thread, NULL, listener_main, NULL);
362#endif
98963de4
PMF
363}
364
d0b5f2b9
PMF
365/* The signal handler itself. Signals must be setup so there cannot be
366 nested signals. */
68c1021b
PMF
367
368void sighandler(int sig)
369{
d0b5f2b9 370 static char have_listener = 0;
68c1021b 371 DBG("sighandler");
d0b5f2b9
PMF
372
373 if(!have_listener) {
374 create_listener();
375 have_listener = 1;
376 }
68c1021b
PMF
377}
378
379/* Called by the app signal handler to chain it to us. */
380
98963de4 381void chain_signal(void)
68c1021b
PMF
382{
383 sighandler(USTSIGNAL);
384}
385
98963de4 386static int init_socket(void)
68c1021b 387{
d0b5f2b9 388 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
389}
390
98963de4 391static void destroy_socket(void)
68c1021b
PMF
392{
393 int result;
394
395 if(mysocketfile[0] == '\0')
396 return;
397
398 result = unlink(mysocketfile);
399 if(result == -1) {
400 PERROR("unlink");
401 }
402}
403
98963de4 404static int init_signal_handler(void)
68c1021b
PMF
405{
406 /* Attempt to handler SIGIO. If the main program wants to
407 * handle it, fine, it'll override us. They it'll have to
408 * use the chaining function.
409 */
410
411 int result;
412 struct sigaction act;
413
414 result = sigemptyset(&act.sa_mask);
415 if(result == -1) {
416 PERROR("sigemptyset");
417 return -1;
418 }
419
420 act.sa_handler = sighandler;
421 act.sa_flags = SA_RESTART;
422
423 /* Only defer ourselves. Also, try to restart interrupted
424 * syscalls to disturb the traced program as little as possible.
425 */
426 result = sigaction(SIGIO, &act, NULL);
427 if(result == -1) {
428 PERROR("sigaction");
429 return -1;
430 }
431
432 return 0;
433}
434
20b37a31 435static void auto_probe_connect(struct marker *m)
68c1021b
PMF
436{
437 int result;
438
20b37a31
PMF
439 result = ltt_marker_connect(m->channel, m->name, "default");
440 if(result)
441 ERR("ltt_marker_connect");
442
443 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
444}
445
446static void __attribute__((constructor(101))) init0()
447{
448 DBG("UST_AUTOPROBE constructor");
449 if(getenv("UST_AUTOPROBE")) {
450 marker_set_new_marker_cb(auto_probe_connect);
451 }
452}
453
a584bc4e
PMF
454static void fini(void);
455
20b37a31
PMF
456static void __attribute__((constructor(1000))) init()
457{
458 int result;
459
460 DBG("UST_TRACE constructor");
461
3847c3ba
PMF
462 /* Must create socket before signal handler to prevent races.
463 */
464 result = init_socket();
465 if(result == -1) {
466 ERR("init_socket error");
467 return;
468 }
469 result = init_signal_handler();
470 if(result == -1) {
471 ERR("init_signal_handler error");
472 return;
473 }
68c1021b 474
4db647c5
PMF
475 if(getenv("UST_TRACE")) {
476 char trace_name[] = "auto";
477 char trace_type[] = "ustrelay";
478
479 DBG("starting early tracing");
480
481 /* Ensure marker control is initialized */
482 init_marker_control();
483
484 /* Ensure relay is initialized */
485 init_ustrelay_transport();
486
487 /* Ensure markers are initialized */
488 init_markers();
489
20b37a31
PMF
490 /* In case. */
491 ltt_channels_register("ust");
4db647c5
PMF
492
493 result = ltt_trace_setup(trace_name);
494 if(result < 0) {
495 ERR("ltt_trace_setup failed");
496 return;
497 }
498
499 result = ltt_trace_set_type(trace_name, trace_type);
500 if(result < 0) {
501 ERR("ltt_trace_set_type failed");
502 return;
503 }
504
505 result = ltt_trace_alloc(trace_name);
506 if(result < 0) {
507 ERR("ltt_trace_alloc failed");
508 return;
509 }
510
511 result = ltt_trace_start(trace_name);
512 if(result < 0) {
513 ERR("ltt_trace_start failed");
514 return;
515 }
3847c3ba
PMF
516 //start_consumer();
517 inform_consumer_daemon();
4db647c5
PMF
518 }
519
68c1021b
PMF
520
521 return;
522
523 /* should decrementally destroy stuff if error */
524
525}
526
527/* This is only called if we terminate normally, not with an unhandled signal,
528 * so we cannot rely on it. */
529
98963de4 530static void __attribute__((destructor)) fini()
68c1021b 531{
a584bc4e
PMF
532 int result;
533
534 /* if trace running, finish it */
535
536 DBG("destructor stopping traces");
537
538 result = ltt_trace_stop("auto");
539 if(result == -1) {
540 ERR("ltt_trace_stop error");
541 }
542
543 result = ltt_trace_destroy("auto");
544 if(result == -1) {
545 ERR("ltt_trace_destroy error");
546 }
547
548 /* FIXME: wait for the consumer to be done */
d0b5f2b9 549 sleep(1);
a584bc4e 550
68c1021b
PMF
551 destroy_socket();
552}
This page took 0.044389 seconds and 4 git commands to generate.