xref: /csrg-svn/old/dbx/coredump.c (revision 33313)
121627Sdist /*
221627Sdist  * Copyright (c) 1983 Regents of the University of California.
321627Sdist  * All rights reserved.  The Berkeley software License Agreement
421627Sdist  * specifies the terms and conditions for redistribution.
521627Sdist  */
69661Slinton 
721627Sdist #ifndef lint
8*33313Sdonn static char sccsid[] = "@(#)coredump.c	5.3 (Berkeley) 01/12/88";
921627Sdist #endif not lint
109661Slinton 
11*33313Sdonn static char rcsid[] = "$Header: coredump.c,v 1.4 87/04/15 03:25:22 donn Exp $";
1218215Slinton 
139661Slinton /*
149661Slinton  * Deal with the core dump anachronism.
159661Slinton  */
169661Slinton 
179661Slinton #include "defs.h"
189661Slinton #include "coredump.h"
199661Slinton #include "machine.h"
209661Slinton #include "object.h"
219661Slinton #include "main.h"
229661Slinton #include <a.out.h>
239661Slinton 
249661Slinton #ifndef public
259661Slinton #define coredump_readin(m, r, s) coredump_xreadin(&(m), r, &(s))
269661Slinton 
279661Slinton #include "machine.h"
289661Slinton #endif
299661Slinton 
309661Slinton typedef struct {
319661Slinton     Address begin;
329661Slinton     Address end;
339661Slinton     Address seekaddr;
349661Slinton } Map;
359661Slinton 
369661Slinton private Map datamap, stkmap;
379661Slinton private File objfile;
389661Slinton private struct exec hdr;
399661Slinton 
4018215Slinton public coredump_getkerinfo ()
4118215Slinton {
4218215Slinton     Symbol s;
4318215Slinton 
4418215Slinton     s = lookup(identname("Sysmap", true));
4518215Slinton     if (s == nil) {
4618215Slinton 	panic("can't find 'Sysmap'");
479661Slinton     }
4818215Slinton     sbr = (struct pte *) (s->symvalue.offset);
4918215Slinton     s = lookup(identname("Syssize", true));
5018215Slinton     if (s == nil) {
5118215Slinton 	panic("can't find 'Syssize'");
5218215Slinton     }
5318215Slinton     slr = (integer) (s->symvalue.offset);
5418215Slinton     printf("sbr %lx slr %lx\n", sbr, slr);
5518215Slinton     s = lookup(identname("masterpaddr", true));
5618215Slinton     if (s == nil) {
5718215Slinton 	panic("can't find 'masterpaddr'");
5818215Slinton     }
59*33313Sdonn     fseek(
60*33313Sdonn 	corefile,
61*33313Sdonn 	datamap.seekaddr + s->symvalue.offset&0x7fffffff - datamap.begin,
62*33313Sdonn 	0
63*33313Sdonn     );
6418215Slinton     get(corefile, masterpcbb);
6526334Ssam     masterpcbb = (masterpcbb&PG_PFNUM)*NBPG;
6618215Slinton     getpcb();
6718215Slinton }
6818215Slinton 
6918215Slinton /*
7018215Slinton  * Read the user area information from the core dump.
7118215Slinton  */
729661Slinton 
7318215Slinton public coredump_xreadin(mask, reg, signo)
7418215Slinton int *mask;
7518215Slinton Word reg[];
7626334Ssam short *signo;
7718215Slinton {
7818215Slinton     register struct user *up;
7918215Slinton     register Word *savreg;
8018215Slinton     union {
8118215Slinton 	struct user u;
8218215Slinton 	char dummy[ctob(UPAGES)];
8318215Slinton     } ustruct;
8418215Slinton     Symbol s;
8518215Slinton 
8618215Slinton     objfile = fopen(objname, "r");
8718215Slinton     if (objfile == nil) {
8818215Slinton 	fatal("can't read \"%s\"", objname);
899661Slinton     }
9018215Slinton     get(objfile, hdr);
9118215Slinton     if (vaddrs) {
9218215Slinton 	datamap.begin = 0;
9318215Slinton 	datamap.end = 0xffffffff;
9418215Slinton 	stkmap.begin = 0xffffffff;
9518215Slinton 	stkmap.end = 0xffffffff;
9618215Slinton     } else {
9718215Slinton 	up = &(ustruct.u);
9818215Slinton 	fread(up, ctob(UPAGES), 1, corefile);
99*33313Sdonn #	if vax || tahoe
100*33313Sdonn 	    savreg = (Word *) &(ustruct.dummy[ctob(UPAGES)]);
101*33313Sdonn #	else ifdef mc68000
102*33313Sdonn 	    savreg = (Word *) (
103*33313Sdonn 		&ustruct.dummy[ctob(UPAGES) - 10] - (NREG * sizeof(Word))
104*33313Sdonn 	    );
105*33313Sdonn #	endif
106*33313Sdonn #       ifdef IRIS
107*33313Sdonn 	    *mask = savreg[RPS];
108*33313Sdonn #       else
109*33313Sdonn 	    *mask = savreg[PS];
110*33313Sdonn #       endif
11118215Slinton 	copyregs(savreg, reg);
11218215Slinton 	*signo = up->u_arg[0];
11318215Slinton 	datamap.seekaddr = ctob(UPAGES);
11426334Ssam 	stkmap.begin = USRSTACK - ctob(up->u_ssize);
11526334Ssam 	stkmap.end = USRSTACK;
11618215Slinton 	stkmap.seekaddr = datamap.seekaddr + ctob(up->u_dsize);
11718215Slinton 	switch (hdr.a_magic) {
11818215Slinton 	    case OMAGIC:
119*33313Sdonn 		datamap.begin = CODESTART;
120*33313Sdonn 		datamap.end = CODESTART + ctob(up->u_tsize) + ctob(up->u_dsize);
12118215Slinton 		break;
12218215Slinton 
12318215Slinton 	    case NMAGIC:
12418215Slinton 	    case ZMAGIC:
125*33313Sdonn 		datamap.begin = (Address)
126*33313Sdonn 		    ptob(btop(ctob(up->u_tsize) - 1) + 1) + CODESTART;
12718215Slinton 		datamap.end = datamap.begin + ctob(up->u_dsize);
12818215Slinton 		break;
12918215Slinton 
13018215Slinton 	    default:
13118215Slinton 		fatal("bad magic number 0x%x", hdr.a_magic);
13218215Slinton 	}
13318215Slinton #ifdef UXMAG
13418215Slinton 	/*
13518215Slinton 	 * Core dump not from this object file?
13618215Slinton 	 */
13718215Slinton 	if (hdr.a_magic != 0 and up->u_exdata.ux_mag  != 0 and
13818215Slinton 	  hdr.a_magic != up->u_exdata.ux_mag) {
13918215Slinton 	    warning("core dump ignored");
14018215Slinton 	    coredump = false;
14118215Slinton 	    fclose(corefile);
14218215Slinton 	    fclose(objfile);
14318215Slinton 	    start(nil, nil, nil);
14418215Slinton 	}
14518215Slinton #endif
14618215Slinton     }
1479661Slinton }
1489661Slinton 
1499661Slinton public coredump_close()
1509661Slinton {
1519661Slinton     fclose(objfile);
1529661Slinton }
1539661Slinton 
1549661Slinton public coredump_readtext(buff, addr, nbytes)
1559661Slinton char *buff;
1569661Slinton Address addr;
1579661Slinton int nbytes;
1589661Slinton {
15918215Slinton     if (hdr.a_magic == OMAGIC or vaddrs) {
1609661Slinton 	coredump_readdata(buff, addr, nbytes);
1619661Slinton     } else {
162*33313Sdonn 	fseek(objfile, N_TXTOFF(hdr) + addr - CODESTART, 0);
1639661Slinton 	fread(buff, nbytes, sizeof(Byte), objfile);
1649661Slinton     }
1659661Slinton }
1669661Slinton 
1679661Slinton public coredump_readdata(buff, addr, nbytes)
1689661Slinton char *buff;
1699661Slinton Address addr;
1709661Slinton int nbytes;
1719661Slinton {
17218215Slinton     Address a;
17318215Slinton 
17418215Slinton     a = addr;
17518215Slinton     if (a < datamap.begin) {
17616608Ssam 	if (hdr.a_magic == OMAGIC) {
17718215Slinton 	    error("[data address 0x%x too low (lb = 0x%x)]", a, datamap.begin);
17816608Ssam 	} else {
17918215Slinton 	    coredump_readtext(buff, a, nbytes);
18016608Ssam 	}
18118215Slinton     } else if (a > stkmap.end) {
18218215Slinton 	error("data address 0x%x too high (ub = 0x%x)", a, stkmap.end);
1839661Slinton     } else {
18418215Slinton 	if (vaddrs) {
18518215Slinton 	    vreadfromfile(corefile, a, buff, nbytes);
18618215Slinton 	} else {
18718215Slinton 	    readfromfile(corefile, a, buff, nbytes);
18818215Slinton 	}
1899661Slinton     }
1909661Slinton }
19118215Slinton 
19218215Slinton /*
19318215Slinton  * Read a block of data from a memory image, mapping virtual addresses.
19418215Slinton  * Have to watch out for page boundaries.
19518215Slinton  */
19618215Slinton 
19718215Slinton private vreadfromfile (corefile, v, buff, nbytes)
19818215Slinton File corefile;
19918215Slinton Address v;
20018215Slinton char *buff;
20118215Slinton integer nbytes;
20218215Slinton {
20318215Slinton     Address a;
20418215Slinton     integer i, remainder, pagesize;
20518215Slinton     char *bufp;
20618215Slinton 
20718215Slinton     a = v;
20818215Slinton     pagesize = (integer) ptob(1);
20918215Slinton     remainder = pagesize - (a mod pagesize);
21018215Slinton     if (remainder >= nbytes) {
21118215Slinton 	readfromfile(corefile, vmap(a), buff, nbytes);
21218215Slinton     } else {
21318215Slinton 	readfromfile(corefile, vmap(a), buff, remainder);
21418215Slinton 	a += remainder;
21518215Slinton 	i = nbytes - remainder;
21618215Slinton 	bufp = buff + remainder;
21718215Slinton 	while (i > pagesize) {
21818215Slinton 	    readfromfile(corefile, vmap(a), bufp, pagesize);
21918215Slinton 	    a += pagesize;
22018215Slinton 	    bufp += pagesize;
22118215Slinton 	    i -= pagesize;
22218215Slinton 	}
22318215Slinton 	readfromfile(corefile, vmap(a), bufp, i);
22418215Slinton     }
22518215Slinton }
22618215Slinton 
22718215Slinton private readfromfile (f, a, buff, nbytes)
22818215Slinton File f;
22918215Slinton Address a;
23018215Slinton char *buff;
23118215Slinton integer nbytes;
23218215Slinton {
23318215Slinton     integer fileaddr;
23418215Slinton 
23518215Slinton     if (a < stkmap.begin) {
23618215Slinton 	fileaddr = datamap.seekaddr + a - datamap.begin;
23718215Slinton     } else {
23818215Slinton 	fileaddr = stkmap.seekaddr + a - stkmap.begin;
23918215Slinton     }
24018215Slinton     fseek(f, fileaddr, 0);
24118215Slinton     fread(buff, nbytes, sizeof(Byte), f);
24218215Slinton }
243