1 /* Copyright (C) 2009 Pierre-Marc Fournier
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <sys/types.h>
39 /* return value: 0 = subbuffer is finished, it won't produce data anymore
40 * 1 = got subbuffer successfully
44 #define GET_SUBBUF_OK 1
45 #define GET_SUBBUF_DONE 0
46 #define GET_SUBBUF_DIED 2
48 #define PUT_SUBBUF_OK 1
49 #define PUT_SUBBUF_DIED 0
50 #define PUT_SUBBUF_PUSHED 2
53 char *trace_path
=NULL
;
57 /* Number of active buffers and the mutex to protect it. */
58 int active_buffers
= 0;
59 pthread_mutex_t active_buffers_mutex
= PTHREAD_MUTEX_INITIALIZER
;
60 /* Whether a request to end the program was received. */
61 sig_atomic_t terminate_req
= 0;
63 int get_subbuffer(struct buffer_info
*buf
)
66 char *received_msg
=NULL
;
71 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
72 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
73 if((result
== -1 && errno
== EPIPE
) || result
== 0) {
74 DBG("app died while being traced");
75 retval
= GET_SUBBUF_DIED
;
79 ERR("get_subbuffer: ustcomm_send_request failed");
84 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
85 if(result
!= 2 && result
!= 1) {
86 ERR("unable to parse response to get_subbuffer");
91 DBG("received msg is %s", received_msg
);
93 if(!strcmp(rep_code
, "OK")) {
94 DBG("got subbuffer %s", buf
->name
);
95 retval
= GET_SUBBUF_OK
;
97 else if(nth_token_is(received_msg
, "END", 0) == 1) {
98 retval
= GET_SUBBUF_DONE
;
102 DBG("error getting subbuffer %s", buf
->name
);
106 /* FIMXE: free correctly the stuff */
119 int put_subbuffer(struct buffer_info
*buf
)
122 char *received_msg
=NULL
;
127 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
128 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
129 if(result
< 0 && errno
== ECONNRESET
) {
130 retval
= PUT_SUBBUF_DIED
;
134 ERR("put_subbuffer: send_message failed");
139 result
= sscanf(received_msg
, "%as", &rep_code
);
141 ERR("unable to parse response to put_subbuffer");
146 if(!strcmp(rep_code
, "OK")) {
147 DBG("subbuffer put %s", buf
->name
);
148 retval
= PUT_SUBBUF_OK
;
151 DBG("put_subbuffer: received error, we were pushed");
152 retval
= PUT_SUBBUF_PUSHED
;
169 void decrement_active_buffers(void *arg
)
171 pthread_mutex_lock(&active_buffers_mutex
);
173 pthread_mutex_unlock(&active_buffers_mutex
);
176 int create_dir_if_needed(char *dir
)
179 result
= mkdir(dir
, 0777);
181 if(errno
!= EEXIST
) {
190 int is_directory(const char *dir
)
195 result
= stat(dir
, &st
);
201 if(!S_ISDIR(st
.st_mode
)) {
208 struct buffer_info
*connect_buffer(pid_t pid
, const char *bufname
)
210 struct buffer_info
*buf
;
216 struct shmid_ds shmds
;
218 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
220 ERR("add_buffer: insufficient memory");
228 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
230 WARN("unable to connect to process, it probably died before we were able to connect");
235 asprintf(&send_msg
, "get_pidunique");
236 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
239 ERR("problem in ustcomm_send_request(get_pidunique)");
243 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
245 ERR("unable to parse response to get_pidunique");
249 DBG("got pidunique %lld", buf
->pidunique
);
252 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
253 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
256 ERR("problem in ustcomm_send_request(get_shmid)");
260 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
262 ERR("unable to parse response to get_shmid (\"%s\")", received_msg
);
266 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
269 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
270 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
273 ERR("problem in ustcomm_send_request(g_n_subbufs)");
277 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
279 ERR("unable to parse response to get_n_subbufs");
283 DBG("got n_subbufs %d", buf
->n_subbufs
);
285 /* get subbuf size */
286 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
287 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
290 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
292 ERR("unable to parse response to get_subbuf_size");
296 DBG("got subbuf_size %d", buf
->subbuf_size
);
299 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
300 if(buf
->mem
== (void *) 0) {
304 DBG("successfully attached buffer memory");
306 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
307 if(buf
->bufstruct_mem
== (void *) 0) {
311 DBG("successfully attached buffer bufstruct memory");
313 /* obtain info on the memory segment */
314 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
319 buf
->memlen
= shmds
.shm_segsz
;
321 /* open file for output */
323 /* Only create the directory if using the default path, because
324 * of the risk of typo when using trace path override. We don't
325 * want to risk creating plenty of useless directories in that case.
327 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
329 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
333 trace_path
= USTD_DEFAULT_TRACE_PATH
;
336 asprintf(&tmp
, "%s/%u_%lld", trace_path
, buf
->pid
, buf
->pidunique
);
337 result
= create_dir_if_needed(tmp
);
339 ERR("could not create directory %s", tmp
);
345 asprintf(&tmp
, "%s/%u_%lld/%s", trace_path
, buf
->pid
, buf
->pidunique
, buf
->name
);
346 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_EXCL
, 00600);
349 ERR("failed opening trace file %s", tmp
);
355 pthread_mutex_lock(&active_buffers_mutex
);
357 pthread_mutex_unlock(&active_buffers_mutex
);
362 int write_current_subbuffer(struct buffer_info
*buf
)
366 void *subbuf_mem
= buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1));
368 size_t cur_sb_size
= subbuffer_data_size(subbuf_mem
);
370 result
= patient_write(buf
->file_fd
, subbuf_mem
, cur_sb_size
);
373 /* FIXME: maybe drop this trace */
380 int consumer_loop(struct buffer_info
*buf
)
384 pthread_cleanup_push(decrement_active_buffers
, NULL
);
387 /* get the subbuffer */
388 result
= get_subbuffer(buf
);
390 ERR("error getting subbuffer");
393 else if(result
== GET_SUBBUF_DONE
) {
397 else if(result
== GET_SUBBUF_DIED
) {
398 finish_consuming_dead_subbuffer(buf
);
402 /* write data to file */
403 write_current_subbuffer(buf
);
404 /* FIXME: handle return value? */
406 /* put the subbuffer */
407 /* FIXME: we actually should unput the buffer before consuming... */
408 result
= put_subbuffer(buf
);
410 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
413 else if(result
== PUT_SUBBUF_PUSHED
) {
414 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
417 else if(result
== PUT_SUBBUF_DIED
) {
418 WARN("application died while putting subbuffer");
419 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
420 finish_consuming_dead_subbuffer(buf
);
423 else if(result
== PUT_SUBBUF_OK
) {
427 DBG("thread for buffer %s is stopping", buf
->name
);
429 /* FIXME: destroy, unalloc... */
431 pthread_cleanup_pop(1);
436 void free_buffer(struct buffer_info
*buf
)
440 struct consumer_thread_args
{
445 void *consumer_thread(void *arg
)
447 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
448 struct consumer_thread_args
*args
= (struct consumer_thread_args
*) arg
;
450 DBG("GOT ARGS: pid %d bufname %s", args
->pid
, args
->bufname
);
452 buf
= connect_buffer(args
->pid
, args
->bufname
);
454 ERR("failed to connect to buffer");
463 /* bufname is free'd in free_buffer() */
468 int start_consuming_buffer(pid_t pid
, const char *bufname
)
471 struct consumer_thread_args
*args
;
473 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid
, bufname
);
475 args
= (struct consumer_thread_args
*) malloc(sizeof(struct consumer_thread_args
));
478 args
->bufname
= strdup(bufname
);
479 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
481 pthread_create(&thr
, NULL
, consumer_thread
, args
);
482 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
489 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
490 "\t-h\t\tDisplay this usage.\n"
491 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
492 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n"
493 "\t-d\t\tStart as a daemon.\n"
494 "\t--pidfile FILE\tWrite the PID in this file (when using -d).\n");
497 int parse_args(int argc
, char **argv
)
502 int option_index
= 0;
503 static struct option long_options
[] = {
504 {"pidfile", 1, 0, 'p'},
506 {"version", 0, 0, 'V'},
510 c
= getopt_long(argc
, argv
, "hs:o:d", long_options
, &option_index
);
516 printf("option %s", long_options
[option_index
].name
);
518 printf(" with arg %s", optarg
);
526 if(!is_directory(trace_path
)) {
527 ERR("Not a valid directory. (%s)", trace_path
);
535 pidfile
= strdup(optarg
);
541 printf("Version 0.0\n");
545 /* unknown option or other error; error is
546 printed by getopt, just return */
554 void sigterm_handler(int sig
)
559 static int write_pidfile(const char *file_name
, pid_t pid
)
563 pidfp
= fopen(file_name
, "w");
565 PERROR("fopen (%s)", pidfile
);
566 WARN("killing child process");
570 fprintf(pidfp
, "%d\n", pid
);
577 int start_ustd(int fd
)
579 struct ustcomm_ustd ustd
;
584 result
= sigemptyset(&sigset
);
586 PERROR("sigemptyset");
589 sa
.sa_handler
= sigterm_handler
;
591 sa
.sa_flags
= SA_RESTART
;
592 result
= sigaction(SIGTERM
, &sa
, NULL
);
598 result
= ustcomm_init_ustd(&ustd
, sock_path
);
600 ERR("failed to initialize socket");
604 /* setup handler for SIGPIPE */
605 result
= sigemptyset(&sigset
);
607 PERROR("sigemptyset");
610 result
= sigaddset(&sigset
, SIGPIPE
);
615 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
617 PERROR("sigprocmask");
623 result
= write_pidfile(pidfile
, getpid());
625 ERR("failed to write pidfile");
630 /* Notify parent that we are successfully started. */
632 /* write any one character */
633 result
= write(fd
, "!", 1);
639 ERR("Problem sending confirmation of daemon start to parent");
652 /* check for requests on our public socket */
653 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
655 ERR("error in ustcomm_ustd_recv_message");
659 if(!strncmp(recvbuf
, "collect", 7)) {
664 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
666 ERR("parsing error: %s", recvbuf
);
670 result
= start_consuming_buffer(pid
, bufname
);
672 ERR("error in add_buffer");
686 pthread_mutex_lock(&active_buffers_mutex
);
687 if(active_buffers
== 0) {
688 pthread_mutex_unlock(&active_buffers_mutex
);
691 pthread_mutex_unlock(&active_buffers_mutex
);
698 int start_ustd_daemon()
706 result
= child_pid
= fork();
711 else if(result
== 0) {
712 return start_ustd(fd
[1]);
717 result
= read(fd
[0], &buf
, 1);
723 ERR("did not receive valid confirmation that the daemon is started");
727 result
= close(fd
[0]);
732 DBG("The daemon is now successfully started");
735 /* Wait for confirmation that the server is ready. */
741 int main(int argc
, char **argv
)
745 result
= parse_args(argc
, argv
);
751 result
= start_ustd_daemon();
754 result
= start_ustd(-1);