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 int test_sigpipe(void)
60 result
= sigemptyset(&sigset
);
62 perror("sigemptyset");
65 result
= sigaddset(&sigset
, SIGPIPE
);
71 result
= sigtimedwait(&sigset
, NULL
, &(struct timespec
){0,0});
72 if(result
== -1 && errno
== EAGAIN
) {
73 /* no signal received */
76 else if(result
== -1) {
77 perror("sigtimedwait");
80 else if(result
== SIGPIPE
) {
81 /* received sigpipe */
89 int get_subbuffer(struct buffer_info
*buf
)
97 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
98 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
101 WARN("process %d destroyed before we could connect to it", buf
->pid
);
102 return GET_SUBBUF_DONE
;
104 else if(result
< 0) {
105 ERR("get_subbuffer: ustcomm_send_request failed");
108 else if(result
== 0) {
109 DBG("app died while being traced");
110 return GET_SUBBUF_DIED
;
113 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
114 if(result
!= 2 && result
!= 1) {
115 ERR("unable to parse response to get_subbuffer");
119 DBG("received msg is %s", received_msg
);
121 if(!strcmp(rep_code
, "OK")) {
122 DBG("got subbuffer %s", buf
->name
);
123 retval
= GET_SUBBUF_OK
;
125 else if(nth_token_is(received_msg
, "END", 0) == 1) {
126 return GET_SUBBUF_DONE
;
129 DBG("error getting subbuffer %s", buf
->name
);
133 /* FIMXE: free correctly the stuff */
139 int put_subbuffer(struct buffer_info
*buf
)
147 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
148 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
150 ERR("put_subbuffer: send_message failed");
155 result
= sscanf(received_msg
, "%as", &rep_code
);
157 ERR("unable to parse response to put_subbuffer");
162 if(!strcmp(rep_code
, "OK")) {
163 DBG("subbuffer put %s", buf
->name
);
164 retval
= PUT_SUBBUF_OK
;
167 DBG("put_subbuffer: received error, we were pushed");
168 return PUT_SUBBUF_PUSHED
;
175 /* This write is patient because it restarts if it was incomplete.
178 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
180 const char *bufc
= (const char *) buf
;
184 result
= write(fd
, bufc
, count
);
196 return bufc
-(const char *)buf
;
199 void *consumer_thread(void *arg
)
201 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
205 /* get the subbuffer */
206 result
= get_subbuffer(buf
);
208 ERR("error getting subbuffer");
211 else if(result
== GET_SUBBUF_DONE
) {
215 else if(result
== GET_SUBBUF_DIED
) {
216 finish_consuming_dead_subbuffer(buf
);
220 /* write data to file */
221 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
224 /* FIXME: maybe drop this trace */
227 /* put the subbuffer */
228 result
= put_subbuffer(buf
);
230 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
233 else if(result
== PUT_SUBBUF_PUSHED
) {
234 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
237 else if(result
== PUT_SUBBUF_DIED
) {
238 WARN("application died while putting subbuffer");
239 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
240 finish_consuming_dead_subbuffer(buf
);
242 else if(result
== PUT_SUBBUF_OK
) {
246 DBG("thread for buffer %s is stopping", buf
->name
);
248 /* FIXME: destroy, unalloc... */
253 int create_dir_if_needed(char *dir
)
256 result
= mkdir(dir
, 0777);
258 if(errno
!= EEXIST
) {
267 int is_directory(const char *dir
)
272 result
= stat(dir
, &st
);
278 if(!S_ISDIR(st
.st_mode
)) {
285 int add_buffer(pid_t pid
, char *bufname
)
287 struct buffer_info
*buf
;
294 struct shmid_ds shmds
;
296 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
298 ERR("add_buffer: insufficient memory");
306 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
308 WARN("unable to connect to process, it probably died before we were able to connect");
313 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
314 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
317 ERR("problem in ustcomm_send_request(get_shmid)");
321 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
323 ERR("unable to parse response to get_shmid");
327 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
330 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
331 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
334 ERR("problem in ustcomm_send_request(g_n_subbufs)");
338 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
340 ERR("unable to parse response to get_n_subbufs");
344 DBG("got n_subbufs %d", buf
->n_subbufs
);
346 /* get subbuf size */
347 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
348 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
351 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
353 ERR("unable to parse response to get_subbuf_size");
357 DBG("got subbuf_size %d", buf
->subbuf_size
);
360 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
361 if(buf
->mem
== (void *) 0) {
365 DBG("successfully attached buffer memory");
367 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
368 if(buf
->bufstruct_mem
== (void *) 0) {
372 DBG("successfully attached buffer bufstruct memory");
374 /* obtain info on the memory segment */
375 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
380 buf
->memlen
= shmds
.shm_segsz
;
382 /* open file for output */
384 /* Only create the directory if using the default path, because
385 * of the risk of typo when using trace path override. We don't
386 * want to risk creating plenty of useless directories in that case.
388 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
390 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
394 trace_path
= USTD_DEFAULT_TRACE_PATH
;
397 asprintf(&tmp
, "%s/%u", trace_path
, buf
->pid
);
398 result
= create_dir_if_needed(tmp
);
400 ERR("could not create directory %s", tmp
);
406 asprintf(&tmp
, "%s/%u/%s_0", trace_path
, buf
->pid
, buf
->name
);
407 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
410 ERR("failed opening trace file %s", tmp
);
416 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
423 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
424 "\t-h\t\tDisplay this usage.\n"
425 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
426 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
429 int parse_args(int argc
, char **argv
)
434 int option_index
= 0;
435 static struct option long_options
[] = {
437 {"version", 0, 0, 'V'},
441 c
= getopt_long(argc
, argv
, "hs:o:", long_options
, &option_index
);
447 printf("option %s", long_options
[option_index
].name
);
449 printf(" with arg %s", optarg
);
457 if(!is_directory(trace_path
)) {
458 ERR("Not a valid directory. (%s)", trace_path
);
466 printf("Version 0.0\n");
470 /* unknown option or other error; error is
471 printed by getopt, just return */
479 int main(int argc
, char **argv
)
481 struct ustcomm_ustd ustd
;
485 result
= parse_args(argc
, argv
);
490 result
= ustcomm_init_ustd(&ustd
, sock_path
);
492 ERR("failed to initialize socket");
496 result
= sigemptyset(&sigset
);
498 perror("sigemptyset");
501 result
= sigaddset(&sigset
, SIGPIPE
);
506 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
508 perror("sigprocmask");
516 /* check for requests on our public socket */
517 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
519 ERR("error in ustcomm_ustd_recv_message");
523 if(!strncmp(recvbuf
, "collect", 7)) {
528 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
530 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
533 result
= add_buffer(pid
, bufname
);
535 ERR("error in add_buffer");
This page took 0.047191 seconds and 4 git commands to generate.