ust: continue work
[ust.git] / libtracectl / tracectl.c
CommitLineData
68c1021b
PMF
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>
98963de4 7#include <sched.h>
a584bc4e 8#include <fcntl.h>
fbd8191b
PMF
9
10#include "marker.h"
a584bc4e 11#include "tracer.h"
d0b5f2b9
PMF
12#include "localerr.h"
13#include "ustcomm.h"
fbd8191b 14
68c1021b
PMF
15#define UNIX_PATH_MAX 108
16
68c1021b
PMF
17#define SOCKETDIR "/tmp/socks"
18#define SOCKETDIRLEN sizeof(SOCKETDIR)
19#define USTSIGNAL SIGIO
20
98963de4
PMF
21#define MAX_MSG_SIZE (100)
22#define MSG_NOTIF 1
23#define MSG_REGISTER_NOTIF 2
24
a584bc4e
PMF
25char consumer_stack[10000];
26
d0b5f2b9
PMF
27static struct ustcomm_app ustcomm_app;
28
68c1021b
PMF
29struct tracecmd { /* no padding */
30 uint32_t size;
31 uint16_t command;
32};
33
98963de4
PMF
34//struct listener_arg {
35// int pipe_fd;
36//};
37
38struct 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};
68c1021b
PMF
47
48pid_t mypid;
49char mysocketfile[UNIX_PATH_MAX] = "";
d0b5f2b9 50//int pfd = -1;
68c1021b 51
a584bc4e
PMF
52struct consumer_channel {
53 int fd;
54 struct ltt_channel_struct *chan;
55};
56
57int 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
1c184644 89 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
a584bc4e
PMF
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) {
1c184644 111 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
a584bc4e
PMF
112 }
113 else {
1c184644 114 DBG("success!");
a584bc4e
PMF
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
163void 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 }
b0540e11
PMF
171// pthread_t thread;
172//
173// pthread_create(&thread, NULL, consumer, NULL);
a584bc4e 174}
fbd8191b
PMF
175
176static void print_markers(void)
177{
178 struct marker_iter iter;
179
d0b5f2b9 180 lock_markers();
fbd8191b
PMF
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 }
d0b5f2b9 188 unlock_markers();
fbd8191b
PMF
189}
190
68c1021b
PMF
191void do_command(struct tracecmd *cmd)
192{
193}
194
195void receive_commands()
196{
197}
198
98963de4
PMF
199int fd_notif = -1;
200void 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
d0b5f2b9
PMF
221#define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
222
223static int inform_consumer_daemon(void)
224{
225}
fbd8191b 226
98963de4
PMF
227int listener_main(void *p)
228{
229 int result;
230
b0540e11
PMF
231 DBG("LISTENER");
232
98963de4 233 for(;;) {
98963de4 234 uint32_t size;
98963de4
PMF
235 struct sockaddr_un addr;
236 socklen_t addrlen = sizeof(addr);
aafb1650
PMF
237 char trace_name[] = "auto";
238 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
239 char *recvbuf;
240 int len;
98963de4 241
d0b5f2b9 242 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf);
b0540e11 243 DBG("HERE");
d0b5f2b9
PMF
244 if(result) {
245 WARN("error in ustcomm_app_recv_message");
246 continue;
247 }
98963de4 248
d0b5f2b9
PMF
249 DBG("received a message! it's: %s\n", recvbuf);
250 len = strlen(recvbuf);
b0540e11
PMF
251 //if(len && recvbuf[len-1] == '\n') {
252 // recvbuf[len-1] = '\0';
253 //}
98963de4 254
d0b5f2b9
PMF
255 if(!strcmp(recvbuf, "print_markers")) {
256 print_markers();
257 }
258 else if(!strcmp(recvbuf, "trace_setup")) {
259 DBG("trace setup");
fbd8191b 260
d0b5f2b9
PMF
261 result = ltt_trace_setup(trace_name);
262 if(result < 0) {
263 ERR("ltt_trace_setup failed");
264 return;
fbd8191b 265 }
d0b5f2b9
PMF
266
267 result = ltt_trace_set_type(trace_name, trace_type);
268 if(result < 0) {
269 ERR("ltt_trace_set_type failed");
270 return;
fbd8191b 271 }
d0b5f2b9
PMF
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;
fbd8191b 280 }
d0b5f2b9
PMF
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;
fbd8191b 289 }
d0b5f2b9
PMF
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;
aafb1650 298 }
d0b5f2b9
PMF
299 }
300 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 301
d0b5f2b9 302 DBG("trace destroy");
aafb1650 303
d0b5f2b9
PMF
304 result = ltt_trace_destroy(trace_name);
305 if(result < 0) {
306 ERR("ltt_trace_destroy failed");
307 return;
fbd8191b 308 }
98963de4 309 }
d0b5f2b9
PMF
310
311 free(recvbuf);
98963de4
PMF
312 }
313}
314
b0540e11
PMF
315static char listener_stack[16384];
316
98963de4
PMF
317void create_listener(void)
318{
319 int result;
320 static char listener_stack[16384];
b0540e11 321 //char *listener_stack = malloc(16384);
98963de4 322
fbd8191b 323 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
324 if(result == -1) {
325 perror("clone");
326 }
b0540e11
PMF
327 //pthread_t thread;
328
329 //pthread_create(&thread, NULL, listener_main, NULL);
98963de4
PMF
330}
331
d0b5f2b9
PMF
332/* The signal handler itself. Signals must be setup so there cannot be
333 nested signals. */
68c1021b
PMF
334
335void sighandler(int sig)
336{
d0b5f2b9 337 static char have_listener = 0;
68c1021b 338 DBG("sighandler");
d0b5f2b9
PMF
339
340 if(!have_listener) {
341 create_listener();
342 have_listener = 1;
343 }
68c1021b
PMF
344}
345
346/* Called by the app signal handler to chain it to us. */
347
98963de4 348void chain_signal(void)
68c1021b
PMF
349{
350 sighandler(USTSIGNAL);
351}
352
98963de4 353static int init_socket(void)
68c1021b 354{
d0b5f2b9 355 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
356}
357
98963de4 358static void destroy_socket(void)
68c1021b
PMF
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
98963de4 371static int init_signal_handler(void)
68c1021b
PMF
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
20b37a31 402static void auto_probe_connect(struct marker *m)
68c1021b
PMF
403{
404 int result;
405
20b37a31
PMF
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
413static 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
a584bc4e
PMF
421static void fini(void);
422
20b37a31
PMF
423static void __attribute__((constructor(1000))) init()
424{
425 int result;
426
427 DBG("UST_TRACE constructor");
428
68c1021b
PMF
429 mypid = getpid();
430
4db647c5
PMF
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
20b37a31
PMF
446 /* In case. */
447 ltt_channels_register("ust");
4db647c5
PMF
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 }
a584bc4e 472 start_consumer();
4db647c5
PMF
473 }
474
b0540e11 475 /* Must create socket before signal handler to prevent races.
98963de4 476 */
68c1021b 477 result = init_socket();
98963de4
PMF
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 }
68c1021b
PMF
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
98963de4 497static void __attribute__((destructor)) fini()
68c1021b 498{
a584bc4e
PMF
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 */
d0b5f2b9 516 sleep(1);
a584bc4e 517
68c1021b
PMF
518 destroy_socket();
519}
This page took 0.043003 seconds and 4 git commands to generate.