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