]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
de355530 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
de355530 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
de355530 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved. | |
23 | * | |
24 | * File: bsd/kern/kern_core.c | |
25 | * | |
26 | * This file contains machine independent code for performing core dumps. | |
27 | * | |
28 | * HISTORY | |
29 | * 16-Feb-91 Mike DeMoney (mike@next.com) | |
30 | * Massaged into MI form from m68k/core.c. | |
31 | */ | |
32 | ||
33 | #include <mach/vm_param.h> | |
34 | #include <mach/thread_status.h> | |
35 | ||
36 | #include <sys/param.h> | |
37 | #include <sys/systm.h> | |
38 | #include <sys/signalvar.h> | |
39 | #include <sys/resourcevar.h> | |
40 | #include <sys/namei.h> | |
41 | #include <sys/vnode.h> | |
42 | #include <sys/proc.h> | |
43 | #include <sys/timeb.h> | |
44 | #include <sys/times.h> | |
45 | #include <sys/buf.h> | |
46 | #include <sys/acct.h> | |
47 | #include <sys/file.h> | |
48 | #include <sys/uio.h> | |
49 | #include <sys/kernel.h> | |
50 | #include <sys/stat.h> | |
51 | ||
52 | #include <mach-o/loader.h> | |
53 | #include <mach/vm_region.h> | |
765c9de3 | 54 | #include <mach/vm_statistics.h> |
1c79356b A |
55 | |
56 | #include <vm/vm_kern.h> | |
57 | ||
58 | typedef struct { | |
59 | int flavor; /* the number for this flavor */ | |
60 | int count; /* count of ints in this flavor */ | |
61 | } mythread_state_flavor_t; | |
62 | ||
63 | #if defined (__ppc__) | |
64 | ||
65 | mythread_state_flavor_t thread_flavor_array[]={ | |
66 | {PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT}, | |
67 | {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT}, | |
de355530 | 68 | {PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT} |
1c79356b | 69 | }; |
de355530 | 70 | int mynum_flavors=3; |
1c79356b A |
71 | #elif defined (__i386__) |
72 | mythread_state_flavor_t thread_flavor_array [] = { | |
73 | {i386_THREAD_STATE, i386_THREAD_STATE_COUNT}, | |
74 | {i386_THREAD_FPSTATE, i386_THREAD_FPSTATE_COUNT}, | |
75 | {i386_THREAD_EXCEPTSTATE, i386_THREAD_EXCEPTSTATE_COUNT}, | |
76 | {i386_THREAD_CTHREADSTATE, i386_THREAD_CTHREADSTATE_COUNT}, | |
77 | {i386_NEW_THREAD_STATE, i386_NEW_THREAD_STATE_COUNT}, | |
78 | {i386_FLOAT_STATE, i386_FLOAT_STATE_COUNT}, | |
79 | {i386_ISA_PORT_MAP_STATE, i386_ISA_PORT_MAP_STATE_COUNT}, | |
80 | {i386_V86_ASSIST_STATE, i386_V86_ASSIST_STATE_COUNT}, | |
81 | {THREAD_SYSCALL_STATE, i386_THREAD_SYSCALL_STATE_COUNT} | |
82 | }; | |
83 | int mynum_flavors=9; | |
84 | ||
85 | #else | |
86 | #error architecture not supported | |
87 | #endif | |
88 | ||
89 | ||
90 | typedef struct { | |
91 | vm_offset_t header; | |
92 | int hoffset; | |
93 | mythread_state_flavor_t *flavors; | |
94 | int tstate_size; | |
95 | } tir_t; | |
96 | ||
97 | collectth_state(thread_act_t th_act, tir_t *t) | |
98 | { | |
99 | vm_offset_t header; | |
100 | int hoffset, i ; | |
101 | mythread_state_flavor_t *flavors; | |
102 | struct thread_command *tc; | |
103 | /* | |
104 | * Fill in thread command structure. | |
105 | */ | |
106 | header = t->header; | |
107 | hoffset = t->hoffset; | |
108 | flavors = t->flavors; | |
109 | ||
110 | tc = (struct thread_command *) (header + hoffset); | |
111 | tc->cmd = LC_THREAD; | |
112 | tc->cmdsize = sizeof(struct thread_command) | |
113 | + t->tstate_size; | |
114 | hoffset += sizeof(struct thread_command); | |
115 | /* | |
116 | * Follow with a struct thread_state_flavor and | |
117 | * the appropriate thread state struct for each | |
118 | * thread state flavor. | |
119 | */ | |
120 | for (i = 0; i < mynum_flavors; i++) { | |
121 | *(mythread_state_flavor_t *)(header+hoffset) = | |
122 | flavors[i]; | |
123 | hoffset += sizeof(mythread_state_flavor_t); | |
124 | thread_getstatus(th_act, flavors[i].flavor, | |
125 | (thread_state_t *)(header+hoffset), | |
126 | &flavors[i].count); | |
127 | hoffset += flavors[i].count*sizeof(int); | |
128 | } | |
129 | ||
130 | t->hoffset = hoffset; | |
131 | } | |
132 | /* | |
133 | * Create a core image on the file "core". | |
134 | */ | |
135 | #define MAX_TSTATE_FLAVORS 10 | |
136 | int | |
137 | coredump(p) | |
138 | register struct proc *p; | |
139 | { | |
140 | int error=0; | |
141 | register struct pcred *pcred = p->p_cred; | |
142 | register struct ucred *cred = pcred->pc_ucred; | |
143 | struct nameidata nd; | |
144 | struct vattr vattr; | |
145 | vm_map_t map; | |
146 | int thread_count, segment_count; | |
147 | int command_size, header_size, tstate_size; | |
148 | int hoffset, foffset, vmoffset; | |
149 | vm_offset_t header; | |
150 | struct machine_slot *ms; | |
151 | struct mach_header *mh; | |
152 | struct segment_command *sc; | |
153 | struct thread_command *tc; | |
154 | vm_size_t size; | |
155 | vm_prot_t prot; | |
156 | vm_prot_t maxprot; | |
157 | vm_inherit_t inherit; | |
158 | vm_offset_t offset; | |
159 | int error1; | |
160 | task_t task; | |
161 | char core_name[MAXCOMLEN+6]; | |
162 | mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS]; | |
163 | vm_size_t nflavors,mapsize; | |
164 | int i; | |
165 | int nesting_depth = 0; | |
166 | kern_return_t kret; | |
167 | struct vm_region_submap_info_64 vbr; | |
168 | int vbrcount=0; | |
169 | tir_t tir1; | |
170 | struct vnode * vp; | |
9bccf70c | 171 | extern boolean_t coredumpok(vm_map_t map, vm_offset_t va); /* temp fix */ |
1c79356b A |
172 | |
173 | if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid) | |
174 | return (EFAULT); | |
175 | ||
176 | task = current_task(); | |
177 | map = current_map(); | |
178 | mapsize = get_vmmap_size(map); | |
179 | ||
180 | if (mapsize >= p->p_rlimit[RLIMIT_CORE].rlim_cur) | |
181 | return (EFAULT); | |
182 | (void) task_suspend(task); | |
183 | ||
1c79356b A |
184 | sprintf(core_name, "/cores/core.%d", p->p_pid); |
185 | NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, core_name, p); | |
186 | if(error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR )) | |
187 | return (error); | |
188 | vp = nd.ni_vp; | |
189 | ||
190 | /* Don't dump to non-regular files or files with links. */ | |
191 | if (vp->v_type != VREG || | |
192 | VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) { | |
193 | error = EFAULT; | |
194 | goto out; | |
195 | } | |
196 | ||
197 | VATTR_NULL(&vattr); | |
198 | vattr.va_size = 0; | |
199 | VOP_LEASE(vp, p, cred, LEASE_WRITE); | |
200 | VOP_SETATTR(vp, &vattr, cred, p); | |
201 | p->p_acflag |= ACORE; | |
202 | ||
203 | /* | |
204 | * If the task is modified while dumping the file | |
205 | * (e.g., changes in threads or VM, the resulting | |
206 | * file will not necessarily be correct. | |
207 | */ | |
208 | ||
209 | thread_count = get_task_numacts(task); | |
210 | segment_count = get_vmmap_entries(map); /* XXX */ | |
211 | /* | |
212 | * nflavors here is really the number of ints in flavors | |
213 | * to meet the thread_getstatus() calling convention | |
214 | */ | |
215 | #if 0 | |
216 | nflavors = sizeof(flavors)/sizeof(int); | |
217 | if (thread_getstatus(current_thread(), THREAD_STATE_FLAVOR_LIST, | |
218 | (thread_state_t)(flavors), | |
219 | &nflavors) != KERN_SUCCESS) | |
220 | panic("core flavor list"); | |
221 | /* now convert to number of flavors */ | |
222 | nflavors /= sizeof(mythread_state_flavor_t)/sizeof(int); | |
223 | #else | |
224 | nflavors = mynum_flavors; | |
225 | bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array)); | |
226 | #endif | |
227 | tstate_size = 0; | |
228 | for (i = 0; i < nflavors; i++) | |
229 | tstate_size += sizeof(mythread_state_flavor_t) + | |
230 | (flavors[i].count * sizeof(int)); | |
231 | ||
232 | command_size = segment_count*sizeof(struct segment_command) + | |
233 | thread_count*sizeof(struct thread_command) + | |
234 | tstate_size*thread_count; | |
235 | ||
236 | header_size = command_size + sizeof(struct mach_header); | |
237 | ||
238 | (void) kmem_alloc_wired(kernel_map, | |
239 | (vm_offset_t *)&header, | |
240 | (vm_size_t)header_size); | |
241 | ||
242 | /* | |
243 | * Set up Mach-O header. | |
244 | */ | |
245 | mh = (struct mach_header *) header; | |
246 | ms = &machine_slot[cpu_number()]; | |
247 | mh->magic = MH_MAGIC; | |
248 | mh->cputype = ms->cpu_type; | |
249 | mh->cpusubtype = ms->cpu_subtype; | |
250 | mh->filetype = MH_CORE; | |
251 | mh->ncmds = segment_count + thread_count; | |
252 | mh->sizeofcmds = command_size; | |
253 | ||
254 | hoffset = sizeof(struct mach_header); /* offset into header */ | |
de355530 | 255 | foffset = round_page(header_size); /* offset into file */ |
1c79356b A |
256 | vmoffset = VM_MIN_ADDRESS; /* offset into VM */ |
257 | /* We use to check for an error, here, now we try and get | |
258 | * as much as we can | |
259 | */ | |
260 | while (segment_count > 0){ | |
261 | /* | |
262 | * Get region information for next region. | |
263 | */ | |
264 | ||
265 | while (1) { | |
266 | vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64; | |
267 | if((kret = vm_region_recurse_64(map, | |
268 | &vmoffset, &size, &nesting_depth, | |
269 | &vbr, &vbrcount)) != KERN_SUCCESS) { | |
270 | break; | |
271 | } | |
272 | if(vbr.is_submap) { | |
273 | nesting_depth++; | |
274 | continue; | |
275 | } else { | |
276 | break; | |
277 | } | |
278 | } | |
279 | if(kret != KERN_SUCCESS) | |
280 | break; | |
281 | ||
282 | prot = vbr.protection; | |
283 | maxprot = vbr.max_protection; | |
284 | inherit = vbr.inheritance; | |
285 | /* | |
286 | * Fill in segment command structure. | |
287 | */ | |
288 | sc = (struct segment_command *) (header + hoffset); | |
289 | sc->cmd = LC_SEGMENT; | |
290 | sc->cmdsize = sizeof(struct segment_command); | |
291 | /* segment name is zerod by kmem_alloc */ | |
9bccf70c | 292 | sc->segname[0] = 0; |
1c79356b A |
293 | sc->vmaddr = vmoffset; |
294 | sc->vmsize = size; | |
295 | sc->fileoff = foffset; | |
296 | sc->filesize = size; | |
297 | sc->maxprot = maxprot; | |
298 | sc->initprot = prot; | |
299 | sc->nsects = 0; | |
300 | ||
301 | /* | |
302 | * Write segment out. Try as hard as possible to | |
303 | * get read access to the data. | |
304 | */ | |
305 | if ((prot & VM_PROT_READ) == 0) { | |
306 | vm_protect(map, vmoffset, size, FALSE, | |
307 | prot|VM_PROT_READ); | |
308 | } | |
309 | /* | |
310 | * Only actually perform write if we can read. | |
311 | * Note: if we can't read, then we end up with | |
312 | * a hole in the file. | |
313 | */ | |
9bccf70c | 314 | if ((maxprot & VM_PROT_READ) == VM_PROT_READ && vbr.user_tag != VM_MEMORY_IOKIT && coredumpok(map,vmoffset)) { |
1c79356b A |
315 | error = vn_rdwr(UIO_WRITE, vp, (caddr_t)vmoffset, size, foffset, |
316 | UIO_USERSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); | |
317 | } | |
318 | ||
319 | hoffset += sizeof(struct segment_command); | |
320 | foffset += size; | |
321 | vmoffset += size; | |
322 | segment_count--; | |
323 | } | |
324 | ||
325 | #if 0 /* [ */ | |
326 | task_lock(task); | |
327 | thread = (thread_t) queue_first(&task->thread_list); | |
328 | while (thread_count > 0) { | |
329 | /* | |
330 | * Fill in thread command structure. | |
331 | */ | |
332 | tc = (struct thread_command *) (header + hoffset); | |
333 | tc->cmd = LC_THREAD; | |
334 | tc->cmdsize = sizeof(struct thread_command) | |
335 | + tstate_size; | |
336 | hoffset += sizeof(struct thread_command); | |
337 | /* | |
338 | * Follow with a struct thread_state_flavor and | |
339 | * the appropriate thread state struct for each | |
340 | * thread state flavor. | |
341 | */ | |
342 | for (i = 0; i < nflavors; i++) { | |
343 | *(mythread_state_flavor_t *)(header+hoffset) = | |
344 | flavors[i]; | |
345 | hoffset += sizeof(mythread_state_flavor_t); | |
346 | thread_getstatus(thread, flavors[i].flavor, | |
347 | (thread_state_t *)(header+hoffset), | |
348 | &flavors[i].count); | |
349 | hoffset += flavors[i].count*sizeof(int); | |
350 | } | |
351 | thread = (thread_t) queue_next(&thread->thread_list); | |
352 | thread_count--; | |
353 | } | |
354 | task_unlock(task); | |
355 | #else /* /* 0 ][ */ | |
356 | tir1.header = header; | |
357 | tir1.hoffset = hoffset; | |
358 | tir1.flavors = flavors; | |
359 | tir1.tstate_size = tstate_size; | |
360 | task_act_iterate_wth_args(task, collectth_state,&tir1); | |
361 | ||
362 | #endif /* 0 ] */ | |
363 | /* | |
364 | * Write out the Mach header at the beginning of the | |
365 | * file. | |
366 | */ | |
367 | error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0, | |
368 | UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); | |
369 | kmem_free(kernel_map, header, header_size); | |
370 | out: | |
371 | VOP_UNLOCK(vp, 0, p); | |
372 | error1 = vn_close(vp, FWRITE, cred, p); | |
373 | if (error == 0) | |
374 | error = error1; | |
375 | } |