2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
25 * File: bsd/kern/kern_core.c
27 * This file contains machine independent code for performing core dumps.
31 #include <mach/vm_param.h>
32 #include <mach/thread_status.h>
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>
39 #include <sys/vnode_internal.h>
40 #include <sys/proc_internal.h>
41 #include <sys/kauth.h>
42 #include <sys/timeb.h>
43 #include <sys/times.h>
45 #include <sys/file_internal.h>
47 #include <sys/kernel.h>
50 #include <mach-o/loader.h>
51 #include <mach/vm_region.h>
52 #include <mach/vm_statistics.h>
54 #include <vm/vm_kern.h>
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() */
62 int flavor
; /* the number for this flavor */
63 int count
; /* count of ints in this flavor */
64 } mythread_state_flavor_t
;
68 mythread_state_flavor_t thread_flavor_array
[]={
69 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
70 {PPC_FLOAT_STATE
, PPC_FLOAT_STATE_COUNT
},
71 {PPC_EXCEPTION_STATE
, PPC_EXCEPTION_STATE_COUNT
},
72 {PPC_VECTOR_STATE
, PPC_VECTOR_STATE_COUNT
}
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
}
90 #error architecture not supported
97 mythread_state_flavor_t
*flavors
;
101 /* XXX should be static */
102 void collectth_state(thread_t th_act
, void *tirp
);
104 /* XXX not in a Mach header anywhere */
105 kern_return_t
thread_getstatus(register thread_t act
, int flavor
,
106 thread_state_t tstate
, mach_msg_type_number_t
*count
);
107 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
110 __private_extern__
int do_coredump
= 1; /* default: dump cores */
111 __private_extern__
int sugid_coredump
= 0; /* default: but not SGUID binaries */
114 collectth_state(thread_t th_act
, void *tirp
)
118 mythread_state_flavor_t
*flavors
;
119 struct thread_command
*tc
;
120 tir_t
*t
= (tir_t
*)tirp
;
123 * Fill in thread command structure.
126 hoffset
= t
->hoffset
;
127 flavors
= t
->flavors
;
129 tc
= (struct thread_command
*) (header
+ hoffset
);
131 tc
->cmdsize
= sizeof(struct thread_command
)
133 hoffset
+= sizeof(struct thread_command
);
135 * Follow with a struct thread_state_flavor and
136 * the appropriate thread state struct for each
137 * thread state flavor.
139 for (i
= 0; i
< mynum_flavors
; i
++) {
140 *(mythread_state_flavor_t
*)(header
+hoffset
) =
142 hoffset
+= sizeof(mythread_state_flavor_t
);
143 thread_getstatus(th_act
, flavors
[i
].flavor
,
144 (thread_state_t
)(header
+hoffset
),
146 hoffset
+= flavors
[i
].count
*sizeof(int);
149 t
->hoffset
= hoffset
;
153 * Create a core image on the file "core".
155 #define MAX_TSTATE_FLAVORS 10
157 coredump(struct proc
*p
)
160 kauth_cred_t cred
= kauth_cred_get();
161 struct vnode_attr va
;
162 struct vfs_context context
;
164 int thread_count
, segment_count
;
165 int command_size
, header_size
, tstate_size
;
168 vm_map_offset_t vmoffset
;
170 vm_map_size_t vmsize
;
173 vm_inherit_t inherit
;
176 char core_name
[MAXCOMLEN
+6];
178 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
181 int nesting_depth
= 0;
183 struct vm_region_submap_info_64 vbr
;
187 struct mach_header
*mh
;
188 struct mach_header_64
*mh64
;
190 size_t mach_header_sz
= sizeof(struct mach_header
);
191 size_t segment_command_sz
= sizeof(struct segment_command
);
193 if (do_coredump
== 0 || /* Not dumping at all */
194 ( (sugid_coredump
== 0) && /* Not dumping SUID/SGID binaries */
195 ( (cred
->cr_svuid
!= cred
->cr_ruid
) ||
196 (cred
->cr_svgid
!= cred
->cr_rgid
)))) {
201 if (IS_64BIT_PROCESS(p
)) {
203 mach_header_sz
= sizeof(struct mach_header_64
);
204 segment_command_sz
= sizeof(struct segment_command_64
);
207 task
= current_task();
209 mapsize
= get_vmmap_size(map
);
211 if (mapsize
>= p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)
213 (void) task_suspend(task
);
215 /* create name according to sysctl'able format string */
216 name
= proc_core_name(p
->p_comm
, kauth_cred_getuid(cred
), p
->p_pid
);
218 /* if name creation fails, fall back to historical behaviour... */
220 sprintf(core_name
, "/cores/core.%d", p
->p_pid
);
224 context
.vc_ucred
= cred
;
226 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), S_IRUSR
, VNODE_LOOKUP_NOFOLLOW
, &vp
, &context
)))
230 VATTR_WANTED(&va
, va_nlink
);
231 /* Don't dump to non-regular files or files with links. */
232 if (vp
->v_type
!= VREG
||
233 vnode_getattr(vp
, &va
, &context
) || va
.va_nlink
!= 1) {
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
);
241 p
->p_acflag
|= ACORE
;
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.
249 thread_count
= get_task_numacts(task
);
250 segment_count
= get_vmmap_entries(map
); /* XXX */
251 bcopy(thread_flavor_array
,flavors
,sizeof(thread_flavor_array
));
253 for (i
= 0; i
< mynum_flavors
; i
++)
254 tstate_size
+= sizeof(mythread_state_flavor_t
) +
255 (flavors
[i
].count
* sizeof(int));
257 command_size
= segment_count
* segment_command_sz
+
258 thread_count
*sizeof(struct thread_command
) +
259 tstate_size
*thread_count
;
261 header_size
= command_size
+ mach_header_sz
;
263 (void) kmem_alloc(kernel_map
,
264 (vm_offset_t
*)&header
,
265 (vm_size_t
)header_size
);
268 * Set up Mach-O header.
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 */
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
;
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 */
294 * We use to check for an error, here, now we try and get
297 while (segment_count
> 0) {
298 struct segment_command
*sc
;
299 struct segment_command_64
*sc64
;
302 * Get region information for next region.
306 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
307 if((kret
= mach_vm_region_recurse(map
,
308 &vmoffset
, &vmsize
, &nesting_depth
,
309 (vm_region_recurse_info_t
)&vbr
,
310 &vbrcount
)) != KERN_SUCCESS
) {
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.
320 (vmoffset
+ vmsize
> VM_MAX_ADDRESS
)) {
321 kret
= KERN_INVALID_ADDRESS
;
331 if(kret
!= KERN_SUCCESS
)
334 prot
= vbr
.protection
;
335 maxprot
= vbr
.max_protection
;
336 inherit
= vbr
.inheritance
;
338 * Fill in segment command structure.
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
;
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 */
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
;
369 * Write segment out. Try as hard as possible to
370 * get read access to the data.
372 if ((prot
& VM_PROT_READ
) == 0) {
373 mach_vm_protect(map
, vmoffset
, vmsize
, FALSE
,
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.
381 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
382 && vbr
.user_tag
!= VM_MEMORY_IOKIT
383 && coredumpok(map
,vmoffset
)) {
384 vm_map_size_t tmp_vmsize
= vmsize
;
385 off_t xfer_foffset
= foffset
;
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
;
401 hoffset
+= segment_command_sz
;
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.
415 mh64
->ncmds
-= segment_count
;
417 mh
->ncmds
-= segment_count
;
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
);
427 * Write out the Mach header at the beginning of the
428 * file. OK to use a 32 bit write for this.
430 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
431 UIO_SYSSPACE32
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
432 kmem_free(kernel_map
, header
, header_size
);
434 error1
= vnode_close(vp
, FWRITE
, &context
);