2 * Copyright (C) 2009 Pierre-Marc Fournier
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
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
50 int test_sigpipe(void)
55 result
= sigemptyset(&sigset
);
57 perror("sigemptyset");
60 result
= sigaddset(&sigset
, SIGPIPE
);
66 result
= sigtimedwait(&sigset
, NULL
, &(struct timespec
){0,0});
67 if(result
== -1 && errno
== EAGAIN
) {
68 /* no signal received */
71 else if(result
== -1) {
72 perror("sigtimedwait");
75 else if(result
== SIGPIPE
) {
76 /* received sigpipe */
84 int get_subbuffer(struct buffer_info
*buf
)
92 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
93 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
96 WARN("process %d destroyed before we could connect to it", buf
->pid
);
97 return GET_SUBBUF_DONE
;
100 ERR("get_subbuffer: ustcomm_send_request failed");
103 else if(result
== 0) {
104 DBG("app died while being traced");
105 return GET_SUBBUF_DIED
;
108 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
109 if(result
!= 2 && result
!= 1) {
110 ERR("unable to parse response to get_subbuffer");
114 DBG("received msg is %s", received_msg
);
116 if(!strcmp(rep_code
, "OK")) {
117 DBG("got subbuffer %s", buf
->name
);
118 retval
= GET_SUBBUF_OK
;
120 else if(nth_token_is(received_msg
, "END", 0) == 1) {
121 return GET_SUBBUF_DONE
;
124 DBG("error getting subbuffer %s", buf
->name
);
128 /* FIMXE: free correctly the stuff */
134 int put_subbuffer(struct buffer_info
*buf
)
142 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
143 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
145 ERR("put_subbuffer: send_message failed");
150 result
= sscanf(received_msg
, "%as", &rep_code
);
152 ERR("unable to parse response to put_subbuffer");
157 if(!strcmp(rep_code
, "OK")) {
158 DBG("subbuffer put %s", buf
->name
);
159 retval
= PUT_SUBBUF_OK
;
162 DBG("put_subbuffer: received error, we were pushed");
163 return PUT_SUBBUF_PUSHED
;
170 /* This write is patient because it restarts if it was incomplete.
173 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
175 const char *bufc
= (const char *) buf
;
179 result
= write(fd
, bufc
, count
);
191 return bufc
-(const char *)buf
;
194 void *consumer_thread(void *arg
)
196 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
200 /* get the subbuffer */
201 result
= get_subbuffer(buf
);
203 ERR("error getting subbuffer");
206 else if(result
== GET_SUBBUF_DONE
) {
210 else if(result
== GET_SUBBUF_DIED
) {
211 finish_consuming_dead_subbuffer(buf
);
215 /* write data to file */
216 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
219 /* FIXME: maybe drop this trace */
222 /* put the subbuffer */
223 result
= put_subbuffer(buf
);
225 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
228 else if(result
== PUT_SUBBUF_PUSHED
) {
229 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
232 else if(result
== PUT_SUBBUF_DIED
) {
233 WARN("application died while putting subbuffer");
234 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
235 finish_consuming_dead_subbuffer(buf
);
237 else if(result
== PUT_SUBBUF_OK
) {
241 DBG("thread for buffer %s is stopping", buf
->name
);
243 /* FIXME: destroy, unalloc... */
248 int add_buffer(pid_t pid
, char *bufname
)
250 struct buffer_info
*buf
;
257 struct shmid_ds shmds
;
259 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
261 ERR("add_buffer: insufficient memory");
269 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
271 WARN("unable to connect to process, it probably died before we were able to connect");
276 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
277 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
280 ERR("problem in ustcomm_send_request(get_shmid)");
284 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
286 ERR("unable to parse response to get_shmid");
290 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
293 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
294 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
297 ERR("problem in ustcomm_send_request(g_n_subbufs)");
301 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
303 ERR("unable to parse response to get_n_subbufs");
307 DBG("got n_subbufs %d", buf
->n_subbufs
);
309 /* get subbuf size */
310 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
311 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
314 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
316 ERR("unable to parse response to get_subbuf_size");
320 DBG("got subbuf_size %d", buf
->subbuf_size
);
323 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
324 if(buf
->mem
== (void *) 0) {
328 DBG("successfully attached buffer memory");
330 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
331 if(buf
->bufstruct_mem
== (void *) 0) {
335 DBG("successfully attached buffer bufstruct memory");
337 /* obtain info on the memory segment */
338 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
343 buf
->memlen
= shmds
.shm_segsz
;
345 /* open file for output */
346 asprintf(&tmp
, "/tmp/trace/%s_0", buf
->name
);
347 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
350 ERR("failed opening trace file %s", tmp
);
356 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
361 int main(int argc
, char **argv
)
363 struct ustcomm_ustd ustd
;
367 result
= ustcomm_init_ustd(&ustd
);
369 ERR("failed to initialize socket");
373 result
= sigemptyset(&sigset
);
375 perror("sigemptyset");
378 result
= sigaddset(&sigset
, SIGPIPE
);
383 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
385 perror("sigprocmask");
393 /* check for requests on our public socket */
394 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
396 ERR("error in ustcomm_ustd_recv_message");
400 if(!strncmp(recvbuf
, "collect", 7)) {
405 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
407 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
410 result
= add_buffer(pid
, bufname
);
412 ERR("error in add_buffer");
This page took 0.057377 seconds and 4 git commands to generate.