unstack, sort: cleanup and improvement
[minix.git] / commands / mdb / kernel.c
blob5e0b0ecf4fa4abc999acd65750cb14d52ef47d4e
1 /*
2 * kernel.c for mdb
3 */
5 #include "mdb.h"
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #define ptrace mdbtrace
11 #include <sys/ptrace.h>
12 #include "proto.h"
14 #include <machine/archtypes.h>
15 #include <kernel/const.h>
16 #include <kernel/type.h>
17 #include <kernel/proc.h>
19 /* Define these here */
20 /* buffer for proc and pointer to proc */
22 #define SIZ (1 + sizeof(struct proc)/sizeof(long))
24 struct proc *prc;
25 long lbuf[SIZ];
27 static char segment_name[] = "TDS";
29 /*
30 * Display memory maps
32 void disp_maps()
34 int i;
35 long int vir, phy, len;
37 Printf("\t Virtual\t Physical\tLength\n");
38 Printf("\t address\t address\n");
39 for (i = 0; i < strlen(segment_name); i++) {
40 vir = (long) prc->p_memmap[i].mem_vir << CLICK_SHIFT;
41 phy = (long) prc->p_memmap[i].mem_phys << CLICK_SHIFT;
42 len = (long) prc->p_memmap[i].mem_len << CLICK_SHIFT;
43 Printf("%c:\t0x%08.8lx\t0x%08.8lx\t%8ld (0x%08.8lx)\n",
44 segment_name[i], vir, phy, len, len);
48 void update()
50 int i;
52 for (i = 0; i < (SIZ - 1); i++)
53 lbuf[i] = ptrace(T_GETUSER, curpid, (long) (i * sizeof(long)), 0L);
55 st_addr = (long) prc->p_memmap[T].mem_vir << CLICK_SHIFT;
56 et_addr = st_addr + ( (long) prc->p_memmap[T].mem_len << CLICK_SHIFT );
58 sd_addr = (long) prc->p_memmap[D].mem_vir << CLICK_SHIFT;
59 ed_addr = end_addr =
60 sd_addr + ( (long) prc->p_memmap[D].mem_len << CLICK_SHIFT );
62 sk_addr = (long) prc->p_memmap[S].mem_vir << CLICK_SHIFT;
63 sk_size = (long) prc->p_memmap[S].mem_len << CLICK_SHIFT;
65 #ifdef MINIX_PC
66 if ( end_addr < et_addr ) end_addr = et_addr;
67 #endif
71 int disp_regs()
73 int i;
75 if (curpid <= 0) {
76 Printf("No active process.\n");
77 return 1;
80 /* Look at kernel/type.h see how this data from the stackframe is laid out */
82 #if defined(MINIX_PC) && defined(__i86)
83 Printf("\
84 es ds di si bp bx dx cx ax ip cs psw sp ss\
85 \n");
86 for (i = 0; i < 16; i++)
87 if ( i != 5 && i != 10 ) Printf("%04x ", ((reg_t *) &prc->p_reg)[i]);
88 Printf("\n");
89 #endif
91 #if defined(MINIX_PC) && defined(__i386)
92 Printf("\n");
93 Printf("\
94 fs gs ds es edi esi ebp ebx edx\n");
95 for (i = 0; i < 8; i++)
96 if ( i != 6 ) Printf("%08lx ", ((reg_t *) &prc->p_reg)[i]);
97 Printf("\n\
98 ecx eax eip cs psw esp ss\n");
99 for (; i < 16; i++)
100 if ( i != 10 ) Printf("%08lx ", ((reg_t *) &prc->p_reg)[i]);
101 Printf("\n");
102 #endif
104 #ifdef MINIX_ST
105 Printf("\npc=%lx psw=%x\n\n",(long)PC_MEMBER(prc), PSW_MEMBER(prc));
106 Printf(
107 " 0 1 2 3 4 5 6 7\nD");
108 for (i = 0; i < 8; i++) Printf(" %08lx", ((reg_t *) &prc->p_reg)[i]);
109 Printf("\nA");
110 for (; i < NR_REGS; i++) Printf(" %08lx", ((reg_t *) &prc->p_reg)[i]);
111 Printf(" %08lx\n\n", (long)SP_MEMBER(prc));
112 #endif
113 return 0;
116 /* System dependent core */
118 #ifdef MINIX_PC
120 #ifdef __i386
121 static char regs[] = "fs gs ds es di si bp bx dx cx ax ip cs ps sp ss";
122 #else
123 static char regs[] = "es ds di si bp bx dx cx ax ip cs ps sp ss";
124 #endif
126 /* Get register for pid at offset k */
127 long get_reg(pid, k)
128 int pid;
129 long k;
131 long off;
132 long val;
133 int reg_size;
135 /* Calculate size of register */
136 reg_size = (k < N_REG16 * 2) ? 2 : sizeof(reg_t);
138 /* Adjust offset */
139 off = k - (k & (sizeof(long) - 1));
141 val = ptrace(T_GETUSER, pid, off, 0L);
143 if (k & (sizeof(long) - 1))
144 val >>= BITSIZE(reg_size);
145 else
146 val &= MASK(reg_size);
147 return val;
151 /* Set register for pid at offset k */
152 void set_reg(pid, k, value)
153 int pid;
154 long k;
155 long value;
157 long off;
159 /* Adjust offset */
160 off = k - (k & (sizeof(long) - 1));
162 ptrace(T_SETUSER, pid, off, value);
167 long reg_addr(s)
168 char *s;
170 long val;
171 char *t;
172 char *send;
173 char q[3];
175 if (*s == ' ')
176 mdb_error("Invalid syntax\n");
177 q[0] = tolower(*s);
178 q[1] = tolower(*++s);
179 q[2] = '\0';
181 t = regs;
182 send = regs + sizeof(regs);
183 while (t < send) {
184 if (strncmp(q, t, 2) == 0) {
185 val = (long) (t - regs);
186 val /= 3L;
187 if (val < N_REG16 - 1)
188 val = val * 2;
189 else
190 val = (N_REG16 - 1) * 2 +
191 (val - N_REG16 + 1) * sizeof(reg_t);
192 return val;
194 t += 3;
196 Printf("Unknown register: %s", q);
197 mdb_error("\n");
201 int outsegreg(num)
202 off_t num;
204 /* print segment register */
206 if ((num % HCLICK_SIZE) != 0 || num >= 0x100000)
208 Printf("%08x",num);
209 return 8;
211 Printf("%04x", (u16_t) (num / HCLICK_SIZE) );
212 return 4;
215 #endif
217 #ifdef MINIX_ST
219 /* Get register for pid at offset k */
220 long get_reg(pid, k)
221 int pid;
222 long k;
224 return ptrace(T_GETUSER, pid, k, 0L);
227 long reg_addr(s)
228 char *s;
230 long val;
232 switch (*s++) {
233 case 'a':
234 case 'A': val = 32; break;
235 case 'd':
236 case 'D': val = 0; break;
237 case 'P':
238 case 'p': if (*s != 'c' && *s != 'C') goto error;
239 return 64;
240 break;
241 default: goto error;
243 if (*s >= '0' && *s <= '7')
244 return val + 4 * (*s - '0');
245 error:
246 Printf("Unknown register: %2.2s", s);
247 mdb_error("\n");
250 #endif