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");
199 buf
->conn
= malloc(sizeof(struct ustcomm_connection
));
200 if(buf
->conn
== NULL
) {
201 ERR("add_buffer: insufficient memory");
210 result
= ustcomm_connect_app(buf
->pid
, buf
->conn
);
212 WARN("unable to connect to process, it probably died before we were able to connect");
217 asprintf(&send_msg
, "get_pidunique");
218 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
221 ERR("problem in ustcomm_send_request(get_pidunique)");
228 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
230 ERR("unable to parse response to get_pidunique");
234 DBG("got pidunique %lld", buf
->pidunique
);
237 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
238 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
241 ERR("problem in ustcomm_send_request(get_shmid)");
248 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
250 ERR("unable to parse response to get_shmid (\"%s\")", received_msg
);
254 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
257 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
258 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
261 ERR("problem in ustcomm_send_request(g_n_subbufs)");
268 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
270 ERR("unable to parse response to get_n_subbufs");
274 DBG("got n_subbufs %d", buf
->n_subbufs
);
276 /* get subbuf size */
277 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
278 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
281 ERR("problem in ustcomm_send_request(get_subbuf_size)");
288 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
290 ERR("unable to parse response to get_subbuf_size");
294 DBG("got subbuf_size %d", buf
->subbuf_size
);
297 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
298 if(buf
->mem
== (void *) 0) {
302 DBG("successfully attached buffer memory");
304 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
305 if(buf
->bufstruct_mem
== (void *) 0) {
309 DBG("successfully attached buffer bufstruct memory");
311 /* obtain info on the memory segment */
312 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
317 buf
->memlen
= shmds
.shm_segsz
;
319 if(instance
->callbacks
->on_open_buffer
)
320 instance
->callbacks
->on_open_buffer(instance
->callbacks
, buf
);
322 pthread_mutex_lock(&instance
->mutex
);
323 instance
->active_buffers
++;
324 pthread_mutex_unlock(&instance
->mutex
);
333 static void destroy_buffer(struct libustd_callbacks
*callbacks
,
334 struct buffer_info
*buf
)
338 result
= ustcomm_close_app(buf
->conn
);
340 WARN("problem calling ustcomm_close_app");
343 result
= shmdt(buf
->mem
);
348 result
= shmdt(buf
->bufstruct_mem
);
353 if(callbacks
->on_close_buffer
)
354 callbacks
->on_close_buffer(callbacks
, buf
);
360 int consumer_loop(struct libustd_instance
*instance
, struct buffer_info
*buf
)
364 pthread_cleanup_push(decrement_active_buffers
, instance
);
367 /* get the subbuffer */
368 result
= get_subbuffer(buf
);
370 ERR("error getting subbuffer");
373 else if(result
== GET_SUBBUF_DONE
) {
377 else if(result
== GET_SUBBUF_DIED
) {
378 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
382 if(instance
->callbacks
->on_read_subbuffer
)
383 instance
->callbacks
->on_read_subbuffer(instance
->callbacks
, buf
);
385 /* put the subbuffer */
386 result
= put_subbuffer(buf
);
388 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
391 else if(result
== PUT_SUBBUF_PUSHED
) {
392 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
395 else if(result
== PUT_SUBBUF_DIED
) {
396 DBG("application died while putting subbuffer");
397 /* Skip the first subbuffer. We are not sure it is trustable
398 * because the put_subbuffer() did not complete.
400 if(instance
->callbacks
->on_put_error
)
401 instance
->callbacks
->on_put_error(instance
->callbacks
, buf
);
403 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
406 else if(result
== PUT_SUBBUF_DONE
) {
407 /* Done with this subbuffer */
408 /* FIXME: add a case where this branch is used? Upon
409 * normal trace termination, at put_subbuf time, a
410 * special last-subbuffer code could be returned by
415 else if(result
== PUT_SUBBUF_OK
) {
419 DBG("thread for buffer %s is stopping", buf
->name
);
421 /* FIXME: destroy, unalloc... */
423 pthread_cleanup_pop(1);
428 struct consumer_thread_args
{
431 struct libustd_instance
*instance
;
434 void *consumer_thread(void *arg
)
436 struct buffer_info
*buf
;
437 struct consumer_thread_args
*args
= (struct consumer_thread_args
*) arg
;
441 DBG("GOT ARGS: pid %d bufname %s", args
->pid
, args
->bufname
);
443 if(args
->instance
->callbacks
->on_new_thread
)
444 args
->instance
->callbacks
->on_new_thread(args
->instance
->callbacks
);
446 /* Block signals that should be handled by the main thread. */
447 result
= sigemptyset(&sigset
);
449 PERROR("sigemptyset");
452 result
= sigaddset(&sigset
, SIGTERM
);
457 result
= sigaddset(&sigset
, SIGINT
);
462 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
464 PERROR("sigprocmask");
468 buf
= connect_buffer(args
->instance
, args
->pid
, args
->bufname
);
470 ERR("failed to connect to buffer");
474 consumer_loop(args
->instance
, buf
);
476 destroy_buffer(args
->instance
->callbacks
, buf
);
480 if(args
->instance
->callbacks
->on_close_thread
)
481 args
->instance
->callbacks
->on_close_thread(args
->instance
->callbacks
);
483 free((void *)args
->bufname
);
488 int start_consuming_buffer(
489 struct libustd_instance
*instance
, pid_t pid
, const char *bufname
)
492 struct consumer_thread_args
*args
;
495 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid
, bufname
);
497 args
= (struct consumer_thread_args
*) malloc(sizeof(struct consumer_thread_args
));
500 args
->bufname
= strdup(bufname
);
501 args
->instance
= instance
;
502 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
504 result
= pthread_create(&thr
, NULL
, consumer_thread
, args
);
506 ERR("pthread_create failed");
509 result
= pthread_detach(thr
);
511 ERR("pthread_detach failed");
514 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
519 int libustd_start_instance(struct libustd_instance
*instance
)
524 if(!instance
->is_init
) {
525 ERR("libustd instance not initialized");
533 /* check for requests on our public socket */
534 result
= ustcomm_ustd_recv_message(instance
->comm
, &recvbuf
, NULL
, timeout
);
535 if(result
== -1 && errno
== EINTR
) {
538 else if(result
== -1) {
539 ERR("error in ustcomm_ustd_recv_message");
542 else if(result
> 0) {
543 if(!strncmp(recvbuf
, "collect", 7)) {
548 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
550 ERR("parsing error: %s", recvbuf
);
554 result
= start_consuming_buffer(instance
, pid
, bufname
);
556 ERR("error in add_buffer");
563 else if(!strncmp(recvbuf
, "exit", 4)) {
564 /* Only there to force poll to return */
567 WARN("unknown command: %s", recvbuf
);
575 if(instance
->quit_program
) {
576 pthread_mutex_lock(&instance
->mutex
);
577 if(instance
->active_buffers
== 0) {
578 pthread_mutex_unlock(&instance
->mutex
);
581 pthread_mutex_unlock(&instance
->mutex
);
586 if(instance
->callbacks
->on_trace_end
)
587 instance
->callbacks
->on_trace_end(instance
);
589 libustd_delete_instance(instance
);
594 void libustd_delete_instance(struct libustd_instance
*instance
)
596 if(instance
->is_init
)
597 ustcomm_fini_ustd(instance
->comm
);
599 pthread_mutex_destroy(&instance
->mutex
);
600 free(instance
->sock_path
);
601 free(instance
->comm
);
605 int libustd_stop_instance(struct libustd_instance
*instance
, int send_msg
)
613 instance
->quit_program
= 1;
618 /* Send a message through the socket to force poll to return */
620 struct sockaddr_un addr
;
622 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
628 addr
.sun_family
= AF_UNIX
;
630 strncpy(addr
.sun_path
, instance
->sock_path
, UNIX_PATH_MAX
);
631 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
633 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
638 while(bytes
!= sizeof(msg
))
639 bytes
+= send(fd
, msg
, sizeof(msg
), 0);
646 struct libustd_instance
*libustd_new_instance(
647 struct libustd_callbacks
*callbacks
, char *sock_path
)
649 struct libustd_instance
*instance
=
650 malloc(sizeof(struct libustd_instance
));
654 instance
->comm
= malloc(sizeof(struct ustcomm_ustd
));
655 if(!instance
->comm
) {
660 instance
->callbacks
= callbacks
;
661 instance
->quit_program
= 0;
662 instance
->is_init
= 0;
663 instance
->active_buffers
= 0;
664 pthread_mutex_init(&instance
->mutex
, NULL
);
667 instance
->sock_path
= strdup(sock_path
);
669 instance
->sock_path
= NULL
;
674 int libustd_init_instance(struct libustd_instance
*instance
)
677 result
= ustcomm_init_ustd(instance
->comm
, instance
->sock_path
);
679 ERR("failed to initialize socket");
682 instance
->is_init
= 1;
This page took 0.058098 seconds and 4 git commands to generate.