gdb
[deliverable/binutils-gdb.git] / gdb / scm-valprint.c
1 /* Scheme/Guile language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2005, 2007, 2008
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "value.h"
28 #include "scm-lang.h"
29 #include "valprint.h"
30 #include "gdbcore.h"
31 #include "c-lang.h"
32 #include "infcall.h"
33 #include "objfiles.h"
34
35 static void scm_ipruk (char *, LONGEST, struct ui_file *);
36 static void scm_scmlist_print (LONGEST, struct ui_file *, int,
37 const struct value_print_options *);
38 static int scm_inferior_print (LONGEST, struct ui_file *, int,
39 const struct value_print_options *);
40
41 /* Prints the SCM value VALUE by invoking the inferior, if appropraite.
42 Returns >= 0 on success; return -1 if the inferior cannot/should not
43 print VALUE. */
44
45 static int
46 scm_inferior_print (LONGEST value, struct ui_file *stream,
47 int recurse, const struct value_print_options *options)
48 {
49 struct objfile *objf;
50 struct gdbarch *gdbarch;
51 struct value *func, *arg, *result;
52 struct symbol *gdb_output_sym, *gdb_output_len_sym;
53 char *output;
54 int ret, output_len;
55
56 func = find_function_in_inferior ("gdb_print", &objf);
57 gdbarch = get_objfile_arch (objf);
58 arg = value_from_longest (builtin_type (gdbarch)->builtin_core_addr, value);
59
60 result = call_function_by_hand (func, 1, &arg);
61 ret = (int) value_as_long (result);
62 if (ret == 0)
63 {
64 /* XXX: Should we cache these symbols? */
65 gdb_output_sym =
66 lookup_symbol_global ("gdb_output", NULL, NULL, VAR_DOMAIN);
67 gdb_output_len_sym =
68 lookup_symbol_global ("gdb_output_length", NULL, NULL, VAR_DOMAIN);
69
70 if ((gdb_output_sym == NULL) || (gdb_output_len_sym == NULL))
71 ret = -1;
72 else
73 {
74 struct value *remote_buffer;
75
76 read_memory (SYMBOL_VALUE_ADDRESS (gdb_output_len_sym),
77 (char *) &output_len, sizeof (output_len));
78
79 output = (char *) alloca (output_len);
80 remote_buffer = value_at (builtin_type (gdbarch)->builtin_core_addr,
81 SYMBOL_VALUE_ADDRESS (gdb_output_sym));
82 read_memory (value_as_address (remote_buffer),
83 output, output_len);
84
85 ui_file_write (stream, output, output_len);
86 }
87 }
88
89 return ret;
90 }
91
92 /* {Names of immediate symbols}
93 * This table must agree with the declarations in scm.h: {Immediate Symbols}.*/
94
95 static char *scm_isymnames[] =
96 {
97 /* This table must agree with the declarations */
98 "and",
99 "begin",
100 "case",
101 "cond",
102 "do",
103 "if",
104 "lambda",
105 "let",
106 "let*",
107 "letrec",
108 "or",
109 "quote",
110 "set!",
111 "define",
112 #if 0
113 "literal-variable-ref",
114 "literal-variable-set!",
115 #endif
116 "apply",
117 "call-with-current-continuation",
118
119 /* user visible ISYMS */
120 /* other keywords */
121 /* Flags */
122
123 "#f",
124 "#t",
125 "#<undefined>",
126 "#<eof>",
127 "()",
128 "#<unspecified>"
129 };
130
131 static void
132 scm_scmlist_print (LONGEST svalue, struct ui_file *stream, int recurse,
133 const struct value_print_options *options)
134 {
135 unsigned int more = options->print_max;
136 if (recurse > 6)
137 {
138 fputs_filtered ("...", stream);
139 return;
140 }
141 scm_scmval_print (SCM_CAR (svalue), stream, recurse + 1, options);
142 svalue = SCM_CDR (svalue);
143 for (; SCM_NIMP (svalue); svalue = SCM_CDR (svalue))
144 {
145 if (SCM_NECONSP (svalue))
146 break;
147 fputs_filtered (" ", stream);
148 if (--more == 0)
149 {
150 fputs_filtered ("...", stream);
151 return;
152 }
153 scm_scmval_print (SCM_CAR (svalue), stream, recurse + 1, options);
154 }
155 if (SCM_NNULLP (svalue))
156 {
157 fputs_filtered (" . ", stream);
158 scm_scmval_print (svalue, stream, recurse + 1, options);
159 }
160 }
161
162 static void
163 scm_ipruk (char *hdr, LONGEST ptr, struct ui_file *stream)
164 {
165 fprintf_filtered (stream, "#<unknown-%s", hdr);
166 #define SCM_SIZE TYPE_LENGTH (builtin_type_scm)
167 if (SCM_CELLP (ptr))
168 fprintf_filtered (stream, " (0x%lx . 0x%lx) @",
169 (long) SCM_CAR (ptr), (long) SCM_CDR (ptr));
170 fprintf_filtered (stream, " 0x%s>", paddr_nz (ptr));
171 }
172
173 void
174 scm_scmval_print (LONGEST svalue, struct ui_file *stream,
175 int recurse, const struct value_print_options *options)
176 {
177 taloop:
178 switch (7 & (int) svalue)
179 {
180 case 2:
181 case 6:
182 print_longest (stream,
183 options->format ? options->format : 'd',
184 1, svalue >> 2);
185 break;
186 case 4:
187 if (SCM_ICHRP (svalue))
188 {
189 svalue = SCM_ICHR (svalue);
190 scm_printchar (svalue, stream);
191 break;
192 }
193 else if (SCM_IFLAGP (svalue)
194 && (SCM_ISYMNUM (svalue)
195 < (sizeof scm_isymnames / sizeof (char *))))
196 {
197 fputs_filtered (SCM_ISYMCHARS (svalue), stream);
198 break;
199 }
200 else if (SCM_ILOCP (svalue))
201 {
202 fprintf_filtered (stream, "#@%ld%c%ld",
203 (long) SCM_IFRAME (svalue),
204 SCM_ICDRP (svalue) ? '-' : '+',
205 (long) SCM_IDIST (svalue));
206 break;
207 }
208 else
209 goto idef;
210 break;
211 case 1:
212 /* gloc */
213 svalue = SCM_CAR (svalue - 1);
214 goto taloop;
215 default:
216 idef:
217 scm_ipruk ("immediate", svalue, stream);
218 break;
219 case 0:
220
221 switch (SCM_TYP7 (svalue))
222 {
223 case scm_tcs_cons_gloc:
224 if (SCM_CDR (SCM_CAR (svalue) - 1L) == 0)
225 {
226 #if 0
227 SCM name;
228 #endif
229 fputs_filtered ("#<latte ", stream);
230 #if 1
231 fputs_filtered ("???", stream);
232 #else
233 name = ((SCM n *) (STRUCT_TYPE (exp)))[struct_i_name];
234 scm_lfwrite (CHARS (name),
235 (sizet) sizeof (char),
236 (sizet) LENGTH (name),
237 port);
238 #endif
239 fprintf_filtered (stream, " #X%s>", paddr_nz (svalue));
240 break;
241 }
242 case scm_tcs_cons_imcar:
243 case scm_tcs_cons_nimcar:
244 fputs_filtered ("(", stream);
245 scm_scmlist_print (svalue, stream, recurse + 1, options);
246 fputs_filtered (")", stream);
247 break;
248 case scm_tcs_closures:
249 fputs_filtered ("#<CLOSURE ", stream);
250 scm_scmlist_print (SCM_CODE (svalue), stream, recurse + 1, options);
251 fputs_filtered (">", stream);
252 break;
253 case scm_tc7_string:
254 {
255 int len = SCM_LENGTH (svalue);
256 CORE_ADDR addr = (CORE_ADDR) SCM_CDR (svalue);
257 int i;
258 int done = 0;
259 int buf_size;
260 gdb_byte buffer[64];
261 int truncate = options->print_max && len > (int) options->print_max;
262 if (truncate)
263 len = options->print_max;
264 fputs_filtered ("\"", stream);
265 for (; done < len; done += buf_size)
266 {
267 buf_size = min (len - done, 64);
268 read_memory (addr + done, buffer, buf_size);
269
270 for (i = 0; i < buf_size; ++i)
271 switch (buffer[i])
272 {
273 case '\"':
274 case '\\':
275 fputs_filtered ("\\", stream);
276 default:
277 fprintf_filtered (stream, "%c", buffer[i]);
278 }
279 }
280 fputs_filtered (truncate ? "...\"" : "\"", stream);
281 break;
282 }
283 break;
284 case scm_tcs_symbols:
285 {
286 int len = SCM_LENGTH (svalue);
287
288 char *str = alloca (len);
289 read_memory (SCM_CDR (svalue), (gdb_byte *) str, len + 1);
290 /* Should handle weird characters FIXME */
291 str[len] = '\0';
292 fputs_filtered (str, stream);
293 break;
294 }
295 case scm_tc7_vector:
296 {
297 int len = SCM_LENGTH (svalue);
298 int i;
299 LONGEST elements = SCM_CDR (svalue);
300 fputs_filtered ("#(", stream);
301 for (i = 0; i < len; ++i)
302 {
303 if (i > 0)
304 fputs_filtered (" ", stream);
305 scm_scmval_print (scm_get_field (elements, i), stream,
306 recurse + 1, options);
307 }
308 fputs_filtered (")", stream);
309 }
310 break;
311 #if 0
312 case tc7_lvector:
313 {
314 SCM result;
315 SCM hook;
316 hook = scm_get_lvector_hook (exp, LV_PRINT_FN);
317 if (hook == BOOL_F)
318 {
319 scm_puts ("#<locked-vector ", port);
320 scm_intprint (CDR (exp), 16, port);
321 scm_puts (">", port);
322 }
323 else
324 {
325 result
326 = scm_apply (hook,
327 scm_listify (exp, port,
328 (writing ? BOOL_T : BOOL_F),
329 SCM_UNDEFINED),
330 EOL);
331 if (result == BOOL_F)
332 goto punk;
333 }
334 break;
335 }
336 break;
337 case tc7_bvect:
338 case tc7_ivect:
339 case tc7_uvect:
340 case tc7_fvect:
341 case tc7_dvect:
342 case tc7_cvect:
343 scm_raprin1 (exp, port, writing);
344 break;
345 #endif
346 case scm_tcs_subrs:
347 {
348 int index = SCM_CAR (svalue) >> 8;
349 #if 1
350 char str[20];
351 sprintf (str, "#%d", index);
352 #else
353 char *str = index ? SCM_CHARS (scm_heap_org + index) : "";
354 #define SCM_CHARS(x) ((char *)(SCM_CDR(x)))
355 char *str = CHARS (SNAME (exp));
356 #endif
357 fprintf_filtered (stream, "#<primitive-procedure %s>",
358 str);
359 }
360 break;
361 #if 0
362 #ifdef CCLO
363 case tc7_cclo:
364 scm_puts ("#<compiled-closure ", port);
365 scm_iprin1 (CCLO_SUBR (exp), port, writing);
366 scm_putc ('>', port);
367 break;
368 #endif
369 case tc7_contin:
370 fprintf_filtered (stream, "#<continuation %d @ #X%lx >",
371 LENGTH (svalue),
372 (long) CHARS (svalue));
373 break;
374 case tc7_port:
375 i = PTOBNUM (exp);
376 if (i < scm_numptob
377 && scm_ptobs[i].print
378 && (scm_ptobs[i].print) (exp, port, writing))
379 break;
380 goto punk;
381 case tc7_smob:
382 i = SMOBNUM (exp);
383 if (i < scm_numsmob && scm_smobs[i].print
384 && (scm_smobs[i].print) (exp, port, writing))
385 break;
386 goto punk;
387 #endif
388 default:
389 #if 0
390 punk:
391 #endif
392 scm_ipruk ("type", svalue, stream);
393 }
394 break;
395 }
396 }
397
398 int
399 scm_val_print (struct type *type, const gdb_byte *valaddr,
400 int embedded_offset, CORE_ADDR address,
401 struct ui_file *stream, int recurse,
402 const struct value_print_options *options)
403 {
404 if (is_scmvalue_type (type))
405 {
406 LONGEST svalue = extract_signed_integer (valaddr, TYPE_LENGTH (type));
407
408 if (scm_inferior_print (svalue, stream, recurse, options) >= 0)
409 {
410 }
411 else
412 {
413 scm_scmval_print (svalue, stream, recurse, options);
414 }
415
416 gdb_flush (stream);
417 return (0);
418 }
419 else
420 {
421 return c_val_print (type, valaddr, 0, address, stream, recurse, options);
422 }
423 }
424
425 int
426 scm_value_print (struct value *val, struct ui_file *stream,
427 const struct value_print_options *options)
428 {
429 struct value_print_options opts = *options;
430 opts.deref_ref = 1;
431 return (common_val_print (val, stream, 0, &opts, current_language));
432 }
This page took 0.037995 seconds and 4 git commands to generate.