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
37 /* return value: 0 = subbuffer is finished, it won't produce data anymore
38 * 1 = got subbuffer successfully
42 #define GET_SUBBUF_OK 1
43 #define GET_SUBBUF_DONE 0
44 #define GET_SUBBUF_DIED 2
46 #define PUT_SUBBUF_OK 1
47 #define PUT_SUBBUF_DIED 0
48 #define PUT_SUBBUF_PUSHED 2
49 #define PUT_SUBBUF_DONE 3
51 #define UNIX_PATH_MAX 108
53 int get_subbuffer(struct buffer_info
*buf
)
56 char *received_msg
=NULL
;
61 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
62 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
63 if((result
== -1 && (errno
== ECONNRESET
|| errno
== EPIPE
)) || result
== 0) {
64 DBG("app died while being traced");
65 retval
= GET_SUBBUF_DIED
;
69 ERR("get_subbuffer: ustcomm_send_request failed");
74 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
75 if(result
!= 2 && result
!= 1) {
76 ERR("unable to parse response to get_subbuffer");
82 if(!strcmp(rep_code
, "OK")) {
83 DBG("got subbuffer %s", buf
->name
);
84 retval
= GET_SUBBUF_OK
;
86 else if(nth_token_is(received_msg
, "END", 0) == 1) {
87 retval
= GET_SUBBUF_DONE
;
90 else if(!strcmp(received_msg
, "NOTFOUND")) {
91 DBG("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf
->name
);
92 retval
= GET_SUBBUF_DIED
;
96 DBG("error getting subbuffer %s", buf
->name
);
100 /* FIXME: free correctly the stuff */
113 int put_subbuffer(struct buffer_info
*buf
)
116 char *received_msg
=NULL
;
121 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
122 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
123 if(result
< 0 && (errno
== ECONNRESET
|| errno
== EPIPE
)) {
124 retval
= PUT_SUBBUF_DIED
;
127 else if(result
< 0) {
128 ERR("put_subbuffer: send_message failed");
132 else if(result
== 0) {
133 /* Program seems finished. However this might not be
134 * the last subbuffer that has to be collected.
136 retval
= PUT_SUBBUF_DIED
;
140 result
= sscanf(received_msg
, "%as", &rep_code
);
142 ERR("unable to parse response to put_subbuffer");
147 if(!strcmp(rep_code
, "OK")) {
148 DBG("subbuffer put %s", buf
->name
);
149 retval
= PUT_SUBBUF_OK
;
151 else if(!strcmp(received_msg
, "NOTFOUND")) {
152 DBG("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf
->name
);
153 /* However, maybe this was not the last subbuffer. So
154 * we return the program died.
156 retval
= PUT_SUBBUF_DIED
;
160 DBG("put_subbuffer: received error, we were pushed");
161 retval
= PUT_SUBBUF_PUSHED
;
178 void decrement_active_buffers(void *arg
)
180 struct libustd_instance
*instance
= arg
;
181 pthread_mutex_lock(&instance
->mutex
);
182 instance
->active_buffers
--;
183 pthread_mutex_unlock(&instance
->mutex
);
186 struct buffer_info
*connect_buffer(struct libustd_instance
*instance
, pid_t pid
, const char *bufname
)
188 struct buffer_info
*buf
;
192 struct shmid_ds shmds
;
194 buf
= (struct buffer_info
*) zmalloc(sizeof(struct buffer_info
));
196 ERR("add_buffer: insufficient memory");
200 buf
->conn
= malloc(sizeof(struct ustcomm_connection
));
201 if(buf
->conn
== NULL
) {
202 ERR("add_buffer: insufficient memory");
211 result
= ustcomm_connect_app(buf
->pid
, buf
->conn
);
213 WARN("unable to connect to process, it probably died before we were able to connect");
218 asprintf(&send_msg
, "get_pidunique");
219 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
222 ERR("problem in ustcomm_send_request(get_pidunique)");
229 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
231 ERR("unable to parse response to get_pidunique");
235 DBG("got pidunique %lld", buf
->pidunique
);
238 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
239 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
242 ERR("problem in ustcomm_send_request(get_shmid)");
249 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
251 ERR("unable to parse response to get_shmid (\"%s\")", received_msg
);
255 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
258 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
259 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
262 ERR("problem in ustcomm_send_request(g_n_subbufs)");
269 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
271 ERR("unable to parse response to get_n_subbufs");
275 DBG("got n_subbufs %d", buf
->n_subbufs
);
277 /* get subbuf size */
278 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
279 result
= ustcomm_send_request(buf
->conn
, send_msg
, &received_msg
);
282 ERR("problem in ustcomm_send_request(get_subbuf_size)");
289 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
291 ERR("unable to parse response to get_subbuf_size");
295 DBG("got subbuf_size %d", buf
->subbuf_size
);
298 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
299 if(buf
->mem
== (void *) 0) {
303 DBG("successfully attached buffer memory");
305 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
306 if(buf
->bufstruct_mem
== (void *) 0) {
310 DBG("successfully attached buffer bufstruct memory");
312 /* obtain info on the memory segment */
313 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
318 buf
->memlen
= shmds
.shm_segsz
;
320 if(instance
->callbacks
->on_open_buffer
)
321 instance
->callbacks
->on_open_buffer(instance
->callbacks
, buf
);
323 pthread_mutex_lock(&instance
->mutex
);
324 instance
->active_buffers
++;
325 pthread_mutex_unlock(&instance
->mutex
);
334 static void destroy_buffer(struct libustd_callbacks
*callbacks
,
335 struct buffer_info
*buf
)
339 result
= ustcomm_close_app(buf
->conn
);
341 WARN("problem calling ustcomm_close_app");
344 result
= shmdt(buf
->mem
);
349 result
= shmdt(buf
->bufstruct_mem
);
354 if(callbacks
->on_close_buffer
)
355 callbacks
->on_close_buffer(callbacks
, buf
);
361 int consumer_loop(struct libustd_instance
*instance
, struct buffer_info
*buf
)
365 pthread_cleanup_push(decrement_active_buffers
, instance
);
368 /* get the subbuffer */
369 result
= get_subbuffer(buf
);
371 ERR("error getting subbuffer");
374 else if(result
== GET_SUBBUF_DONE
) {
378 else if(result
== GET_SUBBUF_DIED
) {
379 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
383 if(instance
->callbacks
->on_read_subbuffer
)
384 instance
->callbacks
->on_read_subbuffer(instance
->callbacks
, buf
);
386 /* put the subbuffer */
387 result
= put_subbuffer(buf
);
389 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
392 else if(result
== PUT_SUBBUF_PUSHED
) {
393 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
396 else if(result
== PUT_SUBBUF_DIED
) {
397 DBG("application died while putting subbuffer");
398 /* Skip the first subbuffer. We are not sure it is trustable
399 * because the put_subbuffer() did not complete.
401 if(instance
->callbacks
->on_put_error
)
402 instance
->callbacks
->on_put_error(instance
->callbacks
, buf
);
404 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
407 else if(result
== PUT_SUBBUF_DONE
) {
408 /* Done with this subbuffer */
409 /* FIXME: add a case where this branch is used? Upon
410 * normal trace termination, at put_subbuf time, a
411 * special last-subbuffer code could be returned by
416 else if(result
== PUT_SUBBUF_OK
) {
420 DBG("thread for buffer %s is stopping", buf
->name
);
422 /* FIXME: destroy, unalloc... */
424 pthread_cleanup_pop(1);
429 struct consumer_thread_args
{
432 struct libustd_instance
*instance
;
435 void *consumer_thread(void *arg
)
437 struct buffer_info
*buf
;
438 struct consumer_thread_args
*args
= (struct consumer_thread_args
*) arg
;
442 DBG("GOT ARGS: pid %d bufname %s", args
->pid
, args
->bufname
);
444 if(args
->instance
->callbacks
->on_new_thread
)
445 args
->instance
->callbacks
->on_new_thread(args
->instance
->callbacks
);
447 /* Block signals that should be handled by the main thread. */
448 result
= sigemptyset(&sigset
);
450 PERROR("sigemptyset");
453 result
= sigaddset(&sigset
, SIGTERM
);
458 result
= sigaddset(&sigset
, SIGINT
);
463 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
465 PERROR("sigprocmask");
469 buf
= connect_buffer(args
->instance
, args
->pid
, args
->bufname
);
471 ERR("failed to connect to buffer");
475 consumer_loop(args
->instance
, buf
);
477 destroy_buffer(args
->instance
->callbacks
, buf
);
481 if(args
->instance
->callbacks
->on_close_thread
)
482 args
->instance
->callbacks
->on_close_thread(args
->instance
->callbacks
);
484 free((void *)args
->bufname
);
489 int start_consuming_buffer(
490 struct libustd_instance
*instance
, pid_t pid
, const char *bufname
)
493 struct consumer_thread_args
*args
;
496 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid
, bufname
);
498 args
= (struct consumer_thread_args
*) zmalloc(sizeof(struct consumer_thread_args
));
501 args
->bufname
= strdup(bufname
);
502 args
->instance
= instance
;
503 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
505 result
= pthread_create(&thr
, NULL
, consumer_thread
, args
);
507 ERR("pthread_create failed");
510 result
= pthread_detach(thr
);
512 ERR("pthread_detach failed");
515 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
520 int libustd_start_instance(struct libustd_instance
*instance
)
525 if(!instance
->is_init
) {
526 ERR("libustd instance not initialized");
534 /* check for requests on our public socket */
535 result
= ustcomm_ustd_recv_message(instance
->comm
, &recvbuf
, NULL
, timeout
);
536 if(result
== -1 && errno
== EINTR
) {
539 else if(result
== -1) {
540 ERR("error in ustcomm_ustd_recv_message");
543 else if(result
> 0) {
544 if(!strncmp(recvbuf
, "collect", 7)) {
549 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
551 ERR("parsing error: %s", recvbuf
);
555 result
= start_consuming_buffer(instance
, pid
, bufname
);
557 ERR("error in add_buffer");
564 else if(!strncmp(recvbuf
, "exit", 4)) {
565 /* Only there to force poll to return */
568 WARN("unknown command: %s", recvbuf
);
576 if(instance
->quit_program
) {
577 pthread_mutex_lock(&instance
->mutex
);
578 if(instance
->active_buffers
== 0) {
579 pthread_mutex_unlock(&instance
->mutex
);
582 pthread_mutex_unlock(&instance
->mutex
);
587 if(instance
->callbacks
->on_trace_end
)
588 instance
->callbacks
->on_trace_end(instance
);
590 libustd_delete_instance(instance
);
595 void libustd_delete_instance(struct libustd_instance
*instance
)
597 if(instance
->is_init
)
598 ustcomm_fini_ustd(instance
->comm
);
600 pthread_mutex_destroy(&instance
->mutex
);
601 free(instance
->sock_path
);
602 free(instance
->comm
);
606 int libustd_stop_instance(struct libustd_instance
*instance
, int send_msg
)
614 instance
->quit_program
= 1;
619 /* Send a message through the socket to force poll to return */
621 struct sockaddr_un addr
;
623 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
629 addr
.sun_family
= AF_UNIX
;
631 strncpy(addr
.sun_path
, instance
->sock_path
, UNIX_PATH_MAX
);
632 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
634 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
639 while(bytes
!= sizeof(msg
))
640 bytes
+= send(fd
, msg
, sizeof(msg
), 0);
647 struct libustd_instance
*libustd_new_instance(
648 struct libustd_callbacks
*callbacks
, char *sock_path
)
650 struct libustd_instance
*instance
=
651 zmalloc(sizeof(struct libustd_instance
));
655 instance
->comm
= malloc(sizeof(struct ustcomm_ustd
));
656 if(!instance
->comm
) {
661 instance
->callbacks
= callbacks
;
662 instance
->quit_program
= 0;
663 instance
->is_init
= 0;
664 instance
->active_buffers
= 0;
665 pthread_mutex_init(&instance
->mutex
, NULL
);
668 instance
->sock_path
= strdup(sock_path
);
670 instance
->sock_path
= NULL
;
675 int libustd_init_instance(struct libustd_instance
*instance
)
678 result
= ustcomm_init_ustd(instance
->comm
, instance
->sock_path
);
680 ERR("failed to initialize socket");
683 instance
->is_init
= 1;
This page took 0.05143 seconds and 5 git commands to generate.