]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/gdbint.c
New upstream version 2.19.65
[lilypond.git] / guile18 / libguile / gdbint.c
1 /* GDB interface for Guile
2  * Copyright (C) 1996,1997,1999,2000,2001,2002,2004
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
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but 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 02110-1301 USA
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
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"
38 #include "libguile/fluids.h"
39 #include "libguile/strings.h"
40 #include "libguile/init.h"
41
42 #include "libguile/gdbint.h"
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
53 #define GDB_TYPE SCM
54
55 #include "libguile/gdb_interface.h"
56
57 \f
58
59 /* Be carefull when this macro is true.
60    scm_gc_running_p is set during gc.
61  */
62 #define SCM_GC_P (scm_gc_running_p)
63
64 /* Macros that encapsulate blocks of code which can be called by the
65  * debugger.
66  */
67 #define SCM_BEGIN_FOREIGN_BLOCK \
68 do { \
69   scm_print_carefully_p = 1; \
70 } while (0)
71
72
73 #define SCM_END_FOREIGN_BLOCK \
74 do { \
75   scm_print_carefully_p = 0; \
76 } while (0)
77
78
79 #define RESET_STRING { gdb_output_length = 0; }
80
81 #define SEND_STRING(str) \
82 do { \
83   gdb_output = (char *) (str); \
84   gdb_output_length = strlen ((const char *) (str)); \
85 } while (0)
86
87
88 /* {Gdb interface}
89  */
90
91 unsigned short gdb_options = GDB_HAVE_BINDINGS;
92
93 char *gdb_language = "lisp/c";
94
95 SCM gdb_result;
96
97 char *gdb_output;
98
99 int gdb_output_length;
100
101 int scm_print_carefully_p;
102
103 static SCM gdb_input_port;
104 static int port_mark_p, stream_mark_p, string_mark_p;
105
106 static SCM gdb_output_port;
107
108
109 static void
110 unmark_port (SCM port)
111 {
112   SCM stream, string;
113   port_mark_p = SCM_GC_MARK_P (port);
114   SCM_CLEAR_GC_MARK (port);
115   stream = SCM_PACK (SCM_STREAM (port));
116   stream_mark_p = SCM_GC_MARK_P (stream);
117   SCM_CLEAR_GC_MARK (stream);
118   string = SCM_CDR (stream);
119   string_mark_p = SCM_GC_MARK_P (string);
120   SCM_CLEAR_GC_MARK (string);
121 }
122
123
124 static void
125 remark_port (SCM port)
126 {
127   SCM stream = SCM_PACK (SCM_STREAM (port));
128   SCM string = SCM_CDR (stream);
129   if (string_mark_p)
130     SCM_SET_GC_MARK (string);
131   if (stream_mark_p)
132     SCM_SET_GC_MARK (stream);
133   if (port_mark_p)
134     SCM_SET_GC_MARK (port);
135 }
136
137
138 int
139 gdb_maybe_valid_type_p (SCM value)
140 {
141   return SCM_IMP (value) || scm_in_heap_p (value);
142 }
143
144
145 int
146 gdb_read (char *str)
147 {
148   SCM ans;
149   int status = 0;
150   RESET_STRING;
151   /* Need to be restrictive about what to read? */
152   if (SCM_GC_P)
153     {
154       char *p;
155       for (p = str; *p != '\0'; ++p)
156         switch (*p)
157           {
158           case '(':
159           case '\'':
160           case '"':
161             SEND_STRING ("Can't read this kind of expressions during gc");
162             return -1;
163           case '#':
164             if (*++p == '\0')
165               goto premature;
166             if (*p == '\\')
167               {
168                 if (*++p != '\0')
169                   continue;
170               premature:
171                 SEND_STRING ("Premature end of lisp expression");
172                 return -1;
173               }
174           default:
175             continue;
176           }
177     }
178   SCM_BEGIN_FOREIGN_BLOCK;
179   unmark_port (gdb_input_port);
180   scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
181   scm_puts (str, gdb_input_port);
182   scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
183   scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
184
185   /* Read one object */
186   ans = scm_read (gdb_input_port);
187   if (SCM_GC_P)
188     {
189       if (SCM_NIMP (ans))
190         {
191           SEND_STRING ("Non-immediate created during gc.  Memory may be trashed.");
192           status = -1;
193           goto exit;
194         }
195     }
196   gdb_result = ans;
197   /* Protect answer from future GC */
198   if (SCM_NIMP (ans))
199     scm_permanent_object (ans);
200 exit:
201   remark_port (gdb_input_port);
202   SCM_END_FOREIGN_BLOCK;
203   return status;
204 }
205
206
207 int
208 gdb_eval (SCM exp)
209 {
210   RESET_STRING;
211   if (SCM_GC_P)
212     {
213       SEND_STRING ("Can't evaluate lisp expressions during gc");
214       return -1;
215     }
216   SCM_BEGIN_FOREIGN_BLOCK;
217   {
218     SCM env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
219     gdb_result = scm_permanent_object (scm_i_eval_x (exp, env));
220   }
221   SCM_END_FOREIGN_BLOCK;
222   return 0;
223 }
224
225
226 int
227 gdb_print (SCM obj)
228 {
229   if (!scm_initialized_p)
230     SEND_STRING ("*** Guile not initialized ***");
231   else
232     {
233       RESET_STRING;
234       SCM_BEGIN_FOREIGN_BLOCK;
235       /* Reset stream */
236       scm_seek (gdb_output_port, SCM_INUM0, scm_from_int (SEEK_SET));
237       scm_write (obj, gdb_output_port);
238       scm_truncate_file (gdb_output_port, SCM_UNDEFINED);
239       {
240         scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
241
242         scm_flush (gdb_output_port);
243         *(pt->write_buf + pt->read_buf_size) = 0;
244         SEND_STRING (pt->read_buf);
245       }
246       SCM_END_FOREIGN_BLOCK;
247     }
248   return 0;
249 }
250
251
252 int
253 gdb_binding (SCM name, SCM value)
254 {
255   RESET_STRING;
256   if (SCM_GC_P)
257     {
258       SEND_STRING ("Can't create new bindings during gc");
259       return -1;
260     }
261   SCM_BEGIN_FOREIGN_BLOCK;
262   {
263     SCM var = scm_sym2var (name, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_T);
264     SCM_VARIABLE_SET (var, value);
265   }
266   SCM_END_FOREIGN_BLOCK;
267   return 0;
268 }
269
270 void
271 scm_init_gdbint ()
272 {
273   static char *s = "scm_init_gdb_interface";
274   SCM port;
275
276   scm_print_carefully_p = 0;
277   
278   port = scm_mkstrport (SCM_INUM0,
279                         scm_c_make_string (0, SCM_UNDEFINED),
280                         SCM_OPN | SCM_WRTNG,
281                         s);
282   gdb_output_port = scm_permanent_object (port);
283   
284   port = scm_mkstrport (SCM_INUM0,
285                         scm_c_make_string (0, SCM_UNDEFINED),
286                         SCM_OPN | SCM_RDNG | SCM_WRTNG,
287                         s);
288   gdb_input_port = scm_permanent_object (port);
289 }
290
291 /*
292   Local Variables:
293   c-file-style: "gnu"
294   End:
295 */