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
;
193 struct shmid_ds shmds
;
195 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
197 ERR("add_buffer: insufficient memory");
205 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
207 WARN("unable to connect to process, it probably died before we were able to connect");
212 asprintf(&send_msg
, "get_pidunique");
213 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
216 ERR("problem in ustcomm_send_request(get_pidunique)");
223 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
225 ERR("unable to parse response to get_pidunique");
229 DBG("got pidunique %lld", buf
->pidunique
);
232 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
233 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
236 ERR("problem in ustcomm_send_request(get_shmid)");
243 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
245 ERR("unable to parse response to get_shmid (\"%s\")", received_msg
);
249 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
252 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
253 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
256 ERR("problem in ustcomm_send_request(g_n_subbufs)");
263 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
265 ERR("unable to parse response to get_n_subbufs");
269 DBG("got n_subbufs %d", buf
->n_subbufs
);
271 /* get subbuf size */
272 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
273 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
276 ERR("problem in ustcomm_send_request(get_subbuf_size)");
283 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
285 ERR("unable to parse response to get_subbuf_size");
289 DBG("got subbuf_size %d", buf
->subbuf_size
);
292 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
293 if(buf
->mem
== (void *) 0) {
297 DBG("successfully attached buffer memory");
299 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
300 if(buf
->bufstruct_mem
== (void *) 0) {
304 DBG("successfully attached buffer bufstruct memory");
306 /* obtain info on the memory segment */
307 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
312 buf
->memlen
= shmds
.shm_segsz
;
314 if(instance
->callbacks
->on_open_buffer
)
315 instance
->callbacks
->on_open_buffer(instance
->callbacks
, buf
);
317 pthread_mutex_lock(&instance
->mutex
);
318 instance
->active_buffers
++;
319 pthread_mutex_unlock(&instance
->mutex
);
328 static void destroy_buffer(struct libustd_callbacks
*callbacks
,
329 struct buffer_info
*buf
)
333 result
= ustcomm_close_app(&buf
->conn
);
335 WARN("problem calling ustcomm_close_app");
338 result
= shmdt(buf
->mem
);
343 result
= shmdt(buf
->bufstruct_mem
);
348 if(callbacks
->on_close_buffer
)
349 callbacks
->on_close_buffer(callbacks
, buf
);
354 int consumer_loop(struct libustd_instance
*instance
, struct buffer_info
*buf
)
358 pthread_cleanup_push(decrement_active_buffers
, instance
);
361 /* get the subbuffer */
362 result
= get_subbuffer(buf
);
364 ERR("error getting subbuffer");
367 else if(result
== GET_SUBBUF_DONE
) {
371 else if(result
== GET_SUBBUF_DIED
) {
372 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
376 if(instance
->callbacks
->on_read_subbuffer
)
377 instance
->callbacks
->on_read_subbuffer(instance
->callbacks
, buf
);
379 /* put the subbuffer */
380 result
= put_subbuffer(buf
);
382 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
385 else if(result
== PUT_SUBBUF_PUSHED
) {
386 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
389 else if(result
== PUT_SUBBUF_DIED
) {
390 DBG("application died while putting subbuffer");
391 /* Skip the first subbuffer. We are not sure it is trustable
392 * because the put_subbuffer() did not complete.
394 if(instance
->callbacks
->on_put_error
)
395 instance
->callbacks
->on_put_error(instance
->callbacks
, buf
);
397 finish_consuming_dead_subbuffer(instance
->callbacks
, buf
);
400 else if(result
== PUT_SUBBUF_DONE
) {
401 /* Done with this subbuffer */
402 /* FIXME: add a case where this branch is used? Upon
403 * normal trace termination, at put_subbuf time, a
404 * special last-subbuffer code could be returned by
409 else if(result
== PUT_SUBBUF_OK
) {
413 DBG("thread for buffer %s is stopping", buf
->name
);
415 /* FIXME: destroy, unalloc... */
417 pthread_cleanup_pop(1);
422 struct consumer_thread_args
{
425 struct libustd_instance
*instance
;
428 void *consumer_thread(void *arg
)
430 struct buffer_info
*buf
;
431 struct consumer_thread_args
*args
= (struct consumer_thread_args
*) arg
;
435 DBG("GOT ARGS: pid %d bufname %s", args
->pid
, args
->bufname
);
437 if(args
->instance
->callbacks
->on_new_thread
)
438 args
->instance
->callbacks
->on_new_thread(args
->instance
->callbacks
);
440 /* Block signals that should be handled by the main thread. */
441 result
= sigemptyset(&sigset
);
443 PERROR("sigemptyset");
446 result
= sigaddset(&sigset
, SIGTERM
);
451 result
= sigaddset(&sigset
, SIGINT
);
456 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
458 PERROR("sigprocmask");
462 buf
= connect_buffer(args
->instance
, args
->pid
, args
->bufname
);
464 ERR("failed to connect to buffer");
468 consumer_loop(args
->instance
, buf
);
470 destroy_buffer(args
->instance
->callbacks
, buf
);
474 if(args
->instance
->callbacks
->on_close_thread
)
475 args
->instance
->callbacks
->on_close_thread(args
->instance
->callbacks
);
477 free((void *)args
->bufname
);
482 int start_consuming_buffer(
483 struct libustd_instance
*instance
, pid_t pid
, const char *bufname
)
486 struct consumer_thread_args
*args
;
489 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid
, bufname
);
491 args
= (struct consumer_thread_args
*) malloc(sizeof(struct consumer_thread_args
));
494 args
->bufname
= strdup(bufname
);
495 args
->instance
= instance
;
496 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
498 result
= pthread_create(&thr
, NULL
, consumer_thread
, args
);
500 ERR("pthread_create failed");
503 result
= pthread_detach(thr
);
505 ERR("pthread_detach failed");
508 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args
->pid
, args
->bufname
);
513 int libustd_start_instance(struct libustd_instance
*instance
)
518 if(!instance
->is_init
) {
519 ERR("libustd instance not initialized");
527 /* check for requests on our public socket */
528 result
= ustcomm_ustd_recv_message(&instance
->comm
, &recvbuf
, NULL
, timeout
);
529 if(result
== -1 && errno
== EINTR
) {
532 else if(result
== -1) {
533 ERR("error in ustcomm_ustd_recv_message");
536 else if(result
> 0) {
537 if(!strncmp(recvbuf
, "collect", 7)) {
542 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
544 ERR("parsing error: %s", recvbuf
);
548 result
= start_consuming_buffer(instance
, pid
, bufname
);
550 ERR("error in add_buffer");
557 else if(!strncmp(recvbuf
, "exit", 4)) {
558 /* Only there to force poll to return */
561 WARN("unknown command: %s", recvbuf
);
569 if(instance
->quit_program
) {
570 pthread_mutex_lock(&instance
->mutex
);
571 if(instance
->active_buffers
== 0) {
572 pthread_mutex_unlock(&instance
->mutex
);
575 pthread_mutex_unlock(&instance
->mutex
);
580 if(instance
->callbacks
->on_trace_end
)
581 instance
->callbacks
->on_trace_end(instance
);
583 libustd_delete_instance(instance
);
588 void libustd_delete_instance(struct libustd_instance
*instance
)
590 if(instance
->is_init
)
591 ustcomm_fini_ustd(&instance
->comm
);
593 pthread_mutex_destroy(&instance
->mutex
);
594 free(instance
->sock_path
);
598 int libustd_stop_instance(struct libustd_instance
*instance
, int send_msg
)
606 instance
->quit_program
= 1;
611 /* Send a message through the socket to force poll to return */
613 struct sockaddr_un addr
;
615 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
621 addr
.sun_family
= AF_UNIX
;
623 strncpy(addr
.sun_path
, instance
->sock_path
, UNIX_PATH_MAX
);
624 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
626 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
631 while(bytes
!= sizeof(msg
))
632 bytes
+= send(fd
, msg
, sizeof(msg
), 0);
639 struct libustd_instance
*libustd_new_instance(
640 struct libustd_callbacks
*callbacks
, char *sock_path
)
642 struct libustd_instance
*instance
=
643 malloc(sizeof(struct libustd_instance
));
647 instance
->callbacks
= callbacks
;
648 instance
->quit_program
= 0;
649 instance
->is_init
= 0;
650 instance
->active_buffers
= 0;
651 pthread_mutex_init(&instance
->mutex
, NULL
);
654 instance
->sock_path
= strdup(sock_path
);
656 instance
->sock_path
= NULL
;
661 int libustd_init_instance(struct libustd_instance
*instance
)
664 result
= ustcomm_init_ustd(&instance
->comm
, instance
->sock_path
);
666 ERR("failed to initialize socket");
669 instance
->is_init
= 1;
This page took 0.065232 seconds and 4 git commands to generate.