plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

setup.c (2520B)


      1 /*
      2  * init routines
      3  */
      4 #include "defs.h"
      5 #include "fns.h"
      6 
      7 char	*symfil;
      8 char	*corfil;
      9 
     10 Map	*symmap;
     11 Map	*cormap;
     12 Regs	*correg;
     13 Map	*dotmap;
     14 
     15 void
     16 setsym(void)
     17 {
     18 	if(symhdr && symopen(symhdr) < 0)
     19 		dprint("symopen: %r\n");
     20 /*
     21 	Symbol s;
     22 	if (mach->sbreg && lookup(0, mach->sbreg, &s) < 0)
     23 		mach->sb = s.loc.addr;
     24 */
     25 }
     26 
     27 void
     28 setcor(void)
     29 {
     30 	static int mapped;
     31 
     32 	if (corhdr) {
     33 		if (!mapped) {
     34 			if (mapfile(corhdr, 0, cormap, &correg) < 0)
     35 				dprint("mapfile %s: %r\n", corfil);
     36 			mapped = 1;
     37 		}
     38 		free(correg);
     39 		if (pid == 0 && corhdr->nthread > 0)
     40 			pid = corhdr->thread[0].id;
     41 		correg = coreregs(corhdr, pid);
     42 		if(correg == nil)
     43 			dprint("no such pid in core dump\n");
     44 	} else {
     45 		unmapproc(cormap);
     46 		unmapfile(corhdr, cormap);
     47 		free(correg);
     48 		correg = nil;
     49 
     50 		if (pid > 0) {
     51 			if (mapproc(pid, cormap, &correg) < 0)
     52 				dprint("mapproc %d: %r\n", pid);
     53 		} else
     54 			dprint("no core image\n");
     55 	}
     56 	kmsys();
     57 	return;
     58 }
     59 
     60 Map*
     61 dumbmap(int fd)
     62 {
     63 	Map *dumb;
     64 	Seg s;
     65 
     66 	dumb = allocmap();
     67 	memset(&s, 0, sizeof s);
     68 	s.fd = fd;
     69 	s.base = 0;
     70 	s.offset = 0;
     71 	s.size = 0xFFFFFFFF;
     72 	s.name = "data";
     73 	s.file = "<dumb>";
     74 	if(addseg(dumb, s) < 0){
     75 		freemap(dumb);
     76 		return nil;
     77 	}
     78 	if(mach == nil)
     79 		mach = machcpu;
     80 	return dumb;
     81 }
     82 
     83 /*
     84  * set up maps for a direct process image (/proc)
     85  */
     86 void
     87 cmdmap(Map *map)
     88 {
     89 	int i;
     90 	char name[MAXSYM];
     91 
     92 	rdc();
     93 	readsym(name);
     94 	i = findseg(map, name, nil);
     95 	if (i < 0)	/* not found */
     96 		error("Invalid map name");
     97 
     98 	if (expr(0)) {
     99 	/*	if (strcmp(name, "text") == 0) */
    100 	/*		textseg(expv, &fhdr); */
    101 		map->seg[i].base = expv;
    102 	} else
    103 		error("Invalid base address");
    104 	if (expr(0))
    105 		map->seg[i].size = expv - map->seg[i].base;
    106 	else
    107 		error("Invalid end address");
    108 	if (expr(0))
    109 		map->seg[i].offset = expv;
    110 	else
    111 		error("Invalid file offset");
    112 /*
    113 	if (rdc()=='?' && map == cormap) {
    114 		if (fcor)
    115 			close(fcor);
    116 		fcor=fsym;
    117 		corfil = symfil;
    118 		cormap = symmap;
    119 	} else if (lastc == '/' && map == symmap) {
    120 		if (fsym)
    121 			close(fsym);
    122 		fsym=fcor;
    123 		symfil=corfil;
    124 		symmap=cormap;
    125 	} else
    126 		reread();
    127 */
    128 }
    129 
    130 void
    131 kmsys(void)
    132 {
    133 	int i;
    134 
    135 	i = findseg(symmap, "text", symfil);
    136 	if (i >= 0) {
    137 		symmap->seg[i].base = symmap->seg[i].base&~mach->ktmask;
    138 		symmap->seg[i].size = -symmap->seg[i].base;
    139 	}
    140 	i = findseg(symmap, "data", symfil);
    141 	if (i >= 0) {
    142 		symmap->seg[i].base = symmap->seg[i].base&~mach->ktmask;
    143 		symmap->seg[i].size = -symmap->seg[i].base;
    144 	}
    145 }
    146 
    147 void
    148 attachprocess(void)
    149 {
    150 	if (!adrflg) {
    151 		dprint("usage: pid$a\n");
    152 		return;
    153 	}
    154 	pid = adrval;
    155 	setcor();
    156 }