]> andersk Git - moira.git/blame - gdb/gdb_trans2.c
added copyright message
[moira.git] / gdb / gdb_trans2.c
CommitLineData
5580185e 1/*
2 * $Source$
3 * $Header$
4 */
5
6#ifndef lint
7static char *rcsid_gdb_trans2_c = "$Header$";
8#endif lint
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30/************************************************************************/
31/*
32/* gdb_trans2.c
33/*
34/* GDB - Data Transport Services Routines (Part 2)
35/*
36/* Author: Noah Mendelsohn
37/* Copyright: 1986 MIT Project Athena
0a5ff702 38/* For copying and distribution information, please see
39/* the file <mit-copyright.h>.
40/*
5580185e 41/*
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.
45/*
46/* Some of the routines specified are actually implemented as
47/* macros defined in gdb.h.
48/*
49/************************************************************************/
50
0a5ff702 51#include <mit-copyright.h>
5580185e 52#include <sys/types.h>
53#include <errno.h>
54#include <stdio.h>
55#include "gdb.h"
56#include <sys/uio.h>
57#include <sys/socket.h>
58extern int errno; /* Unix error slot */
59
60/*
61 * The following values are returned by g_con_progress
62 */
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 */
68\f
69/************************************************************************/
70/*
71/* queue_operation(queue_operation)
72/*
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.
76/*
77/************************************************************************/
78
79int
80queue_operation(con, direction, op)
81CONNECTION con;
82int direction;
83OPERATION op;
84{
85 register HALF_CONNECTION hcon = (direction==CON_INPUT)?(&(con->in)):
86 (&(con->out));
87 GDB_CHECK_CON(con, "queue_operation")
88 /*
89 * Write message to debugging log
90 */
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");
95 /*
96 * Make sure connection is up
97 */
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");
102 return OP_CANCELLED;
103 }
104
105 /*
106 * Put the new operation at the end of the queue
107 */
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;
112 /*
113 * Mark it as queued
114 */
115 op->status = OP_QUEUED;
116 op->halfcon = hcon;
117
118 /*
119 * Force progress on this connection
120 */
121 (void) g_con_progress(con - gdb_cons);
122 /*
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.
125 */
126 (void) con_select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0,
127 &gdb_notime);/* XXX */
128 /*
129 * Return the last known status of the operation
130 */
131 return op->status;
132}
133/************************************************************************/
134/*
135/* requeue_operation
136/*
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.
143/*
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.)
149/*
150/************************************************************************/
151
152int
153requeue_operation(con, direction, op)
154CONNECTION con;
155int direction;
156OPERATION op;
157{
158 /*
159 * Make sure the connection supplied is a legal one
160 */
161 GDB_CHECK_CON(con, "requeue_operation")
162 /*
163 * Write message to debugging log
164 */
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",
168 op);
169 /*
170 * Dequeue the operation from its old half connection
171 */
172 (void) g_op_newhead(op->halfcon);
173
174 /*
175 * Now queue it on the new one
176 */
177 return queue_operation(con, direction, op);
178}
179
180/************************************************************************/
181/*
182/* g_preempt_me
183/*
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
194/* its caller.
195/*
196/************************************************************************/
197
198int
199g_preempt_me(oldop, newop)
200OPERATION oldop;
201OPERATION newop;
202{
203 register OPERATION old=oldop, new=newop;
204 register HALF_CONNECTION hc = old->halfcon;
205
206 /*
207 * Write message to debugging log
208 */
209 if (gdb_Debug & GDB_LOG)
210 fprintf(gdb_log, "op preempted: halfcon=0x%x oldop=0x%x newop=0x%x\n",
211 oldop,newop);
212 /*
213 * link in the new operation
214 */
215 old->prev = new;
216 hc->op_q_first = new;
217 new->prev = (OPERATION)hc;
218 new->next = old;
219 /*
220 * Set the status of the new operation
221 */
222 new->status = OP_QUEUED;
223 new->halfcon = hc;
224 /*
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
227 * is detected.)
228 */
229 old->status = OP_QUEUED;
230 return OP_QUEUED;
231}
232
233
234\f
235/************************************************************************/
236/*
237/* gdb_progress
238/*
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.
245/*
246/* Returns: number of connections on which OPERATIONS have
247/* COMPLETED (not just progressed).
248/*
249/************************************************************************/
250
251int
252
253gdb_progress()
254{
255 register int i; /* index to available */
256 /* connections */
257 register int return_value = 0; /* the value we return */
258 int rc; /* short term storage for */
259 /* a return code */
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. */
272
273 /*
274 * Zero out the completion map for all connections.
275 */
276 for (i=0; i<maxcon; i++)
277 complete_map[i]=0;
278
279 /*
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.
284 */
285
286 do {
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;
291 if (rc == COMPLETE)
292 complete_map[i] = 1;
293 }
294 }
295 } while (progress_made);
296
297 /*
298 * We've gone as far as we can, now find out how many connections
299 * have had operations complete.
300 */
301 for (i=0; i<maxcon; i++)
302 return_value += complete_map[i];
303
304 return return_value;
305}
306
307\f
308/************************************************************************/
309/*
310/* gdb_fastprogress
311/*
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.
321/*
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.
328/*
329/************************************************************************/
330
331gdb_fastprogress()
332{
333 int i;
334 int retval=0, rc;
335
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;
340
341 if(connection_status(con) != CON_UP)
342 continue;
343
344 gdb_conok = TRUE;
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);
349 if (!gdb_conok) {
350 g_stop_with_errno(con);
351 rc = COMPLETE;
352 }
353 if (rc == COMPLETE)
354 retval++;
355 }
356 if ((!(con->out.flags&HCON_UNUSED))&&
357 (FD_ISSET(outfd, &last_cwfds))) {
358 rc = gdb_hcon_progress(CON_OUTPUT, &con->out);
359 if (!gdb_conok) {
360 g_stop_with_errno(con);
361 rc = COMPLETE;
362 }
363 if (rc == COMPLETE)
364 retval++;
365 }
366 }
367 /*
368 * We've gone as far as we can, now find out how many connections
369 * have had operations complete.
370 */
371
372 return retval;
373}
374\f
375/************************************************************************/
376/*
377/* g_con_progress
378/*
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.
385/*
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.
392/*
393/************************************************************************/
394
395
396int
397g_con_progress(con_id)
398int con_id; /* index of this connection */
399 /* in the connection desc. */
400 /* arrays*/
401{
402 register CONNECTION con= (&gdb_cons[con_id]);
403 /* pointer to the connection */
404 /* data structure */
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*/
409 int rc;
410 /*
411 * Check status of connection-if it's not running, then just return.
412 */
413 if (con->status != CON_UP)
414 return NOPROGRESS;
415 /*
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.
419 */
420
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 */
428 /* goes bad */
429 while (live) {
430 live = FALSE; /* until proven otherwise */
431 /*
432 * make progress on the input connection note that following
433 * logic depends on NOPROGRESS being 0
434 */
435 if (rc = gdb_hcon_progress(CON_INPUT, &con->in)) {
436 live = TRUE;
437 progress = max(rc, progress);
438 }
439 /*
440 * See if connection has died
441 */
442 if (!gdb_conok) {
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 */
448 }
449 /*
450 * make progress on the output connection
451 */
452 if (rc = gdb_hcon_progress(CON_OUTPUT, &con->out)) {
453 live = TRUE;
454 progress = max(rc, progress);
455 }
456 /*
457 * See if connection has died
458 */
459 if (!gdb_conok) {
460 g_stop_with_errno(con);
461 return COMPLETE;
462 }
463 }
464
465 return progress;
466}
467
468
469/************************************************************************/
470/*
471/* gdb_hcon_progress
472/*
473/* Allows a specified half-connection to progress as much as possible,
474/* and returns true iff at least one operation is newly completed.
475/*
476/************************************************************************/
477
478int
479gdb_hcon_progress(direction, hc)
480int direction; /* CON_INPUT or CON_OUTPUT */
481struct half_con_data *hc; /* pointer to control struct */
482 /* for this half connection */
483{
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 */
492 /* completed */
493 int done; /* true when no more progress*/
494 /* can be made */
495 int fcn_result; /* result of latest init or */
496 /* continue function */
497
498 /*
499 * Write message to debugging log
500 */
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");
504
505 /*----------------------------------------------------------*/
506 /*
507 /* See if we are being re-entered and are already working
508 /* on this half_con. If so, return right now.
509 /*
510 /*----------------------------------------------------------*/
511
512 if (half_con->flags & HCON_BUSY) {
513 /*
514 * Write message to debugging log
515 */
516 if (gdb_Debug & GDB_LOG)
517 fprintf(gdb_log, "BUSY, returning\n");
518 return NOPROGRESS;
519 }
520
521 /*----------------------------------------------------------*/
522 /*
523 /* See if there is an operation on this half connection.
524 /* If not, return.
525 /*
526 /*----------------------------------------------------------*/
527
528
529 op = half_con->op_q_first; /* pick up first operation */
530 /* in queue */
531 if (op == (OPERATION)half_con) { /* see if end of circular */
532 /* list */
533 /*
534 * Write message to debugging log
535 */
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 */
540 }
541
542
543 /*----------------------------------------------------------*/
544 /*
545 /* Loop until all operations are complete, or until no further
546 /* progress can be made on this one.
547 /*
548 /* Loop invariants:
549 /*
550 /* 1) Op contains the operation at the head of the q, or
551 /* else is == half_con, indicating no more operationos
552 /* to be processed.
553 /*
554 /* 2) The operation at the head of the queue is either running
555 /* or continuing. As soon as one completes, it is dequeued.
556 /*
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.
560 /*
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.
564 /*
565 /* While we're here, mark us busy so we won't try the
566 /* same half_con on reentry.
567 /*
568 /*----------------------------------------------------------*/
569
570 done = FALSE; /* this one may be able to */
571 /* progress */
572
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");
581
582 /*----------------------------------------------------------*/
583 /*
584 /* Loop through the operations queued on this half con
585 /* trying to make progress on them, in order.
586 /*
587 /*----------------------------------------------------------*/
588
589 while (!done &&
590 op != (OPERATION)half_con) {
591
592 if (gdb_Debug & GDB_LOG)
593 fprintf(gdb_log, "\top=0x%x status%d...",
594 op, OP_STATUS(op));
595
596 switch (op->status) {
597 /*
598 * Operation is at head of queue for first time and has
599 * never been started. Try to start it up.
600 */
601 case OP_QUEUED:
602 /*
603 * Call the initialization routine for this operation
604 */
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",
608 fcn_result);
609
610 switch (fcn_result) {
611 case OP_COMPLETE:
612 case OP_CANCELLED:
613 op->status = fcn_result;
614 op = g_op_newhead(half_con);
615 progress = COMPLETE;
616 break;
617 case OP_PREEMPTED:
618 op->status = OP_QUEUED;
619 /* fall thru */
620 case OP_REQUEUED:
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);
626 break;
627 default:
628 op->status = fcn_result;
629 done = TRUE; /* could not get done */
630 }
631 break;
632 /*
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.
639 */
640 case OP_RUNNING:
641 /*
642 * Try to move some more data. If it won't all
643 * go now, we're done with this half connection.
644 *
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
648 * data move.
649 */
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");
654 done = TRUE;
655 break;
656 }
657 } else
658 if (gdb_move_data(direction, half_con)==FALSE) {
659 done = TRUE;
660 if (gdb_Debug & GDB_LOG)
661 fprintf(gdb_log, "NO DATA\n");
662 break;
663 }
664 /*
665 * The pending data transmission has now completed.
666 * Call the continuation routine for this operation
667 */
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",
671 fcn_result);
672
673 switch (fcn_result) {
674 case OP_COMPLETE:
675 case OP_CANCELLED:
676 op->status = fcn_result;
677 op = g_op_newhead(half_con);
678 progress = COMPLETE;
679 break;
680 case OP_PREEMPTED:
681 op->status = OP_QUEUED;
682 /* fall thru */
683 case OP_REQUEUED:
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);
689 break;
690 default:
691 op->status = fcn_result;
692 done = TRUE; /* could not get done */
693 }
694 break;
695 /*
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.
699 */
700 case OP_COMPLETE:
701 GDB_GIVEUP("gdb_hcon_progress: found OP_COMPLETE on q")
702 case OP_CANCELLED:
703 GDB_GIVEUP("gdb_hcon_progress: found OP_CANCELLED on q")
704 case OP_CANCELLING:
705 GDB_GIVEUP("gdb_hcon_progress: OP_CANCELLING")
706 default:
707 GDB_GIVEUP("gdb_hcon_progress: Operation is queued, but is not runnable")
708 }
709 }
710
711 if (progress == NOPROGRESS && (half_con->flags & HCON_PROGRESS))
712 progress = PROGRESS;
713
714 half_con->flags &= ~HCON_BUSY;
715
716 if (gdb_Debug & GDB_LOG)
717 fprintf(gdb_log, "hcon_progress: returns %d\n",progress);
718
719 return progress; /* NOPROGRESS, PROGRESS */
720 /* or COMPLETE */
721}
722\f
723/************************************************************************/
724/*
725/* g_op_newhead
726/*
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.)
732/*
733/************************************************************************/
734
735OPERATION
736g_op_newhead(hcp)
737HALF_CONNECTION hcp;
738{
739 register OPERATION newhead, oldhead;
740
741 /*
742 * Get old and new heads of chain
743 */
744 oldhead = hcp->op_q_first;
745 newhead = oldhead->next;
746 /*
747 * Make sure nobody chained a bad one on us
748 */
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,
753 hcp->op_q_last);
754 }
755 GDB_GIVEUP("g_op_newhead: found NULL chain link")
756 }
757 /*
758 * Remove oldhead from chain, fixing up chain pointers
759 */
760 newhead->prev = oldhead->prev;
761 hcp->op_q_first = newhead;
762
763 /*
764 * Clean up pointers in the newly dequeued operation. This is
765 * just for cleanliness and ease of debugging.
766 */
767 oldhead->next = oldhead->prev = NULL;
768 oldhead->halfcon = NULL;
769
770 return newhead;
771}
772\f
773/************************************************************************/
774/*
775/* gdb_move_data
776/*
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.
781/*
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.
784/*
785/************************************************************************/
786
787#define FIX_BUFFER_POINTERS(hc, count) if (count>0) {hc->next_byte += count; \
788 hc->remaining -= count;}
789int
790gdb_move_data(direction, hc)
791int direction; /* CON_INPUT or CON_OUTPUT */
792struct half_con_data *hc; /* pointer to control struct */
793 /* for this half connection */
794{
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 */
800
801 /*
802 * For safety, in case we're called when nothing is pending.
803 */
804 if (half_con->remaining == 0)
805 return TRUE;
806 /*
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
809 * itself.
810 */
811 if (direction == CON_INPUT) {
812 gdb_transfer_from_buffer(half_con);
813 /*
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
817 * buffer.
818 */
819 if (half_con->remaining) {
820 gdb_read_data_and_buffer(half_con);
821 }
822 } else {
823 gdb_write_data(half_con);
824 }
825 /*
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.
829 */
830 fdbits = (direction == CON_INPUT)? &gdb_crfds : &gdb_cwfds;
831 if (half_con->remaining >0 && gdb_conok)
832 FD_SET(half_con->fd, fdbits);
833 else
834 FD_CLR(half_con->fd, fdbits);
835
836 return (half_con->remaining == 0);
837}
838\f
839/************************************************************************/
840/*
841/* gdb_transfer_from_buffer
842/*
843/* Given an inbound half connection, satisfy as much as possible
844/* of desired data from the stream buffer.
845/*
846/************************************************************************/
847
848int
849gdb_transfer_from_buffer(hc)
850register HALF_CONNECTION hc;
851{
852 register int count; /* amount to move */
853
854 /*
855 * Figure out how much, if any, we'll be able to do here
856 */
857 count = min(hc->remaining, hc->stream_buffer_remaining);
858 if (count <= 0)
859 return; /* could not satisfy */
860 /* any from buffered data*/
861
862 /*
863 * Copy the data, update both stream and data buffer pointers
864 */
865
866 bcopy(hc->stream_buffer_next, hc->next_byte, count);
867
868 hc->stream_buffer_next += count;
869 hc->stream_buffer_remaining -= count;
870 FIX_BUFFER_POINTERS(hc, count)
871
872}
873
874\f/************************************************************************/
875/*
876/* gdb_write_data
877/*
878/* This routine implements gdb_move_data for an outbound half
879/* connection.
880/*
881/************************************************************************/
882
883int
884gdb_write_data(hc)
885struct half_con_data *hc; /* pointer to control struct */
886 /* for this half connection */
887{
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 */
893 /* attempt */
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 */
903 /* progress */
904
905 /*
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.
911 */
912 FD_ZERO(&tst_bits);
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));
917 if (count == 0) {
918 FD_SET(half_con->fd,&tst_bits);
919 selected = select(gdb_mfd,
920 (fd_set *)NULL, &tst_bits,
921 (fd_set *)NULL,
922 &gdb_notime);
923 if (selected == (-1)) {
924 gdb_conok = FALSE;
925 break;
926 }
927 if (selected == 0) {
928 count =0;
929 break;
930 }
931 count = write(half_con->fd, half_con->next_byte,
932 (int)min(half_con->remaining,
933 GDB_MAX_SOCK_WRITE));
934 if (count==0) {
935 if (selected == 1)
936 gdb_conok = FALSE;
937 break; /* no more data available now*/
938 }
939 }
940 /*
941 * Count is != 0
942 */
943 if (count<0) {
944 count = 0;
945 if (errno != EWOULDBLOCK) {
946 gdb_conok = FALSE; /* tell callers */
947 /* that con has */
948 /* died */
949 }
950 break;
951 }
952
953 half_con->flags |= HCON_PROGRESS;
954 FIX_BUFFER_POINTERS(half_con, count)
955 }
956 /*
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.
960 */
961 fdbits = &gdb_cwfds;
962 if (half_con->remaining >0 && gdb_conok)
963 FD_SET(half_con->fd, fdbits);
964 else
965 FD_CLR(half_con->fd, fdbits);
966
967 return;
968}
969\f
970/************************************************************************/
971/*
972/*
973/* gdb_read_data_and_buffer
974/*
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
979/* buffer for later.
980/*
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.
983/*
984/************************************************************************/
985
986int
987gdb_read_data_and_buffer(hc)
988struct half_con_data *hc; /* pointer to control struct */
989 /* for this half connection */
990{
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 */
996 /* attempt */
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*/
1006
1007 /*----------------------------------------------------------*/
1008 /*
1009 /* Mark the stream buffer as empty, in case we don't
1010 /* get around to filling it.
1011 /*
1012 /*----------------------------------------------------------*/
1013
1014 half_con -> stream_buffer_next = half_con -> stream_buffer;
1015 half_con -> stream_buffer_remaining = 0;
1016
1017 /*----------------------------------------------------------*/
1018 /*
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
1024 /* full.
1025 /*
1026 /*----------------------------------------------------------*/
1027
1028 /*
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.
1040 */
1041
1042 while(half_con->remaining>0) {
1043 /*
1044 * First we try a read, and if it works, we believe it
1045 */
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);
1051
1052 if (count<0) {
1053 count = 0;
1054 if (errno != EWOULDBLOCK)
1055 gdb_conok = FALSE; /* tell callers that */
1056 /* con has died */
1057 break;
1058
1059 }
1060 if (count == 0) {/* We hit EOF */
1061 gdb_conok = FALSE;
1062 break;
1063 }
1064
1065 /*
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.
1070 */
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);
1075 }
1076
1077 /*
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.
1081 */
1082 fdbits = &gdb_crfds;
1083 if (half_con->remaining >0)
1084 FD_SET(half_con->fd, fdbits);
1085 else
1086 FD_CLR(half_con->fd, fdbits);
1087
1088 return ;
1089}
1090\f
1091/************************************************************************/
1092/*
1093/* gdb_receive_data (gdb_receive_data)
1094/*
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.
1101/*
1102/************************************************************************/
1103
1104int
1105gdb_receive_data(half_con, ptr, len)
1106HALF_CONNECTION half_con; /* read on this connection*/
1107char *ptr; /* put first byte here */
1108int len; /* number of bytes to read */
1109{
1110 /*
1111 * Fill in the initial state of the attempted receive
1112 */
1113 half_con->remaining = len;
1114 half_con->next_byte = ptr;
1115
1116 /*
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.
1121 */
1122 if(gdb_move_data(CON_INPUT, half_con))
1123 return OP_COMPLETE;
1124 else
1125 return OP_RUNNING;
1126}
1127
1128/************************************************************************/
1129/*
1130/* gdb_send_data (gdb_send_data)
1131/*
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.
1137/*
1138/************************************************************************/
1139
1140int
1141gdb_send_data(half_con, ptr, len)
1142HALF_CONNECTION half_con; /* write on this connection*/
1143char *ptr; /* put first byte here */
1144int len; /* number of bytes to read */
1145{
1146
1147 /*
1148 * Fill in the initial state of the attempted receive
1149 */
1150 half_con->remaining = len;
1151 half_con->next_byte = ptr;
1152
1153 /*
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.
1157 */
1158 if(gdb_move_data(CON_OUTPUT, half_con))
1159 return OP_COMPLETE;
1160 else
1161 return OP_RUNNING;
1162}
1163
1164/************************************************************************/
1165/*
1166/* gdb_start_a_listen
1167/*
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.
1172/*
1173/************************************************************************/
1174
1175int
1176gdb_start_a_listen(half_con, otherside, lenp, fdp)
1177HALF_CONNECTION half_con; /* write on this connection*/
1178char *otherside; /* put first byte here */
1179int *lenp; /* number of bytes to read */
1180int *fdp;
1181{
1182
1183 /*
1184 * Fill in the initial state of the attempted accept
1185 */
1186 half_con->accepted_len = lenp;
1187 half_con->next_byte = otherside;
1188 half_con->accepted_fdp = fdp;
1189
1190 /*
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.
1194 */
1195 if(gdb_listen(half_con))
1196 return OP_COMPLETE;
1197 else
1198 return OP_RUNNING;
1199}
1200\f
1201/************************************************************************/
1202/*
1203/* gdb_listen (gdb_listen)
1204/*
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.
1208/*
1209/************************************************************************/
1210
1211int
1212gdb_listen(hc)
1213struct half_con_data *hc; /* pointer to control struct */
1214 /* for this half connection */
1215{
1216 register HALF_CONNECTION half_con = hc;
1217 /* half connection pointer */
1218 /* fast copy in register */
1219
1220 GDB_INIT_CHECK
1221
1222 half_con->flags &= ~HCON_PENDING_LISTEN;/* in case we succeed */
1223
1224 /*
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.
1228 */
1229#ifdef notdef
1230 FD_ZERO(&tst_bits);
1231 FD_SET(half_con->fd,&tst_bits);
1232 selected = select(gdb_mfd,&tst_bits, (fd_set *)NULL, (fd_set *)NULL,
1233 &gdb_notime);
1234 /*
1235 * If selected==(-1), then we know there's something
1236 * wrong with the socket
1237 */
1238 if (selected == (-1)) {
1239 gdb_conok = FALSE;
1240 return FALSE;
1241 }
1242 /*
1243 * if selected==0, then we know accept won't do anything, so
1244 * don't try.
1245 */
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*/
1250 return FALSE;
1251 }
1252 /*
1253 * Selected is >0. The accept SHOULD not hang.
1254 */
1255#endif notdef
1256
1257 /*
1258 * Here is Bill's non-blocking implementation
1259 */
1260 *(half_con->accepted_fdp) = accept(half_con->fd,
1261 (struct sockaddr *)half_con->next_byte,
1262 half_con->accepted_len);
1263 /*
1264 * See whether the accept succeeded
1265 */
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*/
1270 }
1271 half_con->flags |= HCON_PENDING_LISTEN;
1272 FD_SET(half_con->fd, &gdb_crfds);
1273 return FALSE;
1274 }
1275
1276 FD_CLR(half_con->fd, &gdb_crfds); /* don't select on this */
1277 return TRUE;
1278}
This page took 0.212555 seconds and 5 git commands to generate.