7 static char *rcsid_test_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 extern int errno; /* Unix error slot */
56 * The following values are returned by g_con_progress
58 #define NOPROGRESS 0 /* nothing happened on this */
59 /* connection--must be 0*/
60 #define PROGRESS 1 /* connection has progressed */
61 #define COMPLETE 2 /* an operation has */
62 /* completed on this con */
64 /************************************************************************/
66 /* queue_operation(queue_operation)
68 /* Add an operation to the queue for a given connection, and
69 /* then allows all connections to progress. Returns the last
70 /* known status of the operation.
72 /************************************************************************/
75 queue_operation(con, direction, op)
80 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;
114 * Force progress on all connections
119 * Return the last known status of the operation
123 /************************************************************************/
127 /* This routine may be called from an init or continuation routine
128 /* to cause the current operation to be requeued on a new connection.
129 /* The init routine field ofthe operation should be properly set to
130 /* indicate the routine to receive control when the operation actually
131 /* runs on the new connection. The caller of this routine is
132 /* responsible for returning the status OP_REQUEUED to its caller.
134 /* This routine returns the status of the newly queued operation.
135 /* Note, however, that even if this operation returns the status
136 /* CANCELLED, the operation itself may not continue to execute
137 /* on the old connection and it should return the status OP_REQUEUED,
138 /* NOT OP_CANCELLED (at least in this implementation.)
140 /************************************************************************/
143 requeue_operation(con, direction, op)
149 * Make sure the connection supplied is a legal one
151 GDB_CHECK_CON(con, "requeue_operation")
153 * Write message to debugging log
155 if (gdb_Debug & GDB_LOG)
156 fprintf(gdb_log, "op requeued: new con=0x%x dir=%s op=0x%x\n",
157 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
160 * Dequeue the operation from its old half connection
162 g_op_newhead(op->halfcon);
165 * Now queue it on the new one
167 return queue_operation(con, direction, op);
170 /************************************************************************/
174 /* Sticks a new operation in ahead of the current one and runs it
175 /* on the current connection. May be called only from an init or
176 /* continuation routine. The old operation must have completely
177 /* prepared the descriptor for the new operation, i.e. it should
178 /* be in the same state as it would be for a call to queue_operation.
179 /* g_preempt_me makes it possible for operations to be built by
180 /* composition of other smaller operations, since newop runs, in
181 /* a sense, as a subroutine of oldop. opdop must (1) reset its
182 /* initialization routine to be a routine to be called when newop
183 /* completes or cancels and (2) return the status OP_PREEMPTED to
186 /************************************************************************/
189 g_preempt_me(oldop, newop)
193 register OPERATION old=oldop, new=newop;
194 register HALF_CONNECTION hc = old->halfcon;
197 * Write message to debugging log
199 if (gdb_Debug & GDB_LOG)
200 fprintf(gdb_log, "op preempted: halfcon=0x%x oldop=0x%x newop=0x%x\n",
203 * link in the new operation
206 hc->op_q_first = new;
207 new->prev = (OPERATION)hc;
210 * Set the status of the new operation
212 new->status = OP_QUEUED;
215 * Change the status of the old operation (one could argue that
216 * this should be done in gdb_hcon_progress after the return code
219 old->status = OP_QUEUED;
225 /************************************************************************/
229 /* This routine should be called whenever it is suspected that
230 /* progress can be made on any connection. This routine will
231 /* cause all connections to proceed as far as they can without
232 /* blocking, and will make a best effort to avoid long blocks.
233 /* This routine MAY retain control for long periods when sustained
234 /* progress is possible, but it will not knowingly hang.
236 /* Returns: number of connections on which OPERATIONS have
237 /* COMPLETED (not just progressed).
239 /************************************************************************/
245 register int i; /* index to available */
247 register int return_value = 0; /* the value we return */
248 int rc; /* short term storage for */
250 int progress_made; /* true when some con */
251 /* made progress during */
252 /* latest pass through list */
253 int complete_map[GDB_MAX_CONNECTIONS]; /* indicates whether a */
254 /* transmission operation */
255 /* is newly complete on */
256 /* corresponding connection */
257 /* 1 if yes else 0 */
258 int maxcon = gdb_mcons; /* gdb_mcons may change */
259 /* out from under us if */
260 /* connections break. This */
261 /* is the initial value. */
264 * Zero out the completion map for all connections.
266 for (i=0; i<maxcon; i++)
270 * Make repeated passes through all the fd's until a pass is made
271 * in which none makes any progress. This logic is important,
272 * because it catches the case where A is blocked, B makes progress,
273 * and A unblocks during the period where B is progressing.
277 progress_made = FALSE;
278 for (i=0; i<gdb_mcons; i++) {
279 if (rc = g_con_progress(i)) { /* note: NOPROGRESS==0 */
280 progress_made = TRUE;
285 } while (progress_made);
288 * We've gone as far as we can, now find out how many connections
289 * have had operations complete.
291 for (i=0; i<maxcon; i++)
292 return_value += complete_map[i];
297 /************************************************************************/
301 /* Make as much progress as possible on the specified connection.
302 /* Returns NOPROGRESS if no bytes moved on either half connection,
303 /* PROGRESS, if some moved and no operations completed, or COMPLETE if
304 /* any of the operations completed. Note that each connection
305 /* consists of two half connections, and we must make each of them
306 /* progress as far as possible.
308 /* The nest here starts getting so deep that it's hard to pass state
309 /* around efficiently. We use a single global variable, gdb_conok,
310 /* to indicate whether the connection we're working on now has died.
311 /* The move data routines set this to FALSE whenever there is a
312 /* fatal error on a connection. We check it, and do a proper
313 /* sever on the connection if it seems to be in trouble.
315 /************************************************************************/
319 g_con_progress(con_id)
320 int con_id; /* index of this connection */
321 /* in the connection desc. */
324 register CONNECTION con= (&gdb_cons[con_id]);
325 /* pointer to the connection */
327 register int progress = NOPROGRESS;
328 register int live = TRUE; /* true when we've seen */
329 /* enough to make sure we */
330 /* want to go around again*/
333 * Check status of connection-if it's not running, then just return.
335 if (con->status != CON_UP)
338 * Repeatedly make progress on each half connection until both
339 * are idle. Important to keep trying as one may become active
340 * while the other is progressing.
343 gdb_conok = TRUE; /* this gets set to FALSE */
344 /* for fatal I/O errors */
345 /* there may be a timing */
346 /* window here in use of */
349 live = FALSE; /* until proven otherwise */
351 * make progress on the input connection note that following
352 * logic depends on NOPROGRESS being 0
354 if (rc = gdb_hcon_progress(CON_INPUT, &con->in)) {
356 progress = max(rc, progress);
359 * See if connection has died
362 g_stop_connection(con);
363 return COMPLETE; /* dying connection always */
364 /* implies that the */
365 /* operation at the head */
366 /* of the queue completed */
369 * make progress on the output connection
371 if (rc = gdb_hcon_progress(CON_OUTPUT, &con->out)) {
373 progress = max(rc, progress);
376 * See if connection has died
379 g_stop_connection(con);
388 /************************************************************************/
392 /* Allows a specified half-connection to progress as much as possible,
393 /* and returns true iff at least one operation is newly completed.
395 /************************************************************************/
398 gdb_hcon_progress(direction, hc)
399 int direction; /* CON_INPUT or CON_OUTPUT */
400 struct half_con_data *hc; /* pointer to control struct */
401 /* for this half connection */
403 HALF_CONNECTION half_con = hc;
404 /* half connection pointer */
405 /* fast copy in register */
406 register OPERATION op; /* current operation on this */
407 /* half connection */
408 int progress = NOPROGRESS; /* can indicate any progress */
409 /* on the half con or */
410 /* whether any operations */
412 int done; /* true when no more progress*/
414 int fcn_result; /* result of latest init or */
415 /* continue function */
418 * Write message to debugging log
420 if (gdb_Debug & GDB_LOG)
421 fprintf(gdb_log, "hcon_progress: halfcon=0x%x dir=%s ",
422 half_con, (direction==CON_INPUT)?"INPUT":"OUTPUT");
424 /*----------------------------------------------------------*/
426 /* See if we are being re-entered and are already working
427 /* on this half_con. If so, return right now.
429 /*----------------------------------------------------------*/
431 if (half_con->flags & HCON_BUSY) {
433 * Write message to debugging log
435 if (gdb_Debug & GDB_LOG)
436 fprintf(gdb_log, "BUSY, returning\n");
440 /*----------------------------------------------------------*/
442 /* See if there is an operation on this half connection.
445 /*----------------------------------------------------------*/
448 op = half_con->op_q_first; /* pick up first operation */
450 if (op == (OPERATION)half_con) { /* see if end of circular */
453 * Write message to debugging log
455 if (gdb_Debug & GDB_LOG)
456 fprintf(gdb_log, "Q EMPTY, returning\n");
457 return NOPROGRESS; /* nothing to do on */
458 /* this half session */
462 /*----------------------------------------------------------*/
464 /* Loop until all operations are complete, or until no further
465 /* progress can be made on this one.
469 /* 1) Op contains the operation at the head of the q, or
470 /* else is == half_con, indicating no more operationos
473 /* 2) The operation at the head of the queue is either running
474 /* or continuing. As soon as one completes, it is dequeued.
476 /* Progress is declared whenever an operation newly
477 /* returns OP_COMPLETE, i.e. whenever there has been
478 /* an operation which went from running to complete.
480 /* Done is declared whenever an operation returns anything
481 /* other than complete, indicating that it cannot progress
482 /* further at this time. Loop ends.
484 /* While we're here, mark us busy so we won't try the
485 /* same half_con on reentry.
487 /*----------------------------------------------------------*/
489 done = FALSE; /* this one may be able to */
492 half_con->flags |= HCON_BUSY; /* don't try this hcon */
493 /* while we already doing */
494 /* it. Could happen if */
495 /* we queue new ops */
496 half_con->flags &= ~HCON_PROGRESS; /* gdb_move_data will */
497 /* indicate progress here*/
498 if (gdb_Debug & GDB_LOG)
499 fprintf(gdb_log, "LOOPING\n");
501 /*----------------------------------------------------------*/
503 /* Loop through the operations queued on this half con
504 /* trying to make progress on them, in order.
506 /*----------------------------------------------------------*/
509 op != (OPERATION)half_con) {
511 if (gdb_Debug & GDB_LOG)
512 fprintf(gdb_log, "\top=0x%x status%d...",
515 switch (op->status) {
517 * Operation is at head of queue for first time and has
518 * never been started. Try to start it up.
522 * Call the initialization routine for this operation
524 fcn_result = (*op->fcn.init)(op,half_con,op->arg);
525 if (gdb_Debug & GDB_LOG)
526 fprintf(gdb_log, "init result=%d\n",
529 switch (fcn_result) {
532 op->status = fcn_result;
533 op = g_op_newhead(half_con);
537 op->status = OP_QUEUED;
540 /* important: don't set status on re-queued */
541 /* op as it may already have completed in */
542 /* its second life ! */
543 op = half_con->op_q_first;
544 progress = max(progress, PROGRESS);
547 op->status = fcn_result;
548 done = TRUE; /* could not get done */
552 * Operation is at head of queue and has already
553 * started trying to run. The only reason we could be in this
554 * state is that the last time we tried to do the requested input
555 * or output, all the data could not be moved synchronously.
556 * We therefore try to move some more, and if it all goes now,
557 * we call the continuation routine.
561 * Try to move some more data. If it won't all
562 * go now, we're done with this half connection.
564 * If this is a special listening connection which
565 * has an operation queued trying to do a listen,
566 * then do the listen. Otherwise do an ordinary
569 if (half_con->flags & HCON_PENDING_LISTEN) {
570 if (gdb_listen(half_con)==FALSE) {
571 if (gdb_Debug & GDB_LOG)
572 fprintf(gdb_log, "NO LISTEN\n");
577 if (gdb_move_data(direction, half_con)==FALSE) {
579 if (gdb_Debug & GDB_LOG)
580 fprintf(gdb_log, "NO DATA\n");
584 * The pending data transmission has now completed.
585 * Call the continuation routine for this operation
587 fcn_result = (*op->fcn.cont)(op,half_con,op->arg);
588 if (gdb_Debug & GDB_LOG)
589 fprintf(gdb_log, "cont 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 * Following cases are all unexpected, at least for the
616 * moment. (See explanation of loop invariants for this while
617 * loop. Give up if they turn up.
620 GDB_GIVEUP("gdb_hcon_progress: found OP_COMPLETE on q")
622 GDB_GIVEUP("gdb_hcon_progress: found OP_CANCELLED on q")
624 GDB_GIVEUP("gdb_hcon_progress: OP_CANCELLING")
626 GDB_GIVEUP("gdb_hcon_progress: Operation is queued, but is not runnable")
630 if (progress == NOPROGRESS && (half_con->flags & HCON_PROGRESS))
633 half_con->flags &= ~HCON_BUSY;
635 if (gdb_Debug & GDB_LOG)
636 fprintf(gdb_log, "hcon_progress: returns %d\n",progress);
638 return progress; /* NOPROGRESS, PROGRESS */
642 /************************************************************************/
646 /* Dequeues the operation at the head of the queue for the
647 /* given half connection and returns the pointer to the
648 /* new head of the queue. If the queue is null, then a pointer
649 /* to the half_con itself is returned. (The lists are
650 /* linked circularly.)
652 /************************************************************************/
658 register OPERATION newhead, oldhead;
661 * Get old and new heads of chain
663 oldhead = hcp->op_q_first;
664 newhead = oldhead->next;
666 * Make sure nobody chained a bad one on us
668 if (newhead == NULL) {
669 if (gdb_Debug & GDB_LOG) {
670 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",
671 oldhead, newhead, hcp, hcp->op_q_first,
674 GDB_GIVEUP("g_op_newhead: found NULL chain link")
677 * Remove oldhead from chain, fixing up chain pointers
679 newhead->prev = oldhead->prev;
680 hcp->op_q_first = newhead;
683 * Clean up pointers in the newly dequeued operation. This is
684 * just for cleanliness and ease of debugging.
686 oldhead->next = oldhead->prev = NULL;
687 oldhead->halfcon = NULL;
692 /************************************************************************/
696 /* This routine attempts to make further progress on the pending
697 /* level transmission operation pending on this half connection.
698 /* (Presumes that such an operation is pending.) Returns TRUE
699 /* if all the requested data has been moved, else FALSE.
701 /* We assume here that all fd's are set to non-blocking I/O, so
702 /* we can safely try reading and writing until they return 0 bytes.
704 /************************************************************************/
707 gdb_move_data(direction, hc)
708 int direction; /* CON_INPUT or CON_OUTPUT */
709 struct half_con_data *hc; /* pointer to control struct */
710 /* for this half connection */
712 register HALF_CONNECTION half_con = hc;
713 /* half connection pointer */
714 /* fast copy in register */
715 register int count; /* number of bytes read */
716 /* or written in latest */
718 fd_set *fdbits; /* the mask we should adjust */
719 /* for this direction */
720 fd_set tst_bits; /* these are used for */
721 /* the select we do prior */
722 /* to reading which tells */
723 /* us whether 0 byte read */
724 /* means empty or closed */
725 int selected; /* TRUE iff select says */
726 /* we should be able to */
730 * For safety, in case we're called when nothing is pending.
732 if (half_con->remaining == 0)
735 * Loop until either (1) the connection reported that it could
736 * not progress any further or (2) the full count has been
737 * satisfied. Some versions of Unix observe the rule that
738 * a closed connection, especially when reading, is indicated
739 * by select claiming that there is data when read says there
740 * isn't. Also, some other versions return errors from the
741 * select on that FD. We test for both situations here.
745 while(half_con->remaining>0) {
746 FD_SET(half_con->fd,&tst_bits);
747 if (direction == CON_INPUT) {
748 selected = select(gdb_mfd,&tst_bits, NULL, NULL,
751 * If selected==(-1), then we know there's something
752 * wrong with the socket
754 if (selected == (-1)) {
759 * if selected==0, then we know read won't do
760 * anything, so save the extraneous system call
767 * Selected is >0. Either the read is going to
768 * return 0, or this is one of those versions of
769 * Unix that tells us the connection has died by
770 * indicating select=1, read=0.
772 count = read(half_con->fd, half_con->next_byte,
773 half_con->remaining);
775 selected = select(gdb_mfd, NULL, &tst_bits, NULL,
777 if (selected == (-1)) {
785 count = write(half_con->fd, half_con->next_byte,
786 min(half_con->remaining,
787 GDB_MAX_SOCK_WRITE));
793 half_con->flags |= HCON_PROGRESS;
795 * rc==0 means we didn't mvoe any data, but if accompanied
796 * by select claiming there was data, it really means the
797 * connection is dead!
802 break; /* no more data available now*/
806 if (errno != EWOULDBLOCK) {
807 gdb_conok = FALSE; /* tell callers that */
813 half_con->remaining -=count;
814 half_con->next_byte +=count;
818 * The file descriptor masks used for doing selects must be activated
819 * when and only when there is a pending operation trying to use
820 * the connection. Update the masks for this half connection.
822 fdbits = (direction == CON_INPUT)? &gdb_crfds : &gdb_cwfds;
823 if (half_con->remaining >0)
824 FD_SET(half_con->fd, fdbits);
826 FD_CLR(half_con->fd, fdbits);
828 return (half_con->remaining == 0);
831 /************************************************************************/
833 /* gdb_receive_data (gdb_receive_data)
835 /* This routine is called by an init or continuation routine to
836 /* request that a specified amount of data be read, without
837 /* blocking, on the supplied connection. This routine returns
838 /* OP_COMPLETE if the entire read completed synchronously,
839 /* or OP_RUNNING if the read remains ongoing or is cancelling
840 /* due to error on the socket.
842 /************************************************************************/
845 gdb_receive_data(half_con, ptr, len)
846 HALF_CONNECTION half_con; /* read on this connection*/
847 char *ptr; /* put first byte here */
848 int len; /* number of bytes to read */
851 * Fill in the initial state of the attempted receive
853 half_con->remaining = len;
854 half_con->next_byte = ptr;
857 * Now see if we can make some progress on this read, possibly
858 * even completing it synchronously. Return appropriate
859 * result to our caller. Note: errors are reflected as OP_RUNNING
860 * with global variable gdb_cnok set to FALSE.
862 if(gdb_move_data(CON_INPUT, half_con))
868 /************************************************************************/
870 /* gdb_send_data (gdb_sndat)
872 /* This routine is called by an init or continuation routine to
873 /* request that a specified amount of data be written, without
874 /* blocking, on the supplied connection. This routine returns
875 /* OP_COMPLETE if the entire write completed synchronously,
876 /* or OP_RUNNING if the output remains ongoing or there was an error.
878 /************************************************************************/
881 gdb_sndat(half_con, ptr, len)
882 HALF_CONNECTION half_con; /* write on this connection*/
883 char *ptr; /* put first byte here */
884 int len; /* number of bytes to read */
888 * Fill in the initial state of the attempted receive
890 half_con->remaining = len;
891 half_con->next_byte = ptr;
894 * Now see if we can make some progress on this read, possibly
895 * even completing it synchronously. Return appropriate
896 * result to our caller.
898 if(gdb_move_data(CON_OUTPUT, half_con))
904 /************************************************************************/
906 /* gdb_start_a_listen
908 /* This routine is called by an init or continuation routine to
909 /* request that a connection be done. This routine returns
910 /* OP_COMPLETE if the accept completed synchronously,
911 /* or OP_RUNNING if the output remains ongoing or there was an error.
913 /************************************************************************/
916 gdb_start_a_listen(half_con, otherside, lenp, fdp)
917 HALF_CONNECTION half_con; /* write on this connection*/
918 char *otherside; /* put first byte here */
919 int *lenp; /* number of bytes to read */
924 * Fill in the initial state of the attempted accept
926 half_con->accepted_len = lenp;
927 half_con->next_byte = otherside;
928 half_con->accepted_fdp = fdp;
931 * Now see if we can make some progress on this read, possibly
932 * even completing it synchronously. Return appropriate
933 * result to our caller.
935 if(gdb_listen(half_con))
941 /************************************************************************/
943 /* gdb_listen (gdb_listen)
945 /* This routine is called from gdb_start_a_listen or hcon_progress to attempt
946 /* to continue making progress in accepting a connection on a
947 /* listening connection.
949 /************************************************************************/
953 struct half_con_data *hc; /* pointer to control struct */
954 /* for this half connection */
956 register HALF_CONNECTION half_con = hc;
957 /* half connection pointer */
958 /* fast copy in register */
960 fd_set tst_bits; /* these are used for */
961 /* the select we do prior */
962 /* to reading which tells */
963 /* us whether 0 byte read */
964 /* means empty or closed */
965 int selected; /* TRUE iff select says */
966 /* we should be able to */
972 half_con->flags &= ~HCON_PENDING_LISTEN;/* in case we succeed */
975 FD_SET(half_con->fd,&tst_bits);
976 selected = select(gdb_mfd,&tst_bits, NULL, NULL, &gdb_notime);
978 * If selected==(-1), then we know there's something
979 * wrong with the socket
981 if (selected == (-1)) {
986 * if selected==0, then we know accept won't do anything, so
990 half_con->flags |= HCON_PENDING_LISTEN;
991 FD_SET(half_con->fd, &gdb_crfds); /* we'll be looking for */
992 /* this whenever we select*/
996 * Selected is >0. The accept SHOULD not hang.
998 *(half_con->accepted_fdp) = accept(half_con->fd, half_con->next_byte,
999 half_con->accepted_len);
1001 * See whether the accept succeeded
1003 if (*(half_con->accepted_fdp) < 0) {
1004 perror("gdb: start_listening: error on listen");
1005 GDB_GIVEUP("Unexpected listen error");
1008 FD_CLR(half_con->fd, &gdb_crfds); /* don't select on this */