3 * Deal with mrgdb (bleah!)
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
13 #include "mr_private.h"
15 #include <sys/types.h>
16 #include <netinet/in.h>
24 * GDB operations to send and recieve RPC requests and replies.
28 * This doesn't get called until after the actual buffered write completes.
29 * In a non-preflattening version of this, this would then queue the
30 * write of the next bunch of data.
33 int mr_cont_send(OPERATION op, HALF_CONNECTION hcon, struct mr_params *arg)
35 op->result = OP_SUCCESS;
36 free(arg->mr_flattened);
37 arg->mr_flattened = NULL;
42 int mr_start_send(OPERATION op, HALF_CONNECTION hcon,
43 struct mr_params *arg)
51 * This should probably be split into several routines.
52 * It could also probably be made more efficient (punting most
53 * of the argument marshalling stuff) by doing I/O directly
54 * from the strings. Anyone for a scatter/gather mr_send_data?
56 * that would look a lot like the uio stuff in the kernel.. hmm.
60 * Marshall the entire data right now..
61 * We are sending the version number,
62 * total request size, request number,
63 * argument count, and then each argument.
64 * At least for now, each argument is a string, which is
65 * sent as a count of bytes followed by the bytes
66 * (including the trailing '\0'), padded
67 * to a 32-bit boundary.
70 mr_size = 4 * sizeof(int32);
72 argl = malloc(sizeof(int) * arg->mr_argc);
75 * For each argument, figure out how much space is needed.
78 for (i = 0; i < arg->mr_argc; ++i)
81 argl[i] = len = arg->mr_argl[i];
83 argl[i] = len = strlen(arg->mr_argv[i]) + 1;
84 mr_size += sizeof(int32) + len;
85 /* Round up to next 32-bit boundary.. */
86 mr_size = sizeof(int32) * howmany(mr_size, sizeof(int32));
89 arg->mr_flattened = buf = malloc(mr_size);
91 memset(arg->mr_flattened, 0, mr_size);
93 arg->mr_size = mr_size;
95 ((int32 *)buf)[0] = htonl(mr_size);
96 ((int32 *)buf)[1] = htonl(arg->mr_version_no);
97 ((int32 *)buf)[2] = htonl(arg->mr_procno);
98 ((int32 *)buf)[3] = htonl(arg->mr_argc);
101 * bp is a pointer into the point in the buffer to put
105 bp = (char *)(((int32 *)buf) + 4);
107 for (i = 0; i < arg->mr_argc; ++i)
110 *((int32 *)bp) = htonl(len);
112 memcpy(bp, arg->mr_argv[i], len);
113 bp += sizeof(int32) * howmany(len, sizeof(int32));
115 op->fcn.cont = mr_cont_send;
116 arg->mr_size = mr_size;
120 if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE)
121 return mr_cont_send(op, hcon, arg);
126 int mr_cont_recv(OPERATION op, HALF_CONNECTION hcon, mr_params **argp)
132 mr_params *arg = *argp;
136 switch (arg->mr_state)
139 arg->mr_state = S_RECV_DATA;
140 if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size,
141 sizeof(int32)) == OP_COMPLETE)
147 /* Should validate that length is reasonable */
148 arg->mr_size = ntohl(arg->mr_size);
149 if (arg->mr_size > 65536)
151 arg->mr_flattened = malloc(arg->mr_size);
152 arg->mr_state = S_DECODE_DATA;
153 memcpy(arg->mr_flattened, (caddr_t)&arg->mr_size, sizeof(int32));
155 if (gdb_receive_data(hcon, arg->mr_flattened + sizeof(int32),
156 arg->mr_size - sizeof(int32))
162 cp = arg->mr_flattened;
164 /* we already got the overall length.. */
165 for (i = 1; i < 4; i++)
166 ip[i] = ntohl(ip[i]);
167 arg->mr_version_no = ip[1];
168 if (arg->mr_version_no != MR_VERSION_1 &&
169 arg->mr_version_no != MR_VERSION_2)
170 arg->mr_status = MR_VERSION_MISMATCH;
171 else arg->mr_status = ip[2];
172 arg->mr_argc = ip[3];
173 cp += 4 * sizeof(int);
174 arg->mr_argv = malloc(arg->mr_argc * sizeof(char *));
175 arg->mr_argl = malloc(arg->mr_argc * sizeof(int));
177 for (i = 0; i < arg->mr_argc; ++i)
179 u_short nlen = ntohl(*(int *)cp);
181 if (cp + nlen > arg->mr_flattened + arg->mr_size)
183 free(arg->mr_flattened);
184 arg->mr_flattened = NULL;
187 arg->mr_argv[i] = malloc(nlen);
188 memcpy(arg->mr_argv[i], cp, nlen);
189 arg->mr_argl[i] = nlen;
190 cp += sizeof(int32) * howmany(nlen, sizeof(int32));
192 free(arg->mr_flattened);
193 arg->mr_flattened = NULL;
201 int mr_start_recv(OPERATION op, HALF_CONNECTION hcon, struct mr_params **argp)
203 mr_params *arg = *argp;
206 *argp = arg = malloc(sizeof(mr_params));
209 arg->mr_flattened = NULL;
211 arg->mr_state = S_RECV_START;
212 op->fcn.cont = mr_cont_recv;
213 return mr_cont_recv(op, hcon, argp);
216 void mr_destroy_reply(mr_params *reply)
222 free(reply->mr_argl);
223 reply->mr_argl = NULL;
224 if (reply->mr_flattened)
225 free(reply->mr_flattened);
226 reply->mr_flattened = NULL;
229 for (i = 0; i < reply->mr_argc; i++)
231 if (reply->mr_argv[i])
232 free (reply->mr_argv[i]);
233 reply->mr_argv[i] = NULL;
235 free(reply->mr_argv);
237 reply->mr_argv = NULL;