Change GDB over to GNU General Public License version 2.
[external/binutils.git] / gdb / convex-xdep.c
1 /* Convex stuff for GDB.
2    Copyright (C) 1990-1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "command.h"
24 #include "symtab.h"
25 #include "value.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "wait.h"
29
30 #include <signal.h>
31 #include <fcntl.h>
32 #include "gdbcore.h"
33
34 #include <sys/param.h>
35 #include <sys/dir.h>
36 #include <sys/user.h>
37 #include <sys/ioctl.h>
38 #include <sys/pcntl.h>
39 #include <sys/thread.h>
40 #include <sys/proc.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/mman.h>
44
45 #include <convex/vmparam.h>
46 #include <convex/filehdr.h>
47 #include <convex/opthdr.h>
48 #include <convex/scnhdr.h>
49 #include <convex/core.h>
50
51 /* Per-thread data, read from the inferior at each stop and written
52    back at each resume.  */
53
54 /* Number of active threads.
55    Tables are valid for thread numbers less than this.  */
56
57 static int n_threads;
58
59 #define MAXTHREADS 8
60                 
61 /* Thread state.  The remaining data is valid only if this is PI_TALIVE.  */
62
63 static int thread_state[MAXTHREADS];
64
65 /* Stop pc, signal, signal subcode */
66
67 static int thread_pc[MAXTHREADS];
68 static int thread_signal[MAXTHREADS];
69 static int thread_sigcode[MAXTHREADS];  
70
71 /* Thread registers.
72    If thread is selected, the regs are in registers[] instead.  */
73
74 static char thread_regs[MAXTHREADS][REGISTER_BYTES];
75
76 /* 1 if the top frame on the thread's stack was a context frame,
77    meaning that the kernel is up to something and we should not
78    touch the thread at all except to resume it.  */
79
80 static char thread_is_in_kernel[MAXTHREADS];
81
82 /* The currently selected thread's number.  */
83
84 static int inferior_thread;
85
86 /* Inferior process's file handle and a process control block
87    to feed args to ioctl with.  */
88
89 static int inferior_fd;
90 static struct pcntl ps;
91
92 /* SOFF file headers for exec or core file.  */
93
94 static FILEHDR filehdr;
95 static OPTHDR opthdr;
96 static SCNHDR scnhdr;
97
98 /* Address maps constructed from section headers of exec and core files.
99    Defines process address -> file address translation.  */
100
101 struct pmap 
102 {
103     long mem_addr;              /* process start address */
104     long mem_end;               /* process end+1 address */
105     long file_addr;             /* file start address */
106     long thread;                /* -1 shared; 0,1,... thread-local */
107     long type;                  /* S_TEXT S_DATA S_BSS S_TBSS etc */
108     long which;                 /* used to sort map for info files */
109 };
110
111 static int n_exec, n_core;
112 static struct pmap exec_map[100];
113 static struct pmap core_map[100];
114
115 /* Offsets in the core file of core_context and core_tcontext blocks.  */
116
117 static int context_offset;
118 static int tcontext_offset[MAXTHREADS];
119
120 /* Core file control blocks.  */
121
122 static struct core_context_v70 c;
123 static struct core_tcontext_v70 tc;
124 static struct user u;
125 static thread_t th;
126 static proc_t pr;
127
128 /* The registers of the currently selected thread.  */
129
130 extern char registers[REGISTER_BYTES];
131
132 /* Vector and communication registers from core dump or from inferior.
133    These are read on demand, ie, not normally valid.  */
134
135 static struct vecst vector_registers;
136 static struct creg_ctx comm_registers;
137
138 /* Flag, set on a vanilla CONT command and cleared when the inferior
139    is continued.  */
140
141 static int all_continue;
142
143 /* Flag, set when the inferior is continued by a vanilla CONT command,
144    cleared if it is continued for any other purpose.  */
145
146 static int thread_switch_ok;
147
148 /* Stack of signals recieved from threads but not yet delivered to gdb.  */
149
150 struct threadpid 
151 {
152     int pid;
153     int thread;
154     int signo;
155     int subsig;
156     int pc;
157 };
158
159 static struct threadpid signal_stack_bot[100];
160 static struct threadpid *signal_stack = signal_stack_bot;
161
162 /* How to detect empty stack -- bottom frame is all zero.  */
163
164 #define signal_stack_is_empty() (signal_stack->pid == 0)
165
166 /* Mode controlled by SET PIPE command, controls the psw SEQ bit
167    which forces each instruction to complete before the next one starts.  */
168
169 static int sequential = 0;
170
171 /* Mode controlled by the SET PARALLEL command.  Values are:
172    0  concurrency limit 1 thread, dynamic scheduling
173    1  no concurrency limit, dynamic scheduling
174    2  no concurrency limit, fixed scheduling  */
175
176 static int parallel = 1;
177
178 /* Mode controlled by SET BASE command, output radix for unformatted
179    integer typeout, as in argument lists, aggregates, and so on.
180    Zero means guess whether it's an address (hex) or not (decimal).  */
181
182 static int output_radix = 0;
183
184 /* Signal subcode at last thread stop.  */
185
186 static int stop_sigcode;
187
188 /* Hack, see wait() below.  */
189
190 static int exec_trap_timer;
191
192 #include "gdbcmd.h"
193
194 /* Nonzero if we are debugging an attached outside process
195    rather than an inferior.  */
196
197 extern int attach_flag;
198
199
200
201 static struct type *vector_type ();
202 static long *read_vector_register ();
203 static long *read_vector_register_1 ();
204 static void write_vector_register ();
205 static REGISTER_TYPE read_comm_register ();
206 static void write_comm_register ();
207 static void convex_cont_command ();
208 static void thread_continue ();
209 static void select_thread ();
210 static void scan_stack ();
211 static void set_fixed_scheduling ();
212 static char *subsig_name ();
213 static void psw_info ();
214 static sig_noop ();
215 static ptr_cmp ();
216
217 extern char *sys_siglist[];
218 \f
219 /* Execute ptrace.  Convex V7 replaced ptrace with pattach.
220    Allow ptrace (0) as a no-op.  */
221
222 int
223 call_ptrace (request, pid, procaddr, buf)
224      int request, pid, procaddr, buf;
225 {
226   if (request == 0)
227     return;
228   error ("no ptrace");
229 }
230
231 /* Replacement for system execle routine.
232    Convert it to an equivalent exect, which pattach insists on.  */
233
234 execle (name, argv)
235      char *name, *argv;
236 {
237   char ***envp = (char ***) &argv;
238   while (*envp++) ;
239
240   signal (SIGTRAP, sig_noop);
241   exect (name, &argv, *envp);
242 }
243
244 /* Stupid handler for stupid trace trap that otherwise causes
245    startup to stupidly hang.  */
246
247 static sig_noop () 
248 {}
249
250 /* Read registers from inferior into registers[] array.
251    For convex, they are already there, read in when the inferior stops.  */
252
253 void
254 fetch_inferior_registers (regno)
255      int regno;
256 {
257 }
258
259 /* Store our register values back into the inferior.
260    For Convex, do this only once, right before resuming inferior.  */
261
262 store_inferior_registers (regno)
263      int regno;
264 {
265 }
266
267 /* Copy LEN bytes from inferior's memory starting at MEMADDR
268    to debugger memory starting at MYADDR. 
269    On failure (cannot read from inferior, usually because address is out
270    of bounds) returns the value of errno. */
271
272 int
273 read_inferior_memory (memaddr, myaddr, len)
274      CORE_ADDR memaddr;
275      char *myaddr;
276      int len;
277 {
278   errno = 0;
279   while (len > 0)
280     {
281       /* little-known undocumented max request size */
282       int i = (len < 12288) ? len : 12288;
283
284       lseek (inferior_fd, memaddr, 0);
285       read (inferior_fd, myaddr, i);
286
287       memaddr += i;
288       myaddr += i;
289       len -= i;
290     }
291   if (errno) 
292     bzero (myaddr, len);
293   return errno;
294 }
295
296 /* Copy LEN bytes of data from debugger memory at MYADDR
297    to inferior's memory at MEMADDR.
298    Returns errno on failure (cannot write the inferior) */
299
300 int
301 write_inferior_memory (memaddr, myaddr, len)
302      CORE_ADDR memaddr;
303      char *myaddr;
304      int len;
305 {
306   errno = 0;
307   lseek (inferior_fd, memaddr, 0);
308   write (inferior_fd, myaddr, len);
309   return errno;
310 }
311
312 /* Here from create_inferior when the inferior process has been created
313    and started up.  We must do a pattach to grab it for debugging.
314
315    Also, intercept the CONT command by altering its dispatch address.  */
316
317 create_inferior_hook (pid)
318     int pid;
319 {
320   static char cont[] = "cont";
321   static char cont1[] = "c";
322   char *linep = cont;
323   char *linep1 = cont1;
324   char **line = &linep;
325   char **line1 = &linep1;
326   struct cmd_list_element *c;
327
328   c = lookup_cmd (line, cmdlist, "", 0);
329   c->function = convex_cont_command;
330   c = lookup_cmd (line1, cmdlist, "", 0);
331   c->function = convex_cont_command;
332
333   inferior_fd = pattach (pid, O_EXCL);
334   if (inferior_fd < 0)
335     perror_with_name ("pattach");
336   inferior_thread = 0;
337   set_fixed_scheduling (pid, parallel == 2);
338 }
339
340 /* Attach process PID for debugging.  */
341
342 attach (pid)
343     int pid;
344 {
345   int fd = pattach (pid, O_EXCL);
346   if (fd < 0)
347     perror_with_name ("pattach");
348   attach_flag = 1;
349   /* wait for strange kernel reverberations to go away */
350   sleep (1);
351
352   setpgrp (pid, pid);
353
354   inferior_fd = fd;
355   inferior_thread = 0;
356   return pid;
357 }
358
359 /* Stop debugging the process whose number is PID
360    and continue it with signal number SIGNAL.
361    SIGNAL = 0 means just continue it.  */
362
363 void
364 detach (signal)
365      int signal;
366 {
367   signal_stack = signal_stack_bot;
368   thread_continue (-1, 0, signal);
369   ioctl (inferior_fd, PIXDETACH, &ps);
370   close (inferior_fd);
371   inferior_fd = 0;
372   attach_flag = 0;
373 }
374
375 /* Kill off the inferior process.  */
376
377 kill_inferior ()
378 {
379   if (inferior_pid == 0)
380     return;
381   ioctl (inferior_fd, PIXTERMINATE, 0);
382   wait (0);
383   target_mourn_inferior ();
384 }
385
386 /* This is used when GDB is exiting.  It gives less chance of error.*/
387
388 kill_inferior_fast ()
389 {
390   if (inferior_pid == 0)
391     return;
392   ioctl (inferior_fd, PIXTERMINATE, 0);
393   wait (0);
394 }
395
396 /* Read vector register REG, and return a pointer to the value.  */
397
398 static long *
399 read_vector_register (reg)
400     int reg;
401 {
402   if (have_inferior_p ())
403     {
404       errno = 0;
405       ps.pi_buffer = (char *) &vector_registers;
406       ps.pi_nbytes = sizeof vector_registers;
407       ps.pi_offset = 0;
408       ps.pi_thread = inferior_thread;
409       ioctl (inferior_fd, PIXRDVREGS, &ps);
410       if (errno)
411         bzero (&vector_registers, sizeof vector_registers);
412     }
413   else if (corechan >= 0)
414     {
415       lseek (corechan, tcontext_offset[inferior_thread], 0);
416       if (myread (corechan, &tc, sizeof tc) < 0)
417         perror_with_name (corefile);
418       lseek (corechan, tc.core_thread_p, 0);
419       if (myread (corechan, &th, sizeof th) < 0)
420         perror_with_name (corefile);
421       lseek (corechan, tc.core_vregs_p, 0);
422       if (myread (corechan, &vector_registers, 16*128) < 0)
423         perror_with_name (corefile);
424       vector_registers.vm[0] = th.t_vect_ctx.vc_vm[0];
425       vector_registers.vm[1] = th.t_vect_ctx.vc_vm[1];
426       vector_registers.vls = th.t_vect_ctx.vc_vls;
427     }
428
429   return read_vector_register_1 (reg);
430 }
431
432 /* Return a pointer to vector register REG, which must already have been
433    fetched from the inferior or core file.  */
434
435 static long *
436 read_vector_register_1 (reg) 
437     int reg;
438 {
439   switch (reg)
440     {
441     case VM_REGNUM:
442       return (long *) vector_registers.vm;
443     case VS_REGNUM:
444       return (long *) &vector_registers.vls;
445     case VL_REGNUM:
446       return 1 + (long *) &vector_registers.vls;
447     default:
448       return (long *) &vector_registers.vr[reg];
449     }
450 }
451
452 /* Write vector register REG, element ELEMENT, new value VAL.
453    NB: must use read-modify-write on the entire vector state,
454    since pattach does not do offsetted writes correctly.  */
455
456 static void
457 write_vector_register (reg, element, val)
458     int reg, element;
459     REGISTER_TYPE val;
460 {
461   if (have_inferior_p ())
462     {
463       errno = 0;
464       ps.pi_thread = inferior_thread;
465       ps.pi_offset = 0;
466       ps.pi_buffer = (char *) &vector_registers;
467       ps.pi_nbytes = sizeof vector_registers;
468
469       ioctl (inferior_fd, PIXRDVREGS, &ps);
470
471       switch (reg)
472         {
473         case VL_REGNUM:
474           vector_registers.vls =
475             (vector_registers.vls & 0xffffffff00000000LL)
476               + (unsigned long) val;
477           break;
478
479         case VS_REGNUM:
480           vector_registers.vls =
481             (val << 32) + (unsigned long) vector_registers.vls;
482           break;
483             
484         default:
485           vector_registers.vr[reg].el[element] = val;
486           break;
487         }
488
489       ioctl (inferior_fd, PIXWRVREGS, &ps);
490
491       if (errno)
492         perror_with_name ("writing vector register");
493     }
494 }
495
496 /* Return the contents of communication register NUM.  */ 
497
498 static REGISTER_TYPE 
499 read_comm_register (num)
500      int num;
501 {
502   if (have_inferior_p ())
503     {
504       ps.pi_buffer = (char *) &comm_registers;
505       ps.pi_nbytes = sizeof comm_registers;
506       ps.pi_offset = 0;
507       ps.pi_thread = inferior_thread;
508       ioctl (inferior_fd, PIXRDCREGS, &ps);
509     }
510   return comm_registers.crreg.r4[num];
511 }
512
513 /* Store a new value VAL into communication register NUM.  
514    NB: Must use read-modify-write on the whole comm register set
515    since pattach does not do offsetted writes correctly.  */
516
517 static void
518 write_comm_register (num, val)
519      int num;
520      REGISTER_TYPE val;
521 {
522   if (have_inferior_p ())
523     {
524       ps.pi_buffer = (char *) &comm_registers;
525       ps.pi_nbytes = sizeof comm_registers;
526       ps.pi_offset = 0;
527       ps.pi_thread = inferior_thread;
528       ioctl (inferior_fd, PIXRDCREGS, &ps);
529       comm_registers.crreg.r4[num] = val;
530       ioctl (inferior_fd, PIXWRCREGS, &ps);
531     }
532 }
533
534 /* Resume execution of the inferior process.
535    If STEP is nonzero, single-step it.
536    If SIGNAL is nonzero, give it that signal.  */
537
538 void
539 resume (step, signal)
540      int step;
541      int signal;
542 {
543   errno = 0;
544   if (step || signal)
545     thread_continue (inferior_thread, step, signal);
546   else
547     thread_continue (-1, 0, 0);
548 }
549
550 /* Maybe resume some threads.
551    THREAD is which thread to resume, or -1 to resume them all.
552    STEP and SIGNAL are as in resume.
553
554    Global variable ALL_CONTINUE is set when we are here to do a
555    `cont' command; otherwise we may be doing `finish' or a call or
556    something else that will not tolerate an automatic thread switch.
557
558    If there are stopped threads waiting to deliver signals, and
559    ALL_CONTINUE, do not actually resume anything.  gdb will do a wait
560    and see one of the stopped threads in the queue.  */
561
562 static void
563 thread_continue (thread, step, signal)
564      int thread, step, signal;
565 {
566   int n;
567
568   /* If we are to continue all threads, but not for the CONTINUE command,
569      pay no attention and continue only the selected thread.  */
570
571   if (thread < 0 && ! all_continue)
572     thread = inferior_thread;
573
574   /* If we are not stepping, we have now executed the continue part
575      of a CONTINUE command.  */
576
577   if (! step)
578     all_continue = 0;
579
580   /* Allow wait() to switch threads if this is an all-out continue.  */
581
582   thread_switch_ok = thread < 0;
583
584   /* If there are threads queued up, don't resume.  */
585
586   if (thread_switch_ok && ! signal_stack_is_empty ())
587     return;
588
589   /* OK, do it.  */
590
591   for (n = 0; n < n_threads; n++)
592     if (thread_state[n] == PI_TALIVE)
593       {
594         select_thread (n);
595
596         if ((thread < 0 || n == thread) && ! thread_is_in_kernel[n])
597           {
598             /* Blam the trace bits in the stack's saved psws to match 
599                the desired step mode.  This is required so that
600                single-stepping a return doesn't restore a psw with a
601                clear trace bit and fly away, and conversely,
602                proceeding through a return in a routine that was
603                stepped into doesn't cause a phantom break by restoring
604                a psw with the trace bit set. */
605             scan_stack (PSW_T_BIT, step);
606             scan_stack (PSW_S_BIT, sequential);
607           }
608
609         ps.pi_buffer = registers;
610         ps.pi_nbytes = REGISTER_BYTES;
611         ps.pi_offset = 0;
612         ps.pi_thread = n;
613         if (! thread_is_in_kernel[n])
614           if (ioctl (inferior_fd, PIXWRREGS, &ps))
615             perror_with_name ("PIXWRREGS");
616
617         if (thread < 0 || n == thread)
618           {
619             ps.pi_pc = 1;
620             ps.pi_signo = signal;
621             if (ioctl (inferior_fd, step ? PIXSTEP : PIXCONTINUE, &ps) < 0)
622               perror_with_name ("PIXCONTINUE");
623           }
624       }
625
626   if (ioctl (inferior_fd, PIXRUN, &ps) < 0)
627     perror_with_name ("PIXRUN");
628 }
629
630 /* Replacement for system wait routine.  
631
632    The system wait returns with one or more threads stopped by
633    signals.  Put stopped threads on a stack and return them one by
634    one, so that it appears that wait returns one thread at a time.
635
636    Global variable THREAD_SWITCH_OK is set when gdb can tolerate wait
637    returning a new thread.  If it is false, then only one thread is
638    running; we will do a real wait, the thread will do something, and
639    we will return that.  */
640
641 pid_t
642 wait (w)
643     union wait *w;
644 {
645   int pid;
646
647   if (!w)
648     return wait3 (0, 0, 0);
649
650   /* Do a real wait if we were told to, or if there are no queued threads.  */
651
652   if (! thread_switch_ok || signal_stack_is_empty ())
653     {
654       int thread;
655
656       pid = wait3 (w, 0, 0);
657
658       if (!WIFSTOPPED (*w) || pid != inferior_pid)
659         return pid;
660
661       /* The inferior has done something and stopped.  Read in all the
662          threads' registers, and queue up any signals that happened.  */
663
664       if (ioctl (inferior_fd, PIXGETTHCOUNT, &ps) < 0)
665         perror_with_name ("PIXGETTHCOUNT");
666       
667       n_threads = ps.pi_othdcnt;
668       for (thread = 0; thread < n_threads; thread++)
669         {
670           ps.pi_thread = thread;
671           if (ioctl (inferior_fd, PIXGETSUBCODE, &ps) < 0)
672             perror_with_name ("PIXGETSUBCODE");
673           thread_state[thread] = ps.pi_otstate;
674
675           if (ps.pi_otstate == PI_TALIVE)
676             {
677               select_thread (thread);
678               ps.pi_buffer = registers;
679               ps.pi_nbytes = REGISTER_BYTES;
680               ps.pi_offset = 0;
681               ps.pi_thread = thread;
682               if (ioctl (inferior_fd, PIXRDREGS, &ps) < 0)
683                 perror_with_name ("PIXRDREGS");
684
685               registers_fetched ();
686
687               thread_pc[thread] = read_pc ();
688               thread_signal[thread] = ps.pi_osigno;
689               thread_sigcode[thread] = ps.pi_osigcode;
690
691               /* If the thread's stack has a context frame
692                  on top, something fucked is going on.  I do not
693                  know what, but do I know this: the only thing you
694                  can do with such a thread is continue it.  */
695
696               thread_is_in_kernel[thread] = 
697                 ((read_register (PS_REGNUM) >> 25) & 3) == 0;
698
699               /* Signals push an extended frame and then fault
700                  with a ridiculous pc.  Pop the frame.  */
701
702               if (thread_pc[thread] > STACK_END_ADDR)
703                 {
704                   POP_FRAME;
705                   if (is_break_pc (thread_pc[thread]))
706                     thread_pc[thread] = read_pc () - 2;
707                   else
708                     thread_pc[thread] = read_pc ();
709                   write_register (PC_REGNUM, thread_pc[thread]);
710                 }
711               
712               if (ps.pi_osigno || ps.pi_osigcode)
713                 {
714                   signal_stack++;
715                   signal_stack->pid = pid;
716                   signal_stack->thread = thread;
717                   signal_stack->signo = thread_signal[thread];
718                   signal_stack->subsig = thread_sigcode[thread];
719                   signal_stack->pc = thread_pc[thread];
720                 }
721
722               /* The following hackery is caused by a unix 7.1 feature:
723                  the inferior's fixed scheduling mode is cleared when
724                  it execs the shell (since the shell is not a parallel
725                  program).  So, note the 5.4 trap we get when
726                  the shell does its exec, then catch the 5.0 trap 
727                  that occurs when the debuggee starts, and set fixed
728                  scheduling mode properly.  */
729
730               if (ps.pi_osigno == 5 && ps.pi_osigcode == 4)
731                 exec_trap_timer = 1;
732               else
733                 exec_trap_timer--;
734               
735               if (ps.pi_osigno == 5 && exec_trap_timer == 0)
736                 set_fixed_scheduling (pid, parallel == 2);
737             }
738         }
739
740       if (signal_stack_is_empty ())
741         error ("no active threads?!");
742     }
743
744   /* Select the thread that stopped, and return *w saying why.  */
745
746   select_thread (signal_stack->thread);
747
748   stop_signal = signal_stack->signo;
749   stop_sigcode = signal_stack->subsig;
750
751   WSETSTOP (*w, signal_stack->signo);
752   w->w_thread = signal_stack->thread;
753   return (signal_stack--)->pid;
754 }
755
756 /* Select thread THREAD -- its registers, stack, per-thread memory.
757    This is the only routine that may assign to inferior_thread
758    or thread_regs[].  */
759
760 static void
761 select_thread (thread)
762      int thread;
763 {
764   if (thread == inferior_thread)
765     return;
766
767   bcopy (registers, thread_regs[inferior_thread], REGISTER_BYTES);
768   ps.pi_thread = inferior_thread = thread;
769   if (have_inferior_p ())
770     ioctl (inferior_fd, PISETRWTID, &ps);
771   bcopy (thread_regs[thread], registers, REGISTER_BYTES);
772 }
773   
774 /* Routine to set or clear a psw bit in the psw and also all psws
775    saved on the stack.  Quits when we get to a frame in which the
776    saved psw is correct. */
777
778 static void
779 scan_stack (bit, val)
780     long bit, val;
781 {
782   long ps = read_register (PS_REGNUM);
783   long fp;
784   if (val ? !(ps & bit) : (ps & bit))
785     {    
786       ps ^= bit;
787       write_register (PS_REGNUM, ps);
788
789       fp = read_register (FP_REGNUM);
790       while (fp & 0x80000000)
791         {
792           ps = read_memory_integer (fp + 4, 4);
793           if (val ? (ps & bit) : !(ps & bit))
794             break;
795           ps ^= bit;
796           write_memory (fp + 4, &ps, 4);
797           fp = read_memory_integer (fp + 8, 4);
798         }
799     }
800 }
801
802 /* Set fixed scheduling (alliant mode) of process PID to ARG (0 or 1).  */
803
804 static void
805 set_fixed_scheduling (pid, arg)
806       int arg;
807 {
808   struct pattributes pattr;
809   getpattr (pid, &pattr);
810   pattr.pattr_pfixed = arg;
811   setpattr (pid, &pattr);
812 }
813 \f
814 void
815 core_file_command (filename, from_tty)
816      char *filename;
817      int from_tty;
818 {
819   int n;
820
821   /* Discard all vestiges of any previous core file
822      and mark data and stack spaces as empty.  */
823
824   if (corefile)
825     free (corefile);
826   corefile = 0;
827
828   if (corechan >= 0)
829     close (corechan);
830   corechan = -1;
831
832   data_start = 0;
833   data_end = 0;
834   stack_start = STACK_END_ADDR;
835   stack_end = STACK_END_ADDR;
836   n_core = 0;
837
838   /* Now, if a new core file was specified, open it and digest it.  */
839
840   if (filename)
841     {
842       filename = tilde_expand (filename);
843       make_cleanup (free, filename);
844       
845       if (have_inferior_p ())
846         error ("To look at a core file, you must kill the inferior with \"kill\".");
847       corechan = open (filename, O_RDONLY, 0);
848       if (corechan < 0)
849         perror_with_name (filename);
850
851       if (myread (corechan, &filehdr, sizeof filehdr) < 0)
852         perror_with_name (filename);
853
854       if (!IS_CORE_SOFF_MAGIC (filehdr.h_magic))
855         error ("%s: not a core file.\n", filename);
856
857       if (myread (corechan, &opthdr, filehdr.h_opthdr) < 0)
858         perror_with_name (filename);
859
860       /* Read through the section headers.
861          For text, data, etc, record an entry in the core file map.
862          For context and tcontext, record the file address of
863          the context blocks.  */
864
865       lseek (corechan, (long) filehdr.h_scnptr, 0);
866
867       n_threads = 0;
868       for (n = 0; n < filehdr.h_nscns; n++)
869         {
870           if (myread (corechan, &scnhdr, sizeof scnhdr) < 0)
871             perror_with_name (filename);
872           if ((scnhdr.s_flags & S_TYPMASK) >= S_TEXT
873               && (scnhdr.s_flags & S_TYPMASK) <= S_COMON)
874             {
875               core_map[n_core].mem_addr = scnhdr.s_vaddr;
876               core_map[n_core].mem_end = scnhdr.s_vaddr + scnhdr.s_size;
877               core_map[n_core].file_addr = scnhdr.s_scnptr;
878               core_map[n_core].type = scnhdr.s_flags & S_TYPMASK;
879               if (core_map[n_core].type != S_TBSS
880                   && core_map[n_core].type != S_TDATA
881                   && core_map[n_core].type != S_TTEXT)
882                 core_map[n_core].thread = -1;
883               else if (n_core == 0
884                        || core_map[n_core-1].mem_addr != scnhdr.s_vaddr)
885                 core_map[n_core].thread = 0;
886               else 
887                 core_map[n_core].thread = core_map[n_core-1].thread + 1;
888               n_core++;
889             }
890           else if ((scnhdr.s_flags & S_TYPMASK) == S_CONTEXT)
891             context_offset = scnhdr.s_scnptr;
892           else if ((scnhdr.s_flags & S_TYPMASK) == S_TCONTEXT) 
893             tcontext_offset[n_threads++] = scnhdr.s_scnptr;
894         }
895
896       /* Read the context block, struct user, struct proc,
897          and the comm regs.  */
898
899       lseek (corechan, context_offset, 0);
900       if (myread (corechan, &c, sizeof c) < 0)
901         perror_with_name (filename);
902       lseek (corechan, c.core_user_p, 0);
903       if (myread (corechan, &u, sizeof u) < 0)
904         perror_with_name (filename);
905       lseek (corechan, c.core_proc_p, 0);
906       if (myread (corechan, &pr, sizeof pr) < 0)
907         perror_with_name (filename);
908       comm_registers = pr.p_creg;
909
910       /* Core file apparently is really there.  Make it really exist
911          for xfer_core_file so we can do read_memory on it. */
912
913       if (filename[0] == '/')
914         corefile = savestring (filename, strlen (filename));
915       else
916         corefile = concat (current_directory, "/", filename);
917
918       printf_filtered ("Program %s ", u.u_comm);
919
920       /* Read the thread registers and fill in the thread_xxx[] data.  */
921
922       for (n = 0; n < n_threads; n++)
923         {
924           select_thread (n);
925
926           lseek (corechan, tcontext_offset[n], 0);
927           if (myread (corechan, &tc, sizeof tc) < 0)
928             perror_with_name (corefile);
929           lseek (corechan, tc.core_thread_p, 0);
930           if (myread (corechan, &th, sizeof th) < 0)
931             perror_with_name (corefile);
932
933           lseek (corechan, tc.core_syscall_context_p, 0);
934           if (myread (corechan, registers, REGISTER_BYTES) < 0)
935             perror_with_name (corefile);
936
937           thread_signal[n] = th.t_cursig;
938           thread_sigcode[n] = th.t_code;
939           thread_state[n] = th.t_state;
940           thread_pc[n] = read_pc ();
941
942           if (thread_pc[n] > STACK_END_ADDR)
943             {
944               POP_FRAME;
945               if (is_break_pc (thread_pc[n]))
946                 thread_pc[n] = read_pc () - 2;
947               else
948                 thread_pc[n] = read_pc ();
949               write_register (PC_REGNUM, thread_pc[n]);
950             }
951
952           printf_filtered ("thread %d received signal %d, %s\n",
953                            n, thread_signal[n],
954                            thread_signal[n] < NSIG
955                            ? sys_siglist[thread_signal[n]]
956                            : "(undocumented)");
957         }
958
959       /* Select an interesting thread -- also-rans died with SIGKILL,
960          so find one that didn't.  */
961
962       for (n = 0; n < n_threads; n++)
963         if (thread_signal[n] != 0 && thread_signal[n] != SIGKILL)
964           {
965             select_thread (n);
966             stop_signal = thread_signal[n];
967             stop_sigcode = thread_sigcode[n];
968             break;
969           }
970
971       core_aouthdr.a_magic = 0;
972
973       flush_cached_frames ();
974       set_current_frame (create_new_frame (read_register (FP_REGNUM),
975                                              read_pc ()));
976       select_frame (get_current_frame (), 0);
977       validate_files ();
978
979       print_sel_frame (1);
980     }
981   else if (from_tty)
982     printf_filtered ("No core file now.\n");
983 }