Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / src / debug.cc
blob188495a3ea8fb14847e6cee4499eb02b0dd941a6
1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 #include <debug/debug.h>
27 #include <debug/safe_sequence.h>
28 #include <debug/safe_iterator.h>
29 #include <algorithm>
30 #include <cassert>
31 #include <cstring>
32 #include <cctype>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <functional>
37 using namespace std;
39 namespace
41 /** Returns different instances of __mutex depending on the passed address
42 * in order to limit contention without breaking current library binary
43 * compatibility. */
44 __gnu_cxx::__mutex&
45 get_safe_base_mutex(void* __address)
47 const size_t mask = 0xf;
48 static __gnu_cxx::__mutex safe_base_mutex[mask + 1];
49 const size_t index = _Hash_impl::hash(__address) & mask;
50 return safe_base_mutex[index];
53 void
54 swap_seq(__gnu_debug::_Safe_sequence_base& __lhs,
55 __gnu_debug::_Safe_sequence_base& __rhs)
57 swap(__lhs._M_iterators, __rhs._M_iterators);
58 swap(__lhs._M_const_iterators, __rhs._M_const_iterators);
59 swap(__lhs._M_version, __rhs._M_version);
60 __gnu_debug::_Safe_iterator_base* __iter;
61 for (__iter = __rhs._M_iterators; __iter; __iter = __iter->_M_next)
62 __iter->_M_sequence = &__rhs;
63 for (__iter = __lhs._M_iterators; __iter; __iter = __iter->_M_next)
64 __iter->_M_sequence = &__lhs;
65 for (__iter = __rhs._M_const_iterators; __iter; __iter = __iter->_M_next)
66 __iter->_M_sequence = &__rhs;
67 for (__iter = __lhs._M_const_iterators; __iter; __iter = __iter->_M_next)
68 __iter->_M_sequence = &__lhs;
70 } // anonymous namespace
72 namespace __gnu_debug
74 const char* _S_debug_messages[] =
76 "function requires a valid iterator range [%1.name;, %2.name;)",
77 "attempt to insert into container with a singular iterator",
78 "attempt to insert into container with an iterator"
79 " from a different container",
80 "attempt to erase from container with a %2.state; iterator",
81 "attempt to erase from container with an iterator"
82 " from a different container",
83 "attempt to subscript container with out-of-bounds index %2;,"
84 " but container only holds %3; elements",
85 "attempt to access an element in an empty container",
86 "elements in iterator range [%1.name;, %2.name;)"
87 " are not partitioned by the value %3;",
88 "elements in iterator range [%1.name;, %2.name;)"
89 " are not partitioned by the predicate %3; and value %4;",
90 "elements in iterator range [%1.name;, %2.name;) are not sorted",
91 "elements in iterator range [%1.name;, %2.name;)"
92 " are not sorted according to the predicate %3;",
93 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
94 "elements in iterator range [%1.name;, %2.name;)"
95 " do not form a heap with respect to the predicate %3;",
96 "attempt to write through a singular bitset reference",
97 "attempt to read from a singular bitset reference",
98 "attempt to flip a singular bitset reference",
99 "attempt to splice a list into itself",
100 "attempt to splice lists with inequal allocators",
101 "attempt to splice elements referenced by a %1.state; iterator",
102 "attempt to splice an iterator from a different container",
103 "splice destination %1.name;"
104 " occurs within source range [%2.name;, %3.name;)",
105 "attempt to initialize an iterator that will immediately become singular",
106 "attempt to copy-construct an iterator from a singular iterator",
107 "attempt to construct a constant iterator"
108 " from a singular mutable iterator",
109 "attempt to copy from a singular iterator",
110 "attempt to dereference a %1.state; iterator",
111 "attempt to increment a %1.state; iterator",
112 "attempt to decrement a %1.state; iterator",
113 "attempt to subscript a %1.state; iterator %2; step from"
114 " its current position, which falls outside its dereferenceable range",
115 "attempt to advance a %1.state; iterator %2; steps,"
116 " which falls outside its valid range",
117 "attempt to retreat a %1.state; iterator %2; steps,"
118 " which falls outside its valid range",
119 "attempt to compare a %1.state; iterator to a %2.state; iterator",
120 "attempt to compare iterators from different sequences",
121 "attempt to order a %1.state; iterator to a %2.state; iterator",
122 "attempt to order iterators from different sequences",
123 "attempt to compute the difference between a %1.state;"
124 " iterator to a %2.state; iterator",
125 "attempt to compute the different between two iterators"
126 " from different sequences",
127 "attempt to dereference an end-of-stream istream_iterator",
128 "attempt to increment an end-of-stream istream_iterator",
129 "attempt to output via an ostream_iterator with no associated stream",
130 "attempt to dereference an end-of-stream istreambuf_iterator"
131 " (this is a GNU extension)",
132 "attempt to increment an end-of-stream istreambuf_iterator",
133 "attempt to insert into container after an end iterator",
134 "attempt to erase from container after a %2.state; iterator not followed"
135 " by a dereferenceable one",
136 "function requires a valid iterator range (%2.name;, %3.name;)"
137 ", \"%2.name;\" shall be before and not equal to \"%3.name;\""
140 void
141 _Safe_sequence_base::
142 _M_detach_all()
144 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
145 for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
147 _Safe_iterator_base* __old = __iter;
148 __iter = __iter->_M_next;
149 __old->_M_reset();
151 _M_iterators = 0;
153 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
155 _Safe_iterator_base* __old = __iter2;
156 __iter2 = __iter2->_M_next;
157 __old->_M_reset();
159 _M_const_iterators = 0;
162 void
163 _Safe_sequence_base::
164 _M_detach_singular()
166 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
167 for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
169 _Safe_iterator_base* __old = __iter;
170 __iter = __iter->_M_next;
171 if (__old->_M_singular())
172 __old->_M_detach_single();
175 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
177 _Safe_iterator_base* __old = __iter2;
178 __iter2 = __iter2->_M_next;
179 if (__old->_M_singular())
180 __old->_M_detach_single();
184 void
185 _Safe_sequence_base::
186 _M_revalidate_singular()
188 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
189 for (_Safe_iterator_base* __iter = _M_iterators; __iter;
190 __iter = __iter->_M_next)
191 __iter->_M_version = _M_version;
193 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;
194 __iter2 = __iter2->_M_next)
195 __iter2->_M_version = _M_version;
198 void
199 _Safe_sequence_base::
200 _M_swap(_Safe_sequence_base& __x)
202 // We need to lock both sequences to swap
203 using namespace __gnu_cxx;
204 __mutex *__this_mutex = &_M_get_mutex();
205 __mutex *__x_mutex = &__x._M_get_mutex();
206 if (__this_mutex == __x_mutex)
208 __scoped_lock __lock(*__this_mutex);
209 swap_seq(*this, __x);
211 else
213 __scoped_lock __l1(__this_mutex < __x_mutex
214 ? *__this_mutex : *__x_mutex);
215 __scoped_lock __l2(__this_mutex < __x_mutex
216 ? *__x_mutex : *__this_mutex);
217 swap_seq(*this, __x);
221 __gnu_cxx::__mutex&
222 _Safe_sequence_base::
223 _M_get_mutex() throw ()
224 { return get_safe_base_mutex(this); }
226 void
227 _Safe_sequence_base::
228 _M_attach(_Safe_iterator_base* __it, bool __constant)
230 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
231 _M_attach_single(__it, __constant);
234 void
235 _Safe_sequence_base::
236 _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ()
238 _Safe_iterator_base*& __its =
239 __constant ? _M_const_iterators : _M_iterators;
240 __it->_M_next = __its;
241 if (__it->_M_next)
242 __it->_M_next->_M_prior = __it;
243 __its = __it;
246 void
247 _Safe_sequence_base::
248 _M_detach(_Safe_iterator_base* __it)
250 // Remove __it from this sequence's list
251 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
252 _M_detach_single(__it);
255 void
256 _Safe_sequence_base::
257 _M_detach_single(_Safe_iterator_base* __it) throw ()
259 // Remove __it from this sequence's list
260 if (__it->_M_prior)
261 __it->_M_prior->_M_next = __it->_M_next;
262 if (__it->_M_next)
263 __it->_M_next->_M_prior = __it->_M_prior;
265 if (_M_const_iterators == __it)
266 _M_const_iterators = __it->_M_next;
267 if (_M_iterators == __it)
268 _M_iterators = __it->_M_next;
271 void
272 _Safe_iterator_base::
273 _M_attach(_Safe_sequence_base* __seq, bool __constant)
275 _M_detach();
277 // Attach to the new sequence (if there is one)
278 if (__seq)
280 _M_sequence = __seq;
281 _M_version = _M_sequence->_M_version;
282 _M_sequence->_M_attach(this, __constant);
286 void
287 _Safe_iterator_base::
288 _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ()
290 _M_detach_single();
292 // Attach to the new sequence (if there is one)
293 if (__seq)
295 _M_sequence = __seq;
296 _M_version = _M_sequence->_M_version;
297 _M_sequence->_M_attach_single(this, __constant);
301 void
302 _Safe_iterator_base::
303 _M_detach()
305 if (_M_sequence)
307 _M_sequence->_M_detach(this);
310 _M_reset();
313 void
314 _Safe_iterator_base::
315 _M_detach_single() throw ()
317 if (_M_sequence)
319 _M_sequence->_M_detach_single(this);
322 _M_reset();
325 void
326 _Safe_iterator_base::
327 _M_reset() throw ()
329 _M_sequence = 0;
330 _M_version = 0;
331 _M_prior = 0;
332 _M_next = 0;
335 bool
336 _Safe_iterator_base::
337 _M_singular() const throw ()
338 { return !_M_sequence || _M_version != _M_sequence->_M_version; }
340 bool
341 _Safe_iterator_base::
342 _M_can_compare(const _Safe_iterator_base& __x) const throw ()
344 return (!_M_singular()
345 && !__x._M_singular() && _M_sequence == __x._M_sequence);
348 __gnu_cxx::__mutex&
349 _Safe_iterator_base::
350 _M_get_mutex() throw ()
351 { return get_safe_base_mutex(_M_sequence); }
353 void
354 _Error_formatter::_Parameter::
355 _M_print_field(const _Error_formatter* __formatter, const char* __name) const
357 assert(this->_M_kind != _Parameter::__unused_param);
358 const int __bufsize = 64;
359 char __buf[__bufsize];
361 if (_M_kind == __iterator)
363 if (strcmp(__name, "name") == 0)
365 assert(_M_variant._M_iterator._M_name);
366 __formatter->_M_print_word(_M_variant._M_iterator._M_name);
368 else if (strcmp(__name, "address") == 0)
370 __formatter->_M_format_word(__buf, __bufsize, "%p",
371 _M_variant._M_iterator._M_address);
372 __formatter->_M_print_word(__buf);
374 else if (strcmp(__name, "type") == 0)
376 if (!_M_variant._M_iterator._M_type)
377 __formatter->_M_print_word("<unknown type>");
378 else
379 // TBD: demangle!
380 __formatter->_M_print_word(_M_variant._M_iterator.
381 _M_type->name());
383 else if (strcmp(__name, "constness") == 0)
385 static const char* __constness_names[__last_constness] =
387 "<unknown>",
388 "constant",
389 "mutable"
391 __formatter->_M_print_word(__constness_names[_M_variant.
392 _M_iterator.
393 _M_constness]);
395 else if (strcmp(__name, "state") == 0)
397 static const char* __state_names[__last_state] =
399 "<unknown>",
400 "singular",
401 "dereferenceable (start-of-sequence)",
402 "dereferenceable",
403 "past-the-end",
404 "before-begin"
406 __formatter->_M_print_word(__state_names[_M_variant.
407 _M_iterator._M_state]);
409 else if (strcmp(__name, "sequence") == 0)
411 assert(_M_variant._M_iterator._M_sequence);
412 __formatter->_M_format_word(__buf, __bufsize, "%p",
413 _M_variant._M_iterator._M_sequence);
414 __formatter->_M_print_word(__buf);
416 else if (strcmp(__name, "seq_type") == 0)
418 if (!_M_variant._M_iterator._M_seq_type)
419 __formatter->_M_print_word("<unknown seq_type>");
420 else
421 // TBD: demangle!
422 __formatter->_M_print_word(_M_variant._M_iterator.
423 _M_seq_type->name());
425 else
426 assert(false);
428 else if (_M_kind == __sequence)
430 if (strcmp(__name, "name") == 0)
432 assert(_M_variant._M_sequence._M_name);
433 __formatter->_M_print_word(_M_variant._M_sequence._M_name);
435 else if (strcmp(__name, "address") == 0)
437 assert(_M_variant._M_sequence._M_address);
438 __formatter->_M_format_word(__buf, __bufsize, "%p",
439 _M_variant._M_sequence._M_address);
440 __formatter->_M_print_word(__buf);
442 else if (strcmp(__name, "type") == 0)
444 if (!_M_variant._M_sequence._M_type)
445 __formatter->_M_print_word("<unknown type>");
446 else
447 // TBD: demangle!
448 __formatter->_M_print_word(_M_variant._M_sequence.
449 _M_type->name());
451 else
452 assert(false);
454 else if (_M_kind == __integer)
456 if (strcmp(__name, "name") == 0)
458 assert(_M_variant._M_integer._M_name);
459 __formatter->_M_print_word(_M_variant._M_integer._M_name);
461 else
462 assert(false);
464 else if (_M_kind == __string)
466 if (strcmp(__name, "name") == 0)
468 assert(_M_variant._M_string._M_name);
469 __formatter->_M_print_word(_M_variant._M_string._M_name);
471 else
472 assert(false);
474 else
476 assert(false);
480 void
481 _Error_formatter::_Parameter::
482 _M_print_description(const _Error_formatter* __formatter) const
484 const int __bufsize = 128;
485 char __buf[__bufsize];
487 if (_M_kind == __iterator)
489 __formatter->_M_print_word("iterator ");
490 if (_M_variant._M_iterator._M_name)
492 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
493 _M_variant._M_iterator._M_name);
494 __formatter->_M_print_word(__buf);
497 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
498 _M_variant._M_iterator._M_address);
499 __formatter->_M_print_word(__buf);
500 if (_M_variant._M_iterator._M_type)
502 __formatter->_M_print_word("type = ");
503 _M_print_field(__formatter, "type");
505 if (_M_variant._M_iterator._M_constness != __unknown_constness)
507 __formatter->_M_print_word(" (");
508 _M_print_field(__formatter, "constness");
509 __formatter->_M_print_word(" iterator)");
511 __formatter->_M_print_word(";\n");
514 if (_M_variant._M_iterator._M_state != __unknown_state)
516 __formatter->_M_print_word(" state = ");
517 _M_print_field(__formatter, "state");
518 __formatter->_M_print_word(";\n");
521 if (_M_variant._M_iterator._M_sequence)
523 __formatter->_M_print_word(" references sequence ");
524 if (_M_variant._M_iterator._M_seq_type)
526 __formatter->_M_print_word("with type `");
527 _M_print_field(__formatter, "seq_type");
528 __formatter->_M_print_word("' ");
531 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n",
532 _M_variant._M_sequence._M_address);
533 __formatter->_M_print_word(__buf);
535 __formatter->_M_print_word("}\n");
537 else if (_M_kind == __sequence)
539 __formatter->_M_print_word("sequence ");
540 if (_M_variant._M_sequence._M_name)
542 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
543 _M_variant._M_sequence._M_name);
544 __formatter->_M_print_word(__buf);
547 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
548 _M_variant._M_sequence._M_address);
549 __formatter->_M_print_word(__buf);
551 if (_M_variant._M_sequence._M_type)
553 __formatter->_M_print_word(" type = ");
554 _M_print_field(__formatter, "type");
555 __formatter->_M_print_word(";\n");
557 __formatter->_M_print_word("}\n");
561 const _Error_formatter&
562 _Error_formatter::_M_message(_Debug_msg_id __id) const throw ()
563 { return this->_M_message(_S_debug_messages[__id]); }
565 void
566 _Error_formatter::_M_error() const
568 const int __bufsize = 128;
569 char __buf[__bufsize];
571 // Emit file & line number information
572 _M_column = 1;
573 _M_wordwrap = false;
574 if (_M_file)
576 _M_format_word(__buf, __bufsize, "%s:", _M_file);
577 _M_print_word(__buf);
578 _M_column += strlen(__buf);
581 if (_M_line > 0)
583 _M_format_word(__buf, __bufsize, "%u:", _M_line);
584 _M_print_word(__buf);
585 _M_column += strlen(__buf);
588 if (_M_max_length)
589 _M_wordwrap = true;
590 _M_print_word("error: ");
592 // Print the error message
593 assert(_M_text);
594 _M_print_string(_M_text);
595 _M_print_word(".\n");
597 // Emit descriptions of the objects involved in the operation
598 _M_wordwrap = false;
599 bool __has_noninteger_parameters = false;
600 for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
602 if (_M_parameters[__i]._M_kind == _Parameter::__iterator
603 || _M_parameters[__i]._M_kind == _Parameter::__sequence)
605 if (!__has_noninteger_parameters)
607 _M_first_line = true;
608 _M_print_word("\nObjects involved in the operation:\n");
609 __has_noninteger_parameters = true;
611 _M_parameters[__i]._M_print_description(this);
615 abort();
618 template<typename _Tp>
619 void
620 _Error_formatter::_M_format_word(char* __buf,
621 int __n __attribute__ ((__unused__)),
622 const char* __fmt, _Tp __s) const throw ()
624 #ifdef _GLIBCXX_USE_C99
625 std::snprintf(__buf, __n, __fmt, __s);
626 #else
627 std::sprintf(__buf, __fmt, __s);
628 #endif
632 void
633 _Error_formatter::_M_print_word(const char* __word) const
635 if (!_M_wordwrap)
637 fprintf(stderr, "%s", __word);
638 return;
641 size_t __length = strlen(__word);
642 if (__length == 0)
643 return;
645 if ((_M_column + __length < _M_max_length)
646 || (__length >= _M_max_length && _M_column == 1))
648 // If this isn't the first line, indent
649 if (_M_column == 1 && !_M_first_line)
651 char __spacing[_M_indent + 1];
652 for (int i = 0; i < _M_indent; ++i)
653 __spacing[i] = ' ';
654 __spacing[_M_indent] = '\0';
655 fprintf(stderr, "%s", __spacing);
656 _M_column += _M_indent;
659 fprintf(stderr, "%s", __word);
660 _M_column += __length;
662 if (__word[__length - 1] == '\n')
664 _M_first_line = false;
665 _M_column = 1;
668 else
670 _M_column = 1;
671 _M_print_word("\n");
672 _M_print_word(__word);
676 void
677 _Error_formatter::
678 _M_print_string(const char* __string) const
680 const char* __start = __string;
681 const char* __finish = __start;
682 const int __bufsize = 128;
683 char __buf[__bufsize];
685 while (*__start)
687 if (*__start != '%')
689 // [__start, __finish) denotes the next word
690 __finish = __start;
691 while (isalnum(*__finish))
692 ++__finish;
693 if (__start == __finish)
694 ++__finish;
695 if (isspace(*__finish))
696 ++__finish;
698 const ptrdiff_t __len = __finish - __start;
699 assert(__len < __bufsize);
700 memcpy(__buf, __start, __len);
701 __buf[__len] = '\0';
702 _M_print_word(__buf);
703 __start = __finish;
705 // Skip extra whitespace
706 while (*__start == ' ')
707 ++__start;
709 continue;
712 ++__start;
713 assert(*__start);
714 if (*__start == '%')
716 _M_print_word("%");
717 ++__start;
718 continue;
721 // Get the parameter number
722 assert(*__start >= '1' && *__start <= '9');
723 size_t __param = *__start - '0';
724 --__param;
725 assert(__param < _M_num_parameters);
727 // '.' separates the parameter number from the field
728 // name, if there is one.
729 ++__start;
730 if (*__start != '.')
732 assert(*__start == ';');
733 ++__start;
734 __buf[0] = '\0';
735 if (_M_parameters[__param]._M_kind == _Parameter::__integer)
737 _M_format_word(__buf, __bufsize, "%ld",
738 _M_parameters[__param]._M_variant._M_integer._M_value);
739 _M_print_word(__buf);
741 else if (_M_parameters[__param]._M_kind == _Parameter::__string)
742 _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
743 continue;
746 // Extract the field name we want
747 enum { __max_field_len = 16 };
748 char __field[__max_field_len];
749 int __field_idx = 0;
750 ++__start;
751 while (*__start != ';')
753 assert(*__start);
754 assert(__field_idx < __max_field_len-1);
755 __field[__field_idx++] = *__start++;
757 ++__start;
758 __field[__field_idx] = 0;
760 _M_parameters[__param]._M_print_field(this, __field);
764 void
765 _Error_formatter::_M_get_max_length() const throw ()
767 const char* __nptr = std::getenv("GLIBCXX_DEBUG_MESSAGE_LENGTH");
768 if (__nptr)
770 char* __endptr;
771 const unsigned long __ret = std::strtoul(__nptr, &__endptr, 0);
772 if (*__nptr != '\0' && *__endptr == '\0')
773 _M_max_length = __ret;
777 // Instantiations.
778 template
779 void
780 _Error_formatter::_M_format_word(char*, int, const char*,
781 const void*) const;
783 template
784 void
785 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
787 template
788 void
789 _Error_formatter::_M_format_word(char*, int, const char*,
790 std::size_t) const;
792 template
793 void
794 _Error_formatter::_M_format_word(char*, int, const char*,
795 const char*) const;
796 } // namespace __gnu_debug