Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / libguile / gdbint.c
CommitLineData
4907ff5a 1/* GDB interface for Guile
c7fabadf 2 * Copyright (C) 1996,1997,1999,2000,2001, 2002 Free Software Foundation, Inc.
4907ff5a 3 *
73be1d9e
MV
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.
4907ff5a 8 *
73be1d9e 9 * This library is distributed in the hope that it will be useful,
4907ff5a 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
4907ff5a 13 *
73be1d9e
MV
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
17 */
4907ff5a 18
5ebbe4ef
RB
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
1bbd0b84 22
a0599745 23#include "libguile/_scm.h"
681b9005
MD
24
25#include <stdio.h>
783e7774 26#include <string.h>
681b9005
MD
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30
a0599745
MD
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"
7e73eaee 37#include "libguile/fluids.h"
a0599745 38#include "libguile/strings.h"
9293b3c6 39#include "libguile/init.h"
4907ff5a 40
a0599745 41#include "libguile/gdbint.h"
4907ff5a
MD
42\f
43/* {Support for debugging with gdb}
44 *
45 * TODO:
46 *
47 * 1. Redirect outputs
48 * 2. Catch errors
49 * 3. Prevent print from causing segmentation fault when given broken pairs
50 */
51
4907ff5a
MD
52#define GDB_TYPE SCM
53
a0599745 54#include "libguile/gdb_interface.h"
4907ff5a
MD
55
56\f
57
58/* Be carefull when this macro is true.
406c7d90 59 scm_gc_running_p is set during gc.
4907ff5a 60 */
406c7d90 61#define SCM_GC_P (scm_gc_running_p)
4907ff5a
MD
62
63/* Macros that encapsulate blocks of code which can be called by the
64 * debugger.
65 */
66#define SCM_BEGIN_FOREIGN_BLOCK \
d3a6bc94 67do { \
4907ff5a
MD
68 old_gc = scm_block_gc; scm_block_gc = 1; \
69 scm_print_carefully_p = 1; \
d3a6bc94 70} while (0)
4907ff5a
MD
71
72
73#define SCM_END_FOREIGN_BLOCK \
d3a6bc94 74do { \
4907ff5a
MD
75 scm_print_carefully_p = 0; \
76 scm_block_gc = old_gc; \
d3a6bc94 77} while (0)
4907ff5a
MD
78
79
80#define RESET_STRING { gdb_output_length = 0; }
81
82#define SEND_STRING(str) \
d3a6bc94 83do { \
a57c1cc7
MD
84 gdb_output = (char *) (str); \
85 gdb_output_length = strlen ((const char *) (str)); \
d3a6bc94 86} while (0)
380b6b4c 87
4907ff5a
MD
88
89/* {Gdb interface}
90 */
91
92unsigned short gdb_options = GDB_HAVE_BINDINGS;
93
94char *gdb_language = "lisp/c";
95
96SCM gdb_result;
97
98char *gdb_output;
99
100int gdb_output_length;
101
102int scm_print_carefully_p;
103
104static SCM gdb_input_port;
380b6b4c
MD
105static int port_mark_p, stream_mark_p, string_mark_p;
106
107static SCM tok_buf;
108static int tok_buf_mark_p;
109
4907ff5a 110static SCM gdb_output_port;
c7fabadf 111static int old_gc;
4907ff5a 112
1cc91f1b 113
380b6b4c 114static void
1bbd0b84 115unmark_port (SCM port)
380b6b4c
MD
116{
117 SCM stream, string;
c8a1bdc4
HWN
118 port_mark_p = SCM_GC_MARK_P (port);
119 SCM_CLEAR_GC_MARK (port);
74a16888 120 stream = SCM_PACK (SCM_STREAM (port));
c8a1bdc4
HWN
121 stream_mark_p = SCM_GC_MARK_P (stream);
122 SCM_CLEAR_GC_MARK (stream);
380b6b4c 123 string = SCM_CDR (stream);
c8a1bdc4
HWN
124 string_mark_p = SCM_GC_MARK_P (string);
125 SCM_CLEAR_GC_MARK (string);
380b6b4c
MD
126}
127
1cc91f1b 128
380b6b4c 129static void
1bbd0b84 130remark_port (SCM port)
380b6b4c 131{
74a16888 132 SCM stream = SCM_PACK (SCM_STREAM (port));
380b6b4c 133 SCM string = SCM_CDR (stream);
c8a1bdc4
HWN
134 if (string_mark_p)
135 SCM_SET_GC_MARK (string);
136 if (stream_mark_p)
137 SCM_SET_GC_MARK (stream);
138 if (port_mark_p)
139 SCM_SET_GC_MARK (port);
380b6b4c
MD
140}
141
1cc91f1b 142
4907ff5a 143int
6e8d25a6 144gdb_maybe_valid_type_p (SCM value)
4907ff5a 145{
c8a1bdc4 146 return SCM_IMP (value) || scm_in_heap_p (value);
4907ff5a
MD
147}
148
1cc91f1b 149
4907ff5a 150int
6e8d25a6 151gdb_read (char *str)
4907ff5a
MD
152{
153 SCM ans;
154 int status = 0;
155 RESET_STRING;
156 /* Need to be restrictive about what to read? */
157 if (SCM_GC_P)
158 {
159 char *p;
160 for (p = str; *p != '\0'; ++p)
161 switch (*p)
162 {
163 case '(':
164 case '\'':
165 case '"':
166 SEND_STRING ("Can't read this kind of expressions during gc");
167 return -1;
168 case '#':
169 if (*++p == '\0')
170 goto premature;
171 if (*p == '\\')
172 {
173 if (*++p != '\0')
174 continue;
175 premature:
176 SEND_STRING ("Premature end of lisp expression");
177 return -1;
178 }
179 default:
180 continue;
181 }
182 }
183 SCM_BEGIN_FOREIGN_BLOCK;
380b6b4c 184 unmark_port (gdb_input_port);
c94577b4 185 scm_seek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
840ae05d 186 scm_puts (str, gdb_input_port);
69bc9ff3 187 scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
c94577b4 188 scm_seek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
4907ff5a 189 /* Read one object */
c8a1bdc4
HWN
190 tok_buf_mark_p = SCM_GC_MARK_P (tok_buf);
191 SCM_CLEAR_GC_MARK (tok_buf);
deca31e1 192 ans = scm_lreadr (&tok_buf, gdb_input_port, &ans);
4907ff5a
MD
193 if (SCM_GC_P)
194 {
380b6b4c 195 if (SCM_NIMP (ans))
4907ff5a
MD
196 {
197 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
198 status = -1;
199 goto exit;
200 }
201 }
380b6b4c 202 gdb_result = ans;
4907ff5a 203 /* Protect answer from future GC */
380b6b4c
MD
204 if (SCM_NIMP (ans))
205 scm_permanent_object (ans);
4907ff5a 206exit:
380b6b4c 207 if (tok_buf_mark_p)
c8a1bdc4 208 SCM_SET_GC_MARK (tok_buf);
380b6b4c 209 remark_port (gdb_input_port);
4907ff5a
MD
210 SCM_END_FOREIGN_BLOCK;
211 return status;
212}
213
1cc91f1b 214
4907ff5a 215int
6e8d25a6 216gdb_eval (SCM exp)
4907ff5a
MD
217{
218 RESET_STRING;
219 if (SCM_IMP (exp))
220 {
221 gdb_result = exp;
222 return 0;
223 }
224 if (SCM_GC_P)
225 {
226 SEND_STRING ("Can't evaluate lisp expressions during gc");
227 return -1;
228 }
229 SCM_BEGIN_FOREIGN_BLOCK;
230 {
7e73eaee 231 SCM env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
4907ff5a
MD
232 gdb_result = scm_permanent_object (scm_ceval (exp, env));
233 }
234 SCM_END_FOREIGN_BLOCK;
235 return 0;
236}
237
1cc91f1b 238
4907ff5a 239int
6e8d25a6 240gdb_print (SCM obj)
4907ff5a 241{
9293b3c6
MD
242 if (!scm_initialized_p)
243 SEND_STRING ("*** Guile not initialized ***");
244 else
245 {
246 RESET_STRING;
247 SCM_BEGIN_FOREIGN_BLOCK;
248 /* Reset stream */
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);
252 {
92c2555f 253 scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
9293b3c6
MD
254
255 scm_flush (gdb_output_port);
256 *(pt->write_buf + pt->read_buf_size) = 0;
257 SEND_STRING (pt->read_buf);
258 }
259 SCM_END_FOREIGN_BLOCK;
260 }
4907ff5a
MD
261 return 0;
262}
263
1cc91f1b 264
4907ff5a 265int
6e8d25a6 266gdb_binding (SCM name, SCM value)
4907ff5a
MD
267{
268 RESET_STRING;
269 if (SCM_GC_P)
270 {
271 SEND_STRING ("Can't create new bindings during gc");
272 return -1;
273 }
274 SCM_BEGIN_FOREIGN_BLOCK;
275 {
86d31dfe
MV
276 SCM var = scm_sym2var (name, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_T);
277 SCM_VARIABLE_SET (var, value);
4907ff5a
MD
278 }
279 SCM_END_FOREIGN_BLOCK;
280 return 0;
281}
282
283void
284scm_init_gdbint ()
285{
286 static char *s = "scm_init_gdb_interface";
287 SCM port;
288
289 scm_print_carefully_p = 0;
290
291 port = scm_mkstrport (SCM_INUM0,
840ae05d 292 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
4907ff5a
MD
293 SCM_OPN | SCM_WRTNG,
294 s);
295 gdb_output_port = scm_permanent_object (port);
296
297 port = scm_mkstrport (SCM_INUM0,
298 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
840ae05d 299 SCM_OPN | SCM_RDNG | SCM_WRTNG,
4907ff5a
MD
300 s);
301 gdb_input_port = scm_permanent_object (port);
380b6b4c 302
be54b15d 303 tok_buf = scm_permanent_object (scm_allocate_string (30));
4907ff5a 304}
89e00824
ML
305
306/*
307 Local Variables:
308 c-file-style: "gnu"
309 End:
310*/