tcmdline.c - plan9port - [fork] Plan 9 from user space
HTML git clone git://src.adamsgaard.dk/plan9port
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
tcmdline.c (3559B)
---
1 #include <u.h>
2 #include <libc.h>
3 #include <mach.h>
4
5 Fhdr *symhdr;
6 Fhdr *corhdr;
7 char *symfil;
8 char *corfil;
9 int corpid;
10 Regs *correg;
11 Map *symmap;
12 Map *cormap;
13
14 static int
15 alldigs(char *s)
16 {
17 while(*s){
18 if(*s<'0' || '9'<*s)
19 return 0;
20 s++;
21 }
22 return 1;
23 }
24
25 /*
26 * attach to arguments in argc, argv
27 */
28 int
29 attachargs(int argc, char **argv, int omode, int verbose)
30 {
31 int i;
32 Fhdr *hdr;
33 char *s, *t;
34
35 symhdr = nil;
36 corhdr = nil;
37 symfil = nil;
38 corfil = nil;
39 corpid = 0;
40 correg = nil;
41
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 if(verbose)
60 fprint(2, "%s: %s %s %s\n", argv[i], hdr->aname, hdr->mname, hdr->fname);
61 if(hdr->ftype == FCORE){
62 if(corpid){
63 fprint(2, "already have corpid %d; ignoring core %s\n", corpid, argv[i]);
64 uncrackhdr(hdr);
65 continue;
66 }
67 if(corhdr){
68 fprint(2, "already have core %s; ignoring core %s\n", corfil, argv[i]);
69 uncrackhdr(hdr);
70 continue;
71 }
72 corhdr = hdr;
73 corfil = argv[i];
74 }else{
75 if(symhdr){
76 fprint(2, "already have text %s; ignoring text %s\n", symfil, argv[i]);
77 uncrackhdr(hdr);
78 continue;
79 }
80 symhdr = hdr;
81 symfil = argv[i];
82 }
83 }
84
85 if(symhdr == nil){
86 symfil = "a.out"; /* default */
87 if(corpid){ /* try from corpid */
88 if((s = proctextfile(corpid)) != nil){
89 if(verbose)
90 fprint(2, "corpid %d: text %s\n", corpid, s);
91 symfil = s;
92 }
93 }
94 if(corhdr && corhdr->cmdline){ /* try from core */
95 /*
96 * prog gives only the basename of the command,
97 * so try the command line for a path.
98 */
99 if((s = strdup(corhdr->cmdline)) != nil){
100 t = strchr(s, ' ');
101 if(t)
102 *t = 0;
103 if((t = searchpath(s)) != nil){
104 if(verbose)
105 fprint(2, "core: text %s\n", t);
106 symfil = t;
107 }
108 free(s);
109 }
110 }
111 if((symhdr = crackhdr(symfil, omode)) == nil){
112 fprint(2, "crackhdr %s: %r\n", symfil);
113 symfil = nil;
114 }
115 }
116
117 if(symhdr)
118 symopen(symhdr);
119
120 if(!mach)
121 mach = machcpu;
122
123 /*
124 * Set up maps
125 */
126 symmap = allocmap();
127 cormap = allocmap();
128 if(symmap == nil || cormap == nil)
129 sysfatal("allocating maps: %r");
130
131 if(symhdr){
132 if(mapfile(symhdr, 0, symmap, nil) < 0)
133 fprint(2, "mapfile %s: %r\n", symfil);
134 mapfile(symhdr, 0, cormap, nil);
135 }
136
137 if(corpid)
138 attachproc(corpid);
139 if(corhdr)
140 attachcore(corhdr);
141
142 attachdynamic(verbose);
143 return 0;
144 }
145
146 static int thecorpid;
147 static Fhdr *thecorhdr;
148
149 static void
150 unattach(void)
151 {
152 unmapproc(cormap);
153 unmapfile(corhdr, cormap);
154 free(correg);
155 correg = nil;
156 thecorpid = 0;
157 thecorhdr = nil;
158 corpid = 0;
159 corhdr = nil;
160 corfil = nil;
161 }
162
163 int
164 attachproc(int pid)
165 {
166 unattach();
167 if(pid == 0)
168 return 0;
169 if(mapproc(pid, cormap, &correg) < 0){
170 fprint(2, "attachproc %d: %r\n", pid);
171 return -1;
172 }
173 thecorpid = pid;
174 corpid = pid;
175 return 0;
176 }
177
178 int
179 attachcore(Fhdr *hdr)
180 {
181 unattach();
182 if(hdr == nil)
183 return 0;
184 if(mapfile(hdr, 0, cormap, &correg) < 0){
185 fprint(2, "attachcore %s: %r\n", hdr->filename);
186 return -1;
187 }
188 thecorhdr = hdr;
189 corhdr = hdr;
190 corfil = hdr->filename;
191 return 0;
192 }
193
194 int
195 attachdynamic(int verbose)
196 {
197 extern void elfdl386mapdl(int);
198
199 if(mach && mach->type == M386 && symhdr && symhdr->elf)
200 elfdl386mapdl(verbose);
201 return 0;
202 }