Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / gdbint.c
1 /* GDB interface for Guile
2 * Copyright (C) 1996,1997,1999,2000,2001,2002,2004,2009,2011,2012
3 * Free Software Foundation, Inc.
4 *
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.
9 *
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.
14 *
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
18 * 02110-1301 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/_scm.h"
26
27 #include <stdio.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
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"
42
43 #include "libguile/gdbint.h"
44 \f
45 /* {Support for debugging with gdb}
46 *
47 * TODO:
48 *
49 * 1. Redirect outputs
50 * 2. Catch errors
51 * 3. Prevent print from causing segmentation fault when given broken pairs
52 */
53
54 #define GDB_TYPE SCM
55
56 #include "libguile/gdb_interface.h"
57
58 \f
59
60 /* Be carefull when this macro is true.
61 scm_gc_running_p is set during gc.
62 */
63 #define SCM_GC_P (scm_gc_running_p)
64
65 /* Macros that encapsulate blocks of code which can be called by the
66 * debugger.
67 */
68 #define SCM_BEGIN_FOREIGN_BLOCK \
69 do { \
70 scm_print_carefully_p = 1; \
71 } while (0)
72
73
74 #define SCM_END_FOREIGN_BLOCK \
75 do { \
76 scm_print_carefully_p = 0; \
77 } while (0)
78
79
80 #define RESET_STRING { gdb_output_length = 0; }
81
82 #define SEND_STRING(str) \
83 do { \
84 gdb_output = (char *) (str); \
85 gdb_output_length = strlen ((const char *) (str)); \
86 } while (0)
87
88
89 /* {Gdb interface}
90 */
91
92 unsigned short gdb_options = GDB_HAVE_BINDINGS;
93
94 char *gdb_language = "lisp/c";
95
96 SCM gdb_result;
97
98 char *gdb_output;
99
100 int gdb_output_length;
101
102 int scm_print_carefully_p;
103
104 static SCM gdb_input_port;
105 static SCM gdb_output_port;
106
107
108 int
109 gdb_maybe_valid_type_p (SCM value)
110 {
111 return SCM_IMP (value); /* || scm_in_heap_p (value); */ /* FIXME: What to
112 do? */
113 }
114
115
116 int
117 gdb_read (char *str)
118 {
119 #if 0
120 SCM ans;
121 int status = 0;
122 RESET_STRING;
123 /* Need to be restrictive about what to read? */
124 if (1) /* (SCM_GC_P) */ /* FIXME */
125 {
126 char *p;
127 for (p = str; *p != '\0'; ++p)
128 switch (*p)
129 {
130 case '(':
131 case '\'':
132 case '"':
133 SEND_STRING ("Can't read this kind of expressions during gc");
134 return -1;
135 case '#':
136 if (*++p == '\0')
137 goto premature;
138 if (*p == '\\')
139 {
140 if (*++p != '\0')
141 continue;
142 premature:
143 SEND_STRING ("Premature end of lisp expression");
144 return -1;
145 }
146 default:
147 continue;
148 }
149 }
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));
156
157 /* Read one object */
158 ans = scm_read (gdb_input_port);
159 if (SCM_GC_P)
160 {
161 if (SCM_HEAP_OBJECT_P (ans))
162 {
163 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
164 status = -1;
165 goto exit;
166 }
167 }
168 gdb_result = ans;
169 /* Protect answer from future GC (FIXME: still needed with BDW-GC?) */
170 if (SCM_HEAP_OBJECT_P (ans))
171 scm_permanent_object (ans);
172 exit:
173 remark_port (gdb_input_port);
174 SCM_END_FOREIGN_BLOCK;
175 return status;
176 #else
177 abort ();
178 #endif
179 }
180
181
182 int
183 gdb_eval (SCM exp)
184 {
185 RESET_STRING;
186 if (SCM_GC_P)
187 {
188 SEND_STRING ("Can't evaluate lisp expressions during gc");
189 return -1;
190 }
191 SCM_BEGIN_FOREIGN_BLOCK;
192 {
193 gdb_result = scm_permanent_object (scm_primitive_eval (exp));
194 }
195 SCM_END_FOREIGN_BLOCK;
196 return 0;
197 }
198
199
200 int
201 gdb_print (SCM obj)
202 {
203 if (!scm_initialized_p)
204 SEND_STRING ("*** Guile not initialized ***");
205 else
206 {
207 RESET_STRING;
208 SCM_BEGIN_FOREIGN_BLOCK;
209 /* Reset stream */
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);
213 {
214 scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
215
216 scm_flush_unlocked (gdb_output_port);
217 *(pt->write_buf + pt->read_buf_size) = 0;
218 SEND_STRING (pt->read_buf);
219 }
220 SCM_END_FOREIGN_BLOCK;
221 }
222 return 0;
223 }
224
225
226 int
227 gdb_binding (SCM name, SCM value)
228 {
229 RESET_STRING;
230 if (SCM_GC_P)
231 {
232 SEND_STRING ("Can't create new bindings during gc");
233 return -1;
234 }
235 SCM_BEGIN_FOREIGN_BLOCK;
236 {
237 scm_define (name, value);
238 }
239 SCM_END_FOREIGN_BLOCK;
240 return 0;
241 }
242
243 void
244 scm_init_gdbint ()
245 {
246 static char *s = "scm_init_gdb_interface";
247 SCM port;
248
249 scm_print_carefully_p = 0;
250
251 port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
252 SCM_OPN | SCM_WRTNG,
253 s);
254 gdb_output_port = scm_permanent_object (port);
255
256 port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
257 SCM_OPN | SCM_RDNG | SCM_WRTNG,
258 s);
259 gdb_input_port = scm_permanent_object (port);
260 }
261
262 /*
263 Local Variables:
264 c-file-style: "gnu"
265 End:
266 */