aboutsummaryrefslogtreecommitdiff
path: root/gdb/cadillac.c
blob: 22e7968888de0e0d13136b618b51fe6e528fe0e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/* Cadillac interface routines.
   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.

This file is part of GDB.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "defs.h"
#include "symtab.h"
#include "inferior.h"
#include "command.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <connection.h>
#include <genericreq.h>
#include <debuggerreq.h>
#include <debuggerconn.h>
#include <ttyconn.h>
#include <varargs.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/filio.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/errno.h>
#include <termios.h>
#include <strings.h>

/* Connection block for debugger<=>kernel communications. */
static Connection *conn = 0;

/* fd for our socket to the kernel. */
static int kerfd;

/* The kernel's ID for this instance of the program. */
static int program_id;

static int instance_id;

/* The fd for the pty associated with the inferior. */
static int inferior_pty = -1;
static int inferior_tty = -1;

static int has_run = 0;

extern int cadillac;

char **pprompt;			/* Pointer to pointer to prompt */

static void
prompt()
{
  fputs_filtered(*pprompt, stdout);
}

/* This routine redirects the output of fputs_filtered to the kernel so that
   the user can see what's going on in his debugger window. */

void
cadillac_fputs(ptr)
     char *ptr;
{
  if (conn)
    CVWriteTranscriptInfo (conn, instance_id, ptr);
  else
    fputs (ptr, stdout);
}

/* Copy all data from the pty to the kernel. */

static void
pty_to_kernel()
{
  CTtyRequest *req;
  char buf[1024];
  int cc;

  while (1)
    {
      cc = read(inferior_pty, buf, sizeof(buf));

      if (cc == 0
	  || (cc < 0
	      && errno == EWOULDBLOCK))
	break;

      if (cc < 0)
	{
	  close(inferior_pty);
	  inferior_pty = -1;
	  perror("pty read error");
	  break;
	}

      req = CWriteTtyRequest(conn, TextIORType);
      CWriteVstringLen(conn, buf, cc);
      CWriteLength(conn);
    }
  CWriteRequestBuffer(conn);
}

/* Copy data from the kernel to the pty. */

static void
kernel_to_pty(data, len)
     char *data;
     int len;
{
  int cc;

  cc = write(inferior_pty, data, len);

  if (cc != len)
    {
      if (cc < 0)
	{
	  close(inferior_pty);
	  inferior_pty = -1;
	  perror("pty write error");
	  return;
	}
      printf("Couldn't write all the data to the pty, wanted %d, got %d\n",
	     len, cc);
    }
}

/* Tell the kernel where we are in the program, and what the stack looks like.
   */

static void
send_status()
{
  static int linecount = 48;
  struct symtab_and_line sal;
  struct symbol *symbol;
  char *funcname, *filename;
  static int foo = 0;
  static int sent_prog_inst = 0;

  if (!has_run)
    return;

  if (inferior_pid == 0)	/* target has died */
    {
      CVWriteProgramTerminatedInfo(conn,
				   instance_id,
				   ""
				   );
      return;
    }

  sal = find_pc_line(stop_pc, 0);
  symbol = find_pc_function(stop_pc);

  funcname = symbol ? symbol->name : "";
  filename = sal.symtab ? sal.symtab->filename : "";

  if (!sent_prog_inst)
    {
      sent_prog_inst = 1;
      CVWriteProgramInstanceInfo(conn,
				 program_id,
				 instance_id,
				 "", /* hostname */
				 "", /* arglist */
				 ""
				 );
    }

  CVWriteStackSizeInfo(conn,
		       instance_id,
		       1, /* XXX - frame depth */
		       CInnerFrameIs0,
		       foo++ ? 1 : 0, /* XXX - frame diff */
		       ""
		       );

  CVWriteStackFrameInfo(conn,
			instance_id,
			sal.line,
			CFileLinePos,
			0,	/* XXX - frame # */
			funcname,
			filename,
			""	/* XXX ? transcript */
			);

  CVWriteProgramStoppedInfo(conn,
			    instance_id,
			    0,	/* XXX - breakpoint # or signal # */
			    CDebuggerCommand,
			    funcname,
			    ""	/* XXX ? transcript */
			    );
}

/* Tell the kernel that the target is now running. */

static void
go_busy()
{
  CVWriteProgramBusyInfo(conn,
			 instance_id,
			 "");	/* XXX ? transcript */
  CWriteRequestBuffer(conn);	/* Must take place synchronusly! */
}

/* This wrapper routine is needed because execute_command modifies the command
   string that it's given.  It also echos the commands. */

static void
execute_command_1(va_alist)
     va_dcl
{
  char buf[100];		/* XXX - make buf dynamic! */
  char *cmd;
  va_list args;

  va_start(args);

  cmd = va_arg(args, char *);

  vsprintf(buf, cmd, args);

  printf_filtered("%s\n", buf);
  execute_command(buf, 1);

  va_end(args);
}

#ifdef KERNEL_RECORD
FILE *kerout;

static int
kernel_record(fd, ptr, num)
     int fd, num;
     char *ptr;

{
  fwrite(ptr, num, 1, kerout);
  fflush(kerout);
  return write(fd, ptr, num);
}
#endif

void
cadillac_condition_breakpoint(b)
     struct breakpoint *b;
{
  CVWriteBreakConditionInfo(conn,
			    instance_id,
			    b->number,
			    b->cond_string ? b->cond_string : "",
			    ""	/* transcript */
			    );
}

void
cadillac_commands_breakpoint(b)
     struct breakpoint *b;
{
  struct command_line *l;

  CVWriteBreakCommandBegInfo(conn,
			     instance_id,
			     b->number,
			     ""); /* transcript */

  for (l = b->commands; l; l = l->next)
    CVWriteBreakCommandEntryInfo(conn,
				 instance_id,
				 l->line,
				 ""); /* transcript */

  CVWriteBreakCommandEndInfo(conn,
			     instance_id,
			     ""); /* transcript */
}

static void
breakpoint_notify(b, action)
     struct breakpoint *b;
     int action;
{
  struct symtab *s;
  struct symbol *s1;
  char *funcname = "", *filename = "", *included_in_filename = "";

  if (b->type != bp_breakpoint)
    return;

  s = b->symtab;

  if (s)
    {
      filename = s->filename;
      s1 = find_pc_function(b->address);
      if (s1)
	funcname = SYMBOL_NAME(s1);
    }      

  CVWriteBreakpointInfo (conn,
			 instance_id,
			 b->number,
			 b->line_number,
			 CFileLinePos,
			 CBreakOnInstrAccess,
			 action,
			 b->ignore_count,
			 funcname,
			 filename,
			 "",	/* included_in_filename */
			 ""	/* transcript */
			 );

  if (b->commands)
    cadillac_commands_breakpoint(b);
}

void
cadillac_create_breakpoint(b)
     struct breakpoint *b;
{
  breakpoint_notify(b, CEnableBreakpoint);
}

void
cadillac_delete_breakpoint(b)
     struct breakpoint *b;
{
  breakpoint_notify(b, CDeleteBreakpoint);
}

void
cadillac_enable_breakpoint(b)
     struct breakpoint *b;
{
  breakpoint_notify(b, CEnableBreakpoint);
}

void
cadillac_disable_breakpoint(b)
     struct breakpoint *b;
{
  breakpoint_notify(b, CDisableBreakpoint);
}

void
cadillac_ignore_breakpoint(b)
     struct breakpoint *b;
{
  breakpoint_notify(b, CBreakAttrUnchanged);
}

static int command_line_length = 0;
static char *command_line_text = 0;

int
cadillac_reading()
{
  if (command_line_text)
    return 1;
  else
    return 0;
}

char *
cadillac_command_line_input()
{
  char *p;

  if (command_line_length <= 0)
    return (char *)NULL;

  p = command_line_text;

  while (command_line_length-- > 0)
    {
      if (*command_line_text == '\n')
	{
	  *command_line_text = '\000';
	  command_line_text++;
	  break;
	}
      command_line_text++;
    }

  return p;
}

/* Open up a pty and its associated tty.  Return the fd of the tty. */

char *
cadillac_getpty()
{
  int n, ptyfd, ttyfd;
  static char dev[30];
  struct stat statbuf;
  struct termios termios;

#define HIGHPTY (('z' - 'p') * 16 - 1)

  if (inferior_pty >= 0)		/* Only do this once */
    return dev;

  for (n = 0; n <= HIGHPTY; n++)
    {
      sprintf(dev, "/dev/pty%c%x", n/16 + 'p', n%16);
      if (stat(dev, &statbuf))
	break;
      ptyfd = open(dev, O_RDWR);
      if (ptyfd < 0)
	continue;
      sprintf(dev, "/dev/tty%c%x", n/16 + 'p', n%16);
      ttyfd = open(dev, O_RDWR);
      if (ttyfd < 0) {close(ptyfd); continue;}

      /* Setup pty for non-blocking I/O.  Also make it give us a SIGIO when
	 there's data available.  */

      n = fcntl(ptyfd, F_GETFL, 0);
      fcntl(ptyfd, F_SETFL, n|FNDELAY|FASYNC);
      fcntl(ptyfd, F_SETOWN, getpid());

      tcgetattr(ttyfd, &termios);
      termios.c_oflag &= ~OPOST; /* No post-processing */
      tcsetattr(ttyfd, TCSANOW, &termios);

      inferior_pty = ptyfd;
      inferior_tty = ttyfd;
      return dev;
    }

  error ("getpty: can't get a pty\n");
}

/* Examine a protocol packet from the driver. */

static int
kernel_dispatch(interrupt)
     int interrupt;		/* Non-zero means we are at interrupt level
				   and can only do a few commands. */
{
  register CHeader *head;

  head = (CHeader *)CPeekNextRequest (conn);
  if (head == NULL)
    {
      fprintf (stderr, "EOF on kernel read!\n");
      exit (1);
    }

  if (interrupt)
    switch (head->reqType)
      {
      case KillProgramRType:
      case TextIORType:
      case StopRType:
	break;
      default:
	return;
      }

  if (head->reqType < LastTtyRequestRType)
    {
      CTtyRequest* req = CReadTtyRequest (conn);
      switch (req->head.reqType)
	{
	case AcceptConnectionRType:
	  /* Tell the rest of the world that cadillac is now set up */
	  CSkipRequest (conn);
	  break;

	case RefuseConnectionRType:
	  fprintf (stderr, "Debugger connection refused\n");
	  exit (1);

	case KillProgramRType:
	  exit (0);

	case TextIORType:
	  {
	    char *p;
	    ReqLen len;

	    p = CGetVstring(conn, &len);
	    kernel_to_pty(p, len);
	  }
	  break;
	default:
	  fprintf(stderr, "Unknown request type = %d\n",
		  req->head.reqType);
	  break;
	}
    }
  else
    {
      CVDebuggerRequest *req = CVReadDebuggerRequest (conn);
      if (!req)
	{
	  fprintf (stderr, "CVReadDebuggerRequest returned NULL, type = %d\n",
		   head->reqType);
	  exit(1);
	}

      switch (req->head.request->reqType)
	{
	case OpenProgramInstanceRType:
	  {
	    char *arglist, buf[100]; /* XXX - Make buf dynamic! */
	    int arglen;
	    /* XXX - should notice when program_id changes */
	    arglist = req->openProgramInstance.progArglist.text;
	    arglen = req->openProgramInstance.progArglist.byteLen;

	    execute_command_1("break main");
	    execute_command_1("enable delete $bpnum");
	    if (arglist)
	      {
		execute_command_1("set args %.*s", arglen, arglist);
	      }
	    execute_command_1("run");
	  }
	  break;
	case QuitDebuggerRType:
	  exit (0);
	case RunRType:
	  if (req->run.request->useArglist == CNewArglist)
	    {
	      execute_command_1("set args %.*s",
				req->run.progArglist.byteLen,
				req->run.progArglist.text);
	    }
	  execute_command_1("run");
	  break;
	case ContinueRType:
	  execute_command_1("continue");
	  break;
	case StepRType:
	  execute_command_1("step %d", req->step.request->stepCount);
	  break;
	case NextRType:
	  execute_command_1("next %d", req->next.request->nextCount);
	  break;
	case ChangeStackFrameRType:
	  switch (req->changeStackFrame.request->frameMovement)
	    {
	    case CToCurrentStackFrame:
	      execute_command_1("frame %d",
				req->changeStackFrame.request->frameNo);
	      break;
	    case CToInnerStackFrame:
	      execute_command_1("down %d",
				req->changeStackFrame.request->frameNo);
	      break;
	    case CToOuterStackFrame:
	      execute_command_1("up %d",
				req->changeStackFrame.request->frameNo);
	      break;
	    case CToAbsoluteStackFrame:
	      printf_filtered("ChangeStackFrame ToAbsolute\n");
	      break;
	    }
	  break;
	case BackTraceRType:
	  /* XXX - deal with limit??? */
	  execute_command_1("backtrace");
	  break;
	case FinishRType:
	  execute_command_1("finish");
	  break;
	case TerminateProgramRType:
	  execute_command_1("kill");
	  break;
	case NewBreakpointRType:
	  {
	    char *tail;
	    int skipped;

	    tail = rindex(req->newBreakpoint.fileName.text, '/');
	    if (!tail)
	      tail = req->newBreakpoint.fileName.text;
	    else
	      tail++;
	    skipped = tail - req->newBreakpoint.fileName.text;
	    execute_command_1("break %.*s:%d",
			      req->newBreakpoint.fileName.byteLen - skipped,
			      tail,
			      req->newBreakpoint.request->fileLinePos);
	  }
	  break;
	case StopRType:
	  {
	    extern int pgrp_inferior;
	    killpg(pgrp_inferior, SIGINT);
	  }
	  break;
	case UserInputRType:
	  {
	    char *text;
	    long len;

	    /* XXX - should really break command up into seperate lines
	       and spoon-feed it to execute_command */

	    text = req->userInput.userInput.text;
	    len = req->userInput.userInput.byteLen;

	    if (text[len-1] == '\n') text[len-1] = '\000';
	    execute_command (text);
	    break;
	  }
	case ChangeBreakpointRType:
	  switch (req->changeBreakpoint.request->breakpointAttr)
	    {
	    case CEnableBreakpoint:
	      execute_command_1("enable %d",
				req->changeBreakpoint.request->breakpointId);
	      break;
	    case CDisableBreakpoint:
	      execute_command_1("disable %d",
				req->changeBreakpoint.request->breakpointId);
	      break;
	    case CDeleteBreakpoint:
	      execute_command_1("delete %d",
				req->changeBreakpoint.request->breakpointId);
	      break;
	    case CBreakAttrUnchanged:
	      execute_command_1("ignore %d %d",
				req->changeBreakpoint.request->breakpointId,
				req->changeBreakpoint.request->ignoreCount);
	      break;
	    default:
	      printf_filtered("Got to ChangeBreakpoint\n");
	      printf_filtered("  breakpointId = %d\n",
			      req->changeBreakpoint.request->breakpointId);
	      printf_filtered("  breakpointType = %d\n",
			      req->changeBreakpoint.request->breakpointType);
	      printf_filtered("  breakpointAttr = %d\n",
			      req->changeBreakpoint.request->breakpointAttr);
	      printf_filtered("  ignoreCount = %d\n",
			      req->changeBreakpoint.request->ignoreCount);
	      break;
	    }
	  break;
	case BreakConditionRType:
	  execute_command_1("condition %d %.*s",
			  req->breakCondition.request->breakpointId,
			  req->breakCondition.condition.byteLen,
			  req->breakCondition.condition.text);
	  break;
	case BreakCommandsRType:
	  /* Put pointers to where cadillac_command_line_input() can find
	     them. */
	  command_line_length = req->breakCommands.commands.byteLen;
	  command_line_text = req->breakCommands.commands.text;
	  execute_command_1("commands %d",
			    req->breakCommands.request->breakpointId);
	  command_line_text = (char *)NULL;
	  command_line_length = 0;
	  break;
	default:
	  fprintf(stderr, "Unknown request type = %d\n",
		  req->head.request->reqType);
	  break;
	}
      free (req); /* Should probably call CVFreeDebuggerRequest() here, but
		     can't do so if interrupt level has mucked with req->
		     request.  CVFreeDebuggerRequest() only ends up calling
		     free() anyway! */
    }
}

#define KERNEL_EVENT 1
#define PTY_EVENT 2

/* Return a bitmask indicating if the kernel or the pty did something
   interesting.  Set poll to non-zero if you don't want to wait.  */

static int
wait_for_events(poll)
     int poll;
{
  fd_set readfds;
  int numfds;
  int eventmask = 0;
  static struct timeval tv = {0};

  /* Output all pending requests. */
  CWriteRequestBuffer(conn);

  /* Wait till there's some activity from the kernel or the pty. */
  do
    {
      FD_ZERO(&readfds);
      FD_SET(kerfd, &readfds);
      if (inferior_pty > 0)
	FD_SET(inferior_pty, &readfds);
      if (poll)
	numfds = select(sizeof(readfds)*8, &readfds, 0, 0, &tv);
      else
	numfds = select(sizeof(readfds)*8, &readfds, 0, 0, 0);
    }
  while (numfds <= 0 && !poll);

  if (FD_ISSET(inferior_pty, &readfds))
    eventmask |= PTY_EVENT;

  if (FD_ISSET(kerfd, &readfds))
    eventmask |= KERNEL_EVENT;

  return eventmask;
}

/* Establish contact with the kernel. */

void
cadillac_initialize(cadillac_id, execarg)
     char *cadillac_id;
     char *execarg;
{
  CTtyRequest *req;
  char *ctmp;
  extern long strtol(char *str, char **ptr, int base);
  char pathname[MAXPATHLEN];
  int n;

  if (!execarg) execarg = "";

  printf("gdb-debugger pid=%d\n", getpid()); /* XXX - debugging only */
  
  /* First establish the connection with the kernel. */

  kerfd = COpenClientSocket(NULL);
  if (kerfd < 0) {
    printf("COpenClientSocket() failed\n");
    exit(1);
  }

  /* Setup for I/O interrupts when appropriate. */

  n = fcntl(kerfd, F_GETFL, 0);
  fcntl(kerfd, F_SETFL, n|FASYNC);
  fcntl(kerfd, F_SETOWN, getpid());

  /* Setup connection buffering. */

  CSetSocketBufferSize (kerfd, 12000);

  /* Generate a new connection control block. */

  conn = NewConnection (0, kerfd, kerfd);
  if (!conn) {
    printf("NewConnection() failed\n");
    exit(1);
  }

#ifdef KERNEL_RECORD
  kerout = fopen("kernel.output", "+w");

  CReadWriteHooks(conn, conn->previewMethod, conn->readMethod, kernel_record);
#endif

  /* Tell the kernel that we are the "debugger". */

  req = CWriteTtyRequest (conn, QueryConnectionRType);
  req->generic.queryconnection.major = 0;
  req->generic.queryconnection.minor = 0;
  req->generic.queryconnection.cadillacId1=strtol(cadillac_id, &ctmp, 16);
  req->generic.queryconnection.cadillacId2 = strtol(++ctmp, NULL, 16);
  req->generic.queryconnection.nProtocols = 1;
  CWriteProtocol (conn, 0, 0, "debugger");
  CWriteLength (conn);

  /* Tell the kernel that we are actually running. */

  /* KROCK ALERT!!!  The kernel doesn't really care about the arguments to
     the program at all!  It only cares that argument 7 be the name of the
     target program.  So, we just fill in the rest of the slots with
     padding.  I hope the kernel never cares about this! */

  req = CWriteTtyRequest (conn, RunningProgramRType);
  req->runningprogram.argc = 8;
  getwd (pathname);
  CWriteVstring0 (conn, pathname);

  CWriteVstring0 (conn, "0");
  CWriteVstring0 (conn, "1");
  CWriteVstring0 (conn, "2");
  CWriteVstring0 (conn, "3");
  CWriteVstring0 (conn, "4");
  CWriteVstring0 (conn, "5");
  CWriteVstring0 (conn, "6");
  CWriteVstring0 (conn, execarg);
  CWriteLength (conn);

  /* Tell the kernel our PID and all that */

  program_id = 1;
  CVWriteDebugProgramInfo(conn,
			  getpid(),
			  program_id,
			  execarg,
			  "");

  /* Tell the rest of the world that Cadillac is now set up. */
  cadillac = 1;
}

/* This is called from execute_command, and provides a wrapper around
   various command routines in a place where both protocol messages and
   user input both flow through.
*/

void
cadillac_call_command(cmdblk, arg, from_tty)
     struct cmd_list_element *cmdblk;
     char *arg;
     int from_tty;
{
  if (cmdblk->class == class_run)
    {
      go_busy();
      has_run = 1;
      (*cmdblk->function.cfunc)(arg, from_tty);
      send_status();
    }
  else
    (*cmdblk->function.cfunc)(arg, from_tty);

  prompt();
}

void
cadillac_new_process()
{
  instance_id = inferior_pid;
}

static void
iosig()
{
  while (1)
    {
      int eventmask;

      eventmask = wait_for_events(1);

      if (eventmask == 0)
	return;

      if (eventmask & PTY_EVENT)
	pty_to_kernel();

      if (eventmask & KERNEL_EVENT)
	kernel_dispatch(1);
    }
}

int
cadillac_wait(status)
     int *status;
{
  int pid;

  signal(SIGIO, iosig);

  pid = wait(status);

  signal(SIGIO, SIG_DFL);
  return pid;
}

static void
null_routine(arg)
     int arg;
{
}

/* All requests from the Cadillac kernel eventually end up here. */

void
cadillac_main_loop(pp)
     char **pp;
{
  CTtyRequest *req;
  struct cleanup *old_chain;

  pprompt = pp;

/* We will come thru here any time there is an error, so send status if
   necessary. */

  send_status();

  prompt();

  /* The actual event loop! */

  while (1)
    {
      int eventmask;

      eventmask = wait_for_events(0);

      if (eventmask & PTY_EVENT)
	pty_to_kernel();

      if (eventmask & KERNEL_EVENT)
	{
	  old_chain = make_cleanup(null_routine, 0);
	  kernel_dispatch(0);
	  bpstat_do_actions(&stop_bpstat);
	  do_cleanups(old_chain);
	}
    }
}