7 static char *rcsid_gdb_trans2_c = "$Header$";
30 /************************************************************************/
34 /* GDB - Data Transport Services Routines (Part 2)
36 /* Author: Noah Mendelsohn
37 /* Copyright: 1986 MIT Project Athena
39 /* These routines implement layer 6 of the Client Library
40 /* Specification of the GDB system, as well as the facilities
41 /* outlined in the GDB Protocol Specification. Part 2 of 2.
43 /* Some of the routines specified are actually implemented as
44 /* macros defined in gdb.h.
46 /************************************************************************/
48 #include <sys/types.h>
53 #include <sys/socket.h>
54 extern int errno; /* Unix error slot */
57 * The following values are returned by g_con_progress
59 #define NOPROGRESS 0 /* nothing happened on this */
60 /* connection--must be 0*/
61 #define PROGRESS 1 /* connection has progressed */
62 #define COMPLETE 2 /* an operation has */
63 /* completed on this con */
65 /************************************************************************/
67 /* queue_operation(queue_operation)
69 /* Add an operation to the queue for a given connection, and
70 /* then allows all connections to progress. Returns the last
71 /* known status of the operation.
73 /************************************************************************/
76 queue_operation(con, direction, op)
81 register HALF_CONNECTION hcon = (direction==CON_INPUT)?(&(con->in)):
83 GDB_CHECK_CON(con, "queue_operation")
85 * Write message to debugging log
87 if (gdb_Debug & GDB_LOG)
88 fprintf(gdb_log, "op queued: con=0x%x dir=%s op=0x%x Q was %s empty\n",
89 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
90 op, (hcon->op_q_first == (OPERATION)hcon)?"":"not");
92 * Make sure connection is up
94 if (con->status != CON_UP) {
95 op->status = OP_CANCELLED;
96 if (gdb_Debug & GDB_LOG)
97 fprintf(gdb_log, "\nop NOT queued\n");
102 * Put the new operation at the end of the queue
104 op->prev = hcon->op_q_last;
105 op->next = (OPERATION)hcon;
106 hcon->op_q_last->next = op;
107 hcon->op_q_last = op;
111 op->status = OP_QUEUED;
115 * Force progress on this connection
117 (void) g_con_progress(con - gdb_cons);
119 * Con_select with notime is used here as a kind of fudge for
120 * doing a fastprogress with a select built in before it.
122 (void) con_select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0,
123 &gdb_notime);/* XXX */
125 * Return the last known status of the operation
129 /************************************************************************/
133 /* This routine may be called from an init or continuation routine
134 /* to cause the current operation to be requeued on a new connection.
135 /* The init routine field ofthe operation should be properly set to
136 /* indicate the routine to receive control when the operation actually
137 /* runs on the new connection. The caller of this routine is
138 /* responsible for returning the status OP_REQUEUED to its caller.
140 /* This routine returns the status of the newly queued operation.
141 /* Note, however, that even if this operation returns the status
142 /* CANCELLED, the operation itself may not continue to execute
143 /* on the old connection and it should return the status OP_REQUEUED,
144 /* NOT OP_CANCELLED (at least in this implementation.)
146 /************************************************************************/
149 requeue_operation(con, direction, op)
155 * Make sure the connection supplied is a legal one
157 GDB_CHECK_CON(con, "requeue_operation")
159 * Write message to debugging log
161 if (gdb_Debug & GDB_LOG)
162 fprintf(gdb_log, "op requeued: new con=0x%x dir=%s op=0x%x\n",
163 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
166 * Dequeue the operation from its old half connection
168 (void) g_op_newhead(op->halfcon);
171 * Now queue it on the new one
173 return queue_operation(con, direction, op);
176 /************************************************************************/
180 /* Sticks a new operation in ahead of the current one and runs it
181 /* on the current connection. May be called only from an init or
182 /* continuation routine. The old operation must have completely
183 /* prepared the descriptor for the new operation, i.e. it should
184 /* be in the same state as it would be for a call to queue_operation.
185 /* g_preempt_me makes it possible for operations to be built by
186 /* composition of other smaller operations, since newop runs, in
187 /* a sense, as a subroutine of oldop. opdop must (1) reset its
188 /* initialization routine to be a routine to be called when newop
189 /* completes or cancels and (2) return the status OP_PREEMPTED to
192 /************************************************************************/
195 g_preempt_me(oldop, newop)
199 register OPERATION old=oldop, new=newop;
200 register HALF_CONNECTION hc = old->halfcon;
203 * Write message to debugging log
205 if (gdb_Debug & GDB_LOG)
206 fprintf(gdb_log, "op preempted: halfcon=0x%x oldop=0x%x newop=0x%x\n",
209 * link in the new operation
212 hc->op_q_first = new;
213 new->prev = (OPERATION)hc;
216 * Set the status of the new operation
218 new->status = OP_QUEUED;
221 * Change the status of the old operation (one could argue that
222 * this should be done in gdb_hcon_progress after the return code
225 old->status = OP_QUEUED;
231 /************************************************************************/
235 /* This routine should be called whenever it is suspected that
236 /* progress can be made on any connection. This routine will
237 /* cause all connections to proceed as far as they can without
238 /* blocking, and will make a best effort to avoid long blocks.
239 /* This routine MAY retain control for long periods when sustained
240 /* progress is possible, but it will not knowingly hang.
242 /* Returns: number of connections on which OPERATIONS have
243 /* COMPLETED (not just progressed).
245 /************************************************************************/
251 register int i; /* index to available */
253 register int return_value = 0; /* the value we return */
254 int rc; /* short term storage for */
256 int progress_made; /* true when some con */
257 /* made progress during */
258 /* latest pass through list */
259 int complete_map[GDB_MAX_CONNECTIONS]; /* indicates whether a */
260 /* transmission operation */
261 /* is newly complete on */
262 /* corresponding connection */
263 /* 1 if yes else 0 */
264 int maxcon = gdb_mcons; /* gdb_mcons may change */
265 /* out from under us if */
266 /* connections break. This */
267 /* is the initial value. */
270 * Zero out the completion map for all connections.
272 for (i=0; i<maxcon; i++)
276 * Make repeated passes through all the fd's until a pass is made
277 * in which none makes any progress. This logic is important,
278 * because it catches the case where A is blocked, B makes progress,
279 * and A unblocks during the period where B is progressing.
283 progress_made = FALSE;
284 for (i=0; i<gdb_mcons; i++) {
285 if (rc = g_con_progress(i)) { /* note: NOPROGRESS==0 */
286 progress_made = TRUE;
291 } while (progress_made);
294 * We've gone as far as we can, now find out how many connections
295 * have had operations complete.
297 for (i=0; i<maxcon; i++)
298 return_value += complete_map[i];
304 /************************************************************************/
308 /* Similar to gdb_progress, but this routine attempts progress
309 /* only on those connections which have already shown themselves
310 /* to be ready for activity by a prior select. This is safe to do
311 /* when (1) the only activity we are interested in is that related
312 /* to ongoing I/O and (2) a select was recently done to set the
313 /* last_c.fds flags. Condition (1) is violated in the case where
314 /* an operation may be newly at the head of a queue and its init
315 /* routine may not have had a chance to run. Condition (2) is violated
316 /* when we are entering after having done significant computation.
318 /* This routine was introduced by Bill Sommerfeld after profiling
319 /* revealed that unnecessary attempts to progress on quiescent
320 /* sockets were causing excessive overhead in the system. I am
321 /* still suspicious that this routine may be getting called in
322 /* places where a full gdb_progress is needed. e.g. I'm not
323 /* sure its use in op_select is entirely safe.
325 /************************************************************************/
332 for (i=0; i<gdb_mcons; i++) {
333 register CONNECTION con = &gdb_cons[i];
334 register int infd = gdb_cons[i].in.fd;
335 register int outfd = gdb_cons[i].out.fd;
337 if(connection_status(con) != CON_UP)
341 if ((!(con->in.flags&HCON_UNUSED))&&
342 ((con->in.stream_buffer_remaining > 0)
343 || FD_ISSET(infd, &last_crfds))) {
344 rc = gdb_hcon_progress(CON_INPUT, &con->in);
346 g_stop_with_errno(con);
352 if ((!(con->out.flags&HCON_UNUSED))&&
353 (FD_ISSET(outfd, &last_cwfds))) {
354 rc = gdb_hcon_progress(CON_OUTPUT, &con->out);
356 g_stop_with_errno(con);
364 * We've gone as far as we can, now find out how many connections
365 * have had operations complete.
371 /************************************************************************/
375 /* Make as much progress as possible on the specified connection.
376 /* Returns NOPROGRESS if no bytes moved on either half connection,
377 /* PROGRESS, if some moved and no operations completed, or COMPLETE if
378 /* any of the operations completed. Note that each connection
379 /* consists of two half connections, and we must make each of them
380 /* progress as far as possible.
382 /* The nest here starts getting so deep that it's hard to pass state
383 /* around efficiently. We use a single global variable, gdb_conok,
384 /* to indicate whether the connection we're working on now has died.
385 /* The move data routines set this to FALSE whenever there is a
386 /* fatal error on a connection. We check it, and do a proper
387 /* sever on the connection if it seems to be in trouble.
389 /************************************************************************/
393 g_con_progress(con_id)
394 int con_id; /* index of this connection */
395 /* in the connection desc. */
398 register CONNECTION con= (&gdb_cons[con_id]);
399 /* pointer to the connection */
401 register int progress = NOPROGRESS;
402 register int live = TRUE; /* true when we've seen */
403 /* enough to make sure we */
404 /* want to go around again*/
407 * Check status of connection-if it's not running, then just return.
409 if (con->status != CON_UP)
412 * Repeatedly make progress on each half connection until both
413 * are idle. Important to keep trying as one may become active
414 * while the other is progressing.
417 gdb_conok = TRUE; /* this gets set to FALSE */
418 /* for fatal I/O errors */
419 /* there may be a timing */
420 /* window here in use of */
421 /* HCON_BUSY. Also: it is */
422 /* essential that errno */
423 /* remain valid after conok */
426 live = FALSE; /* until proven otherwise */
428 * make progress on the input connection note that following
429 * logic depends on NOPROGRESS being 0
431 if (rc = gdb_hcon_progress(CON_INPUT, &con->in)) {
433 progress = max(rc, progress);
436 * See if connection has died
439 g_stop_with_errno(con);
440 return COMPLETE; /* dying connection always */
441 /* implies that the */
442 /* operation at the head */
443 /* of the queue completed */
446 * make progress on the output connection
448 if (rc = gdb_hcon_progress(CON_OUTPUT, &con->out)) {
450 progress = max(rc, progress);
453 * See if connection has died
456 g_stop_with_errno(con);
465 /************************************************************************/
469 /* Allows a specified half-connection to progress as much as possible,
470 /* and returns true iff at least one operation is newly completed.
472 /************************************************************************/
475 gdb_hcon_progress(direction, hc)
476 int direction; /* CON_INPUT or CON_OUTPUT */
477 struct half_con_data *hc; /* pointer to control struct */
478 /* for this half connection */
480 HALF_CONNECTION half_con = hc;
481 /* half connection pointer */
482 /* fast copy in register */
483 register OPERATION op; /* current operation on this */
484 /* half connection */
485 int progress = NOPROGRESS; /* can indicate any progress */
486 /* on the half con or */
487 /* whether any operations */
489 int done; /* true when no more progress*/
491 int fcn_result; /* result of latest init or */
492 /* continue function */
495 * Write message to debugging log
497 if (gdb_Debug & GDB_LOG)
498 fprintf(gdb_log, "hcon_progress: halfcon=0x%x dir=%s ",
499 half_con, (direction==CON_INPUT)?"INPUT":"OUTPUT");
501 /*----------------------------------------------------------*/
503 /* See if we are being re-entered and are already working
504 /* on this half_con. If so, return right now.
506 /*----------------------------------------------------------*/
508 if (half_con->flags & HCON_BUSY) {
510 * Write message to debugging log
512 if (gdb_Debug & GDB_LOG)
513 fprintf(gdb_log, "BUSY, returning\n");
517 /*----------------------------------------------------------*/
519 /* See if there is an operation on this half connection.
522 /*----------------------------------------------------------*/
525 op = half_con->op_q_first; /* pick up first operation */
527 if (op == (OPERATION)half_con) { /* see if end of circular */
530 * Write message to debugging log
532 if (gdb_Debug & GDB_LOG)
533 fprintf(gdb_log, "Q EMPTY, returning\n");
534 return NOPROGRESS; /* nothing to do on */
535 /* this half session */
539 /*----------------------------------------------------------*/
541 /* Loop until all operations are complete, or until no further
542 /* progress can be made on this one.
546 /* 1) Op contains the operation at the head of the q, or
547 /* else is == half_con, indicating no more operationos
550 /* 2) The operation at the head of the queue is either running
551 /* or continuing. As soon as one completes, it is dequeued.
553 /* Progress is declared whenever an operation newly
554 /* returns OP_COMPLETE, i.e. whenever there has been
555 /* an operation which went from running to complete.
557 /* Done is declared whenever an operation returns anything
558 /* other than complete, indicating that it cannot progress
559 /* further at this time. Loop ends.
561 /* While we're here, mark us busy so we won't try the
562 /* same half_con on reentry.
564 /*----------------------------------------------------------*/
566 done = FALSE; /* this one may be able to */
569 half_con->flags |= HCON_BUSY; /* don't try this hcon */
570 /* while we already doing */
571 /* it. Could happen if */
572 /* we queue new ops */
573 half_con->flags &= ~HCON_PROGRESS; /* gdb_move_data will */
574 /* indicate progress here*/
575 if (gdb_Debug & GDB_LOG)
576 fprintf(gdb_log, "LOOPING\n");
578 /*----------------------------------------------------------*/
580 /* Loop through the operations queued on this half con
581 /* trying to make progress on them, in order.
583 /*----------------------------------------------------------*/
586 op != (OPERATION)half_con) {
588 if (gdb_Debug & GDB_LOG)
589 fprintf(gdb_log, "\top=0x%x status%d...",
592 switch (op->status) {
594 * Operation is at head of queue for first time and has
595 * never been started. Try to start it up.
599 * Call the initialization routine for this operation
601 fcn_result = (*op->fcn.init)(op,half_con,op->arg);
602 if (gdb_Debug & GDB_LOG)
603 fprintf(gdb_log, "init result=%d\n",
606 switch (fcn_result) {
609 op->status = fcn_result;
610 op = g_op_newhead(half_con);
614 op->status = OP_QUEUED;
617 /* important: don't set status on re-queued */
618 /* op as it may already have completed in */
619 /* its second life ! */
620 op = half_con->op_q_first;
621 progress = max(progress, PROGRESS);
624 op->status = fcn_result;
625 done = TRUE; /* could not get done */
629 * Operation is at head of queue and has already
630 * started trying to run. The only reason we could be in this
631 * state is that the last time we tried to do the requested input
632 * or output, all the data could not be moved synchronously.
633 * We therefore try to move some more, and if it all goes now,
634 * we call the continuation routine.
638 * Try to move some more data. If it won't all
639 * go now, we're done with this half connection.
641 * If this is a special listening connection which
642 * has an operation queued trying to do a listen,
643 * then do the listen. Otherwise do an ordinary
646 if (half_con->flags & HCON_PENDING_LISTEN) {
647 if (gdb_listen(half_con)==FALSE) {
648 if (gdb_Debug & GDB_LOG)
649 fprintf(gdb_log, "NO LISTEN\n");
654 if (gdb_move_data(direction, half_con)==FALSE) {
656 if (gdb_Debug & GDB_LOG)
657 fprintf(gdb_log, "NO DATA\n");
661 * The pending data transmission has now completed.
662 * Call the continuation routine for this operation
664 fcn_result = (*op->fcn.cont)(op,half_con,op->arg);
665 if (gdb_Debug & GDB_LOG)
666 fprintf(gdb_log, "cont result=%d\n",
669 switch (fcn_result) {
672 op->status = fcn_result;
673 op = g_op_newhead(half_con);
677 op->status = OP_QUEUED;
680 /* important: don't set status on re-queued */
681 /* op as it may already have completed in */
682 /* its second life ! */
683 op = half_con->op_q_first;
684 progress = max(progress, PROGRESS);
687 op->status = fcn_result;
688 done = TRUE; /* could not get done */
692 * Following cases are all unexpected, at least for the
693 * moment. (See explanation of loop invariants for this while
694 * loop. Give up if they turn up.
697 GDB_GIVEUP("gdb_hcon_progress: found OP_COMPLETE on q")
699 GDB_GIVEUP("gdb_hcon_progress: found OP_CANCELLED on q")
701 GDB_GIVEUP("gdb_hcon_progress: OP_CANCELLING")
703 GDB_GIVEUP("gdb_hcon_progress: Operation is queued, but is not runnable")
707 if (progress == NOPROGRESS && (half_con->flags & HCON_PROGRESS))
710 half_con->flags &= ~HCON_BUSY;
712 if (gdb_Debug & GDB_LOG)
713 fprintf(gdb_log, "hcon_progress: returns %d\n",progress);
715 return progress; /* NOPROGRESS, PROGRESS */
719 /************************************************************************/
723 /* Dequeues the operation at the head of the queue for the
724 /* given half connection and returns the pointer to the
725 /* new head of the queue. If the queue is null, then a pointer
726 /* to the half_con itself is returned. (The lists are
727 /* linked circularly.)
729 /************************************************************************/
735 register OPERATION newhead, oldhead;
738 * Get old and new heads of chain
740 oldhead = hcp->op_q_first;
741 newhead = oldhead->next;
743 * Make sure nobody chained a bad one on us
745 if (newhead == NULL) {
746 if (gdb_Debug & GDB_LOG) {
747 fprintf(gdb_log,"\t\tg_op_newhead: found null link, oldhead = 0x%x newhead=0x%x halfcon=0x%x\n\t\t\t hc->first=0x%x hc->last=0x%x\n",
748 oldhead, newhead, hcp, hcp->op_q_first,
751 GDB_GIVEUP("g_op_newhead: found NULL chain link")
754 * Remove oldhead from chain, fixing up chain pointers
756 newhead->prev = oldhead->prev;
757 hcp->op_q_first = newhead;
760 * Clean up pointers in the newly dequeued operation. This is
761 * just for cleanliness and ease of debugging.
763 oldhead->next = oldhead->prev = NULL;
764 oldhead->halfcon = NULL;
769 /************************************************************************/
773 /* This routine attempts to make further progress on the pending
774 /* level transmission operation pending on this half connection.
775 /* (Presumes that such an operation is pending.) Returns TRUE
776 /* if all the requested data has been moved, else FALSE.
778 /* We assume here that all fd's are set to non-blocking I/O, so
779 /* we can safely try reading and writing until they return 0 bytes.
781 /************************************************************************/
783 #define FIX_BUFFER_POINTERS(hc, count) if (count>0) {hc->next_byte += count; \
784 hc->remaining -= count;}
786 gdb_move_data(direction, hc)
787 int direction; /* CON_INPUT or CON_OUTPUT */
788 struct half_con_data *hc; /* pointer to control struct */
789 /* for this half connection */
791 register HALF_CONNECTION half_con = hc;
792 /* half connection pointer */
793 /* fast copy in register */
794 register fd_set *fdbits; /* the mask we should adjust */
795 /* for this direction */
798 * For safety, in case we're called when nothing is pending.
800 if (half_con->remaining == 0)
803 * Move the data into the user's buffer. In the case of input
804 * data may come first from the stream buffer, then from the socket
807 if (direction == CON_INPUT) {
808 gdb_transfer_from_buffer(half_con);
810 * If remaining is greater than 0, then we emptied
811 * the stream buffer and still weren't done. Try
812 * to read it from the pipe and re-fill the stream
815 if (half_con->remaining) {
816 gdb_read_data_and_buffer(half_con);
819 gdb_write_data(half_con);
822 * The file descriptor masks used for doing selects must be activated
823 * when and only when there is a pending operation trying to use
824 * the connection. Update the masks for this half connection.
826 fdbits = (direction == CON_INPUT)? &gdb_crfds : &gdb_cwfds;
827 if (half_con->remaining >0 && gdb_conok)
828 FD_SET(half_con->fd, fdbits);
830 FD_CLR(half_con->fd, fdbits);
832 return (half_con->remaining == 0);
835 /************************************************************************/
837 /* gdb_transfer_from_buffer
839 /* Given an inbound half connection, satisfy as much as possible
840 /* of desired data from the stream buffer.
842 /************************************************************************/
845 gdb_transfer_from_buffer(hc)
846 register HALF_CONNECTION hc;
848 register int count; /* amount to move */
851 * Figure out how much, if any, we'll be able to do here
853 count = min(hc->remaining, hc->stream_buffer_remaining);
855 return; /* could not satisfy */
856 /* any from buffered data*/
859 * Copy the data, update both stream and data buffer pointers
862 bcopy(hc->stream_buffer_next, hc->next_byte, count);
864 hc->stream_buffer_next += count;
865 hc->stream_buffer_remaining -= count;
866 FIX_BUFFER_POINTERS(hc, count)
870 \f/************************************************************************/
874 /* This routine implements gdb_move_data for an outbound half
877 /************************************************************************/
881 struct half_con_data *hc; /* pointer to control struct */
882 /* for this half connection */
884 register HALF_CONNECTION half_con = hc;
885 /* half connection pointer */
886 /* fast copy in register */
887 register int count; /* number of bytes read */
888 /* or written in latest */
890 fd_set *fdbits; /* the mask we should adjust */
891 /* for this direction */
892 fd_set tst_bits; /* these are used for */
893 /* the select we do prior */
894 /* to reading which tells */
895 /* us whether 0 byte read */
896 /* means empty or closed */
897 int selected; /* TRUE iff select says */
898 /* we should be able to */
902 * Loop writing to the socket until it claims that no more
903 * progress can be made. Note that some versions of Unix report
904 * socket failure by select = 1, write count = 0. To avoid
905 * extra selects, we try the write first, and only do the select/write
906 * sequence if write seems not to be progressing.
909 while(half_con->remaining>0) {
910 count = write(half_con->fd, half_con->next_byte,
911 (int)min(half_con->remaining,
912 GDB_MAX_SOCK_WRITE));
914 FD_SET(half_con->fd,&tst_bits);
915 selected = select(gdb_mfd,
916 (fd_set *)NULL, &tst_bits,
919 if (selected == (-1)) {
927 count = write(half_con->fd, half_con->next_byte,
928 (int)min(half_con->remaining,
929 GDB_MAX_SOCK_WRITE));
933 break; /* no more data available now*/
941 if (errno != EWOULDBLOCK) {
942 gdb_conok = FALSE; /* tell callers */
949 half_con->flags |= HCON_PROGRESS;
950 FIX_BUFFER_POINTERS(half_con, count)
953 * The file descriptor masks used for doing selects must be activated
954 * when and only when there is a pending operation trying to use
955 * the connection. Update the masks for this half connection.
958 if (half_con->remaining >0 && gdb_conok)
959 FD_SET(half_con->fd, fdbits);
961 FD_CLR(half_con->fd, fdbits);
966 /************************************************************************/
969 /* gdb_read_data_and_buffer
971 /* This routine is called only when the half_connection stream
972 /* buffer is known to be empty and the "next-byte" buffer
973 /* has more to be filled in. We try in one read to finish
974 /* off the user's request and at the same time fill the stream
977 /* We assume here that all fd's are set to non-blocking I/O, so
978 /* we can safely try reading and writing until they return 0 bytes.
980 /************************************************************************/
983 gdb_read_data_and_buffer(hc)
984 struct half_con_data *hc; /* pointer to control struct */
985 /* for this half connection */
987 register HALF_CONNECTION half_con = hc;
988 /* half connection pointer */
989 /* fast copy in register */
990 register int count; /* number of bytes read */
991 /* or written in latest */
993 fd_set *fdbits; /* the mask we should adjust */
994 /* for this direction */
995 struct iovec iov[2]; /* we use this to hold */
996 /* pointers to (1) the */
997 /* actual user data buffer */
998 /* and (2) the pipe length */
999 /* pre-read buffer */
1000 int fix_amount; /* amount to adjust */
1001 /* half_con->remaining*/
1003 /*----------------------------------------------------------*/
1005 /* Mark the stream buffer as empty, in case we don't
1006 /* get around to filling it.
1008 /*----------------------------------------------------------*/
1010 half_con -> stream_buffer_next = half_con -> stream_buffer;
1011 half_con -> stream_buffer_remaining = 0;
1013 /*----------------------------------------------------------*/
1015 /* Loop trying to read data from the socket. We scatter
1016 /* first into the user's buffer directly, then into
1017 /* the stream buffer (which helps us save system
1018 /* calls next time around.) We stop either when:
1019 /* socket reports error/no progress or user's buffer is
1022 /*----------------------------------------------------------*/
1025 * Loop until either (1) the connection reported that it could
1026 * not progress any further or (2) the full count has been
1027 * satisfied. Some versions of Unix observe the rule that
1028 * a closed connection, especially when reading, is indicated
1029 * by returning a count of 0 on read when select claims that progress
1030 * can be made. We used to handle this case. Bill Sommerfeld
1031 * has introduced a performance change which leaves that checking
1032 * out in the latest version. To add it back, then ONLY in
1033 * the case where read returned 0, do a select followed by another
1034 * read (the order is important). If we ever run on a system that
1035 * works in this way, we may hang at close time.
1038 while(half_con->remaining>0) {
1040 * First we try a read, and if it works, we believe it
1042 iov[0].iov_base = half_con -> next_byte;
1043 iov[0].iov_len = half_con -> remaining;
1044 iov[1].iov_base = half_con -> stream_buffer;
1045 iov[1].iov_len = half_con -> stream_buffer_length;
1046 count = readv(half_con->fd, iov, 2);
1050 if (errno != EWOULDBLOCK)
1051 gdb_conok = FALSE; /* tell callers that */
1056 if (count == 0) {/* We hit EOF */
1062 * Count is >0, we moved some data. Note, setting of
1063 * stream_buffer_remaining can only be non-zero on last
1064 * time through the loop, because that will be when
1065 * half_con->remaining goes to zero.
1067 half_con->flags |= HCON_PROGRESS;
1068 half_con->stream_buffer_remaining=max(0, count-iov[0].iov_len);
1069 fix_amount = min(count,half_con->remaining);
1070 FIX_BUFFER_POINTERS(half_con, fix_amount);
1074 * The file descriptor masks used for doing selects must be activated
1075 * when and only when there is a pending operation trying to use
1076 * the connection. Update the masks for this half connection.
1078 fdbits = &gdb_crfds;
1079 if (half_con->remaining >0)
1080 FD_SET(half_con->fd, fdbits);
1082 FD_CLR(half_con->fd, fdbits);
1087 /************************************************************************/
1089 /* gdb_receive_data (gdb_receive_data)
1091 /* This routine is called by an init or continuation routine to
1092 /* request that a specified amount of data be read, without
1093 /* blocking, on the supplied connection. This routine returns
1094 /* OP_COMPLETE if the entire read completed synchronously,
1095 /* or OP_RUNNING if the read remains ongoing or is cancelling
1096 /* due to error on the socket.
1098 /************************************************************************/
1101 gdb_receive_data(half_con, ptr, len)
1102 HALF_CONNECTION half_con; /* read on this connection*/
1103 char *ptr; /* put first byte here */
1104 int len; /* number of bytes to read */
1107 * Fill in the initial state of the attempted receive
1109 half_con->remaining = len;
1110 half_con->next_byte = ptr;
1113 * Now see if we can make some progress on this read, possibly
1114 * even completing it synchronously. Return appropriate
1115 * result to our caller. Note: errors are reflected as OP_RUNNING
1116 * with global variable gdb_cnok set to FALSE.
1118 if(gdb_move_data(CON_INPUT, half_con))
1124 /************************************************************************/
1126 /* gdb_send_data (gdb_send_data)
1128 /* This routine is called by an init or continuation routine to
1129 /* request that a specified amount of data be written, without
1130 /* blocking, on the supplied connection. This routine returns
1131 /* OP_COMPLETE if the entire write completed synchronously,
1132 /* or OP_RUNNING if the output remains ongoing or there was an error.
1134 /************************************************************************/
1137 gdb_send_data(half_con, ptr, len)
1138 HALF_CONNECTION half_con; /* write on this connection*/
1139 char *ptr; /* put first byte here */
1140 int len; /* number of bytes to read */
1144 * Fill in the initial state of the attempted receive
1146 half_con->remaining = len;
1147 half_con->next_byte = ptr;
1150 * Now see if we can make some progress on this read, possibly
1151 * even completing it synchronously. Return appropriate
1152 * result to our caller.
1154 if(gdb_move_data(CON_OUTPUT, half_con))
1160 /************************************************************************/
1162 /* gdb_start_a_listen
1164 /* This routine is called by an init or continuation routine to
1165 /* request that a connection be done. This routine returns
1166 /* OP_COMPLETE if the accept completed synchronously,
1167 /* or OP_RUNNING if the output remains ongoing or there was an error.
1169 /************************************************************************/
1172 gdb_start_a_listen(half_con, otherside, lenp, fdp)
1173 HALF_CONNECTION half_con; /* write on this connection*/
1174 char *otherside; /* put first byte here */
1175 int *lenp; /* number of bytes to read */
1180 * Fill in the initial state of the attempted accept
1182 half_con->accepted_len = lenp;
1183 half_con->next_byte = otherside;
1184 half_con->accepted_fdp = fdp;
1187 * Now see if we can make some progress on this read, possibly
1188 * even completing it synchronously. Return appropriate
1189 * result to our caller.
1191 if(gdb_listen(half_con))
1197 /************************************************************************/
1199 /* gdb_listen (gdb_listen)
1201 /* This routine is called from gdb_start_a_listen or hcon_progress to attempt
1202 /* to continue making progress in accepting a connection on a
1203 /* listening connection.
1205 /************************************************************************/
1209 struct half_con_data *hc; /* pointer to control struct */
1210 /* for this half connection */
1212 register HALF_CONNECTION half_con = hc;
1213 /* half connection pointer */
1214 /* fast copy in register */
1218 half_con->flags &= ~HCON_PENDING_LISTEN;/* in case we succeed */
1221 * The first implementatin of this used to do a select to make sure
1222 * that the accept would not block. Bill Sommerfeld has changed this
1223 * to non-blocking I/O, so the following code is commented out.
1227 FD_SET(half_con->fd,&tst_bits);
1228 selected = select(gdb_mfd,&tst_bits, (fd_set *)NULL, (fd_set *)NULL,
1231 * If selected==(-1), then we know there's something
1232 * wrong with the socket
1234 if (selected == (-1)) {
1239 * if selected==0, then we know accept won't do anything, so
1242 if (selected == 0) {
1243 half_con->flags |= HCON_PENDING_LISTEN;
1244 FD_SET(half_con->fd, &gdb_crfds); /* we'll be looking for */
1245 /* this whenever we select*/
1249 * Selected is >0. The accept SHOULD not hang.
1254 * Here is Bill's non-blocking implementation
1256 *(half_con->accepted_fdp) = accept(half_con->fd,
1257 (struct sockaddr *)half_con->next_byte,
1258 half_con->accepted_len);
1260 * See whether the accept succeeded
1262 if (*(half_con->accepted_fdp) < 0) {
1263 if (errno != EWOULDBLOCK) {
1264 gdb_conok = FALSE; /* error will be returned */
1265 /* in shut-down listening con*/
1267 half_con->flags |= HCON_PENDING_LISTEN;
1268 FD_SET(half_con->fd, &gdb_crfds);
1272 FD_CLR(half_con->fd, &gdb_crfds); /* don't select on this */