From 811e4b93ba6acb72226a9243ae9a525f444e7e80 Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Wed, 25 Feb 2009 13:25:56 -0500 Subject: [PATCH] ust: add info exchange between app and ustd --- libtracectl/tracectl.c | 112 ++++++++++++++++++++++++++++++++++++++++- libustcomm/ustcomm.c | 96 +++++++++++++++++++++++------------ libustcomm/ustcomm.h | 13 ++--- ustd/ustd.c | 30 +++++++++-- 4 files changed, 207 insertions(+), 44 deletions(-) diff --git a/libtracectl/tracectl.c b/libtracectl/tracectl.c index e926c9e..176ee6a 100644 --- a/libtracectl/tracectl.c +++ b/libtracectl/tracectl.c @@ -318,9 +318,16 @@ int listener_main(void *p) struct ltt_trace_struct *trace; char trace_name[] = "auto"; int i; + char *channel_name; DBG("get_shmid"); + channel_name = nth_token(recvbuf, 1); + if(channel_name == NULL) { + ERR("get_shmid: cannot parse channel"); + goto next_cmd; + } + ltt_lock_traces(); trace = _ltt_trace_find(trace_name); ltt_unlock_traces(); @@ -334,8 +341,110 @@ int listener_main(void *p) struct rchan *rchan = trace->channels[i].trans_channel_data; struct rchan_buf *rbuf = rchan->buf; - DBG("the shmid is %d", rbuf->shmid); + if(!strcmp(trace->channels[i].channel_name, channel_name)) { + char *reply; + + DBG("the shmid for the requested channel is %d", rbuf->shmid); + asprintf(&reply, "%d", rbuf->shmid); + + result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); + if(result) { + ERR("listener: get_shmid: ustcomm_send_reply failed"); + goto next_cmd; + } + + free(reply); + + break; + } + } + } + else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) { + struct ltt_trace_struct *trace; + char trace_name[] = "auto"; + int i; + char *channel_name; + + DBG("get_n_subbufs"); + + channel_name = nth_token(recvbuf, 1); + if(channel_name == NULL) { + ERR("get_n_subbufs: cannot parse channel"); + goto next_cmd; + } + + ltt_lock_traces(); + trace = _ltt_trace_find(trace_name); + ltt_unlock_traces(); + + if(trace == NULL) { + CPRINTF("cannot find trace!"); + return 1; + } + + for(i=0; inr_channels; i++) { + struct rchan *rchan = trace->channels[i].trans_channel_data; + + if(!strcmp(trace->channels[i].channel_name, channel_name)) { + char *reply; + + DBG("the n_subbufs for the requested channel is %d", rchan->n_subbufs); + asprintf(&reply, "%d", rchan->n_subbufs); + + result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); + if(result) { + ERR("listener: get_n_subbufs: ustcomm_send_reply failed"); + goto next_cmd; + } + + free(reply); + + break; + } + } + } + else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) { + struct ltt_trace_struct *trace; + char trace_name[] = "auto"; + int i; + char *channel_name; + + DBG("get_subbuf_size"); + + channel_name = nth_token(recvbuf, 1); + if(channel_name == NULL) { + ERR("get_subbuf_size: cannot parse channel"); + goto next_cmd; + } + + ltt_lock_traces(); + trace = _ltt_trace_find(trace_name); + ltt_unlock_traces(); + + if(trace == NULL) { + CPRINTF("cannot find trace!"); + return 1; + } + + for(i=0; inr_channels; i++) { + struct rchan *rchan = trace->channels[i].trans_channel_data; + + if(!strcmp(trace->channels[i].channel_name, channel_name)) { + char *reply; + + DBG("the subbuf_size for the requested channel is %d", rchan->subbuf_size); + asprintf(&reply, "%d", rchan->subbuf_size); + + result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); + if(result) { + ERR("listener: get_subbuf_size: ustcomm_send_reply failed"); + goto next_cmd; + } + + free(reply); + break; + } } } else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) { @@ -346,6 +455,7 @@ int listener_main(void *p) DBG("load_probe_lib loading %s", libfile); } + next_cmd: free(recvbuf); } } diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 2773b1a..e445ed9 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -48,6 +48,31 @@ static void signal_process(pid_t pid) sleep(1); } +int send_message_fd(int fd, const char *msg, char **reply) +{ + int result; + + result = send(fd, msg, strlen(msg), 0); + if(result == -1) { + PERROR("send"); + return -1; + } + + if(!reply) + return 0; + + *reply = (char *) malloc(MSG_MAX+1); + result = recv(fd, *reply, MSG_MAX, 0); + if(result == -1) { + PERROR("recv"); + return -1; + } + + (*reply)[result] = '\0'; + + return 0; +} + int send_message_path(const char *path, const char *msg, char **reply, int signalpid) { int fd; @@ -77,25 +102,7 @@ int send_message_path(const char *path, const char *msg, char **reply, int signa return -1; } - result = send(fd, msg, strlen(msg), 0); - if(result == -1) { - PERROR("send"); - return -1; - } - - if(!reply) - return 0; - - *reply = (char *) malloc(MSG_MAX+1); - result = recvfrom(fd, *reply, MSG_MAX, 0, NULL, NULL); - if(result == -1) { - PERROR("recvfrom"); - return -1; - } - - (*reply)[result] = '\0'; - - return 0; + return send_message_fd(fd, msg, reply); } /* pid: the pid of the trace process that must receive the msg @@ -142,6 +149,8 @@ int ustcomm_request_consumer(pid_t pid, const char *channel) return 0; } + + static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src) { int result; @@ -158,10 +167,26 @@ static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src) DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg)); + if(src) + src->fd = fd; + return 0; } -int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src) +int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src) +{ + int result; + + result = send_message_fd(src->fd, msg, NULL); + if(result) { + ERR("error in send_message_fd"); + return -1; + } + + return 0; +} + +int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src) { struct pollfd *fds; struct ustcomm_connection *conn; @@ -172,7 +197,7 @@ int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustc int idx = 0; int n_fds = 1; - list_for_each_entry(conn, &ustd->connections, list) { + list_for_each_entry(conn, &server->connections, list) { n_fds++; } @@ -183,11 +208,11 @@ int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustc } /* special idx 0 is for listening socket */ - fds[idx].fd = ustd->listen_fd; + fds[idx].fd = server->listen_fd; fds[idx].events = POLLIN; idx++; - list_for_each_entry(conn, &ustd->connections, list) { + list_for_each_entry(conn, &server->connections, list) { fds[idx].fd = conn->fd; fds[idx].events = POLLIN; idx++; @@ -203,7 +228,7 @@ int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustc struct ustcomm_connection *newconn; int newfd; - result = newfd = accept(ustd->listen_fd, NULL, NULL); + result = newfd = accept(server->listen_fd, NULL, NULL); if(result == -1) { PERROR("accept"); return -1; @@ -217,7 +242,7 @@ int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustc newconn->fd = newfd; - list_add(&newconn->list, &ustd->connections); + list_add(&newconn->list, &server->connections); } for(idx=1; idxconnections, list) { + list_for_each_entry(conn, &server->connections, list) { if(conn->fd == fds[idx].fd) { list_del(&conn->list); break; @@ -248,9 +273,14 @@ free_fds_return: return retval; } +int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src) +{ + return ustcomm_recv_message(&ustd->server, msg, src); +} + int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src) { - return ustcomm_ustd_recv_message((struct ustcomm_ustd *)app, msg, src); + return ustcomm_recv_message(&app->server, msg, src); } static int init_named_socket(char *name, char **path_out) @@ -318,14 +348,14 @@ int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle) return -1; } - handle->listen_fd = init_named_socket(name, &(handle->socketpath)); - if(handle->listen_fd < 0) { + handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath)); + if(handle->server.listen_fd < 0) { ERR("error initializing named socket"); goto free_name; } free(name); - INIT_LIST_HEAD(&handle->connections); + INIT_LIST_HEAD(&handle->server.connections); return 0; @@ -345,14 +375,14 @@ int ustcomm_init_ustd(struct ustcomm_ustd *handle) return -1; } - handle->listen_fd = init_named_socket(name, &handle->socketpath); - if(handle->listen_fd < 0) { + handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath); + if(handle->server.listen_fd < 0) { ERR("error initializing named socket"); goto free_name; } free(name); - INIT_LIST_HEAD(&handle->connections); + INIT_LIST_HEAD(&handle->server.connections); return 0; diff --git a/libustcomm/ustcomm.h b/libustcomm/ustcomm.h index 7d84592..91ac80d 100644 --- a/libustcomm/ustcomm.h +++ b/libustcomm/ustcomm.h @@ -11,7 +11,7 @@ struct ustcomm_connection { int fd; }; -struct ustcomm_app { +struct ustcomm_server { /* the "server" socket for serving the external requests */ int listen_fd; char *socketpath; @@ -20,15 +20,16 @@ struct ustcomm_app { }; struct ustcomm_ustd { - /* the "server" socket for serving the external requests */ - int listen_fd; - char *socketpath; + struct ustcomm_server server; +}; - struct list_head connections; +struct ustcomm_app { + struct ustcomm_server server; }; struct ustcomm_source { - struct sockaddr_un addr; + int fd; + void *priv; }; int send_message(pid_t pid, const char *msg, char **reply); diff --git a/ustd/ustd.c b/ustd/ustd.c index 82b68c2..66b9dd3 100644 --- a/ustd/ustd.c +++ b/ustd/ustd.c @@ -10,6 +10,8 @@ #include "localerr.h" #include "ustcomm.h" +struct list_head buffers = LIST_HEAD_INIT(buffers); + struct buffer_info { char *name; pid_t pid; @@ -18,7 +20,10 @@ struct buffer_info { void *mem; int memlen; - int nsubbufs; + int n_subbufs; + int subbuf_size; + + int file_fd; /* output file */ }; int add_buffer(pid_t pid, char *bufname) @@ -41,6 +46,7 @@ int add_buffer(pid_t pid, char *bufname) asprintf(&send_msg, "get_shmid %s", buf->name); send_message(pid, send_msg, &received_msg); free(send_msg); + DBG("got buffer name %s", buf->name); result = sscanf(received_msg, "%d", &buf->shmid); if(result != 1) { @@ -48,18 +54,33 @@ int add_buffer(pid_t pid, char *bufname) return -1; } free(received_msg); + DBG("got shmid %d", buf->shmid); - /* get nsubbufs */ + /* get n_subbufs */ asprintf(&send_msg, "get_n_subbufs %s", buf->name); send_message(pid, send_msg, &received_msg); free(send_msg); - result = sscanf(received_msg, "%d", &buf->nsubbufs); + result = sscanf(received_msg, "%d", &buf->n_subbufs); if(result != 1) { - ERR("unable to parse response to get_shmid"); + ERR("unable to parse response to get_n_subbufs"); + return -1; + } + free(received_msg); + DBG("got n_subbufs %d", buf->n_subbufs); + + /* get subbuf size */ + asprintf(&send_msg, "get_subbuf_size %s", buf->name); + send_message(pid, send_msg, &received_msg); + free(send_msg); + + result = sscanf(received_msg, "%d", &buf->subbuf_size); + if(result != 1) { + ERR("unable to parse response to get_subbuf_size"); return -1; } free(received_msg); + DBG("got subbuf_size %d", buf->subbuf_size); /* attach memory */ buf->mem = shmat(buf->shmid, NULL, 0); @@ -67,6 +88,7 @@ int add_buffer(pid_t pid, char *bufname) perror("shmat"); return -1; } + DBG("successfully attached memory"); return 0; } -- 2.34.1