aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/db/setup.c
blob: eaeb61f2b140ab241ae0af4091ec562388239efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * init routines
 */
#include "defs.h"
#include "fns.h"

char	*symfil;
char	*corfil;

Map	*symmap;
Map	*cormap;
Regs	*correg;
Map	*dotmap;

void
setsym(void)
{
	if(symhdr && symopen(symhdr) < 0)
		dprint("symopen: %r\n");
/*
	Symbol s;
	if (mach->sbreg && lookup(0, mach->sbreg, &s) < 0)
		mach->sb = s.loc.addr;
*/
}

void
setcor(void)
{
	static int mapped;

	if (corhdr) {
		if (!mapped) {
			if (mapfile(corhdr, 0, cormap, &correg) < 0)
				dprint("mapfile %s: %r\n", corfil);
			mapped = 1;
		}
		free(correg);
		if (pid == 0 && corhdr->nthread > 0)
			pid = corhdr->thread[0].id;
		correg = coreregs(corhdr, pid);
		if(correg == nil)
			dprint("no such pid in core dump\n");
	} else {
		unmapproc(cormap);
		unmapfile(corhdr, cormap);
		free(correg);
		correg = nil;

		if (pid > 0) {
			if (mapproc(pid, cormap, &correg) < 0)
				dprint("mapproc %d: %r\n", pid);
		} else
			dprint("no core image\n");
	}
	kmsys();
	return;
}

Map*
dumbmap(int fd)
{
	Map *dumb;
	Seg s;

	dumb = allocmap();
	memset(&s, 0, sizeof s);
	s.fd = fd;
	s.base = 0;
	s.offset = 0;
	s.size = 0xFFFFFFFF;
	s.name = "data";
	s.file = "<dumb>";
	if(addseg(dumb, s) < 0){
		freemap(dumb);
		return nil;
	}
	if(mach == nil)
		mach = machcpu;
	return dumb;
}

/*
 * set up maps for a direct process image (/proc)
 */
void
cmdmap(Map *map)
{
	int i;
	char name[MAXSYM];

	rdc();
	readsym(name);
	i = findseg(map, name, nil);
	if (i < 0)	/* not found */
		error("Invalid map name");

	if (expr(0)) {
	/*	if (strcmp(name, "text") == 0) */
	/*		textseg(expv, &fhdr); */
		map->seg[i].base = expv;
	} else
		error("Invalid base address"); 
	if (expr(0))
		map->seg[i].size = expv - map->seg[i].base;
	else
		error("Invalid end address"); 
	if (expr(0))
		map->seg[i].offset = expv; 
	else
		error("Invalid file offset"); 
/*
	if (rdc()=='?' && map == cormap) {
		if (fcor)
			close(fcor);
		fcor=fsym;
		corfil = symfil;
		cormap = symmap;
	} else if (lastc == '/' && map == symmap) {
		if (fsym)
			close(fsym);
		fsym=fcor;
		symfil=corfil;
		symmap=cormap;
	} else
		reread();
*/
}

void
kmsys(void)
{
	int i;

	i = findseg(symmap, "text", symfil);
	if (i >= 0) {
		symmap->seg[i].base = symmap->seg[i].base&~mach->ktmask;
		symmap->seg[i].size = -symmap->seg[i].base;
	}
	i = findseg(symmap, "data", symfil);
	if (i >= 0) {
		symmap->seg[i].base = symmap->seg[i].base&~mach->ktmask;
		symmap->seg[i].size = -symmap->seg[i].base;
	}
}

void
attachprocess(void)
{
	if (!adrflg) {
		dprint("usage: pid$a\n");
		return;
	}
	pid = adrval;
	setcor();
}