Bug 14363 - Pretty-Printing of cyclic references produces infinite recursion
Summary: Pretty-Printing of cyclic references produces infinite recursion
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 7.4
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on: 30816
  Show dependency treegraph
Reported: 2012-07-13 18:18 UTC by Oliver Buchtala
Modified: 2023-08-31 20:01 UTC (History)
3 users (show)

See Also:
Last reconfirmed:

minimalistic c++ example sourcce (143 bytes, text/x-c++src)
2012-07-13 18:45 UTC, Oliver Buchtala
pretty printer implementation (742 bytes, text/x-python)
2012-07-13 18:46 UTC, Oliver Buchtala
this patch implements proposal I (893 bytes, patch)
2012-08-01 20:08 UTC, Oliver Buchtala
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Oliver Buchtala 2012-07-13 18:18:01 UTC
I have a struct Hash which can contain Hashes as values.
This way one can produce a cyclic reference adding two hashes to each other.

Such a cyclic produces an infinite recursion during pretty-printing of children.
Comment 1 Oliver Buchtala 2012-07-13 18:45:05 UTC
Created attachment 6528 [details]
minimalistic c++ example sourcce
Comment 2 Oliver Buchtala 2012-07-13 18:46:06 UTC
Created attachment 6529 [details]
pretty printer implementation
Comment 3 Oliver Buchtala 2012-08-01 18:22:15 UTC
I have had a glance at the python api source and want to add some proposals:

print_children (PyObject *printer, const char *hint,
		struct ui_file *stream, int recurse,
		const struct value_print_options *options,
		const struct language_defn *language,
		int is_py_none)

I. Let the pretty-printer know about the current recursion level

this would give the pretty-printer more control about how deep it provides children.

II. Handle Visited information

A value has a location (struct) that contains the address which could be collected in a set of visited addresses.
The set would be initialized on recurse==0.

A visited flag could be set in the gdb.Value, leaving handling this to the pretty-printer, or recursion stopped based on this information, and maybe based on gdb pretty-print options:
   common_val_print (value, stream, recurse + 1, options, language);
Comment 4 Oliver Buchtala 2012-08-01 20:08:01 UTC
Created attachment 6564 [details]
this patch implements proposal I
Comment 5 Phil Muldoon 2012-08-02 06:14:00 UTC
Comment on attachment 6564 [details]
this patch implements proposal I

Thanks for the bug report, and the patch.  If you want to submit this as a solution, it would be great if you could post it to gdb-patched@sourceware.org for review.  It will receive the attention if deserves there.  

There is a brief guide to contributions formats and processes here

Comment 6 Phil Muldoon 2012-08-02 06:39:13 UTC
Sorry, typo, that should be:

Comment 7 Tom Tromey 2023-08-31 17:00:46 UTC
Giving printers better access to some gdb printing context
has been requested in a few contexts now.