fix packman sort col, and make sort case-insensitive
[minix3.git] / commands / simple / dumpcore.c
blob64d056cc1482ce790fff6ee63033d653b7dbf4b7
2 #include <fcntl.h>
3 #include <assert.h>
4 #include <unistd.h>
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>
15 #include <dirent.h>
16 #include <timers.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.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;
38 off_t n, o, b_off;
39 block_t b;
40 struct buf *bp;
41 ssize_t w;
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");
50 return 1;
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));
56 return 1;
60 return OK;
64 int dumpcore(endpoint_t proc_e)
66 int r, seg, exists, fd;
67 mode_t omode;
68 vir_bytes len;
69 off_t off, seg_off;
70 long trace_off, trace_data;
71 struct mem_map segs[NR_LOCAL_SEGS];
72 struct proc procentry;
73 int proc_s;
74 ssize_t w;
75 char core_name[200];
77 if(sys_getproctab(proc) != OK) {
78 printf( "Couldn't get proc tab.\n");
79 return 1;
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))
85 break;
87 if(proc_s >= SLOTS) {
88 printf( "endpoint %d not found.\n", proc_e);
89 return 1;
92 if(proc_s < 0 || proc_s >= SLOTS) {
93 printf( "Slot out of range (internal error).\n");
94 return 1;
97 if(proc[proc_s].p_rts_flags & SLOT_FREE) {
98 printf( "slot %d is no process (internal error).\n",
99 proc_s);
100 return 1;
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));
109 return 1;
112 proc[proc_s].p_name[P_NAME_LEN-1] = '\0';
114 memcpy(segs, proc[proc_s].p_memmap, sizeof(segs));
116 off= 0;
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));
120 return 1;
122 off += 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);
128 if (r != OK)
130 break;
132 r= write(fd, &trace_data, sizeof(trace_data));
133 if (r != sizeof(trace_data)) {
134 printf( "trace_data write failed\n");
135 return 1;
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);
145 if (r != OK)
147 printf( "write failed\n");
148 return 1;
150 off += len;
153 close(fd);
155 return 0;
158 main(int argc, char *argv[])
160 if(argc != 2) {
161 printf("usage: %s <endpoint>\n", argv[0]);
162 return 1;
164 dumpcore(atoi(argv[1]));
165 return 1;