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
;
55 /* Number of active buffers and the mutex to protect it. */
56 int active_buffers
= 0;
57 pthread_mutex_t active_buffers_mutex
= PTHREAD_MUTEX_INITIALIZER
;
58 /* Whether a request to end the program was received. */
59 sig_atomic_t terminate_req
= 0;
61 int test_sigpipe(void)
66 result
= sigemptyset(&sigset
);
68 perror("sigemptyset");
71 result
= sigaddset(&sigset
, SIGPIPE
);
77 result
= sigtimedwait(&sigset
, NULL
, &(struct timespec
){0,0});
78 if(result
== -1 && errno
== EAGAIN
) {
79 /* no signal received */
82 else if(result
== -1) {
83 perror("sigtimedwait");
86 else if(result
== SIGPIPE
) {
87 /* received sigpipe */
95 int get_subbuffer(struct buffer_info
*buf
)
103 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
104 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
107 WARN("process %d destroyed before we could connect to it", buf
->pid
);
108 return GET_SUBBUF_DONE
;
110 else if(result
< 0) {
111 ERR("get_subbuffer: ustcomm_send_request failed");
114 else if(result
== 0) {
115 DBG("app died while being traced");
116 return GET_SUBBUF_DIED
;
119 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
120 if(result
!= 2 && result
!= 1) {
121 ERR("unable to parse response to get_subbuffer");
125 DBG("received msg is %s", received_msg
);
127 if(!strcmp(rep_code
, "OK")) {
128 DBG("got subbuffer %s", buf
->name
);
129 retval
= GET_SUBBUF_OK
;
131 else if(nth_token_is(received_msg
, "END", 0) == 1) {
132 return GET_SUBBUF_DONE
;
135 DBG("error getting subbuffer %s", buf
->name
);
139 /* FIMXE: free correctly the stuff */
145 int put_subbuffer(struct buffer_info
*buf
)
153 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
154 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
156 ERR("put_subbuffer: send_message failed");
161 result
= sscanf(received_msg
, "%as", &rep_code
);
163 ERR("unable to parse response to put_subbuffer");
168 if(!strcmp(rep_code
, "OK")) {
169 DBG("subbuffer put %s", buf
->name
);
170 retval
= PUT_SUBBUF_OK
;
173 DBG("put_subbuffer: received error, we were pushed");
174 return PUT_SUBBUF_PUSHED
;
181 /* This write is patient because it restarts if it was incomplete.
184 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
186 const char *bufc
= (const char *) buf
;
190 result
= write(fd
, bufc
, count
);
202 return bufc
-(const char *)buf
;
205 void decrement_active_buffers(void *arg
)
207 pthread_mutex_lock(&active_buffers_mutex
);
209 pthread_mutex_unlock(&active_buffers_mutex
);
212 void *consumer_thread(void *arg
)
214 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
217 pthread_cleanup_push(decrement_active_buffers
, NULL
);
220 /* get the subbuffer */
221 result
= get_subbuffer(buf
);
223 ERR("error getting subbuffer");
226 else if(result
== GET_SUBBUF_DONE
) {
230 else if(result
== GET_SUBBUF_DIED
) {
231 finish_consuming_dead_subbuffer(buf
);
235 /* write data to file */
236 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
239 /* FIXME: maybe drop this trace */
242 /* put the subbuffer */
243 result
= put_subbuffer(buf
);
245 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
248 else if(result
== PUT_SUBBUF_PUSHED
) {
249 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
252 else if(result
== PUT_SUBBUF_DIED
) {
253 WARN("application died while putting subbuffer");
254 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
255 finish_consuming_dead_subbuffer(buf
);
257 else if(result
== PUT_SUBBUF_OK
) {
261 DBG("thread for buffer %s is stopping", buf
->name
);
263 /* FIXME: destroy, unalloc... */
265 pthread_cleanup_pop(1);
270 int create_dir_if_needed(char *dir
)
273 result
= mkdir(dir
, 0777);
275 if(errno
!= EEXIST
) {
284 int is_directory(const char *dir
)
289 result
= stat(dir
, &st
);
295 if(!S_ISDIR(st
.st_mode
)) {
302 int add_buffer(pid_t pid
, char *bufname
)
304 struct buffer_info
*buf
;
311 struct shmid_ds shmds
;
313 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
315 ERR("add_buffer: insufficient memory");
323 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
325 WARN("unable to connect to process, it probably died before we were able to connect");
330 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
331 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
334 ERR("problem in ustcomm_send_request(get_shmid)");
338 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
340 ERR("unable to parse response to get_shmid");
344 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
347 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
348 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
351 ERR("problem in ustcomm_send_request(g_n_subbufs)");
355 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
357 ERR("unable to parse response to get_n_subbufs");
361 DBG("got n_subbufs %d", buf
->n_subbufs
);
363 /* get subbuf size */
364 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
365 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
368 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
370 ERR("unable to parse response to get_subbuf_size");
374 DBG("got subbuf_size %d", buf
->subbuf_size
);
377 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
378 if(buf
->mem
== (void *) 0) {
382 DBG("successfully attached buffer memory");
384 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
385 if(buf
->bufstruct_mem
== (void *) 0) {
389 DBG("successfully attached buffer bufstruct memory");
391 /* obtain info on the memory segment */
392 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
397 buf
->memlen
= shmds
.shm_segsz
;
399 /* open file for output */
401 /* Only create the directory if using the default path, because
402 * of the risk of typo when using trace path override. We don't
403 * want to risk creating plenty of useless directories in that case.
405 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
407 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
411 trace_path
= USTD_DEFAULT_TRACE_PATH
;
414 asprintf(&tmp
, "%s/%u", trace_path
, buf
->pid
);
415 result
= create_dir_if_needed(tmp
);
417 ERR("could not create directory %s", tmp
);
423 asprintf(&tmp
, "%s/%u/%s_0", trace_path
, buf
->pid
, buf
->name
);
424 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
427 ERR("failed opening trace file %s", tmp
);
433 pthread_mutex_lock(&active_buffers_mutex
);
435 pthread_mutex_unlock(&active_buffers_mutex
);
437 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
444 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
445 "\t-h\t\tDisplay this usage.\n"
446 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
447 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
450 int parse_args(int argc
, char **argv
)
455 int option_index
= 0;
456 static struct option long_options
[] = {
458 {"version", 0, 0, 'V'},
462 c
= getopt_long(argc
, argv
, "hs:o:", long_options
, &option_index
);
468 printf("option %s", long_options
[option_index
].name
);
470 printf(" with arg %s", optarg
);
478 if(!is_directory(trace_path
)) {
479 ERR("Not a valid directory. (%s)", trace_path
);
487 printf("Version 0.0\n");
491 /* unknown option or other error; error is
492 printed by getopt, just return */
500 void sigterm_handler(int sig
)
505 int main(int argc
, char **argv
)
507 struct ustcomm_ustd ustd
;
512 result
= sigemptyset(&sigset
);
514 perror("sigemptyset");
517 sa
.sa_handler
= sigterm_handler
;
519 sa
.sa_flags
= SA_RESTART
;
520 result
= sigaction(SIGTERM
, &sa
, NULL
);
526 result
= parse_args(argc
, argv
);
531 result
= ustcomm_init_ustd(&ustd
, sock_path
);
533 ERR("failed to initialize socket");
537 /* setup handler for SIGPIPE */
538 result
= sigemptyset(&sigset
);
540 perror("sigemptyset");
543 result
= sigaddset(&sigset
, SIGPIPE
);
548 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
550 perror("sigprocmask");
558 /* check for requests on our public socket */
559 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
561 ERR("error in ustcomm_ustd_recv_message");
565 if(!strncmp(recvbuf
, "collect", 7)) {
570 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
572 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
575 result
= add_buffer(pid
, bufname
);
577 ERR("error in add_buffer");
586 pthread_mutex_lock(&active_buffers_mutex
);
587 if(active_buffers
== 0) {
588 pthread_mutex_unlock(&active_buffers_mutex
);
591 pthread_mutex_unlock(&active_buffers_mutex
);