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
38 /* For copying and distribution information, please see
39 /* the file <mit-copyright.h>.
42 /* These routines implement layer 6 of the Client Library
43 /* Specification of the GDB system, as well as the facilities
44 /* outlined in the GDB Protocol Specification. Part 2 of 2.
46 /* Some of the routines specified are actually implemented as
47 /* macros defined in gdb.h.
49 /************************************************************************/
51 #include <mit-copyright.h>
52 #include <sys/types.h>
57 #include <sys/socket.h>
58 extern int errno; /* Unix error slot */
61 * The following values are returned by g_con_progress
63 #define NOPROGRESS 0 /* nothing happened on this */
64 /* connection--must be 0*/
65 #define PROGRESS 1 /* connection has progressed */
66 #define COMPLETE 2 /* an operation has */
67 /* completed on this con */
69 /************************************************************************/
71 /* queue_operation(queue_operation)
73 /* Add an operation to the queue for a given connection, and
74 /* then allows all connections to progress. Returns the last
75 /* known status of the operation.
77 /************************************************************************/
80 queue_operation(con, direction, op)
85 register HALF_CONNECTION hcon = (direction==CON_INPUT)?(&(con->in)):
87 GDB_CHECK_CON(con, "queue_operation")
89 * Write message to debugging log
91 if (gdb_Debug & GDB_LOG)
92 fprintf(gdb_log, "op queued: con=0x%x dir=%s op=0x%x Q was %s empty\n",
93 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
94 op, (hcon->op_q_first == (OPERATION)hcon)?"":"not");
96 * Make sure connection is up
98 if (con->status != CON_UP) {
99 op->status = OP_CANCELLED;
100 if (gdb_Debug & GDB_LOG)
101 fprintf(gdb_log, "\nop NOT queued\n");
106 * Put the new operation at the end of the queue
108 op->prev = hcon->op_q_last;
109 op->next = (OPERATION)hcon;
110 hcon->op_q_last->next = op;
111 hcon->op_q_last = op;
115 op->status = OP_QUEUED;
119 * Force progress on this connection
121 (void) g_con_progress(con - gdb_cons);
123 * Con_select with notime is used here as a kind of fudge for
124 * doing a fastprogress with a select built in before it.
126 (void) con_select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0,
127 &gdb_notime);/* XXX */
129 * Return the last known status of the operation
133 /************************************************************************/
137 /* This routine may be called from an init or continuation routine
138 /* to cause the current operation to be requeued on a new connection.
139 /* The init routine field ofthe operation should be properly set to
140 /* indicate the routine to receive control when the operation actually
141 /* runs on the new connection. The caller of this routine is
142 /* responsible for returning the status OP_REQUEUED to its caller.
144 /* This routine returns the status of the newly queued operation.
145 /* Note, however, that even if this operation returns the status
146 /* CANCELLED, the operation itself may not continue to execute
147 /* on the old connection and it should return the status OP_REQUEUED,
148 /* NOT OP_CANCELLED (at least in this implementation.)
150 /************************************************************************/
153 requeue_operation(con, direction, op)
159 * Make sure the connection supplied is a legal one
161 GDB_CHECK_CON(con, "requeue_operation")
163 * Write message to debugging log
165 if (gdb_Debug & GDB_LOG)
166 fprintf(gdb_log, "op requeued: new con=0x%x dir=%s op=0x%x\n",
167 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
170 * Dequeue the operation from its old half connection
172 (void) g_op_newhead(op->halfcon);
175 * Now queue it on the new one
177 return queue_operation(con, direction, op);
180 /************************************************************************/
184 /* Sticks a new operation in ahead of the current one and runs it
185 /* on the current connection. May be called only from an init or
186 /* continuation routine. The old operation must have completely
187 /* prepared the descriptor for the new operation, i.e. it should
188 /* be in the same state as it would be for a call to queue_operation.
189 /* g_preempt_me makes it possible for operations to be built by
190 /* composition of other smaller operations, since newop runs, in
191 /* a sense, as a subroutine of oldop. opdop must (1) reset its
192 /* initialization routine to be a routine to be called when newop
193 /* completes or cancels and (2) return the status OP_PREEMPTED to
196 /************************************************************************/
199 g_preempt_me(oldop, newop)
203 register OPERATION old=oldop, new=newop;
204 register HALF_CONNECTION hc = old->halfcon;
207 * Write message to debugging log
209 if (gdb_Debug & GDB_LOG)
210 fprintf(gdb_log, "op preempted: halfcon=0x%x oldop=0x%x newop=0x%x\n",
213 * link in the new operation
216 hc->op_q_first = new;
217 new->prev = (OPERATION)hc;
220 * Set the status of the new operation
222 new->status = OP_QUEUED;
225 * Change the status of the old operation (one could argue that
226 * this should be done in gdb_hcon_progress after the return code
229 old->status = OP_QUEUED;
235 /************************************************************************/
239 /* This routine should be called whenever it is suspected that
240 /* progress can be made on any connection. This routine will
241 /* cause all connections to proceed as far as they can without
242 /* blocking, and will make a best effort to avoid long blocks.
243 /* This routine MAY retain control for long periods when sustained
244 /* progress is possible, but it will not knowingly hang.
246 /* Returns: number of connections on which OPERATIONS have
247 /* COMPLETED (not just progressed).
249 /************************************************************************/
255 register int i; /* index to available */
257 register int return_value = 0; /* the value we return */
258 int rc; /* short term storage for */
260 int progress_made; /* true when some con */
261 /* made progress during */
262 /* latest pass through list */
263 int complete_map[GDB_MAX_CONNECTIONS]; /* indicates whether a */
264 /* transmission operation */
265 /* is newly complete on */
266 /* corresponding connection */
267 /* 1 if yes else 0 */
268 int maxcon = gdb_mcons; /* gdb_mcons may change */
269 /* out from under us if */
270 /* connections break. This */
271 /* is the initial value. */
274 * Zero out the completion map for all connections.
276 for (i=0; i<maxcon; i++)
280 * Make repeated passes through all the fd's until a pass is made
281 * in which none makes any progress. This logic is important,
282 * because it catches the case where A is blocked, B makes progress,
283 * and A unblocks during the period where B is progressing.
287 progress_made = FALSE;
288 for (i=0; i<gdb_mcons; i++) {
289 if (rc = g_con_progress(i)) { /* note: NOPROGRESS==0 */
290 progress_made = TRUE;
295 } while (progress_made);
298 * We've gone as far as we can, now find out how many connections
299 * have had operations complete.
301 for (i=0; i<maxcon; i++)
302 return_value += complete_map[i];
308 /************************************************************************/
312 /* Similar to gdb_progress, but this routine attempts progress
313 /* only on those connections which have already shown themselves
314 /* to be ready for activity by a prior select. This is safe to do
315 /* when (1) the only activity we are interested in is that related
316 /* to ongoing I/O and (2) a select was recently done to set the
317 /* last_c.fds flags. Condition (1) is violated in the case where
318 /* an operation may be newly at the head of a queue and its init
319 /* routine may not have had a chance to run. Condition (2) is violated
320 /* when we are entering after having done significant computation.
322 /* This routine was introduced by Bill Sommerfeld after profiling
323 /* revealed that unnecessary attempts to progress on quiescent
324 /* sockets were causing excessive overhead in the system. I am
325 /* still suspicious that this routine may be getting called in
326 /* places where a full gdb_progress is needed. e.g. I'm not
327 /* sure its use in op_select is entirely safe.
329 /************************************************************************/
336 for (i=0; i<gdb_mcons; i++) {
337 register CONNECTION con = &gdb_cons[i];
338 register int infd = gdb_cons[i].in.fd;
339 register int outfd = gdb_cons[i].out.fd;
341 if(connection_status(con) != CON_UP)
345 if ((!(con->in.flags&HCON_UNUSED))&&
346 ((con->in.stream_buffer_remaining > 0)
347 || FD_ISSET(infd, &last_crfds))) {
348 rc = gdb_hcon_progress(CON_INPUT, &con->in);
350 g_stop_with_errno(con);
356 if ((!(con->out.flags&HCON_UNUSED))&&
357 (FD_ISSET(outfd, &last_cwfds))) {
358 rc = gdb_hcon_progress(CON_OUTPUT, &con->out);
360 g_stop_with_errno(con);
368 * We've gone as far as we can, now find out how many connections
369 * have had operations complete.
375 /************************************************************************/
379 /* Make as much progress as possible on the specified connection.
380 /* Returns NOPROGRESS if no bytes moved on either half connection,
381 /* PROGRESS, if some moved and no operations completed, or COMPLETE if
382 /* any of the operations completed. Note that each connection
383 /* consists of two half connections, and we must make each of them
384 /* progress as far as possible.
386 /* The nest here starts getting so deep that it's hard to pass state
387 /* around efficiently. We use a single global variable, gdb_conok,
388 /* to indicate whether the connection we're working on now has died.
389 /* The move data routines set this to FALSE whenever there is a
390 /* fatal error on a connection. We check it, and do a proper
391 /* sever on the connection if it seems to be in trouble.
393 /************************************************************************/
397 g_con_progress(con_id)
398 int con_id; /* index of this connection */
399 /* in the connection desc. */
402 register CONNECTION con= (&gdb_cons[con_id]);
403 /* pointer to the connection */
405 register int progress = NOPROGRESS;
406 register int live = TRUE; /* true when we've seen */
407 /* enough to make sure we */
408 /* want to go around again*/
411 * Check status of connection-if it's not running, then just return.
413 if (con->status != CON_UP)
416 * Repeatedly make progress on each half connection until both
417 * are idle. Important to keep trying as one may become active
418 * while the other is progressing.
421 gdb_conok = TRUE; /* this gets set to FALSE */
422 /* for fatal I/O errors */
423 /* there may be a timing */
424 /* window here in use of */
425 /* HCON_BUSY. Also: it is */
426 /* essential that errno */
427 /* remain valid after conok */
430 live = FALSE; /* until proven otherwise */
432 * make progress on the input connection note that following
433 * logic depends on NOPROGRESS being 0
435 if (rc = gdb_hcon_progress(CON_INPUT, &con->in)) {
437 progress = max(rc, progress);
440 * See if connection has died
443 g_stop_with_errno(con);
444 return COMPLETE; /* dying connection always */
445 /* implies that the */
446 /* operation at the head */
447 /* of the queue completed */
450 * make progress on the output connection
452 if (rc = gdb_hcon_progress(CON_OUTPUT, &con->out)) {
454 progress = max(rc, progress);
457 * See if connection has died
460 g_stop_with_errno(con);
469 /************************************************************************/
473 /* Allows a specified half-connection to progress as much as possible,
474 /* and returns true iff at least one operation is newly completed.
476 /************************************************************************/
479 gdb_hcon_progress(direction, hc)
480 int direction; /* CON_INPUT or CON_OUTPUT */
481 struct half_con_data *hc; /* pointer to control struct */
482 /* for this half connection */
484 HALF_CONNECTION half_con = hc;
485 /* half connection pointer */
486 /* fast copy in register */
487 register OPERATION op; /* current operation on this */
488 /* half connection */
489 int progress = NOPROGRESS; /* can indicate any progress */
490 /* on the half con or */
491 /* whether any operations */
493 int done; /* true when no more progress*/
495 int fcn_result; /* result of latest init or */
496 /* continue function */
499 * Write message to debugging log
501 if (gdb_Debug & GDB_LOG)
502 fprintf(gdb_log, "hcon_progress: halfcon=0x%x dir=%s ",
503 half_con, (direction==CON_INPUT)?"INPUT":"OUTPUT");
505 /*----------------------------------------------------------*/
507 /* See if we are being re-entered and are already working
508 /* on this half_con. If so, return right now.
510 /*----------------------------------------------------------*/
512 if (half_con->flags & HCON_BUSY) {
514 * Write message to debugging log
516 if (gdb_Debug & GDB_LOG)
517 fprintf(gdb_log, "BUSY, returning\n");
521 /*----------------------------------------------------------*/
523 /* See if there is an operation on this half connection.
526 /*----------------------------------------------------------*/
529 op = half_con->op_q_first; /* pick up first operation */
531 if (op == (OPERATION)half_con) { /* see if end of circular */
534 * Write message to debugging log
536 if (gdb_Debug & GDB_LOG)
537 fprintf(gdb_log, "Q EMPTY, returning\n");
538 return NOPROGRESS; /* nothing to do on */
539 /* this half session */
543 /*----------------------------------------------------------*/
545 /* Loop until all operations are complete, or until no further
546 /* progress can be made on this one.
550 /* 1) Op contains the operation at the head of the q, or
551 /* else is == half_con, indicating no more operationos
554 /* 2) The operation at the head of the queue is either running
555 /* or continuing. As soon as one completes, it is dequeued.
557 /* Progress is declared whenever an operation newly
558 /* returns OP_COMPLETE, i.e. whenever there has been
559 /* an operation which went from running to complete.
561 /* Done is declared whenever an operation returns anything
562 /* other than complete, indicating that it cannot progress
563 /* further at this time. Loop ends.
565 /* While we're here, mark us busy so we won't try the
566 /* same half_con on reentry.
568 /*----------------------------------------------------------*/
570 done = FALSE; /* this one may be able to */
573 half_con->flags |= HCON_BUSY; /* don't try this hcon */
574 /* while we already doing */
575 /* it. Could happen if */
576 /* we queue new ops */
577 half_con->flags &= ~HCON_PROGRESS; /* gdb_move_data will */
578 /* indicate progress here*/
579 if (gdb_Debug & GDB_LOG)
580 fprintf(gdb_log, "LOOPING\n");
582 /*----------------------------------------------------------*/
584 /* Loop through the operations queued on this half con
585 /* trying to make progress on them, in order.
587 /*----------------------------------------------------------*/
590 op != (OPERATION)half_con) {
592 if (gdb_Debug & GDB_LOG)
593 fprintf(gdb_log, "\top=0x%x status%d...",
596 switch (op->status) {
598 * Operation is at head of queue for first time and has
599 * never been started. Try to start it up.
603 * Call the initialization routine for this operation
605 fcn_result = (*op->fcn.init)(op,half_con,op->arg);
606 if (gdb_Debug & GDB_LOG)
607 fprintf(gdb_log, "init result=%d\n",
610 switch (fcn_result) {
613 op->status = fcn_result;
614 op = g_op_newhead(half_con);
618 op->status = OP_QUEUED;
621 /* important: don't set status on re-queued */
622 /* op as it may already have completed in */
623 /* its second life ! */
624 op = half_con->op_q_first;
625 progress = max(progress, PROGRESS);
628 op->status = fcn_result;
629 done = TRUE; /* could not get done */
633 * Operation is at head of queue and has already
634 * started trying to run. The only reason we could be in this
635 * state is that the last time we tried to do the requested input
636 * or output, all the data could not be moved synchronously.
637 * We therefore try to move some more, and if it all goes now,
638 * we call the continuation routine.
642 * Try to move some more data. If it won't all
643 * go now, we're done with this half connection.
645 * If this is a special listening connection which
646 * has an operation queued trying to do a listen,
647 * then do the listen. Otherwise do an ordinary
650 if (half_con->flags & HCON_PENDING_LISTEN) {
651 if (gdb_listen(half_con)==FALSE) {
652 if (gdb_Debug & GDB_LOG)
653 fprintf(gdb_log, "NO LISTEN\n");
658 if (gdb_move_data(direction, half_con)==FALSE) {
660 if (gdb_Debug & GDB_LOG)
661 fprintf(gdb_log, "NO DATA\n");
665 * The pending data transmission has now completed.
666 * Call the continuation routine for this operation
668 fcn_result = (*op->fcn.cont)(op,half_con,op->arg);
669 if (gdb_Debug & GDB_LOG)
670 fprintf(gdb_log, "cont result=%d\n",
673 switch (fcn_result) {
676 op->status = fcn_result;
677 op = g_op_newhead(half_con);
681 op->status = OP_QUEUED;
684 /* important: don't set status on re-queued */
685 /* op as it may already have completed in */
686 /* its second life ! */
687 op = half_con->op_q_first;
688 progress = max(progress, PROGRESS);
691 op->status = fcn_result;
692 done = TRUE; /* could not get done */
696 * Following cases are all unexpected, at least for the
697 * moment. (See explanation of loop invariants for this while
698 * loop. Give up if they turn up.
701 GDB_GIVEUP("gdb_hcon_progress: found OP_COMPLETE on q")
703 GDB_GIVEUP("gdb_hcon_progress: found OP_CANCELLED on q")
705 GDB_GIVEUP("gdb_hcon_progress: OP_CANCELLING")
707 GDB_GIVEUP("gdb_hcon_progress: Operation is queued, but is not runnable")
711 if (progress == NOPROGRESS && (half_con->flags & HCON_PROGRESS))
714 half_con->flags &= ~HCON_BUSY;
716 if (gdb_Debug & GDB_LOG)
717 fprintf(gdb_log, "hcon_progress: returns %d\n",progress);
719 return progress; /* NOPROGRESS, PROGRESS */
723 /************************************************************************/
727 /* Dequeues the operation at the head of the queue for the
728 /* given half connection and returns the pointer to the
729 /* new head of the queue. If the queue is null, then a pointer
730 /* to the half_con itself is returned. (The lists are
731 /* linked circularly.)
733 /************************************************************************/
739 register OPERATION newhead, oldhead;
742 * Get old and new heads of chain
744 oldhead = hcp->op_q_first;
745 newhead = oldhead->next;
747 * Make sure nobody chained a bad one on us
749 if (newhead == NULL) {
750 if (gdb_Debug & GDB_LOG) {
751 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",
752 oldhead, newhead, hcp, hcp->op_q_first,
755 GDB_GIVEUP("g_op_newhead: found NULL chain link")
758 * Remove oldhead from chain, fixing up chain pointers
760 newhead->prev = oldhead->prev;
761 hcp->op_q_first = newhead;
764 * Clean up pointers in the newly dequeued operation. This is
765 * just for cleanliness and ease of debugging.
767 oldhead->next = oldhead->prev = NULL;
768 oldhead->halfcon = NULL;
773 /************************************************************************/
777 /* This routine attempts to make further progress on the pending
778 /* level transmission operation pending on this half connection.
779 /* (Presumes that such an operation is pending.) Returns TRUE
780 /* if all the requested data has been moved, else FALSE.
782 /* We assume here that all fd's are set to non-blocking I/O, so
783 /* we can safely try reading and writing until they return 0 bytes.
785 /************************************************************************/
787 #define FIX_BUFFER_POINTERS(hc, count) if (count>0) {hc->next_byte += count; \
788 hc->remaining -= count;}
790 gdb_move_data(direction, hc)
791 int direction; /* CON_INPUT or CON_OUTPUT */
792 struct half_con_data *hc; /* pointer to control struct */
793 /* for this half connection */
795 register HALF_CONNECTION half_con = hc;
796 /* half connection pointer */
797 /* fast copy in register */
798 register fd_set *fdbits; /* the mask we should adjust */
799 /* for this direction */
802 * For safety, in case we're called when nothing is pending.
804 if (half_con->remaining == 0)
807 * Move the data into the user's buffer. In the case of input
808 * data may come first from the stream buffer, then from the socket
811 if (direction == CON_INPUT) {
812 gdb_transfer_from_buffer(half_con);
814 * If remaining is greater than 0, then we emptied
815 * the stream buffer and still weren't done. Try
816 * to read it from the pipe and re-fill the stream
819 if (half_con->remaining) {
820 gdb_read_data_and_buffer(half_con);
823 gdb_write_data(half_con);
826 * The file descriptor masks used for doing selects must be activated
827 * when and only when there is a pending operation trying to use
828 * the connection. Update the masks for this half connection.
830 fdbits = (direction == CON_INPUT)? &gdb_crfds : &gdb_cwfds;
831 if (half_con->remaining >0 && gdb_conok)
832 FD_SET(half_con->fd, fdbits);
834 FD_CLR(half_con->fd, fdbits);
836 return (half_con->remaining == 0);
839 /************************************************************************/
841 /* gdb_transfer_from_buffer
843 /* Given an inbound half connection, satisfy as much as possible
844 /* of desired data from the stream buffer.
846 /************************************************************************/
849 gdb_transfer_from_buffer(hc)
850 register HALF_CONNECTION hc;
852 register int count; /* amount to move */
855 * Figure out how much, if any, we'll be able to do here
857 count = min(hc->remaining, hc->stream_buffer_remaining);
859 return; /* could not satisfy */
860 /* any from buffered data*/
863 * Copy the data, update both stream and data buffer pointers
866 bcopy(hc->stream_buffer_next, hc->next_byte, count);
868 hc->stream_buffer_next += count;
869 hc->stream_buffer_remaining -= count;
870 FIX_BUFFER_POINTERS(hc, count)
874 \f/************************************************************************/
878 /* This routine implements gdb_move_data for an outbound half
881 /************************************************************************/
885 struct half_con_data *hc; /* pointer to control struct */
886 /* for this half connection */
888 register HALF_CONNECTION half_con = hc;
889 /* half connection pointer */
890 /* fast copy in register */
891 register int count; /* number of bytes read */
892 /* or written in latest */
894 fd_set *fdbits; /* the mask we should adjust */
895 /* for this direction */
896 fd_set tst_bits; /* these are used for */
897 /* the select we do prior */
898 /* to reading which tells */
899 /* us whether 0 byte read */
900 /* means empty or closed */
901 int selected; /* TRUE iff select says */
902 /* we should be able to */
906 * Loop writing to the socket until it claims that no more
907 * progress can be made. Note that some versions of Unix report
908 * socket failure by select = 1, write count = 0. To avoid
909 * extra selects, we try the write first, and only do the select/write
910 * sequence if write seems not to be progressing.
913 while(half_con->remaining>0) {
914 count = write(half_con->fd, half_con->next_byte,
915 (int)min(half_con->remaining,
916 GDB_MAX_SOCK_WRITE));
918 FD_SET(half_con->fd,&tst_bits);
919 selected = select(gdb_mfd,
920 (fd_set *)NULL, &tst_bits,
923 if (selected == (-1)) {
931 count = write(half_con->fd, half_con->next_byte,
932 (int)min(half_con->remaining,
933 GDB_MAX_SOCK_WRITE));
937 break; /* no more data available now*/
945 if (errno != EWOULDBLOCK) {
946 gdb_conok = FALSE; /* tell callers */
953 half_con->flags |= HCON_PROGRESS;
954 FIX_BUFFER_POINTERS(half_con, count)
957 * The file descriptor masks used for doing selects must be activated
958 * when and only when there is a pending operation trying to use
959 * the connection. Update the masks for this half connection.
962 if (half_con->remaining >0 && gdb_conok)
963 FD_SET(half_con->fd, fdbits);
965 FD_CLR(half_con->fd, fdbits);
970 /************************************************************************/
973 /* gdb_read_data_and_buffer
975 /* This routine is called only when the half_connection stream
976 /* buffer is known to be empty and the "next-byte" buffer
977 /* has more to be filled in. We try in one read to finish
978 /* off the user's request and at the same time fill the stream
981 /* We assume here that all fd's are set to non-blocking I/O, so
982 /* we can safely try reading and writing until they return 0 bytes.
984 /************************************************************************/
987 gdb_read_data_and_buffer(hc)
988 struct half_con_data *hc; /* pointer to control struct */
989 /* for this half connection */
991 register HALF_CONNECTION half_con = hc;
992 /* half connection pointer */
993 /* fast copy in register */
994 register int count; /* number of bytes read */
995 /* or written in latest */
997 fd_set *fdbits; /* the mask we should adjust */
998 /* for this direction */
999 struct iovec iov[2]; /* we use this to hold */
1000 /* pointers to (1) the */
1001 /* actual user data buffer */
1002 /* and (2) the pipe length */
1003 /* pre-read buffer */
1004 int fix_amount; /* amount to adjust */
1005 /* half_con->remaining*/
1007 /*----------------------------------------------------------*/
1009 /* Mark the stream buffer as empty, in case we don't
1010 /* get around to filling it.
1012 /*----------------------------------------------------------*/
1014 half_con -> stream_buffer_next = half_con -> stream_buffer;
1015 half_con -> stream_buffer_remaining = 0;
1017 /*----------------------------------------------------------*/
1019 /* Loop trying to read data from the socket. We scatter
1020 /* first into the user's buffer directly, then into
1021 /* the stream buffer (which helps us save system
1022 /* calls next time around.) We stop either when:
1023 /* socket reports error/no progress or user's buffer is
1026 /*----------------------------------------------------------*/
1029 * Loop until either (1) the connection reported that it could
1030 * not progress any further or (2) the full count has been
1031 * satisfied. Some versions of Unix observe the rule that
1032 * a closed connection, especially when reading, is indicated
1033 * by returning a count of 0 on read when select claims that progress
1034 * can be made. We used to handle this case. Bill Sommerfeld
1035 * has introduced a performance change which leaves that checking
1036 * out in the latest version. To add it back, then ONLY in
1037 * the case where read returned 0, do a select followed by another
1038 * read (the order is important). If we ever run on a system that
1039 * works in this way, we may hang at close time.
1042 while(half_con->remaining>0) {
1044 * First we try a read, and if it works, we believe it
1046 iov[0].iov_base = half_con -> next_byte;
1047 iov[0].iov_len = half_con -> remaining;
1048 iov[1].iov_base = half_con -> stream_buffer;
1049 iov[1].iov_len = half_con -> stream_buffer_length;
1050 count = readv(half_con->fd, iov, 2);
1054 if (errno != EWOULDBLOCK)
1055 gdb_conok = FALSE; /* tell callers that */
1060 if (count == 0) {/* We hit EOF */
1066 * Count is >0, we moved some data. Note, setting of
1067 * stream_buffer_remaining can only be non-zero on last
1068 * time through the loop, because that will be when
1069 * half_con->remaining goes to zero.
1071 half_con->flags |= HCON_PROGRESS;
1072 half_con->stream_buffer_remaining=max(0, count-iov[0].iov_len);
1073 fix_amount = min(count,half_con->remaining);
1074 FIX_BUFFER_POINTERS(half_con, fix_amount);
1078 * The file descriptor masks used for doing selects must be activated
1079 * when and only when there is a pending operation trying to use
1080 * the connection. Update the masks for this half connection.
1082 fdbits = &gdb_crfds;
1083 if (half_con->remaining >0)
1084 FD_SET(half_con->fd, fdbits);
1086 FD_CLR(half_con->fd, fdbits);
1091 /************************************************************************/
1093 /* gdb_receive_data (gdb_receive_data)
1095 /* This routine is called by an init or continuation routine to
1096 /* request that a specified amount of data be read, without
1097 /* blocking, on the supplied connection. This routine returns
1098 /* OP_COMPLETE if the entire read completed synchronously,
1099 /* or OP_RUNNING if the read remains ongoing or is cancelling
1100 /* due to error on the socket.
1102 /************************************************************************/
1105 gdb_receive_data(half_con, ptr, len)
1106 HALF_CONNECTION half_con; /* read on this connection*/
1107 char *ptr; /* put first byte here */
1108 int len; /* number of bytes to read */
1111 * Fill in the initial state of the attempted receive
1113 half_con->remaining = len;
1114 half_con->next_byte = ptr;
1117 * Now see if we can make some progress on this read, possibly
1118 * even completing it synchronously. Return appropriate
1119 * result to our caller. Note: errors are reflected as OP_RUNNING
1120 * with global variable gdb_cnok set to FALSE.
1122 if(gdb_move_data(CON_INPUT, half_con))
1128 /************************************************************************/
1130 /* gdb_send_data (gdb_send_data)
1132 /* This routine is called by an init or continuation routine to
1133 /* request that a specified amount of data be written, without
1134 /* blocking, on the supplied connection. This routine returns
1135 /* OP_COMPLETE if the entire write completed synchronously,
1136 /* or OP_RUNNING if the output remains ongoing or there was an error.
1138 /************************************************************************/
1141 gdb_send_data(half_con, ptr, len)
1142 HALF_CONNECTION half_con; /* write on this connection*/
1143 char *ptr; /* put first byte here */
1144 int len; /* number of bytes to read */
1148 * Fill in the initial state of the attempted receive
1150 half_con->remaining = len;
1151 half_con->next_byte = ptr;
1154 * Now see if we can make some progress on this read, possibly
1155 * even completing it synchronously. Return appropriate
1156 * result to our caller.
1158 if(gdb_move_data(CON_OUTPUT, half_con))
1164 /************************************************************************/
1166 /* gdb_start_a_listen
1168 /* This routine is called by an init or continuation routine to
1169 /* request that a connection be done. This routine returns
1170 /* OP_COMPLETE if the accept completed synchronously,
1171 /* or OP_RUNNING if the output remains ongoing or there was an error.
1173 /************************************************************************/
1176 gdb_start_a_listen(half_con, otherside, lenp, fdp)
1177 HALF_CONNECTION half_con; /* write on this connection*/
1178 char *otherside; /* put first byte here */
1179 int *lenp; /* number of bytes to read */
1184 * Fill in the initial state of the attempted accept
1186 half_con->accepted_len = lenp;
1187 half_con->next_byte = otherside;
1188 half_con->accepted_fdp = fdp;
1191 * Now see if we can make some progress on this read, possibly
1192 * even completing it synchronously. Return appropriate
1193 * result to our caller.
1195 if(gdb_listen(half_con))
1201 /************************************************************************/
1203 /* gdb_listen (gdb_listen)
1205 /* This routine is called from gdb_start_a_listen or hcon_progress to attempt
1206 /* to continue making progress in accepting a connection on a
1207 /* listening connection.
1209 /************************************************************************/
1213 struct half_con_data *hc; /* pointer to control struct */
1214 /* for this half connection */
1216 register HALF_CONNECTION half_con = hc;
1217 /* half connection pointer */
1218 /* fast copy in register */
1222 half_con->flags &= ~HCON_PENDING_LISTEN;/* in case we succeed */
1225 * The first implementatin of this used to do a select to make sure
1226 * that the accept would not block. Bill Sommerfeld has changed this
1227 * to non-blocking I/O, so the following code is commented out.
1231 FD_SET(half_con->fd,&tst_bits);
1232 selected = select(gdb_mfd,&tst_bits, (fd_set *)NULL, (fd_set *)NULL,
1235 * If selected==(-1), then we know there's something
1236 * wrong with the socket
1238 if (selected == (-1)) {
1243 * if selected==0, then we know accept won't do anything, so
1246 if (selected == 0) {
1247 half_con->flags |= HCON_PENDING_LISTEN;
1248 FD_SET(half_con->fd, &gdb_crfds); /* we'll be looking for */
1249 /* this whenever we select*/
1253 * Selected is >0. The accept SHOULD not hang.
1258 * Here is Bill's non-blocking implementation
1260 *(half_con->accepted_fdp) = accept(half_con->fd,
1261 (struct sockaddr *)half_con->next_byte,
1262 half_con->accepted_len);
1264 * See whether the accept succeeded
1266 if (*(half_con->accepted_fdp) < 0) {
1267 if (errno != EWOULDBLOCK) {
1268 gdb_conok = FALSE; /* error will be returned */
1269 /* in shut-down listening con*/
1271 half_con->flags |= HCON_PENDING_LISTEN;
1272 FD_SET(half_con->fd, &gdb_crfds);
1276 FD_CLR(half_con->fd, &gdb_crfds); /* don't select on this */