]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_core.c
xnu-517.9.4.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
23 *
24 * File: bsd/kern/kern_core.c
25 *
26 * This file contains machine independent code for performing core dumps.
27 *
28 */
29
30 #include <mach/vm_param.h>
31 #include <mach/thread_status.h>
32
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>
39 #include <sys/proc.h>
40 #include <sys/timeb.h>
41 #include <sys/times.h>
42 #include <sys/buf.h>
43 #include <sys/acct.h>
44 #include <sys/file.h>
45 #include <sys/uio.h>
46 #include <sys/kernel.h>
47 #include <sys/stat.h>
48
49 #include <mach-o/loader.h>
50 #include <mach/vm_region.h>
51 #include <mach/vm_statistics.h>
52
53 #include <vm/vm_kern.h>
54
55 typedef struct {
56 int flavor; /* the number for this flavor */
57 int count; /* count of ints in this flavor */
58 } mythread_state_flavor_t;
59
60 #if defined (__ppc__)
61
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}
67 };
68 int mynum_flavors=4;
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}
80 };
81 int mynum_flavors=9;
82
83 #else
84 #error architecture not supported
85 #endif
86
87
88 typedef struct {
89 vm_offset_t header;
90 int hoffset;
91 mythread_state_flavor_t *flavors;
92 int tstate_size;
93 } tir_t;
94
95 /* XXX should be static */
96 void collectth_state(thread_act_t th_act, tir_t *t);
97
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);
101
102
103 __private_extern__ do_coredump = 1; /* default: dump cores */
104 __private_extern__ sugid_coredump = 0; /* deafult: but not on SGUID binaries */
105
106 void
107 collectth_state(thread_act_t th_act, tir_t *t)
108 {
109 vm_offset_t header;
110 int hoffset, i ;
111 mythread_state_flavor_t *flavors;
112 struct thread_command *tc;
113 /*
114 * Fill in thread command structure.
115 */
116 header = t->header;
117 hoffset = t->hoffset;
118 flavors = t->flavors;
119
120 tc = (struct thread_command *) (header + hoffset);
121 tc->cmd = LC_THREAD;
122 tc->cmdsize = sizeof(struct thread_command)
123 + t->tstate_size;
124 hoffset += sizeof(struct thread_command);
125 /*
126 * Follow with a struct thread_state_flavor and
127 * the appropriate thread state struct for each
128 * thread state flavor.
129 */
130 for (i = 0; i < mynum_flavors; i++) {
131 *(mythread_state_flavor_t *)(header+hoffset) =
132 flavors[i];
133 hoffset += sizeof(mythread_state_flavor_t);
134 thread_getstatus(th_act, flavors[i].flavor,
135 (thread_state_t)(header+hoffset),
136 &flavors[i].count);
137 hoffset += flavors[i].count*sizeof(int);
138 }
139
140 t->hoffset = hoffset;
141 }
142
143 extern boolean_t coredumpok(vm_map_t map, vm_offset_t va); /* temp fix */
144 extern task_t current_task(void); /* XXX */
145
146 /*
147 * Create a core image on the file "core".
148 */
149 #define MAX_TSTATE_FLAVORS 10
150 int
151 coredump(struct proc *p)
152 {
153 int error=0;
154 register struct pcred *pcred = p->p_cred;
155 register struct ucred *cred = pcred->pc_ucred;
156 struct nameidata nd;
157 struct vattr vattr;
158 vm_map_t map;
159 int thread_count, segment_count;
160 int command_size, header_size, tstate_size;
161 int hoffset, foffset, vmoffset;
162 vm_offset_t header;
163 struct machine_slot *ms;
164 struct mach_header *mh;
165 struct segment_command *sc;
166 vm_size_t size;
167 vm_prot_t prot;
168 vm_prot_t maxprot;
169 vm_inherit_t inherit;
170 int error1;
171 task_t task;
172 char core_name[MAXCOMLEN+6];
173 char *name;
174 mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
175 vm_size_t mapsize;
176 int i;
177 int nesting_depth = 0;
178 kern_return_t kret;
179 struct vm_region_submap_info_64 vbr;
180 int vbrcount=0;
181 tir_t tir1;
182 struct vnode * vp;
183
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)))) {
188
189 return (EFAULT);
190 }
191
192 task = current_task();
193 map = current_map();
194 mapsize = get_vmmap_size(map);
195
196 if (mapsize >= p->p_rlimit[RLIMIT_CORE].rlim_cur)
197 return (EFAULT);
198 (void) task_suspend(task);
199
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);
202
203 /* if name creation fails, fall back to historical behaviour... */
204 if (name == NULL) {
205 sprintf(core_name, "/cores/core.%d", p->p_pid);
206 name = core_name;
207 }
208
209 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
210 if((error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR )) != 0)
211 return (error);
212 vp = nd.ni_vp;
213
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) {
217 error = EFAULT;
218 goto out;
219 }
220
221 VATTR_NULL(&vattr);
222 vattr.va_size = 0;
223 VOP_LEASE(vp, p, cred, LEASE_WRITE);
224 VOP_SETATTR(vp, &vattr, cred, p);
225 p->p_acflag |= ACORE;
226
227 /*
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.
231 */
232
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));
236 tstate_size = 0;
237 for (i = 0; i < mynum_flavors; i++)
238 tstate_size += sizeof(mythread_state_flavor_t) +
239 (flavors[i].count * sizeof(int));
240
241 command_size = segment_count*sizeof(struct segment_command) +
242 thread_count*sizeof(struct thread_command) +
243 tstate_size*thread_count;
244
245 header_size = command_size + sizeof(struct mach_header);
246
247 (void) kmem_alloc_wired(kernel_map,
248 (vm_offset_t *)&header,
249 (vm_size_t)header_size);
250
251 /*
252 * Set up Mach-O header.
253 */
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;
262
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 */
266 /*
267 * We use to check for an error, here, now we try and get
268 * as much as we can
269 */
270 while (segment_count > 0){
271 /*
272 * Get region information for next region.
273 */
274
275 while (1) {
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) {
280 break;
281 }
282 if(vbr.is_submap) {
283 nesting_depth++;
284 continue;
285 } else {
286 break;
287 }
288 }
289 if(kret != KERN_SUCCESS)
290 break;
291
292 prot = vbr.protection;
293 maxprot = vbr.max_protection;
294 inherit = vbr.inheritance;
295 /*
296 * Fill in segment command structure.
297 */
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 */
302 sc->segname[0] = 0;
303 sc->vmaddr = vmoffset;
304 sc->vmsize = size;
305 sc->fileoff = foffset;
306 sc->filesize = size;
307 sc->maxprot = maxprot;
308 sc->initprot = prot;
309 sc->nsects = 0;
310
311 /*
312 * Write segment out. Try as hard as possible to
313 * get read access to the data.
314 */
315 if ((prot & VM_PROT_READ) == 0) {
316 vm_protect(map, vmoffset, size, FALSE,
317 prot|VM_PROT_READ);
318 }
319 /*
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.
323 */
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);
329 }
330
331 hoffset += sizeof(struct segment_command);
332 foffset += size;
333 vmoffset += size;
334 segment_count--;
335 }
336
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);
342
343 /*
344 * Write out the Mach header at the beginning of the
345 * file.
346 */
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);
350 out:
351 VOP_UNLOCK(vp, 0, p);
352 error1 = vn_close(vp, FWRITE, cred, p);
353 if (error == 0)
354 error = error1;
355 return (error);
356 }