7 static char *rcsid_gdb_ops_c = "$Header$";
11 /************************************************************************
15 * GDB - Asynchronous Operations and Their Synchronous
18 * Author: Noah Mendelsohn
19 * Copyright: 1986 MIT Project Athena
20 * For copying and distribution information, please see
21 * the file <mit-copyright.h>.
23 * These routines provide a suite of asynchronous operations
26 ************************************************************************/
28 #include <mit-copyright.h>
31 #include <netinet/in.h>
32 #include <sys/ioctl.h>
34 #include <sys/filio.h>
37 /************************************************************************
39 * send_object (send_object)
41 * Synchronous form of start_sending_object. Returns either
42 * OP_CANCELLED, or OP_RESULT(op).
44 ************************************************************************/
47 send_object(con, objp, type)
52 register OPERATION op;
56 op = create_operation();
57 start_sending_object(op, con, objp, type);
58 (void) complete_operation(op);
59 if (OP_STATUS(op) == OP_COMPLETE)
60 retval = OP_RESULT(op);
62 retval = OP_STATUS(op);
68 /************************************************************************/
70 /* start_send_object (g_snobj)
72 /* Start the asynchronous transmission of a gdb object.
73 /* Note that this routine must be passed the address of the object,
74 /* not the object itself.
76 /* The following three routines work together, and may be considered
77 /* as a single entity implementing the operation. The first merely
78 /* saves away its arguments and queues the operation on the designated
79 /* connection. These stay there until they percolate to the head of
80 /* the queue. The second is the initialization routine, which is
81 /* called by the connection maintenance logic when the operation
82 /* first reaches the head of the queue. This routine encodes
83 /* the supplied data for transmission, and then sends it. If the
84 /* transmission executes synchronously, then the third routine is
85 /* called immediately to clean up. If not, the third routine is
86 /* marked as the 'continuation' routine, which will cause its
87 /* invocation when the transmission completes.
89 /* The data is preceded by its length expressed as a 32-bit number in
90 /* network byte order.
92 /************************************************************************/
95 char *objp; /* address of the object to */
97 int type; /* type code for the object */
99 char *flattened; /* address of first byte */
100 /* of flattened data */
101 int len; /* length of the flattened */
109 start_sending_object(op, con, objp, type)
115 struct obj_data *arg;
118 * Make sure the supplied connection is a legal one
120 GDB_CHECK_CON(con, "start_sending_object")
121 GDB_CHECK_OP(op, "start_sending_object")
123 arg = (struct obj_data *)db_alloc(sizeof(struct obj_data));
127 initialize_operation(op, g_isnobj, (char *)arg, (int (*)())NULL);
128 (void) queue_operation(con, CON_OUTPUT, op);
131 /*----------------------------------------------------------*/
135 /* Init routine for sending an object. This routine is
136 /* called by the connection management logic when the send
137 /* request percolates to the top of the queue. This routine
138 /* reformats the data into an appropriate form for transmission.
139 /* The format used is a length, represented as a 32-bit # in
140 /* network byte order, followed by the data itself. The
141 /* continuation routine below is called, either synchronously
142 /* or asynchronously, once the transmission is complete.
144 /*----------------------------------------------------------*/
147 g_isnobj(op, hcon, arg)
149 HALF_CONNECTION hcon;
150 struct obj_data *arg;
153 * Find out the encoded length of the data
155 arg->len = FCN_PROPERTY(arg->type, CODED_LENGTH_PROPERTY)
159 * Allocate space and flatten (encode) the data
161 arg->flattened = db_alloc(arg->len+sizeof(int32));
162 *(uint32 *)arg->flattened = htonl((uint32)arg->len);
164 FCN_PROPERTY(arg->type, ENCODE_PROPERTY)
165 (arg->objp, hcon, arg->flattened+sizeof(int32));
168 * Set up continuation routine in case it's needed after the return
170 op->fcn.cont = g_csnobj;
173 * Start sending the data, maybe even complete
175 if (gdb_send_data(hcon, arg->flattened, arg->len + sizeof(int32)) ==
177 return g_csnobj(op, hcon, arg) ;/* this return is a little */
178 /* subtle. As continuation */
179 /* routines call each other */
180 /* synchronously, the last */
181 /* one determines whether we */
182 /* completed or are still */
183 /* running. That status */
184 /* percolates back through */
185 /* the entire call chain. */
196 /*----------------------------------------------------------*/
200 /* Continuation routine for sending an object. Since there is
201 /* only one transmission, started by the init routine, this is
202 /* called when that transmission is done, and it does all the
203 /* associated clean up.
205 /*----------------------------------------------------------*/
209 g_csnobj(op, hcon, arg)
211 HALF_CONNECTION hcon;
212 struct obj_data *arg;
214 op->result = OP_SUCCESS;
215 db_free((char *)arg->flattened, arg->len + sizeof(int32));
216 /* free the sent data */
217 db_free((char *)arg, sizeof(struct obj_data)); /* free the state structure */
222 /************************************************************************/
224 /* receive_object (receive_object)
226 /* Synchronous form of start_receiving_object. Returns either
227 /* OP_CANCELLED, or OP_RESULT(op).
229 /************************************************************************/
232 receive_object(con, objp, type)
237 register OPERATION op;
240 op = create_operation();
241 start_receiving_object(op, con, objp, type);
242 (void) complete_operation(op);
243 if (OP_STATUS(op) == OP_COMPLETE)
244 retval = OP_RESULT(op);
246 retval = OP_STATUS(op);
247 delete_operation(op);
252 /************************************************************************/
254 /* start_receiving_object (g_rcobj)
256 /* Start the asynchronous receipt of a gdb object. Note that this
257 /* routine must be passed the address of the object, not the object
258 /* itself. In the case of structured objects, this routine may
259 /* allocate the necessary storage. The work to build the object is
260 /* done by the object's decode routine.
262 /* The following three routines work together, and may be considered
263 /* as a single entity implementing the operation. The first merely
264 /* saves away its arguments and queues the operation on the designated
265 /* connection. These stay there until they percolate to the head of
266 /* the queue. The second is the initialization routine, which is
267 /* called by the connection maintenance logic when the operation
268 /* first reaches the head of the queue. This routine initiates a read
269 /* for the length of the object, and sets up a continuation routine
270 /* to read the object itself. When the object itself has been read, it
271 /* is decoded and the operation completes.
273 /* The data is preceded by its length expressed as a 32-bit number in
274 /* network byte order.
276 /* preempt_and_start_receiving_object (g_prcobj)
278 /* Similar to above, but may be called only from an active operation
279 /* (i.e. an init or continue routine) on an inbound half connection.
280 /* The receive effectively pre-empts the old operation, which wil
281 /* continue after the receive is done.
284 /************************************************************************/
287 char *objp; /* address of the object to */
289 int type; /* type code for the object */
291 char *flattened; /* address of first byte */
292 /* of flattened data */
293 int len; /* length of the flattened */
301 /*----------------------------------------------------------*/
303 /* start_receiving_object
305 /*----------------------------------------------------------*/
308 start_receiving_object(op, con, objp, type)
314 struct robj_data *arg;
317 * Make sure the supplied connection is a legal one
319 GDB_CHECK_CON(con, "start_receiving_object")
320 GDB_CHECK_OP(op, "start_receiving_object")
322 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
326 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
327 (void) queue_operation(con, CON_INPUT, op);
330 /*----------------------------------------------------------*/
332 /* preempt_and_start_receiving_object
334 /*----------------------------------------------------------*/
337 preempt_and_start_receiving_object(op, oldop, objp, type)
343 struct robj_data *arg;
346 * Make sure the supplied connection is a legal one
348 GDB_CHECK_OP(op, "preempt_and_start_receiving_object")
349 GDB_CHECK_OP(oldop, "preempt_and_start_receiving_object")
351 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
355 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
356 (void) g_preempt_me(oldop, op);
359 /*----------------------------------------------------------*/
363 /* Initialization routine for receiving an object.
364 /* Called when the receive operation percolates to the
365 /* top of the queue. First, we must receive the single
366 /* 32-bit # which carries the length of the rest of the data.
367 /* We do that now, either synchronously or asynchronously.
369 /*----------------------------------------------------------*/
372 g_ircobj(op, hcon, arg)
374 HALF_CONNECTION hcon;
375 struct robj_data *arg;
377 op->fcn.cont = g_c1rcobj;
378 if(gdb_receive_data(hcon, (char *)&(arg->len), sizeof(int32)) == OP_COMPLETE) {
379 return g_c1rcobj(op, hcon, arg);/* this return is a little */
380 /* subtle. As continuation */
381 /* routines call each other */
382 /* synchronously, the last */
383 /* one determines whether we */
384 /* completed or are still */
385 /* running. That status */
386 /* percolates back through */
387 /* the entire call chain. */
393 /*----------------------------------------------------------*/
397 /* At this point, we have received the length. Now, allocate
398 /* the space for the rest of the data, and start receiving
401 /*----------------------------------------------------------*/
404 g_c1rcobj(op, hcon, arg)
406 HALF_CONNECTION hcon;
407 struct robj_data *arg;
410 * Now we know the length of the encoded data, convert the length
411 * to local byte order, and allocate the space for the receive.
413 arg->len = (int) ntohl((uint32)arg->len);
414 if (arg->len > 65536)
417 arg->flattened = db_alloc(arg->len);
418 if (arg->flattened == NULL)
421 * Now start receiving the encoded object itself. If it all comes in
422 * synchronously, then just go on to the c2 routine to decode it and
423 * finish up. Else return OP_RUNNING, so the rest of the system
424 * can get some work done while we wait.
426 op->fcn.cont = g_c2rcobj;
427 if(gdb_receive_data(hcon, arg->flattened, arg->len ) == OP_COMPLETE) {
428 return g_c2rcobj(op, hcon, arg);
434 /*----------------------------------------------------------*/
438 /* At this point, all the data has been received. Decode
439 /* it into the place provided by the caller, free all
440 /* temporarily allocated memory, and return.
442 /*----------------------------------------------------------*/
445 g_c2rcobj(op, hcon, arg)
447 HALF_CONNECTION hcon;
448 struct robj_data *arg;
451 * Decode the received data into local representation.
453 FCN_PROPERTY(arg->type, DECODE_PROPERTY)
454 (arg->objp, hcon, arg->flattened);
455 op->result = OP_SUCCESS;
456 db_free(arg->flattened, arg->len); /* free the received data */
457 db_free((char *)arg, sizeof(struct robj_data)); /* free the state structure */
462 /************************************************************************/
464 /* complete_operation(complete_operation)
466 /* Wait for a given operation to complete, allowing everything
467 /* to progress in the meantime. Returns the last known status
468 /* of the operation, which in general will be OP_COMPLETE unless
469 /* errors were encountered (and this version of the code doesn't
470 /* do error handing right anyway!)
472 /* We do this by (1) calling gdb_progress to assure that all
473 /* possible progress has been made, which is always a good thing
474 /* to do when we get the chance and (2) looping on calls to
475 /* con_select, which will make all possible future progress,
476 /* but without burning cycles unnecessarily in the process.
478 /************************************************************************/
481 complete_operation(op)
484 (void) gdb_progress();
486 while(op->status != OP_COMPLETE && op->status != OP_CANCELLED)
487 (void) con_select(0, (fd_set *)NULL, (fd_set *)NULL,
488 (fd_set *)NULL, (struct timeval *)NULL);
495 /************************************************************************/
497 /* cancel_operation(cancel_operation)
499 /* Attempts to cancel an operation.
501 /************************************************************************/
507 register HALF_CONNECTION hcon = op->halfcon;
509 if (op->status != OP_RUNNING && op->status != OP_QUEUED)
513 GDB_GIVEUP("cancel_operation: operation is queued but half connection is unknown")
516 * If we're at the head of the queue and running, then we have to
517 * call the cancelation routine for this particular operation so
520 if (op->prev == (OPERATION)hcon) {
521 if (op->status == OP_RUNNING && op->cancel != NULL)
522 (*op->cancel)(op->halfcon, op->arg);
526 * Looks safe, now cancel it.
528 op->next->prev = op->prev; /* de-q it */
529 op->prev->next = op->next; /* " " " */
530 op->status = OP_CANCELLED;
537 /************************************************************************/
541 /* Start the asynchronous acquisition of a connection. This
542 /* results in the queuing of a GDB "OPERATION" to do the
543 /* requested listening.
545 /************************************************************************/
548 char *otherside; /* data returned from an */
550 int *otherlen; /* length of the otherside */
552 int *fdp; /* ptr to the fd of the */
561 gdb_start_listening(op, con, otherside, lenp, fdp)
568 struct lis_data *arg;
573 * Make sure the supplied connection is a legal one
575 GDB_CHECK_CON(con, "start_listening")
576 GDB_CHECK_OP(op, "start_listening")
578 arg = (struct lis_data *)db_alloc(sizeof(struct lis_data));
580 arg->otherside = otherside;
581 arg->otherlen = lenp;
583 initialize_operation(op, g_ilis, (char *)arg, (int (*)())NULL);
584 (void) queue_operation(con, CON_INPUT, op);
587 /*----------------------------------------------------------*/
591 /* Init routine for doing a listen.
593 /*----------------------------------------------------------*/
596 g_ilis(op, hcon, arg)
598 HALF_CONNECTION hcon;
599 struct lis_data *arg;
604 * Set up continuation routine in case it's needed after the return
606 op->fcn.cont = g_clis;
609 * Try doing the listen now, and then decide whether to go
610 * right on to the continuation routine or to let things hang
613 rc = gdb_start_a_listen(hcon, arg->otherside, arg->otherlen, arg->fdp);
614 if (rc==OP_COMPLETE) {
615 return g_clis(op, hcon, arg); /* this return is a little */
616 /* subtle. As continuation */
617 /* routines call each other */
618 /* synchronously, the last */
619 /* one determines whether we */
620 /* completed or are still */
621 /* running. That status */
622 /* percolates back through */
623 /* the entire call chain. */
631 /*----------------------------------------------------------*/
635 /* Continuation routine for accepting a connection.
637 /* At this point, the fd has been accepted and all
638 /* the necessary information given back to the caller.
640 /*----------------------------------------------------------*/
644 g_clis(op, hcon, arg)
646 HALF_CONNECTION hcon;
647 struct lis_data *arg;
649 op->result = OP_SUCCESS;
650 db_free((char *)arg, sizeof(struct lis_data));
651 /* free the state structure */
656 /************************************************************************/
658 /* start_accepting_client
660 /* Start the asynchronous acquisition of a client. This queueable
661 /* operation first tries to accept a connection. On this connection,
662 /* it reads a startup string from the client, and then completes.
664 /* The return values from this are not quite what you might expect.
665 /* In general, the operation will show complete, rather than cancelled,
666 /* if it gets as far as creating the new connection at all. If
667 /* subsequent activities result in errors from system calls, then
668 /* this operation will complete with a status of OP_COMPLETE and a
669 /* result of OP_CANCELLED. In this case, the applications IS given
670 /* a connection descriptor for the new connection, and that descriptor
671 /* has an errno value indicating why the failure occurred. The
672 /* caller must then sever this connection to free the descriptor.
674 /************************************************************************/
677 char *otherside; /* data returned from an */
679 int *otherlen; /* length of the otherside */
681 OPERATION listenop; /* used to listen for */
683 OPERATION receiveop; /* used when receiving */
684 /* tuple from the client */
685 CONNECTION con; /* the connection we're */
686 /* trying to create */
687 CONNECTION *conp; /* this is where the caller */
688 /* wants the connection */
690 TUPLE *tuplep; /* pointer to tuple we */
691 /* are going to receive */
692 /* from new client */
699 start_accepting_client(listencon, op, conp, otherside, lenp, tuplep)
700 CONNECTION listencon;
707 struct acc_data *arg;
712 * Make sure the supplied connection and operation are legal
714 GDB_CHECK_CON(listencon, "start_accepting_client")
715 GDB_CHECK_OP(op, "start_accepting_client")
717 arg = (struct acc_data *)db_alloc(sizeof(struct acc_data));
719 arg->otherside = otherside;
720 arg->otherlen = lenp;
722 *conp = NULL; /* in case we fail */
723 arg->listenop = create_operation();
724 arg->receiveop = create_operation();
725 arg->con = g_make_con();
726 arg->tuplep = tuplep;
727 *tuplep = NULL; /* in case we fail */
730 * Queue an operation ahead of us which will accept an fd and
731 * put it in arg->con->in. As a byproduct, pick up the from
732 * information that we return to the caller.
734 gdb_start_listening(arg->listenop, listencon,
736 arg->otherlen, &(arg->con->in.fd));
739 * Now queue us behind it. By the time we run our init routine,
740 * a connection should have been acquired.
742 initialize_operation(op, g_iacc, (char *)arg, (int (*)())NULL);
743 (void) queue_operation(listencon, CON_INPUT, op);
746 /*----------------------------------------------------------*/
750 /* Init routine for accepting a connection. By the
751 /* time this runs, the listen has been done, the
752 /* 'from' data put in position for the caller, and
753 /* the fd plugged into the connection descriptor.
754 /* If all went well, fill out the connection descriptor
755 /* and then requeue us on that to do the receive of
756 /* the requested tuple.
758 /*----------------------------------------------------------*/
762 g_iacc(op, hcon, arg)
764 HALF_CONNECTION hcon;
765 struct acc_data *arg;
767 register CONNECTION con = arg->con;
770 * Set up 2nd init routine for after we re-queue ourselves
772 op->fcn.cont = g_i2acc;
774 * See whether we successfully accepted a connection. If
775 * not, we just cancel ourselves. If so, fill out the
776 * connection descriptor and related data structures properly,
777 * then requeue ourselves on the new connection.
779 if (OP_STATUS(arg->listenop) != OP_COMPLETE ||
780 OP_RESULT(arg->listenop) != OP_SUCCESS ||
782 (void) sever_connection(con);
784 op->result = OP_CANCELLED;
789 * OK, we got an fd, but the connection and related structures
790 * aren't really set up straight, and the fd must be put
791 * into non-blocking mode. There really should be a common
792 * routine for this, since some of the logic exists in 2
795 con->status = CON_STARTING;
796 con->out.fd = con->in.fd;
797 g_ver_iprotocol(con); /* make sure we're at */
798 /* same level of protocol */
799 if (con->status == CON_UP) {
801 * We've successfully started the connection, now mark
802 * it for non-blocking I/O. Also, update the high water
803 * mark of fd's controlled by our system.
806 if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) {
807 g_stop_with_errno(con);
808 *arg->conp = con; /* give failed con to */
809 /* caller so he can find */
811 gdb_perror("gdb: ioctl for non-block failed");
813 op->result = OP_CANCELLED; /* we didn't really, but */
814 /* we want caller to look */
815 /* at the connection so he */
819 if (con->in.fd +1 > gdb_mfd)
820 gdb_mfd = con->in.fd + 1;
822 * Allocate a buffer, if necessary, and reset buffer pointers
823 * so first request will result in a long read into the buffer
825 g_allocate_connection_buffers(con);
828 *arg->conp = con; /* give failed con to */
829 /* caller so he can find */
832 op->result = OP_CANCELLED;
837 * Before we requeue ourselves on the new connection, queue
838 * up a receive for the expected tuple. Then we'll be
839 * sure that it's there by the time we run.
841 start_receiving_object(arg->receiveop, con, (char *)(arg->tuplep),
844 * Requeue ourselves behind the receive operation.
847 (void) requeue_operation(con, CON_INPUT, op);
853 /*----------------------------------------------------------*/
857 /* Second init routine for accepting a connection.
858 /* This one is run after the operation is requeued on
859 /* the new connection. By the time we run here, the
860 /* attempt to receive the tuple has already been made.
861 /* We just check on status and clean-up.
863 /*----------------------------------------------------------*/
867 g_i2acc(op, hcon, arg)
869 HALF_CONNECTION hcon;
870 struct acc_data *arg;
874 rc = OP_STATUS(arg->receiveop); /* if it completes, then */
876 *arg->conp = arg->con; /* give caller the new con */
877 if (rc != OP_COMPLETE)
878 (void) g_stop_connection(arg->con);
880 * Release all transient data structures.
887 /*----------------------------------------------------------*/
891 /* Free all data structures used by start_accepting_client.
893 /*----------------------------------------------------------*/
897 struct acc_data *arg;
899 delete_operation(arg->listenop);
900 delete_operation(arg->receiveop);
901 db_free((char *)arg, sizeof(struct acc_data));