2 * Copyright (c) 2000 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.
29 * 16-Feb-91 Mike DeMoney (mike@next.com)
30 * Massaged into MI form from m68k/core.c.
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>
55 #include <vm/vm_kern.h>
58 int flavor
; /* the number for this flavor */
59 int count
; /* count of ints in this flavor */
60 } mythread_state_flavor_t
;
64 mythread_state_flavor_t thread_flavor_array
[]={
65 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
66 {PPC_FLOAT_STATE
, PPC_FLOAT_STATE_COUNT
},
67 {PPC_EXCEPTION_STATE
, PPC_EXCEPTION_STATE_COUNT
}
70 #elif defined (__i386__)
71 mythread_state_flavor_t thread_flavor_array
[] = {
72 {i386_THREAD_STATE
, i386_THREAD_STATE_COUNT
},
73 {i386_THREAD_FPSTATE
, i386_THREAD_FPSTATE_COUNT
},
74 {i386_THREAD_EXCEPTSTATE
, i386_THREAD_EXCEPTSTATE_COUNT
},
75 {i386_THREAD_CTHREADSTATE
, i386_THREAD_CTHREADSTATE_COUNT
},
76 {i386_NEW_THREAD_STATE
, i386_NEW_THREAD_STATE_COUNT
},
77 {i386_FLOAT_STATE
, i386_FLOAT_STATE_COUNT
},
78 {i386_ISA_PORT_MAP_STATE
, i386_ISA_PORT_MAP_STATE_COUNT
},
79 {i386_V86_ASSIST_STATE
, i386_V86_ASSIST_STATE_COUNT
},
80 {THREAD_SYSCALL_STATE
, i386_THREAD_SYSCALL_STATE_COUNT
}
85 #error architecture not supported
92 mythread_state_flavor_t
*flavors
;
96 collectth_state(thread_act_t th_act
, tir_t
*t
)
100 mythread_state_flavor_t
*flavors
;
101 struct thread_command
*tc
;
103 * Fill in thread command structure.
106 hoffset
= t
->hoffset
;
107 flavors
= t
->flavors
;
109 tc
= (struct thread_command
*) (header
+ hoffset
);
111 tc
->cmdsize
= sizeof(struct thread_command
)
113 hoffset
+= sizeof(struct thread_command
);
115 * Follow with a struct thread_state_flavor and
116 * the appropriate thread state struct for each
117 * thread state flavor.
119 for (i
= 0; i
< mynum_flavors
; i
++) {
120 *(mythread_state_flavor_t
*)(header
+hoffset
) =
122 hoffset
+= sizeof(mythread_state_flavor_t
);
123 thread_getstatus(th_act
, flavors
[i
].flavor
,
124 (thread_state_t
*)(header
+hoffset
),
126 hoffset
+= flavors
[i
].count
*sizeof(int);
129 t
->hoffset
= hoffset
;
132 * Create a core image on the file "core".
134 #define MAX_TSTATE_FLAVORS 10
137 register struct proc
*p
;
140 register struct pcred
*pcred
= p
->p_cred
;
141 register struct ucred
*cred
= pcred
->pc_ucred
;
145 int thread_count
, segment_count
;
146 int command_size
, header_size
, tstate_size
;
147 int hoffset
, foffset
, vmoffset
;
149 struct machine_slot
*ms
;
150 struct mach_header
*mh
;
151 struct segment_command
*sc
;
152 struct thread_command
*tc
;
156 vm_inherit_t inherit
;
160 char core_name
[MAXCOMLEN
+6];
161 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
162 vm_size_t nflavors
,mapsize
;
164 int nesting_depth
= 0;
166 struct vm_region_submap_info_64 vbr
;
172 if (pcred
->p_svuid
!= pcred
->p_ruid
|| pcred
->p_svgid
!= pcred
->p_rgid
)
175 task
= current_task();
177 mapsize
= get_vmmap_size(map
);
179 if (mapsize
>= p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)
181 (void) task_suspend(task
);
184 * Make sure all registers, etc. are in pcb so they get
187 #if defined (__ppc__)
188 fpu_save(current_act());
189 vec_save(current_act());
191 sprintf(core_name
, "/cores/core.%d", p
->p_pid
);
192 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, core_name
, p
);
193 if(error
= vn_open(&nd
, O_CREAT
| FWRITE
, S_IRUSR
))
197 /* Don't dump to non-regular files or files with links. */
198 if (vp
->v_type
!= VREG
||
199 VOP_GETATTR(vp
, &vattr
, cred
, p
) || vattr
.va_nlink
!= 1) {
206 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
207 VOP_SETATTR(vp
, &vattr
, cred
, p
);
208 p
->p_acflag
|= ACORE
;
211 * If the task is modified while dumping the file
212 * (e.g., changes in threads or VM, the resulting
213 * file will not necessarily be correct.
216 thread_count
= get_task_numacts(task
);
217 segment_count
= get_vmmap_entries(map
); /* XXX */
219 * nflavors here is really the number of ints in flavors
220 * to meet the thread_getstatus() calling convention
223 nflavors
= sizeof(flavors
)/sizeof(int);
224 if (thread_getstatus(current_thread(), THREAD_STATE_FLAVOR_LIST
,
225 (thread_state_t
)(flavors
),
226 &nflavors
) != KERN_SUCCESS
)
227 panic("core flavor list");
228 /* now convert to number of flavors */
229 nflavors
/= sizeof(mythread_state_flavor_t
)/sizeof(int);
231 nflavors
= mynum_flavors
;
232 bcopy(thread_flavor_array
,flavors
,sizeof(thread_flavor_array
));
235 for (i
= 0; i
< nflavors
; i
++)
236 tstate_size
+= sizeof(mythread_state_flavor_t
) +
237 (flavors
[i
].count
* sizeof(int));
239 command_size
= segment_count
*sizeof(struct segment_command
) +
240 thread_count
*sizeof(struct thread_command
) +
241 tstate_size
*thread_count
;
243 header_size
= command_size
+ sizeof(struct mach_header
);
245 (void) kmem_alloc_wired(kernel_map
,
246 (vm_offset_t
*)&header
,
247 (vm_size_t
)header_size
);
250 * Set up Mach-O header.
252 mh
= (struct mach_header
*) header
;
253 ms
= &machine_slot
[cpu_number()];
254 mh
->magic
= MH_MAGIC
;
255 mh
->cputype
= ms
->cpu_type
;
256 mh
->cpusubtype
= ms
->cpu_subtype
;
257 mh
->filetype
= MH_CORE
;
258 mh
->ncmds
= segment_count
+ thread_count
;
259 mh
->sizeofcmds
= command_size
;
261 hoffset
= sizeof(struct mach_header
); /* offset into header */
262 foffset
= round_page(header_size
); /* offset into file */
263 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
264 /* We use to check for an error, here, now we try and get
267 while (segment_count
> 0){
269 * Get region information for next region.
273 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
274 if((kret
= vm_region_recurse_64(map
,
275 &vmoffset
, &size
, &nesting_depth
,
276 &vbr
, &vbrcount
)) != KERN_SUCCESS
) {
286 if(kret
!= KERN_SUCCESS
)
289 prot
= vbr
.protection
;
290 maxprot
= vbr
.max_protection
;
291 inherit
= vbr
.inheritance
;
293 * Fill in segment command structure.
295 sc
= (struct segment_command
*) (header
+ hoffset
);
296 sc
->cmd
= LC_SEGMENT
;
297 sc
->cmdsize
= sizeof(struct segment_command
);
298 /* segment name is zerod by kmem_alloc */
299 sc
->vmaddr
= vmoffset
;
301 sc
->fileoff
= foffset
;
303 sc
->maxprot
= maxprot
;
308 * Write segment out. Try as hard as possible to
309 * get read access to the data.
311 if ((prot
& VM_PROT_READ
) == 0) {
312 vm_protect(map
, vmoffset
, size
, FALSE
,
316 * Only actually perform write if we can read.
317 * Note: if we can't read, then we end up with
318 * a hole in the file.
320 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
) {
321 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)vmoffset
, size
, foffset
,
322 UIO_USERSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
325 hoffset
+= sizeof(struct segment_command
);
333 thread
= (thread_t
) queue_first(&task
->thread_list
);
334 while (thread_count
> 0) {
336 * Fill in thread command structure.
338 tc
= (struct thread_command
*) (header
+ hoffset
);
340 tc
->cmdsize
= sizeof(struct thread_command
)
342 hoffset
+= sizeof(struct thread_command
);
344 * Follow with a struct thread_state_flavor and
345 * the appropriate thread state struct for each
346 * thread state flavor.
348 for (i
= 0; i
< nflavors
; i
++) {
349 *(mythread_state_flavor_t
*)(header
+hoffset
) =
351 hoffset
+= sizeof(mythread_state_flavor_t
);
352 thread_getstatus(thread
, flavors
[i
].flavor
,
353 (thread_state_t
*)(header
+hoffset
),
355 hoffset
+= flavors
[i
].count
*sizeof(int);
357 thread
= (thread_t
) queue_next(&thread
->thread_list
);
362 tir1
.header
= header
;
363 tir1
.hoffset
= hoffset
;
364 tir1
.flavors
= flavors
;
365 tir1
.tstate_size
= tstate_size
;
366 task_act_iterate_wth_args(task
, collectth_state
,&tir1
);
370 * Write out the Mach header at the beginning of the
373 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
374 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, cred
, (int *) 0, p
);
375 kmem_free(kernel_map
, header
, header_size
);
377 VOP_UNLOCK(vp
, 0, p
);
378 error1
= vn_close(vp
, FWRITE
, cred
, p
);