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>
40 /* return value: 0 = subbuffer is finished, it won't produce data anymore
41 * 1 = got subbuffer successfully
45 #define GET_SUBBUF_OK 1
46 #define GET_SUBBUF_DONE 0
47 #define GET_SUBBUF_DIED 2
49 #define PUT_SUBBUF_OK 1
50 #define PUT_SUBBUF_DIED 0
51 #define PUT_SUBBUF_PUSHED 2
54 char *trace_path
=NULL
;
56 /* Number of active buffers and the mutex to protect it. */
57 int active_buffers
= 0;
58 pthread_mutex_t active_buffers_mutex
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Whether a request to end the program was received. */
60 sig_atomic_t terminate_req
= 0;
62 int get_subbuffer(struct buffer_info
*buf
)
65 char *received_msg
=NULL
;
70 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
71 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
72 if((result
== -1 && errno
== EPIPE
) || result
== 0) {
73 DBG("app died while being traced");
74 retval
= GET_SUBBUF_DIED
;
78 ERR("get_subbuffer: ustcomm_send_request failed");
83 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
84 if(result
!= 2 && result
!= 1) {
85 ERR("unable to parse response to get_subbuffer");
90 DBG("received msg is %s", received_msg
);
92 if(!strcmp(rep_code
, "OK")) {
93 DBG("got subbuffer %s", buf
->name
);
94 retval
= GET_SUBBUF_OK
;
96 else if(nth_token_is(received_msg
, "END", 0) == 1) {
97 retval
= GET_SUBBUF_DONE
;
101 DBG("error getting subbuffer %s", buf
->name
);
105 /* FIMXE: free correctly the stuff */
118 int put_subbuffer(struct buffer_info
*buf
)
121 char *received_msg
=NULL
;
126 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
127 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
128 if(result
< 0 && errno
== ECONNRESET
) {
129 retval
= PUT_SUBBUF_DIED
;
133 ERR("put_subbuffer: send_message failed");
138 result
= sscanf(received_msg
, "%as", &rep_code
);
140 ERR("unable to parse response to put_subbuffer");
145 if(!strcmp(rep_code
, "OK")) {
146 DBG("subbuffer put %s", buf
->name
);
147 retval
= PUT_SUBBUF_OK
;
150 DBG("put_subbuffer: received error, we were pushed");
151 retval
= PUT_SUBBUF_PUSHED
;
168 void decrement_active_buffers(void *arg
)
170 pthread_mutex_lock(&active_buffers_mutex
);
172 pthread_mutex_unlock(&active_buffers_mutex
);
175 void *consumer_thread(void *arg
)
177 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
180 pthread_cleanup_push(decrement_active_buffers
, NULL
);
183 /* get the subbuffer */
184 result
= get_subbuffer(buf
);
186 ERR("error getting subbuffer");
189 else if(result
== GET_SUBBUF_DONE
) {
193 else if(result
== GET_SUBBUF_DIED
) {
194 finish_consuming_dead_subbuffer(buf
);
198 /* write data to file */
199 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
202 /* FIXME: maybe drop this trace */
205 /* put the subbuffer */
206 result
= put_subbuffer(buf
);
208 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
211 else if(result
== PUT_SUBBUF_PUSHED
) {
212 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
215 else if(result
== PUT_SUBBUF_DIED
) {
216 WARN("application died while putting subbuffer");
217 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
218 finish_consuming_dead_subbuffer(buf
);
221 else if(result
== PUT_SUBBUF_OK
) {
225 DBG("thread for buffer %s is stopping", buf
->name
);
227 /* FIXME: destroy, unalloc... */
229 pthread_cleanup_pop(1);
234 int create_dir_if_needed(char *dir
)
237 result
= mkdir(dir
, 0777);
239 if(errno
!= EEXIST
) {
248 int is_directory(const char *dir
)
253 result
= stat(dir
, &st
);
259 if(!S_ISDIR(st
.st_mode
)) {
266 int add_buffer(pid_t pid
, char *bufname
)
268 struct buffer_info
*buf
;
275 struct shmid_ds shmds
;
277 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
279 ERR("add_buffer: insufficient memory");
287 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
289 WARN("unable to connect to process, it probably died before we were able to connect");
294 asprintf(&send_msg
, "get_pidunique");
295 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
298 ERR("problem in ustcomm_send_request(get_pidunique)");
302 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
304 ERR("unable to parse response to get_pidunique");
308 DBG("got pidunique %lld", buf
->pidunique
);
311 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
312 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
315 ERR("problem in ustcomm_send_request(get_shmid)");
319 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
321 ERR("unable to parse response to get_shmid");
325 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
328 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
329 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
332 ERR("problem in ustcomm_send_request(g_n_subbufs)");
336 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
338 ERR("unable to parse response to get_n_subbufs");
342 DBG("got n_subbufs %d", buf
->n_subbufs
);
344 /* get subbuf size */
345 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
346 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
349 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
351 ERR("unable to parse response to get_subbuf_size");
355 DBG("got subbuf_size %d", buf
->subbuf_size
);
358 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
359 if(buf
->mem
== (void *) 0) {
363 DBG("successfully attached buffer memory");
365 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
366 if(buf
->bufstruct_mem
== (void *) 0) {
370 DBG("successfully attached buffer bufstruct memory");
372 /* obtain info on the memory segment */
373 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
378 buf
->memlen
= shmds
.shm_segsz
;
380 /* open file for output */
382 /* Only create the directory if using the default path, because
383 * of the risk of typo when using trace path override. We don't
384 * want to risk creating plenty of useless directories in that case.
386 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
388 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
392 trace_path
= USTD_DEFAULT_TRACE_PATH
;
395 asprintf(&tmp
, "%s/%u_%lld", trace_path
, buf
->pid
, buf
->pidunique
);
396 result
= create_dir_if_needed(tmp
);
398 ERR("could not create directory %s", tmp
);
404 asprintf(&tmp
, "%s/%u_%lld/%s_0", trace_path
, buf
->pid
, buf
->pidunique
, buf
->name
);
405 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_EXCL
, 00600);
408 ERR("failed opening trace file %s", tmp
);
414 pthread_mutex_lock(&active_buffers_mutex
);
416 pthread_mutex_unlock(&active_buffers_mutex
);
418 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
425 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
426 "\t-h\t\tDisplay this usage.\n"
427 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
428 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
431 int parse_args(int argc
, char **argv
)
436 int option_index
= 0;
437 static struct option long_options
[] = {
439 {"version", 0, 0, 'V'},
443 c
= getopt_long(argc
, argv
, "hs:o:", long_options
, &option_index
);
449 printf("option %s", long_options
[option_index
].name
);
451 printf(" with arg %s", optarg
);
459 if(!is_directory(trace_path
)) {
460 ERR("Not a valid directory. (%s)", trace_path
);
468 printf("Version 0.0\n");
472 /* unknown option or other error; error is
473 printed by getopt, just return */
481 void sigterm_handler(int sig
)
486 int main(int argc
, char **argv
)
488 struct ustcomm_ustd ustd
;
493 result
= sigemptyset(&sigset
);
495 PERROR("sigemptyset");
498 sa
.sa_handler
= sigterm_handler
;
500 sa
.sa_flags
= SA_RESTART
;
501 result
= sigaction(SIGTERM
, &sa
, NULL
);
507 result
= parse_args(argc
, argv
);
512 result
= ustcomm_init_ustd(&ustd
, sock_path
);
514 ERR("failed to initialize socket");
518 /* setup handler for SIGPIPE */
519 result
= sigemptyset(&sigset
);
521 PERROR("sigemptyset");
524 result
= sigaddset(&sigset
, SIGPIPE
);
529 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
531 PERROR("sigprocmask");
539 /* check for requests on our public socket */
540 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
542 ERR("error in ustcomm_ustd_recv_message");
546 if(!strncmp(recvbuf
, "collect", 7)) {
551 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
553 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
556 result
= add_buffer(pid
, bufname
);
558 ERR("error in add_buffer");
567 pthread_mutex_lock(&active_buffers_mutex
);
568 if(active_buffers
== 0) {
569 pthread_mutex_unlock(&active_buffers_mutex
);
572 pthread_mutex_unlock(&active_buffers_mutex
);