summaryrefslogtreecommitdiff
path: root/libguile/gdbint.c
blob: 73c00a1f67498d54d840ccdfefd0fc292724d3b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* GDB interface for Guile
 * Copyright (C) 1996, 1997 Free Software Foundation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 * As a special exception, the Free Software Foundation gives permission
 * for additional uses of the text contained in its release of GUILE.
 *
 * The exception is that, if you link the GUILE library with other files
 * to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the GUILE library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the
 * Free Software Foundation under the name GUILE.  If you copy
 * code from other Free Software Foundation releases into a copy of
 * GUILE, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status of such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for GUILE, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 *
 * The author can be reached at djurfeldt@nada.kth.se
 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */

#include <stdio.h>
#include "_scm.h"
#include "tag.h"
#include "strports.h"
#include "read.h"
#include "eval.h"
#include "chars.h"
#include "modules.h"

#include "gdbint.h"

/* {Support for debugging with gdb}
 *
 * TODO:
 *
 * 1. Redirect outputs
 * 2. Catch errors
 * 3. Prevent print from causing segmentation fault when given broken pairs
 */

#include <stdio.h>
#include "_scm.h"

#define GDB_TYPE SCM

#include "gdb_interface.h"



/* Be carefull when this macro is true.
   scm_gc_heap_lock is set during gc.
 */
#define SCM_GC_P (scm_gc_heap_lock)

/* Macros that encapsulate blocks of code which can be called by the
 * debugger.
 */
#define SCM_BEGIN_FOREIGN_BLOCK \
{ \
  old_ints = scm_ints_disabled; scm_ints_disabled = 1; \
  old_gc = scm_block_gc; scm_block_gc = 1; \
  scm_print_carefully_p = 1; \
} \


#define SCM_END_FOREIGN_BLOCK \
{ \
  scm_print_carefully_p = 0; \
  scm_block_gc = old_gc; \
  scm_ints_disabled = old_ints; \
} \


#define RESET_STRING { gdb_output_length = 0; }

#define SEND_STRING(str) \
{ \
  gdb_output = str; \
  gdb_output_length = strlen (str); \
} \


/* {Gdb interface}
 */

unsigned short gdb_options = GDB_HAVE_BINDINGS;

char *gdb_language = "lisp/c";

SCM gdb_result;

char *gdb_output;

int gdb_output_length;

int scm_print_carefully_p;

static SCM gdb_input_port;
static int port_mark_p, stream_mark_p, string_mark_p;

static SCM tok_buf;
static int tok_buf_mark_p;

static SCM gdb_output_port;
static int old_ints, old_gc;


static void unmark_port SCM_P ((SCM port));

static void
unmark_port (port)
     SCM port;
{
  SCM stream, string;
  port_mark_p = SCM_GC8MARKP (port);
  SCM_CLRGC8MARK (port);
  stream = SCM_STREAM (port);
  stream_mark_p = SCM_GCMARKP (stream);
  SCM_CLRGCMARK (stream);
  string = SCM_CDR (stream);
  string_mark_p = SCM_GC8MARKP (string);
  SCM_CLRGC8MARK (string);
}


static void remark_port SCM_P ((SCM port));

static void
remark_port (port)
     SCM port;
{
  SCM stream = SCM_STREAM (port);
  SCM string = SCM_CDR (stream);
  if (string_mark_p) SCM_SETGC8MARK (string);
  if (stream_mark_p) SCM_SETGCMARK (stream);
  if (port_mark_p) SCM_SETGC8MARK (port);
}


int
gdb_maybe_valid_type_p (value)
     SCM value;
{
  if (SCM_IMP (value) || scm_cellp (value))
    return scm_tag (value) != SCM_MAKINUM (-1);
  return 0;
}


int
gdb_read (str)
     char *str;
{
  SCM ans;
  int status = 0;
  RESET_STRING;
  /* Need to be restrictive about what to read? */
  if (SCM_GC_P)
    {
      char *p;
      for (p = str; *p != '\0'; ++p)
	switch (*p)
	  {
	  case '(':
	  case '\'':
	  case '"':
	    SEND_STRING ("Can't read this kind of expressions during gc");
	    return -1;
	  case '#':
	    if (*++p == '\0')
	      goto premature;
	    if (*p == '\\')
	      {
		if (*++p != '\0')
		  continue;
	      premature:
		SEND_STRING ("Premature end of lisp expression");
		return -1;
	      }
	  default:
	    continue;
	  }
    }
  SCM_BEGIN_FOREIGN_BLOCK;
  unmark_port (gdb_input_port);
  scm_lseek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
  scm_puts (str, gdb_input_port);
  scm_ftruncate (gdb_input_port, SCM_UNDEFINED);
  scm_lseek (gdb_input_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
  /* Read one object */
  tok_buf_mark_p = SCM_GC8MARKP (tok_buf);
  SCM_CLRGC8MARK (tok_buf);
  ans = scm_lreadr (&tok_buf, gdb_input_port, &ans);
  if (SCM_GC_P)
    {
      if (SCM_NIMP (ans))
	{
	  SEND_STRING ("Non-immediate created during gc.  Memory may be trashed.");
	  status = -1;
	  goto exit;
	}
    }
  gdb_result = ans;
  /* Protect answer from future GC */
  if (SCM_NIMP (ans))
    scm_permanent_object (ans);
exit:
  if (tok_buf_mark_p)
    SCM_SETGC8MARK (tok_buf);
  remark_port (gdb_input_port);
  SCM_END_FOREIGN_BLOCK;
  return status;
}


int
gdb_eval (exp)
     SCM exp;
{
  RESET_STRING;
  if (SCM_IMP (exp))
    {
      gdb_result = exp;
      return 0;
    }
  if (SCM_GC_P)
    {
      SEND_STRING ("Can't evaluate lisp expressions during gc");
      return -1;
    }
  SCM_BEGIN_FOREIGN_BLOCK;
  {
    SCM env = scm_top_level_env (SCM_CDR (scm_top_level_lookup_closure_var));
    gdb_result = scm_permanent_object (scm_ceval (exp, env));
  }
  SCM_END_FOREIGN_BLOCK;
  return 0;
}


int
gdb_print (obj)
     SCM obj;
{
  RESET_STRING;
  SCM_BEGIN_FOREIGN_BLOCK;
  /* Reset stream */
  scm_lseek (gdb_output_port, SCM_INUM0, SCM_MAKINUM (SEEK_SET));
  scm_write (obj, gdb_output_port);
  scm_ftruncate (gdb_output_port, SCM_UNDEFINED);
  SEND_STRING (SCM_CHARS (SCM_STREAM (gdb_output_port)));
  SCM_END_FOREIGN_BLOCK;
  return 0;
}


int
gdb_binding (name, value)
     SCM name;
     SCM value;
{
  RESET_STRING;
  if (SCM_GC_P)
    {
      SEND_STRING ("Can't create new bindings during gc");
      return -1;
    }
  SCM_BEGIN_FOREIGN_BLOCK;
  {
    SCM vcell = scm_sym2vcell (name,
			       SCM_CDR (scm_top_level_lookup_closure_var),
			       SCM_BOOL_T);
    SCM_SETCDR (vcell, value);
  }
  SCM_END_FOREIGN_BLOCK;
  return 0;
}

void
scm_init_gdbint ()
{
  static char *s = "scm_init_gdb_interface";
  SCM port;

  scm_print_carefully_p = 0;
  
  port = scm_mkstrport (SCM_INUM0,
			scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
			SCM_OPN | SCM_WRTNG,
			s);
  gdb_output_port = scm_permanent_object (port);
  
  port = scm_mkstrport (SCM_INUM0,
			scm_make_string (SCM_MAKINUM (0), SCM_UNDEFINED),
			SCM_OPN | SCM_RDNG | SCM_WRTNG,
			s);
  gdb_input_port = scm_permanent_object (port);

  tok_buf = scm_permanent_object (scm_makstr (30L, 0));
}