*** empty log message ***
[bpt/guile.git] / libguile / gdbint.c
CommitLineData
4907ff5a 1/* GDB interface for Guile
b7f3516f 2 * Copyright (C) 1996, 1997 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
MD
45
46#include <stdio.h>
47#include "_scm.h"
20e6290e
JB
48#include "tag.h"
49#include "strports.h"
50#include "read.h"
20e6290e
JB
51#include "eval.h"
52#include "chars.h"
bcf12299 53#include "modules.h"
4907ff5a 54
20e6290e 55#include "gdbint.h"
4907ff5a
MD
56\f
57/* {Support for debugging with gdb}
58 *
59 * TODO:
60 *
61 * 1. Redirect outputs
62 * 2. Catch errors
63 * 3. Prevent print from causing segmentation fault when given broken pairs
64 */
65
66#include <stdio.h>
67#include "_scm.h"
68
69#define GDB_TYPE SCM
70
71#include "gdb_interface.h"
72
73\f
74
75/* Be carefull when this macro is true.
76 scm_gc_heap_lock is set during gc.
77 */
78#define SCM_GC_P (scm_gc_heap_lock)
79
80/* Macros that encapsulate blocks of code which can be called by the
81 * debugger.
82 */
83#define SCM_BEGIN_FOREIGN_BLOCK \
84{ \
85 old_ints = scm_ints_disabled; scm_ints_disabled = 1; \
86 old_gc = scm_block_gc; scm_block_gc = 1; \
87 scm_print_carefully_p = 1; \
88} \
89
90
91#define SCM_END_FOREIGN_BLOCK \
92{ \
93 scm_print_carefully_p = 0; \
94 scm_block_gc = old_gc; \
95 scm_ints_disabled = old_ints; \
96} \
97
98
99#define RESET_STRING { gdb_output_length = 0; }
100
101#define SEND_STRING(str) \
102{ \
103 gdb_output = str; \
104 gdb_output_length = strlen (str); \
380b6b4c
MD
105} \
106
4907ff5a
MD
107
108/* {Gdb interface}
109 */
110
111unsigned short gdb_options = GDB_HAVE_BINDINGS;
112
113char *gdb_language = "lisp/c";
114
115SCM gdb_result;
116
117char *gdb_output;
118
119int gdb_output_length;
120
121int scm_print_carefully_p;
122
123static SCM gdb_input_port;
380b6b4c
MD
124static int port_mark_p, stream_mark_p, string_mark_p;
125
126static SCM tok_buf;
127static int tok_buf_mark_p;
128
4907ff5a
MD
129static SCM gdb_output_port;
130static int old_ints, old_gc;
131
1cc91f1b
JB
132
133static void unmark_port SCM_P ((SCM port));
134
380b6b4c
MD
135static void
136unmark_port (port)
137 SCM port;
380b6b4c
MD
138{
139 SCM stream, string;
140 port_mark_p = SCM_GC8MARKP (port);
141 SCM_CLRGC8MARK (port);
142 stream = SCM_STREAM (port);
143 stream_mark_p = SCM_GCMARKP (stream);
144 SCM_CLRGCMARK (stream);
145 string = SCM_CDR (stream);
146 string_mark_p = SCM_GC8MARKP (string);
147 SCM_CLRGC8MARK (string);
148}
149
1cc91f1b
JB
150
151static void remark_port SCM_P ((SCM port));
152
380b6b4c
MD
153static void
154remark_port (port)
155 SCM port;
380b6b4c
MD
156{
157 SCM stream = SCM_STREAM (port);
158 SCM string = SCM_CDR (stream);
159 if (string_mark_p) SCM_SETGC8MARK (string);
160 if (stream_mark_p) SCM_SETGCMARK (stream);
161 if (port_mark_p) SCM_SETGC8MARK (port);
162}
163
1cc91f1b 164
4907ff5a
MD
165int
166gdb_maybe_valid_type_p (value)
167 SCM value;
4907ff5a
MD
168{
169 if (SCM_IMP (value) || scm_cellp (value))
170 return scm_tag (value) != SCM_MAKINUM (-1);
171 return 0;
172}
173
1cc91f1b 174
4907ff5a
MD
175int
176gdb_read (str)
177 char *str;
4907ff5a
MD
178{
179 SCM ans;
180 int status = 0;
181 RESET_STRING;
182 /* Need to be restrictive about what to read? */
183 if (SCM_GC_P)
184 {
185 char *p;
186 for (p = str; *p != '\0'; ++p)
187 switch (*p)
188 {
189 case '(':
190 case '\'':
191 case '"':
192 SEND_STRING ("Can't read this kind of expressions during gc");
193 return -1;
194 case '#':
195 if (*++p == '\0')
196 goto premature;
197 if (*p == '\\')
198 {
199 if (*++p != '\0')
200 continue;
201 premature:
202 SEND_STRING ("Premature end of lisp expression");
203 return -1;
204 }
205 default:
206 continue;
207 }
208 }
209 SCM_BEGIN_FOREIGN_BLOCK;
380b6b4c 210 unmark_port (gdb_input_port);
4907ff5a
MD
211 /* Replace string in input port and reset stream */
212 ans = SCM_CDR (SCM_STREAM (gdb_input_port));
213 SCM_SETCHARS (ans, str);
214 SCM_SETLENGTH (ans, strlen (str), scm_tc7_string);
215 SCM_SETCAR (SCM_STREAM (gdb_input_port), SCM_INUM0);
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
MD
242int
243gdb_eval (exp)
244 SCM exp;
4907ff5a
MD
245{
246 RESET_STRING;
247 if (SCM_IMP (exp))
248 {
249 gdb_result = exp;
250 return 0;
251 }
252 if (SCM_GC_P)
253 {
254 SEND_STRING ("Can't evaluate lisp expressions during gc");
255 return -1;
256 }
257 SCM_BEGIN_FOREIGN_BLOCK;
258 {
dc19d1d2 259 SCM env = scm_top_level_env (SCM_CDR (scm_top_level_lookup_closure_var));
4907ff5a
MD
260 gdb_result = scm_permanent_object (scm_ceval (exp, env));
261 }
262 SCM_END_FOREIGN_BLOCK;
263 return 0;
264}
265
1cc91f1b 266
4907ff5a
MD
267int
268gdb_print (obj)
269 SCM obj;
4907ff5a
MD
270{
271 RESET_STRING;
272 SCM_BEGIN_FOREIGN_BLOCK;
273 /* Reset stream */
274 SCM_SETCAR (SCM_STREAM (gdb_output_port), SCM_INUM0);
275 scm_write (obj, gdb_output_port);
276 scm_display (SCM_MAKICHR (0), gdb_output_port);
277 SEND_STRING (SCM_CHARS (SCM_CDR (SCM_STREAM (gdb_output_port))));
278 SCM_END_FOREIGN_BLOCK;
279 return 0;
280}
281
1cc91f1b 282
4907ff5a
MD
283int
284gdb_binding (name, value)
285 SCM name;
286 SCM value;
4907ff5a
MD
287{
288 RESET_STRING;
289 if (SCM_GC_P)
290 {
291 SEND_STRING ("Can't create new bindings during gc");
292 return -1;
293 }
294 SCM_BEGIN_FOREIGN_BLOCK;
295 {
296 SCM vcell = scm_sym2vcell (name,
dc19d1d2 297 SCM_CDR (scm_top_level_lookup_closure_var),
4907ff5a
MD
298 SCM_BOOL_T);
299 SCM_SETCDR (vcell, value);
300 }
301 SCM_END_FOREIGN_BLOCK;
302 return 0;
303}
304
305void
306scm_init_gdbint ()
307{
308 static char *s = "scm_init_gdb_interface";
309 SCM port;
310
311 scm_print_carefully_p = 0;
312
313 port = scm_mkstrport (SCM_INUM0,
314 scm_make_string (SCM_MAKINUM (80), SCM_UNDEFINED),
315 SCM_OPN | SCM_WRTNG,
316 s);
317 gdb_output_port = scm_permanent_object (port);
318
319 port = scm_mkstrport (SCM_INUM0,
320 scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
321 SCM_OPN | SCM_RDNG,
322 s);
323 gdb_input_port = scm_permanent_object (port);
380b6b4c
MD
324
325 tok_buf = scm_permanent_object (scm_makstr (30L, 0));
4907ff5a 326}