]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ff6e181a A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
1c79356b | 12 | * |
ff6e181a A |
13 | * The Original Code and all software distributed under the License are |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ff6e181a A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
1c79356b A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved. | |
24 | * | |
25 | * File: bsd/kern/kern_core.c | |
26 | * | |
27 | * This file contains machine independent code for performing core dumps. | |
28 | * | |
1c79356b A |
29 | */ |
30 | ||
31 | #include <mach/vm_param.h> | |
32 | #include <mach/thread_status.h> | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/systm.h> | |
36 | #include <sys/signalvar.h> | |
37 | #include <sys/resourcevar.h> | |
38 | #include <sys/namei.h> | |
91447636 A |
39 | #include <sys/vnode_internal.h> |
40 | #include <sys/proc_internal.h> | |
41 | #include <sys/kauth.h> | |
1c79356b A |
42 | #include <sys/timeb.h> |
43 | #include <sys/times.h> | |
1c79356b | 44 | #include <sys/acct.h> |
91447636 | 45 | #include <sys/file_internal.h> |
1c79356b A |
46 | #include <sys/uio.h> |
47 | #include <sys/kernel.h> | |
48 | #include <sys/stat.h> | |
49 | ||
50 | #include <mach-o/loader.h> | |
51 | #include <mach/vm_region.h> | |
765c9de3 | 52 | #include <mach/vm_statistics.h> |
1c79356b A |
53 | |
54 | #include <vm/vm_kern.h> | |
91447636 A |
55 | #include <vm/vm_protos.h> /* last */ |
56 | #include <vm/vm_map.h> /* current_map() */ | |
57 | #include <mach/mach_vm.h> /* mach_vm_region_recurse() */ | |
58 | #include <mach/task.h> /* task_suspend() */ | |
59 | #include <kern/task.h> /* get_task_numacts() */ | |
1c79356b A |
60 | |
61 | typedef struct { | |
62 | int flavor; /* the number for this flavor */ | |
63 | int count; /* count of ints in this flavor */ | |
64 | } mythread_state_flavor_t; | |
65 | ||
66 | #if defined (__ppc__) | |
67 | ||
68 | mythread_state_flavor_t thread_flavor_array[]={ | |
69 | {PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT}, | |
70 | {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT}, | |
55e303ae A |
71 | {PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT}, |
72 | {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT} | |
1c79356b | 73 | }; |
55e303ae | 74 | int mynum_flavors=4; |
1c79356b A |
75 | #elif defined (__i386__) |
76 | mythread_state_flavor_t thread_flavor_array [] = { | |
77 | {i386_THREAD_STATE, i386_THREAD_STATE_COUNT}, | |
78 | {i386_THREAD_FPSTATE, i386_THREAD_FPSTATE_COUNT}, | |
79 | {i386_THREAD_EXCEPTSTATE, i386_THREAD_EXCEPTSTATE_COUNT}, | |
80 | {i386_THREAD_CTHREADSTATE, i386_THREAD_CTHREADSTATE_COUNT}, | |
81 | {i386_NEW_THREAD_STATE, i386_NEW_THREAD_STATE_COUNT}, | |
82 | {i386_FLOAT_STATE, i386_FLOAT_STATE_COUNT}, | |
83 | {i386_ISA_PORT_MAP_STATE, i386_ISA_PORT_MAP_STATE_COUNT}, | |
84 | {i386_V86_ASSIST_STATE, i386_V86_ASSIST_STATE_COUNT}, | |
85 | {THREAD_SYSCALL_STATE, i386_THREAD_SYSCALL_STATE_COUNT} | |
86 | }; | |
87 | int mynum_flavors=9; | |
88 | ||
89 | #else | |
90 | #error architecture not supported | |
91 | #endif | |
92 | ||
93 | ||
94 | typedef struct { | |
95 | vm_offset_t header; | |
96 | int hoffset; | |
97 | mythread_state_flavor_t *flavors; | |
98 | int tstate_size; | |
99 | } tir_t; | |
100 | ||
e5568f75 | 101 | /* XXX should be static */ |
91447636 | 102 | void collectth_state(thread_t th_act, void *tirp); |
e5568f75 A |
103 | |
104 | /* XXX not in a Mach header anywhere */ | |
91447636 | 105 | kern_return_t thread_getstatus(register thread_t act, int flavor, |
e5568f75 | 106 | thread_state_t tstate, mach_msg_type_number_t *count); |
91447636 | 107 | void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *); |
e5568f75 A |
108 | |
109 | ||
91447636 A |
110 | __private_extern__ int do_coredump = 1; /* default: dump cores */ |
111 | __private_extern__ int sugid_coredump = 0; /* default: but not SGUID binaries */ | |
e5568f75 | 112 | |
55e303ae | 113 | void |
91447636 | 114 | collectth_state(thread_t th_act, void *tirp) |
1c79356b A |
115 | { |
116 | vm_offset_t header; | |
117 | int hoffset, i ; | |
118 | mythread_state_flavor_t *flavors; | |
119 | struct thread_command *tc; | |
91447636 A |
120 | tir_t *t = (tir_t *)tirp; |
121 | ||
1c79356b A |
122 | /* |
123 | * Fill in thread command structure. | |
124 | */ | |
125 | header = t->header; | |
126 | hoffset = t->hoffset; | |
127 | flavors = t->flavors; | |
128 | ||
129 | tc = (struct thread_command *) (header + hoffset); | |
130 | tc->cmd = LC_THREAD; | |
131 | tc->cmdsize = sizeof(struct thread_command) | |
132 | + t->tstate_size; | |
133 | hoffset += sizeof(struct thread_command); | |
134 | /* | |
135 | * Follow with a struct thread_state_flavor and | |
136 | * the appropriate thread state struct for each | |
137 | * thread state flavor. | |
138 | */ | |
139 | for (i = 0; i < mynum_flavors; i++) { | |
140 | *(mythread_state_flavor_t *)(header+hoffset) = | |
141 | flavors[i]; | |
142 | hoffset += sizeof(mythread_state_flavor_t); | |
143 | thread_getstatus(th_act, flavors[i].flavor, | |
e5568f75 | 144 | (thread_state_t)(header+hoffset), |
1c79356b A |
145 | &flavors[i].count); |
146 | hoffset += flavors[i].count*sizeof(int); | |
147 | } | |
148 | ||
149 | t->hoffset = hoffset; | |
150 | } | |
e5568f75 | 151 | |
1c79356b A |
152 | /* |
153 | * Create a core image on the file "core". | |
154 | */ | |
155 | #define MAX_TSTATE_FLAVORS 10 | |
156 | int | |
e5568f75 | 157 | coredump(struct proc *p) |
1c79356b A |
158 | { |
159 | int error=0; | |
91447636 A |
160 | kauth_cred_t cred = kauth_cred_get(); |
161 | struct vnode_attr va; | |
162 | struct vfs_context context; | |
1c79356b A |
163 | vm_map_t map; |
164 | int thread_count, segment_count; | |
165 | int command_size, header_size, tstate_size; | |
91447636 A |
166 | int hoffset; |
167 | off_t foffset; | |
168 | vm_map_offset_t vmoffset; | |
1c79356b | 169 | vm_offset_t header; |
91447636 | 170 | vm_map_size_t vmsize; |
1c79356b A |
171 | vm_prot_t prot; |
172 | vm_prot_t maxprot; | |
173 | vm_inherit_t inherit; | |
1c79356b A |
174 | int error1; |
175 | task_t task; | |
176 | char core_name[MAXCOMLEN+6]; | |
e5568f75 | 177 | char *name; |
1c79356b | 178 | mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS]; |
e5568f75 | 179 | vm_size_t mapsize; |
1c79356b A |
180 | int i; |
181 | int nesting_depth = 0; | |
182 | kern_return_t kret; | |
183 | struct vm_region_submap_info_64 vbr; | |
184 | int vbrcount=0; | |
185 | tir_t tir1; | |
186 | struct vnode * vp; | |
91447636 A |
187 | struct mach_header *mh; |
188 | struct mach_header_64 *mh64; | |
189 | int is_64 = 0; | |
190 | size_t mach_header_sz = sizeof(struct mach_header); | |
191 | size_t segment_command_sz = sizeof(struct segment_command); | |
1c79356b | 192 | |
e5568f75 A |
193 | if (do_coredump == 0 || /* Not dumping at all */ |
194 | ( (sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */ | |
91447636 A |
195 | ( (cred->cr_svuid != cred->cr_ruid) || |
196 | (cred->cr_svgid != cred->cr_rgid)))) { | |
e5568f75 | 197 | |
1c79356b | 198 | return (EFAULT); |
e5568f75 | 199 | } |
1c79356b | 200 | |
91447636 A |
201 | if (IS_64BIT_PROCESS(p)) { |
202 | is_64 = 1; | |
203 | mach_header_sz = sizeof(struct mach_header_64); | |
204 | segment_command_sz = sizeof(struct segment_command_64); | |
205 | } | |
206 | ||
1c79356b A |
207 | task = current_task(); |
208 | map = current_map(); | |
209 | mapsize = get_vmmap_size(map); | |
210 | ||
211 | if (mapsize >= p->p_rlimit[RLIMIT_CORE].rlim_cur) | |
212 | return (EFAULT); | |
213 | (void) task_suspend(task); | |
214 | ||
e5568f75 | 215 | /* create name according to sysctl'able format string */ |
91447636 | 216 | name = proc_core_name(p->p_comm, kauth_cred_getuid(cred), p->p_pid); |
e5568f75 A |
217 | |
218 | /* if name creation fails, fall back to historical behaviour... */ | |
219 | if (name == NULL) { | |
91447636 | 220 | sprintf(core_name, "/cores/core.%d", p->p_pid); |
e5568f75 A |
221 | name = core_name; |
222 | } | |
91447636 A |
223 | context.vc_proc = p; |
224 | context.vc_ucred = cred; | |
e5568f75 | 225 | |
3a60a9f5 | 226 | if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, &context))) |
91447636 A |
227 | return (error); |
228 | ||
229 | VATTR_INIT(&va); | |
230 | VATTR_WANTED(&va, va_nlink); | |
1c79356b A |
231 | /* Don't dump to non-regular files or files with links. */ |
232 | if (vp->v_type != VREG || | |
91447636 | 233 | vnode_getattr(vp, &va, &context) || va.va_nlink != 1) { |
1c79356b A |
234 | error = EFAULT; |
235 | goto out; | |
236 | } | |
237 | ||
91447636 A |
238 | VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */ |
239 | VATTR_SET(&va, va_data_size, 0); | |
240 | vnode_setattr(vp, &va, &context); | |
1c79356b A |
241 | p->p_acflag |= ACORE; |
242 | ||
243 | /* | |
244 | * If the task is modified while dumping the file | |
245 | * (e.g., changes in threads or VM, the resulting | |
246 | * file will not necessarily be correct. | |
247 | */ | |
248 | ||
249 | thread_count = get_task_numacts(task); | |
250 | segment_count = get_vmmap_entries(map); /* XXX */ | |
1c79356b | 251 | bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array)); |
1c79356b | 252 | tstate_size = 0; |
e5568f75 | 253 | for (i = 0; i < mynum_flavors; i++) |
1c79356b A |
254 | tstate_size += sizeof(mythread_state_flavor_t) + |
255 | (flavors[i].count * sizeof(int)); | |
256 | ||
91447636 | 257 | command_size = segment_count * segment_command_sz + |
1c79356b A |
258 | thread_count*sizeof(struct thread_command) + |
259 | tstate_size*thread_count; | |
260 | ||
91447636 | 261 | header_size = command_size + mach_header_sz; |
1c79356b | 262 | |
91447636 | 263 | (void) kmem_alloc(kernel_map, |
1c79356b A |
264 | (vm_offset_t *)&header, |
265 | (vm_size_t)header_size); | |
266 | ||
267 | /* | |
268 | * Set up Mach-O header. | |
269 | */ | |
91447636 A |
270 | if (is_64) { |
271 | mh64 = (struct mach_header_64 *)header; | |
272 | mh64->magic = MH_MAGIC_64; | |
273 | mh64->cputype = cpu_type(); | |
274 | mh64->cpusubtype = cpu_subtype(); | |
275 | mh64->filetype = MH_CORE; | |
276 | mh64->ncmds = segment_count + thread_count; | |
277 | mh64->sizeofcmds = command_size; | |
278 | mh64->reserved = 0; /* 8 byte alignment */ | |
279 | } else { | |
280 | mh = (struct mach_header *)header; | |
281 | mh->magic = MH_MAGIC; | |
282 | mh->cputype = cpu_type(); | |
283 | mh->cpusubtype = cpu_subtype(); | |
284 | mh->filetype = MH_CORE; | |
285 | mh->ncmds = segment_count + thread_count; | |
286 | mh->sizeofcmds = command_size; | |
287 | } | |
288 | ||
289 | hoffset = mach_header_sz; /* offset into header */ | |
290 | foffset = round_page(header_size); /* offset into file */ | |
291 | vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */ | |
292 | ||
55e303ae A |
293 | /* |
294 | * We use to check for an error, here, now we try and get | |
1c79356b A |
295 | * as much as we can |
296 | */ | |
91447636 A |
297 | while (segment_count > 0) { |
298 | struct segment_command *sc; | |
299 | struct segment_command_64 *sc64; | |
300 | ||
1c79356b A |
301 | /* |
302 | * Get region information for next region. | |
303 | */ | |
304 | ||
305 | while (1) { | |
306 | vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64; | |
91447636 A |
307 | if((kret = mach_vm_region_recurse(map, |
308 | &vmoffset, &vmsize, &nesting_depth, | |
309 | (vm_region_recurse_info_t)&vbr, | |
310 | &vbrcount)) != KERN_SUCCESS) { | |
311 | break; | |
312 | } | |
313 | /* | |
314 | * If we get a valid mapping back, but we're dumping | |
315 | * a 32 bit process, and it's over the allowable | |
316 | * address space of a 32 bit process, it's the same | |
317 | * as if mach_vm_region_recurse() failed. | |
318 | */ | |
319 | if (!(is_64) && | |
320 | (vmoffset + vmsize > VM_MAX_ADDRESS)) { | |
321 | kret = KERN_INVALID_ADDRESS; | |
1c79356b A |
322 | break; |
323 | } | |
324 | if(vbr.is_submap) { | |
325 | nesting_depth++; | |
326 | continue; | |
327 | } else { | |
328 | break; | |
329 | } | |
330 | } | |
331 | if(kret != KERN_SUCCESS) | |
332 | break; | |
333 | ||
334 | prot = vbr.protection; | |
335 | maxprot = vbr.max_protection; | |
336 | inherit = vbr.inheritance; | |
337 | /* | |
338 | * Fill in segment command structure. | |
339 | */ | |
91447636 A |
340 | if (is_64) { |
341 | sc64 = (struct segment_command_64 *)(header + hoffset); | |
342 | sc64->cmd = LC_SEGMENT_64; | |
343 | sc64->cmdsize = sizeof(struct segment_command_64); | |
344 | /* segment name is zeroed by kmem_alloc */ | |
345 | sc64->segname[0] = 0; | |
346 | sc64->vmaddr = vmoffset; | |
347 | sc64->vmsize = vmsize; | |
348 | sc64->fileoff = foffset; | |
349 | sc64->filesize = vmsize; | |
350 | sc64->maxprot = maxprot; | |
351 | sc64->initprot = prot; | |
352 | sc64->nsects = 0; | |
353 | } else { | |
354 | sc = (struct segment_command *) (header + hoffset); | |
355 | sc->cmd = LC_SEGMENT; | |
356 | sc->cmdsize = sizeof(struct segment_command); | |
357 | /* segment name is zeroed by kmem_alloc */ | |
358 | sc->segname[0] = 0; | |
359 | sc->vmaddr = CAST_DOWN(vm_offset_t,vmoffset); | |
360 | sc->vmsize = CAST_DOWN(vm_size_t,vmsize); | |
361 | sc->fileoff = CAST_DOWN(uint32_t,foffset); | |
362 | sc->filesize = CAST_DOWN(uint32_t,vmsize); | |
363 | sc->maxprot = maxprot; | |
364 | sc->initprot = prot; | |
365 | sc->nsects = 0; | |
366 | } | |
1c79356b A |
367 | |
368 | /* | |
369 | * Write segment out. Try as hard as possible to | |
370 | * get read access to the data. | |
371 | */ | |
372 | if ((prot & VM_PROT_READ) == 0) { | |
91447636 A |
373 | mach_vm_protect(map, vmoffset, vmsize, FALSE, |
374 | prot|VM_PROT_READ); | |
1c79356b A |
375 | } |
376 | /* | |
377 | * Only actually perform write if we can read. | |
378 | * Note: if we can't read, then we end up with | |
379 | * a hole in the file. | |
380 | */ | |
55e303ae A |
381 | if ((maxprot & VM_PROT_READ) == VM_PROT_READ |
382 | && vbr.user_tag != VM_MEMORY_IOKIT | |
383 | && coredumpok(map,vmoffset)) { | |
91447636 A |
384 | vm_map_size_t tmp_vmsize = vmsize; |
385 | off_t xfer_foffset = foffset; | |
386 | ||
387 | //LP64todo - works around vn_rdwr_64() 2G limit | |
388 | while (tmp_vmsize > 0) { | |
389 | vm_map_size_t xfer_vmsize = tmp_vmsize; | |
390 | if (xfer_vmsize > INT_MAX) | |
391 | xfer_vmsize = INT_MAX; | |
392 | error = vn_rdwr_64(UIO_WRITE, vp, | |
393 | vmoffset, xfer_vmsize, xfer_foffset, | |
394 | (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32), | |
395 | IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); | |
396 | tmp_vmsize -= xfer_vmsize; | |
397 | xfer_foffset += xfer_vmsize; | |
398 | } | |
1c79356b A |
399 | } |
400 | ||
91447636 A |
401 | hoffset += segment_command_sz; |
402 | foffset += vmsize; | |
403 | vmoffset += vmsize; | |
1c79356b A |
404 | segment_count--; |
405 | } | |
406 | ||
91447636 A |
407 | /* |
408 | * If there are remaining segments which have not been written | |
409 | * out because break in the loop above, then they were not counted | |
410 | * because they exceed the real address space of the executable | |
411 | * type: remove them from the header's count. This is OK, since | |
412 | * we are allowed to have a sparse area following the segments. | |
413 | */ | |
414 | if (is_64) { | |
415 | mh64->ncmds -= segment_count; | |
416 | } else { | |
417 | mh->ncmds -= segment_count; | |
418 | } | |
419 | ||
1c79356b A |
420 | tir1.header = header; |
421 | tir1.hoffset = hoffset; | |
422 | tir1.flavors = flavors; | |
423 | tir1.tstate_size = tstate_size; | |
424 | task_act_iterate_wth_args(task, collectth_state,&tir1); | |
425 | ||
1c79356b A |
426 | /* |
427 | * Write out the Mach header at the beginning of the | |
91447636 | 428 | * file. OK to use a 32 bit write for this. |
1c79356b A |
429 | */ |
430 | error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0, | |
91447636 | 431 | UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); |
1c79356b A |
432 | kmem_free(kernel_map, header, header_size); |
433 | out: | |
91447636 | 434 | error1 = vnode_close(vp, FWRITE, &context); |
1c79356b A |
435 | if (error == 0) |
436 | error = error1; | |
91447636 | 437 | |
55e303ae | 438 | return (error); |
1c79356b | 439 | } |