50cb3038882a19b055e4bdd8600ea83d33acf3e9
[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 UNIX_PATH_MAX 108
16
17 #define SOCKETDIR "/tmp/socks"
18 #define SOCKETDIRLEN sizeof(SOCKETDIR)
19 #define USTSIGNAL SIGIO
20
21 #define MAX_MSG_SIZE (100)
22 #define MSG_NOTIF 1
23 #define MSG_REGISTER_NOTIF 2
24
25 char consumer_stack[10000];
26
27 static struct ustcomm_app ustcomm_app;
28
29 struct tracecmd { /* no padding */
30 uint32_t size;
31 uint16_t command;
32 };
33
34 //struct listener_arg {
35 // int pipe_fd;
36 //};
37
38 struct 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 };
47
48 pid_t mypid;
49 char mysocketfile[UNIX_PATH_MAX] = "";
50 //int pfd = -1;
51
52 struct consumer_channel {
53 int fd;
54 struct ltt_channel_struct *chan;
55 };
56
57 int 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
89 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
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) {
111 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
112 }
113 else {
114 DBG("success!");
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
163 void 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 // pthread_t thread;
172 //
173 // pthread_create(&thread, NULL, consumer, NULL);
174 }
175
176 static void print_markers(void)
177 {
178 struct marker_iter iter;
179
180 lock_markers();
181 marker_iter_reset(&iter);
182 marker_iter_start(&iter);
183
184 while(iter.marker) {
185 fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format);
186 marker_iter_next(&iter);
187 }
188 unlock_markers();
189 }
190
191 void do_command(struct tracecmd *cmd)
192 {
193 }
194
195 void receive_commands()
196 {
197 }
198
199 int fd_notif = -1;
200 void notif_cb(void)
201 {
202 int result;
203 struct trctl_msg msg;
204
205 /* FIXME: fd_notif should probably be protected by a spinlock */
206
207 if(fd_notif == -1)
208 return;
209
210 msg.type = MSG_NOTIF;
211 msg.size = sizeof(msg.type);
212
213 /* FIXME: don't block here */
214 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
215 if(result == -1) {
216 PERROR("write");
217 return;
218 }
219 }
220
221 #define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
222
223 static int inform_consumer_daemon(void)
224 {
225 }
226
227 int listener_main(void *p)
228 {
229 int result;
230
231 DBG("LISTENER");
232
233 for(;;) {
234 uint32_t size;
235 struct sockaddr_un addr;
236 socklen_t addrlen = sizeof(addr);
237 char trace_name[] = "auto";
238 char trace_type[] = "ustrelay";
239 char *recvbuf;
240 int len;
241
242 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf);
243 DBG("HERE");
244 if(result) {
245 WARN("error in ustcomm_app_recv_message");
246 continue;
247 }
248
249 DBG("received a message! it's: %s\n", recvbuf);
250 len = strlen(recvbuf);
251 //if(len && recvbuf[len-1] == '\n') {
252 // recvbuf[len-1] = '\0';
253 //}
254
255 if(!strcmp(recvbuf, "print_markers")) {
256 print_markers();
257 }
258 else if(!strcmp(recvbuf, "trace_setup")) {
259 DBG("trace setup");
260
261 result = ltt_trace_setup(trace_name);
262 if(result < 0) {
263 ERR("ltt_trace_setup failed");
264 return;
265 }
266
267 result = ltt_trace_set_type(trace_name, trace_type);
268 if(result < 0) {
269 ERR("ltt_trace_set_type failed");
270 return;
271 }
272 }
273 else if(!strcmp(recvbuf, "trace_alloc")) {
274 DBG("trace alloc");
275
276 result = ltt_trace_alloc(trace_name);
277 if(result < 0) {
278 ERR("ltt_trace_alloc failed");
279 return;
280 }
281 }
282 else if(!strcmp(recvbuf, "trace_start")) {
283 DBG("trace start");
284
285 result = ltt_trace_start(trace_name);
286 if(result < 0) {
287 ERR("ltt_trace_start failed");
288 continue;
289 }
290 }
291 else if(!strcmp(recvbuf, "trace_stop")) {
292 DBG("trace stop");
293
294 result = ltt_trace_stop(trace_name);
295 if(result < 0) {
296 ERR("ltt_trace_stop failed");
297 return;
298 }
299 }
300 else if(!strcmp(recvbuf, "trace_destroy")) {
301
302 DBG("trace destroy");
303
304 result = ltt_trace_destroy(trace_name);
305 if(result < 0) {
306 ERR("ltt_trace_destroy failed");
307 return;
308 }
309 }
310
311 free(recvbuf);
312 }
313 }
314
315 static char listener_stack[16384];
316
317 void create_listener(void)
318 {
319 int result;
320 static char listener_stack[16384];
321 //char *listener_stack = malloc(16384);
322
323 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
324 if(result == -1) {
325 perror("clone");
326 }
327 //pthread_t thread;
328
329 //pthread_create(&thread, NULL, listener_main, NULL);
330 }
331
332 /* The signal handler itself. Signals must be setup so there cannot be
333 nested signals. */
334
335 void sighandler(int sig)
336 {
337 static char have_listener = 0;
338 DBG("sighandler");
339
340 if(!have_listener) {
341 create_listener();
342 have_listener = 1;
343 }
344 }
345
346 /* Called by the app signal handler to chain it to us. */
347
348 void chain_signal(void)
349 {
350 sighandler(USTSIGNAL);
351 }
352
353 static int init_socket(void)
354 {
355 return ustcomm_init_app(getpid(), &ustcomm_app);
356 }
357
358 static void destroy_socket(void)
359 {
360 int result;
361
362 if(mysocketfile[0] == '\0')
363 return;
364
365 result = unlink(mysocketfile);
366 if(result == -1) {
367 PERROR("unlink");
368 }
369 }
370
371 static int init_signal_handler(void)
372 {
373 /* Attempt to handler SIGIO. If the main program wants to
374 * handle it, fine, it'll override us. They it'll have to
375 * use the chaining function.
376 */
377
378 int result;
379 struct sigaction act;
380
381 result = sigemptyset(&act.sa_mask);
382 if(result == -1) {
383 PERROR("sigemptyset");
384 return -1;
385 }
386
387 act.sa_handler = sighandler;
388 act.sa_flags = SA_RESTART;
389
390 /* Only defer ourselves. Also, try to restart interrupted
391 * syscalls to disturb the traced program as little as possible.
392 */
393 result = sigaction(SIGIO, &act, NULL);
394 if(result == -1) {
395 PERROR("sigaction");
396 return -1;
397 }
398
399 return 0;
400 }
401
402 static void auto_probe_connect(struct marker *m)
403 {
404 int result;
405
406 result = ltt_marker_connect(m->channel, m->name, "default");
407 if(result)
408 ERR("ltt_marker_connect");
409
410 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
411 }
412
413 static void __attribute__((constructor(101))) init0()
414 {
415 DBG("UST_AUTOPROBE constructor");
416 if(getenv("UST_AUTOPROBE")) {
417 marker_set_new_marker_cb(auto_probe_connect);
418 }
419 }
420
421 static void fini(void);
422
423 static void __attribute__((constructor(1000))) init()
424 {
425 int result;
426
427 DBG("UST_TRACE constructor");
428
429 mypid = getpid();
430
431 if(getenv("UST_TRACE")) {
432 char trace_name[] = "auto";
433 char trace_type[] = "ustrelay";
434
435 DBG("starting early tracing");
436
437 /* Ensure marker control is initialized */
438 init_marker_control();
439
440 /* Ensure relay is initialized */
441 init_ustrelay_transport();
442
443 /* Ensure markers are initialized */
444 init_markers();
445
446 /* In case. */
447 ltt_channels_register("ust");
448
449 result = ltt_trace_setup(trace_name);
450 if(result < 0) {
451 ERR("ltt_trace_setup failed");
452 return;
453 }
454
455 result = ltt_trace_set_type(trace_name, trace_type);
456 if(result < 0) {
457 ERR("ltt_trace_set_type failed");
458 return;
459 }
460
461 result = ltt_trace_alloc(trace_name);
462 if(result < 0) {
463 ERR("ltt_trace_alloc failed");
464 return;
465 }
466
467 result = ltt_trace_start(trace_name);
468 if(result < 0) {
469 ERR("ltt_trace_start failed");
470 return;
471 }
472 start_consumer();
473 }
474
475 /* Must create socket before signal handler to prevent races.
476 */
477 result = init_socket();
478 if(result == -1) {
479 ERR("init_socket error");
480 return;
481 }
482 result = init_signal_handler();
483 if(result == -1) {
484 ERR("init_signal_handler error");
485 return;
486 }
487
488 return;
489
490 /* should decrementally destroy stuff if error */
491
492 }
493
494 /* This is only called if we terminate normally, not with an unhandled signal,
495 * so we cannot rely on it. */
496
497 static void __attribute__((destructor)) fini()
498 {
499 int result;
500
501 /* if trace running, finish it */
502
503 DBG("destructor stopping traces");
504
505 result = ltt_trace_stop("auto");
506 if(result == -1) {
507 ERR("ltt_trace_stop error");
508 }
509
510 result = ltt_trace_destroy("auto");
511 if(result == -1) {
512 ERR("ltt_trace_destroy error");
513 }
514
515 /* FIXME: wait for the consumer to be done */
516 sleep(1);
517
518 destroy_socket();
519 }
This page took 0.038644 seconds and 3 git commands to generate.