1 /* GDB/Scheme pretty-printing.
3 Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
25 #include "gdb_assert.h"
26 #include "symtab.h" /* Needed by language.h. */
31 #include "guile-internal.h"
33 /* Return type of print_string_repr. */
35 enum string_repr_result
37 /* The string method returned None. */
39 /* The string method had an error. */
49 /* No display hint. */
51 /* The display hint has a bad value. */
53 /* Print as an array. */
57 /* Print as a string. */
61 /* The <gdb:pretty-printer> smob. */
65 /* This must appear first. */
68 /* A string representing the name of the printer. */
71 /* A boolean indicating whether the printer is enabled. */
74 /* A procedure called to look up the printer for the given value.
75 The procedure is called as (lookup gdb:pretty-printer value).
76 The result should either be a gdb:pretty-printer object that will print
77 the value, or #f if the value is not recognized. */
80 /* Note: Attaching subprinters to this smob is left to Scheme. */
81 } pretty_printer_smob
;
83 /* The <gdb:pretty-printer-worker> smob. */
87 /* This must appear first. */
90 /* Either #f or one of the supported display hints: map, array, string.
91 If neither of those then the display hint is ignored (treated as #f). */
94 /* A procedure called to pretty-print the value.
95 (lambda (printer) ...) -> string | <gdb:lazy-string> | <gdb:value> */
98 /* A procedure called to print children of the value.
99 (lambda (printer) ...) -> <gdb:iterator>
100 The iterator returns a pair for each iteration: (name . value),
101 where "value" can have the same types as to_string. */
103 } pretty_printer_worker_smob
;
105 static const char pretty_printer_smob_name
[] =
106 "gdb:pretty-printer";
107 static const char pretty_printer_worker_smob_name
[] =
108 "gdb:pretty-printer-worker";
110 /* The tag Guile knows the pretty-printer smobs by. */
111 static scm_t_bits pretty_printer_smob_tag
;
112 static scm_t_bits pretty_printer_worker_smob_tag
;
114 /* Global list of pretty-printers. */
115 static const char pretty_printer_list_name
[] = "*pretty-printers*";
117 /* The *pretty-printer* variable. */
118 static SCM pretty_printer_list_var
;
120 /* gdb:pp-type-error. */
121 static SCM pp_type_error_symbol
;
123 /* Pretty-printer display hints are specified by strings. */
124 static SCM ppscm_map_string
;
125 static SCM ppscm_array_string
;
126 static SCM ppscm_string_string
;
128 /* Administrivia for pretty-printer matcher smobs. */
130 /* The smob "mark" function for <gdb:pretty-printer>. */
133 ppscm_mark_pretty_printer_smob (SCM self
)
135 pretty_printer_smob
*pp_smob
= (pretty_printer_smob
*) SCM_SMOB_DATA (self
);
137 scm_gc_mark (pp_smob
->name
);
138 scm_gc_mark (pp_smob
->enabled
);
139 scm_gc_mark (pp_smob
->lookup
);
141 return gdbscm_mark_gsmob (&pp_smob
->base
);
144 /* The smob "print" function for <gdb:pretty-printer>. */
147 ppscm_print_pretty_printer_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
149 pretty_printer_smob
*pp_smob
= (pretty_printer_smob
*) SCM_SMOB_DATA (self
);
151 gdbscm_printf (port
, "#<%s ", pretty_printer_smob_name
);
152 scm_write (pp_smob
->name
, port
);
153 scm_puts (gdbscm_is_true (pp_smob
->enabled
) ? " enabled" : " disabled",
155 scm_puts (">", port
);
157 scm_remember_upto_here_1 (self
);
159 /* Non-zero means success. */
163 /* (make-pretty-printer string procedure) -> <gdb:pretty-printer> */
166 gdbscm_make_pretty_printer (SCM name
, SCM lookup
)
168 pretty_printer_smob
*pp_smob
= (pretty_printer_smob
*)
169 scm_gc_malloc (sizeof (pretty_printer_smob
),
170 pretty_printer_smob_name
);
173 SCM_ASSERT_TYPE (scm_is_string (name
), name
, SCM_ARG1
, FUNC_NAME
,
175 SCM_ASSERT_TYPE (gdbscm_is_procedure (lookup
), lookup
, SCM_ARG2
, FUNC_NAME
,
178 pp_smob
->name
= name
;
179 pp_smob
->lookup
= lookup
;
180 pp_smob
->enabled
= SCM_BOOL_T
;
181 smob
= scm_new_smob (pretty_printer_smob_tag
, (scm_t_bits
) pp_smob
);
182 gdbscm_init_gsmob (&pp_smob
->base
);
187 /* Return non-zero if SCM is a <gdb:pretty-printer> object. */
190 ppscm_is_pretty_printer (SCM scm
)
192 return SCM_SMOB_PREDICATE (pretty_printer_smob_tag
, scm
);
195 /* (pretty-printer? object) -> boolean */
198 gdbscm_pretty_printer_p (SCM scm
)
200 return scm_from_bool (ppscm_is_pretty_printer (scm
));
203 /* Returns the <gdb:pretty-printer> object in SELF.
204 Throws an exception if SELF is not a <gdb:pretty-printer> object. */
207 ppscm_get_pretty_printer_arg_unsafe (SCM self
, int arg_pos
,
208 const char *func_name
)
210 SCM_ASSERT_TYPE (ppscm_is_pretty_printer (self
), self
, arg_pos
, func_name
,
211 pretty_printer_smob_name
);
216 /* Returns a pointer to the pretty-printer smob of SELF.
217 Throws an exception if SELF is not a <gdb:pretty-printer> object. */
219 static pretty_printer_smob
*
220 ppscm_get_pretty_printer_smob_arg_unsafe (SCM self
, int arg_pos
,
221 const char *func_name
)
223 SCM pp_scm
= ppscm_get_pretty_printer_arg_unsafe (self
, arg_pos
, func_name
);
224 pretty_printer_smob
*pp_smob
225 = (pretty_printer_smob
*) SCM_SMOB_DATA (pp_scm
);
230 /* Pretty-printer methods. */
232 /* (pretty-printer-enabled? <gdb:pretty-printer>) -> boolean */
235 gdbscm_pretty_printer_enabled_p (SCM self
)
237 pretty_printer_smob
*pp_smob
238 = ppscm_get_pretty_printer_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
240 return pp_smob
->enabled
;
243 /* (set-pretty-printer-enabled! <gdb:pretty-printer> boolean)
247 gdbscm_set_pretty_printer_enabled_x (SCM self
, SCM enabled
)
249 pretty_printer_smob
*pp_smob
250 = ppscm_get_pretty_printer_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
252 pp_smob
->enabled
= scm_from_bool (gdbscm_is_true (enabled
));
254 return SCM_UNSPECIFIED
;
257 /* Administrivia for pretty-printer-worker smobs.
258 These are created when a matcher recognizes a value. */
260 /* The smob "mark" function for <gdb:pretty-printer-worker>. */
263 ppscm_mark_pretty_printer_worker_smob (SCM self
)
265 pretty_printer_worker_smob
*w_smob
266 = (pretty_printer_worker_smob
*) SCM_SMOB_DATA (self
);
268 scm_gc_mark (w_smob
->display_hint
);
269 scm_gc_mark (w_smob
->to_string
);
270 scm_gc_mark (w_smob
->children
);
272 return gdbscm_mark_gsmob (&w_smob
->base
);
275 /* The smob "print" function for <gdb:pretty-printer-worker>. */
278 ppscm_print_pretty_printer_worker_smob (SCM self
, SCM port
,
279 scm_print_state
*pstate
)
281 pretty_printer_worker_smob
*w_smob
282 = (pretty_printer_worker_smob
*) SCM_SMOB_DATA (self
);
284 gdbscm_printf (port
, "#<%s ", pretty_printer_worker_smob_name
);
285 scm_write (w_smob
->display_hint
, port
);
286 scm_puts (" ", port
);
287 scm_write (w_smob
->to_string
, port
);
288 scm_puts (" ", port
);
289 scm_write (w_smob
->children
, port
);
290 scm_puts (">", port
);
292 scm_remember_upto_here_1 (self
);
294 /* Non-zero means success. */
298 /* (make-pretty-printer-worker string procedure procedure)
299 -> <gdb:pretty-printer-worker> */
302 gdbscm_make_pretty_printer_worker (SCM display_hint
, SCM to_string
,
305 pretty_printer_worker_smob
*w_smob
= (pretty_printer_worker_smob
*)
306 scm_gc_malloc (sizeof (pretty_printer_worker_smob
),
307 pretty_printer_worker_smob_name
);
310 w_smob
->display_hint
= display_hint
;
311 w_smob
->to_string
= to_string
;
312 w_smob
->children
= children
;
313 w_scm
= scm_new_smob (pretty_printer_worker_smob_tag
, (scm_t_bits
) w_smob
);
314 gdbscm_init_gsmob (&w_smob
->base
);
318 /* Return non-zero if SCM is a <gdb:pretty-printer-worker> object. */
321 ppscm_is_pretty_printer_worker (SCM scm
)
323 return SCM_SMOB_PREDICATE (pretty_printer_worker_smob_tag
, scm
);
326 /* (pretty-printer-worker? object) -> boolean */
329 gdbscm_pretty_printer_worker_p (SCM scm
)
331 return scm_from_bool (ppscm_is_pretty_printer_worker (scm
));
334 /* Helper function to create a <gdb:exception> object indicating that the
335 type of some value returned from a pretty-printer is invalid. */
338 ppscm_make_pp_type_error_exception (const char *message
, SCM object
)
340 char *msg
= xstrprintf ("%s: ~S", message
);
341 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
343 = gdbscm_make_error (pp_type_error_symbol
,
344 NULL
/* func */, msg
,
345 scm_list_1 (object
), scm_list_1 (object
));
347 do_cleanups (cleanup
);
352 /* Print MESSAGE as an exception (meaning it is controlled by
353 "guile print-stack").
354 Called from the printer code when the Scheme code returns an invalid type
358 ppscm_print_pp_type_error (const char *message
, SCM object
)
360 SCM exception
= ppscm_make_pp_type_error_exception (message
, object
);
362 gdbscm_print_gdb_exception (SCM_BOOL_F
, exception
);
365 /* Helper function for find_pretty_printer which iterates over a list,
366 calls each function and inspects output. This will return a
367 <gdb:pretty-printer> object if one recognizes VALUE. If no printer is
368 found, it will return #f. On error, it will return a <gdb:exception>
371 Note: This has to be efficient and careful.
372 We don't want to excessively slow down printing of values, but any kind of
373 random crud can appear in the pretty-printer list, and we can't crash
377 ppscm_search_pp_list (SCM list
, SCM value
)
379 SCM orig_list
= list
;
381 if (scm_is_null (list
))
383 if (gdbscm_is_false (scm_list_p (list
))) /* scm_is_pair? */
385 return ppscm_make_pp_type_error_exception
386 (_("pretty-printer list is not a list"), list
);
389 for ( ; scm_is_pair (list
); list
= scm_cdr (list
))
391 SCM matcher
= scm_car (list
);
393 pretty_printer_smob
*pp_smob
;
396 if (!ppscm_is_pretty_printer (matcher
))
398 return ppscm_make_pp_type_error_exception
399 (_("pretty-printer list contains non-pretty-printer object"),
403 pp_smob
= (pretty_printer_smob
*) SCM_SMOB_DATA (matcher
);
405 /* Skip if disabled. */
406 if (gdbscm_is_false (pp_smob
->enabled
))
409 if (!gdbscm_is_procedure (pp_smob
->lookup
))
411 return ppscm_make_pp_type_error_exception
412 (_("invalid lookup object in pretty-printer matcher"),
416 worker
= gdbscm_safe_call_2 (pp_smob
->lookup
, matcher
,
417 value
, gdbscm_memory_error_p
);
418 if (!gdbscm_is_false (worker
))
420 if (gdbscm_is_exception (worker
))
422 if (ppscm_is_pretty_printer_worker (worker
))
424 return ppscm_make_pp_type_error_exception
425 (_("invalid result from pretty-printer lookup"), worker
);
429 if (!scm_is_null (list
))
431 return ppscm_make_pp_type_error_exception
432 (_("pretty-printer list is not a list"), orig_list
);
438 /* Subroutine of find_pretty_printer to simplify it.
439 Look for a pretty-printer to print VALUE in all objfiles.
440 If there's an error an exception smob is returned.
441 The result is #f, if no pretty-printer was found.
442 Otherwise the result is the pretty-printer smob. */
445 ppscm_find_pretty_printer_from_objfiles (SCM value
)
447 struct objfile
*objfile
;
449 ALL_OBJFILES (objfile
)
451 objfile_smob
*o_smob
= ofscm_objfile_smob_from_objfile (objfile
);
452 SCM pp
= ppscm_search_pp_list (ofscm_objfile_smob_pretty_printers (o_smob
),
455 /* Note: This will return if pp is a <gdb:exception> object,
456 which is what we want. */
457 if (gdbscm_is_true (pp
))
464 /* Subroutine of find_pretty_printer to simplify it.
465 Look for a pretty-printer to print VALUE in the current program space.
466 If there's an error an exception smob is returned.
467 The result is #f, if no pretty-printer was found.
468 Otherwise the result is the pretty-printer smob. */
471 ppscm_find_pretty_printer_from_progspace (SCM value
)
473 return SCM_BOOL_F
; /*TODO*/
476 /* Subroutine of find_pretty_printer to simplify it.
477 Look for a pretty-printer to print VALUE in the gdb module.
478 If there's an error a Scheme exception is returned.
479 The result is #f, if no pretty-printer was found.
480 Otherwise the result is the pretty-printer smob. */
483 ppscm_find_pretty_printer_from_gdb (SCM value
)
487 /* Fetch the global pretty printer list. */
488 pp_list
= scm_variable_ref (pretty_printer_list_var
);
489 pp
= ppscm_search_pp_list (pp_list
, value
);
493 /* Find the pretty-printing constructor function for VALUE. If no
494 pretty-printer exists, return #f. If one exists, return the
495 gdb:pretty-printer smob that implements it. On error, an exception smob
498 Note: In the end it may be better to call out to Scheme once, and then
499 do all of the lookup from Scheme. TBD. */
502 ppscm_find_pretty_printer (SCM value
)
506 /* Look at the pretty-printer list for each objfile
507 in the current program-space. */
508 pp
= ppscm_find_pretty_printer_from_objfiles (value
);
509 /* Note: This will return if function is a <gdb:exception> object,
510 which is what we want. */
511 if (gdbscm_is_true (pp
))
514 /* Look at the pretty-printer list for the current program-space. */
515 pp
= ppscm_find_pretty_printer_from_progspace (value
);
516 /* Note: This will return if function is a <gdb:exception> object,
517 which is what we want. */
518 if (gdbscm_is_true (pp
))
521 /* Look at the pretty-printer list in the gdb module. */
522 pp
= ppscm_find_pretty_printer_from_gdb (value
);
526 /* Pretty-print a single value, via the PRINTER, which must be a
527 <gdb:pretty-printer-worker> object.
528 The caller is responsible for ensuring PRINTER is valid.
529 If the function returns a string, an SCM containing the string
530 is returned. If the function returns #f that means the pretty
531 printer returned #f as a value. Otherwise, if the function returns a
532 <gdb:value> object, *OUT_VALUE is set to the value and #t is returned.
533 It is an error if the printer returns #t.
534 On error, an exception smob is returned. */
537 ppscm_pretty_print_one_value (SCM printer
, struct value
**out_value
,
538 struct gdbarch
*gdbarch
,
539 const struct language_defn
*language
)
541 volatile struct gdb_exception except
;
542 SCM result
= SCM_BOOL_F
;
545 TRY_CATCH (except
, RETURN_MASK_ALL
)
548 pretty_printer_worker_smob
*w_smob
549 = (pretty_printer_worker_smob
*) SCM_SMOB_DATA (printer
);
551 result
= gdbscm_safe_call_1 (w_smob
->to_string
, printer
,
552 gdbscm_memory_error_p
);
553 if (gdbscm_is_false (result
))
555 else if (scm_is_string (result
)
556 || lsscm_is_lazy_string (result
))
558 else if (vlscm_is_value (result
))
563 = vlscm_convert_value_from_scheme (FUNC_NAME
, GDBSCM_ARG_NONE
,
566 if (*out_value
!= NULL
)
571 else if (gdbscm_is_exception (result
))
575 /* Invalid result from to-string. */
576 result
= ppscm_make_pp_type_error_exception
577 (_("invalid result from pretty-printer to-string"), result
);
584 /* Return the display hint for PRINTER as a Scheme object.
585 The caller is responsible for ensuring PRINTER is a
586 <gdb:pretty-printer-worker> object. */
589 ppscm_get_display_hint_scm (SCM printer
)
591 pretty_printer_worker_smob
*w_smob
592 = (pretty_printer_worker_smob
*) SCM_SMOB_DATA (printer
);
594 return w_smob
->display_hint
;
597 /* Return the display hint for the pretty-printer PRINTER.
598 The caller is responsible for ensuring PRINTER is a
599 <gdb:pretty-printer-worker> object.
600 Returns the display hint or #f if the hint is not a string. */
602 static enum display_hint
603 ppscm_get_display_hint_enum (SCM printer
)
605 SCM hint
= ppscm_get_display_hint_scm (printer
);
607 if (gdbscm_is_false (hint
))
609 if (scm_is_string (hint
))
611 if (gdbscm_is_true (scm_string_equal_p (hint
, ppscm_array_string
)))
613 if (gdbscm_is_true (scm_string_equal_p (hint
, ppscm_map_string
)))
615 if (gdbscm_is_true (scm_string_equal_p (hint
, ppscm_string_string
)))
622 /* A wrapper for gdbscm_print_gdb_exception that ignores memory errors.
623 EXCEPTION is a <gdb:exception> object. */
626 ppscm_print_exception_unless_memory_error (SCM exception
,
627 struct ui_file
*stream
)
629 if (gdbscm_memory_error_p (gdbscm_exception_key (exception
)))
631 char *msg
= gdbscm_exception_message_to_string (exception
);
632 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
634 /* This "shouldn't happen", but play it safe. */
635 if (msg
== NULL
|| *msg
== '\0')
636 fprintf_filtered (stream
, _("<error reading variable>"));
639 /* Remove the trailing newline. We could instead call a special
640 routine for printing memory error messages, but this is easy
642 size_t len
= strlen (msg
);
644 if (msg
[len
- 1] == '\n')
646 fprintf_filtered (stream
, _("<error reading variable: %s>"), msg
);
649 do_cleanups (cleanup
);
652 gdbscm_print_gdb_exception (SCM_BOOL_F
, exception
);
655 /* Helper for gdbscm_apply_val_pretty_printer which calls to_string and
656 formats the result. */
658 static enum string_repr_result
659 ppscm_print_string_repr (SCM printer
, enum display_hint hint
,
660 struct ui_file
*stream
, int recurse
,
661 const struct value_print_options
*options
,
662 struct gdbarch
*gdbarch
,
663 const struct language_defn
*language
)
665 struct value
*replacement
= NULL
;
667 enum string_repr_result result
= STRING_REPR_ERROR
;
669 str_scm
= ppscm_pretty_print_one_value (printer
, &replacement
,
671 if (gdbscm_is_false (str_scm
))
673 result
= STRING_REPR_NONE
;
675 else if (scm_is_eq (str_scm
, SCM_BOOL_T
))
677 struct value_print_options opts
= *options
;
679 gdb_assert (replacement
!= NULL
);
680 opts
.addressprint
= 0;
681 common_val_print (replacement
, stream
, recurse
, &opts
, language
);
682 result
= STRING_REPR_OK
;
684 else if (scm_is_string (str_scm
))
686 struct cleanup
*cleanup
;
689 = gdbscm_scm_to_string (str_scm
, &length
,
690 target_charset (gdbarch
), 0 /*!strict*/, NULL
);
692 cleanup
= make_cleanup (xfree
, string
);
693 if (hint
== HINT_STRING
)
695 struct type
*type
= builtin_type (gdbarch
)->builtin_char
;
697 LA_PRINT_STRING (stream
, type
, (gdb_byte
*) string
,
698 length
, NULL
, 0, options
);
702 /* Alas scm_to_stringn doesn't nul-terminate the string if we
703 ask for the length. */
706 for (i
= 0; i
< length
; ++i
)
708 if (string
[i
] == '\0')
709 fputs_filtered ("\\000", stream
);
711 fputc_filtered (string
[i
], stream
);
714 result
= STRING_REPR_OK
;
715 do_cleanups (cleanup
);
717 else if (lsscm_is_lazy_string (str_scm
))
719 struct value_print_options local_opts
= *options
;
721 local_opts
.addressprint
= 0;
722 lsscm_val_print_lazy_string (str_scm
, stream
, &local_opts
);
723 result
= STRING_REPR_OK
;
727 gdb_assert (gdbscm_is_exception (str_scm
));
728 ppscm_print_exception_unless_memory_error (str_scm
, stream
);
729 result
= STRING_REPR_ERROR
;
735 /* Helper for gdbscm_apply_val_pretty_printer that formats children of the
736 printer, if any exist.
737 The caller is responsible for ensuring PRINTER is a printer smob.
738 If PRINTED_NOTHING is true, then nothing has been printed by to_string,
739 and format output accordingly. */
742 ppscm_print_children (SCM printer
, enum display_hint hint
,
743 struct ui_file
*stream
, int recurse
,
744 const struct value_print_options
*options
,
745 struct gdbarch
*gdbarch
,
746 const struct language_defn
*language
,
749 pretty_printer_worker_smob
*w_smob
750 = (pretty_printer_worker_smob
*) SCM_SMOB_DATA (printer
);
751 int is_map
, is_array
, done_flag
, pretty
;
753 SCM children
, status
;
754 SCM iter
= SCM_BOOL_F
; /* -Wall */
755 struct cleanup
*cleanups
;
757 if (gdbscm_is_false (w_smob
->children
))
759 if (!gdbscm_is_procedure (w_smob
->children
))
761 ppscm_print_pp_type_error
762 (_("pretty-printer \"children\" object is not a procedure or #f"),
767 cleanups
= make_cleanup (null_cleanup
, NULL
);
769 /* If we are printing a map or an array, we want special formatting. */
770 is_map
= hint
== HINT_MAP
;
771 is_array
= hint
== HINT_ARRAY
;
773 children
= gdbscm_safe_call_1 (w_smob
->children
, printer
,
774 gdbscm_memory_error_p
);
775 if (gdbscm_is_exception (children
))
777 ppscm_print_exception_unless_memory_error (children
, stream
);
780 /* We combine two steps here: get children, make an iterator out of them.
781 This simplifies things because there's no language means of creating
782 iterators, and it's the printer object that knows how it will want its
783 children iterated over. */
784 if (!itscm_is_iterator (children
))
786 ppscm_print_pp_type_error
787 (_("result of pretty-printer \"children\" procedure is not"
788 " a <gdb:iterator> object"), children
);
793 /* Use the prettyformat_arrays option if we are printing an array,
794 and the pretty option otherwise. */
796 pretty
= options
->prettyformat_arrays
;
799 if (options
->prettyformat
== Val_prettyformat
)
802 pretty
= options
->prettyformat_structs
;
806 for (i
= 0; i
< options
->print_max
; ++i
)
811 SCM item
= itscm_safe_call_next_x (iter
, gdbscm_memory_error_p
);
812 struct cleanup
*inner_cleanup
= make_cleanup (null_cleanup
, NULL
);
814 if (gdbscm_is_exception (item
))
816 ppscm_print_exception_unless_memory_error (item
, stream
);
819 if (itscm_is_end_of_iteration (item
))
821 /* Set a flag so we can know whether we printed all the
822 available elements. */
827 if (! scm_is_pair (item
))
829 ppscm_print_pp_type_error
830 (_("result of pretty-printer children iterator is not a pair"
831 " or (end-of-iteration)"),
835 scm_name
= scm_car (item
);
836 v_scm
= scm_cdr (item
);
837 if (!scm_is_string (scm_name
))
839 ppscm_print_pp_type_error
840 (_("first element of pretty-printer children iterator is not"
844 name
= gdbscm_scm_to_c_string (scm_name
);
845 make_cleanup (xfree
, name
);
847 /* Print initial "{". For other elements, there are three cases:
848 1. Maps. Print a "," after each value element.
849 2. Arrays. Always print a ",".
850 3. Other. Always print a ",". */
854 fputs_filtered ("{", stream
);
856 fputs_filtered (" = {", stream
);
859 else if (! is_map
|| i
% 2 == 0)
860 fputs_filtered (pretty
? "," : ", ", stream
);
862 /* In summary mode, we just want to print "= {...}" if there is
864 if (options
->summary
)
866 /* This increment tricks the post-loop logic to print what
874 if (! is_map
|| i
% 2 == 0)
878 fputs_filtered ("\n", stream
);
879 print_spaces_filtered (2 + 2 * recurse
, stream
);
882 wrap_here (n_spaces (2 + 2 *recurse
));
885 if (is_map
&& i
% 2 == 0)
886 fputs_filtered ("[", stream
);
889 /* We print the index, not whatever the child method
890 returned as the name. */
891 if (options
->print_array_indexes
)
892 fprintf_filtered (stream
, "[%d] = ", i
);
896 fputs_filtered (name
, stream
);
897 fputs_filtered (" = ", stream
);
900 if (lsscm_is_lazy_string (v_scm
))
902 struct value_print_options local_opts
= *options
;
904 local_opts
.addressprint
= 0;
905 lsscm_val_print_lazy_string (v_scm
, stream
, &local_opts
);
907 else if (scm_is_string (v_scm
))
909 char *output
= gdbscm_scm_to_c_string (v_scm
);
911 fputs_filtered (output
, stream
);
918 = vlscm_convert_value_from_scheme (FUNC_NAME
, GDBSCM_ARG_NONE
,
924 ppscm_print_exception_unless_memory_error (except_scm
, stream
);
927 common_val_print (value
, stream
, recurse
+ 1, options
, language
);
930 if (is_map
&& i
% 2 == 0)
931 fputs_filtered ("] = ", stream
);
933 do_cleanups (inner_cleanup
);
942 fputs_filtered ("\n", stream
);
943 print_spaces_filtered (2 + 2 * recurse
, stream
);
945 fputs_filtered ("...", stream
);
949 fputs_filtered ("\n", stream
);
950 print_spaces_filtered (2 * recurse
, stream
);
952 fputs_filtered ("}", stream
);
956 do_cleanups (cleanups
);
958 /* Play it safe, make sure ITER doesn't get GC'd. */
959 scm_remember_upto_here_1 (iter
);
962 /* This is the extension_language_ops.apply_val_pretty_printer "method". */
965 gdbscm_apply_val_pretty_printer (const struct extension_language_defn
*extlang
,
966 struct type
*type
, const gdb_byte
*valaddr
,
967 int embedded_offset
, CORE_ADDR address
,
968 struct ui_file
*stream
, int recurse
,
969 const struct value
*val
,
970 const struct value_print_options
*options
,
971 const struct language_defn
*language
)
973 struct gdbarch
*gdbarch
= get_type_arch (type
);
974 SCM exception
= SCM_BOOL_F
;
975 SCM printer
= SCM_BOOL_F
;
976 SCM val_obj
= SCM_BOOL_F
;
978 enum display_hint hint
;
979 struct cleanup
*cleanups
;
980 int result
= EXT_LANG_RC_NOP
;
981 enum string_repr_result print_result
;
983 /* No pretty-printer support for unavailable values. */
984 if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
985 return EXT_LANG_RC_NOP
;
987 if (!gdb_scheme_initialized
)
988 return EXT_LANG_RC_NOP
;
990 cleanups
= make_cleanup (null_cleanup
, NULL
);
992 /* Instantiate the printer. */
994 valaddr
+= embedded_offset
;
995 value
= value_from_contents_and_address (type
, valaddr
,
996 address
+ embedded_offset
);
998 set_value_component_location (value
, val
);
999 /* set_value_component_location resets the address, so we may
1000 need to set it again. */
1001 if (VALUE_LVAL (value
) != lval_internalvar
1002 && VALUE_LVAL (value
) != lval_internalvar_component
1003 && VALUE_LVAL (value
) != lval_computed
)
1004 set_value_address (value
, address
+ embedded_offset
);
1006 val_obj
= vlscm_scm_from_value (value
);
1007 if (gdbscm_is_exception (val_obj
))
1009 exception
= val_obj
;
1010 result
= EXT_LANG_RC_ERROR
;
1014 printer
= ppscm_find_pretty_printer (val_obj
);
1016 if (gdbscm_is_exception (printer
))
1018 exception
= printer
;
1019 result
= EXT_LANG_RC_ERROR
;
1022 if (gdbscm_is_false (printer
))
1024 result
= EXT_LANG_RC_NOP
;
1027 gdb_assert (ppscm_is_pretty_printer_worker (printer
));
1029 /* If we are printing a map, we want some special formatting. */
1030 hint
= ppscm_get_display_hint_enum (printer
);
1031 if (hint
== HINT_ERROR
)
1033 /* Print the error as an exception for consistency. */
1034 SCM hint_scm
= ppscm_get_display_hint_scm (printer
);
1036 ppscm_print_pp_type_error ("Invalid display hint", hint_scm
);
1037 /* Fall through. A bad hint doesn't stop pretty-printing. */
1041 /* Print the section. */
1042 print_result
= ppscm_print_string_repr (printer
, hint
, stream
, recurse
,
1043 options
, gdbarch
, language
);
1044 if (print_result
!= STRING_REPR_ERROR
)
1046 ppscm_print_children (printer
, hint
, stream
, recurse
, options
,
1048 print_result
== STRING_REPR_NONE
);
1051 result
= EXT_LANG_RC_OK
;
1054 if (gdbscm_is_exception (exception
))
1055 ppscm_print_exception_unless_memory_error (exception
, stream
);
1056 do_cleanups (cleanups
);
1060 /* Initialize the Scheme pretty-printer code. */
1062 static const scheme_function pretty_printer_functions
[] =
1064 { "make-pretty-printer", 2, 0, 0, gdbscm_make_pretty_printer
,
1066 Create a <gdb:pretty-printer> object.\n\
1068 Arguments: name lookup\n\
1069 name: a string naming the matcher\n\
1070 lookup: a procedure:\n\
1071 (pretty-printer <gdb:value>) -> <gdb:pretty-printer-worker> | #f." },
1073 { "pretty-printer?", 1, 0, 0, gdbscm_pretty_printer_p
,
1075 Return #t if the object is a <gdb:pretty-printer> object." },
1077 { "pretty-printer-enabled?", 1, 0, 0, gdbscm_pretty_printer_enabled_p
,
1079 Return #t if the pretty-printer is enabled." },
1081 { "set-pretty-printer-enabled!", 2, 0, 0,
1082 gdbscm_set_pretty_printer_enabled_x
,
1084 Set the enabled flag of the pretty-printer.\n\
1085 Returns \"unspecified\"." },
1087 { "make-pretty-printer-worker", 3, 0, 0, gdbscm_make_pretty_printer_worker
,
1089 Create a <gdb:pretty-printer-worker> object.\n\
1091 Arguments: display-hint to-string children\n\
1092 display-hint: either #f or one of \"array\", \"map\", or \"string\"\n\
1093 to-string: a procedure:\n\
1094 (pretty-printer) -> string | #f | <gdb:value>\n\
1095 children: either #f or a procedure:\n\
1096 (pretty-printer) -> <gdb:iterator>" },
1098 { "pretty-printer-worker?", 1, 0, 0, gdbscm_pretty_printer_worker_p
,
1100 Return #t if the object is a <gdb:pretty-printer-worker> object." },
1106 gdbscm_initialize_pretty_printers (void)
1108 pretty_printer_smob_tag
1109 = gdbscm_make_smob_type (pretty_printer_smob_name
,
1110 sizeof (pretty_printer_smob
));
1111 scm_set_smob_mark (pretty_printer_smob_tag
,
1112 ppscm_mark_pretty_printer_smob
);
1113 scm_set_smob_print (pretty_printer_smob_tag
,
1114 ppscm_print_pretty_printer_smob
);
1116 pretty_printer_worker_smob_tag
1117 = gdbscm_make_smob_type (pretty_printer_worker_smob_name
,
1118 sizeof (pretty_printer_worker_smob
));
1119 scm_set_smob_mark (pretty_printer_worker_smob_tag
,
1120 ppscm_mark_pretty_printer_worker_smob
);
1121 scm_set_smob_print (pretty_printer_worker_smob_tag
,
1122 ppscm_print_pretty_printer_worker_smob
);
1124 gdbscm_define_functions (pretty_printer_functions
, 1);
1126 scm_c_define (pretty_printer_list_name
, SCM_EOL
);
1128 pretty_printer_list_var
1129 = scm_c_private_variable (gdbscm_module_name
,
1130 pretty_printer_list_name
);
1131 gdb_assert (!gdbscm_is_false (pretty_printer_list_var
));
1133 pp_type_error_symbol
= scm_from_latin1_symbol ("gdb:pp-type-error");
1135 ppscm_map_string
= scm_from_latin1_string ("map");
1136 ppscm_array_string
= scm_from_latin1_string ("array");
1137 ppscm_string_string
= scm_from_latin1_string ("string");