]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libmach/map.c
merge
[plan9front.git] / sys / src / libmach / map.c
1 /*
2  * file map routines
3  */
4 #include <u.h>
5 #include <libc.h>
6 #include <bio.h>
7 #include <mach.h>
8
9 Map *
10 newmap(Map *map, int n)
11 {
12         int size;
13
14         size = sizeof(Map)+(n-1)*sizeof(struct segment);
15         if (map == 0)
16                 map = malloc(size);
17         else
18                 map = realloc(map, size);
19         if (map == 0) {
20                 werrstr("out of memory: %r");
21                 return 0;
22         }
23         memset(map, 0, size);
24         map->nsegs = n;
25         return map;
26 }
27
28 int
29 setmap(Map *map, int fd, uvlong b, uvlong e, vlong f, char *name)
30 {
31         int i;
32
33         if (map == 0)
34                 return 0;
35         for (i = 0; i < map->nsegs; i++)
36                 if (!map->seg[i].inuse)
37                         break;
38         if (i >= map->nsegs)
39                 return 0;
40         map->seg[i].b = b;
41         map->seg[i].e = e;
42         map->seg[i].f = f;
43         map->seg[i].inuse = 1;
44         map->seg[i].name = name;
45         map->seg[i].fd = fd;
46         return 1;
47 }
48
49 static uvlong
50 stacktop(int pid)
51 {
52         char buf[64];
53         int fd;
54         int n;
55         char *cp;
56
57         snprint(buf, sizeof(buf), "/proc/%d/segment", pid);
58         fd = open(buf, 0);
59         if (fd < 0)
60                 return 0;
61         n = read(fd, buf, sizeof(buf)-1);
62         close(fd);
63         buf[n] = 0;
64         if (strncmp(buf, "Stack", 5))
65                 return 0;
66         for (cp = buf+5; *cp && *cp == ' '; cp++)
67                 ;
68         if (!*cp)
69                 return 0;
70         cp = strchr(cp, ' ');
71         if (!cp)
72                 return 0;
73         while (*cp && *cp == ' ')
74                 cp++;
75         if (!*cp)
76                 return 0;
77         return strtoull(cp, 0, 16);
78 }
79
80 Map*
81 attachproc(int pid, int kflag, int corefd, Fhdr *fp)
82 {
83         char buf[64], *regs;
84         int fd;
85         Map *map;
86         uvlong n;
87         int mode;
88
89         map = newmap(0, 4);
90         if (!map)
91                 return 0;
92         if(kflag)
93                 regs = "kregs";
94         else
95                 regs = "regs";
96         mode = ORDWR;
97         if (mach->regsize) {
98                 sprint(buf, "/proc/%d/%s", pid, regs);
99                 fd = open(buf, mode);
100                 if(fd < 0) {
101                         free(map);
102                         return 0;
103                 }
104                 setmap(map, fd, 0, mach->regsize, 0, "regs");
105         }
106         if (mach->fpregsize) {
107                 sprint(buf, "/proc/%d/fpregs", pid);
108                 fd = open(buf, mode);
109                 if(fd < 0) {
110                         close(map->seg[0].fd);
111                         free(map);
112                         return 0;
113                 }
114                 setmap(map, fd, mach->regsize, mach->regsize+mach->fpregsize, 0, "fpregs");
115         }
116         setmap(map, corefd, fp->txtaddr, fp->txtaddr+fp->txtsz, fp->txtaddr, "text");
117         if(kflag || fp->dataddr >= mach->utop) {
118                 setmap(map, corefd, fp->dataddr, ~0, fp->dataddr, "data");
119                 return map;
120         }
121         n = stacktop(pid);
122         if (n == 0) {
123                 setmap(map, corefd, fp->dataddr, mach->utop, fp->dataddr, "data");
124                 return map;
125         }
126         setmap(map, corefd, fp->dataddr, n, fp->dataddr, "data");
127         return map;
128 }
129         
130 int
131 findseg(Map *map, char *name)
132 {
133         int i;
134
135         if (!map)
136                 return -1;
137         for (i = 0; i < map->nsegs; i++)
138                 if (map->seg[i].inuse && !strcmp(map->seg[i].name, name))
139                         return i;
140         return -1;
141 }
142
143 void
144 unusemap(Map *map, int i)
145 {
146         if (map != 0 && 0 <= i && i < map->nsegs)
147                 map->seg[i].inuse = 0;
148 }
149
150 Map*
151 loadmap(Map *map, int fd, Fhdr *fp)
152 {
153         map = newmap(map, 2);
154         if (map == 0)
155                 return 0;
156
157         map->seg[0].b = fp->txtaddr;
158         map->seg[0].e = fp->txtaddr+fp->txtsz;
159         map->seg[0].f = fp->txtoff;
160         map->seg[0].fd = fd;
161         map->seg[0].inuse = 1;
162         map->seg[0].name = "text";
163         map->seg[1].b = fp->dataddr;
164         map->seg[1].e = fp->dataddr+fp->datsz;
165         map->seg[1].f = fp->datoff;
166         map->seg[1].fd = fd;
167         map->seg[1].inuse = 1;
168         map->seg[1].name = "data";
169         return map;
170 }