2016-10-03 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / src / c++11 / debug.cc
blob0e24d0140dcda4b10c6d997b5f70693055f0ddc3
1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003-2016 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 template<typename _Tp>
545 format_word(char* buf, int n, const char* fmt, _Tp s)
546 { return std::min(__builtin_snprintf(buf, n, fmt, s), n - 1); }
548 void
549 get_max_length(std::size_t& max_length)
551 const char* nptr = std::getenv("GLIBCXX_DEBUG_MESSAGE_LENGTH");
552 if (nptr)
554 char* endptr;
555 const unsigned long ret = std::strtoul(nptr, &endptr, 0);
556 if (*nptr != '\0' && *endptr == '\0')
557 max_length = ret;
561 struct PrintContext
563 PrintContext()
564 : _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
565 { get_max_length(_M_max_length); }
567 std::size_t _M_max_length;
568 enum { _M_indent = 4 } ;
569 std::size_t _M_column;
570 bool _M_first_line;
571 bool _M_wordwrap;
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_word(ctx, "\n", 1);
625 print_word(ctx, word, count);
629 void
630 print_type(PrintContext& ctx,
631 const type_info* info,
632 const char* unknown_name)
634 if (!info)
635 print_word(ctx, unknown_name);
636 else
638 int status;
639 char* demangled_name =
640 __cxxabiv1::__cxa_demangle(info->name(), NULL, NULL, &status);
641 print_word(ctx, status == 0 ? demangled_name : info->name());
642 free(demangled_name);
646 bool
647 print_field(PrintContext& ctx,
648 const char* name, const _Parameter::_Type& type)
650 if (__builtin_strcmp(name, "name") == 0)
652 assert(type._M_name);
653 print_word(ctx, type._M_name);
655 else if (__builtin_strcmp(name, "type") == 0)
656 print_type(ctx, type._M_type, "<unknown type>");
657 else
658 return false;
660 return true;
663 bool
664 print_field(PrintContext& ctx,
665 const char* name, const _Parameter::_Instance& inst)
667 const _Parameter::_Type& type = inst;
668 if (print_field(ctx, name, type))
670 else if (__builtin_strcmp(name, "address") == 0)
672 char buf[64];
673 int ret = __builtin_sprintf(buf, "%p", inst._M_address);
674 print_word(ctx, buf, ret);
676 else
677 return false;
679 return true;
682 void
683 print_field(PrintContext& ctx, const _Parameter& param, const char* name)
685 assert(param._M_kind != _Parameter::__unused_param);
686 const int bufsize = 64;
687 char buf[bufsize];
689 const auto& variant = param._M_variant;
690 switch (param._M_kind)
692 case _Parameter::__iterator:
694 const auto& iterator = variant._M_iterator;
695 if (print_field(ctx, name, iterator))
697 else if (__builtin_strcmp(name, "constness") == 0)
699 static const char*
700 constness_names[_Error_formatter::__last_constness] =
702 "<unknown>",
703 "constant",
704 "mutable"
706 print_word(ctx, constness_names[iterator._M_constness]);
708 else if (__builtin_strcmp(name, "state") == 0)
710 static const char*
711 state_names[_Error_formatter::__last_state] =
713 "<unknown>",
714 "singular",
715 "dereferenceable (start-of-sequence)",
716 "dereferenceable",
717 "past-the-end",
718 "before-begin"
720 print_word(ctx, state_names[iterator._M_state]);
722 else if (__builtin_strcmp(name, "sequence") == 0)
724 assert(iterator._M_sequence);
725 int written = __builtin_sprintf(buf, "%p", iterator._M_sequence);
726 print_word(ctx, buf, written);
728 else if (__builtin_strcmp(name, "seq_type") == 0)
729 print_type(ctx, iterator._M_seq_type, "<unknown seq_type>");
730 else
731 assert(false);
733 break;
735 case _Parameter::__sequence:
736 if (!print_field(ctx, name, variant._M_sequence))
737 assert(false);
738 break;
740 case _Parameter::__integer:
741 if (__builtin_strcmp(name, "name") == 0)
743 assert(variant._M_integer._M_name);
744 print_word(ctx, variant._M_integer._M_name);
746 else
747 assert(false);
748 break;
750 case _Parameter::__string:
751 if (__builtin_strcmp(name, "name") == 0)
753 assert(variant._M_string._M_name);
754 print_word(ctx, variant._M_string._M_name);
756 else
757 assert(false);
758 break;
760 case _Parameter::__instance:
761 if (!print_field(ctx, name, variant._M_instance))
762 assert(false);
763 break;
765 case _Parameter::__iterator_value_type:
766 if (!print_field(ctx, name, variant._M_iterator_value_type))
767 assert(false);
768 break;
770 default:
771 assert(false);
772 break;
776 void
777 print_description(PrintContext& ctx, const _Parameter::_Type& type)
779 if (type._M_name)
781 const int bufsize = 64;
782 char buf[bufsize];
783 int written
784 = format_word(buf, bufsize, "\"%s\"", type._M_name);
785 print_word(ctx, buf, written);
788 print_word(ctx, " {\n");
790 if (type._M_type)
792 print_word(ctx, " type = ");
793 print_type(ctx, type._M_type, "<unknown type>");
794 print_word(ctx, ";\n");
798 void
799 print_description(PrintContext& ctx, const _Parameter::_Instance& inst)
801 const int bufsize = 64;
802 char buf[bufsize];
804 if (inst._M_name)
806 int written
807 = format_word(buf, bufsize, "\"%s\" ", inst._M_name);
808 print_word(ctx, buf, written);
811 int written
812 = __builtin_sprintf(buf, "@ 0x%p {\n", inst._M_address);
813 print_word(ctx, buf, written);
815 if (inst._M_type)
817 print_word(ctx, " type = ");
818 print_type(ctx, inst._M_type, "<unknown type>");
822 void
823 print_description(PrintContext& ctx, const _Parameter& param)
825 const int bufsize = 128;
826 char buf[bufsize];
828 const auto& variant = param._M_variant;
829 switch (param._M_kind)
831 case _Parameter::__iterator:
833 const auto& ite = variant._M_iterator;
835 print_word(ctx, "iterator ");
836 print_description(ctx, ite);
838 if (ite._M_type)
840 if (ite._M_constness != _Error_formatter::__unknown_constness)
842 print_word(ctx, " (");
843 print_field(ctx, param, "constness");
844 print_word(ctx, " iterator)");
847 print_word(ctx, ";\n");
850 if (ite._M_state != _Error_formatter::__unknown_state)
852 print_word(ctx, " state = ");
853 print_field(ctx, param, "state");
854 print_word(ctx, ";\n");
857 if (ite._M_sequence)
859 print_word(ctx, " references sequence ");
860 if (ite._M_seq_type)
862 print_word(ctx, "with type '");
863 print_field(ctx, param, "seq_type");
864 print_word(ctx, "' ");
867 int written
868 = __builtin_sprintf(buf, "@ 0x%p\n", ite._M_sequence);
869 print_word(ctx, buf, written);
872 print_word(ctx, "}\n", 2);
874 break;
876 case _Parameter::__sequence:
877 print_word(ctx, "sequence ");
878 print_description(ctx, variant._M_sequence);
880 if (variant._M_sequence._M_type)
881 print_word(ctx, ";\n", 2);
883 print_word(ctx, "}\n", 2);
884 break;
886 case _Parameter::__instance:
887 print_word(ctx, "instance ");
888 print_description(ctx, variant._M_instance);
890 if (variant._M_instance._M_type)
891 print_word(ctx, ";\n", 2);
893 print_word(ctx, "}\n", 2);
894 break;
896 case _Parameter::__iterator_value_type:
897 print_word(ctx, "iterator::value_type ");
898 print_description(ctx, variant._M_iterator_value_type);
899 print_word(ctx, "}\n", 2);
900 break;
902 default:
903 break;
907 void
908 print_string(PrintContext& ctx, const char* string,
909 const _Parameter* parameters, std::size_t num_parameters)
911 const char* start = string;
912 const int bufsize = 128;
913 char buf[bufsize];
914 int bufindex = 0;
916 while (*start)
918 if (isspace(*start))
920 buf[bufindex++] = *start++;
921 buf[bufindex] = '\0';
922 print_word(ctx, buf, bufindex);
923 bufindex = 0;
924 continue;
927 if (*start != '%')
929 // Normal char.
930 buf[bufindex++] = *start++;
931 continue;
934 if (*++start == '%')
936 // Escaped '%'
937 buf[bufindex++] = *start++;
938 continue;
941 // We are on a parameter property reference, we need to flush buffer
942 // first.
943 if (bufindex != 0)
945 buf[bufindex] = '\0';
946 print_word(ctx, buf, bufindex);
947 bufindex = 0;
950 // Get the parameter number
951 assert(*start >= '1' && *start <= '9');
952 size_t param_index = *start - '0' - 1;
953 assert(param_index < num_parameters);
954 const auto& param = parameters[param_index];
956 // '.' separates the parameter number from the field
957 // name, if there is one.
958 ++start;
959 if (*start != '.')
961 assert(*start == ';');
962 ++start;
963 if (param._M_kind == _Parameter::__integer)
965 int written
966 = __builtin_sprintf(buf, "%ld",
967 param._M_variant._M_integer._M_value);
968 print_word(ctx, buf, written);
970 else if (param._M_kind == _Parameter::__string)
971 print_string(ctx, param._M_variant._M_string._M_value,
972 parameters, num_parameters);
973 continue;
976 // Extract the field name we want
977 const int max_field_len = 16;
978 char field[max_field_len];
979 int field_idx = 0;
980 ++start;
981 while (*start != ';')
983 assert(*start);
984 assert(field_idx < max_field_len - 1);
985 field[field_idx++] = *start++;
987 ++start;
988 field[field_idx] = '\0';
990 print_field(ctx, param, field);
993 // Might need to flush.
994 if (bufindex)
996 buf[bufindex] = '\0';
997 print_word(ctx, buf, bufindex);
1002 namespace __gnu_debug
1004 _Error_formatter&
1005 _Error_formatter::_M_message(_Debug_msg_id __id) const throw ()
1007 return const_cast<_Error_formatter*>(this)
1008 ->_M_message(_S_debug_messages[__id]);
1011 void
1012 _Error_formatter::_M_error() const
1014 const int bufsize = 128;
1015 char buf[bufsize];
1017 // Emit file & line number information
1018 bool go_to_next_line = false;
1019 PrintContext ctx;
1020 if (_M_file)
1022 int written = format_word(buf, bufsize, "%s:", _M_file);
1023 print_word(ctx, buf, written);
1024 go_to_next_line = true;
1027 if (_M_line > 0)
1029 int written = __builtin_sprintf(buf, "%u:", _M_line);
1030 print_word(ctx, buf, written);
1031 go_to_next_line = true;
1034 if (go_to_next_line)
1035 print_word(ctx, "\n", 1);
1037 if (ctx._M_max_length)
1038 ctx._M_wordwrap = true;
1040 print_word(ctx, "Error: ");
1042 // Print the error message
1043 assert(_M_text);
1044 print_string(ctx, _M_text, _M_parameters, _M_num_parameters);
1045 print_word(ctx, ".\n", 2);
1047 // Emit descriptions of the objects involved in the operation
1048 ctx._M_first_line = true;
1049 ctx._M_wordwrap = false;
1050 bool has_header = false;
1051 for (unsigned int i = 0; i < _M_num_parameters; ++i)
1053 switch (_M_parameters[i]._M_kind)
1055 case _Parameter::__iterator:
1056 case _Parameter::__sequence:
1057 case _Parameter::__instance:
1058 case _Parameter::__iterator_value_type:
1059 if (!has_header)
1061 print_word(ctx, "\nObjects involved in the operation:\n");
1062 has_header = true;
1064 print_description(ctx, _M_parameters[i]);
1065 break;
1067 default:
1068 break;
1072 abort();
1075 // Deprecated methods kept for backward compatibility.
1076 void
1077 _Error_formatter::_Parameter::_M_print_field(
1078 const _Error_formatter*, const char*) const
1081 void
1082 _Error_formatter::_Parameter::_M_print_description(const _Error_formatter*) const
1085 template<typename _Tp>
1086 void
1087 _Error_formatter::_M_format_word(char*, int, const char*, _Tp)
1088 const throw ()
1091 void
1092 _Error_formatter::_M_print_word(const char*) const
1095 void
1096 _Error_formatter::_M_print_string(const char*) const
1099 void
1100 _Error_formatter::_M_get_max_length() const throw ()
1103 // Instantiations.
1104 template
1105 void
1106 _Error_formatter::_M_format_word(char*, int, const char*,
1107 const void*) const;
1109 template
1110 void
1111 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
1113 template
1114 void
1115 _Error_formatter::_M_format_word(char*, int, const char*,
1116 std::size_t) const;
1118 template
1119 void
1120 _Error_formatter::_M_format_word(char*, int, const char*,
1121 const char*) const;
1122 } // namespace __gnu_debug