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