ust: additional work on libmallocwrap and cleanups
[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), 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
240 fprintf(stderr, "received a message! it's: %s\n", recvbuf);
241
242
243 if(!strcmp(recvbuf, "print_markers")) {
244 print_markers();
245 }
246 else if(!strcmp(recvbuf, "trace_setup")) {
247 DBG("trace setup");
248
249 result = ltt_trace_setup(trace_name);
250 if(result < 0) {
251 ERR("ltt_trace_setup failed");
252 return;
253 }
254
255 result = ltt_trace_set_type(trace_name, trace_type);
256 if(result < 0) {
257 ERR("ltt_trace_set_type failed");
258 return;
259 }
260 }
261 else if(!strcmp(recvbuf, "trace_alloc")) {
262 DBG("trace alloc");
263
264 result = ltt_trace_alloc(trace_name);
265 if(result < 0) {
266 ERR("ltt_trace_alloc failed");
267 return;
268 }
269 }
270 else if(!strcmp(recvbuf, "trace_start")) {
271 DBG("trace start");
272
273 result = ltt_trace_start(trace_name);
274 if(result < 0) {
275 ERR("ltt_trace_start failed");
276 return;
277 }
278 }
279 else if(!strcmp(recvbuf, "trace_stop")) {
280 DBG("trace stop");
281
282 result = ltt_trace_stop(trace_name);
283 if(result < 0) {
284 ERR("ltt_trace_stop failed");
285 return;
286 }
287 }
288 else if(!strcmp(recvbuf, "trace_destroy")) {
289
290 DBG("trace destroy");
291
292 result = ltt_trace_destroy(trace_name);
293 if(result < 0) {
294 ERR("ltt_trace_destroy failed");
295 return;
296 }
297 }
298 }
299 next_conn:;
300 }
301 }
302
303 void create_listener(void)
304 {
305 int result;
306 static char listener_stack[16384];
307
308 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
309 if(result == -1) {
310 perror("clone");
311 }
312 }
313
314 /* The signal handler itself. */
315
316 void sighandler(int sig)
317 {
318 DBG("sighandler");
319 create_listener();
320 }
321
322 /* Called by the app signal handler to chain it to us. */
323
324 void chain_signal(void)
325 {
326 sighandler(USTSIGNAL);
327 }
328
329 static int init_socket(void)
330 {
331 int result;
332 int fd;
333 char pidstr[6];
334 int pidlen;
335
336 struct sockaddr_un addr;
337
338 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
339 if(result == -1) {
340 PERROR("socket");
341 return -1;
342 }
343
344 addr.sun_family = AF_UNIX;
345
346 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCKETDIR, mypid);
347 if(result >= UNIX_PATH_MAX) {
348 ERR("string overflow allocating socket name");
349 goto close_sock;
350 }
351 //DBG("opening socket at %s", addr.sun_path);
352
353 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
354 if(result == -1) {
355 PERROR("bind");
356 goto close_sock;
357 }
358
359 strcpy(mysocketfile, addr.sun_path);
360
361 pfd = fd;
362 return 0;
363
364 close_sock:
365 close(fd);
366
367 return -1;
368 }
369
370 static void destroy_socket(void)
371 {
372 int result;
373
374 if(mysocketfile[0] == '\0')
375 return;
376
377 result = unlink(mysocketfile);
378 if(result == -1) {
379 PERROR("unlink");
380 }
381 }
382
383 static int init_signal_handler(void)
384 {
385 /* Attempt to handler SIGIO. If the main program wants to
386 * handle it, fine, it'll override us. They it'll have to
387 * use the chaining function.
388 */
389
390 int result;
391 struct sigaction act;
392
393 result = sigemptyset(&act.sa_mask);
394 if(result == -1) {
395 PERROR("sigemptyset");
396 return -1;
397 }
398
399 act.sa_handler = sighandler;
400 act.sa_flags = SA_RESTART;
401
402 /* Only defer ourselves. Also, try to restart interrupted
403 * syscalls to disturb the traced program as little as possible.
404 */
405 result = sigaction(SIGIO, &act, NULL);
406 if(result == -1) {
407 PERROR("sigaction");
408 return -1;
409 }
410
411 return 0;
412 }
413
414 static void auto_probe_connect(struct marker *m)
415 {
416 int result;
417
418 result = ltt_marker_connect(m->channel, m->name, "default");
419 if(result)
420 ERR("ltt_marker_connect");
421
422 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
423 }
424
425 static void __attribute__((constructor(101))) init0()
426 {
427 DBG("UST_AUTOPROBE constructor");
428 if(getenv("UST_AUTOPROBE")) {
429 marker_set_new_marker_cb(auto_probe_connect);
430 }
431 }
432
433 static void fini(void);
434
435 static void __attribute__((constructor(1000))) init()
436 {
437 int result;
438
439 DBG("UST_TRACE constructor");
440
441 mypid = getpid();
442
443 if(getenv("UST_TRACE")) {
444 char trace_name[] = "auto";
445 char trace_type[] = "ustrelay";
446
447 DBG("starting early tracing");
448
449 /* Ensure marker control is initialized */
450 init_marker_control();
451
452 /* Ensure relay is initialized */
453 init_ustrelay_transport();
454
455 /* Ensure markers are initialized */
456 init_markers();
457
458 /* In case. */
459 ltt_channels_register("ust");
460
461 result = ltt_trace_setup(trace_name);
462 if(result < 0) {
463 ERR("ltt_trace_setup failed");
464 return;
465 }
466
467 result = ltt_trace_set_type(trace_name, trace_type);
468 if(result < 0) {
469 ERR("ltt_trace_set_type failed");
470 return;
471 }
472
473 result = ltt_trace_alloc(trace_name);
474 if(result < 0) {
475 ERR("ltt_trace_alloc failed");
476 return;
477 }
478
479 result = ltt_trace_start(trace_name);
480 if(result < 0) {
481 ERR("ltt_trace_start failed");
482 return;
483 }
484 start_consumer();
485 }
486
487 /* Must create socket before signal handler to prevent races
488 * on pfd variable.
489 */
490 result = init_socket();
491 if(result == -1) {
492 ERR("init_socket error");
493 return;
494 }
495 result = init_signal_handler();
496 if(result == -1) {
497 ERR("init_signal_handler error");
498 return;
499 }
500
501 return;
502
503 /* should decrementally destroy stuff if error */
504
505 }
506
507 /* This is only called if we terminate normally, not with an unhandled signal,
508 * so we cannot rely on it. */
509
510 static void __attribute__((destructor)) fini()
511 {
512 int result;
513
514 /* if trace running, finish it */
515
516 DBG("destructor stopping traces");
517
518 result = ltt_trace_stop("auto");
519 if(result == -1) {
520 ERR("ltt_trace_stop error");
521 }
522
523 result = ltt_trace_destroy("auto");
524 if(result == -1) {
525 ERR("ltt_trace_destroy error");
526 }
527
528 /* FIXME: wait for the consumer to be done */
529 sleep(3);
530
531 destroy_socket();
532 }
This page took 0.041187 seconds and 5 git commands to generate.