7 static char *rcsid_gdb_ops_c = "$Header$";
30 /************************************************************************/
34 /* GDB - Asynchronous Operations and Their Synchronous
37 /* Author: Noah Mendelsohn
38 /* Copyright: 1986 MIT Project Athena
39 /* For copying and distribution information, please see
40 /* the file <mit-copyright.h>.
42 /* These routines provide a suite of asynchronous operations
45 /************************************************************************/
47 #include <mit-copyright.h>
50 #include <netinet/in.h>
51 #include <sys/ioctl.h>
53 extern u_long htonl();
56 /************************************************************************/
58 /* send_object (send_object)
60 /* Synchronous form of start_sending_object. Returns either
61 /* OP_CANCELLED, or OP_RESULT(op).
63 /************************************************************************/
66 send_object(con, objp, type)
71 register OPERATION op;
75 op = create_operation();
76 start_sending_object(op, con, objp, type);
77 (void) complete_operation(op);
78 if (OP_STATUS(op) == OP_COMPLETE)
79 retval = OP_RESULT(op);
81 retval = OP_STATUS(op);
86 /************************************************************************/
88 /* start_send_object (g_snobj)
90 /* Start the asynchronous transmission of a gdb object.
91 /* Note that this routine must be passed the address of the object,
92 /* not the object itself.
94 /* The following three routines work together, and may be considered
95 /* as a single entity implementing the operation. The first merely
96 /* saves away its arguments and queues the operation on the designated
97 /* connection. These stay there until they percolate to the head of
98 /* the queue. The second is the initialization routine, which is
99 /* called by the connection maintenance logic when the operation
100 /* first reaches the head of the queue. This routine encodes
101 /* the supplied data for transmission, and then sends it. If the
102 /* transmission executes synchronously, then the third routine is
103 /* called immediately to clean up. If not, the third routine is
104 /* marked as the 'continuation' routine, which will cause its
105 /* invocation when the transmission completes.
107 /* The data is preceded by its length expressed as a long in
108 /* network byte order.
110 /************************************************************************/
113 char *objp; /* address of the object to */
115 int type; /* type code for the object */
117 char *flattened; /* address of first byte */
118 /* of flattened data */
119 int len; /* length of the flattened */
127 start_sending_object(op, con, objp, type)
133 struct obj_data *arg;
136 * Make sure the supplied connection is a legal one
138 GDB_CHECK_CON(con, "start_sending_object")
139 GDB_CHECK_OP(op, "start_sending_object")
141 arg = (struct obj_data *)db_alloc(sizeof(struct obj_data));
145 initialize_operation(op, g_isnobj, (char *)arg, (int (*)())NULL);
146 (void) queue_operation(con, CON_OUTPUT, op);
149 /*----------------------------------------------------------*/
153 /* Init routine for sending an object. This routine is
154 /* called by the connection management logic when the send
155 /* request percolates to the top of the queue. This routine
156 /* reformats the data into an appropriate form for transmission.
157 /* The format used is a length, represented as a long in
158 /* network byte order, followed by the data itself. The
159 /* continuation routine below is called, either synchronously
160 /* or asynchronously, once the transmission is complete.
162 /*----------------------------------------------------------*/
165 g_isnobj(op, hcon, arg)
167 HALF_CONNECTION hcon;
168 struct obj_data *arg;
171 * Find out the encoded length of the data
173 arg->len = FCN_PROPERTY(arg->type, CODED_LENGTH_PROPERTY)
177 * Allocate space and flatten (encode) the data
179 arg->flattened = db_alloc(arg->len+sizeof(long));
180 *(u_long *)arg->flattened = htonl((u_long)arg->len);
182 FCN_PROPERTY(arg->type, ENCODE_PROPERTY)
183 (arg->objp, hcon, arg->flattened+sizeof(long));
186 * Set up continuation routine in case it's needed after the return
188 op->fcn.cont = g_csnobj;
191 * Start sending the data, maybe even complete
193 if (gdb_send_data(hcon, arg->flattened, arg->len + sizeof(long)) ==
195 return g_csnobj(op, hcon, arg) ;/* this return is a little */
196 /* subtle. As continuation */
197 /* routines call each other */
198 /* synchronously, the last */
199 /* one determines whether we */
200 /* completed or are still */
201 /* running. That status */
202 /* percolates back through */
203 /* the entire call chain. */
214 /*----------------------------------------------------------*/
218 /* Continuation routine for sending an object. Since there is
219 /* only one transmission, started by the init routine, this is
220 /* called when that transmission is done, and it does all the
221 /* associated clean up.
223 /*----------------------------------------------------------*/
226 g_csnobj(op, hcon, arg)
228 HALF_CONNECTION hcon;
229 struct obj_data *arg;
231 op->result = OP_SUCCESS;
232 db_free((char *)arg->flattened, arg->len + sizeof(long));
233 /* free the sent data */
234 db_free((char *)arg, sizeof(struct obj_data)); /* free the state structure */
239 /************************************************************************/
241 /* receive_object (receive_object)
243 /* Synchronous form of start_receiving_object. Returns either
244 /* OP_CANCELLED, or OP_RESULT(op).
246 /************************************************************************/
249 receive_object(con, objp, type)
254 register OPERATION op;
257 op = create_operation();
258 start_receiving_object(op, con, objp, type);
259 (void) complete_operation(op);
260 if (OP_STATUS(op) == OP_COMPLETE)
261 retval = OP_RESULT(op);
263 retval = OP_STATUS(op);
264 delete_operation(op);
268 /************************************************************************/
270 /* start_receiving_object (g_rcobj)
272 /* Start the asynchronous receipt of a gdb object. Note that this
273 /* routine must be passed the address of the object, not the object
274 /* itself. In the case of structured objects, this routine may
275 /* allocate the necessary storage. The work to build the object is
276 /* done by the object's decode routine.
278 /* The following three routines work together, and may be considered
279 /* as a single entity implementing the operation. The first merely
280 /* saves away its arguments and queues the operation on the designated
281 /* connection. These stay there until they percolate to the head of
282 /* the queue. The second is the initialization routine, which is
283 /* called by the connection maintenance logic when the operation
284 /* first reaches the head of the queue. This routine initiates a read
285 /* for the length of the object, and sets up a continuation routine
286 /* to read the object itself. When the object itself has been read, it
287 /* is decoded and the operation completes.
289 /* The data is preceded by its length expressed as a long in
290 /* network byte order.
292 /* preempt_and_start_receiving_object (g_prcobj)
294 /* Similar to above, but may be called only from an active operation
295 /* (i.e. an init or continue routine) on an inbound half connection.
296 /* The receive effectively pre-empts the old operation, which wil
297 /* continue after the receive is done.
300 /************************************************************************/
303 char *objp; /* address of the object to */
305 int type; /* type code for the object */
307 char *flattened; /* address of first byte */
308 /* of flattened data */
309 int len; /* length of the flattened */
317 /*----------------------------------------------------------*/
319 /* start_receiving_object
321 /*----------------------------------------------------------*/
324 start_receiving_object(op, con, objp, type)
330 struct robj_data *arg;
333 * Make sure the supplied connection is a legal one
335 GDB_CHECK_CON(con, "start_receiving_object")
336 GDB_CHECK_OP(op, "start_receiving_object")
338 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
342 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
343 (void) queue_operation(con, CON_INPUT, op);
346 /*----------------------------------------------------------*/
348 /* preempt_and_start_receiving_object
350 /*----------------------------------------------------------*/
353 preempt_and_start_receiving_object(op, oldop, objp, type)
359 struct robj_data *arg;
362 * Make sure the supplied connection is a legal one
364 GDB_CHECK_OP(op, "preempt_and_start_receiving_object")
365 GDB_CHECK_OP(oldop, "preempt_and_start_receiving_object")
367 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
371 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
372 (void) g_preempt_me(oldop, op);
375 /*----------------------------------------------------------*/
379 /* Initialization routine for receiving an object.
380 /* Called when the receive operation percolates to the
381 /* top of the queue. First, we must receive the single
382 /* 'long' which carries the length of the rest of the data.
383 /* We do that now, either synchronously or asynchronously.
385 /*----------------------------------------------------------*/
388 g_ircobj(op, hcon, arg)
390 HALF_CONNECTION hcon;
391 struct robj_data *arg;
393 op->fcn.cont = g_c1rcobj;
394 if(gdb_receive_data(hcon, (char *)&(arg->len), sizeof(long)) == OP_COMPLETE) {
395 return g_c1rcobj(op, hcon, arg);/* this return is a little */
396 /* subtle. As continuation */
397 /* routines call each other */
398 /* synchronously, the last */
399 /* one determines whether we */
400 /* completed or are still */
401 /* running. That status */
402 /* percolates back through */
403 /* the entire call chain. */
409 /*----------------------------------------------------------*/
413 /* At this point, we have received the length. Now, allocate
414 /* the space for the rest of the data, and start receiving
417 /*----------------------------------------------------------*/
420 g_c1rcobj(op, hcon, arg)
422 HALF_CONNECTION hcon;
423 struct robj_data *arg;
426 extern u_long ntohl();
430 * Now we know the length of the encoded data, convert the length
431 * to local byte order, and allocate the space for the receive.
433 arg->len = (int) ntohl((u_long)arg->len);
434 if (arg->len > 65536)
437 arg->flattened = db_alloc(arg->len);
438 if (arg->flattened == NULL)
441 * Now start receiving the encoded object itself. If it all comes in
442 * synchronously, then just go on to the c2 routine to decode it and
443 * finish up. Else return OP_RUNNING, so the rest of the system
444 * can get some work done while we wait.
446 op->fcn.cont = g_c2rcobj;
447 if(gdb_receive_data(hcon, arg->flattened, arg->len ) == OP_COMPLETE) {
448 return g_c2rcobj(op, hcon, arg);
454 /*----------------------------------------------------------*/
458 /* At this point, all the data has been received. Decode
459 /* it into the place provided by the caller, free all
460 /* temporarily allocated memory, and return.
462 /*----------------------------------------------------------*/
465 g_c2rcobj(op, hcon, arg)
467 HALF_CONNECTION hcon;
468 struct robj_data *arg;
471 * Decode the received data into local representation.
473 FCN_PROPERTY(arg->type, DECODE_PROPERTY)
474 (arg->objp, hcon, arg->flattened);
475 op->result = OP_SUCCESS;
476 db_free(arg->flattened, arg->len); /* free the received data */
477 db_free((char *)arg, sizeof(struct robj_data)); /* free the state structure */
481 /************************************************************************/
483 /* complete_operation(complete_operation)
485 /* Wait for a given operation to complete, allowing everything
486 /* to progress in the meantime. Returns the last known status
487 /* of the operation, which in general will be OP_COMPLETE unless
488 /* errors were encountered (and this version of the code doesn't
489 /* do error handing right anyway!)
491 /* We do this by (1) calling gdb_progress to assure that all
492 /* possible progress has been made, which is always a good thing
493 /* to do when we get the chance and (2) looping on calls to
494 /* con_select, which will make all possible future progress,
495 /* but without burning cycles unnecessarily in the process.
497 /************************************************************************/
500 complete_operation(op)
503 (void) gdb_progress();
505 while(op->status != OP_COMPLETE && op->status != OP_CANCELLED)
506 (void) con_select(0, (fd_set *)NULL, (fd_set *)NULL,
507 (fd_set *)NULL, (struct timeval *)NULL);
514 /************************************************************************/
516 /* cancel_operation(cancel_operation)
518 /* Attempts to cancel an operation.
520 /************************************************************************/
526 register HALF_CONNECTION hcon = op->halfcon;
528 if (op->status != OP_RUNNING && op->status != OP_QUEUED)
532 GDB_GIVEUP("cancel_operation: operation is queued but half connection is unknown")
535 * If we're at the head of the queue and running, then we have to
536 * call the cancelation routine for this particular operation so
539 if (op->prev == (OPERATION)hcon) {
540 if (op->status == OP_RUNNING && op->cancel != NULL)
541 (*op->cancel)(op->halfcon, op->arg);
545 * Looks safe, now cancel it.
547 op->next->prev = op->prev; /* de-q it */
548 op->prev->next = op->next; /* " " " */
549 op->status = OP_CANCELLED;
555 /************************************************************************/
559 /* Start the asynchronous acquisition of a connection. This
560 /* results in the queuing of a GDB "OPERATION" to do the
561 /* requested listening.
563 /************************************************************************/
566 char *otherside; /* data returned from an */
568 int *otherlen; /* length of the otherside */
570 int *fdp; /* ptr to the fd of the */
579 gdb_start_listening(op, con, otherside, lenp, fdp)
586 struct lis_data *arg;
591 * Make sure the supplied connection is a legal one
593 GDB_CHECK_CON(con, "start_listening")
594 GDB_CHECK_OP(op, "start_listening")
596 arg = (struct lis_data *)db_alloc(sizeof(struct lis_data));
598 arg->otherside = otherside;
599 arg->otherlen = lenp;
601 initialize_operation(op, g_ilis, (char *)arg, (int (*)())NULL);
602 (void) queue_operation(con, CON_INPUT, op);
605 /*----------------------------------------------------------*/
609 /* Init routine for doing a listen.
611 /*----------------------------------------------------------*/
614 g_ilis(op, hcon, arg)
616 HALF_CONNECTION hcon;
617 struct lis_data *arg;
622 * Set up continuation routine in case it's needed after the return
624 op->fcn.cont = g_clis;
627 * Try doing the listen now, and then decide whether to go
628 * right on to the continuation routine or to let things hang
631 rc = gdb_start_a_listen(hcon, arg->otherside, arg->otherlen, arg->fdp);
632 if (rc==OP_COMPLETE) {
633 return g_clis(op, hcon, arg); /* this return is a little */
634 /* subtle. As continuation */
635 /* routines call each other */
636 /* synchronously, the last */
637 /* one determines whether we */
638 /* completed or are still */
639 /* running. That status */
640 /* percolates back through */
641 /* the entire call chain. */
649 /*----------------------------------------------------------*/
653 /* Continuation routine for accepting a connection.
655 /* At this point, the fd has been accepted and all
656 /* the necessary information given back to the caller.
658 /*----------------------------------------------------------*/
661 g_clis(op, hcon, arg)
663 HALF_CONNECTION hcon;
664 struct lis_data *arg;
666 op->result = OP_SUCCESS;
667 db_free((char *)arg, sizeof(struct lis_data));
668 /* free the state structure */
673 /************************************************************************/
675 /* start_accepting_client
677 /* Start the asynchronous acquisition of a client. This queueable
678 /* operation first tries to accept a connection. On this connection,
679 /* it reads a startup string from the client, and then completes.
681 /* The return values from this are not quite what you might expect.
682 /* In general, the operation will show complete, rather than cancelled,
683 /* if it gets as far as creating the new connection at all. If
684 /* subsequent activities result in errors from system calls, then
685 /* this operation will complete with a status of OP_COMPLETE and a
686 /* result of OP_CANCELLED. In this case, the applications IS given
687 /* a connection descriptor for the new connection, and that descriptor
688 /* has an errno value indicating why the failure occurred. The
689 /* caller must then sever this connection to free the descriptor.
691 /************************************************************************/
694 char *otherside; /* data returned from an */
696 int *otherlen; /* length of the otherside */
698 OPERATION listenop; /* used to listen for */
700 OPERATION receiveop; /* used when receiving */
701 /* tuple from the client */
702 CONNECTION con; /* the connection we're */
703 /* trying to create */
704 CONNECTION *conp; /* this is where the caller */
705 /* wants the connection */
707 TUPLE *tuplep; /* pointer to tuple we */
708 /* are going to receive */
709 /* from new client */
716 start_accepting_client(listencon, op, conp, otherside, lenp, tuplep)
717 CONNECTION listencon;
724 struct acc_data *arg;
729 * Make sure the supplied connection and operation are legal
731 GDB_CHECK_CON(listencon, "start_accepting_client")
732 GDB_CHECK_OP(op, "start_accepting_client")
734 arg = (struct acc_data *)db_alloc(sizeof(struct acc_data));
736 arg->otherside = otherside;
737 arg->otherlen = lenp;
739 *conp = NULL; /* in case we fail */
740 arg->listenop = create_operation();
741 arg->receiveop = create_operation();
742 arg->con = g_make_con();
743 arg->tuplep = tuplep;
744 *tuplep = NULL; /* in case we fail */
747 * Queue an operation ahead of us which will accept an fd and
748 * put it in arg->con->in. As a byproduct, pick up the from
749 * information that we return to the caller.
751 gdb_start_listening(arg->listenop, listencon,
753 arg->otherlen, &(arg->con->in.fd));
756 * Now queue us behind it. By the time we run our init routine,
757 * a connection should have been acquired.
759 initialize_operation(op, g_iacc, (char *)arg, (int (*)())NULL);
760 (void) queue_operation(listencon, CON_INPUT, op);
763 /*----------------------------------------------------------*/
767 /* Init routine for accepting a connection. By the
768 /* time this runs, the listen has been done, the
769 /* 'from' data put in position for the caller, and
770 /* the fd plugged into the connection descriptor.
771 /* If all went well, fill out the connection descriptor
772 /* and then requeue us on that to do the receive of
773 /* the requested tuple.
775 /*----------------------------------------------------------*/
778 g_iacc(op, hcon, arg)
780 HALF_CONNECTION hcon;
781 struct acc_data *arg;
783 register CONNECTION con = arg->con;
786 * Set up 2nd init routine for after we re-queue ourselves
788 op->fcn.cont = g_i2acc;
790 * See whether we successfully accepted a connection. If
791 * not, we just cancel ourselves. If so, fill out the
792 * connection descriptor and related data structures properly,
793 * then requeue ourselves on the new connection.
795 if (OP_STATUS(arg->listenop) != OP_COMPLETE ||
796 OP_RESULT(arg->listenop) != OP_SUCCESS ||
798 (void) sever_connection(con);
800 op->result = OP_CANCELLED;
805 * OK, we got an fd, but the connection and related structures
806 * aren't really set up straight, and the fd must be put
807 * into non-blocking mode. There really should be a common
808 * routine for this, since some of the logic exists in 2
811 con->status = CON_STARTING;
812 con->out.fd = con->in.fd;
813 g_ver_iprotocol(con); /* make sure we're at */
814 /* same level of protocol */
815 if (con->status == CON_UP) {
817 * We've successfully started the connection, now mark
818 * it for non-blocking I/O. Also, update the high water
819 * mark of fd's controlled by our system.
822 if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) {
823 g_stop_with_errno(con);
824 *arg->conp = con; /* give failed con to */
825 /* caller so he can find */
827 gdb_perror("gdb: ioctl for non-block failed");
829 op->result = OP_CANCELLED; /* we didn't really, but */
830 /* we want caller to look */
831 /* at the connection so he */
835 if (con->in.fd +1 > gdb_mfd)
836 gdb_mfd = con->in.fd + 1;
838 * Allocate a buffer, if necessary, and reset buffer pointers
839 * so first request will result in a long read into the buffer
841 g_allocate_connection_buffers(con);
844 *arg->conp = con; /* give failed con to */
845 /* caller so he can find */
848 op->result = OP_CANCELLED;
853 * Before we requeue ourselves on the new connection, queue
854 * up a receive for the expected tuple. Then we'll be
855 * sure that it's there by the time we run.
857 start_receiving_object(arg->receiveop, con, (char *)(arg->tuplep),
860 * Requeue ourselves behind the receive operation.
863 (void) requeue_operation(con, CON_INPUT, op);
869 /*----------------------------------------------------------*/
873 /* Second init routine for accepting a connection.
874 /* This one is run after the operation is requeued on
875 /* the new connection. By the time we run here, the
876 /* attempt to receive the tuple has already been made.
877 /* We just check on status and clean-up.
879 /*----------------------------------------------------------*/
882 g_i2acc(op, hcon, arg)
884 HALF_CONNECTION hcon;
885 struct acc_data *arg;
889 rc = OP_STATUS(arg->receiveop); /* if it completes, then */
891 *arg->conp = arg->con; /* give caller the new con */
892 if (rc != OP_COMPLETE)
893 (void) g_stop_connection(arg->con);
895 * Release all transient data structures.
902 /*----------------------------------------------------------*/
906 /* Free all data structures used by start_accepting_client.
908 /*----------------------------------------------------------*/
912 struct acc_data *arg;
914 delete_operation(arg->listenop);
915 delete_operation(arg->receiveop);
916 db_free((char *)arg, sizeof(struct acc_data));