1 /* GDB interface for Guile
2 * Copyright (C) 1996,1997,1999,2000,2001,2002,2004,2009,2011,2012
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 SCM gdb_output_port
;
109 gdb_maybe_valid_type_p (SCM value
)
111 return SCM_IMP (value
); /* || scm_in_heap_p (value); */ /* FIXME: What to
123 /* Need to be restrictive about what to read? */
124 if (1) /* (SCM_GC_P) */ /* FIXME */
127 for (p
= str
; *p
!= '\0'; ++p
)
133 SEND_STRING ("Can't read this kind of expressions during gc");
143 SEND_STRING ("Premature end of lisp expression");
150 SCM_BEGIN_FOREIGN_BLOCK
;
151 unmark_port (gdb_input_port
);
152 scm_seek (gdb_input_port
, SCM_INUM0
, scm_from_int (SEEK_SET
));
153 scm_puts_unlocked (str
, gdb_input_port
);
154 scm_truncate_file (gdb_input_port
, SCM_UNDEFINED
);
155 scm_seek (gdb_input_port
, SCM_INUM0
, scm_from_int (SEEK_SET
));
157 /* Read one object */
158 ans
= scm_read (gdb_input_port
);
161 if (SCM_HEAP_OBJECT_P (ans
))
163 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
169 /* Protect answer from future GC (FIXME: still needed with BDW-GC?) */
170 if (SCM_HEAP_OBJECT_P (ans
))
171 scm_permanent_object (ans
);
173 remark_port (gdb_input_port
);
174 SCM_END_FOREIGN_BLOCK
;
188 SEND_STRING ("Can't evaluate lisp expressions during gc");
191 SCM_BEGIN_FOREIGN_BLOCK
;
193 gdb_result
= scm_permanent_object (scm_primitive_eval (exp
));
195 SCM_END_FOREIGN_BLOCK
;
203 if (!scm_initialized_p
)
204 SEND_STRING ("*** Guile not initialized ***");
208 SCM_BEGIN_FOREIGN_BLOCK
;
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
);
214 scm_t_port
*pt
= SCM_PTAB_ENTRY (gdb_output_port
);
216 scm_flush_unlocked (gdb_output_port
);
217 *(pt
->write_buf
+ pt
->read_buf_size
) = 0;
218 SEND_STRING (pt
->read_buf
);
220 SCM_END_FOREIGN_BLOCK
;
227 gdb_binding (SCM name
, SCM value
)
232 SEND_STRING ("Can't create new bindings during gc");
235 SCM_BEGIN_FOREIGN_BLOCK
;
237 scm_define (name
, value
);
239 SCM_END_FOREIGN_BLOCK
;
246 static char *s
= "scm_init_gdb_interface";
249 scm_print_carefully_p
= 0;
251 port
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
254 gdb_output_port
= scm_permanent_object (port
);
256 port
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
257 SCM_OPN
| SCM_RDNG
| SCM_WRTNG
,
259 gdb_input_port
= scm_permanent_object (port
);