* *.[hc]: add Emacs magic at the end of file, to ensure GNU
[bpt/guile.git] / libguile / gdbint.c
CommitLineData
4907ff5a 1/* GDB interface for Guile
62f36a0d 2 * Copyright (C) 1996, 1997, 1999 Free Software Foundation
4907ff5a
MD
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; see the file COPYING. If not, write to
82892bed
JB
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
4907ff5a
MD
18 *
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
21 *
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
27 *
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
30 *
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
38 *
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
42 *
43 * The author can be reached at djurfeldt@nada.kth.se
82892bed 44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
4907ff5a 45
1bbd0b84
GB
46/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48
49
4907ff5a 50#include "_scm.h"
681b9005
MD
51
52#include <stdio.h>
53#ifdef HAVE_UNISTD_H
54#include <unistd.h>
55#endif
56
20e6290e
JB
57#include "tag.h"
58#include "strports.h"
59#include "read.h"
20e6290e
JB
60#include "eval.h"
61#include "chars.h"
bcf12299 62#include "modules.h"
f04d8caf 63#include "ports.h"
ba11fd4c 64#include "root.h"
7ab3fdd5 65#include "strings.h"
4907ff5a 66
20e6290e 67#include "gdbint.h"
4907ff5a
MD
68\f
69/* {Support for debugging with gdb}
70 *
71 * TODO:
72 *
73 * 1. Redirect outputs
74 * 2. Catch errors
75 * 3. Prevent print from causing segmentation fault when given broken pairs
76 */
77
4907ff5a
MD
78#define GDB_TYPE SCM
79
80#include "gdb_interface.h"
81
82\f
83
84/* Be carefull when this macro is true.
85 scm_gc_heap_lock is set during gc.
86 */
87#define SCM_GC_P (scm_gc_heap_lock)
88
89/* Macros that encapsulate blocks of code which can be called by the
90 * debugger.
91 */
92#define SCM_BEGIN_FOREIGN_BLOCK \
d3a6bc94 93do { \
4907ff5a
MD
94 old_ints = scm_ints_disabled; scm_ints_disabled = 1; \
95 old_gc = scm_block_gc; scm_block_gc = 1; \
96 scm_print_carefully_p = 1; \
d3a6bc94 97} while (0)
4907ff5a
MD
98
99
100#define SCM_END_FOREIGN_BLOCK \
d3a6bc94 101do { \
4907ff5a
MD
102 scm_print_carefully_p = 0; \
103 scm_block_gc = old_gc; \
104 scm_ints_disabled = old_ints; \
d3a6bc94 105} while (0)
4907ff5a
MD
106
107
108#define RESET_STRING { gdb_output_length = 0; }
109
110#define SEND_STRING(str) \
d3a6bc94 111do { \
4907ff5a
MD
112 gdb_output = str; \
113 gdb_output_length = strlen (str); \
d3a6bc94 114} while (0)
380b6b4c 115
4907ff5a
MD
116
117/* {Gdb interface}
118 */
119
120unsigned short gdb_options = GDB_HAVE_BINDINGS;
121
122char *gdb_language = "lisp/c";
123
124SCM gdb_result;
125
126char *gdb_output;
127
128int gdb_output_length;
129
130int scm_print_carefully_p;
131
132static SCM gdb_input_port;
380b6b4c
MD
133static int port_mark_p, stream_mark_p, string_mark_p;
134
135static SCM tok_buf;
136static int tok_buf_mark_p;
137
4907ff5a
MD
138static SCM gdb_output_port;
139static int old_ints, old_gc;
140
1cc91f1b 141
380b6b4c 142static void
1bbd0b84 143unmark_port (SCM port)
380b6b4c
MD
144{
145 SCM stream, string;
146 port_mark_p = SCM_GC8MARKP (port);
147 SCM_CLRGC8MARK (port);
148 stream = SCM_STREAM (port);
149 stream_mark_p = SCM_GCMARKP (stream);
150 SCM_CLRGCMARK (stream);
151 string = SCM_CDR (stream);
152 string_mark_p = SCM_GC8MARKP (string);
153 SCM_CLRGC8MARK (string);
154}
155
1cc91f1b 156
380b6b4c 157static void
1bbd0b84 158remark_port (SCM port)
380b6b4c
MD
159{
160 SCM stream = SCM_STREAM (port);
161 SCM string = SCM_CDR (stream);
162 if (string_mark_p) SCM_SETGC8MARK (string);
163 if (stream_mark_p) SCM_SETGCMARK (stream);
164 if (port_mark_p) SCM_SETGC8MARK (port);
165}
166
1cc91f1b 167
4907ff5a 168int
6e8d25a6 169gdb_maybe_valid_type_p (SCM value)
4907ff5a
MD
170{
171 if (SCM_IMP (value) || scm_cellp (value))
172 return scm_tag (value) != SCM_MAKINUM (-1);
173 return 0;
174}
175
1cc91f1b 176
4907ff5a 177int
6e8d25a6 178gdb_read (char *str)
4907ff5a
MD
179{
180 SCM ans;
181 int status = 0;
182 RESET_STRING;
183 /* Need to be restrictive about what to read? */
184 if (SCM_GC_P)
185 {
186 char *p;
187 for (p = str; *p != '\0'; ++p)
188 switch (*p)
189 {
190 case '(':
191 case '\'':
192 case '"':
193 SEND_STRING ("Can't read this kind of expressions during gc");
194 return -1;
195 case '#':
196 if (*++p == '\0')
197 goto premature;
198 if (*p == '\\')
199 {
200 if (*++p != '\0')
201 continue;
202 premature:
203 SEND_STRING ("Premature end of lisp expression");
204 return -1;
205 }
206 default:
207 continue;
208 }
209 }
210 SCM_BEGIN_FOREIGN_BLOCK;
380b6b4c 211 unmark_port (gdb_input_port);
c94577b4 212 scm_seek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
840ae05d 213 scm_puts (str, gdb_input_port);
69bc9ff3 214 scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
c94577b4 215 scm_seek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
4907ff5a 216 /* Read one object */
380b6b4c
MD
217 tok_buf_mark_p = SCM_GC8MARKP (tok_buf);
218 SCM_CLRGC8MARK (tok_buf);
deca31e1 219 ans = scm_lreadr (&tok_buf, gdb_input_port, &ans);
4907ff5a
MD
220 if (SCM_GC_P)
221 {
380b6b4c 222 if (SCM_NIMP (ans))
4907ff5a
MD
223 {
224 SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
225 status = -1;
226 goto exit;
227 }
228 }
380b6b4c 229 gdb_result = ans;
4907ff5a 230 /* Protect answer from future GC */
380b6b4c
MD
231 if (SCM_NIMP (ans))
232 scm_permanent_object (ans);
4907ff5a 233exit:
380b6b4c
MD
234 if (tok_buf_mark_p)
235 SCM_SETGC8MARK (tok_buf);
236 remark_port (gdb_input_port);
4907ff5a
MD
237 SCM_END_FOREIGN_BLOCK;
238 return status;
239}
240
1cc91f1b 241
4907ff5a 242int
6e8d25a6 243gdb_eval (SCM exp)
4907ff5a
MD
244{
245 RESET_STRING;
246 if (SCM_IMP (exp))
247 {
248 gdb_result = exp;
249 return 0;
250 }
251 if (SCM_GC_P)
252 {
253 SEND_STRING ("Can't evaluate lisp expressions during gc");
254 return -1;
255 }
256 SCM_BEGIN_FOREIGN_BLOCK;
257 {
dc19d1d2 258 SCM env = scm_top_level_env (SCM_CDR (scm_top_level_lookup_closure_var));
4907ff5a
MD
259 gdb_result = scm_permanent_object (scm_ceval (exp, env));
260 }
261 SCM_END_FOREIGN_BLOCK;
262 return 0;
263}
264
1cc91f1b 265
4907ff5a 266int
6e8d25a6 267gdb_print (SCM obj)
4907ff5a
MD
268{
269 RESET_STRING;
270 SCM_BEGIN_FOREIGN_BLOCK;
271 /* Reset stream */
c94577b4 272 scm_seek (gdb_output_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
4907ff5a 273 scm_write (obj, gdb_output_port);
69bc9ff3 274 scm_truncate_file (gdb_output_port, SCM_UNDEFINED);
e684c60f
GH
275 {
276 scm_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
277
278 scm_flush (gdb_output_port);
279 *(pt->write_buf + pt->read_buf_size) = 0;
280 SEND_STRING (pt->read_buf);
281 }
4907ff5a
MD
282 SCM_END_FOREIGN_BLOCK;
283 return 0;
284}
285
1cc91f1b 286
4907ff5a 287int
6e8d25a6 288gdb_binding (SCM name, SCM value)
4907ff5a
MD
289{
290 RESET_STRING;
291 if (SCM_GC_P)
292 {
293 SEND_STRING ("Can't create new bindings during gc");
294 return -1;
295 }
296 SCM_BEGIN_FOREIGN_BLOCK;
297 {
298 SCM vcell = scm_sym2vcell (name,
dc19d1d2 299 SCM_CDR (scm_top_level_lookup_closure_var),
4907ff5a
MD
300 SCM_BOOL_T);
301 SCM_SETCDR (vcell, value);
302 }
303 SCM_END_FOREIGN_BLOCK;
304 return 0;
305}
306
307void
308scm_init_gdbint ()
309{
310 static char *s = "scm_init_gdb_interface";
311 SCM port;
312
313 scm_print_carefully_p = 0;
314
315 port = scm_mkstrport (SCM_INUM0,
840ae05d 316 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
4907ff5a
MD
317 SCM_OPN | SCM_WRTNG,
318 s);
319 gdb_output_port = scm_permanent_object (port);
320
321 port = scm_mkstrport (SCM_INUM0,
322 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
840ae05d 323 SCM_OPN | SCM_RDNG | SCM_WRTNG,
4907ff5a
MD
324 s);
325 gdb_input_port = scm_permanent_object (port);
380b6b4c
MD
326
327 tok_buf = scm_permanent_object (scm_makstr (30L, 0));
4907ff5a 328}
89e00824
ML
329
330/*
331 Local Variables:
332 c-file-style: "gnu"
333 End:
334*/