2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
24 * File: bsd/kern/kern_core.c
26 * This file contains machine independent code for performing core dumps.
30 #include <mach/vm_param.h>
31 #include <mach/thread_status.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/signalvar.h>
36 #include <sys/resourcevar.h>
37 #include <sys/namei.h>
38 #include <sys/vnode.h>
40 #include <sys/timeb.h>
41 #include <sys/times.h>
46 #include <sys/kernel.h>
49 #include <mach-o/loader.h>
50 #include <mach/vm_region.h>
51 #include <mach/vm_statistics.h>
53 #include <vm/vm_kern.h>
56 int flavor
; /* the number for this flavor */
57 int count
; /* count of ints in this flavor */
58 } mythread_state_flavor_t
;
62 mythread_state_flavor_t thread_flavor_array
[]={
63 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
64 {PPC_FLOAT_STATE
, PPC_FLOAT_STATE_COUNT
},
65 {PPC_EXCEPTION_STATE
, PPC_EXCEPTION_STATE_COUNT
},
66 {PPC_VECTOR_STATE
, PPC_VECTOR_STATE_COUNT
}
69 #elif defined (__i386__)
70 mythread_state_flavor_t thread_flavor_array
[] = {
71 {i386_THREAD_STATE
, i386_THREAD_STATE_COUNT
},
72 {i386_THREAD_FPSTATE
, i386_THREAD_FPSTATE_COUNT
},
73 {i386_THREAD_EXCEPTSTATE
, i386_THREAD_EXCEPTSTATE_COUNT
},
74 {i386_THREAD_CTHREADSTATE
, i386_THREAD_CTHREADSTATE_COUNT
},
75 {i386_NEW_THREAD_STATE
, i386_NEW_THREAD_STATE_COUNT
},
76 {i386_FLOAT_STATE
, i386_FLOAT_STATE_COUNT
},
77 {i386_ISA_PORT_MAP_STATE
, i386_ISA_PORT_MAP_STATE_COUNT
},
78 {i386_V86_ASSIST_STATE
, i386_V86_ASSIST_STATE_COUNT
},
79 {THREAD_SYSCALL_STATE
, i386_THREAD_SYSCALL_STATE_COUNT
}
84 #error architecture not supported
91 mythread_state_flavor_t
*flavors
;
95 /* XXX should be static */
96 void collectth_state(thread_act_t th_act
, tir_t
*t
);
98 /* XXX not in a Mach header anywhere */
99 kern_return_t
thread_getstatus(register thread_act_t act
, int flavor
,
100 thread_state_t tstate
, mach_msg_type_number_t
*count
);
103 __private_extern__ do_coredump
= 1; /* default: dump cores */
104 __private_extern__ sugid_coredump
= 0; /* deafult: but not on SGUID binaries */
107 collectth_state(thread_act_t th_act
, tir_t
*t
)
111 mythread_state_flavor_t
*flavors
;
112 struct thread_command
*tc
;
114 * Fill in thread command structure.
117 hoffset
= t
->hoffset
;
118 flavors
= t
->flavors
;
120 tc
= (struct thread_command
*) (header
+ hoffset
);
122 tc
->cmdsize
= sizeof(struct thread_command
)
124 hoffset
+= sizeof(struct thread_command
);
126 * Follow with a struct thread_state_flavor and
127 * the appropriate thread state struct for each
128 * thread state flavor.
130 for (i
= 0; i
< mynum_flavors
; i
++) {
131 *(mythread_state_flavor_t
*)(header
+hoffset
) =
133 hoffset
+= sizeof(mythread_state_flavor_t
);
134 thread_getstatus(th_act
, flavors
[i
].flavor
,
135 (thread_state_t
)(header
+hoffset
),
137 hoffset
+= flavors
[i
].count
*sizeof(int);
140 t
->hoffset
= hoffset
;
143 extern boolean_t
coredumpok(vm_map_t map
, vm_offset_t va
); /* temp fix */
144 extern task_t
current_task(void); /* XXX */
147 * Create a core image on the file "core".
149 #define MAX_TSTATE_FLAVORS 10
151 coredump(struct proc
*p
)
154 register struct pcred
*pcred
= p
->p_cred
;
155 register struct ucred
*cred
= pcred
->pc_ucred
;
159 int thread_count
, segment_count
;
160 int command_size
, header_size
, tstate_size
;
161 int hoffset
, foffset
, vmoffset
;
163 struct machine_slot
*ms
;
164 struct mach_header
*mh
;
165 struct segment_command
*sc
;
169 vm_inherit_t inherit
;
172 char core_name
[MAXCOMLEN
+6];
174 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
177 int nesting_depth
= 0;
179 struct vm_region_submap_info_64 vbr
;
184 if (do_coredump
== 0 || /* Not dumping at all */
185 ( (sugid_coredump
== 0) && /* Not dumping SUID/SGID binaries */
186 ( (pcred
->p_svuid
!= pcred
->p_ruid
) ||
187 (pcred
->p_svgid
!= pcred
->p_rgid
)))) {
192 task
= current_task();
194 mapsize
= get_vmmap_size(map
);
196 if (mapsize
>= p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)
198 (void) task_suspend(task
);
200 /* create name according to sysctl'able format string */
201 name
= proc_core_name(p
->p_comm
, p
->p_ucred
->cr_uid
, p
->p_pid
);
203 /* if name creation fails, fall back to historical behaviour... */
205 sprintf(core_name
, "/cores/core.%d", p
->p_pid
);
209 NDINIT(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, name
, p
);
210 if((error
= vn_open(&nd
, O_CREAT
| FWRITE
| O_NOFOLLOW
, S_IRUSR
)) != 0)
214 /* Don't dump to non-regular files or files with links. */
215 if (vp
->v_type
!= VREG
||
216 VOP_GETATTR(vp
, &vattr
, cred
, p
) || vattr
.va_nlink
!= 1) {
223 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
224 VOP_SETATTR(vp
, &vattr
, cred
, p
);
225 p
->p_acflag
|= ACORE
;
228 * If the task is modified while dumping the file
229 * (e.g., changes in threads or VM, the resulting
230 * file will not necessarily be correct.
233 thread_count
= get_task_numacts(task
);
234 segment_count
= get_vmmap_entries(map
); /* XXX */
235 bcopy(thread_flavor_array
,flavors
,sizeof(thread_flavor_array
));
237 for (i
= 0; i
< mynum_flavors
; i
++)
238 tstate_size
+= sizeof(mythread_state_flavor_t
) +
239 (flavors
[i
].count
* sizeof(int));
241 command_size
= segment_count
*sizeof(struct segment_command
) +
242 thread_count
*sizeof(struct thread_command
) +
243 tstate_size
*thread_count
;
245 header_size
= command_size
+ sizeof(struct mach_header
);
247 (void) kmem_alloc_wired(kernel_map
,
248 (vm_offset_t
*)&header
,
249 (vm_size_t
)header_size
);
252 * Set up Mach-O header.
254 mh
= (struct mach_header
*) header
;
255 ms
= &machine_slot
[cpu_number()];
256 mh
->magic
= MH_MAGIC
;
257 mh
->cputype
= ms
->cpu_type
;
258 mh
->cpusubtype
= ms
->cpu_subtype
;
259 mh
->filetype
= MH_CORE
;
260 mh
->ncmds
= segment_count
+ thread_count
;
261 mh
->sizeofcmds
= command_size
;
263 hoffset
= sizeof(struct mach_header
); /* offset into header */
264 foffset
= round_page_32(header_size
); /* offset into file */
265 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
267 * We use to check for an error, here, now we try and get
270 while (segment_count
> 0){
272 * Get region information for next region.
276 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
277 if((kret
= vm_region_recurse_64(map
,
278 &vmoffset
, &size
, &nesting_depth
,
279 &vbr
, &vbrcount
)) != KERN_SUCCESS
) {
289 if(kret
!= KERN_SUCCESS
)
292 prot
= vbr
.protection
;
293 maxprot
= vbr
.max_protection
;
294 inherit
= vbr
.inheritance
;
296 * Fill in segment command structure.
298 sc
= (struct segment_command
*) (header
+ hoffset
);
299 sc
->cmd
= LC_SEGMENT
;
300 sc
->cmdsize
= sizeof(struct segment_command
);
301 /* segment name is zerod by kmem_alloc */
303 sc
->vmaddr
= vmoffset
;
305 sc
->fileoff
= foffset
;
307 sc
->maxprot
= maxprot
;
312 * Write segment out. Try as hard as possible to
313 * get read access to the data.
315 if ((prot
& VM_PROT_READ
) == 0) {
316 vm_protect(map
, vmoffset
, size
, FALSE
,
320 * Only actually perform write if we can read.
321 * Note: if we can't read, then we end up with
322 * a hole in the file.
324 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
325 && vbr
.user_tag
!= VM_MEMORY_IOKIT
326 && coredumpok(map
,vmoffset
)) {
327 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)vmoffset
, size
, foffset
,
328 UIO_USERSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
331 hoffset
+= sizeof(struct segment_command
);
337 tir1
.header
= header
;
338 tir1
.hoffset
= hoffset
;
339 tir1
.flavors
= flavors
;
340 tir1
.tstate_size
= tstate_size
;
341 task_act_iterate_wth_args(task
, collectth_state
,&tir1
);
344 * Write out the Mach header at the beginning of the
347 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
348 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
349 kmem_free(kernel_map
, header
, header_size
);
351 VOP_UNLOCK(vp
, 0, p
);
352 error1
= vn_close(vp
, FWRITE
, cred
, p
);