2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
30 * File: bsd/kern/kern_core.c
32 * This file contains machine independent code for performing core dumps.
36 #include <mach/vm_param.h>
37 #include <mach/thread_status.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/signalvar.h>
42 #include <sys/resourcevar.h>
43 #include <sys/namei.h>
44 #include <sys/vnode_internal.h>
45 #include <sys/proc_internal.h>
46 #include <sys/kauth.h>
47 #include <sys/timeb.h>
48 #include <sys/times.h>
50 #include <sys/file_internal.h>
52 #include <sys/kernel.h>
55 #include <mach-o/loader.h>
56 #include <mach/vm_region.h>
57 #include <mach/vm_statistics.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_protos.h> /* last */
61 #include <vm/vm_map.h> /* current_map() */
62 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
63 #include <mach/task.h> /* task_suspend() */
64 #include <kern/task.h> /* get_task_numacts() */
67 int flavor
; /* the number for this flavor */
68 mach_msg_type_number_t count
; /* count of ints in this flavor */
69 } mythread_state_flavor_t
;
73 mythread_state_flavor_t thread_flavor_array64
[]={
74 {PPC_THREAD_STATE64
, PPC_THREAD_STATE64_COUNT
},
75 {PPC_FLOAT_STATE
, PPC_FLOAT_STATE_COUNT
},
76 {PPC_EXCEPTION_STATE64
, PPC_EXCEPTION_STATE64_COUNT
},
77 {PPC_VECTOR_STATE
, PPC_VECTOR_STATE_COUNT
}
81 mythread_state_flavor_t thread_flavor_array
[]={
82 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
83 {PPC_FLOAT_STATE
, PPC_FLOAT_STATE_COUNT
},
84 {PPC_EXCEPTION_STATE
, PPC_EXCEPTION_STATE_COUNT
},
85 {PPC_VECTOR_STATE
, PPC_VECTOR_STATE_COUNT
}
88 #elif defined (__i386__)
89 mythread_state_flavor_t thread_flavor_array
[] = {
90 {x86_THREAD_STATE
, x86_THREAD_STATE_COUNT
},
91 {x86_FLOAT_STATE
, x86_FLOAT_STATE_COUNT
},
92 {x86_EXCEPTION_STATE
, x86_EXCEPTION_STATE_COUNT
},
96 #error architecture not supported
103 mythread_state_flavor_t
*flavors
;
108 /* XXX should be static */
109 void collectth_state(thread_t th_act
, void *tirp
);
111 /* XXX not in a Mach header anywhere */
112 kern_return_t
thread_getstatus(register thread_t act
, int flavor
,
113 thread_state_t tstate
, mach_msg_type_number_t
*count
);
114 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
117 __private_extern__
int do_coredump
= 1; /* default: dump cores */
118 __private_extern__
int sugid_coredump
= 0; /* default: but not SGUID binaries */
121 collectth_state(thread_t th_act
, void *tirp
)
125 mythread_state_flavor_t
*flavors
;
126 struct thread_command
*tc
;
127 tir_t
*t
= (tir_t
*)tirp
;
130 * Fill in thread command structure.
133 hoffset
= t
->hoffset
;
134 flavors
= t
->flavors
;
136 tc
= (struct thread_command
*) (header
+ hoffset
);
138 tc
->cmdsize
= sizeof(struct thread_command
)
140 hoffset
+= sizeof(struct thread_command
);
142 * Follow with a struct thread_state_flavor and
143 * the appropriate thread state struct for each
144 * thread state flavor.
146 for (i
= 0; i
< t
->flavor_count
; i
++) {
147 *(mythread_state_flavor_t
*)(header
+hoffset
) =
149 hoffset
+= sizeof(mythread_state_flavor_t
);
150 thread_getstatus(th_act
, flavors
[i
].flavor
,
151 (thread_state_t
)(header
+hoffset
),
153 hoffset
+= flavors
[i
].count
*sizeof(int);
156 t
->hoffset
= hoffset
;
163 * Description: Create a core image on the file "core" for the process
166 * Parameters: core_proc Process to dump core [*]
171 * IMPORTANT: This function can only be called on the current process, due
172 * to assumptions below; see variable declaration section for
175 #define MAX_TSTATE_FLAVORS 10
177 coredump(proc_t core_proc
)
179 /* Begin assumptions that limit us to only the current process */
180 vfs_context_t ctx
= vfs_context_current();
181 vm_map_t map
= current_map();
182 task_t task
= current_task();
183 /* End assumptions */
184 kauth_cred_t cred
= vfs_context_ucred(ctx
);
186 struct vnode_attr va
;
187 int thread_count
, segment_count
;
188 int command_size
, header_size
, tstate_size
;
191 vm_map_offset_t vmoffset
;
193 vm_map_size_t vmsize
;
196 vm_inherit_t inherit
;
198 char stack_name
[MAXCOMLEN
+6];
199 char *alloced_name
= NULL
;
201 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
204 uint32_t nesting_depth
= 0;
206 struct vm_region_submap_info_64 vbr
;
207 mach_msg_type_number_t vbrcount
= 0;
210 struct mach_header
*mh
= NULL
; /* protected by is_64 */
211 struct mach_header_64
*mh64
= NULL
; /* protected by is_64 */
213 size_t mach_header_sz
= sizeof(struct mach_header
);
214 size_t segment_command_sz
= sizeof(struct segment_command
);
216 if (do_coredump
== 0 || /* Not dumping at all */
217 ( (sugid_coredump
== 0) && /* Not dumping SUID/SGID binaries */
218 ( (cred
->cr_svuid
!= cred
->cr_ruid
) ||
219 (cred
->cr_svgid
!= cred
->cr_rgid
)))) {
224 if (IS_64BIT_PROCESS(core_proc
)) {
226 mach_header_sz
= sizeof(struct mach_header_64
);
227 segment_command_sz
= sizeof(struct segment_command_64
);
230 mapsize
= get_vmmap_size(map
);
232 if (mapsize
>= core_proc
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)
234 (void) task_suspend(task
);
236 MALLOC(alloced_name
, char *, MAXPATHLEN
, M_TEMP
, M_NOWAIT
| M_ZERO
);
238 /* create name according to sysctl'able format string */
239 /* if name creation fails, fall back to historical behaviour... */
240 if (proc_core_name(core_proc
->p_comm
, kauth_cred_getuid(cred
),
241 core_proc
->p_pid
, alloced_name
, MAXPATHLEN
)) {
242 snprintf(stack_name
, sizeof(stack_name
),
243 "/cores/core.%d", core_proc
->p_pid
);
248 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), S_IRUSR
, VNODE_LOOKUP_NOFOLLOW
, &vp
, ctx
)))
252 VATTR_WANTED(&va
, va_nlink
);
253 /* Don't dump to non-regular files or files with links. */
254 if (vp
->v_type
!= VREG
||
255 vnode_getattr(vp
, &va
, ctx
) || va
.va_nlink
!= 1) {
260 VATTR_INIT(&va
); /* better to do it here than waste more stack in vnode_setsize */
261 VATTR_SET(&va
, va_data_size
, 0);
262 vnode_setattr(vp
, &va
, ctx
);
263 core_proc
->p_acflag
|= ACORE
;
266 * If the task is modified while dumping the file
267 * (e.g., changes in threads or VM, the resulting
268 * file will not necessarily be correct.
271 thread_count
= get_task_numacts(task
);
272 segment_count
= get_vmmap_entries(map
); /* XXX */
273 #if defined (__ppc__)
275 tir1
.flavor_count
= sizeof(thread_flavor_array64
)/sizeof(mythread_state_flavor_t
);
276 bcopy(thread_flavor_array64
, flavors
,sizeof(thread_flavor_array64
));
278 #endif /* __ppc __ */
279 tir1
.flavor_count
= sizeof(thread_flavor_array
)/sizeof(mythread_state_flavor_t
);
280 bcopy(thread_flavor_array
, flavors
,sizeof(thread_flavor_array
));
281 #if defined (__ppc__)
283 #endif /* __ppc __ */
285 for (i
= 0; i
< tir1
.flavor_count
; i
++)
286 tstate_size
+= sizeof(mythread_state_flavor_t
) +
287 (flavors
[i
].count
* sizeof(int));
288 command_size
= segment_count
* segment_command_sz
+
289 thread_count
*sizeof(struct thread_command
) +
290 tstate_size
*thread_count
;
292 header_size
= command_size
+ mach_header_sz
;
294 (void) kmem_alloc(kernel_map
,
295 (vm_offset_t
*)&header
,
296 (vm_size_t
)header_size
);
299 * Set up Mach-O header.
302 mh64
= (struct mach_header_64
*)header
;
303 mh64
->magic
= MH_MAGIC_64
;
304 mh64
->cputype
= cpu_type();
305 mh64
->cpusubtype
= cpu_subtype();
306 mh64
->filetype
= MH_CORE
;
307 mh64
->ncmds
= segment_count
+ thread_count
;
308 mh64
->sizeofcmds
= command_size
;
309 mh64
->reserved
= 0; /* 8 byte alignment */
311 mh
= (struct mach_header
*)header
;
312 mh
->magic
= MH_MAGIC
;
313 mh
->cputype
= cpu_type();
314 mh
->cpusubtype
= cpu_subtype();
315 mh
->filetype
= MH_CORE
;
316 mh
->ncmds
= segment_count
+ thread_count
;
317 mh
->sizeofcmds
= command_size
;
320 hoffset
= mach_header_sz
; /* offset into header */
321 foffset
= round_page(header_size
); /* offset into file */
322 vmoffset
= MACH_VM_MIN_ADDRESS
; /* offset into VM */
325 * We use to check for an error, here, now we try and get
328 while (segment_count
> 0) {
329 struct segment_command
*sc
;
330 struct segment_command_64
*sc64
;
333 * Get region information for next region.
337 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
338 if((kret
= mach_vm_region_recurse(map
,
339 &vmoffset
, &vmsize
, &nesting_depth
,
340 (vm_region_recurse_info_t
)&vbr
,
341 &vbrcount
)) != KERN_SUCCESS
) {
345 * If we get a valid mapping back, but we're dumping
346 * a 32 bit process, and it's over the allowable
347 * address space of a 32 bit process, it's the same
348 * as if mach_vm_region_recurse() failed.
351 (vmoffset
+ vmsize
> VM_MAX_ADDRESS
)) {
352 kret
= KERN_INVALID_ADDRESS
;
362 if(kret
!= KERN_SUCCESS
)
365 prot
= vbr
.protection
;
366 maxprot
= vbr
.max_protection
;
367 inherit
= vbr
.inheritance
;
369 * Fill in segment command structure.
372 sc64
= (struct segment_command_64
*)(header
+ hoffset
);
373 sc64
->cmd
= LC_SEGMENT_64
;
374 sc64
->cmdsize
= sizeof(struct segment_command_64
);
375 /* segment name is zeroed by kmem_alloc */
376 sc64
->segname
[0] = 0;
377 sc64
->vmaddr
= vmoffset
;
378 sc64
->vmsize
= vmsize
;
379 sc64
->fileoff
= foffset
;
380 sc64
->filesize
= vmsize
;
381 sc64
->maxprot
= maxprot
;
382 sc64
->initprot
= prot
;
385 sc
= (struct segment_command
*) (header
+ hoffset
);
386 sc
->cmd
= LC_SEGMENT
;
387 sc
->cmdsize
= sizeof(struct segment_command
);
388 /* segment name is zeroed by kmem_alloc */
390 sc
->vmaddr
= CAST_DOWN(vm_offset_t
,vmoffset
);
391 sc
->vmsize
= CAST_DOWN(vm_size_t
,vmsize
);
392 sc
->fileoff
= CAST_DOWN(uint32_t,foffset
);
393 sc
->filesize
= CAST_DOWN(uint32_t,vmsize
);
394 sc
->maxprot
= maxprot
;
400 * Write segment out. Try as hard as possible to
401 * get read access to the data.
403 if ((prot
& VM_PROT_READ
) == 0) {
404 mach_vm_protect(map
, vmoffset
, vmsize
, FALSE
,
408 * Only actually perform write if we can read.
409 * Note: if we can't read, then we end up with
410 * a hole in the file.
412 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
413 && vbr
.user_tag
!= VM_MEMORY_IOKIT
414 && coredumpok(map
,vmoffset
)) {
415 vm_map_size_t tmp_vmsize
= vmsize
;
416 off_t xfer_foffset
= foffset
;
418 //LP64todo - works around vn_rdwr_64() 2G limit
419 while (tmp_vmsize
> 0) {
420 vm_map_size_t xfer_vmsize
= tmp_vmsize
;
421 if (xfer_vmsize
> INT_MAX
)
422 xfer_vmsize
= INT_MAX
;
423 error
= vn_rdwr_64(UIO_WRITE
, vp
,
424 vmoffset
, xfer_vmsize
, xfer_foffset
,
425 (IS_64BIT_PROCESS(core_proc
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
426 IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, core_proc
);
427 tmp_vmsize
-= xfer_vmsize
;
428 xfer_foffset
+= xfer_vmsize
;
432 hoffset
+= segment_command_sz
;
439 * If there are remaining segments which have not been written
440 * out because break in the loop above, then they were not counted
441 * because they exceed the real address space of the executable
442 * type: remove them from the header's count. This is OK, since
443 * we are allowed to have a sparse area following the segments.
446 mh64
->ncmds
-= segment_count
;
447 mh64
->sizeofcmds
-= segment_count
* segment_command_sz
;
449 mh
->ncmds
-= segment_count
;
450 mh
->sizeofcmds
-= segment_count
* segment_command_sz
;
453 tir1
.header
= header
;
454 tir1
.hoffset
= hoffset
;
455 tir1
.flavors
= flavors
;
456 tir1
.tstate_size
= tstate_size
;
457 task_act_iterate_wth_args(task
, collectth_state
,&tir1
);
460 * Write out the Mach header at the beginning of the
461 * file. OK to use a 32 bit write for this.
463 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
464 UIO_SYSSPACE32
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, core_proc
);
465 kmem_free(kernel_map
, header
, header_size
);
467 error1
= vnode_close(vp
, FWRITE
, ctx
);
469 if (alloced_name
!= NULL
)
470 FREE(alloced_name
, M_TEMP
);