blob: e6efc95a0da1d031b09e679303577e2c5f7da1db [file] [log] [blame]
rsc564ca702004-04-19 19:32:07 +00001#include <u.h>
2#include <libc.h>
3#include <bio.h>
4#include <ctype.h>
5#include <mach.h>
6#define Extern extern
7#include "acid.h"
8#include "y.tab.h"
9
10static void install(int);
11
12void
13sproc(int xpid)
14{
15 Lsym *s;
16 int i;
rsc783aadb2005-02-11 19:44:37 +000017 Regs *regs;
rsc564ca702004-04-19 19:32:07 +000018
19 if(symmap == 0)
20 error("no map");
21
22 if(pid == xpid)
23 return;
24
rsc4f2ac1b2005-01-23 22:48:19 +000025 if(corhdr){
rsc783aadb2005-02-11 19:44:37 +000026 regs = coreregs(corhdr, xpid);
27 if(regs == nil)
rsc4f2ac1b2005-01-23 22:48:19 +000028 error("no such pid in core dump");
rsc783aadb2005-02-11 19:44:37 +000029 free(correg);
30 correg = regs;
rsc4f2ac1b2005-01-23 22:48:19 +000031 }else{
32 /* XXX should only change register set here if cormap already mapped */
33 if(xpid <= 0)
34 error("bad pid");
35 unmapproc(cormap);
36 unmapfile(corhdr, cormap);
37 free(correg);
38 correg = nil;
rsc783aadb2005-02-11 19:44:37 +000039 pid = -1;
40 corpid = -1;
rsc564ca702004-04-19 19:32:07 +000041
rsc4f2ac1b2005-01-23 22:48:19 +000042 if(mapproc(xpid, cormap, &correg) < 0)
43 error("setproc %d: %r", xpid);
rsc564ca702004-04-19 19:32:07 +000044
rsc4f2ac1b2005-01-23 22:48:19 +000045 /* XXX check text file here? */
rsc564ca702004-04-19 19:32:07 +000046
rsc4f2ac1b2005-01-23 22:48:19 +000047 for(i=0; i<cormap->nseg; i++){
48 if(cormap->seg[i].file == nil){
49 if(strcmp(cormap->seg[i].name, "data") == 0)
50 cormap->seg[i].name = "*data";
51 if(strcmp(cormap->seg[i].name, "text") == 0)
52 cormap->seg[i].name = "*text";
53 }
54 }
55 }
rsc564ca702004-04-19 19:32:07 +000056 pid = xpid;
rsc211073f2005-02-11 16:44:04 +000057 corpid = pid;
rsc564ca702004-04-19 19:32:07 +000058 s = look("pid");
59 s->v->store.u.ival = pid;
60
rsc564ca702004-04-19 19:32:07 +000061 install(pid);
62}
63
64int
65nproc(char **argv)
66{
rscb4a659b2004-04-19 23:03:46 +000067 int pid, i;
rsc564ca702004-04-19 19:32:07 +000068
69 pid = fork();
70 switch(pid) {
71 case -1:
72 error("new: fork %r");
73 case 0:
74 rfork(RFNAMEG|RFNOTEG);
75 if(ctlproc(getpid(), "hang") < 0)
76 fatal("new: hang %d: %r", getpid());
77
78 close(0);
79 close(1);
80 close(2);
81 for(i = 3; i < NFD; i++)
82 close(i);
83
84 open("/dev/tty", OREAD);
85 open("/dev/tty", OWRITE);
86 open("/dev/tty", OWRITE);
rsc4b016102005-11-28 00:43:50 +000087 execv(argv[0], argv);
88 fatal("new: exec %s: %r", argv[0]);
rsc564ca702004-04-19 19:32:07 +000089 default:
90 install(pid);
rsc689be542005-11-28 00:39:42 +000091 msg(pid, "attached");
rsc564ca702004-04-19 19:32:07 +000092 msg(pid, "waitstop");
93 notes(pid);
94 sproc(pid);
95 dostop(pid);
96 break;
97 }
98
99 return pid;
100}
101
102void
103notes(int pid)
104{
105 Lsym *s;
106 Value *v;
107 int i, n;
108 char **notes;
109 List *l, **tail;
110
111 s = look("notes");
112 if(s == 0)
113 return;
114
115 v = s->v;
116 n = procnotes(pid, &notes);
117 if(n < 0)
118 error("procnotes pid=%d: %r", pid);
119
120 v->set = 1;
121 v->type = TLIST;
122 v->store.u.l = 0;
123 tail = &v->store.u.l;
124 for(i=0; i<n; i++) {
125 l = al(TSTRING);
126 l->store.u.string = strnode(notes[i]);
127 l->store.fmt = 's';
128 *tail = l;
129 tail = &l->next;
130 }
131 free(notes);
132}
133
134void
135dostop(int pid)
136{
137 Lsym *s;
138 Node *np, *p;
139
140 s = look("stopped");
141 if(s && s->proc) {
142 np = an(ONAME, ZN, ZN);
143 np->sym = s;
144 np->store.fmt = 'D';
145 np->type = TINT;
146 p = con(pid);
147 p->store.fmt = 'D';
148 np = an(OCALL, np, p);
149 execute(np);
150 }
151}
152
153static void
154install(int pid)
155{
156 Lsym *s;
157 List *l;
158 int i, new, p;
159
160 new = -1;
161 for(i = 0; i < Maxproc; i++) {
162 p = ptab[i].pid;
163 if(p == pid)
164 return;
165 if(p == 0 && new == -1)
166 new = i;
167 }
168 if(new == -1)
169 error("no free process slots");
170
171 ptab[new].pid = pid;
172
173 s = look("proclist");
174 l = al(TINT);
175 l->store.fmt = 'D';
176 l->store.u.ival = pid;
177 l->next = s->v->store.u.l;
178 s->v->store.u.l = l;
179 s->v->set = 1;
180}
181
rsc4f2ac1b2005-01-23 22:48:19 +0000182/*
rsc2e965b32004-05-05 04:22:16 +0000183static int
184installed(int pid)
185{
186 int i;
187
188 for(i=0; i<Maxproc; i++)
189 if(ptab[i].pid == pid)
190 return 1;
191 return 0;
192}
rsc4f2ac1b2005-01-23 22:48:19 +0000193*/
rsc2e965b32004-05-05 04:22:16 +0000194
rsc564ca702004-04-19 19:32:07 +0000195void
196deinstall(int pid)
197{
198 int i;
199 Lsym *s;
200 List *f, **d;
201
202 for(i = 0; i < Maxproc; i++) {
203 if(ptab[i].pid == pid) {
204 detachproc(pid);
rsccbeb0b22006-04-01 19:24:03 +0000205 /* close(ptab[i].ctl); */
rsc564ca702004-04-19 19:32:07 +0000206 ptab[i].pid = 0;
207 s = look("proclist");
208 d = &s->v->store.u.l;
209 for(f = *d; f; f = f->next) {
210 if(f->store.u.ival == pid) {
211 *d = f->next;
212 break;
213 }
214 }
215 s = look("pid");
216 if(s->v->store.u.ival == pid)
217 s->v->store.u.ival = 0;
218 return;
219 }
220 }
221}
222
223void
224msg(int pid, char *msg)
225{
226 int i;
227 char err[ERRMAX];
228
229 for(i = 0; i < Maxproc; i++) {
230 if(ptab[i].pid == pid) {
231 if(ctlproc(pid, msg) < 0){
232 errstr(err, sizeof err);
233 if(strcmp(err, "process exited") == 0)
234 deinstall(pid);
235 error("msg: pid=%d %s: %s", pid, msg, err);
236 }
237 return;
238 }
239 }
240 error("msg: pid=%d: not found for %s", pid, msg);
241}
242
243char *
244getstatus(int pid)
245{
rsc689be542005-11-28 00:39:42 +0000246 return "unknown";
rsc564ca702004-04-19 19:32:07 +0000247}
248