2009-12-29 22:34:06 +01:00
|
|
|
/* dumpcore - create core file of running process */
|
2006-08-15 17:59:38 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/type.h>
|
2009-12-29 22:34:06 +01:00
|
|
|
#include <minix/ipc.h>
|
2006-08-15 17:59:38 +02:00
|
|
|
#include <minix/const.h>
|
|
|
|
#include <sys/ptrace.h>
|
2009-12-29 22:34:06 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
2006-08-15 17:59:38 +02:00
|
|
|
#include <timers.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
Split of architecture-dependent and -independent functions for i386,
mainly in the kernel and headers. This split based on work by
Ingmar Alting <iaalting@cs.vu.nl> done for his Minix PowerPC architecture
port.
. kernel does not program the interrupt controller directly, do any
other architecture-dependent operations, or contain assembly any more,
but uses architecture-dependent functions in arch/$(ARCH)/.
. architecture-dependent constants and types defined in arch/$(ARCH)/include.
. <ibm/portio.h> moved to <minix/portio.h>, as they have become, for now,
architecture-independent functions.
. int86, sdevio, readbios, and iopenable are now i386-specific kernel calls
and live in arch/i386/do_* now.
. i386 arch now supports even less 86 code; e.g. mpx86.s and klib86.s have
gone, and 'machine.protected' is gone (and always taken to be 1 in i386).
If 86 support is to return, it should be a new architecture.
. prototypes for the architecture-dependent functions defined in
kernel/arch/$(ARCH)/*.c but used in kernel/ are in kernel/proto.h
. /etc/make.conf included in makefiles and shell scripts that need to
know the building architecture; it defines ARCH=<arch>, currently only
i386.
. some basic per-architecture build support outside of the kernel (lib)
. in clock.c, only dequeue a process if it was ready
. fixes for new include files
files deleted:
. mpx/klib.s - only for choosing between mpx/klib86 and -386
. klib86.s - only for 86
i386-specific files files moved (or arch-dependent stuff moved) to arch/i386/:
. mpx386.s (entry point)
. klib386.s
. sconst.h
. exception.c
. protect.c
. protect.h
. i8269.c
2006-12-22 16:22:27 +01:00
|
|
|
#include "../../kernel/arch/i386/include/archtypes.h"
|
2006-08-15 17:59:38 +02:00
|
|
|
#include "../../kernel/proc.h"
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
#define CLICK_WORDS (CLICK_SIZE / sizeof(unsigned long))
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
int adjust_stack(pid_t pid, struct mem_map *seg)
|
|
|
|
{
|
|
|
|
static unsigned long buf[CLICK_WORDS];
|
|
|
|
struct ptrace_range pr;
|
|
|
|
size_t off, top, bottom;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* FIXME: kernel/VM strangeness */
|
|
|
|
seg->mem_vir -= seg->mem_len - 1;
|
|
|
|
|
|
|
|
/* Scan the stack, top to bottom, to find the lowest accessible region.
|
|
|
|
* In practice that will be at 64MB, so we also scan for the lowest non-zero
|
|
|
|
* region in order to keep the core file size managable.
|
|
|
|
* Portability note: this code assumes that the stack grows down.
|
|
|
|
*/
|
|
|
|
top = seg->mem_vir + seg->mem_len;
|
|
|
|
|
|
|
|
pr.pr_space = TS_DATA;
|
|
|
|
pr.pr_addr = (top - 1) << CLICK_SHIFT;
|
|
|
|
pr.pr_size = sizeof(buf);
|
|
|
|
pr.pr_ptr = buf;
|
|
|
|
|
|
|
|
for (off = top - 1; off >= seg->mem_vir; off--) {
|
|
|
|
if (ptrace(T_GETRANGE, pid, (long) &pr, 0)) {
|
|
|
|
if (errno == EFAULT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
perror("ptrace(T_GETRANGE)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < CLICK_WORDS; i += sizeof(buf[0]))
|
|
|
|
if (buf[i] != 0)
|
|
|
|
bottom = off;
|
|
|
|
|
|
|
|
pr.pr_addr -= sizeof(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add one extra zero page as margin. */
|
|
|
|
if (bottom > off && bottom > seg->mem_vir)
|
|
|
|
bottom--;
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
seg->mem_len -= bottom - seg->mem_vir;
|
|
|
|
seg->mem_vir = bottom;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_seg(int fd, pid_t pid, int seg, off_t seg_off, phys_bytes seg_bytes)
|
2006-08-15 17:59:38 +02:00
|
|
|
{
|
2009-12-29 22:34:06 +01:00
|
|
|
int r;
|
|
|
|
off_t off;
|
2006-08-15 17:59:38 +02:00
|
|
|
ssize_t w;
|
2009-12-29 22:34:06 +01:00
|
|
|
static char buf[CLICK_SIZE];
|
|
|
|
struct ptrace_range pr;
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
pr.pr_space = (seg == T) ? TS_INS : TS_DATA;
|
|
|
|
pr.pr_addr = seg_off;
|
|
|
|
pr.pr_size = sizeof(buf);
|
|
|
|
pr.pr_ptr = buf;
|
|
|
|
|
|
|
|
for ( ; pr.pr_addr < seg_off + seg_bytes; pr.pr_addr += sizeof(buf))
|
2006-08-15 17:59:38 +02:00
|
|
|
{
|
|
|
|
/* Copy a chunk from user space to the block buffer. */
|
2009-12-29 22:34:06 +01:00
|
|
|
if (ptrace(T_GETRANGE, pid, (long) &pr, 0)) {
|
|
|
|
/* Create holes for inaccessible areas. */
|
|
|
|
if (errno == EFAULT) {
|
|
|
|
lseek(fd, sizeof(buf), SEEK_CUR);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
perror("ptrace(T_GETRANGE)");
|
2006-08-15 17:59:38 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((w=write(fd, buf, sizeof(buf))) != sizeof(buf)) {
|
|
|
|
if(w < 0) printf("write error: %s\n", strerror(errno));
|
|
|
|
printf("write_seg: write failed: %d/%d\n", w, sizeof(buf));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
return 0;
|
2006-08-15 17:59:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
int dumpcore(pid_t pid)
|
2006-08-15 17:59:38 +02:00
|
|
|
{
|
2009-12-29 22:34:06 +01:00
|
|
|
int r, seg, fd;
|
2006-08-15 17:59:38 +02:00
|
|
|
vir_bytes len;
|
|
|
|
off_t off, seg_off;
|
2009-12-29 22:34:06 +01:00
|
|
|
long data;
|
2006-08-15 17:59:38 +02:00
|
|
|
struct mem_map segs[NR_LOCAL_SEGS];
|
|
|
|
struct proc procentry;
|
|
|
|
ssize_t w;
|
2009-12-29 22:34:06 +01:00
|
|
|
char core_name[PATH_MAX];
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
/* Get the process table entry for this process. */
|
|
|
|
len = sizeof(struct proc) / sizeof(long);
|
|
|
|
for (off = 0; off < len; off++)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
data = ptrace(T_GETUSER, pid, off * sizeof(long), 0);
|
|
|
|
if (data == -1 && errno != 0)
|
|
|
|
{
|
|
|
|
perror("ptrace(T_GETUSER)");
|
|
|
|
return 1;
|
|
|
|
}
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
((long *) &procentry)[off] = data;
|
2006-08-15 17:59:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
memcpy(segs, procentry.p_memmap, sizeof(segs));
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
/* Correct and reduce the stack segment. */
|
|
|
|
r = adjust_stack(pid, &segs[S]);
|
|
|
|
if (r != 0)
|
|
|
|
goto error;
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
/* Create a core file with a temporary, unique name. */
|
|
|
|
sprintf(core_name, "core.%d", pid);
|
2006-08-15 17:59:38 +02:00
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
if((fd = open(core_name, O_CREAT|O_EXCL|O_WRONLY, 0600)) < 0) {
|
|
|
|
fprintf(stderr, "couldn't open %s (%s)\n", core_name,
|
|
|
|
strerror(errno));
|
2006-08-15 17:59:38 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
/* Write out the process's segments. */
|
2006-08-15 17:59:38 +02:00
|
|
|
if((w=write(fd, segs, sizeof(segs))) != sizeof(segs)) {
|
|
|
|
if(w < 0) printf("write error: %s\n", strerror(errno));
|
|
|
|
printf( "segs write failed: %d/%d\n", w, sizeof(segs));
|
2009-12-29 22:34:06 +01:00
|
|
|
goto error;
|
2006-08-15 17:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write out the whole kernel process table entry to get the regs. */
|
2009-12-29 22:34:06 +01:00
|
|
|
if((w=write(fd, &procentry, sizeof(procentry))) != sizeof(procentry)) {
|
|
|
|
if(w < 0) printf("write error: %s\n", strerror(errno));
|
|
|
|
printf( "proc write failed: %d/%d\n", w, sizeof(procentry));
|
|
|
|
goto error;
|
2006-08-15 17:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through segments and write the segments themselves out. */
|
|
|
|
for (seg = 0; seg < NR_LOCAL_SEGS; seg++) {
|
|
|
|
len= segs[seg].mem_len << CLICK_SHIFT;
|
|
|
|
seg_off= segs[seg].mem_vir << CLICK_SHIFT;
|
2009-12-29 22:34:06 +01:00
|
|
|
r= write_seg(fd, pid, seg, seg_off, len);
|
|
|
|
if (r != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Give the core file its final name. */
|
|
|
|
if (rename(core_name, "core")) {
|
|
|
|
perror("rename");
|
|
|
|
goto error;
|
2006-08-15 17:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
2009-12-29 22:34:06 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
unlink(core_name);
|
|
|
|
|
|
|
|
return 1;
|
2006-08-15 17:59:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
int main(int argc, char *argv[])
|
2006-08-15 17:59:38 +02:00
|
|
|
{
|
2009-12-29 22:34:06 +01:00
|
|
|
pid_t pid;
|
|
|
|
int r, status;
|
|
|
|
|
2006-08-15 17:59:38 +02:00
|
|
|
if(argc != 2) {
|
2009-12-29 22:34:06 +01:00
|
|
|
printf("usage: %s <pid>\n", argv[0]);
|
2006-08-15 17:59:38 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-29 22:34:06 +01:00
|
|
|
pid = atoi(argv[1]);
|
|
|
|
|
|
|
|
if (ptrace(T_ATTACH, pid, 0, 0) != 0) {
|
|
|
|
perror("ptrace(T_ATTACH)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (waitpid(pid, &status, 0) != pid) {
|
|
|
|
perror("waitpid");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {
|
|
|
|
/* whatever happens here is fine */
|
|
|
|
ptrace(T_RESUME, pid, 0, WSTOPSIG(status));
|
|
|
|
|
|
|
|
if (waitpid(pid, &status, 0) != pid) {
|
|
|
|
perror("waitpid");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFSTOPPED(status)) {
|
|
|
|
fprintf(stderr, "process died while attaching\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = dumpcore(pid);
|
|
|
|
|
|
|
|
if (ptrace(T_DETACH, pid, 0, 0)) {
|
|
|
|
fprintf(stderr, "warning, detaching failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|