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