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
51 #define PUT_SUBBUF_DONE 3
54 char *trace_path
=NULL
;
58 /* Number of active buffers and the mutex to protect it. */
59 int active_buffers
= 0;
60 pthread_mutex_t active_buffers_mutex
= PTHREAD_MUTEX_INITIALIZER
;
61 /* Whether a request to end the program was received. */
62 sig_atomic_t terminate_req
= 0;
64 int get_subbuffer(struct buffer_info
*buf
)
67 char *received_msg
=NULL
;
72 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
73 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
74 if((result
== -1 && errno
== EPIPE
) || result
== 0) {
75 DBG("app died while being traced");
76 retval
= GET_SUBBUF_DIED
;
80 ERR("get_subbuffer: ustcomm_send_request failed");
85 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
86 if(result
!= 2 && result
!= 1) {
87 ERR("unable to parse response to get_subbuffer");
92 DBG("received msg is %s", received_msg
);
94 if(!strcmp(rep_code
, "OK")) {
95 DBG("got subbuffer %s", buf
->name
);
96 retval
= GET_SUBBUF_OK
;
98 else if(nth_token_is(received_msg
, "END", 0) == 1) {
99 retval
= GET_SUBBUF_DONE
;
102 else if(!strcmp(received_msg
, "NOTFOUND")) {
103 WARN("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf
->name
);
104 retval
= GET_SUBBUF_DONE
;
108 DBG("error getting subbuffer %s", buf
->name
);
112 /* FIMXE: free correctly the stuff */
125 int put_subbuffer(struct buffer_info
*buf
)
128 char *received_msg
=NULL
;
133 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
134 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
135 if(result
< 0 && errno
== ECONNRESET
) {
136 retval
= PUT_SUBBUF_DIED
;
139 else if(result
< 0) {
140 ERR("put_subbuffer: send_message failed");
144 else if(result
== 0) {
145 /* Program seems finished. However this might not be
146 * the last subbuffer that has to be collected.
148 retval
= PUT_SUBBUF_DIED
;
152 result
= sscanf(received_msg
, "%as", &rep_code
);
154 ERR("unable to parse response to put_subbuffer");
159 if(!strcmp(rep_code
, "OK")) {
160 DBG("subbuffer put %s", buf
->name
);
161 retval
= PUT_SUBBUF_OK
;
163 else if(!strcmp(received_msg
, "NOTFOUND")) {
164 WARN("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf
->name
);
165 /* However, maybe this was not the last subbuffer. So
166 * we return the program died.
168 retval
= PUT_SUBBUF_DIED
;
172 DBG("put_subbuffer: received error, we were pushed");
173 retval
= PUT_SUBBUF_PUSHED
;
190 void decrement_active_buffers(void *arg
)
192 pthread_mutex_lock(&active_buffers_mutex
);
194 pthread_mutex_unlock(&active_buffers_mutex
);
197 int create_dir_if_needed(char *dir
)
200 result
= mkdir(dir
, 0777);
202 if(errno
!= EEXIST
) {
211 int is_directory(const char *dir
)
216 result
= stat(dir
, &st
);
222 if(!S_ISDIR(st
.st_mode
)) {
229 struct buffer_info
*connect_buffer(pid_t pid
, const char *bufname
)
231 struct buffer_info
*buf
;
237 struct shmid_ds shmds
;
239 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
241 ERR("add_buffer: insufficient memory");
249 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
251 WARN("unable to connect to process, it probably died before we were able to connect");
256 asprintf(&send_msg
, "get_pidunique");
257 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
260 ERR("problem in ustcomm_send_request(get_pidunique)");
267 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
269 ERR("unable to parse response to get_pidunique");
273 DBG("got pidunique %lld", buf
->pidunique
);
276 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
277 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
280 ERR("problem in ustcomm_send_request(get_shmid)");
287 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
289 ERR("unable to parse response to get_shmid (\"%s\")", received_msg
);
293 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
296 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
297 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
300 ERR("problem in ustcomm_send_request(g_n_subbufs)");
307 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
309 ERR("unable to parse response to get_n_subbufs");
313 DBG("got n_subbufs %d", buf
->n_subbufs
);
315 /* get subbuf size */
316 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
317 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
320 ERR("problem in ustcomm_send_request(get_subbuf_size)");
327 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
329 ERR("unable to parse response to get_subbuf_size");
333 DBG("got subbuf_size %d", buf
->subbuf_size
);
336 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
337 if(buf
->mem
== (void *) 0) {
341 DBG("successfully attached buffer memory");
343 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
344 if(buf
->bufstruct_mem
== (void *) 0) {
348 DBG("successfully attached buffer bufstruct memory");
350 /* obtain info on the memory segment */
351 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
356 buf
->memlen
= shmds
.shm_segsz
;
358 /* open file for output */
360 /* Only create the directory if using the default path, because
361 * of the risk of typo when using trace path override. We don't
362 * want to risk creating plenty of useless directories in that case.
364 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
366 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
370 trace_path
= USTD_DEFAULT_TRACE_PATH
;
373 asprintf(&tmp
, "%s/%u_%lld", trace_path
, buf
->pid
, buf
->pidunique
);
374 result
= create_dir_if_needed(tmp
);
376 ERR("could not create directory %s", tmp
);
382 asprintf(&tmp
, "%s/%u_%lld/%s", trace_path
, buf
->pid
, buf
->pidunique
, buf
->name
);
383 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_EXCL
, 00600);
386 ERR("failed opening trace file %s", tmp
);
392 pthread_mutex_lock(&active_buffers_mutex
);
394 pthread_mutex_unlock(&active_buffers_mutex
);
403 int unwrite_last_subbuffer(struct buffer_info
*buf
)
407 result
= ftruncate(buf
->file_fd
, buf
->previous_offset
);
413 result
= lseek(buf
->file_fd
, buf
->previous_offset
, SEEK_SET
);
414 if(result
== (int)(off_t
)-1) {
422 int write_current_subbuffer(struct buffer_info
*buf
)
426 void *subbuf_mem
= buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1));
428 size_t cur_sb_size
= subbuffer_data_size(subbuf_mem
);
430 off_t cur_offset
= lseek(buf
->file_fd
, 0, SEEK_CUR
);
431 if(cur_offset
== (off_t
)-1) {
436 buf
->previous_offset
= cur_offset
;
437 DBG("previous_offset: %ld", cur_offset
);
439 result
= patient_write(buf
->file_fd
, subbuf_mem
, cur_sb_size
);
442 /* FIXME: maybe drop this trace */
449 int consumer_loop(struct buffer_info
*buf
)
453 pthread_cleanup_push(decrement_active_buffers
, NULL
);
456 /* get the subbuffer */
457 result
= get_subbuffer(buf
);
459 ERR("error getting subbuffer");
462 else if(result
== GET_SUBBUF_DONE
) {
466 else if(result
== GET_SUBBUF_DIED
) {
467 finish_consuming_dead_subbuffer(buf
);
471 /* write data to file */
472 write_current_subbuffer(buf
);
473 /* FIXME: handle return value? */
475 /* put the subbuffer */
476 result
= put_subbuffer(buf
);
478 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
481 else if(result
== PUT_SUBBUF_PUSHED
) {
482 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
485 else if(result
== PUT_SUBBUF_DIED
) {
486 WARN("application died while putting subbuffer");
487 /* Skip the first subbuffer. We are not sure it is trustable
488 * because the put_subbuffer() did not complete.
490 unwrite_last_subbuffer(buf
);
491 finish_consuming_dead_subbuffer(buf
);
494 else if(result
== PUT_SUBBUF_DONE
) {
495 /* Done with this subbuffer */
496 /* FIXME: add a case where this branch is used? Upon
497 * normal trace termination, at put_subbuf time, a
498 * special last-subbuffer code could be returned by
503 else if(result
== PUT_SUBBUF_OK
) {
507 DBG("thread for buffer %s is stopping", buf
->name
);
509 /* FIXME: destroy, unalloc... */
511 pthread_cleanup_pop(1);
516 void free_buffer(struct buffer_info
*buf
)
520 struct consumer_thread_args
{
525 void *consumer_thread(void *arg
)
527 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
528 struct consumer_thread_args
*args
= (struct consumer_thread_args
*) arg
;
530 DBG("GOT ARGS: pid %d bufname %s", args
->pid
, args
->bufname
);
532 buf
= connect_buffer(args
->pid
, args
->bufname
);
534 ERR("failed to connect to buffer");
543 /* bufname is free'd in free_buffer() */
548 int start_consuming_buffer(pid_t pid
, const char *bufname
)
551 struct consumer_thread_args
*args
;
553 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid
, bufname
);
555 args
= (struct consumer_thread_args
*) malloc(sizeof(struct consumer_thread_args
));
558 args
->bufname
= strdup(bufname
);
559 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
561 pthread_create(&thr
, NULL
, consumer_thread
, args
);
562 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
569 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
570 "\t-h\t\tDisplay this usage.\n"
571 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
572 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n"
573 "\t-d\t\tStart as a daemon.\n"
574 "\t--pidfile FILE\tWrite the PID in this file (when using -d).\n");
577 int parse_args(int argc
, char **argv
)
582 int option_index
= 0;
583 static struct option long_options
[] = {
584 {"pidfile", 1, 0, 'p'},
586 {"version", 0, 0, 'V'},
590 c
= getopt_long(argc
, argv
, "hs:o:d", long_options
, &option_index
);
596 printf("option %s", long_options
[option_index
].name
);
598 printf(" with arg %s", optarg
);
606 if(!is_directory(trace_path
)) {
607 ERR("Not a valid directory. (%s)", trace_path
);
615 pidfile
= strdup(optarg
);
621 printf("Version 0.0\n");
625 /* unknown option or other error; error is
626 printed by getopt, just return */
634 void sigterm_handler(int sig
)
639 static int write_pidfile(const char *file_name
, pid_t pid
)
643 pidfp
= fopen(file_name
, "w");
645 PERROR("fopen (%s)", pidfile
);
646 WARN("killing child process");
650 fprintf(pidfp
, "%d\n", pid
);
657 int start_ustd(int fd
)
659 struct ustcomm_ustd ustd
;
664 result
= sigemptyset(&sigset
);
666 PERROR("sigemptyset");
669 sa
.sa_handler
= sigterm_handler
;
671 sa
.sa_flags
= SA_RESTART
;
672 result
= sigaction(SIGTERM
, &sa
, NULL
);
678 result
= ustcomm_init_ustd(&ustd
, sock_path
);
680 ERR("failed to initialize socket");
684 /* setup handler for SIGPIPE */
685 result
= sigemptyset(&sigset
);
687 PERROR("sigemptyset");
690 result
= sigaddset(&sigset
, SIGPIPE
);
695 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
697 PERROR("sigprocmask");
703 result
= write_pidfile(pidfile
, getpid());
705 ERR("failed to write pidfile");
710 /* Notify parent that we are successfully started. */
712 /* write any one character */
713 result
= write(fd
, "!", 1);
719 ERR("Problem sending confirmation of daemon start to parent");
732 /* check for requests on our public socket */
733 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
735 ERR("error in ustcomm_ustd_recv_message");
739 if(!strncmp(recvbuf
, "collect", 7)) {
744 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
746 ERR("parsing error: %s", recvbuf
);
750 result
= start_consuming_buffer(pid
, bufname
);
752 ERR("error in add_buffer");
766 pthread_mutex_lock(&active_buffers_mutex
);
767 if(active_buffers
== 0) {
768 pthread_mutex_unlock(&active_buffers_mutex
);
771 pthread_mutex_unlock(&active_buffers_mutex
);
778 int start_ustd_daemon()
786 result
= child_pid
= fork();
791 else if(result
== 0) {
792 return start_ustd(fd
[1]);
797 result
= read(fd
[0], &buf
, 1);
803 ERR("did not receive valid confirmation that the daemon is started");
807 result
= close(fd
[0]);
812 DBG("The daemon is now successfully started");
815 /* Wait for confirmation that the server is ready. */
821 int main(int argc
, char **argv
)
825 result
= parse_args(argc
, argv
);
831 result
= start_ustd_daemon();
834 result
= start_ustd(-1);