7 static char *rcsid_gdb_trans_c = "$Header$";
30 /************************************************************************/
34 /* GDB - Data Transport Services Routines (Part 1)
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 1 of 2)
43 /* Some of the routines specified are actually implemented as
44 /* macros defined in gdb.h.
46 /************************************************************************/
48 #include <sys/types.h>
57 /************************************************************************/
59 /* OPERATION Manipulation
61 /* The routines in this section provide services for creating
62 /* and manipulating GDB queueable operations.
64 /************************************************************************/
66 /*----------------------------------------------------------*/
68 /* create_operation (create_operation)
70 /* Allocates space for an operation descriptor and
71 /* returns a pointer to it. Initialize the eyecatcher
72 /* so that in the future we can prove by inspection that
73 /* this really is an operation descriptor.
75 /*----------------------------------------------------------*/
80 register OPERATION op;
82 op = (OPERATION)db_alloc(sizeof(OPERATION_DATA));
83 op->status = OP_NOT_STARTED; /* things like */
85 /* want valid status */
91 /*----------------------------------------------------------*/
93 /* reset_operation (reset_operation)
95 /* Sets the status of an operation to OP_NOT_STARTED
97 /*----------------------------------------------------------*/
105 GDB_CHECK_OP(op, "reset_operation")
107 status = OP_STATUS(op);
108 if (status == OP_QUEUED ||
109 status == OP_RUNNING)
110 GDB_GIVEUP("gdb:reset_operation invalid on running operation")
111 op->status = OP_NOT_STARTED;
114 /*----------------------------------------------------------*/
116 /* delete_operation(delete_operation)
118 /* Frees an operation descriptor.
120 /*----------------------------------------------------------*/
126 GDB_CHECK_OP(op, "delete_operation");
128 db_free((char *)op, sizeof(OPERATION_DATA));
131 /*----------------------------------------------------------*/
133 /* initialize_operation(initialize_operation)
135 /* Initialize the data in an operation descriptor
136 /* based on the supplied parameters.
138 /*----------------------------------------------------------*/
141 initialize_operation(operation, init_function, arg, cancel_function)
142 OPERATION operation; /* initialize this */
143 int (*init_function)(); /* function to call when */
144 /* starting operation */
145 char *arg; /* arg to pass to init and */
146 /* continue routines */
147 int (*cancel_function)(); /* call this function */
148 /* when cancelling the op. */
149 /* may be NULL, may not */
152 register OPERATION op = operation; /* just for speed */
154 GDB_CHECK_OP(op, "initialize_operation")
158 * Fill in boilerplate - same for all newly initialized ops.
160 op->next = op->prev = NULL; /* it's not in a queue yet */
161 op->tag = (-1); /* not tagged until queued */
162 op->status = OP_NOT_STARTED;
164 op->result = (-1); /* result is not valid until */
165 /* status is OP_COMPLETE. */
166 /* All users of result field */
167 /* are expected to treat -1 */
169 op->halfcon = NULL; /* not queued yet */
172 * Fill in supplied parameters
175 op->fcn.init = init_function;
176 op->cancel = cancel_function;
181 /*----------------------------------------------------------*/
183 /* create_list_of_operations (create_list_of_operations)
185 /* Allocates and fills in a data structure which
186 /* contains a list of operations. This list is
187 /* suitable for passing to op_select.
189 /* The facilities of <varargs.h> are used to parse
190 /* the variable length parameter list to this function.
191 /* See "man 3 varargs" for details.
193 /*----------------------------------------------------------*/
198 create_list_of_operations(count, va_alist)
202 va_list ap; /* the control structure */
203 /* for varargs parsing */
206 register LIST_OF_OPERATIONS newlist; /* newly allocated list */
209 * Allocate the new list of operations, initialize its count.
211 newlist = (LIST_OF_OPERATIONS)
212 db_alloc(size_of_list_of_operations(count));
213 newlist->count = count;
216 * Using the facilities of varargs.h, pick up the operations
217 * and put them in the list.
220 for (i=0; i<count; i++) {
221 newlist->op[i] = va_arg(ap, OPERATION);
222 GDB_CHECK_OP((newlist->op[i]), "create_list_of_operations")
229 /*----------------------------------------------------------*/
231 /* delete_list_of_operations(delete_list_of_operations)
233 /* Deallocate the space for a list of operations.
235 /*----------------------------------------------------------*/
238 delete_list_of_operations(op_list)
239 LIST_OF_OPERATIONS op_list;
241 db_free((char *)op_list, size_of_list_of_operations(op_list->count));
244 /************************************************************************/
246 /* OPERATION QUEUE MANIPULATION
248 /* Routines to queue (initiate) and track completion of GDB
251 /************************************************************************/
253 /*----------------------------------------------------------*/
255 /* op_select(op_select)
257 /* This function is an analog of the standard Berkeley
258 /* select system call. It provides all the functions
259 /* of select, but in addition, it awaits completion
260 /* of a specified list of queued gdb operations.
262 /* This function returns when any combination of the
263 /* following are found to be true:
265 /* 1) Any of the designated operations has completed
266 /* or terminated prematurely.
268 /* 2) Activity is detected on any of the explictly supplied
269 /* file descriptors or the supplied timer has expired.
271 /* The count returned is only for file descriptors
272 /* explicitly listed. Completed operatons may be detected
273 /* by checking OP_STATUS for each of the operations in
274 /* the list. File descriptors controlled by CONNECTIONS
275 /* should never be passed explictly in the bit maps to
276 /* this routine. Only user controlled file descriptors
277 /* may be explictly selected.
279 /* Return code summary:
281 /* -2 One or more listed operations completed.
282 /* Timer did not ring (as far as we could
283 /* tell), other fd's did not complete, or
284 /* we didn't get far enough to bother
285 /* looking. (see fairness below.)
287 /* -1 An error was returned on select of a
288 /* user supplied socket. errno has the
291 /* 0 The timer rang. Some operations may
292 /* be complete, but it's unlikely.
294 /* >0 This many user supplied fd's were
295 /* satisfied--same as for select.
296 /* Operations in the list may also have
299 /* Fairness is not guaranteed. This routine tends to
300 /* favor activity on CONNECTIONs. In particular, if
301 /* some operation(s) in the list can be completed
302 /* synchronously, a real select is never done to
303 /* check on the file descriptors.
305 /*----------------------------------------------------------*/
308 op_select(op_list, nfds, readfds, writefds, exceptfds, timeout)
309 LIST_OF_OPERATIONS op_list;
311 fd_set *readfds, *writefds, *exceptfds;
312 struct timeval *timeout;
314 int rc; /* return code holder */
315 fd_set rfds, wfds, efds; /* local copys of read */
316 /* write, and exception */
320 * Make sure that any activity which can be done immediately is
321 * indeed done now--we may not have to wait at all.
323 (void) gdb_fastprogress(); /*<==FIX (check to make sure this is OK)*/
326 * Loop checking for termination conditions, and if none arise,
327 * use con_select to make wait for one or more of the
328 * connections to wake up and do the appropriate processing.
332 * If any of the queue operations have completed,
333 * then just return now.
335 if (gdb_list_complete(op_list))
336 return (-2); /* no fd's satisfied here */
339 * Use con_select to await all the appropriate events
341 g_fd_copy(nfds, readfds, &rfds);
342 g_fd_copy(nfds, writefds, &wfds);
343 g_fd_copy(nfds, exceptfds, &efds);
345 rc = con_select(nfds, &rfds, &wfds, &efds, timeout);
348 * At this point, either some of the supplied fd's have
349 * been satisfied(rc>0), or the timer has rung (rc=0),
350 * an error was returned from select on a user specified socket
351 * (-1) or none of these (-2). In any case, there may
352 * have been progress on one of the connections, and
353 * con_select will already have progressed as far as
354 * possible before returning. Now, decide what to
355 * do, given the return code.
359 * Return updated bit-maps to caller.
361 g_fd_copy(nfds, &rfds, readfds);
362 g_fd_copy(nfds, &wfds, writefds);
363 g_fd_copy(nfds, &efds, exceptfds);
369 /*----------------------------------------------------------*/
371 /* op_select_all(op_select_all)
373 /* Similar to op_select_any, but returns (-1) only
374 /* in the case that all of the designated descriptors
375 /* are OP_COMPLETE or OP_CANCELLED.
377 /*----------------------------------------------------------*/
380 op_select_all(op_list, nfds, readfds, writefds, exceptfds, timeout)
381 LIST_OF_OPERATIONS op_list;
383 fd_set *readfds, *writefds, *exceptfds;
384 struct timeval *timeout;
388 register int left = op_list->count;
391 * take care of those which are already complete by decrementing
395 for (i=0; i<op_list->count; i++) {
396 if (op_list->op[i]->status==OP_COMPLETE) {
397 op_list->op[i]->flags |= OPF_MARKED_COMPLETE;
398 op_list->op[i]->status = OP_MARKED;
399 /* so op_select_any won't */
400 /* fall right through*/
405 if (op_list->op[i]->status==OP_CANCELLED) {
406 op_list->op[i]->flags |= OPF_MARKED_CANCELLED;
407 op_list->op[i]->status = OP_MARKED;
408 /* so op_select_any won't */
409 /* fall right through*/
417 * As long as there are incomplete operations left in the list,
418 * keep calling op_select_any
421 rc = op_select(op_list, nfds, readfds, writefds, exceptfds,
425 for (i=0; i<op_list->count; i++) {
426 if (op_list->op[i]->status==OP_COMPLETE) {
427 op_list->op[i]->flags |= OPF_MARKED_COMPLETE;
428 op_list->op[i]->status = OP_MARKED;
429 /* so op_select_any won't */
430 /* fall right through*/
435 if (op_list->op[i]->status==OP_CANCELLED) {
436 op_list->op[i]->flags |= OPF_MARKED_CANCELLED;
437 op_list->op[i]->status = OP_MARKED;
438 /* so op_select_any won't */
439 /* fall right through*/
447 * Clean up the marked operations and return
449 for (i=0; i<op_list->count; i++) {
450 if (op_list->op[i]->status==OP_MARKED) {
451 op_list->op[i]->status = (op_list->op[i]->flags &
452 OPF_MARKED_COMPLETE) ?
455 op_list->op[i]->flags &= ~(OPF_MARKED_COMPLETE |
456 OPF_MARKED_CANCELLED);
464 /************************************************************************/
466 /* Internal transport layer routines, not called directly
467 /* from client library.
469 /************************************************************************/
471 /*----------------------------------------------------------*/
475 /* Given a list of gdb operations, return TRUE if any
476 /* of them are complete, otherwise return FALSE.
478 /*----------------------------------------------------------*/
481 gdb_list_complete(op_list)
482 LIST_OF_OPERATIONS op_list;
486 register LIST_OF_OPERATIONS oplist = op_list; /* for speed */
487 int count = oplist -> count;
489 for (i=0; i<count; i++) {
490 status = OP_STATUS(oplist->op[i]);
491 if (status == OP_COMPLETE ||
492 status == OP_CANCELLED)
499 /*----------------------------------------------------------*/
503 /* Copy one set of fd masks to another.
505 /*----------------------------------------------------------*/
507 g_fd_copy(nfds, source, targ)
509 fd_set *source, *targ;
512 register int n = howmany(nfds, NFDBITS); /* number of words for */
516 targ->fds_bits[i] = source->fds_bits[i];
518 /*----------------------------------------------------------*/
522 /* Or two sets of file descriptor masks together and
523 /* copy to a third. Parameters are:
525 /* 1) First mask count
526 /* 2) Second mask count
527 /* 3) pointer to first mask
528 /* 4) Pointer to second mask
529 /* 5) Pointer to output mask
531 /*----------------------------------------------------------*/
533 g_fd_or_and_copy(nfds1, nfds2 , source1, source2, targ)
535 fd_set *source1, *source2, *targ;
538 fd_set *longer; /* points to whichever */
539 /* of the two masks is */
541 int tot_words = howmany(max(nfds1,nfds2), NFDBITS);
542 int shared_words = howmany(min(nfds1,nfds2), NFDBITS);
546 * For words which exist in both masks, or the bits together
548 for (i=0; i<shared_words; i++)
549 targ->fds_bits[i] = source1->fds_bits[i] |
550 source2->fds_bits[i];
552 * Copy the rest of whichever is longer
554 longer = (nfds1>nfds2) ? source1 : source2;
556 while (i<tot_words) {
557 targ->fds_bits[i] = longer->fds_bits[i];
564 /*----------------------------------------------------------*/
566 /* g_compute_return_fds
568 /* When con_select returns, the count it gives back
569 /* applies only to user supplied fd's, not those
570 /* relating to connections. Unfortunately, select
571 /* gives back both in the masks it returns. Here we
572 /* fix only the count to reflect the fact that we will be
573 /* turning off bits when and if they are returned.
575 /* We can assume here that the masks provided are as long
576 /* as merged_nfds, which means they are at least as long
579 /*----------------------------------------------------------*/
582 g_compute_return_fds(select_count, rfds, wfds, efds)
583 int select_count; /* count of 1 bits as */
584 /* returned from select */
585 fd_set *rfds, *wfds, *efds; /* read, write, and except */
586 /* maps as returned from */
590 register int return_count = select_count; /* the value we'll return */
591 register int words; /* number of words in each */
592 /* of the connection masks */
596 * Since we can only decrement the count, there's no sense doing
597 * any work if it's already 0;
599 if (return_count == 0)
602 * Figure out how many words we have to look at to get all the
603 * bits covered by connection masks.
605 words = howmany(gdb_mfd, NFDBITS);
608 * For words which are involved in the connection masks, check
609 * for matches and decrement the count accordingly. Stop when
610 * the count hits 0 or we're out of words.
612 for (i=0; i<words && (return_count>0) ; i++) {
613 return_count -= g_bitcount((unsigned int)
614 (gdb_crfds.fds_bits[i] &
616 return_count -= g_bitcount((unsigned int)
617 (gdb_cwfds.fds_bits[i] &
619 return_count -= g_bitcount((unsigned int)
620 (gdb_cefds.fds_bits[i] &
627 /*----------------------------------------------------------*/
629 /* g_fd_reset_conbits
631 /* Given a user supplied fd bit mask and a connection
632 /* related bit mask, turn off any of the connection related
633 /* bits in the user mask, and copy the result to a supplied
636 /* 1) User mask count
637 /* 2) Connection mask count
638 /* 3) Pointer to user mask
639 /* 4) Pointer to connection mask
640 /* 5) Pointer to output mask
642 /* Output is always the same length as the user mask.
644 /*----------------------------------------------------------*/
647 g_fd_reset_conbits(nfds, con_nfds , source, conbits, targ)
649 fd_set *source, *conbits, *targ;
652 register int tot_words = howmany(nfds, NFDBITS); /* this rtn never*/
653 /* returns a mask longer */
655 register int shared_words = howmany(min(nfds, con_nfds), NFDBITS);
659 * For words which exist in both masks, turn off bits from conmask
661 for (i=0; i<shared_words; i++)
662 targ->fds_bits[i] = source->fds_bits[i] &
663 ~(conbits->fds_bits[i]);
665 * Copy the rest of source, if any
667 if (tot_words > shared_words)
668 while (i<tot_words) {
669 targ->fds_bits[i] = source->fds_bits[i];
677 /************************************************************************/
679 /* CONNECTION MANIPULATION
681 /* Routines to control data transmission on gdb connections.
683 /************************************************************************/
685 /*----------------------------------------------------------*/
687 /* con_select (con_select)
689 /* This operation has exactly the same semantics as the select
690 /* system call, except that (1) it implicitly selects all file
691 /* descriptors controlled by connections, as well as those
692 /* explictly specified and (2) it allows transmission and
693 /* receipt to progress on all connections and (3) it considers
694 /* a connection to be selected iff a transmission operation
695 /* which had been pending becomes complete. One may consider
696 /* that con_select turns the fd's controlled by sockets into
697 /* packet streams rather than byte streams. Note also that
698 /* this operation differs from a traditional select and an
699 /* op_select in that it is not robust against waiting for
700 /* connections with pre-completed activity. This could be
701 /* added, but since it's an internal routine anyway, it seems
702 /* not to be worthwhile. Also, this routine presumes that all
703 /* possible progress has been made before con_select is invoked.
705 /* This operation hangs in a select. If activity is
706 /* discovered on any of the sockets controlled by the database
707 /* library, then the corresponding input is read and
708 /* appropriate processing is done. If any of the transmission
709 /* operations which had been pending on one of these
710 /* connections completes, then con_select may return. In
711 /* fact, con_select attempts to complete any further
712 /* connection related activity which can be done without
713 /* blocking, but con_select never blocks AFTER a transmission
714 /* operation has gone complete.
716 /* If activity is detected on any of the file descriptors
717 /* supplied by the user, then a count and bit fields are
718 /* returned just as for select. (Activity on database sockets
719 /* is never reflected in either count or bitfields.) Timeout
720 /* causes a return, as with select. Upon return, the program
721 /* must check for competion or termination on all of the
722 /* connections in which he/she is interested, for activity on
723 /* the selected file descriptors, and for timeouts, if
724 /* requested, since any or all of these may be reported
727 /* Return values for con_select: >0 same as for select, 0 time
728 /* expired, -1, error in select on user fd, -2, connections
729 /* have progressed but nothing else of interest.
732 /*----------------------------------------------------------*/
735 con_select(nfds, readfds, writefds, exceptfds, timeout)
737 fd_set *readfds, *writefds, *exceptfds;
738 struct timeval *timeout;
740 int merged_nfds; /* the index of the last */
741 /* file desc we care about.*/
742 int select_fds; /* number of file */
743 /* returned from select */
745 int return_fds; /* fds count to be returned */
747 int complete_count; /* number of connections on */
748 /* which operations have */
753 * figure out highest number file descriptor to worry about
755 merged_nfds = max(nfds, gdb_mfd); /* the number we control */
756 /* or last one the user */
757 /* supplied, whichevere is */
760 * Loop waiting for activity to occur and processing it as appropriate.
761 * Note that the order of the tests, calls to select, and gdb_progress
762 * in this code is critical. Think it through before you change it
763 * to make sure that progress is always made when possible, and that
764 * returns are always made when needed.
768 * Prepare working copies of the file descriptor maps
769 * based on the ones supplied by the caller, as well as
770 * all the descriptors used by connections. The latter
771 * are mapped in the gdb_c?fds global variables.
773 g_fd_or_and_copy(nfds,gdb_mfd,readfds,&gdb_crfds,&last_crfds);
774 g_fd_or_and_copy(nfds,gdb_mfd,writefds,&gdb_cwfds,&last_cwfds);
775 g_fd_or_and_copy(nfds,gdb_mfd,exceptfds,&gdb_cefds,&last_cefds);
777 * Use select to wait for something to happen. Compute
778 * number select would have returned if connection related
779 * fd's had not been supplied.
781 select_fds = select(merged_nfds, &last_crfds, &last_cwfds, &last_cefds,
784 * There are some messy things we have to worry about here:
786 * 1) Select could return an error. In particular, some
787 * versions of Unix will return -1 with EBADF if one
788 * of the sockets has closed. We should call a
789 * procedure here to see if this has happened to one
792 * 2) Other versions of Unix will claim that there is activity
793 * on our socket when in fact the other end is closed.
794 * We will leave it to gdb_move_data to try the select
795 * and make the appropriate decision.
797 * Yes folks, messy but true. If we got an error (case 1),
798 * then let's see if we get the same error when we're only
799 * looking at the caller's fd's.
807 g_fd_or_and_copy(nfds,0,readfds,&gdb_crfds,&last_crfds);
808 g_fd_or_and_copy(nfds,0,writefds,&gdb_cwfds,&last_cwfds);
809 g_fd_or_and_copy(nfds,0,exceptfds,&gdb_cefds,&last_cefds);
810 if (select(nfds, &last_crfds, &last_cwfds, &last_cefds,
812 g_fd_copy(nfds, &last_crfds, readfds);
813 g_fd_copy(nfds, &last_cwfds, writefds);
814 g_fd_copy(nfds, &last_cefds, exceptfds);
815 return -1; /* select EBADF */
818 * We should close the connection here
820 GDB_GIVEUP("con_select: EBADF on GDB controlled file.")
824 return_fds = g_compute_return_fds(select_fds,
825 &last_crfds, &last_cwfds, &last_cefds);
828 * If some connection related descriptors were selected, then
829 * try to make progress on them. Find out if any new operations
830 * could be completed.
832 if (select_fds != return_fds)
833 complete_count = gdb_fastprogress();
837 * Now, based on the number of operations complete,
838 * the number of user file descriptors satisfied, and
839 * the possible return of a timeout from select, decide
840 * whether to return to the caller. Note that timeout
841 * is identified by select_fds == 0.
843 if (complete_count > 0 || /* operations are complete */
844 return_fds >0 || /* user files satisfied */
845 select_fds ==0) { /* timeout in select */
847 * Connection related fd bits are never returned
848 * to the caller. Reset them. The count of bits
849 * was already adjusted appropriately above. I don't
850 * think there's too much wasted effort in this,
851 * but we should watch it if profiling indicates
852 * lots of time being spent computing in con_select.
853 * Put the updated bit masks in the caller supplied
856 g_fd_reset_conbits(nfds,gdb_mfd,&last_crfds,&gdb_crfds,
858 g_fd_reset_conbits(nfds,gdb_mfd,&last_cwfds,&gdb_cwfds,
860 g_fd_reset_conbits(nfds,gdb_mfd,&last_cefds,&gdb_cefds,
863 return 0; /* real timeout */
864 if (return_fds ==0) /* something must have */
865 /* completed, but our */
866 /* count looks like a */
868 return (-2); /* only connections have */
869 /* somethine complete */