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