7 static char *rcsid_gdb_trans2_c = "$Header$";
11 /************************************************************************
15 * GDB - Data Transport Services Routines (Part 2)
17 * Author: Noah Mendelsohn
18 * Copyright: 1986 MIT Project Athena
19 * For copying and distribution information, please see
20 * the file <mit-copyright.h>.
23 * These routines implement layer 6 of the Client Library
24 * Specification of the GDB system, as well as the facilities
25 * outlined in the GDB Protocol Specification. Part 2 of 2.
27 * Some of the routines specified are actually implemented as
28 * macros defined in gdb.h.
30 ************************************************************************/
32 #include <mit-copyright.h>
33 #include <sys/types.h>
38 #include <sys/socket.h>
40 extern int errno; /* Unix error slot */
43 * The following values are returned by g_con_progress
45 #define NOPROGRESS 0 /* nothing happened on this */
46 /* connection--must be 0*/
47 #define PROGRESS 1 /* connection has progressed */
48 #define COMPLETE 2 /* an operation has */
49 /* completed on this con */
51 /************************************************************************/
53 /* queue_operation(queue_operation)
55 /* Add an operation to the queue for a given connection, and
56 /* then allows all connections to progress. Returns the last
57 /* known status of the operation.
59 /************************************************************************/
62 queue_operation(con, direction, op)
67 register HALF_CONNECTION hcon = (direction==CON_INPUT)?(&(con->in)):
69 GDB_CHECK_CON(con, "queue_operation")
71 * Write message to debugging log
73 if (gdb_Debug & GDB_LOG)
74 fprintf(gdb_log, "op queued: con=0x%x dir=%s op=0x%x Q was %s empty\n",
75 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
76 op, (hcon->op_q_first == (OPERATION)hcon)?"":"not");
78 * Make sure connection is up
80 if (con->status != CON_UP) {
81 op->status = OP_CANCELLED;
82 if (gdb_Debug & GDB_LOG)
83 fprintf(gdb_log, "\nop NOT queued\n");
88 * Put the new operation at the end of the queue
90 op->prev = hcon->op_q_last;
91 op->next = (OPERATION)hcon;
92 hcon->op_q_last->next = op;
97 op->status = OP_QUEUED;
101 * Force progress on this connection
103 (void) g_con_progress(con - gdb_cons);
105 * Con_select with notime is used here as a kind of fudge for
106 * doing a fastprogress with a select built in before it.
108 (void) con_select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0,
109 &gdb_notime);/* XXX */
111 * Return the last known status of the operation
115 /************************************************************************/
119 /* This routine may be called from an init or continuation routine
120 /* to cause the current operation to be requeued on a new connection.
121 /* The init routine field ofthe operation should be properly set to
122 /* indicate the routine to receive control when the operation actually
123 /* runs on the new connection. The caller of this routine is
124 /* responsible for returning the status OP_REQUEUED to its caller.
126 /* This routine returns the status of the newly queued operation.
127 /* Note, however, that even if this operation returns the status
128 /* CANCELLED, the operation itself may not continue to execute
129 /* on the old connection and it should return the status OP_REQUEUED,
130 /* NOT OP_CANCELLED (at least in this implementation.)
132 /************************************************************************/
135 requeue_operation(con, direction, op)
141 * Make sure the connection supplied is a legal one
143 GDB_CHECK_CON(con, "requeue_operation")
145 * Write message to debugging log
147 if (gdb_Debug & GDB_LOG)
148 fprintf(gdb_log, "op requeued: new con=0x%x dir=%s op=0x%x\n",
149 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
152 * Dequeue the operation from its old half connection
154 (void) g_op_newhead(op->halfcon);
157 * Now queue it on the new one
159 return queue_operation(con, direction, op);
162 /************************************************************************/
166 /* Sticks a new operation in ahead of the current one and runs it
167 /* on the current connection. May be called only from an init or
168 /* continuation routine. The old operation must have completely
169 /* prepared the descriptor for the new operation, i.e. it should
170 /* be in the same state as it would be for a call to queue_operation.
171 /* g_preempt_me makes it possible for operations to be built by
172 /* composition of other smaller operations, since newop runs, in
173 /* a sense, as a subroutine of oldop. opdop must (1) reset its
174 /* initialization routine to be a routine to be called when newop
175 /* completes or cancels and (2) return the status OP_PREEMPTED to
178 /************************************************************************/
181 g_preempt_me(oldop, newop)
185 register OPERATION old=oldop, new=newop;
186 register HALF_CONNECTION hc = old->halfcon;
189 * Write message to debugging log
191 if (gdb_Debug & GDB_LOG)
192 fprintf(gdb_log, "op preempted: halfcon=0x%x oldop=0x%x newop=0x%x\n",
195 * link in the new operation
198 hc->op_q_first = new;
199 new->prev = (OPERATION)hc;
202 * Set the status of the new operation
204 new->status = OP_QUEUED;
207 * Change the status of the old operation (one could argue that
208 * this should be done in gdb_hcon_progress after the return code
211 old->status = OP_QUEUED;
217 /************************************************************************/
221 /* This routine should be called whenever it is suspected that
222 /* progress can be made on any connection. This routine will
223 /* cause all connections to proceed as far as they can without
224 /* blocking, and will make a best effort to avoid long blocks.
225 /* This routine MAY retain control for long periods when sustained
226 /* progress is possible, but it will not knowingly hang.
228 /* Returns: number of connections on which OPERATIONS have
229 /* COMPLETED (not just progressed).
231 /************************************************************************/
237 register int i; /* index to available */
239 register int return_value = 0; /* the value we return */
240 int rc; /* short term storage for */
242 int progress_made; /* true when some con */
243 /* made progress during */
244 /* latest pass through list */
245 int complete_map[GDB_MAX_CONNECTIONS]; /* indicates whether a */
246 /* transmission operation */
247 /* is newly complete on */
248 /* corresponding connection */
249 /* 1 if yes else 0 */
250 int maxcon = gdb_mcons; /* gdb_mcons may change */
251 /* out from under us if */
252 /* connections break. This */
253 /* is the initial value. */
256 * Zero out the completion map for all connections.
258 for (i=0; i<maxcon; i++)
262 * Make repeated passes through all the fd's until a pass is made
263 * in which none makes any progress. This logic is important,
264 * because it catches the case where A is blocked, B makes progress,
265 * and A unblocks during the period where B is progressing.
269 progress_made = FALSE;
270 for (i=0; i<gdb_mcons; i++) {
271 if (rc = g_con_progress(i)) { /* note: NOPROGRESS==0 */
272 progress_made = TRUE;
277 } while (progress_made);
280 * We've gone as far as we can, now find out how many connections
281 * have had operations complete.
283 for (i=0; i<maxcon; i++)
284 return_value += complete_map[i];
290 /************************************************************************/
294 /* Similar to gdb_progress, but this routine attempts progress
295 /* only on those connections which have already shown themselves
296 /* to be ready for activity by a prior select. This is safe to do
297 /* when (1) the only activity we are interested in is that related
298 /* to ongoing I/O and (2) a select was recently done to set the
299 /* last_c.fds flags. Condition (1) is violated in the case where
300 /* an operation may be newly at the head of a queue and its init
301 /* routine may not have had a chance to run. Condition (2) is violated
302 /* when we are entering after having done significant computation.
304 /* This routine was introduced by Bill Sommerfeld after profiling
305 /* revealed that unnecessary attempts to progress on quiescent
306 /* sockets were causing excessive overhead in the system. I am
307 /* still suspicious that this routine may be getting called in
308 /* places where a full gdb_progress is needed. e.g. I'm not
309 /* sure its use in op_select is entirely safe.
311 /************************************************************************/
318 for (i=0; i<gdb_mcons; i++) {
319 register CONNECTION con = &gdb_cons[i];
320 register int infd = gdb_cons[i].in.fd;
321 register int outfd = gdb_cons[i].out.fd;
323 if(connection_status(con) != CON_UP)
327 if ((!(con->in.flags&HCON_UNUSED))&&
328 ((con->in.stream_buffer_remaining > 0)
329 || FD_ISSET(infd, &last_crfds))) {
330 rc = gdb_hcon_progress(CON_INPUT, &con->in);
332 g_stop_with_errno(con);
338 if ((!(con->out.flags&HCON_UNUSED))&&
339 (FD_ISSET(outfd, &last_cwfds))) {
340 rc = gdb_hcon_progress(CON_OUTPUT, &con->out);
342 g_stop_with_errno(con);
350 * We've gone as far as we can, now find out how many connections
351 * have had operations complete.
357 /************************************************************************/
361 /* Make as much progress as possible on the specified connection.
362 /* Returns NOPROGRESS if no bytes moved on either half connection,
363 /* PROGRESS, if some moved and no operations completed, or COMPLETE if
364 /* any of the operations completed. Note that each connection
365 /* consists of two half connections, and we must make each of them
366 /* progress as far as possible.
368 /* The nest here starts getting so deep that it's hard to pass state
369 /* around efficiently. We use a single global variable, gdb_conok,
370 /* to indicate whether the connection we're working on now has died.
371 /* The move data routines set this to FALSE whenever there is a
372 /* fatal error on a connection. We check it, and do a proper
373 /* sever on the connection if it seems to be in trouble.
375 /************************************************************************/
379 g_con_progress(con_id)
380 int con_id; /* index of this connection */
381 /* in the connection desc. */
384 register CONNECTION con= (&gdb_cons[con_id]);
385 /* pointer to the connection */
387 register int progress = NOPROGRESS;
388 register int live = TRUE; /* true when we've seen */
389 /* enough to make sure we */
390 /* want to go around again*/
393 * Check status of connection-if it's not running, then just return.
395 if (con->status != CON_UP)
398 * Repeatedly make progress on each half connection until both
399 * are idle. Important to keep trying as one may become active
400 * while the other is progressing.
403 gdb_conok = TRUE; /* this gets set to FALSE */
404 /* for fatal I/O errors */
405 /* there may be a timing */
406 /* window here in use of */
407 /* HCON_BUSY. Also: it is */
408 /* essential that errno */
409 /* remain valid after conok */
412 live = FALSE; /* until proven otherwise */
414 * make progress on the input connection note that following
415 * logic depends on NOPROGRESS being 0
417 if (rc = gdb_hcon_progress(CON_INPUT, &con->in)) {
419 progress = max(rc, progress);
422 * See if connection has died
425 g_stop_with_errno(con);
426 return COMPLETE; /* dying connection always */
427 /* implies that the */
428 /* operation at the head */
429 /* of the queue completed */
432 * make progress on the output connection
434 if (rc = gdb_hcon_progress(CON_OUTPUT, &con->out)) {
436 progress = max(rc, progress);
439 * See if connection has died
442 g_stop_with_errno(con);
451 /************************************************************************/
455 /* Allows a specified half-connection to progress as much as possible,
456 /* and returns true iff at least one operation is newly completed.
458 /************************************************************************/
461 gdb_hcon_progress(direction, hc)
462 int direction; /* CON_INPUT or CON_OUTPUT */
463 struct half_con_data *hc; /* pointer to control struct */
464 /* for this half connection */
466 HALF_CONNECTION half_con = hc;
467 /* half connection pointer */
468 /* fast copy in register */
469 register OPERATION op; /* current operation on this */
470 /* half connection */
471 int progress = NOPROGRESS; /* can indicate any progress */
472 /* on the half con or */
473 /* whether any operations */
475 int done; /* true when no more progress*/
477 int fcn_result; /* result of latest init or */
478 /* continue function */
481 * Write message to debugging log
483 if (gdb_Debug & GDB_LOG)
484 fprintf(gdb_log, "hcon_progress: halfcon=0x%x dir=%s ",
485 half_con, (direction==CON_INPUT)?"INPUT":"OUTPUT");
487 /*----------------------------------------------------------*/
489 /* See if we are being re-entered and are already working
490 /* on this half_con. If so, return right now.
492 /*----------------------------------------------------------*/
494 if (half_con->flags & HCON_BUSY) {
496 * Write message to debugging log
498 if (gdb_Debug & GDB_LOG)
499 fprintf(gdb_log, "BUSY, returning\n");
503 /*----------------------------------------------------------*/
505 /* See if there is an operation on this half connection.
508 /*----------------------------------------------------------*/
511 op = half_con->op_q_first; /* pick up first operation */
513 if (op == (OPERATION)half_con) { /* see if end of circular */
516 * Write message to debugging log
518 if (gdb_Debug & GDB_LOG)
519 fprintf(gdb_log, "Q EMPTY, returning\n");
520 return NOPROGRESS; /* nothing to do on */
521 /* this half session */
525 /*----------------------------------------------------------*/
527 /* Loop until all operations are complete, or until no further
528 /* progress can be made on this one.
532 /* 1) Op contains the operation at the head of the q, or
533 /* else is == half_con, indicating no more operationos
536 /* 2) The operation at the head of the queue is either running
537 /* or continuing. As soon as one completes, it is dequeued.
539 /* Progress is declared whenever an operation newly
540 /* returns OP_COMPLETE, i.e. whenever there has been
541 /* an operation which went from running to complete.
543 /* Done is declared whenever an operation returns anything
544 /* other than complete, indicating that it cannot progress
545 /* further at this time. Loop ends.
547 /* While we're here, mark us busy so we won't try the
548 /* same half_con on reentry.
550 /*----------------------------------------------------------*/
552 done = FALSE; /* this one may be able to */
555 half_con->flags |= HCON_BUSY; /* don't try this hcon */
556 /* while we already doing */
557 /* it. Could happen if */
558 /* we queue new ops */
559 half_con->flags &= ~HCON_PROGRESS; /* gdb_move_data will */
560 /* indicate progress here*/
561 if (gdb_Debug & GDB_LOG)
562 fprintf(gdb_log, "LOOPING\n");
564 /*----------------------------------------------------------*/
566 /* Loop through the operations queued on this half con
567 /* trying to make progress on them, in order.
569 /*----------------------------------------------------------*/
572 op != (OPERATION)half_con) {
574 if (gdb_Debug & GDB_LOG)
575 fprintf(gdb_log, "\top=0x%x status%d...",
578 switch (op->status) {
580 * Operation is at head of queue for first time and has
581 * never been started. Try to start it up.
585 * Call the initialization routine for this operation
587 fcn_result = (*op->fcn.init)(op,half_con,op->arg);
588 if (gdb_Debug & GDB_LOG)
589 fprintf(gdb_log, "init result=%d\n",
592 switch (fcn_result) {
595 op->status = fcn_result;
596 op = g_op_newhead(half_con);
600 op->status = OP_QUEUED;
603 /* important: don't set status on re-queued */
604 /* op as it may already have completed in */
605 /* its second life ! */
606 op = half_con->op_q_first;
607 progress = max(progress, PROGRESS);
610 op->status = fcn_result;
611 done = TRUE; /* could not get done */
615 * Operation is at head of queue and has already
616 * started trying to run. The only reason we could be in this
617 * state is that the last time we tried to do the requested input
618 * or output, all the data could not be moved synchronously.
619 * We therefore try to move some more, and if it all goes now,
620 * we call the continuation routine.
624 * Try to move some more data. If it won't all
625 * go now, we're done with this half connection.
627 * If this is a special listening connection which
628 * has an operation queued trying to do a listen,
629 * then do the listen. Otherwise do an ordinary
632 if (half_con->flags & HCON_PENDING_LISTEN) {
633 if (gdb_listen(half_con)==FALSE) {
634 if (gdb_Debug & GDB_LOG)
635 fprintf(gdb_log, "NO LISTEN\n");
640 if (gdb_move_data(direction, half_con)==FALSE) {
642 if (gdb_Debug & GDB_LOG)
643 fprintf(gdb_log, "NO DATA\n");
647 * The pending data transmission has now completed.
648 * Call the continuation routine for this operation
650 fcn_result = (*op->fcn.cont)(op,half_con,op->arg);
651 if (gdb_Debug & GDB_LOG)
652 fprintf(gdb_log, "cont result=%d\n",
655 switch (fcn_result) {
658 op->status = fcn_result;
659 op = g_op_newhead(half_con);
663 op->status = OP_QUEUED;
666 /* important: don't set status on re-queued */
667 /* op as it may already have completed in */
668 /* its second life ! */
669 op = half_con->op_q_first;
670 progress = max(progress, PROGRESS);
673 op->status = fcn_result;
674 done = TRUE; /* could not get done */
678 * Following cases are all unexpected, at least for the
679 * moment. (See explanation of loop invariants for this while
680 * loop. Give up if they turn up.
683 GDB_GIVEUP("gdb_hcon_progress: found OP_COMPLETE on q")
685 GDB_GIVEUP("gdb_hcon_progress: found OP_CANCELLED on q")
687 GDB_GIVEUP("gdb_hcon_progress: OP_CANCELLING")
689 GDB_GIVEUP("gdb_hcon_progress: Operation is queued, but is not runnable")
693 if (progress == NOPROGRESS && (half_con->flags & HCON_PROGRESS))
696 half_con->flags &= ~HCON_BUSY;
698 if (gdb_Debug & GDB_LOG)
699 fprintf(gdb_log, "hcon_progress: returns %d\n",progress);
701 return progress; /* NOPROGRESS, PROGRESS */
705 /************************************************************************/
709 /* Dequeues the operation at the head of the queue for the
710 /* given half connection and returns the pointer to the
711 /* new head of the queue. If the queue is null, then a pointer
712 /* to the half_con itself is returned. (The lists are
713 /* linked circularly.)
715 /************************************************************************/
721 register OPERATION newhead, oldhead;
724 * Get old and new heads of chain
726 oldhead = hcp->op_q_first;
727 newhead = oldhead->next;
729 * Make sure nobody chained a bad one on us
731 if (newhead == NULL) {
732 if (gdb_Debug & GDB_LOG) {
733 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",
734 oldhead, newhead, hcp, hcp->op_q_first,
737 GDB_GIVEUP("g_op_newhead: found NULL chain link")
740 * Remove oldhead from chain, fixing up chain pointers
742 newhead->prev = oldhead->prev;
743 hcp->op_q_first = newhead;
746 * Clean up pointers in the newly dequeued operation. This is
747 * just for cleanliness and ease of debugging.
749 oldhead->next = oldhead->prev = NULL;
750 oldhead->halfcon = NULL;
755 /************************************************************************/
759 /* This routine attempts to make further progress on the pending
760 /* level transmission operation pending on this half connection.
761 /* (Presumes that such an operation is pending.) Returns TRUE
762 /* if all the requested data has been moved, else FALSE.
764 /* We assume here that all fd's are set to non-blocking I/O, so
765 /* we can safely try reading and writing until they return 0 bytes.
767 /************************************************************************/
769 #define FIX_BUFFER_POINTERS(hc, count) if (count>0) {hc->next_byte += count; \
770 hc->remaining -= count;}
772 gdb_move_data(direction, hc)
773 int direction; /* CON_INPUT or CON_OUTPUT */
774 struct half_con_data *hc; /* pointer to control struct */
775 /* for this half connection */
777 register HALF_CONNECTION half_con = hc;
778 /* half connection pointer */
779 /* fast copy in register */
780 register fd_set *fdbits; /* the mask we should adjust */
781 /* for this direction */
784 * For safety, in case we're called when nothing is pending.
786 if (half_con->remaining == 0)
789 * Move the data into the user's buffer. In the case of input
790 * data may come first from the stream buffer, then from the socket
793 if (direction == CON_INPUT) {
794 gdb_transfer_from_buffer(half_con);
796 * If remaining is greater than 0, then we emptied
797 * the stream buffer and still weren't done. Try
798 * to read it from the pipe and re-fill the stream
801 if (half_con->remaining) {
802 gdb_read_data_and_buffer(half_con);
805 gdb_write_data(half_con);
808 * The file descriptor masks used for doing selects must be activated
809 * when and only when there is a pending operation trying to use
810 * the connection. Update the masks for this half connection.
812 fdbits = (direction == CON_INPUT)? &gdb_crfds : &gdb_cwfds;
813 if (half_con->remaining >0 && gdb_conok)
814 FD_SET(half_con->fd, fdbits);
816 FD_CLR(half_con->fd, fdbits);
818 return (half_con->remaining == 0);
821 /************************************************************************/
823 /* gdb_transfer_from_buffer
825 /* Given an inbound half connection, satisfy as much as possible
826 /* of desired data from the stream buffer.
828 /************************************************************************/
831 gdb_transfer_from_buffer(hc)
832 register HALF_CONNECTION hc;
834 register int count; /* amount to move */
837 * Figure out how much, if any, we'll be able to do here
839 count = min(hc->remaining, hc->stream_buffer_remaining);
841 return; /* could not satisfy */
842 /* any from buffered data*/
845 * Copy the data, update both stream and data buffer pointers
848 memcpy(hc->next_byte, hc->stream_buffer_next, count);
850 hc->stream_buffer_next += count;
851 hc->stream_buffer_remaining -= count;
852 FIX_BUFFER_POINTERS(hc, count)
856 \f/************************************************************************/
860 /* This routine implements gdb_move_data for an outbound half
863 /************************************************************************/
867 struct half_con_data *hc; /* pointer to control struct */
868 /* for this half connection */
870 register HALF_CONNECTION half_con = hc;
871 /* half connection pointer */
872 /* fast copy in register */
873 register int count; /* number of bytes read */
874 /* or written in latest */
876 fd_set *fdbits; /* the mask we should adjust */
877 /* for this direction */
878 fd_set tst_bits; /* these are used for */
879 /* the select we do prior */
880 /* to reading which tells */
881 /* us whether 0 byte read */
882 /* means empty or closed */
883 int selected; /* TRUE iff select says */
884 /* we should be able to */
888 * Loop writing to the socket until it claims that no more
889 * progress can be made. Note that some versions of Unix report
890 * socket failure by select = 1, write count = 0. To avoid
891 * extra selects, we try the write first, and only do the select/write
892 * sequence if write seems not to be progressing.
895 while(half_con->remaining>0) {
896 count = write(half_con->fd, half_con->next_byte,
897 (int)min(half_con->remaining,
898 GDB_MAX_SOCK_WRITE));
900 FD_SET(half_con->fd,&tst_bits);
901 selected = select(gdb_mfd,
902 (fd_set *)NULL, &tst_bits,
905 if (selected == (-1)) {
913 count = write(half_con->fd, half_con->next_byte,
914 (int)min(half_con->remaining,
915 GDB_MAX_SOCK_WRITE));
919 break; /* no more data available now*/
927 if (errno != EWOULDBLOCK) {
928 gdb_conok = FALSE; /* tell callers */
935 half_con->flags |= HCON_PROGRESS;
936 FIX_BUFFER_POINTERS(half_con, count)
939 * The file descriptor masks used for doing selects must be activated
940 * when and only when there is a pending operation trying to use
941 * the connection. Update the masks for this half connection.
944 if (half_con->remaining >0 && gdb_conok)
945 FD_SET(half_con->fd, fdbits);
947 FD_CLR(half_con->fd, fdbits);
952 /************************************************************************/
955 /* gdb_read_data_and_buffer
957 /* This routine is called only when the half_connection stream
958 /* buffer is known to be empty and the "next-byte" buffer
959 /* has more to be filled in. We try in one read to finish
960 /* off the user's request and at the same time fill the stream
963 /* We assume here that all fd's are set to non-blocking I/O, so
964 /* we can safely try reading and writing until they return 0 bytes.
966 /************************************************************************/
969 gdb_read_data_and_buffer(hc)
970 struct half_con_data *hc; /* pointer to control struct */
971 /* for this half connection */
973 register HALF_CONNECTION half_con = hc;
974 /* half connection pointer */
975 /* fast copy in register */
976 register int count; /* number of bytes read */
977 /* or written in latest */
979 fd_set *fdbits; /* the mask we should adjust */
980 /* for this direction */
981 struct iovec iov[2]; /* we use this to hold */
982 /* pointers to (1) the */
983 /* actual user data buffer */
984 /* and (2) the pipe length */
985 /* pre-read buffer */
986 int fix_amount; /* amount to adjust */
987 /* half_con->remaining*/
989 /*----------------------------------------------------------*/
991 /* Mark the stream buffer as empty, in case we don't
992 /* get around to filling it.
994 /*----------------------------------------------------------*/
996 half_con -> stream_buffer_next = half_con -> stream_buffer;
997 half_con -> stream_buffer_remaining = 0;
999 /*----------------------------------------------------------*/
1001 /* Loop trying to read data from the socket. We scatter
1002 /* first into the user's buffer directly, then into
1003 /* the stream buffer (which helps us save system
1004 /* calls next time around.) We stop either when:
1005 /* socket reports error/no progress or user's buffer is
1008 /*----------------------------------------------------------*/
1011 * Loop until either (1) the connection reported that it could
1012 * not progress any further or (2) the full count has been
1013 * satisfied. Some versions of Unix observe the rule that
1014 * a closed connection, especially when reading, is indicated
1015 * by returning a count of 0 on read when select claims that progress
1016 * can be made. We used to handle this case. Bill Sommerfeld
1017 * has introduced a performance change which leaves that checking
1018 * out in the latest version. To add it back, then ONLY in
1019 * the case where read returned 0, do a select followed by another
1020 * read (the order is important). If we ever run on a system that
1021 * works in this way, we may hang at close time.
1024 while(half_con->remaining>0) {
1026 * First we try a read, and if it works, we believe it
1028 iov[0].iov_base = half_con -> next_byte;
1029 iov[0].iov_len = half_con -> remaining;
1030 iov[1].iov_base = half_con -> stream_buffer;
1031 iov[1].iov_len = half_con -> stream_buffer_length;
1032 count = readv(half_con->fd, iov, 2);
1036 if (errno != EWOULDBLOCK)
1037 gdb_conok = FALSE; /* tell callers that */
1042 if (count == 0) {/* We hit EOF */
1048 * Count is >0, we moved some data. Note, setting of
1049 * stream_buffer_remaining can only be non-zero on last
1050 * time through the loop, because that will be when
1051 * half_con->remaining goes to zero.
1053 half_con->flags |= HCON_PROGRESS;
1054 half_con->stream_buffer_remaining=max(0, count-iov[0].iov_len);
1055 fix_amount = min(count,half_con->remaining);
1056 FIX_BUFFER_POINTERS(half_con, fix_amount);
1060 * The file descriptor masks used for doing selects must be activated
1061 * when and only when there is a pending operation trying to use
1062 * the connection. Update the masks for this half connection.
1064 fdbits = &gdb_crfds;
1065 if (half_con->remaining >0)
1066 FD_SET(half_con->fd, fdbits);
1068 FD_CLR(half_con->fd, fdbits);
1073 /************************************************************************/
1075 /* gdb_receive_data (gdb_receive_data)
1077 /* This routine is called by an init or continuation routine to
1078 /* request that a specified amount of data be read, without
1079 /* blocking, on the supplied connection. This routine returns
1080 /* OP_COMPLETE if the entire read completed synchronously,
1081 /* or OP_RUNNING if the read remains ongoing or is cancelling
1082 /* due to error on the socket.
1084 /************************************************************************/
1087 gdb_receive_data(half_con, ptr, len)
1088 HALF_CONNECTION half_con; /* read on this connection*/
1089 char *ptr; /* put first byte here */
1090 int len; /* number of bytes to read */
1093 * Fill in the initial state of the attempted receive
1095 half_con->remaining = len;
1096 half_con->next_byte = ptr;
1099 * Now see if we can make some progress on this read, possibly
1100 * even completing it synchronously. Return appropriate
1101 * result to our caller. Note: errors are reflected as OP_RUNNING
1102 * with global variable gdb_cnok set to FALSE.
1104 if(gdb_move_data(CON_INPUT, half_con))
1110 /************************************************************************/
1112 /* gdb_send_data (gdb_send_data)
1114 /* This routine is called by an init or continuation routine to
1115 /* request that a specified amount of data be written, without
1116 /* blocking, on the supplied connection. This routine returns
1117 /* OP_COMPLETE if the entire write completed synchronously,
1118 /* or OP_RUNNING if the output remains ongoing or there was an error.
1120 /************************************************************************/
1123 gdb_send_data(half_con, ptr, len)
1124 HALF_CONNECTION half_con; /* write on this connection*/
1125 char *ptr; /* put first byte here */
1126 int len; /* number of bytes to read */
1130 * Fill in the initial state of the attempted receive
1132 half_con->remaining = len;
1133 half_con->next_byte = ptr;
1136 * Now see if we can make some progress on this read, possibly
1137 * even completing it synchronously. Return appropriate
1138 * result to our caller.
1140 if(gdb_move_data(CON_OUTPUT, half_con))
1146 /************************************************************************/
1148 /* gdb_start_a_listen
1150 /* This routine is called by an init or continuation routine to
1151 /* request that a connection be done. This routine returns
1152 /* OP_COMPLETE if the accept completed synchronously,
1153 /* or OP_RUNNING if the output remains ongoing or there was an error.
1155 /************************************************************************/
1158 gdb_start_a_listen(half_con, otherside, lenp, fdp)
1159 HALF_CONNECTION half_con; /* write on this connection*/
1160 char *otherside; /* put first byte here */
1161 int *lenp; /* number of bytes to read */
1166 * Fill in the initial state of the attempted accept
1168 half_con->accepted_len = lenp;
1169 half_con->next_byte = otherside;
1170 half_con->accepted_fdp = fdp;
1173 * Now see if we can make some progress on this read, possibly
1174 * even completing it synchronously. Return appropriate
1175 * result to our caller.
1177 if(gdb_listen(half_con))
1183 /************************************************************************/
1185 /* gdb_listen (gdb_listen)
1187 /* This routine is called from gdb_start_a_listen or hcon_progress to attempt
1188 /* to continue making progress in accepting a connection on a
1189 /* listening connection.
1191 /************************************************************************/
1195 struct half_con_data *hc; /* pointer to control struct */
1196 /* for this half connection */
1198 register HALF_CONNECTION half_con = hc;
1199 /* half connection pointer */
1200 /* fast copy in register */
1204 half_con->flags &= ~HCON_PENDING_LISTEN;/* in case we succeed */
1207 * The first implementatin of this used to do a select to make sure
1208 * that the accept would not block. Bill Sommerfeld has changed this
1209 * to non-blocking I/O, so the following code is commented out.
1213 FD_SET(half_con->fd,&tst_bits);
1214 selected = select(gdb_mfd,&tst_bits, (fd_set *)NULL, (fd_set *)NULL,
1217 * If selected==(-1), then we know there's something
1218 * wrong with the socket
1220 if (selected == (-1)) {
1225 * if selected==0, then we know accept won't do anything, so
1228 if (selected == 0) {
1229 half_con->flags |= HCON_PENDING_LISTEN;
1230 FD_SET(half_con->fd, &gdb_crfds); /* we'll be looking for */
1231 /* this whenever we select*/
1235 * Selected is >0. The accept SHOULD not hang.
1240 * Here is Bill's non-blocking implementation
1242 *(half_con->accepted_fdp) = accept(half_con->fd,
1243 (struct sockaddr *)half_con->next_byte,
1244 half_con->accepted_len);
1246 * See whether the accept succeeded
1248 if (*(half_con->accepted_fdp) < 0) {
1249 if (errno != EWOULDBLOCK) {
1250 gdb_conok = FALSE; /* error will be returned */
1251 /* in shut-down listening con*/
1253 half_con->flags |= HCON_PENDING_LISTEN;
1254 FD_SET(half_con->fd, &gdb_crfds);
1258 FD_CLR(half_con->fd, &gdb_crfds); /* don't select on this */