#include <mach/vm_param.h>
#include <mach/thread_status.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signalvar.h>
#include <sys/resourcevar.h>
#include <sys/namei.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <sys/buf.h>
#include <sys/acct.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/stat.h>
#include <mach-o/loader.h>
#include <mach/vm_region.h>
#include <vm/vm_kern.h>
typedef struct {
int flavor;
int count;
} mythread_state_flavor_t;
#if defined (__ppc__)
mythread_state_flavor_t thread_flavor_array[]={
{PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT},
{PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT},
{PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT}
};
int mynum_flavors=3;
#elif defined (__i386__)
mythread_state_flavor_t thread_flavor_array [] = {
{i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
{i386_THREAD_FPSTATE, i386_THREAD_FPSTATE_COUNT},
{i386_THREAD_EXCEPTSTATE, i386_THREAD_EXCEPTSTATE_COUNT},
{i386_THREAD_CTHREADSTATE, i386_THREAD_CTHREADSTATE_COUNT},
{i386_NEW_THREAD_STATE, i386_NEW_THREAD_STATE_COUNT},
{i386_FLOAT_STATE, i386_FLOAT_STATE_COUNT},
{i386_ISA_PORT_MAP_STATE, i386_ISA_PORT_MAP_STATE_COUNT},
{i386_V86_ASSIST_STATE, i386_V86_ASSIST_STATE_COUNT},
{THREAD_SYSCALL_STATE, i386_THREAD_SYSCALL_STATE_COUNT}
};
int mynum_flavors=9;
#else
#error architecture not supported
#endif
typedef struct {
vm_offset_t header;
int hoffset;
mythread_state_flavor_t *flavors;
int tstate_size;
} tir_t;
collectth_state(thread_act_t th_act, tir_t *t)
{
vm_offset_t header;
int hoffset, i ;
mythread_state_flavor_t *flavors;
struct thread_command *tc;
header = t->header;
hoffset = t->hoffset;
flavors = t->flavors;
tc = (struct thread_command *) (header + hoffset);
tc->cmd = LC_THREAD;
tc->cmdsize = sizeof(struct thread_command)
+ t->tstate_size;
hoffset += sizeof(struct thread_command);
for (i = 0; i < mynum_flavors; i++) {
*(mythread_state_flavor_t *)(header+hoffset) =
flavors[i];
hoffset += sizeof(mythread_state_flavor_t);
thread_getstatus(th_act, flavors[i].flavor,
(thread_state_t *)(header+hoffset),
&flavors[i].count);
hoffset += flavors[i].count*sizeof(int);
}
t->hoffset = hoffset;
}
#define MAX_TSTATE_FLAVORS 10
int
coredump(p)
register struct proc *p;
{
int error=0;
register struct pcred *pcred = p->p_cred;
register struct ucred *cred = pcred->pc_ucred;
struct nameidata nd;
struct vattr vattr;
vm_map_t map;
int thread_count, segment_count;
int command_size, header_size, tstate_size;
int hoffset, foffset, vmoffset;
vm_offset_t header;
struct machine_slot *ms;
struct mach_header *mh;
struct segment_command *sc;
struct thread_command *tc;
vm_size_t size;
vm_prot_t prot;
vm_prot_t maxprot;
vm_inherit_t inherit;
vm_offset_t offset;
int error1;
task_t task;
char core_name[MAXCOMLEN+6];
mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
vm_size_t nflavors,mapsize;
int i;
int nesting_depth = 0;
kern_return_t kret;
struct vm_region_submap_info_64 vbr;
int vbrcount=0;
tir_t tir1;
struct vnode * vp;
if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
return (EFAULT);
task = current_task();
map = current_map();
mapsize = get_vmmap_size(map);
if (mapsize >= p->p_rlimit[RLIMIT_CORE].rlim_cur)
return (EFAULT);
(void) task_suspend(task);
#if defined (__ppc__)
fpu_save();
#endif
sprintf(core_name, "/cores/core.%d", p->p_pid);
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, core_name, p);
if(error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR ))
return (error);
vp = nd.ni_vp;
if (vp->v_type != VREG ||
VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
error = EFAULT;
goto out;
}
VATTR_NULL(&vattr);
vattr.va_size = 0;
VOP_LEASE(vp, p, cred, LEASE_WRITE);
VOP_SETATTR(vp, &vattr, cred, p);
p->p_acflag |= ACORE;
thread_count = get_task_numacts(task);
segment_count = get_vmmap_entries(map);
#if 0
nflavors = sizeof(flavors)/sizeof(int);
if (thread_getstatus(current_thread(), THREAD_STATE_FLAVOR_LIST,
(thread_state_t)(flavors),
&nflavors) != KERN_SUCCESS)
panic("core flavor list");
nflavors /= sizeof(mythread_state_flavor_t)/sizeof(int);
#else
nflavors = mynum_flavors;
bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array));
#endif
tstate_size = 0;
for (i = 0; i < nflavors; i++)
tstate_size += sizeof(mythread_state_flavor_t) +
(flavors[i].count * sizeof(int));
command_size = segment_count*sizeof(struct segment_command) +
thread_count*sizeof(struct thread_command) +
tstate_size*thread_count;
header_size = command_size + sizeof(struct mach_header);
(void) kmem_alloc_wired(kernel_map,
(vm_offset_t *)&header,
(vm_size_t)header_size);
mh = (struct mach_header *) header;
ms = &machine_slot[cpu_number()];
mh->magic = MH_MAGIC;
mh->cputype = ms->cpu_type;
mh->cpusubtype = ms->cpu_subtype;
mh->filetype = MH_CORE;
mh->ncmds = segment_count + thread_count;
mh->sizeofcmds = command_size;
hoffset = sizeof(struct mach_header);
foffset = round_page(header_size);
vmoffset = VM_MIN_ADDRESS;
while (segment_count > 0){
while (1) {
vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
if((kret = vm_region_recurse_64(map,
&vmoffset, &size, &nesting_depth,
&vbr, &vbrcount)) != KERN_SUCCESS) {
break;
}
if(vbr.is_submap) {
nesting_depth++;
continue;
} else {
break;
}
}
if(kret != KERN_SUCCESS)
break;
prot = vbr.protection;
maxprot = vbr.max_protection;
inherit = vbr.inheritance;
sc = (struct segment_command *) (header + hoffset);
sc->cmd = LC_SEGMENT;
sc->cmdsize = sizeof(struct segment_command);
sc->vmaddr = vmoffset;
sc->vmsize = size;
sc->fileoff = foffset;
sc->filesize = size;
sc->maxprot = maxprot;
sc->initprot = prot;
sc->nsects = 0;
if ((prot & VM_PROT_READ) == 0) {
vm_protect(map, vmoffset, size, FALSE,
prot|VM_PROT_READ);
}
if ((maxprot & VM_PROT_READ) == VM_PROT_READ) {
error = vn_rdwr(UIO_WRITE, vp, (caddr_t)vmoffset, size, foffset,
UIO_USERSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
}
hoffset += sizeof(struct segment_command);
foffset += size;
vmoffset += size;
segment_count--;
}
#if 0
task_lock(task);
thread = (thread_t) queue_first(&task->thread_list);
while (thread_count > 0) {
tc = (struct thread_command *) (header + hoffset);
tc->cmd = LC_THREAD;
tc->cmdsize = sizeof(struct thread_command)
+ tstate_size;
hoffset += sizeof(struct thread_command);
for (i = 0; i < nflavors; i++) {
*(mythread_state_flavor_t *)(header+hoffset) =
flavors[i];
hoffset += sizeof(mythread_state_flavor_t);
thread_getstatus(thread, flavors[i].flavor,
(thread_state_t *)(header+hoffset),
&flavors[i].count);
hoffset += flavors[i].count*sizeof(int);
}
thread = (thread_t) queue_next(&thread->thread_list);
thread_count--;
}
task_unlock(task);
#else
tir1.header = header;
tir1.hoffset = hoffset;
tir1.flavors = flavors;
tir1.tstate_size = tstate_size;
task_act_iterate_wth_args(task, collectth_state,&tir1);
#endif
error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
kmem_free(kernel_map, header, header_size);
out:
VOP_UNLOCK(vp, 0, p);
error1 = vn_close(vp, FWRITE, cred, p);
if (error == 0)
error = error1;
}