2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
27 * File: bsd/kern/kern_core.c
29 * This file contains machine independent code for performing core dumps.
32 * 16-Feb-91 Mike DeMoney (mike@next.com)
33 * Massaged into MI form from m68k/core.c.
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.h>
46 #include <sys/timeb.h>
47 #include <sys/times.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>
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 collectth_state(thread_act_t th_act
, tir_t
*t
)
105 mythread_state_flavor_t
*flavors
;
106 struct thread_command
*tc
;
108 * Fill in thread command structure.
111 hoffset
= t
->hoffset
;
112 flavors
= t
->flavors
;
114 tc
= (struct thread_command
*) (header
+ hoffset
);
116 tc
->cmdsize
= sizeof(struct thread_command
)
118 hoffset
+= sizeof(struct thread_command
);
120 * Follow with a struct thread_state_flavor and
121 * the appropriate thread state struct for each
122 * thread state flavor.
124 for (i
= 0; i
< mynum_flavors
; i
++) {
125 *(mythread_state_flavor_t
*)(header
+hoffset
) =
127 hoffset
+= sizeof(mythread_state_flavor_t
);
128 thread_getstatus(th_act
, flavors
[i
].flavor
,
129 (thread_state_t
*)(header
+hoffset
),
131 hoffset
+= flavors
[i
].count
*sizeof(int);
134 t
->hoffset
= hoffset
;
137 * Create a core image on the file "core".
139 #define MAX_TSTATE_FLAVORS 10
142 register struct proc
*p
;
145 register struct pcred
*pcred
= p
->p_cred
;
146 register struct ucred
*cred
= pcred
->pc_ucred
;
150 int thread_count
, segment_count
;
151 int command_size
, header_size
, tstate_size
;
152 int hoffset
, foffset
, vmoffset
;
154 struct machine_slot
*ms
;
155 struct mach_header
*mh
;
156 struct segment_command
*sc
;
157 struct thread_command
*tc
;
161 vm_inherit_t inherit
;
165 char core_name
[MAXCOMLEN
+6];
166 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
167 vm_size_t nflavors
,mapsize
;
169 int nesting_depth
= 0;
171 struct vm_region_submap_info_64 vbr
;
175 extern boolean_t
coredumpok(vm_map_t map
, vm_offset_t va
); /* temp fix */
177 if (pcred
->p_svuid
!= pcred
->p_ruid
|| pcred
->p_svgid
!= pcred
->p_rgid
)
180 task
= current_task();
182 mapsize
= get_vmmap_size(map
);
184 if (mapsize
>= p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)
186 (void) task_suspend(task
);
188 sprintf(core_name
, "/cores/core.%d", p
->p_pid
);
189 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, core_name
, p
);
190 if(error
= vn_open(&nd
, O_CREAT
| FWRITE
, S_IRUSR
))
194 /* Don't dump to non-regular files or files with links. */
195 if (vp
->v_type
!= VREG
||
196 VOP_GETATTR(vp
, &vattr
, cred
, p
) || vattr
.va_nlink
!= 1) {
203 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
204 VOP_SETATTR(vp
, &vattr
, cred
, p
);
205 p
->p_acflag
|= ACORE
;
208 * If the task is modified while dumping the file
209 * (e.g., changes in threads or VM, the resulting
210 * file will not necessarily be correct.
213 thread_count
= get_task_numacts(task
);
214 segment_count
= get_vmmap_entries(map
); /* XXX */
216 * nflavors here is really the number of ints in flavors
217 * to meet the thread_getstatus() calling convention
220 nflavors
= sizeof(flavors
)/sizeof(int);
221 if (thread_getstatus(current_thread(), THREAD_STATE_FLAVOR_LIST
,
222 (thread_state_t
)(flavors
),
223 &nflavors
) != KERN_SUCCESS
)
224 panic("core flavor list");
225 /* now convert to number of flavors */
226 nflavors
/= sizeof(mythread_state_flavor_t
)/sizeof(int);
228 nflavors
= mynum_flavors
;
229 bcopy(thread_flavor_array
,flavors
,sizeof(thread_flavor_array
));
232 for (i
= 0; i
< nflavors
; i
++)
233 tstate_size
+= sizeof(mythread_state_flavor_t
) +
234 (flavors
[i
].count
* sizeof(int));
236 command_size
= segment_count
*sizeof(struct segment_command
) +
237 thread_count
*sizeof(struct thread_command
) +
238 tstate_size
*thread_count
;
240 header_size
= command_size
+ sizeof(struct mach_header
);
242 (void) kmem_alloc_wired(kernel_map
,
243 (vm_offset_t
*)&header
,
244 (vm_size_t
)header_size
);
247 * Set up Mach-O header.
249 mh
= (struct mach_header
*) header
;
250 ms
= &machine_slot
[cpu_number()];
251 mh
->magic
= MH_MAGIC
;
252 mh
->cputype
= ms
->cpu_type
;
253 mh
->cpusubtype
= ms
->cpu_subtype
;
254 mh
->filetype
= MH_CORE
;
255 mh
->ncmds
= segment_count
+ thread_count
;
256 mh
->sizeofcmds
= command_size
;
258 hoffset
= sizeof(struct mach_header
); /* offset into header */
259 foffset
= round_page_32(header_size
); /* offset into file */
260 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
261 /* We use to check for an error, here, now we try and get
264 while (segment_count
> 0){
266 * Get region information for next region.
270 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
271 if((kret
= vm_region_recurse_64(map
,
272 &vmoffset
, &size
, &nesting_depth
,
273 &vbr
, &vbrcount
)) != KERN_SUCCESS
) {
283 if(kret
!= KERN_SUCCESS
)
286 prot
= vbr
.protection
;
287 maxprot
= vbr
.max_protection
;
288 inherit
= vbr
.inheritance
;
290 * Fill in segment command structure.
292 sc
= (struct segment_command
*) (header
+ hoffset
);
293 sc
->cmd
= LC_SEGMENT
;
294 sc
->cmdsize
= sizeof(struct segment_command
);
295 /* segment name is zerod by kmem_alloc */
297 sc
->vmaddr
= vmoffset
;
299 sc
->fileoff
= foffset
;
301 sc
->maxprot
= maxprot
;
306 * Write segment out. Try as hard as possible to
307 * get read access to the data.
309 if ((prot
& VM_PROT_READ
) == 0) {
310 vm_protect(map
, vmoffset
, size
, FALSE
,
314 * Only actually perform write if we can read.
315 * Note: if we can't read, then we end up with
316 * a hole in the file.
318 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
&& vbr
.user_tag
!= VM_MEMORY_IOKIT
&& coredumpok(map
,vmoffset
)) {
319 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)vmoffset
, size
, foffset
,
320 UIO_USERSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
323 hoffset
+= sizeof(struct segment_command
);
331 thread
= (thread_t
) queue_first(&task
->thread_list
);
332 while (thread_count
> 0) {
334 * Fill in thread command structure.
336 tc
= (struct thread_command
*) (header
+ hoffset
);
338 tc
->cmdsize
= sizeof(struct thread_command
)
340 hoffset
+= sizeof(struct thread_command
);
342 * Follow with a struct thread_state_flavor and
343 * the appropriate thread state struct for each
344 * thread state flavor.
346 for (i
= 0; i
< nflavors
; i
++) {
347 *(mythread_state_flavor_t
*)(header
+hoffset
) =
349 hoffset
+= sizeof(mythread_state_flavor_t
);
350 thread_getstatus(thread
, flavors
[i
].flavor
,
351 (thread_state_t
*)(header
+hoffset
),
353 hoffset
+= flavors
[i
].count
*sizeof(int);
355 thread
= (thread_t
) queue_next(&thread
->thread_list
);
360 tir1
.header
= header
;
361 tir1
.hoffset
= hoffset
;
362 tir1
.flavors
= flavors
;
363 tir1
.tstate_size
= tstate_size
;
364 task_act_iterate_wth_args(task
, collectth_state
,&tir1
);
368 * Write out the Mach header at the beginning of the
371 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
372 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
373 kmem_free(kernel_map
, header
, header_size
);
375 VOP_UNLOCK(vp
, 0, p
);
376 error1
= vn_close(vp
, FWRITE
, cred
, p
);