Blob
1 #include <u.h>2 #include <libc.h>3 #include <mach.h>5 Fhdr *symhdr;6 Fhdr *corhdr;7 char *symfil;8 char *corfil;9 int corpid;10 Regs *correg;11 Map *symmap;12 Map *cormap;14 static int15 alldigs(char *s)16 {17 while(*s){18 if(*s<'0' || '9'<*s)19 return 0;20 s++;21 }22 return 1;23 }25 /*26 * attach to arguments in argc, argv27 */28 int29 attachargs(int argc, char **argv, int omode)30 {31 int i;32 Fhdr *hdr;33 char *s;35 symhdr = nil;36 corhdr = nil;37 symfil = nil;38 corfil = nil;39 corpid = 0;40 correg = nil;42 for(i=0; i<argc; i++){43 if(alldigs(argv[i])){44 if(corpid){45 fprint(2, "already have corpid %d; ignoring corpid %d\n", corpid, argv[i]);46 continue;47 }48 if(corhdr){49 fprint(2, "already have core %s; ignoring corpid %d\n", corfil, corpid);50 continue;51 }52 corpid = atoi(argv[i]);53 continue;54 }55 if((hdr = crackhdr(argv[i], omode)) == nil){56 fprint(2, "crackhdr %s: %r\n", argv[i]);57 continue;58 }59 fprint(2, "%s: %s %s %s\n", argv[i], hdr->aname, hdr->mname, hdr->fname);60 if(hdr->ftype == FCORE){61 if(corpid){62 fprint(2, "already have corpid %d; ignoring core %s\n", corpid, argv[i]);63 uncrackhdr(hdr);64 continue;65 }66 if(corhdr){67 fprint(2, "already have core %s; ignoring core %s\n", corfil, argv[i]);68 uncrackhdr(hdr);69 continue;70 }71 corhdr = hdr;72 corfil = argv[i];73 }else{74 if(symhdr){75 fprint(2, "already have text %s; ignoring text %s\n", symfil, argv[i]);76 uncrackhdr(hdr);77 continue;78 }79 symhdr = hdr;80 symfil = argv[i];81 }82 }84 if(symhdr == nil){85 symfil = "a.out"; /* default */86 if(corpid){ /* try from corpid */87 if((s = proctextfile(corpid)) != nil){88 fprint(2, "corpid %d: text %s\n", corpid, s);89 symfil = s;90 }91 }92 if(corhdr){ /* try from core */93 if(corhdr->txtfil != nil){94 fprint(2, "core %s: text %s\n", corfil, corhdr->txtfil);95 symfil = corhdr->txtfil;96 }97 }98 if((symhdr = crackhdr(symfil, omode)) == nil){99 fprint(2, "crackhdr %s: %r\n", symfil);100 symfil = nil;101 }102 }104 if(symhdr)105 symopen(symhdr);107 if(!mach)108 mach = machcpu;110 /*111 * Set up maps112 */113 symmap = allocmap();114 cormap = allocmap();115 if(symmap == nil || cormap == nil)116 sysfatal("allocating maps: %r");118 if(symhdr){119 if(mapfile(symhdr, 0, symmap, nil) < 0)120 fprint(2, "mapfile %s: %r\n", symfil);121 mapfile(symhdr, 0, cormap, nil);122 }124 if(corpid)125 attachproc(corpid);127 if(corhdr)128 attachcore(corhdr);130 return 0;131 }133 static int thecorpid;134 static Fhdr *thecorhdr;136 static void137 unattach(void)138 {139 unmapproc(cormap);140 unmapfile(corhdr, cormap);141 free(correg);142 correg = nil;143 thecorpid = 0;144 thecorhdr = nil;145 corpid = 0;146 corhdr = nil;147 corfil = nil;148 }150 int151 attachproc(int pid)152 {153 unattach();154 if(pid == 0)155 return 0;156 if(mapproc(pid, cormap, &correg) < 0){157 fprint(2, "attachproc %d: %r\n", pid);158 return -1;159 }160 thecorpid = pid;161 corpid = pid;162 return 0;163 }165 int166 attachcore(Fhdr *hdr)167 {168 unattach();169 if(corhdr == nil)170 return 0;171 if(mapfile(hdr, 0, cormap, &correg) < 0){172 fprint(2, "attachcore %s: %r\n", hdr->filename);173 return -1;174 }175 thecorhdr = hdr;176 corhdr = hdr;177 corfil = hdr->filename;178 return 0;179 }181 int182 attachdynamic(void)183 {184 extern void elfdl386mapdl(void);185 elfdl386mapdl();186 return 0;187 }