Import GCC-8 to a new vendor branch
[dragonfly.git] / contrib / gcc-8.0 / libstdc++-v3 / src / c++11 / debug.cc
blob7c0b540a7dfbe843dbf3e3c838fd1b60fd638f3b
1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #include <bits/move.h>
26 #include <bits/stl_iterator_base_types.h>
28 #include <debug/formatter.h>
29 #include <debug/safe_base.h>
30 #include <debug/safe_unordered_base.h>
31 #include <debug/safe_iterator.h>
32 #include <debug/safe_local_iterator.h>
33 #include <debug/vector>
35 #include <cassert>
36 #include <cstdio>
37 #include <cctype> // for std::isspace
39 #include <algorithm> // for std::min
41 #include <cxxabi.h> // for __cxa_demangle
43 #include "mutex_pool.h"
45 using namespace std;
47 namespace
49 /** Returns different instances of __mutex depending on the passed address
50 * in order to limit contention without breaking current library binary
51 * compatibility. */
52 __gnu_cxx::__mutex&
53 get_safe_base_mutex(void* address)
55 // Use arbitrarily __gnu_debug::vector<int> as the container giving
56 // alignment of debug containers.
57 const auto alignbits = __builtin_ctz(alignof(__gnu_debug::vector<int>));
58 const unsigned char index
59 = (reinterpret_cast<std::size_t>(address) >> alignbits)
60 & __gnu_internal::mask;
61 return __gnu_internal::get_mutex(index);
64 void
65 swap_its(__gnu_debug::_Safe_sequence_base& __lhs,
66 __gnu_debug::_Safe_iterator_base*& __lhs_its,
67 __gnu_debug::_Safe_sequence_base& __rhs,
68 __gnu_debug::_Safe_iterator_base*& __rhs_its)
70 swap(__lhs_its, __rhs_its);
71 __gnu_debug::_Safe_iterator_base* __iter;
72 for (__iter = __rhs_its; __iter; __iter = __iter->_M_next)
73 __iter->_M_sequence = &__rhs;
74 for (__iter = __lhs_its; __iter; __iter = __iter->_M_next)
75 __iter->_M_sequence = &__lhs;
78 void
79 swap_seq_single(__gnu_debug::_Safe_sequence_base& __lhs,
80 __gnu_debug::_Safe_sequence_base& __rhs)
82 swap(__lhs._M_version, __rhs._M_version);
83 swap_its(__lhs, __lhs._M_iterators,
84 __rhs, __rhs._M_iterators);
85 swap_its(__lhs, __lhs._M_const_iterators,
86 __rhs, __rhs._M_const_iterators);
89 template<typename _Action>
90 void
91 lock_and_run(__gnu_cxx::__mutex& lhs_mutex, __gnu_cxx::__mutex& rhs_mutex,
92 _Action action)
94 // We need to lock both sequences to run action.
95 if (&lhs_mutex == &rhs_mutex)
97 __gnu_cxx::__scoped_lock sentry(lhs_mutex);
98 action();
100 else
102 __gnu_cxx::__scoped_lock sentry1(&lhs_mutex < &rhs_mutex
103 ? lhs_mutex : rhs_mutex);
104 __gnu_cxx::__scoped_lock sentry2(&lhs_mutex < &rhs_mutex
105 ? rhs_mutex : lhs_mutex);
106 action();
110 void
111 swap_seq(__gnu_cxx::__mutex& lhs_mutex,
112 __gnu_debug::_Safe_sequence_base& lhs,
113 __gnu_cxx::__mutex& rhs_mutex,
114 __gnu_debug::_Safe_sequence_base& rhs)
116 lock_and_run(lhs_mutex, rhs_mutex,
117 [&lhs, &rhs]() { swap_seq_single(lhs, rhs); });
120 void
121 swap_ucont_single(__gnu_debug::_Safe_unordered_container_base& __lhs,
122 __gnu_debug::_Safe_unordered_container_base& __rhs)
124 swap_seq_single(__lhs, __rhs);
125 swap_its(__lhs, __lhs._M_local_iterators,
126 __rhs, __rhs._M_local_iterators);
127 swap_its(__lhs, __lhs._M_const_local_iterators,
128 __rhs, __rhs._M_const_local_iterators);
131 void
132 swap_ucont(__gnu_cxx::__mutex& lhs_mutex,
133 __gnu_debug::_Safe_unordered_container_base& lhs,
134 __gnu_cxx::__mutex& rhs_mutex,
135 __gnu_debug::_Safe_unordered_container_base& rhs)
137 lock_and_run(lhs_mutex, rhs_mutex,
138 [&lhs, &rhs]() { swap_ucont_single(lhs, rhs); });
141 void
142 detach_all(__gnu_debug::_Safe_iterator_base* __iter)
144 for (; __iter;)
146 __gnu_debug::_Safe_iterator_base* __old = __iter;
147 __iter = __iter->_M_next;
148 __old->_M_reset();
151 } // anonymous namespace
153 namespace __gnu_debug
155 const char* const _S_debug_messages[] =
157 // General Checks
158 "function requires a valid iterator range [%1.name;, %2.name;)",
159 "attempt to insert into container with a singular iterator",
160 "attempt to insert into container with an iterator"
161 " from a different container",
162 "attempt to erase from container with a %2.state; iterator",
163 "attempt to erase from container with an iterator"
164 " from a different container",
165 "attempt to subscript container with out-of-bounds index %2;,"
166 " but container only holds %3; elements",
167 "attempt to access an element in an empty container",
168 "elements in iterator range [%1.name;, %2.name;)"
169 " are not partitioned by the value %3;",
170 "elements in iterator range [%1.name;, %2.name;)"
171 " are not partitioned by the predicate %3; and value %4;",
172 "elements in iterator range [%1.name;, %2.name;) are not sorted",
173 "elements in iterator range [%1.name;, %2.name;)"
174 " are not sorted according to the predicate %3;",
175 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
176 "elements in iterator range [%1.name;, %2.name;)"
177 " do not form a heap with respect to the predicate %3;",
178 // std::bitset checks
179 "attempt to write through a singular bitset reference",
180 "attempt to read from a singular bitset reference",
181 "attempt to flip a singular bitset reference",
182 // std::list checks
183 "attempt to splice a list into itself",
184 "attempt to splice lists with unequal allocators",
185 "attempt to splice elements referenced by a %1.state; iterator",
186 "attempt to splice an iterator from a different container",
187 "splice destination %1.name;"
188 " occurs within source range [%2.name;, %3.name;)",
189 // iterator checks
190 "attempt to initialize an iterator that will immediately become singular",
191 "attempt to copy-construct an iterator from a singular iterator",
192 "attempt to construct a constant iterator"
193 " from a singular mutable iterator",
194 "attempt to copy from a singular iterator",
195 "attempt to dereference a %1.state; iterator",
196 "attempt to increment a %1.state; iterator",
197 "attempt to decrement a %1.state; iterator",
198 "attempt to subscript a %1.state; iterator %2; step from"
199 " its current position, which falls outside its dereferenceable range",
200 "attempt to advance a %1.state; iterator %2; steps,"
201 " which falls outside its valid range",
202 "attempt to retreat a %1.state; iterator %2; steps,"
203 " which falls outside its valid range",
204 "attempt to compare a %1.state; iterator to a %2.state; iterator",
205 "attempt to compare iterators from different sequences",
206 "attempt to order a %1.state; iterator to a %2.state; iterator",
207 "attempt to order iterators from different sequences",
208 "attempt to compute the difference between a %1.state;"
209 " iterator to a %2.state; iterator",
210 "attempt to compute the different between two iterators"
211 " from different sequences",
212 // istream_iterator
213 "attempt to dereference an end-of-stream istream_iterator",
214 "attempt to increment an end-of-stream istream_iterator",
215 // ostream_iterator
216 "attempt to output via an ostream_iterator with no associated stream",
217 // istreambuf_iterator
218 "attempt to dereference an end-of-stream istreambuf_iterator"
219 " (this is a GNU extension)",
220 "attempt to increment an end-of-stream istreambuf_iterator",
221 // std::forward_list
222 "attempt to insert into container after an end iterator",
223 "attempt to erase from container after a %2.state; iterator not followed"
224 " by a dereferenceable one",
225 "function requires a valid iterator range (%2.name;, %3.name;)"
226 ", \"%2.name;\" shall be before and not equal to \"%3.name;\"",
227 // std::unordered_container::local_iterator
228 "attempt to compare local iterators from different unordered container"
229 " buckets",
230 "function requires a non-empty iterator range [%1.name;, %2.name;)",
231 "attempt to self move assign",
232 "attempt to access container with out-of-bounds bucket index %2;,"
233 " container only holds %3; buckets",
234 "load factor shall be positive",
235 "allocators must be equal",
236 "attempt to insert with an iterator range [%1.name;, %2.name;) from this"
237 " container",
238 "comparison doesn't meet irreflexive requirements, assert(!(a < a))"
241 void
242 _Safe_sequence_base::
243 _M_detach_all()
245 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
246 detach_all(_M_iterators);
247 _M_iterators = 0;
249 detach_all(_M_const_iterators);
250 _M_const_iterators = 0;
253 void
254 _Safe_sequence_base::
255 _M_detach_singular()
257 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
258 for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
260 _Safe_iterator_base* __old = __iter;
261 __iter = __iter->_M_next;
262 if (__old->_M_singular())
263 __old->_M_detach_single();
266 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
268 _Safe_iterator_base* __old = __iter2;
269 __iter2 = __iter2->_M_next;
270 if (__old->_M_singular())
271 __old->_M_detach_single();
275 void
276 _Safe_sequence_base::
277 _M_revalidate_singular()
279 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
280 for (_Safe_iterator_base* __iter = _M_iterators; __iter;
281 __iter = __iter->_M_next)
282 __iter->_M_version = _M_version;
284 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;
285 __iter2 = __iter2->_M_next)
286 __iter2->_M_version = _M_version;
289 void
290 _Safe_sequence_base::
291 _M_swap(_Safe_sequence_base& __x) noexcept
292 { swap_seq(_M_get_mutex(), *this, __x._M_get_mutex(), __x); }
294 __gnu_cxx::__mutex&
295 _Safe_sequence_base::
296 _M_get_mutex() throw ()
297 { return get_safe_base_mutex(this); }
299 void
300 _Safe_sequence_base::
301 _M_attach(_Safe_iterator_base* __it, bool __constant)
303 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
304 _M_attach_single(__it, __constant);
307 void
308 _Safe_sequence_base::
309 _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ()
311 _Safe_iterator_base*& __its =
312 __constant ? _M_const_iterators : _M_iterators;
313 __it->_M_next = __its;
314 if (__it->_M_next)
315 __it->_M_next->_M_prior = __it;
316 __its = __it;
319 void
320 _Safe_sequence_base::
321 _M_detach(_Safe_iterator_base* __it)
323 // Remove __it from this sequence's list
324 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
325 _M_detach_single(__it);
328 void
329 _Safe_sequence_base::
330 _M_detach_single(_Safe_iterator_base* __it) throw ()
332 // Remove __it from this sequence's list
333 __it->_M_unlink();
334 if (_M_const_iterators == __it)
335 _M_const_iterators = __it->_M_next;
336 if (_M_iterators == __it)
337 _M_iterators = __it->_M_next;
340 void
341 _Safe_iterator_base::
342 _M_attach(_Safe_sequence_base* __seq, bool __constant)
344 _M_detach();
346 // Attach to the new sequence (if there is one)
347 if (__seq)
349 _M_sequence = __seq;
350 _M_version = _M_sequence->_M_version;
351 _M_sequence->_M_attach(this, __constant);
355 void
356 _Safe_iterator_base::
357 _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ()
359 _M_detach_single();
361 // Attach to the new sequence (if there is one)
362 if (__seq)
364 _M_sequence = __seq;
365 _M_version = _M_sequence->_M_version;
366 _M_sequence->_M_attach_single(this, __constant);
370 void
371 _Safe_iterator_base::
372 _M_detach()
374 if (_M_sequence)
375 _M_sequence->_M_detach(this);
377 _M_reset();
380 void
381 _Safe_iterator_base::
382 _M_detach_single() throw ()
384 if (_M_sequence)
385 _M_sequence->_M_detach_single(this);
387 _M_reset();
390 void
391 _Safe_iterator_base::
392 _M_reset() throw ()
394 _M_sequence = 0;
395 _M_version = 0;
396 _M_prior = 0;
397 _M_next = 0;
400 bool
401 _Safe_iterator_base::
402 _M_singular() const throw ()
403 { return !_M_sequence || _M_version != _M_sequence->_M_version; }
405 bool
406 _Safe_iterator_base::
407 _M_can_compare(const _Safe_iterator_base& __x) const throw ()
409 return (!_M_singular()
410 && !__x._M_singular() && _M_sequence == __x._M_sequence);
413 __gnu_cxx::__mutex&
414 _Safe_iterator_base::
415 _M_get_mutex() throw ()
416 { return _M_sequence->_M_get_mutex(); }
418 _Safe_unordered_container_base*
419 _Safe_local_iterator_base::
420 _M_get_container() const noexcept
421 { return static_cast<_Safe_unordered_container_base*>(_M_sequence); }
423 void
424 _Safe_local_iterator_base::
425 _M_attach(_Safe_sequence_base* __cont, bool __constant)
427 _M_detach();
429 // Attach to the new container (if there is one)
430 if (__cont)
432 _M_sequence = __cont;
433 _M_version = _M_sequence->_M_version;
434 _M_get_container()->_M_attach_local(this, __constant);
438 void
439 _Safe_local_iterator_base::
440 _M_attach_single(_Safe_sequence_base* __cont, bool __constant) throw ()
442 _M_detach_single();
444 // Attach to the new container (if there is one)
445 if (__cont)
447 _M_sequence = __cont;
448 _M_version = _M_sequence->_M_version;
449 _M_get_container()->_M_attach_local_single(this, __constant);
453 void
454 _Safe_local_iterator_base::
455 _M_detach()
457 if (_M_sequence)
458 _M_get_container()->_M_detach_local(this);
460 _M_reset();
463 void
464 _Safe_local_iterator_base::
465 _M_detach_single() throw ()
467 if (_M_sequence)
468 _M_get_container()->_M_detach_local_single(this);
470 _M_reset();
473 void
474 _Safe_unordered_container_base::
475 _M_detach_all()
477 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
478 detach_all(_M_iterators);
479 _M_iterators = 0;
481 detach_all(_M_const_iterators);
482 _M_const_iterators = 0;
484 detach_all(_M_local_iterators);
485 _M_local_iterators = 0;
487 detach_all(_M_const_local_iterators);
488 _M_const_local_iterators = 0;
491 void
492 _Safe_unordered_container_base::
493 _M_swap(_Safe_unordered_container_base& __x) noexcept
494 { swap_ucont(_M_get_mutex(), *this, __x._M_get_mutex(), __x); }
496 void
497 _Safe_unordered_container_base::
498 _M_attach_local(_Safe_iterator_base* __it, bool __constant)
500 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
501 _M_attach_local_single(__it, __constant);
504 void
505 _Safe_unordered_container_base::
506 _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) throw ()
508 _Safe_iterator_base*& __its =
509 __constant ? _M_const_local_iterators : _M_local_iterators;
510 __it->_M_next = __its;
511 if (__it->_M_next)
512 __it->_M_next->_M_prior = __it;
513 __its = __it;
516 void
517 _Safe_unordered_container_base::
518 _M_detach_local(_Safe_iterator_base* __it)
520 // Remove __it from this container's list
521 __gnu_cxx::__scoped_lock sentry(_M_get_mutex());
522 _M_detach_local_single(__it);
525 void
526 _Safe_unordered_container_base::
527 _M_detach_local_single(_Safe_iterator_base* __it) throw ()
529 // Remove __it from this container's list
530 __it->_M_unlink();
531 if (_M_const_local_iterators == __it)
532 _M_const_local_iterators = __it->_M_next;
533 if (_M_local_iterators == __it)
534 _M_local_iterators = __it->_M_next;
538 namespace
540 using _Error_formatter = __gnu_debug::_Error_formatter;
541 using _Parameter = __gnu_debug::_Error_formatter::_Parameter;
543 void
544 get_max_length(std::size_t& max_length)
546 const char* nptr = std::getenv("GLIBCXX_DEBUG_MESSAGE_LENGTH");
547 if (nptr)
549 char* endptr;
550 const unsigned long ret = std::strtoul(nptr, &endptr, 0);
551 if (*nptr != '\0' && *endptr == '\0')
552 max_length = ret;
556 struct PrintContext
558 PrintContext()
559 : _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
560 { get_max_length(_M_max_length); }
562 std::size_t _M_max_length;
563 enum { _M_indent = 4 } ;
564 std::size_t _M_column;
565 bool _M_first_line;
566 bool _M_wordwrap;
569 template<size_t Length>
570 void
571 print_literal(PrintContext& ctx, const char(&word)[Length])
572 { print_word(ctx, word, Length - 1); }
574 void
575 print_word(PrintContext& ctx, const char* word,
576 std::ptrdiff_t count = -1)
578 size_t length = count >= 0 ? count : __builtin_strlen(word);
579 if (length == 0)
580 return;
582 // Consider first '\n' at begining cause it impacts column.
583 if (word[0] == '\n')
585 fprintf(stderr, "\n");
586 ctx._M_column = 1;
587 ++word;
588 --length;
590 if (length == 0)
591 return;
594 size_t visual_length
595 = isspace(word[length - 1]) ? length - 1 : length;
596 if (visual_length == 0
597 || !ctx._M_wordwrap
598 || (ctx._M_column + visual_length < ctx._M_max_length)
599 || (visual_length >= ctx._M_max_length && ctx._M_column == 1))
601 // If this isn't the first line, indent
602 if (ctx._M_column == 1 && !ctx._M_first_line)
604 char spacing[ctx._M_indent + 1];
605 for (int i = 0; i < ctx._M_indent; ++i)
606 spacing[i] = ' ';
607 spacing[ctx._M_indent] = '\0';
608 fprintf(stderr, "%s", spacing);
609 ctx._M_column += ctx._M_indent;
612 int written = fprintf(stderr, "%s", word);
614 if (word[length - 1] == '\n')
616 ctx._M_first_line = false;
617 ctx._M_column = 1;
619 else
620 ctx._M_column += written;
622 else
624 print_literal(ctx, "\n");
625 print_word(ctx, word, count);
629 template<size_t Length>
630 void
631 print_type(PrintContext& ctx,
632 const type_info* info,
633 const char(&unknown_name)[Length])
635 if (!info)
636 print_literal(ctx, unknown_name);
637 else
639 int status;
640 char* demangled_name =
641 __cxxabiv1::__cxa_demangle(info->name(), NULL, NULL, &status);
642 print_word(ctx, status == 0 ? demangled_name : info->name());
643 free(demangled_name);
647 bool
648 print_field(PrintContext& ctx,
649 const char* name, const _Parameter::_Type& type)
651 if (__builtin_strcmp(name, "name") == 0)
653 assert(type._M_name);
654 print_word(ctx, type._M_name);
656 else if (__builtin_strcmp(name, "type") == 0)
657 print_type(ctx, type._M_type, "<unknown type>");
658 else
659 return false;
661 return true;
664 bool
665 print_field(PrintContext& ctx,
666 const char* name, const _Parameter::_Instance& inst)
668 const _Parameter::_Type& type = inst;
669 if (print_field(ctx, name, type))
671 else if (__builtin_strcmp(name, "address") == 0)
673 char buf[64];
674 int ret = __builtin_sprintf(buf, "%p", inst._M_address);
675 print_word(ctx, buf, ret);
677 else
678 return false;
680 return true;
683 void
684 print_field(PrintContext& ctx, const _Parameter& param, const char* name)
686 assert(param._M_kind != _Parameter::__unused_param);
687 const int bufsize = 64;
688 char buf[bufsize];
690 const auto& variant = param._M_variant;
691 switch (param._M_kind)
693 case _Parameter::__iterator:
695 const auto& iterator = variant._M_iterator;
696 if (print_field(ctx, name, iterator))
698 else if (__builtin_strcmp(name, "constness") == 0)
700 static const char*
701 constness_names[_Error_formatter::__last_constness] =
703 "<unknown>",
704 "constant",
705 "mutable"
707 print_word(ctx, constness_names[iterator._M_constness]);
709 else if (__builtin_strcmp(name, "state") == 0)
711 static const char*
712 state_names[_Error_formatter::__last_state] =
714 "<unknown>",
715 "singular",
716 "dereferenceable (start-of-sequence)",
717 "dereferenceable",
718 "past-the-end",
719 "before-begin"
721 print_word(ctx, state_names[iterator._M_state]);
723 else if (__builtin_strcmp(name, "sequence") == 0)
725 assert(iterator._M_sequence);
726 int written = __builtin_sprintf(buf, "%p", iterator._M_sequence);
727 print_word(ctx, buf, written);
729 else if (__builtin_strcmp(name, "seq_type") == 0)
730 print_type(ctx, iterator._M_seq_type, "<unknown seq_type>");
731 else
732 assert(false);
734 break;
736 case _Parameter::__sequence:
737 if (!print_field(ctx, name, variant._M_sequence))
738 assert(false);
739 break;
741 case _Parameter::__integer:
742 if (__builtin_strcmp(name, "name") == 0)
744 assert(variant._M_integer._M_name);
745 print_word(ctx, variant._M_integer._M_name);
747 else
748 assert(false);
749 break;
751 case _Parameter::__string:
752 if (__builtin_strcmp(name, "name") == 0)
754 assert(variant._M_string._M_name);
755 print_word(ctx, variant._M_string._M_name);
757 else
758 assert(false);
759 break;
761 case _Parameter::__instance:
762 if (!print_field(ctx, name, variant._M_instance))
763 assert(false);
764 break;
766 case _Parameter::__iterator_value_type:
767 if (!print_field(ctx, name, variant._M_iterator_value_type))
768 assert(false);
769 break;
771 default:
772 assert(false);
773 break;
777 void
778 print_description(PrintContext& ctx, const _Parameter::_Type& type)
780 if (type._M_name)
782 print_literal(ctx, "\"");
783 print_word(ctx, type._M_name);
784 print_literal(ctx, "\"");
787 print_literal(ctx, " {\n");
789 if (type._M_type)
791 print_literal(ctx, " type = ");
792 print_type(ctx, type._M_type, "<unknown type>");
793 print_literal(ctx, ";\n");
797 void
798 print_description(PrintContext& ctx, const _Parameter::_Instance& inst)
800 const int bufsize = 64;
801 char buf[bufsize];
803 if (inst._M_name)
805 print_literal(ctx, "\"");
806 print_word(ctx, inst._M_name);
807 print_literal(ctx, "\" ");
810 int written
811 = __builtin_sprintf(buf, "@ 0x%p {\n", inst._M_address);
812 print_word(ctx, buf, written);
814 if (inst._M_type)
816 print_literal(ctx, " type = ");
817 print_type(ctx, inst._M_type, "<unknown type>");
821 void
822 print_description(PrintContext& ctx, const _Parameter& param)
824 const int bufsize = 128;
825 char buf[bufsize];
827 const auto& variant = param._M_variant;
828 switch (param._M_kind)
830 case _Parameter::__iterator:
832 const auto& ite = variant._M_iterator;
834 print_literal(ctx, "iterator ");
835 print_description(ctx, ite);
837 if (ite._M_type)
839 if (ite._M_constness != _Error_formatter::__unknown_constness)
841 print_literal(ctx, " (");
842 print_field(ctx, param, "constness");
843 print_literal(ctx, " iterator)");
846 print_literal(ctx, ";\n");
849 if (ite._M_state != _Error_formatter::__unknown_state)
851 print_literal(ctx, " state = ");
852 print_field(ctx, param, "state");
853 print_literal(ctx, ";\n");
856 if (ite._M_sequence)
858 print_literal(ctx, " references sequence ");
859 if (ite._M_seq_type)
861 print_literal(ctx, "with type '");
862 print_field(ctx, param, "seq_type");
863 print_literal(ctx, "' ");
866 int written
867 = __builtin_sprintf(buf, "@ 0x%p\n", ite._M_sequence);
868 print_word(ctx, buf, written);
871 print_literal(ctx, "}\n");
873 break;
875 case _Parameter::__sequence:
876 print_literal(ctx, "sequence ");
877 print_description(ctx, variant._M_sequence);
879 if (variant._M_sequence._M_type)
880 print_literal(ctx, ";\n");
882 print_literal(ctx, "}\n");
883 break;
885 case _Parameter::__instance:
886 print_literal(ctx, "instance ");
887 print_description(ctx, variant._M_instance);
889 if (variant._M_instance._M_type)
890 print_literal(ctx, ";\n");
892 print_literal(ctx, "}\n");
893 break;
895 case _Parameter::__iterator_value_type:
896 print_literal(ctx, "iterator::value_type ");
897 print_description(ctx, variant._M_iterator_value_type);
898 print_literal(ctx, "}\n");
899 break;
901 default:
902 break;
906 void
907 print_string(PrintContext& ctx, const char* string,
908 const _Parameter* parameters, std::size_t num_parameters)
910 const char* start = string;
911 const int bufsize = 128;
912 char buf[bufsize];
913 int bufindex = 0;
915 while (*start)
917 if (isspace(*start))
919 buf[bufindex++] = *start++;
920 buf[bufindex] = '\0';
921 print_word(ctx, buf, bufindex);
922 bufindex = 0;
923 continue;
926 if (*start != '%')
928 // Normal char.
929 buf[bufindex++] = *start++;
930 continue;
933 if (*++start == '%')
935 // Escaped '%'
936 buf[bufindex++] = *start++;
937 continue;
940 // We are on a parameter property reference, we need to flush buffer
941 // first.
942 if (bufindex != 0)
944 buf[bufindex] = '\0';
945 print_word(ctx, buf, bufindex);
946 bufindex = 0;
949 // Get the parameter number
950 assert(*start >= '1' && *start <= '9');
951 size_t param_index = *start - '0' - 1;
952 assert(param_index < num_parameters);
953 const auto& param = parameters[param_index];
955 // '.' separates the parameter number from the field
956 // name, if there is one.
957 ++start;
958 if (*start != '.')
960 assert(*start == ';');
961 ++start;
962 if (param._M_kind == _Parameter::__integer)
964 int written
965 = __builtin_sprintf(buf, "%ld",
966 param._M_variant._M_integer._M_value);
967 print_word(ctx, buf, written);
969 else if (param._M_kind == _Parameter::__string)
970 print_string(ctx, param._M_variant._M_string._M_value,
971 parameters, num_parameters);
972 continue;
975 // Extract the field name we want
976 const int max_field_len = 16;
977 char field[max_field_len];
978 int field_idx = 0;
979 ++start;
980 while (*start != ';')
982 assert(*start);
983 assert(field_idx < max_field_len - 1);
984 field[field_idx++] = *start++;
986 ++start;
987 field[field_idx] = '\0';
989 print_field(ctx, param, field);
992 // Might need to flush.
993 if (bufindex)
995 buf[bufindex] = '\0';
996 print_word(ctx, buf, bufindex);
1001 namespace __gnu_debug
1003 _Error_formatter&
1004 _Error_formatter::_M_message(_Debug_msg_id __id) const throw ()
1006 return const_cast<_Error_formatter*>(this)
1007 ->_M_message(_S_debug_messages[__id]);
1010 void
1011 _Error_formatter::_M_error() const
1013 // Emit file & line number information
1014 bool go_to_next_line = false;
1015 PrintContext ctx;
1016 if (_M_file)
1018 print_word(ctx, _M_file);
1019 print_literal(ctx, ":");
1020 go_to_next_line = true;
1023 if (_M_line > 0)
1025 char buf[64];
1026 int written = __builtin_sprintf(buf, "%u:", _M_line);
1027 print_word(ctx, buf, written);
1028 go_to_next_line = true;
1031 if (go_to_next_line)
1032 print_literal(ctx, "\n");
1034 if (ctx._M_max_length)
1035 ctx._M_wordwrap = true;
1037 print_literal(ctx, "Error: ");
1039 // Print the error message
1040 assert(_M_text);
1041 print_string(ctx, _M_text, _M_parameters, _M_num_parameters);
1042 print_literal(ctx, ".\n");
1044 // Emit descriptions of the objects involved in the operation
1045 ctx._M_first_line = true;
1046 ctx._M_wordwrap = false;
1047 bool has_header = false;
1048 for (unsigned int i = 0; i < _M_num_parameters; ++i)
1050 switch (_M_parameters[i]._M_kind)
1052 case _Parameter::__iterator:
1053 case _Parameter::__sequence:
1054 case _Parameter::__instance:
1055 case _Parameter::__iterator_value_type:
1056 if (!has_header)
1058 print_literal(ctx, "\nObjects involved in the operation:\n");
1059 has_header = true;
1061 print_description(ctx, _M_parameters[i]);
1062 break;
1064 default:
1065 break;
1069 abort();
1072 #if !_GLIBCXX_INLINE_VERSION
1073 // Deprecated methods kept for backward compatibility.
1074 void
1075 _Error_formatter::_Parameter::_M_print_field(
1076 const _Error_formatter*, const char*) const
1079 void
1080 _Error_formatter::_Parameter::_M_print_description(const _Error_formatter*) const
1083 template<typename _Tp>
1084 void
1085 _Error_formatter::_M_format_word(char*, int, const char*, _Tp)
1086 const throw ()
1089 void
1090 _Error_formatter::_M_print_word(const char*) const
1093 void
1094 _Error_formatter::_M_print_string(const char*) const
1097 void
1098 _Error_formatter::_M_get_max_length() const throw ()
1101 // Instantiations.
1102 template
1103 void
1104 _Error_formatter::_M_format_word(char*, int, const char*,
1105 const void*) const;
1107 template
1108 void
1109 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
1111 template
1112 void
1113 _Error_formatter::_M_format_word(char*, int, const char*,
1114 std::size_t) const;
1116 template
1117 void
1118 _Error_formatter::_M_format_word(char*, int, const char*,
1119 const char*) const;
1120 #endif
1122 } // namespace __gnu_debug