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 /* FIXME: we actually should unput the buffer before consuming... */
207 result
= put_subbuffer(buf
);
209 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
212 else if(result
== PUT_SUBBUF_PUSHED
) {
213 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
216 else if(result
== PUT_SUBBUF_DIED
) {
217 WARN("application died while putting subbuffer");
218 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
219 finish_consuming_dead_subbuffer(buf
);
222 else if(result
== PUT_SUBBUF_OK
) {
226 DBG("thread for buffer %s is stopping", buf
->name
);
228 /* FIXME: destroy, unalloc... */
230 pthread_cleanup_pop(1);
235 int create_dir_if_needed(char *dir
)
238 result
= mkdir(dir
, 0777);
240 if(errno
!= EEXIST
) {
249 int is_directory(const char *dir
)
254 result
= stat(dir
, &st
);
260 if(!S_ISDIR(st
.st_mode
)) {
267 int add_buffer(pid_t pid
, char *bufname
)
269 struct buffer_info
*buf
;
276 struct shmid_ds shmds
;
278 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
280 ERR("add_buffer: insufficient memory");
288 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
290 WARN("unable to connect to process, it probably died before we were able to connect");
295 asprintf(&send_msg
, "get_pidunique");
296 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
299 ERR("problem in ustcomm_send_request(get_pidunique)");
303 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
305 ERR("unable to parse response to get_pidunique");
309 DBG("got pidunique %lld", buf
->pidunique
);
312 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
313 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
316 ERR("problem in ustcomm_send_request(get_shmid)");
320 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
322 ERR("unable to parse response to get_shmid");
326 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
329 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
330 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
333 ERR("problem in ustcomm_send_request(g_n_subbufs)");
337 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
339 ERR("unable to parse response to get_n_subbufs");
343 DBG("got n_subbufs %d", buf
->n_subbufs
);
345 /* get subbuf size */
346 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
347 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
350 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
352 ERR("unable to parse response to get_subbuf_size");
356 DBG("got subbuf_size %d", buf
->subbuf_size
);
359 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
360 if(buf
->mem
== (void *) 0) {
364 DBG("successfully attached buffer memory");
366 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
367 if(buf
->bufstruct_mem
== (void *) 0) {
371 DBG("successfully attached buffer bufstruct memory");
373 /* obtain info on the memory segment */
374 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
379 buf
->memlen
= shmds
.shm_segsz
;
381 /* open file for output */
383 /* Only create the directory if using the default path, because
384 * of the risk of typo when using trace path override. We don't
385 * want to risk creating plenty of useless directories in that case.
387 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
389 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
393 trace_path
= USTD_DEFAULT_TRACE_PATH
;
396 asprintf(&tmp
, "%s/%u_%lld", trace_path
, buf
->pid
, buf
->pidunique
);
397 result
= create_dir_if_needed(tmp
);
399 ERR("could not create directory %s", tmp
);
405 asprintf(&tmp
, "%s/%u_%lld/%s_0", trace_path
, buf
->pid
, buf
->pidunique
, buf
->name
);
406 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_EXCL
, 00600);
409 ERR("failed opening trace file %s", tmp
);
415 pthread_mutex_lock(&active_buffers_mutex
);
417 pthread_mutex_unlock(&active_buffers_mutex
);
419 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
426 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
427 "\t-h\t\tDisplay this usage.\n"
428 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
429 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
432 int parse_args(int argc
, char **argv
)
437 int option_index
= 0;
438 static struct option long_options
[] = {
440 {"version", 0, 0, 'V'},
444 c
= getopt_long(argc
, argv
, "hs:o:", long_options
, &option_index
);
450 printf("option %s", long_options
[option_index
].name
);
452 printf(" with arg %s", optarg
);
460 if(!is_directory(trace_path
)) {
461 ERR("Not a valid directory. (%s)", trace_path
);
469 printf("Version 0.0\n");
473 /* unknown option or other error; error is
474 printed by getopt, just return */
482 void sigterm_handler(int sig
)
487 int main(int argc
, char **argv
)
489 struct ustcomm_ustd ustd
;
494 result
= sigemptyset(&sigset
);
496 PERROR("sigemptyset");
499 sa
.sa_handler
= sigterm_handler
;
501 sa
.sa_flags
= SA_RESTART
;
502 result
= sigaction(SIGTERM
, &sa
, NULL
);
508 result
= parse_args(argc
, argv
);
513 result
= ustcomm_init_ustd(&ustd
, sock_path
);
515 ERR("failed to initialize socket");
519 /* setup handler for SIGPIPE */
520 result
= sigemptyset(&sigset
);
522 PERROR("sigemptyset");
525 result
= sigaddset(&sigset
, SIGPIPE
);
530 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
532 PERROR("sigprocmask");
540 /* check for requests on our public socket */
541 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
543 ERR("error in ustcomm_ustd_recv_message");
547 if(!strncmp(recvbuf
, "collect", 7)) {
552 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
554 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
557 result
= add_buffer(pid
, bufname
);
559 ERR("error in add_buffer");
568 pthread_mutex_lock(&active_buffers_mutex
);
569 if(active_buffers
== 0) {
570 pthread_mutex_unlock(&active_buffers_mutex
);
573 pthread_mutex_unlock(&active_buffers_mutex
);