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*26334Ssam static char sccsid[] = "@(#)coredump.c 5.2 (Berkeley) 02/23/86"; 921627Sdist #endif not lint 109661Slinton 1118215Slinton static char rcsid[] = "$Header: coredump.c,v 1.5 84/12/26 10:38:56 linton 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*26334Ssam fseek(corefile, 60*26334Ssam datamap.seekaddr + physaddr(s->symvalue.offset) - datamap.begin, 0); 6118215Slinton get(corefile, masterpcbb); 62*26334Ssam masterpcbb = (masterpcbb&PG_PFNUM)*NBPG; 6318215Slinton getpcb(); 6418215Slinton } 6518215Slinton 6618215Slinton /* 6718215Slinton * Read the user area information from the core dump. 6818215Slinton */ 699661Slinton 7018215Slinton public coredump_xreadin(mask, reg, signo) 7118215Slinton int *mask; 7218215Slinton Word reg[]; 73*26334Ssam short *signo; 7418215Slinton { 7518215Slinton register struct user *up; 7618215Slinton register Word *savreg; 7718215Slinton union { 7818215Slinton struct user u; 7918215Slinton char dummy[ctob(UPAGES)]; 8018215Slinton } ustruct; 8118215Slinton Symbol s; 8218215Slinton 8318215Slinton objfile = fopen(objname, "r"); 8418215Slinton if (objfile == nil) { 8518215Slinton fatal("can't read \"%s\"", objname); 869661Slinton } 8718215Slinton get(objfile, hdr); 8818215Slinton if (vaddrs) { 8918215Slinton datamap.begin = 0; 9018215Slinton datamap.end = 0xffffffff; 9118215Slinton stkmap.begin = 0xffffffff; 9218215Slinton stkmap.end = 0xffffffff; 9318215Slinton } else { 9418215Slinton up = &(ustruct.u); 9518215Slinton fread(up, ctob(UPAGES), 1, corefile); 9618215Slinton savreg = (Word *) &(ustruct.dummy[ctob(UPAGES)]); 9718215Slinton *mask = savreg[PS]; 9818215Slinton copyregs(savreg, reg); 9918215Slinton *signo = up->u_arg[0]; 10018215Slinton datamap.seekaddr = ctob(UPAGES); 101*26334Ssam stkmap.begin = USRSTACK - ctob(up->u_ssize); 102*26334Ssam stkmap.end = USRSTACK; 10318215Slinton stkmap.seekaddr = datamap.seekaddr + ctob(up->u_dsize); 10418215Slinton switch (hdr.a_magic) { 10518215Slinton case OMAGIC: 10618215Slinton datamap.begin = 0; 10718215Slinton datamap.end = ctob(up->u_tsize) + ctob(up->u_dsize); 10818215Slinton break; 10918215Slinton 11018215Slinton case NMAGIC: 11118215Slinton case ZMAGIC: 11218215Slinton datamap.begin = (Address) ptob(btop(ctob(up->u_tsize) - 1) + 1); 11318215Slinton datamap.end = datamap.begin + ctob(up->u_dsize); 11418215Slinton break; 11518215Slinton 11618215Slinton default: 11718215Slinton fatal("bad magic number 0x%x", hdr.a_magic); 11818215Slinton } 11918215Slinton #ifdef UXMAG 12018215Slinton /* 12118215Slinton * Core dump not from this object file? 12218215Slinton */ 12318215Slinton if (hdr.a_magic != 0 and up->u_exdata.ux_mag != 0 and 12418215Slinton hdr.a_magic != up->u_exdata.ux_mag) { 12518215Slinton warning("core dump ignored"); 12618215Slinton coredump = false; 12718215Slinton fclose(corefile); 12818215Slinton fclose(objfile); 12918215Slinton start(nil, nil, nil); 13018215Slinton } 13118215Slinton #endif 13218215Slinton } 1339661Slinton } 1349661Slinton 1359661Slinton public coredump_close() 1369661Slinton { 1379661Slinton fclose(objfile); 1389661Slinton } 1399661Slinton 1409661Slinton public coredump_readtext(buff, addr, nbytes) 1419661Slinton char *buff; 1429661Slinton Address addr; 1439661Slinton int nbytes; 1449661Slinton { 14518215Slinton if (hdr.a_magic == OMAGIC or vaddrs) { 1469661Slinton coredump_readdata(buff, addr, nbytes); 1479661Slinton } else { 1489661Slinton fseek(objfile, N_TXTOFF(hdr) + addr, 0); 1499661Slinton fread(buff, nbytes, sizeof(Byte), objfile); 1509661Slinton } 1519661Slinton } 1529661Slinton 1539661Slinton public coredump_readdata(buff, addr, nbytes) 1549661Slinton char *buff; 1559661Slinton Address addr; 1569661Slinton int nbytes; 1579661Slinton { 15818215Slinton Address a; 15918215Slinton 16018215Slinton a = addr; 16118215Slinton if (a < datamap.begin) { 16216608Ssam if (hdr.a_magic == OMAGIC) { 16318215Slinton error("[data address 0x%x too low (lb = 0x%x)]", a, datamap.begin); 16416608Ssam } else { 16518215Slinton coredump_readtext(buff, a, nbytes); 16616608Ssam } 16718215Slinton } else if (a > stkmap.end) { 16818215Slinton error("data address 0x%x too high (ub = 0x%x)", a, stkmap.end); 1699661Slinton } else { 17018215Slinton if (vaddrs) { 17118215Slinton vreadfromfile(corefile, a, buff, nbytes); 17218215Slinton } else { 17318215Slinton readfromfile(corefile, a, buff, nbytes); 17418215Slinton } 1759661Slinton } 1769661Slinton } 17718215Slinton 17818215Slinton /* 17918215Slinton * Read a block of data from a memory image, mapping virtual addresses. 18018215Slinton * Have to watch out for page boundaries. 18118215Slinton */ 18218215Slinton 18318215Slinton private vreadfromfile (corefile, v, buff, nbytes) 18418215Slinton File corefile; 18518215Slinton Address v; 18618215Slinton char *buff; 18718215Slinton integer nbytes; 18818215Slinton { 18918215Slinton Address a; 19018215Slinton integer i, remainder, pagesize; 19118215Slinton char *bufp; 19218215Slinton 19318215Slinton a = v; 19418215Slinton pagesize = (integer) ptob(1); 19518215Slinton remainder = pagesize - (a mod pagesize); 19618215Slinton if (remainder >= nbytes) { 19718215Slinton readfromfile(corefile, vmap(a), buff, nbytes); 19818215Slinton } else { 19918215Slinton readfromfile(corefile, vmap(a), buff, remainder); 20018215Slinton a += remainder; 20118215Slinton i = nbytes - remainder; 20218215Slinton bufp = buff + remainder; 20318215Slinton while (i > pagesize) { 20418215Slinton readfromfile(corefile, vmap(a), bufp, pagesize); 20518215Slinton a += pagesize; 20618215Slinton bufp += pagesize; 20718215Slinton i -= pagesize; 20818215Slinton } 20918215Slinton readfromfile(corefile, vmap(a), bufp, i); 21018215Slinton } 21118215Slinton } 21218215Slinton 21318215Slinton private readfromfile (f, a, buff, nbytes) 21418215Slinton File f; 21518215Slinton Address a; 21618215Slinton char *buff; 21718215Slinton integer nbytes; 21818215Slinton { 21918215Slinton integer fileaddr; 22018215Slinton 22118215Slinton if (a < stkmap.begin) { 22218215Slinton fileaddr = datamap.seekaddr + a - datamap.begin; 22318215Slinton } else { 22418215Slinton fileaddr = stkmap.seekaddr + a - stkmap.begin; 22518215Slinton } 22618215Slinton fseek(f, fileaddr, 0); 22718215Slinton fread(buff, nbytes, sizeof(Byte), f); 22818215Slinton } 229