Blob
1 #include <u.h>2 #include <libc.h>3 #include <bio.h>4 #include <ctype.h>5 #include <mach.h>6 #define Extern extern7 #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 void46 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 }64 if(l->v->store.u.l == nil)65 print("no debugging symbols\n");66 }68 void69 addvarsym(Fhdr *fp)70 {71 int i;72 Symbol s;73 Lsym *l;74 String *file;75 ulong v;76 char buf[1024];77 List *list, **tail, *tl;79 if(fp == nil)80 return;82 l = look("symbols");83 if(l == nil)84 return;86 l->v->set = 1;87 l->v->type = TLIST;88 tail = &l->v->store.u.l;89 while(*tail)90 tail = &(*tail)->next;92 file = strnode(fp->filename);93 for(i=0; findexsym(fp, i, &s)>=0; i++){94 switch(s.type) {95 case 'T':96 case 'L':97 case 'D':98 case 'B':99 case 'b':100 case 'd':101 case 'l':102 case 't':103 if(s.name[0] == '.')104 continue;105 if(s.loc.type != LADDR)106 continue;107 v = s.loc.addr;108 tl = al(TLIST);109 *tail = tl;110 tail = &tl->next;112 l = unique(buf, &s);113 l->v->set = 1;114 l->v->type = TINT;115 l->v->store.u.ival = v;116 if(l->v->store.comt == 0)117 l->v->store.fmt = 'X';119 /* Enter as list of { name, type, value, file } */120 list = al(TSTRING);121 tl->store.u.l = list;122 list->store.u.string = strnode(buf);123 list->store.fmt = 's';124 list->next = al(TINT);125 list = list->next;126 list->store.fmt = 'c';127 list->store.u.ival = s.type;128 list->next = al(TINT);129 list = list->next;130 list->store.fmt = 'X';131 list->store.u.ival = v;132 list->next = al(TSTRING);133 list = list->next;134 list->store.fmt = 's';135 list->store.u.string = file;136 }137 }138 *tail = nil;139 }141 static int142 infile(List *list, char *file, char **name)143 {144 /* name */145 if(list->type != TSTRING)146 return 0;147 *name = list->store.u.string->string;148 if(list->next == nil)149 return 0;150 list = list->next;152 /* type character */153 if(list->next == nil)154 return 0;155 list = list->next;157 /* address */158 if(list->next == nil)159 return 0;160 list = list->next;162 /* file */163 if(list->type != TSTRING)164 return 0;165 return strcmp(list->store.u.string->string, file) == 0;166 }168 void169 delvarsym(char *file)170 {171 char *name;172 Lsym *l;173 List **lp, *p;175 l = look("symbols");176 if(l == nil)177 return;179 if(l->v->type != TLIST)180 return;182 for(lp=&l->v->store.u.l; *lp; lp=&(*lp)->next){183 while(*lp){184 p = *lp;185 if(p->type != TLIST)186 break;187 if(!infile(p->store.u.l, file, &name))188 break;189 *lp = p->next;190 /* XXX remove from hash tables */191 }192 if(*lp == nil)193 break;194 }195 }197 void198 varreg(void)199 {200 Lsym *l;201 Value *v;202 Regdesc *r;203 List **tail, *li;205 l = mkvar("registers");206 v = l->v;207 v->set = 1;208 v->type = TLIST;209 v->store.u.l = 0;210 tail = &v->store.u.l;212 if(mach == nil)213 return;215 for(r = mach->reglist; r->name; r++) {216 l = mkvar(r->name);217 v = l->v;218 v->set = 1;219 v->store.u.reg.name = r->name;220 v->store.u.reg.thread = 0;221 v->store.fmt = r->format;222 v->type = TREG;224 li = al(TSTRING);225 li->store.u.string = strnode(r->name);226 li->store.fmt = 's';227 *tail = li;228 tail = &li->next;229 }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);240 }242 void243 loadvars(void)244 {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;266 }268 String*269 strnodlen(char *name, int len)270 {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;284 }286 String*287 strnode(char *name)288 {289 return strnodlen(name, strlen(name));290 }292 String*293 runenode(Rune *name)294 {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;314 }316 String*317 stradd(String *l, String *r)318 {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;332 }334 int335 scmp(String *sr, String *sl)336 {337 if(sr->len != sl->len)338 return 0;340 if(memcmp(sr->string, sl->string, sl->len))341 return 0;343 return 1;344 }