]> andersk Git - moira.git/blame - gdb/gdb_trans2.c
Added make depend.
[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
38/*
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.
42/*
43/* Some of the routines specified are actually implemented as
44/* macros defined in gdb.h.
45/*
46/************************************************************************/
47
48#include <sys/types.h>
49#include <errno.h>
50#include <stdio.h>
51#include "gdb.h"
52#include <sys/uio.h>
53#include <sys/socket.h>
54extern int errno; /* Unix error slot */
55
56/*
57 * The following values are returned by g_con_progress
58 */
59#define NOPROGRESS 0 /* nothing happened on this */
60 /* connection--must be 0*/
61#define PROGRESS 1 /* connection has progressed */
62#define COMPLETE 2 /* an operation has */
63 /* completed on this con */
64\f
65/************************************************************************/
66/*
67/* queue_operation(queue_operation)
68/*
69/* Add an operation to the queue for a given connection, and
70/* then allows all connections to progress. Returns the last
71/* known status of the operation.
72/*
73/************************************************************************/
74
75int
76queue_operation(con, direction, op)
77CONNECTION con;
78int direction;
79OPERATION op;
80{
81 register HALF_CONNECTION hcon = (direction==CON_INPUT)?(&(con->in)):
82 (&(con->out));
83 GDB_CHECK_CON(con, "queue_operation")
84 /*
85 * Write message to debugging log
86 */
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");
91 /*
92 * Make sure connection is up
93 */
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");
98 return OP_CANCELLED;
99 }
100
101 /*
102 * Put the new operation at the end of the queue
103 */
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;
108 /*
109 * Mark it as queued
110 */
111 op->status = OP_QUEUED;
112 op->halfcon = hcon;
113
114 /*
115 * Force progress on this connection
116 */
117 (void) g_con_progress(con - gdb_cons);
118 /*
119 * Con_select with notime is used here as a kind of fudge for
120 * doing a fastprogress with a select built in before it.
121 */
122 (void) con_select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0,
123 &gdb_notime);/* XXX */
124 /*
125 * Return the last known status of the operation
126 */
127 return op->status;
128}
129/************************************************************************/
130/*
131/* requeue_operation
132/*
133/* This routine may be called from an init or continuation routine
134/* to cause the current operation to be requeued on a new connection.
135/* The init routine field ofthe operation should be properly set to
136/* indicate the routine to receive control when the operation actually
137/* runs on the new connection. The caller of this routine is
138/* responsible for returning the status OP_REQUEUED to its caller.
139/*
140/* This routine returns the status of the newly queued operation.
141/* Note, however, that even if this operation returns the status
142/* CANCELLED, the operation itself may not continue to execute
143/* on the old connection and it should return the status OP_REQUEUED,
144/* NOT OP_CANCELLED (at least in this implementation.)
145/*
146/************************************************************************/
147
148int
149requeue_operation(con, direction, op)
150CONNECTION con;
151int direction;
152OPERATION op;
153{
154 /*
155 * Make sure the connection supplied is a legal one
156 */
157 GDB_CHECK_CON(con, "requeue_operation")
158 /*
159 * Write message to debugging log
160 */
161 if (gdb_Debug & GDB_LOG)
162 fprintf(gdb_log, "op requeued: new con=0x%x dir=%s op=0x%x\n",
163 con, (direction == CON_INPUT)?"INPUT":"OUTPUT",
164 op);
165 /*
166 * Dequeue the operation from its old half connection
167 */
168 (void) g_op_newhead(op->halfcon);
169
170 /*
171 * Now queue it on the new one
172 */
173 return queue_operation(con, direction, op);
174}
175
176/************************************************************************/
177/*
178/* g_preempt_me
179/*
180/* Sticks a new operation in ahead of the current one and runs it
181/* on the current connection. May be called only from an init or
182/* continuation routine. The old operation must have completely
183/* prepared the descriptor for the new operation, i.e. it should
184/* be in the same state as it would be for a call to queue_operation.
185/* g_preempt_me makes it possible for operations to be built by
186/* composition of other smaller operations, since newop runs, in
187/* a sense, as a subroutine of oldop. opdop must (1) reset its
188/* initialization routine to be a routine to be called when newop
189/* completes or cancels and (2) return the status OP_PREEMPTED to
190/* its caller.
191/*
192/************************************************************************/
193
194int
195g_preempt_me(oldop, newop)
196OPERATION oldop;
197OPERATION newop;
198{
199 register OPERATION old=oldop, new=newop;
200 register HALF_CONNECTION hc = old->halfcon;
201
202 /*
203 * Write message to debugging log
204 */
205 if (gdb_Debug & GDB_LOG)
206 fprintf(gdb_log, "op preempted: halfcon=0x%x oldop=0x%x newop=0x%x\n",
207 oldop,newop);
208 /*
209 * link in the new operation
210 */
211 old->prev = new;
212 hc->op_q_first = new;
213 new->prev = (OPERATION)hc;
214 new->next = old;
215 /*
216 * Set the status of the new operation
217 */
218 new->status = OP_QUEUED;
219 new->halfcon = hc;
220 /*
221 * Change the status of the old operation (one could argue that
222 * this should be done in gdb_hcon_progress after the return code
223 * is detected.)
224 */
225 old->status = OP_QUEUED;
226 return OP_QUEUED;
227}
228
229
230\f
231/************************************************************************/
232/*
233/* gdb_progress
234/*
235/* This routine should be called whenever it is suspected that
236/* progress can be made on any connection. This routine will
237/* cause all connections to proceed as far as they can without
238/* blocking, and will make a best effort to avoid long blocks.
239/* This routine MAY retain control for long periods when sustained
240/* progress is possible, but it will not knowingly hang.
241/*
242/* Returns: number of connections on which OPERATIONS have
243/* COMPLETED (not just progressed).
244/*
245/************************************************************************/
246
247int
248
249gdb_progress()
250{
251 register int i; /* index to available */
252 /* connections */
253 register int return_value = 0; /* the value we return */
254 int rc; /* short term storage for */
255 /* a return code */
256 int progress_made; /* true when some con */
257 /* made progress during */
258 /* latest pass through list */
259 int complete_map[GDB_MAX_CONNECTIONS]; /* indicates whether a */
260 /* transmission operation */
261 /* is newly complete on */
262 /* corresponding connection */
263 /* 1 if yes else 0 */
264 int maxcon = gdb_mcons; /* gdb_mcons may change */
265 /* out from under us if */
266 /* connections break. This */
267 /* is the initial value. */
268
269 /*
270 * Zero out the completion map for all connections.
271 */
272 for (i=0; i<maxcon; i++)
273 complete_map[i]=0;
274
275 /*
276 * Make repeated passes through all the fd's until a pass is made
277 * in which none makes any progress. This logic is important,
278 * because it catches the case where A is blocked, B makes progress,
279 * and A unblocks during the period where B is progressing.
280 */
281
282 do {
283 progress_made = FALSE;
284 for (i=0; i<gdb_mcons; i++) {
285 if (rc = g_con_progress(i)) { /* note: NOPROGRESS==0 */
286 progress_made = TRUE;
287 if (rc == COMPLETE)
288 complete_map[i] = 1;
289 }
290 }
291 } while (progress_made);
292
293 /*
294 * We've gone as far as we can, now find out how many connections
295 * have had operations complete.
296 */
297 for (i=0; i<maxcon; i++)
298 return_value += complete_map[i];
299
300 return return_value;
301}
302
303\f
304/************************************************************************/
305/*
306/* gdb_fastprogress
307/*
308/* Similar to gdb_progress, but this routine attempts progress
309/* only on those connections which have already shown themselves
310/* to be ready for activity by a prior select. This is safe to do
311/* when (1) the only activity we are interested in is that related
312/* to ongoing I/O and (2) a select was recently done to set the
313/* last_c.fds flags. Condition (1) is violated in the case where
314/* an operation may be newly at the head of a queue and its init
315/* routine may not have had a chance to run. Condition (2) is violated
316/* when we are entering after having done significant computation.
317/*
318/* This routine was introduced by Bill Sommerfeld after profiling
319/* revealed that unnecessary attempts to progress on quiescent
320/* sockets were causing excessive overhead in the system. I am
321/* still suspicious that this routine may be getting called in
322/* places where a full gdb_progress is needed. e.g. I'm not
323/* sure its use in op_select is entirely safe.
324/*
325/************************************************************************/
326
327gdb_fastprogress()
328{
329 int i;
330 int retval=0, rc;
331
332 for (i=0; i<gdb_mcons; i++) {
333 register CONNECTION con = &gdb_cons[i];
334 register int infd = gdb_cons[i].in.fd;
335 register int outfd = gdb_cons[i].out.fd;
336
337 if(connection_status(con) != CON_UP)
338 continue;
339
340 gdb_conok = TRUE;
341 if ((!(con->in.flags&HCON_UNUSED))&&
342 ((con->in.stream_buffer_remaining > 0)
343 || FD_ISSET(infd, &last_crfds))) {
344 rc = gdb_hcon_progress(CON_INPUT, &con->in);
345 if (!gdb_conok) {
346 g_stop_with_errno(con);
347 rc = COMPLETE;
348 }
349 if (rc == COMPLETE)
350 retval++;
351 }
352 if ((!(con->out.flags&HCON_UNUSED))&&
353 (FD_ISSET(outfd, &last_cwfds))) {
354 rc = gdb_hcon_progress(CON_OUTPUT, &con->out);
355 if (!gdb_conok) {
356 g_stop_with_errno(con);
357 rc = COMPLETE;
358 }
359 if (rc == COMPLETE)
360 retval++;
361 }
362 }
363 /*
364 * We've gone as far as we can, now find out how many connections
365 * have had operations complete.
366 */
367
368 return retval;
369}
370\f
371/************************************************************************/
372/*
373/* g_con_progress
374/*
375/* Make as much progress as possible on the specified connection.
376/* Returns NOPROGRESS if no bytes moved on either half connection,
377/* PROGRESS, if some moved and no operations completed, or COMPLETE if
378/* any of the operations completed. Note that each connection
379/* consists of two half connections, and we must make each of them
380/* progress as far as possible.
381/*
382/* The nest here starts getting so deep that it's hard to pass state
383/* around efficiently. We use a single global variable, gdb_conok,
384/* to indicate whether the connection we're working on now has died.
385/* The move data routines set this to FALSE whenever there is a
386/* fatal error on a connection. We check it, and do a proper
387/* sever on the connection if it seems to be in trouble.
388/*
389/************************************************************************/
390
391
392int
393g_con_progress(con_id)
394int con_id; /* index of this connection */
395 /* in the connection desc. */
396 /* arrays*/
397{
398 register CONNECTION con= (&gdb_cons[con_id]);
399 /* pointer to the connection */
400 /* data structure */
401 register int progress = NOPROGRESS;
402 register int live = TRUE; /* true when we've seen */
403 /* enough to make sure we */
404 /* want to go around again*/
405 int rc;
406 /*
407 * Check status of connection-if it's not running, then just return.
408 */
409 if (con->status != CON_UP)
410 return NOPROGRESS;
411 /*
412 * Repeatedly make progress on each half connection until both
413 * are idle. Important to keep trying as one may become active
414 * while the other is progressing.
415 */
416
417 gdb_conok = TRUE; /* this gets set to FALSE */
418 /* for fatal I/O errors */
419 /* there may be a timing */
420 /* window here in use of */
421 /* HCON_BUSY. Also: it is */
422 /* essential that errno */
423 /* remain valid after conok */
424 /* goes bad */
425 while (live) {
426 live = FALSE; /* until proven otherwise */
427 /*
428 * make progress on the input connection note that following
429 * logic depends on NOPROGRESS being 0
430 */
431 if (rc = gdb_hcon_progress(CON_INPUT, &con->in)) {
432 live = TRUE;
433 progress = max(rc, progress);
434 }
435 /*
436 * See if connection has died
437 */
438 if (!gdb_conok) {
439 g_stop_with_errno(con);
440 return COMPLETE; /* dying connection always */
441 /* implies that the */
442 /* operation at the head */
443 /* of the queue completed */
444 }
445 /*
446 * make progress on the output connection
447 */
448 if (rc = gdb_hcon_progress(CON_OUTPUT, &con->out)) {
449 live = TRUE;
450 progress = max(rc, progress);
451 }
452 /*
453 * See if connection has died
454 */
455 if (!gdb_conok) {
456 g_stop_with_errno(con);
457 return COMPLETE;
458 }
459 }
460
461 return progress;
462}
463
464
465/************************************************************************/
466/*
467/* gdb_hcon_progress
468/*
469/* Allows a specified half-connection to progress as much as possible,
470/* and returns true iff at least one operation is newly completed.
471/*
472/************************************************************************/
473
474int
475gdb_hcon_progress(direction, hc)
476int direction; /* CON_INPUT or CON_OUTPUT */
477struct half_con_data *hc; /* pointer to control struct */
478 /* for this half connection */
479{
480 HALF_CONNECTION half_con = hc;
481 /* half connection pointer */
482 /* fast copy in register */
483 register OPERATION op; /* current operation on this */
484 /* half connection */
485 int progress = NOPROGRESS; /* can indicate any progress */
486 /* on the half con or */
487 /* whether any operations */
488 /* completed */
489 int done; /* true when no more progress*/
490 /* can be made */
491 int fcn_result; /* result of latest init or */
492 /* continue function */
493
494 /*
495 * Write message to debugging log
496 */
497 if (gdb_Debug & GDB_LOG)
498 fprintf(gdb_log, "hcon_progress: halfcon=0x%x dir=%s ",
499 half_con, (direction==CON_INPUT)?"INPUT":"OUTPUT");
500
501 /*----------------------------------------------------------*/
502 /*
503 /* See if we are being re-entered and are already working
504 /* on this half_con. If so, return right now.
505 /*
506 /*----------------------------------------------------------*/
507
508 if (half_con->flags & HCON_BUSY) {
509 /*
510 * Write message to debugging log
511 */
512 if (gdb_Debug & GDB_LOG)
513 fprintf(gdb_log, "BUSY, returning\n");
514 return NOPROGRESS;
515 }
516
517 /*----------------------------------------------------------*/
518 /*
519 /* See if there is an operation on this half connection.
520 /* If not, return.
521 /*
522 /*----------------------------------------------------------*/
523
524
525 op = half_con->op_q_first; /* pick up first operation */
526 /* in queue */
527 if (op == (OPERATION)half_con) { /* see if end of circular */
528 /* list */
529 /*
530 * Write message to debugging log
531 */
532 if (gdb_Debug & GDB_LOG)
533 fprintf(gdb_log, "Q EMPTY, returning\n");
534 return NOPROGRESS; /* nothing to do on */
535 /* this half session */
536 }
537
538
539 /*----------------------------------------------------------*/
540 /*
541 /* Loop until all operations are complete, or until no further
542 /* progress can be made on this one.
543 /*
544 /* Loop invariants:
545 /*
546 /* 1) Op contains the operation at the head of the q, or
547 /* else is == half_con, indicating no more operationos
548 /* to be processed.
549 /*
550 /* 2) The operation at the head of the queue is either running
551 /* or continuing. As soon as one completes, it is dequeued.
552 /*
553 /* Progress is declared whenever an operation newly
554 /* returns OP_COMPLETE, i.e. whenever there has been
555 /* an operation which went from running to complete.
556 /*
557 /* Done is declared whenever an operation returns anything
558 /* other than complete, indicating that it cannot progress
559 /* further at this time. Loop ends.
560 /*
561 /* While we're here, mark us busy so we won't try the
562 /* same half_con on reentry.
563 /*
564 /*----------------------------------------------------------*/
565
566 done = FALSE; /* this one may be able to */
567 /* progress */
568
569 half_con->flags |= HCON_BUSY; /* don't try this hcon */
570 /* while we already doing */
571 /* it. Could happen if */
572 /* we queue new ops */
573 half_con->flags &= ~HCON_PROGRESS; /* gdb_move_data will */
574 /* indicate progress here*/
575 if (gdb_Debug & GDB_LOG)
576 fprintf(gdb_log, "LOOPING\n");
577
578 /*----------------------------------------------------------*/
579 /*
580 /* Loop through the operations queued on this half con
581 /* trying to make progress on them, in order.
582 /*
583 /*----------------------------------------------------------*/
584
585 while (!done &&
586 op != (OPERATION)half_con) {
587
588 if (gdb_Debug & GDB_LOG)
589 fprintf(gdb_log, "\top=0x%x status%d...",
590 op, OP_STATUS(op));
591
592 switch (op->status) {
593 /*
594 * Operation is at head of queue for first time and has
595 * never been started. Try to start it up.
596 */
597 case OP_QUEUED:
598 /*
599 * Call the initialization routine for this operation
600 */
601 fcn_result = (*op->fcn.init)(op,half_con,op->arg);
602 if (gdb_Debug & GDB_LOG)
603 fprintf(gdb_log, "init result=%d\n",
604 fcn_result);
605
606 switch (fcn_result) {
607 case OP_COMPLETE:
608 case OP_CANCELLED:
609 op->status = fcn_result;
610 op = g_op_newhead(half_con);
611 progress = COMPLETE;
612 break;
613 case OP_PREEMPTED:
614 op->status = OP_QUEUED;
615 /* fall thru */
616 case OP_REQUEUED:
617 /* important: don't set status on re-queued */
618 /* op as it may already have completed in */
619 /* its second life ! */
620 op = half_con->op_q_first;
621 progress = max(progress, PROGRESS);
622 break;
623 default:
624 op->status = fcn_result;
625 done = TRUE; /* could not get done */
626 }
627 break;
628 /*
629 * Operation is at head of queue and has already
630 * started trying to run. The only reason we could be in this
631 * state is that the last time we tried to do the requested input
632 * or output, all the data could not be moved synchronously.
633 * We therefore try to move some more, and if it all goes now,
634 * we call the continuation routine.
635 */
636 case OP_RUNNING:
637 /*
638 * Try to move some more data. If it won't all
639 * go now, we're done with this half connection.
640 *
641 * If this is a special listening connection which
642 * has an operation queued trying to do a listen,
643 * then do the listen. Otherwise do an ordinary
644 * data move.
645 */
646 if (half_con->flags & HCON_PENDING_LISTEN) {
647 if (gdb_listen(half_con)==FALSE) {
648 if (gdb_Debug & GDB_LOG)
649 fprintf(gdb_log, "NO LISTEN\n");
650 done = TRUE;
651 break;
652 }
653 } else
654 if (gdb_move_data(direction, half_con)==FALSE) {
655 done = TRUE;
656 if (gdb_Debug & GDB_LOG)
657 fprintf(gdb_log, "NO DATA\n");
658 break;
659 }
660 /*
661 * The pending data transmission has now completed.
662 * Call the continuation routine for this operation
663 */
664 fcn_result = (*op->fcn.cont)(op,half_con,op->arg);
665 if (gdb_Debug & GDB_LOG)
666 fprintf(gdb_log, "cont result=%d\n",
667 fcn_result);
668
669 switch (fcn_result) {
670 case OP_COMPLETE:
671 case OP_CANCELLED:
672 op->status = fcn_result;
673 op = g_op_newhead(half_con);
674 progress = COMPLETE;
675 break;
676 case OP_PREEMPTED:
677 op->status = OP_QUEUED;
678 /* fall thru */
679 case OP_REQUEUED:
680 /* important: don't set status on re-queued */
681 /* op as it may already have completed in */
682 /* its second life ! */
683 op = half_con->op_q_first;
684 progress = max(progress, PROGRESS);
685 break;
686 default:
687 op->status = fcn_result;
688 done = TRUE; /* could not get done */
689 }
690 break;
691 /*
692 * Following cases are all unexpected, at least for the
693 * moment. (See explanation of loop invariants for this while
694 * loop. Give up if they turn up.
695 */
696 case OP_COMPLETE:
697 GDB_GIVEUP("gdb_hcon_progress: found OP_COMPLETE on q")
698 case OP_CANCELLED:
699 GDB_GIVEUP("gdb_hcon_progress: found OP_CANCELLED on q")
700 case OP_CANCELLING:
701 GDB_GIVEUP("gdb_hcon_progress: OP_CANCELLING")
702 default:
703 GDB_GIVEUP("gdb_hcon_progress: Operation is queued, but is not runnable")
704 }
705 }
706
707 if (progress == NOPROGRESS && (half_con->flags & HCON_PROGRESS))
708 progress = PROGRESS;
709
710 half_con->flags &= ~HCON_BUSY;
711
712 if (gdb_Debug & GDB_LOG)
713 fprintf(gdb_log, "hcon_progress: returns %d\n",progress);
714
715 return progress; /* NOPROGRESS, PROGRESS */
716 /* or COMPLETE */
717}
718\f
719/************************************************************************/
720/*
721/* g_op_newhead
722/*
723/* Dequeues the operation at the head of the queue for the
724/* given half connection and returns the pointer to the
725/* new head of the queue. If the queue is null, then a pointer
726/* to the half_con itself is returned. (The lists are
727/* linked circularly.)
728/*
729/************************************************************************/
730
731OPERATION
732g_op_newhead(hcp)
733HALF_CONNECTION hcp;
734{
735 register OPERATION newhead, oldhead;
736
737 /*
738 * Get old and new heads of chain
739 */
740 oldhead = hcp->op_q_first;
741 newhead = oldhead->next;
742 /*
743 * Make sure nobody chained a bad one on us
744 */
745 if (newhead == NULL) {
746 if (gdb_Debug & GDB_LOG) {
747 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",
748 oldhead, newhead, hcp, hcp->op_q_first,
749 hcp->op_q_last);
750 }
751 GDB_GIVEUP("g_op_newhead: found NULL chain link")
752 }
753 /*
754 * Remove oldhead from chain, fixing up chain pointers
755 */
756 newhead->prev = oldhead->prev;
757 hcp->op_q_first = newhead;
758
759 /*
760 * Clean up pointers in the newly dequeued operation. This is
761 * just for cleanliness and ease of debugging.
762 */
763 oldhead->next = oldhead->prev = NULL;
764 oldhead->halfcon = NULL;
765
766 return newhead;
767}
768\f
769/************************************************************************/
770/*
771/* gdb_move_data
772/*
773/* This routine attempts to make further progress on the pending
774/* level transmission operation pending on this half connection.
775/* (Presumes that such an operation is pending.) Returns TRUE
776/* if all the requested data has been moved, else FALSE.
777/*
778/* We assume here that all fd's are set to non-blocking I/O, so
779/* we can safely try reading and writing until they return 0 bytes.
780/*
781/************************************************************************/
782
783#define FIX_BUFFER_POINTERS(hc, count) if (count>0) {hc->next_byte += count; \
784 hc->remaining -= count;}
785int
786gdb_move_data(direction, hc)
787int direction; /* CON_INPUT or CON_OUTPUT */
788struct half_con_data *hc; /* pointer to control struct */
789 /* for this half connection */
790{
791 register HALF_CONNECTION half_con = hc;
792 /* half connection pointer */
793 /* fast copy in register */
794 register fd_set *fdbits; /* the mask we should adjust */
795 /* for this direction */
796
797 /*
798 * For safety, in case we're called when nothing is pending.
799 */
800 if (half_con->remaining == 0)
801 return TRUE;
802 /*
803 * Move the data into the user's buffer. In the case of input
804 * data may come first from the stream buffer, then from the socket
805 * itself.
806 */
807 if (direction == CON_INPUT) {
808 gdb_transfer_from_buffer(half_con);
809 /*
810 * If remaining is greater than 0, then we emptied
811 * the stream buffer and still weren't done. Try
812 * to read it from the pipe and re-fill the stream
813 * buffer.
814 */
815 if (half_con->remaining) {
816 gdb_read_data_and_buffer(half_con);
817 }
818 } else {
819 gdb_write_data(half_con);
820 }
821 /*
822 * The file descriptor masks used for doing selects must be activated
823 * when and only when there is a pending operation trying to use
824 * the connection. Update the masks for this half connection.
825 */
826 fdbits = (direction == CON_INPUT)? &gdb_crfds : &gdb_cwfds;
827 if (half_con->remaining >0 && gdb_conok)
828 FD_SET(half_con->fd, fdbits);
829 else
830 FD_CLR(half_con->fd, fdbits);
831
832 return (half_con->remaining == 0);
833}
834\f
835/************************************************************************/
836/*
837/* gdb_transfer_from_buffer
838/*
839/* Given an inbound half connection, satisfy as much as possible
840/* of desired data from the stream buffer.
841/*
842/************************************************************************/
843
844int
845gdb_transfer_from_buffer(hc)
846register HALF_CONNECTION hc;
847{
848 register int count; /* amount to move */
849
850 /*
851 * Figure out how much, if any, we'll be able to do here
852 */
853 count = min(hc->remaining, hc->stream_buffer_remaining);
854 if (count <= 0)
855 return; /* could not satisfy */
856 /* any from buffered data*/
857
858 /*
859 * Copy the data, update both stream and data buffer pointers
860 */
861
862 bcopy(hc->stream_buffer_next, hc->next_byte, count);
863
864 hc->stream_buffer_next += count;
865 hc->stream_buffer_remaining -= count;
866 FIX_BUFFER_POINTERS(hc, count)
867
868}
869
870\f/************************************************************************/
871/*
872/* gdb_write_data
873/*
874/* This routine implements gdb_move_data for an outbound half
875/* connection.
876/*
877/************************************************************************/
878
879int
880gdb_write_data(hc)
881struct half_con_data *hc; /* pointer to control struct */
882 /* for this half connection */
883{
884 register HALF_CONNECTION half_con = hc;
885 /* half connection pointer */
886 /* fast copy in register */
887 register int count; /* number of bytes read */
888 /* or written in latest */
889 /* attempt */
890 fd_set *fdbits; /* the mask we should adjust */
891 /* for this direction */
892 fd_set tst_bits; /* these are used for */
893 /* the select we do prior */
894 /* to reading which tells */
895 /* us whether 0 byte read */
896 /* means empty or closed */
897 int selected; /* TRUE iff select says */
898 /* we should be able to */
899 /* progress */
900
901 /*
902 * Loop writing to the socket until it claims that no more
903 * progress can be made. Note that some versions of Unix report
904 * socket failure by select = 1, write count = 0. To avoid
905 * extra selects, we try the write first, and only do the select/write
906 * sequence if write seems not to be progressing.
907 */
908 FD_ZERO(&tst_bits);
909 while(half_con->remaining>0) {
910 count = write(half_con->fd, half_con->next_byte,
911 (int)min(half_con->remaining,
912 GDB_MAX_SOCK_WRITE));
913 if (count == 0) {
914 FD_SET(half_con->fd,&tst_bits);
915 selected = select(gdb_mfd,
916 (fd_set *)NULL, &tst_bits,
917 (fd_set *)NULL,
918 &gdb_notime);
919 if (selected == (-1)) {
920 gdb_conok = FALSE;
921 break;
922 }
923 if (selected == 0) {
924 count =0;
925 break;
926 }
927 count = write(half_con->fd, half_con->next_byte,
928 (int)min(half_con->remaining,
929 GDB_MAX_SOCK_WRITE));
930 if (count==0) {
931 if (selected == 1)
932 gdb_conok = FALSE;
933 break; /* no more data available now*/
934 }
935 }
936 /*
937 * Count is != 0
938 */
939 if (count<0) {
940 count = 0;
941 if (errno != EWOULDBLOCK) {
942 gdb_conok = FALSE; /* tell callers */
943 /* that con has */
944 /* died */
945 }
946 break;
947 }
948
949 half_con->flags |= HCON_PROGRESS;
950 FIX_BUFFER_POINTERS(half_con, count)
951 }
952 /*
953 * The file descriptor masks used for doing selects must be activated
954 * when and only when there is a pending operation trying to use
955 * the connection. Update the masks for this half connection.
956 */
957 fdbits = &gdb_cwfds;
958 if (half_con->remaining >0 && gdb_conok)
959 FD_SET(half_con->fd, fdbits);
960 else
961 FD_CLR(half_con->fd, fdbits);
962
963 return;
964}
965\f
966/************************************************************************/
967/*
968/*
969/* gdb_read_data_and_buffer
970/*
971/* This routine is called only when the half_connection stream
972/* buffer is known to be empty and the "next-byte" buffer
973/* has more to be filled in. We try in one read to finish
974/* off the user's request and at the same time fill the stream
975/* buffer for later.
976/*
977/* We assume here that all fd's are set to non-blocking I/O, so
978/* we can safely try reading and writing until they return 0 bytes.
979/*
980/************************************************************************/
981
982int
983gdb_read_data_and_buffer(hc)
984struct half_con_data *hc; /* pointer to control struct */
985 /* for this half connection */
986{
987 register HALF_CONNECTION half_con = hc;
988 /* half connection pointer */
989 /* fast copy in register */
990 register int count; /* number of bytes read */
991 /* or written in latest */
992 /* attempt */
993 fd_set *fdbits; /* the mask we should adjust */
994 /* for this direction */
995 struct iovec iov[2]; /* we use this to hold */
996 /* pointers to (1) the */
997 /* actual user data buffer */
998 /* and (2) the pipe length */
999 /* pre-read buffer */
1000 int fix_amount; /* amount to adjust */
1001 /* half_con->remaining*/
1002
1003 /*----------------------------------------------------------*/
1004 /*
1005 /* Mark the stream buffer as empty, in case we don't
1006 /* get around to filling it.
1007 /*
1008 /*----------------------------------------------------------*/
1009
1010 half_con -> stream_buffer_next = half_con -> stream_buffer;
1011 half_con -> stream_buffer_remaining = 0;
1012
1013 /*----------------------------------------------------------*/
1014 /*
1015 /* Loop trying to read data from the socket. We scatter
1016 /* first into the user's buffer directly, then into
1017 /* the stream buffer (which helps us save system
1018 /* calls next time around.) We stop either when:
1019 /* socket reports error/no progress or user's buffer is
1020 /* full.
1021 /*
1022 /*----------------------------------------------------------*/
1023
1024 /*
1025 * Loop until either (1) the connection reported that it could
1026 * not progress any further or (2) the full count has been
1027 * satisfied. Some versions of Unix observe the rule that
1028 * a closed connection, especially when reading, is indicated
1029 * by returning a count of 0 on read when select claims that progress
1030 * can be made. We used to handle this case. Bill Sommerfeld
1031 * has introduced a performance change which leaves that checking
1032 * out in the latest version. To add it back, then ONLY in
1033 * the case where read returned 0, do a select followed by another
1034 * read (the order is important). If we ever run on a system that
1035 * works in this way, we may hang at close time.
1036 */
1037
1038 while(half_con->remaining>0) {
1039 /*
1040 * First we try a read, and if it works, we believe it
1041 */
1042 iov[0].iov_base = half_con -> next_byte;
1043 iov[0].iov_len = half_con -> remaining;
1044 iov[1].iov_base = half_con -> stream_buffer;
1045 iov[1].iov_len = half_con -> stream_buffer_length;
1046 count = readv(half_con->fd, iov, 2);
1047
1048 if (count<0) {
1049 count = 0;
1050 if (errno != EWOULDBLOCK)
1051 gdb_conok = FALSE; /* tell callers that */
1052 /* con has died */
1053 break;
1054
1055 }
1056 if (count == 0) {/* We hit EOF */
1057 gdb_conok = FALSE;
1058 break;
1059 }
1060
1061 /*
1062 * Count is >0, we moved some data. Note, setting of
1063 * stream_buffer_remaining can only be non-zero on last
1064 * time through the loop, because that will be when
1065 * half_con->remaining goes to zero.
1066 */
1067 half_con->flags |= HCON_PROGRESS;
1068 half_con->stream_buffer_remaining=max(0, count-iov[0].iov_len);
1069 fix_amount = min(count,half_con->remaining);
1070 FIX_BUFFER_POINTERS(half_con, fix_amount);
1071 }
1072
1073 /*
1074 * The file descriptor masks used for doing selects must be activated
1075 * when and only when there is a pending operation trying to use
1076 * the connection. Update the masks for this half connection.
1077 */
1078 fdbits = &gdb_crfds;
1079 if (half_con->remaining >0)
1080 FD_SET(half_con->fd, fdbits);
1081 else
1082 FD_CLR(half_con->fd, fdbits);
1083
1084 return ;
1085}
1086\f
1087/************************************************************************/
1088/*
1089/* gdb_receive_data (gdb_receive_data)
1090/*
1091/* This routine is called by an init or continuation routine to
1092/* request that a specified amount of data be read, without
1093/* blocking, on the supplied connection. This routine returns
1094/* OP_COMPLETE if the entire read completed synchronously,
1095/* or OP_RUNNING if the read remains ongoing or is cancelling
1096/* due to error on the socket.
1097/*
1098/************************************************************************/
1099
1100int
1101gdb_receive_data(half_con, ptr, len)
1102HALF_CONNECTION half_con; /* read on this connection*/
1103char *ptr; /* put first byte here */
1104int len; /* number of bytes to read */
1105{
1106 /*
1107 * Fill in the initial state of the attempted receive
1108 */
1109 half_con->remaining = len;
1110 half_con->next_byte = ptr;
1111
1112 /*
1113 * Now see if we can make some progress on this read, possibly
1114 * even completing it synchronously. Return appropriate
1115 * result to our caller. Note: errors are reflected as OP_RUNNING
1116 * with global variable gdb_cnok set to FALSE.
1117 */
1118 if(gdb_move_data(CON_INPUT, half_con))
1119 return OP_COMPLETE;
1120 else
1121 return OP_RUNNING;
1122}
1123
1124/************************************************************************/
1125/*
1126/* gdb_send_data (gdb_send_data)
1127/*
1128/* This routine is called by an init or continuation routine to
1129/* request that a specified amount of data be written, without
1130/* blocking, on the supplied connection. This routine returns
1131/* OP_COMPLETE if the entire write completed synchronously,
1132/* or OP_RUNNING if the output remains ongoing or there was an error.
1133/*
1134/************************************************************************/
1135
1136int
1137gdb_send_data(half_con, ptr, len)
1138HALF_CONNECTION half_con; /* write on this connection*/
1139char *ptr; /* put first byte here */
1140int len; /* number of bytes to read */
1141{
1142
1143 /*
1144 * Fill in the initial state of the attempted receive
1145 */
1146 half_con->remaining = len;
1147 half_con->next_byte = ptr;
1148
1149 /*
1150 * Now see if we can make some progress on this read, possibly
1151 * even completing it synchronously. Return appropriate
1152 * result to our caller.
1153 */
1154 if(gdb_move_data(CON_OUTPUT, half_con))
1155 return OP_COMPLETE;
1156 else
1157 return OP_RUNNING;
1158}
1159
1160/************************************************************************/
1161/*
1162/* gdb_start_a_listen
1163/*
1164/* This routine is called by an init or continuation routine to
1165/* request that a connection be done. This routine returns
1166/* OP_COMPLETE if the accept completed synchronously,
1167/* or OP_RUNNING if the output remains ongoing or there was an error.
1168/*
1169/************************************************************************/
1170
1171int
1172gdb_start_a_listen(half_con, otherside, lenp, fdp)
1173HALF_CONNECTION half_con; /* write on this connection*/
1174char *otherside; /* put first byte here */
1175int *lenp; /* number of bytes to read */
1176int *fdp;
1177{
1178
1179 /*
1180 * Fill in the initial state of the attempted accept
1181 */
1182 half_con->accepted_len = lenp;
1183 half_con->next_byte = otherside;
1184 half_con->accepted_fdp = fdp;
1185
1186 /*
1187 * Now see if we can make some progress on this read, possibly
1188 * even completing it synchronously. Return appropriate
1189 * result to our caller.
1190 */
1191 if(gdb_listen(half_con))
1192 return OP_COMPLETE;
1193 else
1194 return OP_RUNNING;
1195}
1196\f
1197/************************************************************************/
1198/*
1199/* gdb_listen (gdb_listen)
1200/*
1201/* This routine is called from gdb_start_a_listen or hcon_progress to attempt
1202/* to continue making progress in accepting a connection on a
1203/* listening connection.
1204/*
1205/************************************************************************/
1206
1207int
1208gdb_listen(hc)
1209struct half_con_data *hc; /* pointer to control struct */
1210 /* for this half connection */
1211{
1212 register HALF_CONNECTION half_con = hc;
1213 /* half connection pointer */
1214 /* fast copy in register */
1215
1216 GDB_INIT_CHECK
1217
1218 half_con->flags &= ~HCON_PENDING_LISTEN;/* in case we succeed */
1219
1220 /*
1221 * The first implementatin of this used to do a select to make sure
1222 * that the accept would not block. Bill Sommerfeld has changed this
1223 * to non-blocking I/O, so the following code is commented out.
1224 */
1225#ifdef notdef
1226 FD_ZERO(&tst_bits);
1227 FD_SET(half_con->fd,&tst_bits);
1228 selected = select(gdb_mfd,&tst_bits, (fd_set *)NULL, (fd_set *)NULL,
1229 &gdb_notime);
1230 /*
1231 * If selected==(-1), then we know there's something
1232 * wrong with the socket
1233 */
1234 if (selected == (-1)) {
1235 gdb_conok = FALSE;
1236 return FALSE;
1237 }
1238 /*
1239 * if selected==0, then we know accept won't do anything, so
1240 * don't try.
1241 */
1242 if (selected == 0) {
1243 half_con->flags |= HCON_PENDING_LISTEN;
1244 FD_SET(half_con->fd, &gdb_crfds); /* we'll be looking for */
1245 /* this whenever we select*/
1246 return FALSE;
1247 }
1248 /*
1249 * Selected is >0. The accept SHOULD not hang.
1250 */
1251#endif notdef
1252
1253 /*
1254 * Here is Bill's non-blocking implementation
1255 */
1256 *(half_con->accepted_fdp) = accept(half_con->fd,
1257 (struct sockaddr *)half_con->next_byte,
1258 half_con->accepted_len);
1259 /*
1260 * See whether the accept succeeded
1261 */
1262 if (*(half_con->accepted_fdp) < 0) {
1263 if (errno != EWOULDBLOCK) {
1264 gdb_conok = FALSE; /* error will be returned */
1265 /* in shut-down listening con*/
1266 }
1267 half_con->flags |= HCON_PENDING_LISTEN;
1268 FD_SET(half_con->fd, &gdb_crfds);
1269 return FALSE;
1270 }
1271
1272 FD_CLR(half_con->fd, &gdb_crfds); /* don't select on this */
1273 return TRUE;
1274}
This page took 0.208465 seconds and 5 git commands to generate.