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 License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "libguile/_scm.h"
33 #include "libguile/strports.h"
34 #include "libguile/read.h"
35 #include "libguile/eval.h"
36 #include "libguile/chars.h"
37 #include "libguile/modules.h"
38 #include "libguile/ports.h"
39 #include "libguile/fluids.h"
40 #include "libguile/strings.h"
41 #include "libguile/init.h"
43 #include "libguile/gdbint.h"
45 /* {Support for debugging with gdb}
51 * 3. Prevent print from causing segmentation fault when given broken pairs
56 #include "libguile/gdb_interface.h"
60 /* Be carefull when this macro is true.
61 scm_gc_running_p is set during gc.
63 #define SCM_GC_P (scm_gc_running_p)
65 /* Macros that encapsulate blocks of code which can be called by the
68 #define SCM_BEGIN_FOREIGN_BLOCK \
70 scm_print_carefully_p = 1; \
74 #define SCM_END_FOREIGN_BLOCK \
76 scm_print_carefully_p = 0; \
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
;
107 static SCM gdb_output_port
;
111 unmark_port (SCM port
)
114 port_mark_p
= SCM_GC_MARK_P (port
);
115 SCM_CLEAR_GC_MARK (port
);
116 stream
= SCM_PACK (SCM_STREAM (port
));
117 stream_mark_p
= SCM_GC_MARK_P (stream
);
118 SCM_CLEAR_GC_MARK (stream
);
119 string
= SCM_CDR (stream
);
120 string_mark_p
= SCM_GC_MARK_P (string
);
121 SCM_CLEAR_GC_MARK (string
);
126 remark_port (SCM port
)
128 SCM stream
= SCM_PACK (SCM_STREAM (port
));
129 SCM string
= SCM_CDR (stream
);
131 SCM_SET_GC_MARK (string
);
133 SCM_SET_GC_MARK (stream
);
135 SCM_SET_GC_MARK (port
);
140 gdb_maybe_valid_type_p (SCM value
)
142 return SCM_IMP (value
) || scm_in_heap_p (value
);
152 /* Need to be restrictive about what to read? */
156 for (p
= str
; *p
!= '\0'; ++p
)
162 SEND_STRING ("Can't read this kind of expressions during gc");
172 SEND_STRING ("Premature end of lisp expression");
179 SCM_BEGIN_FOREIGN_BLOCK
;
180 unmark_port (gdb_input_port
);
181 scm_seek (gdb_input_port
, SCM_INUM0
, scm_from_int (SEEK_SET
));
182 scm_puts (str
, gdb_input_port
);
183 scm_truncate_file (gdb_input_port
, SCM_UNDEFINED
);
184 scm_seek (gdb_input_port
, SCM_INUM0
, scm_from_int (SEEK_SET
));
186 /* Read one object */
187 ans
= scm_read (gdb_input_port
);
192 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
198 /* Protect answer from future GC */
200 scm_permanent_object (ans
);
202 remark_port (gdb_input_port
);
203 SCM_END_FOREIGN_BLOCK
;
214 SEND_STRING ("Can't evaluate lisp expressions during gc");
217 SCM_BEGIN_FOREIGN_BLOCK
;
219 SCM env
= scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE
);
220 gdb_result
= scm_permanent_object (scm_i_eval_x (exp
, env
));
222 SCM_END_FOREIGN_BLOCK
;
230 if (!scm_initialized_p
)
231 SEND_STRING ("*** Guile not initialized ***");
235 SCM_BEGIN_FOREIGN_BLOCK
;
237 scm_seek (gdb_output_port
, SCM_INUM0
, scm_from_int (SEEK_SET
));
238 scm_write (obj
, gdb_output_port
);
239 scm_truncate_file (gdb_output_port
, SCM_UNDEFINED
);
241 scm_t_port
*pt
= SCM_PTAB_ENTRY (gdb_output_port
);
243 scm_flush (gdb_output_port
);
244 *(pt
->write_buf
+ pt
->read_buf_size
) = 0;
245 SEND_STRING (pt
->read_buf
);
247 SCM_END_FOREIGN_BLOCK
;
254 gdb_binding (SCM name
, SCM value
)
259 SEND_STRING ("Can't create new bindings during gc");
262 SCM_BEGIN_FOREIGN_BLOCK
;
264 SCM var
= scm_sym2var (name
, SCM_TOP_LEVEL_LOOKUP_CLOSURE
, SCM_BOOL_T
);
265 SCM_VARIABLE_SET (var
, value
);
267 SCM_END_FOREIGN_BLOCK
;
274 static char *s
= "scm_init_gdb_interface";
277 scm_print_carefully_p
= 0;
279 port
= scm_mkstrport (SCM_INUM0
,
280 scm_c_make_string (0, SCM_UNDEFINED
),
283 gdb_output_port
= scm_permanent_object (port
);
285 port
= scm_mkstrport (SCM_INUM0
,
286 scm_c_make_string (0, SCM_UNDEFINED
),
287 SCM_OPN
| SCM_RDNG
| SCM_WRTNG
,
289 gdb_input_port
= scm_permanent_object (port
);