* gc_os_dep.c: #include <config.h> if HAVE_CONFIG_H. Replace
[bpt/guile.git] / libguile / gdbint.c
... / ...
CommitLineData
1/* GDB interface for Guile
2 * Copyright (C) 1996,1997,1999,2000,2001, 2002 Free Software Foundation, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
18 *
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
21 *
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
27 *
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
30 *
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
38 *
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
42 *
43 * The author can be reached at djurfeldt@nada.kth.se
44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
45
46
47
48#include "libguile/_scm.h"
49
50#include <stdio.h>
51#include <string.h>
52#ifdef HAVE_UNISTD_H
53#include <unistd.h>
54#endif
55
56#include "libguile/strports.h"
57#include "libguile/read.h"
58#include "libguile/eval.h"
59#include "libguile/chars.h"
60#include "libguile/modules.h"
61#include "libguile/ports.h"
62#include "libguile/fluids.h"
63#include "libguile/strings.h"
64#include "libguile/init.h"
65
66#include "libguile/gdbint.h"
67\f
68/* {Support for debugging with gdb}
69 *
70 * TODO:
71 *
72 * 1. Redirect outputs
73 * 2. Catch errors
74 * 3. Prevent print from causing segmentation fault when given broken pairs
75 */
76
77#define GDB_TYPE SCM
78
79#include "libguile/gdb_interface.h"
80
81\f
82
83/* Be carefull when this macro is true.
84 scm_gc_running_p is set during gc.
85 */
86#define SCM_GC_P (scm_gc_running_p)
87
88/* Macros that encapsulate blocks of code which can be called by the
89 * debugger.
90 */
91#define SCM_BEGIN_FOREIGN_BLOCK \
92do { \
93 old_gc = scm_block_gc; scm_block_gc = 1; \
94 scm_print_carefully_p = 1; \
95} while (0)
96
97
98#define SCM_END_FOREIGN_BLOCK \
99do { \
100 scm_print_carefully_p = 0; \
101 scm_block_gc = old_gc; \
102} while (0)
103
104
105#define RESET_STRING { gdb_output_length = 0; }
106
107#define SEND_STRING(str) \
108do { \
109 gdb_output = (char *) (str); \
110 gdb_output_length = strlen ((const char *) (str)); \
111} while (0)
112
113
114/* {Gdb interface}
115 */
116
117unsigned short gdb_options = GDB_HAVE_BINDINGS;
118
119char *gdb_language = "lisp/c";
120
121SCM gdb_result;
122
123char *gdb_output;
124
125int gdb_output_length;
126
127int scm_print_carefully_p;
128
129static SCM gdb_input_port;
130static int port_mark_p, stream_mark_p, string_mark_p;
131
132static SCM tok_buf;
133static int tok_buf_mark_p;
134
135static SCM gdb_output_port;
136static int old_gc;
137
138
139static void
140unmark_port (SCM port)
141{
142 SCM stream, string;
143 port_mark_p = SCM_GC_MARK_P (port);
144 SCM_CLEAR_GC_MARK (port);
145 stream = SCM_PACK (SCM_STREAM (port));
146 stream_mark_p = SCM_GC_MARK_P (stream);
147 SCM_CLEAR_GC_MARK (stream);
148 string = SCM_CDR (stream);
149 string_mark_p = SCM_GC_MARK_P (string);
150 SCM_CLEAR_GC_MARK (string);
151}
152
153
154static void
155remark_port (SCM port)
156{
157 SCM stream = SCM_PACK (SCM_STREAM (port));
158 SCM string = SCM_CDR (stream);
159 if (string_mark_p)
160 SCM_SET_GC_MARK (string);
161 if (stream_mark_p)
162 SCM_SET_GC_MARK (stream);
163 if (port_mark_p)
164 SCM_SET_GC_MARK (port);
165}
166
167
168int
169gdb_maybe_valid_type_p (SCM value)
170{
171 return SCM_IMP (value) || scm_in_heap_p (value);
172}
173
174
175int
176gdb_read (char *str)
177{
178 SCM ans;
179 int status = 0;
180 RESET_STRING;
181 /* Need to be restrictive about what to read? */
182 if (SCM_GC_P)
183 {
184 char *p;
185 for (p = str; *p != '\0'; ++p)
186 switch (*p)
187 {
188 case '(':
189 case '\'':
190 case '"':
191 SEND_STRING ("Can't read this kind of expressions during gc");
192 return -1;
193 case '#':
194 if (*++p == '\0')
195 goto premature;
196 if (*p == '\\')
197 {
198 if (*++p != '\0')
199 continue;
200 premature:
201 SEND_STRING ("Premature end of lisp expression");
202 return -1;
203 }
204 default:
205 continue;
206 }
207 }
208 SCM_BEGIN_FOREIGN_BLOCK;
209 unmark_port (gdb_input_port);
210 scm_seek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
211 scm_puts (str, gdb_input_port);
212 scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
213 scm_seek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
214 /* Read one object */
215 tok_buf_mark_p = SCM_GC_MARK_P (tok_buf);
216 SCM_CLEAR_GC_MARK (tok_buf);
217 ans = scm_lreadr (&tok_buf, gdb_input_port, &ans);
218 if (SCM_GC_P)
219 {
220 if (SCM_NIMP (ans))
221 {
222 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
223 status = -1;
224 goto exit;
225 }
226 }
227 gdb_result = ans;
228 /* Protect answer from future GC */
229 if (SCM_NIMP (ans))
230 scm_permanent_object (ans);
231exit:
232 if (tok_buf_mark_p)
233 SCM_SET_GC_MARK (tok_buf);
234 remark_port (gdb_input_port);
235 SCM_END_FOREIGN_BLOCK;
236 return status;
237}
238
239
240int
241gdb_eval (SCM exp)
242{
243 RESET_STRING;
244 if (SCM_IMP (exp))
245 {
246 gdb_result = exp;
247 return 0;
248 }
249 if (SCM_GC_P)
250 {
251 SEND_STRING ("Can't evaluate lisp expressions during gc");
252 return -1;
253 }
254 SCM_BEGIN_FOREIGN_BLOCK;
255 {
256 SCM env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
257 gdb_result = scm_permanent_object (scm_ceval (exp, env));
258 }
259 SCM_END_FOREIGN_BLOCK;
260 return 0;
261}
262
263
264int
265gdb_print (SCM obj)
266{
267 if (!scm_initialized_p)
268 SEND_STRING ("*** Guile not initialized ***");
269 else
270 {
271 RESET_STRING;
272 SCM_BEGIN_FOREIGN_BLOCK;
273 /* Reset stream */
274 scm_seek (gdb_output_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
275 scm_write (obj, gdb_output_port);
276 scm_truncate_file (gdb_output_port, SCM_UNDEFINED);
277 {
278 scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
279
280 scm_flush (gdb_output_port);
281 *(pt->write_buf + pt->read_buf_size) = 0;
282 SEND_STRING (pt->read_buf);
283 }
284 SCM_END_FOREIGN_BLOCK;
285 }
286 return 0;
287}
288
289
290int
291gdb_binding (SCM name, SCM value)
292{
293 RESET_STRING;
294 if (SCM_GC_P)
295 {
296 SEND_STRING ("Can't create new bindings during gc");
297 return -1;
298 }
299 SCM_BEGIN_FOREIGN_BLOCK;
300 {
301 SCM var = scm_sym2var (name, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_T);
302 SCM_VARIABLE_SET (var, value);
303 }
304 SCM_END_FOREIGN_BLOCK;
305 return 0;
306}
307
308void
309scm_init_gdbint ()
310{
311 static char *s = "scm_init_gdb_interface";
312 SCM port;
313
314 scm_print_carefully_p = 0;
315
316 port = scm_mkstrport (SCM_INUM0,
317 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
318 SCM_OPN | SCM_WRTNG,
319 s);
320 gdb_output_port = scm_permanent_object (port);
321
322 port = scm_mkstrport (SCM_INUM0,
323 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
324 SCM_OPN | SCM_RDNG | SCM_WRTNG,
325 s);
326 gdb_input_port = scm_permanent_object (port);
327
328 tok_buf = scm_permanent_object (scm_allocate_string (30));
329}
330
331/*
332 Local Variables:
333 c-file-style: "gnu"
334 End:
335*/