move functions that send messages to traced processes to libustcomm (which is linked...
[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
PMF
11#include "tracer.h"
12#include "usterr.h"
fbd8191b 13
68c1021b
PMF
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
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
68c1021b
PMF
27struct tracecmd { /* no padding */
28 uint32_t size;
29 uint16_t command;
30};
31
98963de4
PMF
32//struct listener_arg {
33// int pipe_fd;
34//};
35
36struct 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};
68c1021b
PMF
45
46pid_t mypid;
47char mysocketfile[UNIX_PATH_MAX] = "";
98963de4 48int pfd = -1;
68c1021b 49
a584bc4e
PMF
50struct consumer_channel {
51 int fd;
52 struct ltt_channel_struct *chan;
53};
54
55int 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
1c184644 87 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
a584bc4e
PMF
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) {
1c184644 109 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
a584bc4e
PMF
110 }
111 else {
1c184644 112 DBG("success!");
a584bc4e
PMF
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
161void 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}
fbd8191b
PMF
170
171static 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
68c1021b
PMF
184void do_command(struct tracecmd *cmd)
185{
186}
187
188void receive_commands()
189{
190}
191
98963de4
PMF
192int fd_notif = -1;
193void 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
fbd8191b
PMF
214char recvbuf[10000];
215
98963de4
PMF
216int listener_main(void *p)
217{
218 int result;
219
98963de4 220 for(;;) {
98963de4 221 uint32_t size;
98963de4
PMF
222 struct sockaddr_un addr;
223 socklen_t addrlen = sizeof(addr);
aafb1650
PMF
224 char trace_name[] = "auto";
225 char trace_type[] = "ustrelay";
98963de4 226
98963de4
PMF
227 for(;;) {
228 struct trctl_msg msg;
fbd8191b 229 int len;
98963de4 230
681a8a78 231 result = len = recvfrom(pfd, recvbuf, sizeof(recvbuf-1), 0, &addr, &addrlen);
98963de4 232 if(result == -1) {
fbd8191b 233 PERROR("recvfrom");
98963de4
PMF
234 continue;
235 }
236
aafb1650
PMF
237 if(recvbuf[len-1] == '\n')
238 recvbuf[len-1] = '\0';
681a8a78
PMF
239 else
240 recvbuf[len] = 0;
98963de4 241
fbd8191b 242 fprintf(stderr, "received a message! it's: %s\n", recvbuf);
98963de4 243
fbd8191b
PMF
244
245 if(!strcmp(recvbuf, "print_markers")) {
246 print_markers();
247 }
248 else if(!strcmp(recvbuf, "trace_setup")) {
249 DBG("trace setup");
aafb1650
PMF
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 }
fbd8191b
PMF
262 }
263 else if(!strcmp(recvbuf, "trace_alloc")) {
264 DBG("trace alloc");
aafb1650
PMF
265
266 result = ltt_trace_alloc(trace_name);
267 if(result < 0) {
268 ERR("ltt_trace_alloc failed");
269 return;
270 }
fbd8191b
PMF
271 }
272 else if(!strcmp(recvbuf, "trace_start")) {
273 DBG("trace start");
aafb1650
PMF
274
275 result = ltt_trace_start(trace_name);
276 if(result < 0) {
277 ERR("ltt_trace_start failed");
278 return;
279 }
fbd8191b
PMF
280 }
281 else if(!strcmp(recvbuf, "trace_stop")) {
282 DBG("trace stop");
aafb1650
PMF
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 }
fbd8191b 299 }
98963de4
PMF
300 }
301 next_conn:;
302 }
303}
304
305void create_listener(void)
306{
307 int result;
308 static char listener_stack[16384];
309
fbd8191b 310 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
311 if(result == -1) {
312 perror("clone");
313 }
314}
315
68c1021b
PMF
316/* The signal handler itself. */
317
318void sighandler(int sig)
319{
320 DBG("sighandler");
fbd8191b 321 create_listener();
68c1021b
PMF
322}
323
324/* Called by the app signal handler to chain it to us. */
325
98963de4 326void chain_signal(void)
68c1021b
PMF
327{
328 sighandler(USTSIGNAL);
329}
330
98963de4 331static int init_socket(void)
68c1021b
PMF
332{
333 int result;
334 int fd;
335 char pidstr[6];
336 int pidlen;
337
338 struct sockaddr_un addr;
339
fbd8191b 340 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
68c1021b
PMF
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
98963de4
PMF
363 pfd = fd;
364 return 0;
365
68c1021b
PMF
366 close_sock:
367 close(fd);
368
369 return -1;
370}
371
98963de4 372static void destroy_socket(void)
68c1021b
PMF
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
98963de4 385static int init_signal_handler(void)
68c1021b
PMF
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
20b37a31 416static void auto_probe_connect(struct marker *m)
68c1021b
PMF
417{
418 int result;
419
20b37a31
PMF
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
427static 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
a584bc4e
PMF
435static void fini(void);
436
20b37a31
PMF
437static void __attribute__((constructor(1000))) init()
438{
439 int result;
440
441 DBG("UST_TRACE constructor");
442
68c1021b
PMF
443 mypid = getpid();
444
4db647c5
PMF
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
20b37a31
PMF
460 /* In case. */
461 ltt_channels_register("ust");
4db647c5
PMF
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 }
a584bc4e 486 start_consumer();
4db647c5
PMF
487 }
488
98963de4
PMF
489 /* Must create socket before signal handler to prevent races
490 * on pfd variable.
491 */
68c1021b 492 result = init_socket();
98963de4
PMF
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 }
68c1021b
PMF
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
98963de4 512static void __attribute__((destructor)) fini()
68c1021b 513{
a584bc4e
PMF
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 */
1c184644 531 sleep(3);
a584bc4e 532
68c1021b
PMF
533 destroy_socket();
534}
This page took 0.044739 seconds and 4 git commands to generate.