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