1 /* GDB interface for Guile
2 * Copyright (C) 1996,1997,1999,2000,2001,2002,2004
3 * Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "libguile/_scm.h"
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"
42 #include "libguile/gdbint.h"
44 /* {Support for debugging with gdb}
50 * 3. Prevent print from causing segmentation fault when given broken pairs
55 #include "libguile/gdb_interface.h"
59 /* Be carefull when this macro is true.
60 scm_gc_running_p is set during gc.
62 #define SCM_GC_P (scm_gc_running_p)
64 /* Macros that encapsulate blocks of code which can be called by the
67 #define SCM_BEGIN_FOREIGN_BLOCK \
69 old_gc = scm_block_gc; scm_block_gc = 1; \
70 scm_print_carefully_p = 1; \
74 #define SCM_END_FOREIGN_BLOCK \
76 scm_print_carefully_p = 0; \
77 scm_block_gc = old_gc; \
81 #define RESET_STRING { gdb_output_length = 0; }
83 #define SEND_STRING(str) \
85 gdb_output = (char *) (str); \
86 gdb_output_length = strlen ((const char *) (str)); \
93 unsigned short gdb_options
= GDB_HAVE_BINDINGS
;
95 char *gdb_language
= "lisp/c";
101 int gdb_output_length
;
103 int scm_print_carefully_p
;
105 static SCM gdb_input_port
;
106 static int port_mark_p
, stream_mark_p
, string_mark_p
;
109 static int tok_buf_mark_p
;
111 static SCM gdb_output_port
;
116 unmark_port (SCM port
)
119 port_mark_p
= SCM_GC_MARK_P (port
);
120 SCM_CLEAR_GC_MARK (port
);
121 stream
= SCM_PACK (SCM_STREAM (port
));
122 stream_mark_p
= SCM_GC_MARK_P (stream
);
123 SCM_CLEAR_GC_MARK (stream
);
124 string
= SCM_CDR (stream
);
125 string_mark_p
= SCM_GC_MARK_P (string
);
126 SCM_CLEAR_GC_MARK (string
);
131 remark_port (SCM port
)
133 SCM stream
= SCM_PACK (SCM_STREAM (port
));
134 SCM string
= SCM_CDR (stream
);
136 SCM_SET_GC_MARK (string
);
138 SCM_SET_GC_MARK (stream
);
140 SCM_SET_GC_MARK (port
);
145 gdb_maybe_valid_type_p (SCM value
)
147 return SCM_IMP (value
) || scm_in_heap_p (value
);
157 /* Need to be restrictive about what to read? */
161 for (p
= str
; *p
!= '\0'; ++p
)
167 SEND_STRING ("Can't read this kind of expressions during gc");
177 SEND_STRING ("Premature end of lisp expression");
184 SCM_BEGIN_FOREIGN_BLOCK
;
185 unmark_port (gdb_input_port
);
186 scm_seek (gdb_input_port
, SCM_INUM0
, SCM_I_MAKINUM (SEEK_SET
));
187 scm_puts (str
, gdb_input_port
);
188 scm_truncate_file (gdb_input_port
, SCM_UNDEFINED
);
189 scm_seek (gdb_input_port
, SCM_INUM0
, SCM_I_MAKINUM (SEEK_SET
));
190 /* Read one object */
191 tok_buf_mark_p
= SCM_GC_MARK_P (tok_buf
);
192 SCM_CLEAR_GC_MARK (tok_buf
);
193 ans
= scm_lreadr (&tok_buf
, gdb_input_port
, &ans
);
198 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
204 /* Protect answer from future GC */
206 scm_permanent_object (ans
);
209 SCM_SET_GC_MARK (tok_buf
);
210 remark_port (gdb_input_port
);
211 SCM_END_FOREIGN_BLOCK
;
222 SEND_STRING ("Can't evaluate lisp expressions during gc");
225 SCM_BEGIN_FOREIGN_BLOCK
;
227 SCM env
= scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE
);
228 gdb_result
= scm_permanent_object (scm_i_eval_x (exp
, env
));
230 SCM_END_FOREIGN_BLOCK
;
238 if (!scm_initialized_p
)
239 SEND_STRING ("*** Guile not initialized ***");
243 SCM_BEGIN_FOREIGN_BLOCK
;
245 scm_seek (gdb_output_port
, SCM_INUM0
, SCM_I_MAKINUM (SEEK_SET
));
246 scm_write (obj
, gdb_output_port
);
247 scm_truncate_file (gdb_output_port
, SCM_UNDEFINED
);
249 scm_t_port
*pt
= SCM_PTAB_ENTRY (gdb_output_port
);
251 scm_flush (gdb_output_port
);
252 *(pt
->write_buf
+ pt
->read_buf_size
) = 0;
253 SEND_STRING (pt
->read_buf
);
255 SCM_END_FOREIGN_BLOCK
;
262 gdb_binding (SCM name
, SCM value
)
267 SEND_STRING ("Can't create new bindings during gc");
270 SCM_BEGIN_FOREIGN_BLOCK
;
272 SCM var
= scm_sym2var (name
, SCM_TOP_LEVEL_LOOKUP_CLOSURE
, SCM_BOOL_T
);
273 SCM_VARIABLE_SET (var
, value
);
275 SCM_END_FOREIGN_BLOCK
;
282 static char *s
= "scm_init_gdb_interface";
285 scm_print_carefully_p
= 0;
287 port
= scm_mkstrport (SCM_INUM0
,
288 scm_make_string (SCM_I_MAKINUM (0), SCM_UNDEFINED
),
291 gdb_output_port
= scm_permanent_object (port
);
293 port
= scm_mkstrport (SCM_INUM0
,
294 scm_make_string (SCM_I_MAKINUM (0), SCM_UNDEFINED
),
295 SCM_OPN
| SCM_RDNG
| SCM_WRTNG
,
297 gdb_input_port
= scm_permanent_object (port
);
299 tok_buf
= scm_permanent_object (scm_allocate_string (30));