ust: continue implementation of ustd
[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
248 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf);
249 DBG("HERE");
250 if(result) {
251 WARN("error in ustcomm_app_recv_message");
252 continue;
253 }
254
255 DBG("received a message! it's: %s\n", recvbuf);
256 len = strlen(recvbuf);
257 //if(len && recvbuf[len-1] == '\n') {
258 // recvbuf[len-1] = '\0';
259 //}
260
261 if(!strcmp(recvbuf, "print_markers")) {
262 print_markers();
263 }
264 else if(!strcmp(recvbuf, "trace_setup")) {
265 DBG("trace setup");
266
267 result = ltt_trace_setup(trace_name);
268 if(result < 0) {
269 ERR("ltt_trace_setup failed");
270 return;
271 }
272
273 result = ltt_trace_set_type(trace_name, trace_type);
274 if(result < 0) {
275 ERR("ltt_trace_set_type failed");
276 return;
277 }
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;
286 }
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;
295 }
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;
304 }
305 }
306 else if(!strcmp(recvbuf, "trace_destroy")) {
307
308 DBG("trace destroy");
309
310 result = ltt_trace_destroy(trace_name);
311 if(result < 0) {
312 ERR("ltt_trace_destroy failed");
313 return;
314 }
315 }
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 }
340
341 free(recvbuf);
342 }
343 }
344
345 static char listener_stack[16384];
346
347 void create_listener(void)
348 {
349 int result;
350 static char listener_stack[16384];
351 //char *listener_stack = malloc(16384);
352
353 #ifdef USE_CLONE
354 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
355 if(result == -1) {
356 perror("clone");
357 }
358 #else
359 pthread_t thread;
360
361 pthread_create(&thread, NULL, listener_main, NULL);
362 #endif
363 }
364
365 /* The signal handler itself. Signals must be setup so there cannot be
366 nested signals. */
367
368 void sighandler(int sig)
369 {
370 static char have_listener = 0;
371 DBG("sighandler");
372
373 if(!have_listener) {
374 create_listener();
375 have_listener = 1;
376 }
377 }
378
379 /* Called by the app signal handler to chain it to us. */
380
381 void chain_signal(void)
382 {
383 sighandler(USTSIGNAL);
384 }
385
386 static int init_socket(void)
387 {
388 return ustcomm_init_app(getpid(), &ustcomm_app);
389 }
390
391 static void destroy_socket(void)
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
404 static int init_signal_handler(void)
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
435 static void auto_probe_connect(struct marker *m)
436 {
437 int result;
438
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
446 static 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
454 static void fini(void);
455
456 static void __attribute__((constructor(1000))) init()
457 {
458 int result;
459
460 DBG("UST_TRACE constructor");
461
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 }
474
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
490 /* In case. */
491 ltt_channels_register("ust");
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 }
516 //start_consumer();
517 inform_consumer_daemon();
518 }
519
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
530 static void __attribute__((destructor)) fini()
531 {
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 */
549 sleep(1);
550
551 destroy_socket();
552 }
This page took 0.039332 seconds and 4 git commands to generate.