Take pointer, not word count, as upper limit in verify_space()
[sbcl.git] / src / runtime / parse.c
blob2919797b6249c8b3a94332dd06ae5ba0879e5443
1 /* parsing for LDB monitor */
3 /*
4 * This software is part of the SBCL system. See the README file for
5 * more information.
7 * This software is derived from the CMU CL system, which was
8 * written at Carnegie Mellon University and released into the
9 * public domain. The software is in the public domain and is
10 * provided with absolutely no warranty. See the COPYING and CREDITS
11 * files for more information.
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <ctype.h>
18 #include "sbcl.h"
19 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
20 #include "pthreads_win32.h"
21 #else
22 #include <signal.h>
23 #endif
24 #include "runtime.h"
26 #if defined(LISP_FEATURE_SB_LDB)
28 #include "globals.h"
29 #include "vars.h"
30 #include "parse.h"
31 #include "os.h"
32 #include "interrupt.h"
33 #include "lispregs.h"
34 #include "monitor.h"
35 #include "validate.h"
36 #include "arch.h"
37 #include "search.h"
38 #include "thread.h"
39 #include "pseudo-atomic.h"
41 #include "genesis/simple-fun.h"
42 #include "genesis/fdefn.h"
43 #include "genesis/symbol.h"
44 #include "genesis/static-symbols.h"
46 static void skip_ws(char **ptr)
48 while (**ptr <= ' ' && **ptr != '\0')
49 (*ptr)++;
52 static boolean string_to_long(char *token, uword_t *value)
54 int base, digit;
55 uword_t num;
56 char *ptr;
58 if (token == 0)
59 return 0;
61 if (token[0] == '0')
62 if (token[1] == 'x') {
63 base = 16;
64 token += 2;
66 else {
67 base = 8;
68 token++;
70 else if (token[0] == '#') {
71 switch (token[1]) {
72 case 'x':
73 case 'X':
74 base = 16;
75 token += 2;
76 break;
77 case 'o':
78 case 'O':
79 base = 8;
80 token += 2;
81 break;
82 default:
83 return 0;
86 else
87 base = 10;
89 num = 0;
90 ptr = token;
91 while (*ptr != '\0') {
92 if (*ptr >= 'a' && *ptr <= 'f')
93 digit = *ptr + 10 - 'a';
94 else if (*ptr >= 'A' && *ptr <= 'F')
95 digit = *ptr + 10 - 'A';
96 else if (*ptr >= '0' && *ptr <= '9')
97 digit = *ptr - '0';
98 else
99 return 0;
100 if (digit < 0 || digit >= base)
101 return 0;
103 ptr++;
104 num = num * base + digit;
107 *value = num;
108 return 1;
111 static boolean lookup_variable(char *name, lispobj *result)
113 struct var *var = lookup_by_name(name);
115 if (var == NULL)
116 return 0;
117 else {
118 *result = var_value(var);
119 return 1;
124 boolean more_p(char **ptr)
126 skip_ws(ptr);
128 if (**ptr == '\0')
129 return 0;
130 else
131 return 1;
134 char *parse_token(char **ptr)
136 char *token;
138 skip_ws(ptr);
140 if (**ptr == '\0')
141 return NULL;
143 token = *ptr;
145 while (**ptr > ' ')
146 (*ptr)++;
148 if (**ptr != '\0') {
149 **ptr = '\0';
150 (*ptr)++;
153 return token;
156 uword_t parse_number(char **ptr)
158 char *token = parse_token(ptr);
159 uword_t result;
161 if (token == NULL) {
162 printf("expected a number\n");
163 throw_to_monitor();
165 else if (string_to_long(token, &result))
166 return result;
167 else {
168 printf("invalid number: ``%s''\n", token);
169 throw_to_monitor();
171 return 0;
174 char *parse_addr(char **ptr, boolean safely)
176 char *token = parse_token(ptr);
177 lispobj result;
179 if (token == NULL) {
180 printf("expected an address\n");
181 throw_to_monitor();
183 else if (token[0] == '$') {
184 if (!lookup_variable(token+1, &result)) {
185 printf("unknown variable: ``%s''\n", token);
186 throw_to_monitor();
188 result &= ~7;
190 else {
191 uword_t value;
192 if (!string_to_long(token, &value)) {
193 printf("invalid number: ``%s''\n", token);
194 throw_to_monitor();
196 result = (value & ~3);
199 if (safely && !is_valid_lisp_addr((os_vm_address_t)result)) {
200 printf("invalid Lisp-level address: %p\n", (void *)result);
201 throw_to_monitor();
204 return (char *)result;
207 static lispobj lookup_symbol(char *name)
209 int count;
210 lispobj *headerptr;
212 /* Search static space. */
213 headerptr = (lispobj *)STATIC_SPACE_START;
214 count =
215 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
216 (lispobj *)STATIC_SPACE_START;
217 if (search_for_symbol(name, &headerptr, &count))
218 return make_lispobj(headerptr, OTHER_POINTER_LOWTAG);
220 #ifdef LISP_FEATURE_IMMOBILE_SPACE
221 /* Search immobile space. */
222 headerptr = (lispobj *)IMMOBILE_SPACE_START;
223 count = IMMOBILE_FIXEDOBJ_SUBSPACE_SIZE / N_WORD_BYTES;
224 if (search_for_symbol(name, &headerptr, &count))
225 return make_lispobj(headerptr, OTHER_POINTER_LOWTAG);
226 #endif
228 /* Search dynamic space. */
229 #if defined(LISP_FEATURE_GENCGC)
230 headerptr = (lispobj *)DYNAMIC_SPACE_START;
231 count = (lispobj *)get_alloc_pointer() - headerptr;
232 #else
233 headerptr = (lispobj *)current_dynamic_space;
234 count = dynamic_space_free_pointer - headerptr;
235 #endif
236 if (search_for_symbol(name, &headerptr, &count))
237 return make_lispobj(headerptr, OTHER_POINTER_LOWTAG);
239 return 0;
242 static int parse_regnum(char *s)
244 if ((s[1] == 'R') || (s[1] == 'r')) {
245 int regnum;
247 if (s[2] == '\0')
248 return -1;
250 /* skip the $R part and call atoi on the number */
251 regnum = atoi(s + 2);
252 if ((regnum >= 0) && (regnum < NREGS))
253 return regnum;
254 else
255 return -1;
256 } else {
257 int i;
259 for (i = 0; i < NREGS ; i++)
260 if (strcasecmp(s + 1, lisp_register_names[i]) == 0)
261 #ifdef LISP_FEATURE_X86
262 return i*2;
263 #else
264 return i;
265 #endif
267 return -1;
271 lispobj parse_lispobj(char **ptr)
273 struct thread *thread=arch_os_get_current_thread();
274 char *token = parse_token(ptr);
275 uword_t pointer;
276 lispobj result;
277 uword_t value;
279 if (token == NULL) {
280 printf("expected an object\n");
281 throw_to_monitor();
282 } else if (token[0] == '$') {
283 if (isalpha(token[1])) {
284 int free_ici;
285 int regnum;
286 os_context_t *context;
288 free_ici = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
290 if (free_ici == 0) {
291 printf("Variable ``%s'' is not valid -- there is no current interrupt context.\n", token);
292 throw_to_monitor();
295 context = thread->interrupt_contexts[free_ici - 1];
297 regnum = parse_regnum(token);
298 if (regnum < 0) {
299 printf("bogus register: ``%s''\n", token);
300 throw_to_monitor();
303 result = *os_context_register_addr(context, regnum);
304 } else if (!lookup_variable(token+1, &result)) {
305 printf("unknown variable: ``%s''\n", token);
306 throw_to_monitor();
308 } else if (token[0] == '@') {
309 if (string_to_long(token+1, &pointer)) {
310 pointer &= ~3;
311 if (is_valid_lisp_addr((os_vm_address_t)pointer))
312 result = *(lispobj *)pointer;
313 else {
314 printf("invalid Lisp-level address: ``%s''\n", token+1);
315 throw_to_monitor();
318 else {
319 printf("invalid address: ``%s''\n", token+1);
320 throw_to_monitor();
323 else if (string_to_long(token, &value))
324 result = value;
325 else if ((result = lookup_symbol(token)) != 0)
327 else {
328 printf("invalid Lisp object: ``%s''\n", token);
329 throw_to_monitor();
332 return result;
335 #endif /* defined(LISP_FEATURE_SB_LDB) */