1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
4 // Free Software Foundation, Inc.
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 2, or (at your option)
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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 #include <debug/debug.h>
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_iterator.h>
40 #include <bits/concurrence.h>
44 namespace __gnu_internal
46 __glibcxx_mutex_define_initialized(iterator_base_mutex);
47 } // namespace __gnu_internal
52 __fancy_abort(const char* __file, int __line, const char* __function,
53 const char* __condition)
55 printf("%s:%d: %s: Assertion '%s' failed.\n", __file, __line,
56 __function, __condition);
60 const char* _S_debug_messages[] =
62 "function requires a valid iterator range [%1.name;, %2.name;)",
63 "attempt to insert into container with a singular iterator",
64 "attempt to insert into container with an iterator"
65 " from a different container",
66 "attempt to erase from container with a %2.state; iterator",
67 "attempt to erase from container with an iterator"
68 " from a different container",
69 "attempt to subscript container with out-of-bounds index %2;,"
70 " but container only holds %3; elements",
71 "attempt to access an element in an empty container",
72 "elements in iterator range [%1.name;, %2.name;)"
73 " are not partitioned by the value %3;",
74 "elements in iterator range [%1.name;, %2.name;)"
75 " are not partitioned by the predicate %3; and value %4;",
76 "elements in iterator range [%1.name;, %2.name;) are not sorted",
77 "elements in iterator range [%1.name;, %2.name;)"
78 " are not sorted according to the predicate %3;",
79 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
80 "elements in iterator range [%1.name;, %2.name;)"
81 " do not form a heap with respect to the predicate %3;",
82 "attempt to write through a singular bitset reference",
83 "attempt to read from a singular bitset reference",
84 "attempt to flip a singular bitset reference",
85 "attempt to splice a list into itself",
86 "attempt to splice lists with inequal allocators",
87 "attempt to splice elements referenced by a %1.state; iterator",
88 "attempt to splice an iterator from a different container",
89 "splice destination %1.name;"
90 " occurs within source range [%2.name;, %3.name;)",
91 "attempt to initialize an iterator that will immediately become singular",
92 "attempt to copy-construct an iterator from a singular iterator",
93 "attempt to construct a constant iterator"
94 " from a singular mutable iterator",
95 "attempt to copy from a singular iterator",
96 "attempt to dereference a %1.state; iterator",
97 "attempt to increment a %1.state; iterator",
98 "attempt to decrement a %1.state; iterator",
99 "attempt to subscript a %1.state; iterator %2; step from"
100 " its current position, which falls outside its dereferenceable range",
101 "attempt to advance a %1.state; iterator %2; steps,"
102 " which falls outside its valid range",
103 "attempt to retreat a %1.state; iterator %2; steps,"
104 " which falls outside its valid range",
105 "attempt to compare a %1.state; iterator to a %2.state; iterator",
106 "attempt to compare iterators from different sequences",
107 "attempt to order a %1.state; iterator to a %2.state; iterator",
108 "attempt to order iterators from different sequences",
109 "attempt to compute the difference between a %1.state;"
110 " iterator to a %2.state; iterator",
111 "attempt to compute the different between two iterators"
112 " from different sequences",
113 "attempt to dereference an end-of-stream istream_iterator",
114 "attempt to increment an end-of-stream istream_iterator",
115 "attempt to output via an ostream_iterator with no associated stream",
116 "attempt to dereference an end-of-stream istreambuf_iterator"
117 " (this is a GNU extension)",
118 "attempt to increment an end-of-stream istreambuf_iterator"
122 _Safe_sequence_base::
125 for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
127 _Safe_iterator_base* __old = __iter;
128 __iter = __iter->_M_next;
129 __old->_M_attach(0, false);
132 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
134 _Safe_iterator_base* __old = __iter2;
135 __iter2 = __iter2->_M_next;
136 __old->_M_attach(0, true);
141 _Safe_sequence_base::
144 for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
146 _Safe_iterator_base* __old = __iter;
147 __iter = __iter->_M_next;
148 if (__old->_M_singular())
149 __old->_M_attach(0, false);
152 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
154 _Safe_iterator_base* __old = __iter2;
155 __iter2 = __iter2->_M_next;
156 if (__old->_M_singular())
157 __old->_M_attach(0, true);
162 _Safe_sequence_base::
163 _M_revalidate_singular()
165 _Safe_iterator_base* __iter;
166 for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
168 __iter->_M_version = _M_version;
169 __iter = __iter->_M_next;
172 for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
174 __iter->_M_version = _M_version;
175 __iter = __iter->_M_next;
180 _Safe_sequence_base::
181 _M_swap(_Safe_sequence_base& __x)
183 swap(_M_iterators, __x._M_iterators);
184 swap(_M_const_iterators, __x._M_const_iterators);
185 swap(_M_version, __x._M_version);
186 _Safe_iterator_base* __iter;
187 for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
188 __iter->_M_sequence = this;
189 for (__iter = __x._M_iterators; __iter; __iter = __iter->_M_next)
190 __iter->_M_sequence = &__x;
191 for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
192 __iter->_M_sequence = this;
193 for (__iter = __x._M_const_iterators; __iter; __iter = __iter->_M_next)
194 __iter->_M_sequence = &__x;
198 _Safe_iterator_base::
199 _M_attach(_Safe_sequence_base* __seq, bool __constant)
203 // Attach to the new sequence (if there is one)
206 __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
208 _M_version = _M_sequence->_M_version;
212 _M_next = _M_sequence->_M_const_iterators;
214 _M_next->_M_prior = this;
215 _M_sequence->_M_const_iterators = this;
219 _M_next = _M_sequence->_M_iterators;
221 _M_next->_M_prior = this;
222 _M_sequence->_M_iterators = this;
228 _Safe_iterator_base::
231 __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
234 // Remove us from this sequence's list
236 _M_prior->_M_next = _M_next;
238 _M_next->_M_prior = _M_prior;
240 if (_M_sequence->_M_const_iterators == this)
241 _M_sequence->_M_const_iterators = _M_next;
242 if (_M_sequence->_M_iterators == this)
243 _M_sequence->_M_iterators = _M_next;
253 _Safe_iterator_base::
255 { return !_M_sequence || _M_version != _M_sequence->_M_version; }
258 _Safe_iterator_base::
259 _M_can_compare(const _Safe_iterator_base& __x) const
261 return (!_M_singular() && !__x._M_singular()
262 && _M_sequence == __x._M_sequence);
266 _Error_formatter::_Parameter::
267 _M_print_field(const _Error_formatter* __formatter, const char* __name) const
269 assert(this->_M_kind != _Parameter::__unused_param);
270 const int __bufsize = 64;
271 char __buf[__bufsize];
273 if (_M_kind == __iterator)
275 if (strcmp(__name, "name") == 0)
277 assert(_M_variant._M_iterator._M_name);
278 __formatter->_M_print_word(_M_variant._M_iterator._M_name);
280 else if (strcmp(__name, "address") == 0)
282 __formatter->_M_format_word(__buf, __bufsize, "%p",
283 _M_variant._M_iterator._M_address);
284 __formatter->_M_print_word(__buf);
286 else if (strcmp(__name, "type") == 0)
288 assert(_M_variant._M_iterator._M_type);
290 __formatter->_M_print_word(_M_variant._M_iterator._M_type->name());
292 else if (strcmp(__name, "constness") == 0)
294 static const char* __constness_names[__last_constness] =
300 __formatter->_M_print_word(__constness_names[_M_variant._M_iterator._M_constness]);
302 else if (strcmp(__name, "state") == 0)
304 static const char* __state_names[__last_state] =
308 "dereferenceable (start-of-sequence)",
312 __formatter->_M_print_word(__state_names[_M_variant._M_iterator._M_state]);
314 else if (strcmp(__name, "sequence") == 0)
316 assert(_M_variant._M_iterator._M_sequence);
317 __formatter->_M_format_word(__buf, __bufsize, "%p",
318 _M_variant._M_iterator._M_sequence);
319 __formatter->_M_print_word(__buf);
321 else if (strcmp(__name, "seq_type") == 0)
324 assert(_M_variant._M_iterator._M_seq_type);
325 __formatter->_M_print_word(_M_variant._M_iterator._M_seq_type->name());
330 else if (_M_kind == __sequence)
332 if (strcmp(__name, "name") == 0)
334 assert(_M_variant._M_sequence._M_name);
335 __formatter->_M_print_word(_M_variant._M_sequence._M_name);
337 else if (strcmp(__name, "address") == 0)
339 assert(_M_variant._M_sequence._M_address);
340 __formatter->_M_format_word(__buf, __bufsize, "%p",
341 _M_variant._M_sequence._M_address);
342 __formatter->_M_print_word(__buf);
344 else if (strcmp(__name, "type") == 0)
347 assert(_M_variant._M_sequence._M_type);
348 __formatter->_M_print_word(_M_variant._M_sequence._M_type->name());
353 else if (_M_kind == __integer)
355 if (strcmp(__name, "name") == 0)
357 assert(_M_variant._M_integer._M_name);
358 __formatter->_M_print_word(_M_variant._M_integer._M_name);
363 else if (_M_kind == __string)
365 if (strcmp(__name, "name") == 0)
367 assert(_M_variant._M_string._M_name);
368 __formatter->_M_print_word(_M_variant._M_string._M_name);
380 _Error_formatter::_Parameter::
381 _M_print_description(const _Error_formatter* __formatter) const
383 const int __bufsize = 128;
384 char __buf[__bufsize];
386 if (_M_kind == __iterator)
388 __formatter->_M_print_word("iterator ");
389 if (_M_variant._M_iterator._M_name)
391 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
392 _M_variant._M_iterator._M_name);
393 __formatter->_M_print_word(__buf);
396 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
397 _M_variant._M_iterator._M_address);
398 __formatter->_M_print_word(__buf);
399 if (_M_variant._M_iterator._M_type)
401 __formatter->_M_print_word("type = ");
402 _M_print_field(__formatter, "type");
404 if (_M_variant._M_iterator._M_constness != __unknown_constness)
406 __formatter->_M_print_word(" (");
407 _M_print_field(__formatter, "constness");
408 __formatter->_M_print_word(" iterator)");
410 __formatter->_M_print_word(";\n");
413 if (_M_variant._M_iterator._M_state != __unknown_state)
415 __formatter->_M_print_word(" state = ");
416 _M_print_field(__formatter, "state");
417 __formatter->_M_print_word(";\n");
420 if (_M_variant._M_iterator._M_sequence)
422 __formatter->_M_print_word(" references sequence ");
423 if (_M_variant._M_iterator._M_seq_type)
425 __formatter->_M_print_word("with type `");
426 _M_print_field(__formatter, "seq_type");
427 __formatter->_M_print_word("' ");
430 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n",
431 _M_variant._M_sequence._M_address);
432 __formatter->_M_print_word(__buf);
434 __formatter->_M_print_word("}\n");
436 else if (_M_kind == __sequence)
438 __formatter->_M_print_word("sequence ");
439 if (_M_variant._M_sequence._M_name)
441 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
442 _M_variant._M_sequence._M_name);
443 __formatter->_M_print_word(__buf);
446 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
447 _M_variant._M_sequence._M_address);
448 __formatter->_M_print_word(__buf);
450 if (_M_variant._M_sequence._M_type)
452 __formatter->_M_print_word(" type = ");
453 _M_print_field(__formatter, "type");
454 __formatter->_M_print_word(";\n");
456 __formatter->_M_print_word("}\n");
460 const _Error_formatter&
461 _Error_formatter::_M_message(_Debug_msg_id __id) const
462 { return this->_M_message(_S_debug_messages[__id]); }
465 _Error_formatter::_M_error() const
467 const int __bufsize = 128;
468 char __buf[__bufsize];
470 // Emit file & line number information
475 _M_format_word(__buf, __bufsize, "%s:", _M_file);
476 _M_print_word(__buf);
477 _M_column += strlen(__buf);
482 _M_format_word(__buf, __bufsize, "%u:", _M_line);
483 _M_print_word(__buf);
484 _M_column += strlen(__buf);
488 _M_print_word("error: ");
490 // Print the error message
492 _M_print_string(_M_text);
493 _M_print_word(".\n");
495 // Emit descriptions of the objects involved in the operation
497 bool __has_noninteger_parameters = false;
498 for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
500 if (_M_parameters[__i]._M_kind == _Parameter::__iterator
501 || _M_parameters[__i]._M_kind == _Parameter::__sequence)
503 if (!__has_noninteger_parameters)
505 _M_first_line = true;
506 _M_print_word("\nObjects involved in the operation:\n");
507 __has_noninteger_parameters = true;
509 _M_parameters[__i]._M_print_description(this);
516 template<typename _Tp>
518 _Error_formatter::_M_format_word(char* __buf,
519 int __n __attribute__ ((__unused__)),
520 const char* __fmt, _Tp __s) const
522 #ifdef _GLIBCXX_USE_C99
523 std::snprintf(__buf, __n, __fmt, __s);
525 std::sprintf(__buf, __fmt, __s);
531 _Error_formatter::_M_print_word(const char* __word) const
535 fprintf(stderr, "%s", __word);
539 size_t __length = strlen(__word);
543 if ((_M_column + __length < _M_max_length)
544 || (__length >= _M_max_length && _M_column == 1))
546 // If this isn't the first line, indent
547 if (_M_column == 1 && !_M_first_line)
549 char __spacing[_M_indent + 1];
550 for (int i = 0; i < _M_indent; ++i)
552 __spacing[_M_indent] = '\0';
553 fprintf(stderr, "%s", __spacing);
554 _M_column += _M_indent;
557 fprintf(stderr, "%s", __word);
558 _M_column += __length;
560 if (__word[__length - 1] == '\n')
562 _M_first_line = false;
570 _M_print_word(__word);
576 _M_print_string(const char* __string) const
578 const char* __start = __string;
579 const char* __end = __start;
580 const int __bufsize = 128;
581 char __buf[__bufsize];
587 // [__start, __end) denotes the next word
589 while (isalnum(*__end))
591 if (__start == __end)
596 const ptrdiff_t __len = __end - __start;
597 assert(__len < __bufsize);
598 memcpy(__buf, __start, __len);
600 _M_print_word(__buf);
603 // Skip extra whitespace
604 while (*__start == ' ')
619 // Get the parameter number
620 assert(*__start >= '1' && *__start <= '9');
621 size_t __param = *__start - '0';
623 assert(__param < _M_num_parameters);
625 // '.' separates the parameter number from the field
626 // name, if there is one.
630 assert(*__start == ';');
633 if (_M_parameters[__param]._M_kind == _Parameter::__integer)
635 _M_format_word(__buf, __bufsize, "%ld",
636 _M_parameters[__param]._M_variant._M_integer._M_value);
637 _M_print_word(__buf);
639 else if (_M_parameters[__param]._M_kind == _Parameter::__string)
640 _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
644 // Extract the field name we want
645 enum { __max_field_len = 16 };
646 char __field[__max_field_len];
649 while (*__start != ';')
652 assert(__field_idx < __max_field_len-1);
653 __field[__field_idx++] = *__start++;
656 __field[__field_idx] = 0;
658 _M_parameters[__param]._M_print_field(this, __field);
665 _Error_formatter::_M_format_word(char*, int, const char*,
670 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
674 _Error_formatter::_M_format_word(char*, int, const char*,
679 _Error_formatter::_M_format_word(char*, int, const char*,
681 } // namespace __gnu_debug