c5a3d8bed26b10bbfdcbd885efb189209d67b332
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>
34 struct list_head buffers
= LIST_HEAD_INIT(buffers
);
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 int get_subbuffer(struct buffer_info
*buf
)
53 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
54 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
57 ERR("get_subbuffer: ustcomm_send_request failed");
60 else if(result
== 0) {
61 DBG("app died while being traced");
62 return GET_SUBBUF_DIED
;
65 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
66 if(result
!= 2 && result
!= 1) {
67 ERR("unable to parse response to get_subbuffer");
71 DBG("received msg is %s", received_msg
);
73 if(!strcmp(rep_code
, "OK")) {
74 DBG("got subbuffer %s", buf
->name
);
75 retval
= GET_SUBBUF_OK
;
77 else if(nth_token_is(received_msg
, "END", 0) == 1) {
78 return GET_SUBBUF_DONE
;
81 DBG("error getting subbuffer %s", buf
->name
);
85 /* FIMXE: free correctly the stuff */
91 int put_subbuffer(struct buffer_info
*buf
)
99 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
100 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
102 ERR("put_subbuffer: send_message failed");
107 result
= sscanf(received_msg
, "%as", &rep_code
);
109 ERR("unable to parse response to put_subbuffer");
114 if(!strcmp(rep_code
, "OK")) {
115 DBG("subbuffer put %s", buf
->name
);
119 ERR("invalid response to put_subbuffer");
126 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
128 const char *bufc
= (const char *) buf
;
132 result
= write(fd
, bufc
, count
);
144 return bufc
-(const char *)buf
;
147 void *consumer_thread(void *arg
)
149 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
153 /* get the subbuffer */
154 result
= get_subbuffer(buf
);
156 ERR("error getting subbuffer");
159 else if(result
== GET_SUBBUF_DONE
) {
163 else if(result
== GET_SUBBUF_DIED
) {
164 finish_consuming_dead_subbuffer(buf
);
168 /* write data to file */
169 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
172 /* FIXME: maybe drop this trace */
175 /* put the subbuffer */
176 result
= put_subbuffer(buf
);
178 ERR("error putting subbuffer");
183 DBG("thread for buffer %s is stopping", buf
->name
);
185 /* FIXME: destroy, unalloc... */
190 int add_buffer(pid_t pid
, char *bufname
)
192 struct buffer_info
*buf
;
200 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
202 ERR("add_buffer: insufficient memory");
210 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
212 ERR("unable to connect to process");
217 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
218 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
220 DBG("got buffer name %s", buf
->name
);
222 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
224 ERR("unable to parse response to get_shmid");
228 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
231 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
232 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
235 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
237 ERR("unable to parse response to get_n_subbufs");
241 DBG("got n_subbufs %d", buf
->n_subbufs
);
243 /* get subbuf size */
244 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
245 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
248 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
250 ERR("unable to parse response to get_subbuf_size");
254 DBG("got subbuf_size %d", buf
->subbuf_size
);
257 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
258 if(buf
->mem
== (void *) 0) {
262 DBG("successfully attached buffer memory");
264 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
265 if(buf
->bufstruct_mem
== (void *) 0) {
269 DBG("successfully attached buffer bufstruct memory");
271 /* open file for output */
272 asprintf(&tmp
, "/tmp/trace/%s_0", buf
->name
);
273 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
276 ERR("failed opening trace file %s", tmp
);
282 //list_add(&buf->list, &buffers);
284 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
289 int main(int argc
, char **argv
)
291 struct ustcomm_ustd ustd
;
294 result
= ustcomm_init_ustd(&ustd
);
296 ERR("failed to initialize socket");
304 /* check for requests on our public socket */
305 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
307 ERR("error in ustcomm_ustd_recv_message");
311 if(!strncmp(recvbuf
, "collect", 7)) {
316 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
318 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
321 result
= add_buffer(pid
, bufname
);
323 ERR("error in add_buffer");
This page took 0.035098 seconds and 3 git commands to generate.