opendir change: refinement
[minix.git] / commands / mdb / misc.c
blob8bcaff4aa9ecd2173d92aae9a0c562ea3fee6731
1 /*
2 * misc.c for mdb
3 */
5 #include "mdb.h"
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 #include <sys/param.h>
11 #define ptrace mdbtrace
12 #include <sys/ptrace.h>
13 #include "proto.h"
15 static void pr_ascii(long val , int size );
17 /* Print ascii */
18 static void pr_ascii(val, size)
19 long val;
20 int size;
22 int i;
23 int v;
24 int sh;
26 #ifdef BYTES_SWAPPED
27 sh = 8 * size;
28 #else
29 sh = 0;
30 #endif
32 for (i = 0; i < size; i++) {
33 v = (int) (val >> sh) & 0xFF;
34 #ifdef BYTES_SWAPPED
35 sh -= 8;
36 #else
37 sh += 8;
38 #endif
39 Printf(isprint(v) ? "%c" : "\\%03o", v);
41 Printf("\n");
44 /* Dump stack */
45 void dump_stack(cnt)
46 long cnt;
48 vir_bytes v, vi;
49 long val, sp;
50 int num, size, nmode;
52 size = INTSIZE; /* size of stack element */
53 num = (int) cnt;
54 if (num <= 0) num = 0;
55 if (num > sk_size) num = (int) sk_size / size;
56 nmode = num; /* Save mode */
58 /* Get current SP */
59 sp = get_reg(curpid, reg_addr("sp"));
61 /* Starting address is top of stack seg -1 */
62 vi = (vir_bytes) sk_addr + (vir_bytes) sk_size - size;
64 /* Ending address */
65 v = (vir_bytes) end_addr;
66 if (nmode == 0) v = MAX(v, sp);
68 Printf("Stack Dump SP=%*lx\nAddr\tHex\tAscii\n", 2 * size, sp);
69 do {
70 val = (ptrace(T_GETDATA, curpid, (long) vi, 0L) >> SHIFT(size))
71 & MASK(size);
72 Printf("%*lx\t", 2 * ADDRSIZE, (vi >> SHIFT(ADDRSIZE))
73 & MASK(ADDRSIZE));
74 Printf("%*lx\t", 2 * size, val);
75 pr_ascii(val, size);
76 num -= 1;
77 vi -= size;
78 } while (vi >= v && (nmode ? num > 0 : 1));
83 /* Get file size */
84 off_t file_size(fd)
85 int fd;
87 struct stat st;
89 if(fstat(fd,&st) <0 ) {
90 Printf("Cannot stat\n");
91 return 0L;
93 else
94 return st.st_size;
97 /* Print help page */
98 void help_page()
100 outstr("\nHelp for mdb. For more details, type 'command ?'\n");
101 outstr("!#\t- Shell escape / Set Variable or register\n");
102 outstr("Tt\t- Current call / Backtrace all\n");
103 outstr("/nsf\t- Display for n size s with format f\n");
104 outstr("Xx [n]\t- Disasm / & display reg for n instructions\n");
105 outstr("Rr a\t- Run / with arguments a\n");
106 outstr("Cc [n]\t- Continue with current signal / no signal n times\n");
107 outstr("Ii [n]\t- Single step with / no signal for n instructions\n");
108 outstr("Mm t n\t- Trace until / Stop when modified t type for n instructions\n");
109 outstr("k \t- Kill traced process\n");
110 outstr("Bb\t- Display / Set Break-pt\n");
111 outstr("Dd\t- Delete all / one break-points\n");
112 outstr("P\t- Toggle Paging\n");
113 outstr("Ll name\t- Log to file name / and to standard output\n");
114 #ifdef DEBUG
115 outstr("Vv\t- Version info / Toggle debug flag\n");
116 #else
117 outstr("V\t- Version info\n");
118 #endif
119 outstr("e [t]\t- List symbols for type t\n");
120 outstr("y\t- Print segment mappings\n");
121 outstr("s [n]\t- Dump stack for n words\n");
122 #if SYSCALLS_SUPPORT
123 outstr("z [a]\t- Trace syscalls with address a\n");
124 #endif
125 outstr("? \t- Help - this screen\n");
126 outstr("@ file\t- Execute commands from file\n");
127 outstr("Qq\t- Quit / and kill traced process\n");
128 #ifdef DEBUG
129 outstr("Usage: mdb -x debug-level [-Ll]logfile exec-file core-file @command-file\n");
130 #else
131 outstr("Usage: mdb [-Ll]logfile exec-file core-file @command-file\n");
132 #endif
133 outstr(" mdb [-fc] file\n");
136 void version_info()
138 Printf("\nmdb version %s.%d for Minix", MDBVERSION, MDBBUILD );
139 Printf(" %s.%s", OS_RELEASE, OS_VERSION);
140 #ifdef MINIX_PC
141 #ifdef __i386
142 Printf(" (32-bit)");
143 #else
144 Printf(" (16-bit)");
145 #endif
146 #endif
147 #ifdef MINIX_ST
148 Printf("-ST");
149 #endif
150 Printf("\n");
153 /* Print help message on command */
154 void help_on(h)
155 int h;
158 switch (h) {
159 case '/':
160 outstr("<address> /nsf\t- Display for n items of size s with format f from address\n");
161 outstr("\t n defaults to 1\n");
162 outstr("\t s defaults to size of int\n");
163 outstr("\t can be b for byte h for short l for long\n");
164 outstr("\t f defaults to d for decimal\n");
165 outstr("\t can be x X o d D c s or u as in printf\n");
166 outstr("\t y treat value as address\n");
167 outstr("\t i disasm\n");
168 break;
169 case '@':
170 outstr("@ file\t- Execute commands from file\n");
171 break;
172 case '#':
173 outstr("# <address> cs value\t- Set Variable(s) at address to value\n");
174 outstr("\t\t\t for c count and size s\n");
175 outstr("\t\t\t b for byte h for short or l for long\n");
176 outstr("\t\t\t Count or size must be specified\n");
177 outstr("# $xx value\t\t- Set register $xx to value\n");
178 break;
179 case 'C':
180 outstr("C [n]\t- Continue with curent signal n times\n");
181 outstr("\t n defaults to 1\n");
182 break;
183 case 'c':
184 outstr("c [n]\t- Continue with no signal n times\n");
185 outstr("\t n defaults to 1\n");
186 break;
187 case 'e':
188 outstr("e [t]\t- List symbols for type t\n");
189 break;
190 case 's':
191 outstr("s [n]\t- Dump stack for n words\n");
192 outstr("\t n defaults to whole stack\n");
193 break;
194 case 'I':
195 outstr("I n\t- Single step with signal for n instructions n defaults to 1\n");
196 break;
197 case 'i':
198 outstr("i n\t- Single step with no signal for n instructions n defaults to 1\n");
199 break;
200 case 'M':
201 case 'm':
202 if ( h == 'M')
203 outstr("<address> M t n\t- Trace until\n");
204 else
205 outstr("<address> m t n\t- Stop when\n");
206 outstr("\t\t<address> is modified t type for n instructions\n");
207 outstr("\t\tn defaults to 1\n");
208 outstr("\t\tb for byte h for short l for long defaults to size of int\n");
209 break;
210 case 'T':
211 outstr("T\t- Display current call\n");
212 break;
213 case 't':
214 outstr("t\t- Backtrace all calls\n");
215 break;
216 case '!':
217 outstr("![command]\t- Shell escape or spawn command\n");
218 break;
219 case 'R':
220 outstr("R\t- Run the exec-file\n");
221 break;
222 case 'r':
223 outstr("r [arguments]\t- Run the exec-file with arguments\n");
224 break;
225 case 'k':
226 outstr("k\t- Kill traced process\n");
227 break;
228 case 'B':
229 outstr("B\t- Display all the Break points\n");
230 break;
231 case 'b':
232 outstr("<address> b [commands]\t- Set Break-pt at address\n");
233 outstr("\t\t\t commands will be executed by mdb at break-pt\n");
234 break;
235 case 'D':
236 outstr("D\t- Delete all break-points\n");
237 break;
238 case 'd':
239 outstr("<address> d\t- Delete one break-point at address\n");
240 break;
241 case 'q':
242 outstr("q\t- Quit mdb (and kill traced program)\n");
243 break;
244 case 'Q':
245 outstr("Q\t- Quit mdb immediately\n");
246 break;
247 case 'P':
248 outstr("P\t- Toggle Paging\n");
249 outstr("\t Defaults is OFF\n");
250 break;
251 case 'L':
252 outstr("L name\t- Log to file name\n");
253 outstr("L\t- Reset output to standard output\n");
254 break;
255 case 'l':
256 outstr("l name\t- Log to file name and standard output\n");
257 outstr("l\t- Reset output to standard output\n");
258 outstr("\t Defaults to none\n");
259 break;
260 #ifdef DEBUG
261 case 'v':
262 outstr("v\t- Toggle debug flag\n");
263 break;
264 #endif
265 case 'V':
266 outstr("V\t- Print Version Information for mdb\n");
267 break;
268 case 'X':
269 outstr("<address> X [n] [offset]\t- Disasm for n instructions\n");
270 outstr("\t\t\t Starting at address+offset\n");
271 break;
272 case 'x':
273 outstr("<address> x [n] offset\t- Disasm & display registers for n instructions\n");
274 outstr("\t\t\t Starting at address+offset\n");
275 break;
276 case 'y':
277 outstr("y\t- Print segment mappings\n");
278 break;
279 #if SYSCALLS_SUPPORT
280 case 'z':
281 outstr("z [address]\t- Trace system calls using address\n");
282 outstr("\t\t If the exec-file has symbols, mdb looks for __sendrec\n");
283 break;
284 #endif
285 default:
286 Printf("No help on command '%c' is available\n",h);
287 break;