Blob


1 #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"
10 static int syren;
12 Lsym*
13 unique(char *buf, Symbol *s)
14 {
15 Lsym *l;
16 int i, renamed;
18 renamed = 0;
19 strcpy(buf, s->name);
20 for(;;) {
21 l = look(buf);
22 if(l == 0 || (l->lexval == Tid && l->v->set == 0))
23 break;
25 if(syren == 0 && !quiet) {
26 print("Symbol renames:\n");
27 syren = 1;
28 }
29 i = strlen(buf)+1;
30 memmove(buf+1, buf, i);
31 buf[0] = '$';
32 renamed++;
33 if(renamed > 5 && !quiet) {
34 print("Too many renames; must be X source!\n");
35 break;
36 }
37 }
38 if(renamed && !quiet)
39 print("\t%s=%s %c/%L\n", s->name, buf, s->type, s->loc);
40 if(l == 0)
41 l = enter(buf, Tid);
42 return l;
43 }
45 void
46 varsym(void)
47 {
48 Lsym *l;
49 Fhdr *fp;
51 l = mkvar("symbols");
52 if(l->v->set)
53 return;
55 l->v->set = 1;
56 l->v->type = TLIST;
57 l->v->store.u.l = nil;
59 for(fp=fhdrlist; fp; fp=fp->next){
60 if(fp->ftype == FCORE)
61 continue;
62 addvarsym(fp);
63 }
65 if(l->v->store.u.l == nil)
66 print("no debugging symbols\n");
67 }
69 void
70 addvarsym(Fhdr *fp)
71 {
72 int i;
73 Symbol s;
74 Lsym *l;
75 String *file;
76 ulong v;
77 char buf[1024];
78 List *list, **tail, *tl;
80 if(fp == nil)
81 return;
83 l = look("symbols");
84 if(l == nil)
85 return;
87 l->v->set = 1;
88 l->v->type = TLIST;
89 tail = &l->v->store.u.l;
90 while(*tail)
91 tail = &(*tail)->next;
93 file = strnode(fp->filename);
94 for(i=0; findexsym(fp, i, &s)>=0; i++){
95 switch(s.type) {
96 case 'T':
97 case 'L':
98 case 'D':
99 case 'B':
100 case 'b':
101 case 'd':
102 case 'l':
103 case 't':
104 if(s.name[0] == '.')
105 continue;
106 if(s.loc.type != LADDR)
107 continue;
108 v = s.loc.addr;
109 tl = al(TLIST);
110 *tail = tl;
111 tail = &tl->next;
113 l = unique(buf, &s);
114 l->v->set = 1;
115 l->v->type = TINT;
116 l->v->store.u.ival = v;
117 if(l->v->store.comt == 0)
118 l->v->store.fmt = 'X';
120 /* Enter as list of { name, type, value, file } */
121 list = al(TSTRING);
122 tl->store.u.l = list;
123 list->store.u.string = strnode(buf);
124 list->store.fmt = 's';
125 list->next = al(TINT);
126 list = list->next;
127 list->store.fmt = 'c';
128 list->store.u.ival = s.type;
129 list->next = al(TINT);
130 list = list->next;
131 list->store.fmt = 'X';
132 list->store.u.ival = v;
133 list->next = al(TSTRING);
134 list = list->next;
135 list->store.fmt = 's';
136 list->store.u.string = file;
139 *tail = nil;
142 static int
143 infile(List *list, char *file, char **name)
145 /* name */
146 if(list->type != TSTRING)
147 return 0;
148 *name = list->store.u.string->string;
149 if(list->next == nil)
150 return 0;
151 list = list->next;
153 /* type character */
154 if(list->next == nil)
155 return 0;
156 list = list->next;
158 /* address */
159 if(list->next == nil)
160 return 0;
161 list = list->next;
163 /* file */
164 if(list->type != TSTRING)
165 return 0;
166 return strcmp(list->store.u.string->string, file) == 0;
169 void
170 delvarsym(char *file)
172 char *name;
173 Lsym *l;
174 List **lp, *p;
176 l = look("symbols");
177 if(l == nil)
178 return;
180 if(l->v->type != TLIST)
181 return;
183 for(lp=&l->v->store.u.l; *lp; lp=&(*lp)->next){
184 while(*lp){
185 p = *lp;
186 if(p->type != TLIST)
187 break;
188 if(!infile(p->store.u.l, file, &name))
189 break;
190 *lp = p->next;
191 /* XXX remove from hash tables */
193 if(*lp == nil)
194 break;
198 void
199 varreg(void)
201 Lsym *l;
202 Value *v;
203 Regdesc *r;
204 List **tail, *li;
206 l = mkvar("registers");
207 v = l->v;
208 v->set = 1;
209 v->type = TLIST;
210 v->store.u.l = 0;
211 tail = &v->store.u.l;
213 if(mach == nil)
214 return;
216 for(r = mach->reglist; r->name; r++) {
217 l = mkvar(r->name);
218 v = l->v;
219 v->set = 1;
220 v->store.u.ival = r->offset;
221 v->store.fmt = r->format;
222 v->type = TINT;
224 li = al(TSTRING);
225 li->store.u.string = strnode(r->name);
226 li->store.fmt = 's';
227 *tail = li;
228 tail = &li->next;
231 l = mkvar("bpinst"); /* Breakpoint text */
232 v = l->v;
233 v->type = TSTRING;
234 v->store.fmt = 's';
235 v->set = 1;
236 v->store.u.string = gmalloc(sizeof(String));
237 v->store.u.string->len = mach->bpsize;
238 v->store.u.string->string = gmalloc(mach->bpsize);
239 memmove(v->store.u.string->string, mach->bpinst, mach->bpsize);
242 void
243 loadvars(void)
245 Lsym *l;
246 Value *v;
248 l = mkvar("proc");
249 v = l->v;
250 v->type = TINT;
251 v->store.fmt = 'X';
252 v->set = 1;
253 v->store.u.ival = 0;
255 l = mkvar("pid"); /* Current process */
256 v = l->v;
257 v->type = TINT;
258 v->store.fmt = 'D';
259 v->set = 1;
260 v->store.u.ival = 0;
262 mkvar("notes"); /* Pending notes */
264 l = mkvar("proclist"); /* Attached processes */
265 l->v->type = TLIST;
268 String*
269 strnodlen(char *name, int len)
271 String *s;
273 s = gmalloc(sizeof(String)+len+1);
274 s->string = (char*)s+sizeof(String);
275 s->len = len;
276 if(name != 0)
277 memmove(s->string, name, len);
278 s->string[len] = '\0';
280 s->gc.gclink = gcl;
281 gcl = (Gc*)s;
283 return s;
286 String*
287 strnode(char *name)
289 return strnodlen(name, strlen(name));
292 String*
293 runenode(Rune *name)
295 int len;
296 Rune *p;
297 String *s;
299 p = name;
300 for(len = 0; *p; p++)
301 len++;
303 len++;
304 len *= sizeof(Rune);
305 s = gmalloc(sizeof(String)+len);
306 s->string = (char*)s+sizeof(String);
307 s->len = len;
308 memmove(s->string, name, len);
310 s->gc.gclink = gcl;
311 gcl = (Gc*)s;
313 return s;
316 String*
317 stradd(String *l, String *r)
319 int len;
320 String *s;
322 len = l->len+r->len;
323 s = gmalloc(sizeof(String)+len+1);
324 s->gc.gclink = gcl;
325 gcl = (Gc*)s;
326 s->len = len;
327 s->string = (char*)s+sizeof(String);
328 memmove(s->string, l->string, l->len);
329 memmove(s->string+l->len, r->string, r->len);
330 s->string[s->len] = 0;
331 return s;
334 int
335 scmp(String *sr, String *sl)
337 if(sr->len != sl->len)
338 return 0;
340 if(memcmp(sr->string, sl->string, sl->len))
341 return 0;
343 return 1;