5 #include <minix/config.h>
6 #include <minix/type.h>
7 #include <minix/callnr.h>
8 #include <minix/safecopies.h>
9 #include <minix/endpoint.h>
10 #include <minix/com.h>
11 #include <minix/syslib.h>
12 #include <minix/const.h>
13 #include <sys/ptrace.h>
14 #include <sys/svrctl.h>
22 #include "../../kernel/arch/i386/include/archtypes.h"
23 #include "../../kernel/const.h"
24 #include "../../kernel/type.h"
25 #include "../../kernel/config.h"
26 #include "../../kernel/debug.h"
27 #include "../../kernel/proc.h"
28 #include "../../kernel/ipc.h"
30 #define SLOTS (NR_TASKS + NR_PROCS)
31 struct proc proc
[SLOTS
];
34 int write_seg(int fd
, off_t off
, endpoint_t proc_e
, int seg
,
35 off_t seg_off
, phys_bytes seg_bytes
)
37 int r
, block_size
, fl
;
42 static char buf
[1024];
44 for (o
= seg_off
; o
< seg_off
+seg_bytes
; o
+= sizeof(buf
))
46 /* Copy a chunk from user space to the block buffer. */
47 if(sys_vircopy(proc_e
, seg
, (phys_bytes
) o
,
48 SELF
, D
, (vir_bytes
) buf
, (phys_bytes
) sizeof(buf
)) != OK
) {
49 printf("write_seg: sys_vircopy failed\n");
53 if((w
=write(fd
, buf
, sizeof(buf
))) != sizeof(buf
)) {
54 if(w
< 0) printf("write error: %s\n", strerror(errno
));
55 printf("write_seg: write failed: %d/%d\n", w
, sizeof(buf
));
64 int dumpcore(endpoint_t proc_e
)
66 int r
, seg
, exists
, fd
;
70 long trace_off
, trace_data
;
71 struct mem_map segs
[NR_LOCAL_SEGS
];
72 struct proc procentry
;
77 if(sys_getproctab(proc
) != OK
) {
78 printf( "Couldn't get proc tab.\n");
82 for(proc_s
= 0; proc_s
< SLOTS
; proc_s
++)
83 if(proc
[proc_s
].p_endpoint
== proc_e
&&
84 !(proc
[proc_s
].p_rts_flags
& SLOT_FREE
))
88 printf( "endpoint %d not found.\n", proc_e
);
92 if(proc_s
< 0 || proc_s
>= SLOTS
) {
93 printf( "Slot out of range (internal error).\n");
97 if(proc
[proc_s
].p_rts_flags
& SLOT_FREE
) {
98 printf( "slot %d is no process (internal error).\n",
103 sprintf(core_name
, "/tmp/core.%d", proc_e
);
105 if((fd
= open(core_name
,
106 O_CREAT
|O_WRONLY
|O_EXCL
|O_NONBLOCK
, 0600)) < 0) {
107 printf("couldn't open %s (%s)\n",
108 core_name
, strerror(errno
));
112 proc
[proc_s
].p_name
[P_NAME_LEN
-1] = '\0';
114 memcpy(segs
, proc
[proc_s
].p_memmap
, sizeof(segs
));
117 if((w
=write(fd
, segs
, sizeof(segs
))) != sizeof(segs
)) {
118 if(w
< 0) printf("write error: %s\n", strerror(errno
));
119 printf( "segs write failed: %d/%d\n", w
, sizeof(segs
));
124 /* Write out the whole kernel process table entry to get the regs. */
125 for (trace_off
= 0;; trace_off
+= sizeof(long))
127 r
= sys_trace(T_GETUSER
, proc_e
, trace_off
, &trace_data
);
132 r
= write(fd
, &trace_data
, sizeof(trace_data
));
133 if (r
!= sizeof(trace_data
)) {
134 printf( "trace_data write failed\n");
137 off
+= sizeof(trace_data
);
140 /* Loop through segments and write the segments themselves out. */
141 for (seg
= 0; seg
< NR_LOCAL_SEGS
; seg
++) {
142 len
= segs
[seg
].mem_len
<< CLICK_SHIFT
;
143 seg_off
= segs
[seg
].mem_vir
<< CLICK_SHIFT
;
144 r
= write_seg(fd
, off
, proc_e
, seg
, seg_off
, len
);
147 printf( "write failed\n");
158 main(int argc
, char *argv
[])
161 printf("usage: %s <endpoint>\n", argv
[0]);
164 dumpcore(atoi(argv
[1]));