a25bd3b214dd0018b2765f3363ec2296e7d6a4de
[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 "usterr.h"
13
14 #define UNIX_PATH_MAX 108
15
16 //#define SOCKETDIR "/var/run/ust/socks"
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 struct tracecmd { /* no padding */
28 uint32_t size;
29 uint16_t command;
30 };
31
32 //struct listener_arg {
33 // int pipe_fd;
34 //};
35
36 struct trctl_msg {
37 /* size: the size of all the fields except size itself */
38 uint32_t size;
39 uint16_t type;
40 /* Only the necessary part of the payload is transferred. It
41 * may even be none of it.
42 */
43 char payload[94];
44 };
45
46 pid_t mypid;
47 char mysocketfile[UNIX_PATH_MAX] = "";
48 int pfd = -1;
49
50 struct consumer_channel {
51 int fd;
52 struct ltt_channel_struct *chan;
53 };
54
55 int consumer(void *arg)
56 {
57 int result;
58 int fd;
59 char str[] = "Hello, this is the consumer.\n";
60 struct ltt_trace_struct *trace;
61 struct consumer_channel *consumer_channels;
62 int i;
63 char trace_name[] = "auto";
64
65 ltt_lock_traces();
66 trace = _ltt_trace_find(trace_name);
67 ltt_unlock_traces();
68
69 if(trace == NULL) {
70 CPRINTF("cannot find trace!");
71 return 1;
72 }
73
74 consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel));
75 if(consumer_channels == NULL) {
76 ERR("malloc returned NULL");
77 return 1;
78 }
79
80 CPRINTF("opening trace files");
81 for(i=0; i<trace->nr_channels; i++) {
82 char tmp[100];
83 struct ltt_channel_struct *chan = &trace->channels[i];
84
85 consumer_channels[i].chan = chan;
86
87 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
88 result = consumer_channels[i].fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 00644);
89 if(result == -1) {
90 perror("open");
91 return -1;
92 }
93 CPRINTF("\topened trace file %s", tmp);
94
95 }
96 CPRINTF("done opening trace files");
97
98 for(;;) {
99 /*wait*/
100
101 for(i=0; i<trace->nr_channels; i++) {
102 struct rchan *rchan = consumer_channels[i].chan->trans_channel_data;
103 struct rchan_buf *rbuf = rchan->buf;
104 struct ltt_channel_buf_struct *lttbuf = consumer_channels[i].chan->buf;
105 long consumed_old;
106
107 result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
108 if(result < 0) {
109 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
110 }
111 else {
112 DBG("success!");
113
114 result = write(consumer_channels[i].fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
115 ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
116 }
117 }
118
119 sleep(1);
120 }
121
122 // CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
123 //
124 // struct ltt_channel_struct *chan = &trace->channels[0];
125 //
126 // CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
127
128 // struct rchan *rchan = chan->trans_channel_data;
129 // struct rchan_buf *rbuf = rchan->buf;
130 // struct ltt_channel_buf_struct *lttbuf = chan->buf;
131 // long consumed_old;
132 //
133 // result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
134 // if(result == -1) {
135 // perror("open");
136 // return -1;
137 // }
138
139 // for(;;) {
140 // write(STDOUT_FILENO, str, sizeof(str));
141 //
142 // result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
143 // if(result < 0) {
144 // CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
145 // }
146 // else {
147 // CPRINTF("success!");
148 //
149 // result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
150 // ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
151 // }
152 //
153 // //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(&lttbuf->offset), rbuf->chan) - consumed_old);
154 // CPRINTF("Commit count %ld", local_read(&lttbuf->commit_count[0]));
155 //
156 //
157 // sleep(1);
158 // }
159 }
160
161 void start_consumer(void)
162 {
163 int result;
164
165 result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
166 if(result == -1) {
167 perror("clone");
168 }
169 }
170
171 static void print_markers(void)
172 {
173 struct marker_iter iter;
174
175 marker_iter_reset(&iter);
176 marker_iter_start(&iter);
177
178 while(iter.marker) {
179 fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format);
180 marker_iter_next(&iter);
181 }
182 }
183
184 void do_command(struct tracecmd *cmd)
185 {
186 }
187
188 void receive_commands()
189 {
190 }
191
192 int fd_notif = -1;
193 void notif_cb(void)
194 {
195 int result;
196 struct trctl_msg msg;
197
198 /* FIXME: fd_notif should probably be protected by a spinlock */
199
200 if(fd_notif == -1)
201 return;
202
203 msg.type = MSG_NOTIF;
204 msg.size = sizeof(msg.type);
205
206 /* FIXME: don't block here */
207 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
208 if(result == -1) {
209 PERROR("write");
210 return;
211 }
212 }
213
214 char recvbuf[10000];
215
216 int listener_main(void *p)
217 {
218 int result;
219
220 for(;;) {
221 uint32_t size;
222 struct sockaddr_un addr;
223 socklen_t addrlen = sizeof(addr);
224 char trace_name[] = "auto";
225 char trace_type[] = "ustrelay";
226
227 for(;;) {
228 struct trctl_msg msg;
229 int len;
230
231 result = len = recvfrom(pfd, recvbuf, sizeof(recvbuf-1), 0, &addr, &addrlen);
232 if(result == -1) {
233 PERROR("recvfrom");
234 continue;
235 }
236
237 if(recvbuf[len-1] == '\n')
238 recvbuf[len-1] = '\0';
239 else
240 recvbuf[len] = 0;
241
242 fprintf(stderr, "received a message! it's: %s\n", recvbuf);
243
244
245 if(!strcmp(recvbuf, "print_markers")) {
246 print_markers();
247 }
248 else if(!strcmp(recvbuf, "trace_setup")) {
249 DBG("trace setup");
250
251 result = ltt_trace_setup(trace_name);
252 if(result < 0) {
253 ERR("ltt_trace_setup failed");
254 return;
255 }
256
257 result = ltt_trace_set_type(trace_name, trace_type);
258 if(result < 0) {
259 ERR("ltt_trace_set_type failed");
260 return;
261 }
262 }
263 else if(!strcmp(recvbuf, "trace_alloc")) {
264 DBG("trace alloc");
265
266 result = ltt_trace_alloc(trace_name);
267 if(result < 0) {
268 ERR("ltt_trace_alloc failed");
269 return;
270 }
271 }
272 else if(!strcmp(recvbuf, "trace_start")) {
273 DBG("trace start");
274
275 result = ltt_trace_start(trace_name);
276 if(result < 0) {
277 ERR("ltt_trace_start failed");
278 return;
279 }
280 }
281 else if(!strcmp(recvbuf, "trace_stop")) {
282 DBG("trace stop");
283
284 result = ltt_trace_stop(trace_name);
285 if(result < 0) {
286 ERR("ltt_trace_stop failed");
287 return;
288 }
289 }
290 else if(!strcmp(recvbuf, "trace_destroy")) {
291
292 DBG("trace destroy");
293
294 result = ltt_trace_destroy(trace_name);
295 if(result < 0) {
296 ERR("ltt_trace_destroy failed");
297 return;
298 }
299 }
300 }
301 next_conn:;
302 }
303 }
304
305 void create_listener(void)
306 {
307 int result;
308 static char listener_stack[16384];
309
310 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
311 if(result == -1) {
312 perror("clone");
313 }
314 }
315
316 /* The signal handler itself. */
317
318 void sighandler(int sig)
319 {
320 DBG("sighandler");
321 create_listener();
322 }
323
324 /* Called by the app signal handler to chain it to us. */
325
326 void chain_signal(void)
327 {
328 sighandler(USTSIGNAL);
329 }
330
331 static int init_socket(void)
332 {
333 int result;
334 int fd;
335 char pidstr[6];
336 int pidlen;
337
338 struct sockaddr_un addr;
339
340 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
341 if(result == -1) {
342 PERROR("socket");
343 return -1;
344 }
345
346 addr.sun_family = AF_UNIX;
347
348 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCKETDIR, mypid);
349 if(result >= UNIX_PATH_MAX) {
350 ERR("string overflow allocating socket name");
351 goto close_sock;
352 }
353 //DBG("opening socket at %s", addr.sun_path);
354
355 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
356 if(result == -1) {
357 PERROR("bind");
358 goto close_sock;
359 }
360
361 strcpy(mysocketfile, addr.sun_path);
362
363 pfd = fd;
364 return 0;
365
366 close_sock:
367 close(fd);
368
369 return -1;
370 }
371
372 static void destroy_socket(void)
373 {
374 int result;
375
376 if(mysocketfile[0] == '\0')
377 return;
378
379 result = unlink(mysocketfile);
380 if(result == -1) {
381 PERROR("unlink");
382 }
383 }
384
385 static int init_signal_handler(void)
386 {
387 /* Attempt to handler SIGIO. If the main program wants to
388 * handle it, fine, it'll override us. They it'll have to
389 * use the chaining function.
390 */
391
392 int result;
393 struct sigaction act;
394
395 result = sigemptyset(&act.sa_mask);
396 if(result == -1) {
397 PERROR("sigemptyset");
398 return -1;
399 }
400
401 act.sa_handler = sighandler;
402 act.sa_flags = SA_RESTART;
403
404 /* Only defer ourselves. Also, try to restart interrupted
405 * syscalls to disturb the traced program as little as possible.
406 */
407 result = sigaction(SIGIO, &act, NULL);
408 if(result == -1) {
409 PERROR("sigaction");
410 return -1;
411 }
412
413 return 0;
414 }
415
416 static void auto_probe_connect(struct marker *m)
417 {
418 int result;
419
420 result = ltt_marker_connect(m->channel, m->name, "default");
421 if(result)
422 ERR("ltt_marker_connect");
423
424 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
425 }
426
427 static void __attribute__((constructor(101))) init0()
428 {
429 DBG("UST_AUTOPROBE constructor");
430 if(getenv("UST_AUTOPROBE")) {
431 marker_set_new_marker_cb(auto_probe_connect);
432 }
433 }
434
435 static void fini(void);
436
437 static void __attribute__((constructor(1000))) init()
438 {
439 int result;
440
441 DBG("UST_TRACE constructor");
442
443 mypid = getpid();
444
445 if(getenv("UST_TRACE")) {
446 char trace_name[] = "auto";
447 char trace_type[] = "ustrelay";
448
449 DBG("starting early tracing");
450
451 /* Ensure marker control is initialized */
452 init_marker_control();
453
454 /* Ensure relay is initialized */
455 init_ustrelay_transport();
456
457 /* Ensure markers are initialized */
458 init_markers();
459
460 /* In case. */
461 ltt_channels_register("ust");
462
463 result = ltt_trace_setup(trace_name);
464 if(result < 0) {
465 ERR("ltt_trace_setup failed");
466 return;
467 }
468
469 result = ltt_trace_set_type(trace_name, trace_type);
470 if(result < 0) {
471 ERR("ltt_trace_set_type failed");
472 return;
473 }
474
475 result = ltt_trace_alloc(trace_name);
476 if(result < 0) {
477 ERR("ltt_trace_alloc failed");
478 return;
479 }
480
481 result = ltt_trace_start(trace_name);
482 if(result < 0) {
483 ERR("ltt_trace_start failed");
484 return;
485 }
486 start_consumer();
487 }
488
489 /* Must create socket before signal handler to prevent races
490 * on pfd variable.
491 */
492 result = init_socket();
493 if(result == -1) {
494 ERR("init_socket error");
495 return;
496 }
497 result = init_signal_handler();
498 if(result == -1) {
499 ERR("init_signal_handler error");
500 return;
501 }
502
503 return;
504
505 /* should decrementally destroy stuff if error */
506
507 }
508
509 /* This is only called if we terminate normally, not with an unhandled signal,
510 * so we cannot rely on it. */
511
512 static void __attribute__((destructor)) fini()
513 {
514 int result;
515
516 /* if trace running, finish it */
517
518 DBG("destructor stopping traces");
519
520 result = ltt_trace_stop("auto");
521 if(result == -1) {
522 ERR("ltt_trace_stop error");
523 }
524
525 result = ltt_trace_destroy("auto");
526 if(result == -1) {
527 ERR("ltt_trace_destroy error");
528 }
529
530 /* FIXME: wait for the consumer to be done */
531 sleep(3);
532
533 destroy_socket();
534 }
This page took 0.038912 seconds and 3 git commands to generate.