1 /* Copyright (C) 2009 Pierre-Marc Fournier
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 /* return value: 0 = subbuffer is finished, it won't produce data anymore
37 * 1 = got subbuffer successfully
41 #define GET_SUBBUF_OK 1
42 #define GET_SUBBUF_DONE 0
43 #define GET_SUBBUF_DIED 2
45 #define PUT_SUBBUF_OK 1
46 #define PUT_SUBBUF_DIED 0
47 #define PUT_SUBBUF_PUSHED 2
48 #define PUT_SUBBUF_DONE 3
50 #define UNIX_PATH_MAX 108
52 int get_subbuffer(struct buffer_info
*buf
)
55 char *received_msg
=NULL
;
60 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
61 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
62 if((result
== -1 && (errno
== ECONNRESET
|| errno
== EPIPE
)) || result
== 0) {
63 DBG("app died while being traced");
64 retval
= GET_SUBBUF_DIED
;
68 ERR("get_subbuffer: ustcomm_send_request failed");
73 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
74 if(result
!= 2 && result
!= 1) {
75 ERR("unable to parse response to get_subbuffer");
81 if(!strcmp(rep_code
, "OK")) {
82 DBG("got subbuffer %s", buf
->name
);
83 retval
= GET_SUBBUF_OK
;
85 else if(nth_token_is(received_msg
, "END", 0) == 1) {
86 retval
= GET_SUBBUF_DONE
;
89 else if(!strcmp(received_msg
, "NOTFOUND")) {
90 DBG("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf
->name
);
91 retval
= GET_SUBBUF_DIED
;
95 DBG("error getting subbuffer %s", buf
->name
);
99 /* FIXME: free correctly the stuff */
112 int put_subbuffer(struct buffer_info
*buf
)
115 char *received_msg
=NULL
;
120 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
121 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
122 if(result
< 0 && (errno
== ECONNRESET
|| errno
== EPIPE
)) {
123 retval
= PUT_SUBBUF_DIED
;
126 else if(result
< 0) {
127 ERR("put_subbuffer: send_message failed");
131 else if(result
== 0) {
132 /* Program seems finished. However this might not be
133 * the last subbuffer that has to be collected.
135 retval
= PUT_SUBBUF_DIED
;
139 result
= sscanf(received_msg
, "%as", &rep_code
);
141 ERR("unable to parse response to put_subbuffer");
146 if(!strcmp(rep_code
, "OK")) {
147 DBG("subbuffer put %s", buf
->name
);
148 retval
= PUT_SUBBUF_OK
;
150 else if(!strcmp(received_msg
, "NOTFOUND")) {
151 DBG("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf
->name
);
152 /* However, maybe this was not the last subbuffer. So
153 * we return the program died.
155 retval
= PUT_SUBBUF_DIED
;
159 DBG("put_subbuffer: received error, we were pushed");
160 retval
= PUT_SUBBUF_PUSHED
;
177 void decrement_active_buffers(void *arg
)
179 struct libustd_instance
*instance
= arg
;
180 pthread_mutex_lock(&instance
->mutex
);
181 instance
->active_buffers
--;
182 pthread_mutex_unlock(&instance
->mutex
);
185 struct buffer_info
*connect_buffer(struct libustd_instance
*instance
, pid_t pid
, const char *bufname
)
187 struct buffer_info
*buf
;
191 struct shmid_ds shmds
;
193 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
195 ERR("add_buffer: insufficient memory");
203 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
205 WARN("unable to connect to process, it probably died before we were able to connect");
210 asprintf(&send_msg
, "get_pidunique");
211 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
214 ERR("problem in ustcomm_send_request(get_pidunique)");
221 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
223 ERR("unable to parse response to get_pidunique");
227 DBG("got pidunique %lld", buf
->pidunique
);
230 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
231 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
234 ERR("problem in ustcomm_send_request(get_shmid)");
241 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
243 ERR("unable to parse response to get_shmid (\"%s\")", received_msg
);
247 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
250 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
251 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
254 ERR("problem in ustcomm_send_request(g_n_subbufs)");
261 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
263 ERR("unable to parse response to get_n_subbufs");
267 DBG("got n_subbufs %d", buf
->n_subbufs
);
269 /* get subbuf size */
270 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
271 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
274 ERR("problem in ustcomm_send_request(get_subbuf_size)");
281 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
283 ERR("unable to parse response to get_subbuf_size");
287 DBG("got subbuf_size %d", buf
->subbuf_size
);
290 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
291 if(buf
->mem
== (void *) 0) {
295 DBG("successfully attached buffer memory");
297 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
298 if(buf
->bufstruct_mem
== (void *) 0) {
302 DBG("successfully attached buffer bufstruct memory");
304 /* obtain info on the memory segment */
305 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
310 buf
->memlen
= shmds
.shm_segsz
;
312 if(instance
->callbacks
->on_open_buffer
)
313 instance
->callbacks
->on_open_buffer(instance
->callbacks
, buf
);
315 pthread_mutex_lock(&instance
->mutex
);
316 instance
->active_buffers
++;
317 pthread_mutex_unlock(&instance
->mutex
);
326 static void destroy_buffer(struct libustd_callbacks
*callbacks
,
327 struct buffer_info
*buf
)
331 result
= ustcomm_close_app(&buf
->conn
);
333 WARN("problem calling ustcomm_close_app");
336 result
= shmdt(buf
->mem
);
341 result
= shmdt(buf
->bufstruct_mem
);
346 if(callbacks
->on_close_buffer
)
347 callbacks
->on_close_buffer(callbacks
, buf
);
352 int consumer_loop(struct libustd_instance
*instance
, struct buffer_info
*buf
)
356 pthread_cleanup_push(decrement_active_buffers
, instance
);
359 /* get the subbuffer */
360 result
= get_subbuffer(buf
);
362 ERR("error getting subbuffer");
365 else if(result
== GET_SUBBUF_DONE
) {
369 else if(result
== GET_SUBBUF_DIED
) {
370 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
374 if(instance
->callbacks
->on_read_subbuffer
)
375 instance
->callbacks
->on_read_subbuffer(instance
->callbacks
, buf
);
377 /* put the subbuffer */
378 result
= put_subbuffer(buf
);
380 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
383 else if(result
== PUT_SUBBUF_PUSHED
) {
384 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
387 else if(result
== PUT_SUBBUF_DIED
) {
388 DBG("application died while putting subbuffer");
389 /* Skip the first subbuffer. We are not sure it is trustable
390 * because the put_subbuffer() did not complete.
392 if(instance
->callbacks
->on_put_error
)
393 instance
->callbacks
->on_put_error(instance
->callbacks
, buf
);
395 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
398 else if(result
== PUT_SUBBUF_DONE
) {
399 /* Done with this subbuffer */
400 /* FIXME: add a case where this branch is used? Upon
401 * normal trace termination, at put_subbuf time, a
402 * special last-subbuffer code could be returned by
407 else if(result
== PUT_SUBBUF_OK
) {
411 DBG("thread for buffer %s is stopping", buf
->name
);
413 /* FIXME: destroy, unalloc... */
415 pthread_cleanup_pop(1);
420 struct consumer_thread_args
{
423 struct libustd_instance
*instance
;
426 void *consumer_thread(void *arg
)
428 struct buffer_info
*buf
;
429 struct consumer_thread_args
*args
= (struct consumer_thread_args
*) arg
;
433 DBG("GOT ARGS: pid %d bufname %s", args
->pid
, args
->bufname
);
435 if(args
->instance
->callbacks
->on_new_thread
)
436 args
->instance
->callbacks
->on_new_thread(args
->instance
->callbacks
);
438 /* Block signals that should be handled by the main thread. */
439 result
= sigemptyset(&sigset
);
441 PERROR("sigemptyset");
444 result
= sigaddset(&sigset
, SIGTERM
);
449 result
= sigaddset(&sigset
, SIGINT
);
454 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
456 PERROR("sigprocmask");
460 buf
= connect_buffer(args
->instance
, args
->pid
, args
->bufname
);
462 ERR("failed to connect to buffer");
466 consumer_loop(args
->instance
, buf
);
468 destroy_buffer(args
->instance
->callbacks
, buf
);
472 if(args
->instance
->callbacks
->on_close_thread
)
473 args
->instance
->callbacks
->on_close_thread(args
->instance
->callbacks
);
475 free((void *)args
->bufname
);
480 int start_consuming_buffer(
481 struct libustd_instance
*instance
, pid_t pid
, const char *bufname
)
484 struct consumer_thread_args
*args
;
487 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid
, bufname
);
489 args
= (struct consumer_thread_args
*) malloc(sizeof(struct consumer_thread_args
));
492 args
->bufname
= strdup(bufname
);
493 args
->instance
= instance
;
494 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
496 result
= pthread_create(&thr
, NULL
, consumer_thread
, args
);
498 ERR("pthread_create failed");
501 result
= pthread_detach(thr
);
503 ERR("pthread_detach failed");
506 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
511 int libustd_start_instance(struct libustd_instance
*instance
)
516 if(!instance
->is_init
) {
517 ERR("libustd instance not initialized");
525 /* check for requests on our public socket */
526 result
= ustcomm_ustd_recv_message(&instance
->comm
, &recvbuf
, NULL
, timeout
);
527 if(result
== -1 && errno
== EINTR
) {
530 else if(result
== -1) {
531 ERR("error in ustcomm_ustd_recv_message");
534 else if(result
> 0) {
535 if(!strncmp(recvbuf
, "collect", 7)) {
540 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
542 ERR("parsing error: %s", recvbuf
);
546 result
= start_consuming_buffer(instance
, pid
, bufname
);
548 ERR("error in add_buffer");
555 else if(!strncmp(recvbuf
, "exit", 4)) {
556 /* Only there to force poll to return */
559 WARN("unknown command: %s", recvbuf
);
567 if(instance
->quit_program
) {
568 pthread_mutex_lock(&instance
->mutex
);
569 if(instance
->active_buffers
== 0) {
570 pthread_mutex_unlock(&instance
->mutex
);
573 pthread_mutex_unlock(&instance
->mutex
);
578 if(instance
->callbacks
->on_trace_end
)
579 instance
->callbacks
->on_trace_end(instance
);
581 libustd_delete_instance(instance
);
586 void libustd_delete_instance(struct libustd_instance
*instance
)
588 if(instance
->is_init
)
589 ustcomm_fini_ustd(&instance
->comm
);
591 pthread_mutex_destroy(&instance
->mutex
);
592 free(instance
->sock_path
);
596 int libustd_stop_instance(struct libustd_instance
*instance
, int send_msg
)
604 instance
->quit_program
= 1;
609 /* Send a message through the socket to force poll to return */
611 struct sockaddr_un addr
;
613 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
619 addr
.sun_family
= AF_UNIX
;
621 strncpy(addr
.sun_path
, instance
->sock_path
, UNIX_PATH_MAX
);
622 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
624 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
629 while(bytes
!= sizeof(msg
))
630 bytes
+= send(fd
, msg
, sizeof(msg
), 0);
637 struct libustd_instance
*libustd_new_instance(
638 struct libustd_callbacks
*callbacks
, char *sock_path
)
640 struct libustd_instance
*instance
=
641 malloc(sizeof(struct libustd_instance
));
645 instance
->callbacks
= callbacks
;
646 instance
->quit_program
= 0;
647 instance
->is_init
= 0;
648 instance
->active_buffers
= 0;
649 pthread_mutex_init(&instance
->mutex
, NULL
);
652 instance
->sock_path
= strdup(sock_path
);
654 instance
->sock_path
= NULL
;
659 int libustd_init_instance(struct libustd_instance
*instance
)
662 result
= ustcomm_init_ustd(&instance
->comm
, instance
->sock_path
);
664 ERR("failed to initialize socket");
667 instance
->is_init
= 1;
This page took 0.059975 seconds and 4 git commands to generate.