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