1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003-2018 Free Software Foundation, Inc.
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)
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>
37 #include <cctype> // for std::isspace
39 #include <algorithm> // for std::min
41 #include <cxxabi.h> // for __cxa_demangle
43 #include "mutex_pool.h"
49 /** Returns different instances of __mutex depending on the passed address
50 * in order to limit contention without breaking current library binary
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
);
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
;
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
>
91 lock_and_run(__gnu_cxx::__mutex
& lhs_mutex
, __gnu_cxx::__mutex
& rhs_mutex
,
94 // We need to lock both sequences to run action.
95 if (&lhs_mutex
== &rhs_mutex
)
97 __gnu_cxx::__scoped_lock
sentry(lhs_mutex
);
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
);
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
); });
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
);
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
); });
142 detach_all(__gnu_debug::_Safe_iterator_base
* __iter
)
146 __gnu_debug::_Safe_iterator_base
* __old
= __iter
;
147 __iter
= __iter
->_M_next
;
151 } // anonymous namespace
153 namespace __gnu_debug
155 const char* const _S_debug_messages
[] =
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",
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;)",
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",
213 "attempt to dereference an end-of-stream istream_iterator",
214 "attempt to increment an end-of-stream istream_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",
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"
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"
238 "comparison doesn't meet irreflexive requirements, assert(!(a < a))"
242 _Safe_sequence_base::
245 __gnu_cxx::__scoped_lock
sentry(_M_get_mutex());
246 detach_all(_M_iterators
);
249 detach_all(_M_const_iterators
);
250 _M_const_iterators
= 0;
254 _Safe_sequence_base::
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();
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
;
290 _Safe_sequence_base::
291 _M_swap(_Safe_sequence_base
& __x
) noexcept
292 { swap_seq(_M_get_mutex(), *this, __x
._M_get_mutex(), __x
); }
295 _Safe_sequence_base::
296 _M_get_mutex() throw ()
297 { return get_safe_base_mutex(this); }
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
);
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
;
315 __it
->_M_next
->_M_prior
= __it
;
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
);
329 _Safe_sequence_base::
330 _M_detach_single(_Safe_iterator_base
* __it
) throw ()
332 // Remove __it from this sequence's list
334 if (_M_const_iterators
== __it
)
335 _M_const_iterators
= __it
->_M_next
;
336 if (_M_iterators
== __it
)
337 _M_iterators
= __it
->_M_next
;
341 _Safe_iterator_base::
342 _M_attach(_Safe_sequence_base
* __seq
, bool __constant
)
346 // Attach to the new sequence (if there is one)
350 _M_version
= _M_sequence
->_M_version
;
351 _M_sequence
->_M_attach(this, __constant
);
356 _Safe_iterator_base::
357 _M_attach_single(_Safe_sequence_base
* __seq
, bool __constant
) throw ()
361 // Attach to the new sequence (if there is one)
365 _M_version
= _M_sequence
->_M_version
;
366 _M_sequence
->_M_attach_single(this, __constant
);
371 _Safe_iterator_base::
375 _M_sequence
->_M_detach(this);
381 _Safe_iterator_base::
382 _M_detach_single() throw ()
385 _M_sequence
->_M_detach_single(this);
391 _Safe_iterator_base::
401 _Safe_iterator_base::
402 _M_singular() const throw ()
403 { return !_M_sequence
|| _M_version
!= _M_sequence
->_M_version
; }
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
);
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
); }
424 _Safe_local_iterator_base::
425 _M_attach(_Safe_sequence_base
* __cont
, bool __constant
)
429 // Attach to the new container (if there is one)
432 _M_sequence
= __cont
;
433 _M_version
= _M_sequence
->_M_version
;
434 _M_get_container()->_M_attach_local(this, __constant
);
439 _Safe_local_iterator_base::
440 _M_attach_single(_Safe_sequence_base
* __cont
, bool __constant
) throw ()
444 // Attach to the new container (if there is one)
447 _M_sequence
= __cont
;
448 _M_version
= _M_sequence
->_M_version
;
449 _M_get_container()->_M_attach_local_single(this, __constant
);
454 _Safe_local_iterator_base::
458 _M_get_container()->_M_detach_local(this);
464 _Safe_local_iterator_base::
465 _M_detach_single() throw ()
468 _M_get_container()->_M_detach_local_single(this);
474 _Safe_unordered_container_base::
477 __gnu_cxx::__scoped_lock
sentry(_M_get_mutex());
478 detach_all(_M_iterators
);
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;
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
); }
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
);
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
;
512 __it
->_M_next
->_M_prior
= __it
;
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
);
526 _Safe_unordered_container_base::
527 _M_detach_local_single(_Safe_iterator_base
* __it
) throw ()
529 // Remove __it from this container's list
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
;
540 using _Error_formatter
= __gnu_debug::_Error_formatter
;
541 using _Parameter
= __gnu_debug::_Error_formatter::_Parameter
;
544 get_max_length(std::size_t& max_length
)
546 const char* nptr
= std::getenv("GLIBCXX_DEBUG_MESSAGE_LENGTH");
550 const unsigned long ret
= std::strtoul(nptr
, &endptr
, 0);
551 if (*nptr
!= '\0' && *endptr
== '\0')
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
;
569 template<size_t Length
>
571 print_literal(PrintContext
& ctx
, const char(&word
)[Length
])
572 { print_word(ctx
, word
, Length
- 1); }
575 print_word(PrintContext
& ctx
, const char* word
,
576 std::ptrdiff_t count
= -1)
578 size_t length
= count
>= 0 ? count
: __builtin_strlen(word
);
582 // Consider first '\n' at begining cause it impacts column.
585 fprintf(stderr
, "\n");
595 = isspace(word
[length
- 1]) ? length
- 1 : length
;
596 if (visual_length
== 0
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
)
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;
620 ctx
._M_column
+= written
;
624 print_literal(ctx
, "\n");
625 print_word(ctx
, word
, count
);
629 template<size_t Length
>
631 print_type(PrintContext
& ctx
,
632 const type_info
* info
,
633 const char(&unknown_name
)[Length
])
636 print_literal(ctx
, unknown_name
);
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
);
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>");
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)
674 int ret
= __builtin_sprintf(buf
, "%p", inst
._M_address
);
675 print_word(ctx
, buf
, ret
);
684 print_field(PrintContext
& ctx
, const _Parameter
& param
, const char* name
)
686 assert(param
._M_kind
!= _Parameter::__unused_param
);
687 const int bufsize
= 64;
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)
701 constness_names
[_Error_formatter::__last_constness
] =
707 print_word(ctx
, constness_names
[iterator
._M_constness
]);
709 else if (__builtin_strcmp(name
, "state") == 0)
712 state_names
[_Error_formatter::__last_state
] =
716 "dereferenceable (start-of-sequence)",
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>");
736 case _Parameter::__sequence
:
737 if (!print_field(ctx
, name
, variant
._M_sequence
))
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
);
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
);
761 case _Parameter::__instance
:
762 if (!print_field(ctx
, name
, variant
._M_instance
))
766 case _Parameter::__iterator_value_type
:
767 if (!print_field(ctx
, name
, variant
._M_iterator_value_type
))
778 print_description(PrintContext
& ctx
, const _Parameter::_Type
& type
)
782 print_literal(ctx
, "\"");
783 print_word(ctx
, type
._M_name
);
784 print_literal(ctx
, "\"");
787 print_literal(ctx
, " {\n");
791 print_literal(ctx
, " type = ");
792 print_type(ctx
, type
._M_type
, "<unknown type>");
793 print_literal(ctx
, ";\n");
798 print_description(PrintContext
& ctx
, const _Parameter::_Instance
& inst
)
800 const int bufsize
= 64;
805 print_literal(ctx
, "\"");
806 print_word(ctx
, inst
._M_name
);
807 print_literal(ctx
, "\" ");
811 = __builtin_sprintf(buf
, "@ 0x%p {\n", inst
._M_address
);
812 print_word(ctx
, buf
, written
);
816 print_literal(ctx
, " type = ");
817 print_type(ctx
, inst
._M_type
, "<unknown type>");
822 print_description(PrintContext
& ctx
, const _Parameter
& param
)
824 const int bufsize
= 128;
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
);
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");
858 print_literal(ctx
, " references sequence ");
861 print_literal(ctx
, "with type '");
862 print_field(ctx
, param
, "seq_type");
863 print_literal(ctx
, "' ");
867 = __builtin_sprintf(buf
, "@ 0x%p\n", ite
._M_sequence
);
868 print_word(ctx
, buf
, written
);
871 print_literal(ctx
, "}\n");
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");
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");
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");
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;
919 buf
[bufindex
++] = *start
++;
920 buf
[bufindex
] = '\0';
921 print_word(ctx
, buf
, bufindex
);
929 buf
[bufindex
++] = *start
++;
936 buf
[bufindex
++] = *start
++;
940 // We are on a parameter property reference, we need to flush buffer
944 buf
[bufindex
] = '\0';
945 print_word(ctx
, buf
, bufindex
);
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.
960 assert(*start
== ';');
962 if (param
._M_kind
== _Parameter::__integer
)
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
);
975 // Extract the field name we want
976 const int max_field_len
= 16;
977 char field
[max_field_len
];
980 while (*start
!= ';')
983 assert(field_idx
< max_field_len
- 1);
984 field
[field_idx
++] = *start
++;
987 field
[field_idx
] = '\0';
989 print_field(ctx
, param
, field
);
992 // Might need to flush.
995 buf
[bufindex
] = '\0';
996 print_word(ctx
, buf
, bufindex
);
1001 namespace __gnu_debug
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
]);
1011 _Error_formatter::_M_error() const
1013 // Emit file & line number information
1014 bool go_to_next_line
= false;
1018 print_word(ctx
, _M_file
);
1019 print_literal(ctx
, ":");
1020 go_to_next_line
= true;
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
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
:
1058 print_literal(ctx
, "\nObjects involved in the operation:\n");
1061 print_description(ctx
, _M_parameters
[i
]);
1072 #if !_GLIBCXX_INLINE_VERSION
1073 // Deprecated methods kept for backward compatibility.
1075 _Error_formatter::_Parameter::_M_print_field(
1076 const _Error_formatter
*, const char*) const
1080 _Error_formatter::_Parameter::_M_print_description(const _Error_formatter
*) const
1083 template<typename _Tp
>
1085 _Error_formatter::_M_format_word(char*, int, const char*, _Tp
)
1090 _Error_formatter::_M_print_word(const char*) const
1094 _Error_formatter::_M_print_string(const char*) const
1098 _Error_formatter::_M_get_max_length() const throw ()
1104 _Error_formatter::_M_format_word(char*, int, const char*,
1109 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
1113 _Error_formatter::_M_format_word(char*, int, const char*,
1118 _Error_formatter::_M_format_word(char*, int, const char*,
1122 } // namespace __gnu_debug