Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / gdbint.c
1 /* GDB interface for Guile
2 * Copyright (C) 1996,1997,1999,2000,2001,2002,2004
3 * Free Software Foundation, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #include "libguile/strports.h"
33 #include "libguile/read.h"
34 #include "libguile/eval.h"
35 #include "libguile/chars.h"
36 #include "libguile/modules.h"
37 #include "libguile/ports.h"
38 #include "libguile/fluids.h"
39 #include "libguile/strings.h"
40 #include "libguile/init.h"
41
42 #include "libguile/gdbint.h"
43 \f
44 /* {Support for debugging with gdb}
45 *
46 * TODO:
47 *
48 * 1. Redirect outputs
49 * 2. Catch errors
50 * 3. Prevent print from causing segmentation fault when given broken pairs
51 */
52
53 #define GDB_TYPE SCM
54
55 #include "libguile/gdb_interface.h"
56
57 \f
58
59 /* Be carefull when this macro is true.
60 scm_gc_running_p is set during gc.
61 */
62 #define SCM_GC_P (scm_gc_running_p)
63
64 /* Macros that encapsulate blocks of code which can be called by the
65 * debugger.
66 */
67 #define SCM_BEGIN_FOREIGN_BLOCK \
68 do { \
69 scm_print_carefully_p = 1; \
70 } while (0)
71
72
73 #define SCM_END_FOREIGN_BLOCK \
74 do { \
75 scm_print_carefully_p = 0; \
76 } while (0)
77
78
79 #define RESET_STRING { gdb_output_length = 0; }
80
81 #define SEND_STRING(str) \
82 do { \
83 gdb_output = (char *) (str); \
84 gdb_output_length = strlen ((const char *) (str)); \
85 } while (0)
86
87
88 /* {Gdb interface}
89 */
90
91 unsigned short gdb_options = GDB_HAVE_BINDINGS;
92
93 char *gdb_language = "lisp/c";
94
95 SCM gdb_result;
96
97 char *gdb_output;
98
99 int gdb_output_length;
100
101 int scm_print_carefully_p;
102
103 static SCM gdb_input_port;
104 static SCM gdb_output_port;
105
106
107 int
108 gdb_maybe_valid_type_p (SCM value)
109 {
110 return SCM_IMP (value); /* || scm_in_heap_p (value); */ /* FIXME: What to
111 do? */
112 }
113
114
115 int
116 gdb_read (char *str)
117 {
118 #if 0
119 SCM ans;
120 int status = 0;
121 RESET_STRING;
122 /* Need to be restrictive about what to read? */
123 if (1) /* (SCM_GC_P) */ /* FIXME */
124 {
125 char *p;
126 for (p = str; *p != '\0'; ++p)
127 switch (*p)
128 {
129 case '(':
130 case '\'':
131 case '"':
132 SEND_STRING ("Can't read this kind of expressions during gc");
133 return -1;
134 case '#':
135 if (*++p == '\0')
136 goto premature;
137 if (*p == '\\')
138 {
139 if (*++p != '\0')
140 continue;
141 premature:
142 SEND_STRING ("Premature end of lisp expression");
143 return -1;
144 }
145 default:
146 continue;
147 }
148 }
149 SCM_BEGIN_FOREIGN_BLOCK;
150 unmark_port (gdb_input_port);
151 scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
152 scm_puts (str, gdb_input_port);
153 scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
154 scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
155
156 /* Read one object */
157 ans = scm_read (gdb_input_port);
158 if (SCM_GC_P)
159 {
160 if (SCM_NIMP (ans))
161 {
162 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
163 status = -1;
164 goto exit;
165 }
166 }
167 gdb_result = ans;
168 /* Protect answer from future GC */
169 if (SCM_NIMP (ans))
170 scm_permanent_object (ans);
171 exit:
172 remark_port (gdb_input_port);
173 SCM_END_FOREIGN_BLOCK;
174 return status;
175 #else
176 abort ();
177 #endif
178 }
179
180
181 int
182 gdb_eval (SCM exp)
183 {
184 RESET_STRING;
185 if (SCM_GC_P)
186 {
187 SEND_STRING ("Can't evaluate lisp expressions during gc");
188 return -1;
189 }
190 SCM_BEGIN_FOREIGN_BLOCK;
191 {
192 SCM env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
193 gdb_result = scm_permanent_object (scm_i_eval_x (exp, env));
194 }
195 SCM_END_FOREIGN_BLOCK;
196 return 0;
197 }
198
199
200 int
201 gdb_print (SCM obj)
202 {
203 if (!scm_initialized_p)
204 SEND_STRING ("*** Guile not initialized ***");
205 else
206 {
207 RESET_STRING;
208 SCM_BEGIN_FOREIGN_BLOCK;
209 /* Reset stream */
210 scm_seek (gdb_output_port, SCM_INUM0, scm_from_int (SEEK_SET));
211 scm_write (obj, gdb_output_port);
212 scm_truncate_file (gdb_output_port, SCM_UNDEFINED);
213 {
214 scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
215
216 scm_flush (gdb_output_port);
217 *(pt->write_buf + pt->read_buf_size) = 0;
218 SEND_STRING (pt->read_buf);
219 }
220 SCM_END_FOREIGN_BLOCK;
221 }
222 return 0;
223 }
224
225
226 int
227 gdb_binding (SCM name, SCM value)
228 {
229 RESET_STRING;
230 if (SCM_GC_P)
231 {
232 SEND_STRING ("Can't create new bindings during gc");
233 return -1;
234 }
235 SCM_BEGIN_FOREIGN_BLOCK;
236 {
237 SCM var = scm_sym2var (name, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_T);
238 SCM_VARIABLE_SET (var, value);
239 }
240 SCM_END_FOREIGN_BLOCK;
241 return 0;
242 }
243
244 void
245 scm_init_gdbint ()
246 {
247 static char *s = "scm_init_gdb_interface";
248 SCM port;
249
250 scm_print_carefully_p = 0;
251
252 port = scm_mkstrport (SCM_INUM0,
253 scm_c_make_string (0, SCM_UNDEFINED),
254 SCM_OPN | SCM_WRTNG,
255 s);
256 gdb_output_port = scm_permanent_object (port);
257
258 port = scm_mkstrport (SCM_INUM0,
259 scm_c_make_string (0, SCM_UNDEFINED),
260 SCM_OPN | SCM_RDNG | SCM_WRTNG,
261 s);
262 gdb_input_port = scm_permanent_object (port);
263 }
264
265 /*
266 Local Variables:
267 c-file-style: "gnu"
268 End:
269 */