2 * Copyright (c) 2000-2002 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.
33 #include <mach/vm_param.h>
34 #include <mach/thread_status.h>
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>
43 #include <sys/timeb.h>
44 #include <sys/times.h>
49 #include <sys/kernel.h>
52 #include <mach-o/loader.h>
53 #include <mach/vm_region.h>
54 #include <mach/vm_statistics.h>
56 #include <vm/vm_kern.h>
59 int flavor
; /* the number for this flavor */
60 int count
; /* count of ints in this flavor */
61 } mythread_state_flavor_t
;
65 mythread_state_flavor_t thread_flavor_array
[]={
66 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
67 {PPC_FLOAT_STATE
, PPC_FLOAT_STATE_COUNT
},
68 {PPC_EXCEPTION_STATE
, PPC_EXCEPTION_STATE_COUNT
},
69 {PPC_VECTOR_STATE
, PPC_VECTOR_STATE_COUNT
}
72 #elif defined (__i386__)
73 mythread_state_flavor_t thread_flavor_array
[] = {
74 {i386_THREAD_STATE
, i386_THREAD_STATE_COUNT
},
75 {i386_THREAD_FPSTATE
, i386_THREAD_FPSTATE_COUNT
},
76 {i386_THREAD_EXCEPTSTATE
, i386_THREAD_EXCEPTSTATE_COUNT
},
77 {i386_THREAD_CTHREADSTATE
, i386_THREAD_CTHREADSTATE_COUNT
},
78 {i386_NEW_THREAD_STATE
, i386_NEW_THREAD_STATE_COUNT
},
79 {i386_FLOAT_STATE
, i386_FLOAT_STATE_COUNT
},
80 {i386_ISA_PORT_MAP_STATE
, i386_ISA_PORT_MAP_STATE_COUNT
},
81 {i386_V86_ASSIST_STATE
, i386_V86_ASSIST_STATE_COUNT
},
82 {THREAD_SYSCALL_STATE
, i386_THREAD_SYSCALL_STATE_COUNT
}
87 #error architecture not supported
94 mythread_state_flavor_t
*flavors
;
99 collectth_state(thread_act_t th_act
, tir_t
*t
)
103 mythread_state_flavor_t
*flavors
;
104 struct thread_command
*tc
;
106 * Fill in thread command structure.
109 hoffset
= t
->hoffset
;
110 flavors
= t
->flavors
;
112 tc
= (struct thread_command
*) (header
+ hoffset
);
114 tc
->cmdsize
= sizeof(struct thread_command
)
116 hoffset
+= sizeof(struct thread_command
);
118 * Follow with a struct thread_state_flavor and
119 * the appropriate thread state struct for each
120 * thread state flavor.
122 for (i
= 0; i
< mynum_flavors
; i
++) {
123 *(mythread_state_flavor_t
*)(header
+hoffset
) =
125 hoffset
+= sizeof(mythread_state_flavor_t
);
126 thread_getstatus(th_act
, flavors
[i
].flavor
,
127 (thread_state_t
*)(header
+hoffset
),
129 hoffset
+= flavors
[i
].count
*sizeof(int);
132 t
->hoffset
= hoffset
;
135 * Create a core image on the file "core".
137 #define MAX_TSTATE_FLAVORS 10
140 register struct proc
*p
;
143 register struct pcred
*pcred
= p
->p_cred
;
144 register struct ucred
*cred
= pcred
->pc_ucred
;
148 int thread_count
, segment_count
;
149 int command_size
, header_size
, tstate_size
;
150 int hoffset
, foffset
, vmoffset
;
152 struct machine_slot
*ms
;
153 struct mach_header
*mh
;
154 struct segment_command
*sc
;
155 struct thread_command
*tc
;
159 vm_inherit_t inherit
;
163 char core_name
[MAXCOMLEN
+6];
164 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
165 vm_size_t nflavors
,mapsize
;
167 int nesting_depth
= 0;
169 struct vm_region_submap_info_64 vbr
;
173 extern boolean_t
coredumpok(vm_map_t map
, vm_offset_t va
); /* temp fix */
174 extern task_t
current_task(); /* XXX */
176 if (pcred
->p_svuid
!= pcred
->p_ruid
|| pcred
->p_svgid
!= pcred
->p_rgid
)
179 task
= current_task();
181 mapsize
= get_vmmap_size(map
);
183 if (mapsize
>= p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)
185 (void) task_suspend(task
);
187 sprintf(core_name
, "/cores/core.%d", p
->p_pid
);
188 NDINIT(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, core_name
, p
);
189 if(error
= vn_open(&nd
, O_CREAT
| FWRITE
| O_NOFOLLOW
, S_IRUSR
))
193 /* Don't dump to non-regular files or files with links. */
194 if (vp
->v_type
!= VREG
||
195 VOP_GETATTR(vp
, &vattr
, cred
, p
) || vattr
.va_nlink
!= 1) {
202 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
203 VOP_SETATTR(vp
, &vattr
, cred
, p
);
204 p
->p_acflag
|= ACORE
;
207 * If the task is modified while dumping the file
208 * (e.g., changes in threads or VM, the resulting
209 * file will not necessarily be correct.
212 thread_count
= get_task_numacts(task
);
213 segment_count
= get_vmmap_entries(map
); /* XXX */
215 * nflavors here is really the number of ints in flavors
216 * to meet the thread_getstatus() calling convention
218 nflavors
= mynum_flavors
;
219 bcopy(thread_flavor_array
,flavors
,sizeof(thread_flavor_array
));
221 for (i
= 0; i
< nflavors
; i
++)
222 tstate_size
+= sizeof(mythread_state_flavor_t
) +
223 (flavors
[i
].count
* sizeof(int));
225 command_size
= segment_count
*sizeof(struct segment_command
) +
226 thread_count
*sizeof(struct thread_command
) +
227 tstate_size
*thread_count
;
229 header_size
= command_size
+ sizeof(struct mach_header
);
231 (void) kmem_alloc_wired(kernel_map
,
232 (vm_offset_t
*)&header
,
233 (vm_size_t
)header_size
);
236 * Set up Mach-O header.
238 mh
= (struct mach_header
*) header
;
239 ms
= &machine_slot
[cpu_number()];
240 mh
->magic
= MH_MAGIC
;
241 mh
->cputype
= ms
->cpu_type
;
242 mh
->cpusubtype
= ms
->cpu_subtype
;
243 mh
->filetype
= MH_CORE
;
244 mh
->ncmds
= segment_count
+ thread_count
;
245 mh
->sizeofcmds
= command_size
;
247 hoffset
= sizeof(struct mach_header
); /* offset into header */
248 foffset
= round_page_32(header_size
); /* offset into file */
249 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
251 * We use to check for an error, here, now we try and get
254 while (segment_count
> 0){
256 * Get region information for next region.
260 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
261 if((kret
= vm_region_recurse_64(map
,
262 &vmoffset
, &size
, &nesting_depth
,
263 &vbr
, &vbrcount
)) != KERN_SUCCESS
) {
273 if(kret
!= KERN_SUCCESS
)
276 prot
= vbr
.protection
;
277 maxprot
= vbr
.max_protection
;
278 inherit
= vbr
.inheritance
;
280 * Fill in segment command structure.
282 sc
= (struct segment_command
*) (header
+ hoffset
);
283 sc
->cmd
= LC_SEGMENT
;
284 sc
->cmdsize
= sizeof(struct segment_command
);
285 /* segment name is zerod by kmem_alloc */
287 sc
->vmaddr
= vmoffset
;
289 sc
->fileoff
= foffset
;
291 sc
->maxprot
= maxprot
;
296 * Write segment out. Try as hard as possible to
297 * get read access to the data.
299 if ((prot
& VM_PROT_READ
) == 0) {
300 vm_protect(map
, vmoffset
, size
, FALSE
,
304 * Only actually perform write if we can read.
305 * Note: if we can't read, then we end up with
306 * a hole in the file.
308 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
309 && vbr
.user_tag
!= VM_MEMORY_IOKIT
310 && coredumpok(map
,vmoffset
)) {
311 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)vmoffset
, size
, foffset
,
312 UIO_USERSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
315 hoffset
+= sizeof(struct segment_command
);
321 tir1
.header
= header
;
322 tir1
.hoffset
= hoffset
;
323 tir1
.flavors
= flavors
;
324 tir1
.tstate_size
= tstate_size
;
325 task_act_iterate_wth_args(task
, collectth_state
,&tir1
);
328 * Write out the Mach header at the beginning of the
331 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
332 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
333 kmem_free(kernel_map
, header
, header_size
);
335 VOP_UNLOCK(vp
, 0, p
);
336 error1
= vn_close(vp
, FWRITE
, cred
, p
);