1 /* GDB interface for Guile
2 * Copyright (C) 1996,1997,1999,2000,2001, 2002 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "libguile/_scm.h"
31 #include "libguile/strports.h"
32 #include "libguile/read.h"
33 #include "libguile/eval.h"
34 #include "libguile/chars.h"
35 #include "libguile/modules.h"
36 #include "libguile/ports.h"
37 #include "libguile/fluids.h"
38 #include "libguile/strings.h"
39 #include "libguile/init.h"
41 #include "libguile/gdbint.h"
43 /* {Support for debugging with gdb}
49 * 3. Prevent print from causing segmentation fault when given broken pairs
54 #include "libguile/gdb_interface.h"
58 /* Be carefull when this macro is true.
59 scm_gc_running_p is set during gc.
61 #define SCM_GC_P (scm_gc_running_p)
63 /* Macros that encapsulate blocks of code which can be called by the
66 #define SCM_BEGIN_FOREIGN_BLOCK \
68 old_gc = scm_block_gc; scm_block_gc = 1; \
69 scm_print_carefully_p = 1; \
73 #define SCM_END_FOREIGN_BLOCK \
75 scm_print_carefully_p = 0; \
76 scm_block_gc = old_gc; \
80 #define RESET_STRING { gdb_output_length = 0; }
82 #define SEND_STRING(str) \
84 gdb_output = (char *) (str); \
85 gdb_output_length = strlen ((const char *) (str)); \
92 unsigned short gdb_options
= GDB_HAVE_BINDINGS
;
94 char *gdb_language
= "lisp/c";
100 int gdb_output_length
;
102 int scm_print_carefully_p
;
104 static SCM gdb_input_port
;
105 static int port_mark_p
, stream_mark_p
, string_mark_p
;
108 static int tok_buf_mark_p
;
110 static SCM gdb_output_port
;
115 unmark_port (SCM port
)
118 port_mark_p
= SCM_GC_MARK_P (port
);
119 SCM_CLEAR_GC_MARK (port
);
120 stream
= SCM_PACK (SCM_STREAM (port
));
121 stream_mark_p
= SCM_GC_MARK_P (stream
);
122 SCM_CLEAR_GC_MARK (stream
);
123 string
= SCM_CDR (stream
);
124 string_mark_p
= SCM_GC_MARK_P (string
);
125 SCM_CLEAR_GC_MARK (string
);
130 remark_port (SCM port
)
132 SCM stream
= SCM_PACK (SCM_STREAM (port
));
133 SCM string
= SCM_CDR (stream
);
135 SCM_SET_GC_MARK (string
);
137 SCM_SET_GC_MARK (stream
);
139 SCM_SET_GC_MARK (port
);
144 gdb_maybe_valid_type_p (SCM value
)
146 return SCM_IMP (value
) || scm_in_heap_p (value
);
156 /* Need to be restrictive about what to read? */
160 for (p
= str
; *p
!= '\0'; ++p
)
166 SEND_STRING ("Can't read this kind of expressions during gc");
176 SEND_STRING ("Premature end of lisp expression");
183 SCM_BEGIN_FOREIGN_BLOCK
;
184 unmark_port (gdb_input_port
);
185 scm_seek (gdb_input_port
, SCM_INUM0
, SCM_MAKINUM (SEEK_SET
));
186 scm_puts (str
, gdb_input_port
);
187 scm_truncate_file (gdb_input_port
, SCM_UNDEFINED
);
188 scm_seek (gdb_input_port
, SCM_INUM0
, SCM_MAKINUM (SEEK_SET
));
189 /* Read one object */
190 tok_buf_mark_p
= SCM_GC_MARK_P (tok_buf
);
191 SCM_CLEAR_GC_MARK (tok_buf
);
192 ans
= scm_lreadr (&tok_buf
, gdb_input_port
, &ans
);
197 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
203 /* Protect answer from future GC */
205 scm_permanent_object (ans
);
208 SCM_SET_GC_MARK (tok_buf
);
209 remark_port (gdb_input_port
);
210 SCM_END_FOREIGN_BLOCK
;
226 SEND_STRING ("Can't evaluate lisp expressions during gc");
229 SCM_BEGIN_FOREIGN_BLOCK
;
231 SCM env
= scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE
);
232 gdb_result
= scm_permanent_object (scm_ceval (exp
, env
));
234 SCM_END_FOREIGN_BLOCK
;
242 if (!scm_initialized_p
)
243 SEND_STRING ("*** Guile not initialized ***");
247 SCM_BEGIN_FOREIGN_BLOCK
;
249 scm_seek (gdb_output_port
, SCM_INUM0
, SCM_MAKINUM (SEEK_SET
));
250 scm_write (obj
, gdb_output_port
);
251 scm_truncate_file (gdb_output_port
, SCM_UNDEFINED
);
253 scm_t_port
*pt
= SCM_PTAB_ENTRY (gdb_output_port
);
255 scm_flush (gdb_output_port
);
256 *(pt
->write_buf
+ pt
->read_buf_size
) = 0;
257 SEND_STRING (pt
->read_buf
);
259 SCM_END_FOREIGN_BLOCK
;
266 gdb_binding (SCM name
, SCM value
)
271 SEND_STRING ("Can't create new bindings during gc");
274 SCM_BEGIN_FOREIGN_BLOCK
;
276 SCM var
= scm_sym2var (name
, SCM_TOP_LEVEL_LOOKUP_CLOSURE
, SCM_BOOL_T
);
277 SCM_VARIABLE_SET (var
, value
);
279 SCM_END_FOREIGN_BLOCK
;
286 static char *s
= "scm_init_gdb_interface";
289 scm_print_carefully_p
= 0;
291 port
= scm_mkstrport (SCM_INUM0
,
292 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED
),
295 gdb_output_port
= scm_permanent_object (port
);
297 port
= scm_mkstrport (SCM_INUM0
,
298 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED
),
299 SCM_OPN
| SCM_RDNG
| SCM_WRTNG
,
301 gdb_input_port
= scm_permanent_object (port
);
303 tok_buf
= scm_permanent_object (scm_allocate_string (30));