1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003, 2004
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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_attach_mutex);
47 __glibcxx_mutex_define_initialized(iterator_base_detach_mutex);
49 } // namespace __gnu_internal
53 const char* _S_debug_messages[] =
55 "function requires a valid iterator range [%1.name;, %2.name;)",
56 "attempt to insert into container with a singular iterator",
57 "attempt to insert into container with an iterator"
58 " from a different container",
59 "attempt to erase from container with a %2.state; iterator",
60 "attempt to erase from container with an iterator"
61 " from a different container",
62 "attempt to subscript container with out-of-bounds index %2;,"
63 " but container only holds %3; elements",
64 "attempt to access an element in an empty container",
65 "elements in iterator range [%1.name;, %2.name;)"
66 " are not partitioned by the value %3;",
67 "elements in iterator range [%1.name;, %2.name;)"
68 " are not partitioned by the predicate %3; and value %4;",
69 "elements in iterator range [%1.name;, %2.name;) are not sorted",
70 "elements in iterator range [%1.name;, %2.name;)"
71 " are not sorted according to the predicate %3;",
72 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
73 "elements in iterator range [%1.name;, %2.name;)"
74 " do not form a heap with respect to the predicate %3;",
75 "attempt to write through a singular bitset reference",
76 "attempt to read from a singular bitset reference",
77 "attempt to flip a singular bitset reference",
78 "attempt to splice a list into itself",
79 "attempt to splice lists with inequal allocators",
80 "attempt to splice elements referenced by a %1.state; iterator",
81 "attempt to splice an iterator from a different container",
82 "splice destination %1.name;"
83 " occurs within source range [%2.name;, %3.name;)",
84 "attempt to initialize an iterator that will immediately become singular",
85 "attempt to copy-construct an iterator from a singular iterator",
86 "attempt to construct a constant iterator"
87 " from a singular mutable iterator",
88 "attempt to copy from a singular iterator",
89 "attempt to dereference a %1.state; iterator",
90 "attempt to increment a %1.state; iterator",
91 "attempt to decrement a %1.state; iterator",
92 "attempt to subscript a %1.state; iterator %2; step from"
93 " its current position, which falls outside its dereferenceable range",
94 "attempt to advance a %1.state; iterator %2; steps,"
95 " which falls outside its valid range",
96 "attempt to retreat a %1.state; iterator %2; steps,"
97 " which falls outside its valid range",
98 "attempt to compare a %1.state; iterator to a %2.state; iterator",
99 "attempt to compare iterators from different sequences",
100 "attempt to order a %1.state; iterator to a %2.state; iterator",
101 "attempt to order iterators from different sequences",
102 "attempt to compute the difference between a %1.state;"
103 " iterator to a %2.state; iterator",
104 "attempt to compute the different between two iterators"
105 " from different sequences",
106 "attempt to dereference an end-of-stream istream_iterator",
107 "attempt to increment an end-of-stream istream_iterator",
108 "attempt to output via an ostream_iterator with no associated stream",
109 "attempt to dereference an end-of-stream istreambuf_iterator"
110 " (this is a GNU extension)",
111 "attempt to increment an end-of-stream istreambuf_iterator"
115 _Safe_sequence_base::
118 for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
120 _Safe_iterator_base* __old = __iter;
121 __iter = __iter->_M_next;
122 __old->_M_attach(0, false);
125 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
127 _Safe_iterator_base* __old = __iter2;
128 __iter2 = __iter2->_M_next;
129 __old->_M_attach(0, true);
134 _Safe_sequence_base::
137 for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
139 _Safe_iterator_base* __old = __iter;
140 __iter = __iter->_M_next;
141 if (__old->_M_singular())
142 __old->_M_attach(0, false);
145 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
147 _Safe_iterator_base* __old = __iter2;
148 __iter2 = __iter2->_M_next;
149 if (__old->_M_singular())
150 __old->_M_attach(0, true);
155 _Safe_sequence_base::
156 _M_revalidate_singular()
158 _Safe_iterator_base* __iter;
159 for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
161 __iter->_M_version = _M_version;
162 __iter = __iter->_M_next;
165 for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
167 __iter->_M_version = _M_version;
168 __iter = __iter->_M_next;
173 _Safe_sequence_base::
174 _M_swap(_Safe_sequence_base& __x)
176 swap(_M_iterators, __x._M_iterators);
177 swap(_M_const_iterators, __x._M_const_iterators);
178 swap(_M_version, __x._M_version);
179 _Safe_iterator_base* __iter;
180 for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
181 __iter->_M_sequence = this;
182 for (__iter = __x._M_iterators; __iter; __iter = __iter->_M_next)
183 __iter->_M_sequence = &__x;
184 for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
185 __iter->_M_sequence = this;
186 for (__iter = __x._M_const_iterators; __iter; __iter = __iter->_M_next)
187 __iter->_M_sequence = &__x;
191 _Safe_iterator_base::
192 _M_attach(_Safe_sequence_base* __seq, bool __constant)
196 // Attach to the new sequence (if there is one)
199 __gnu_cxx::lock sentry(__gnu_internal::iterator_base_attach_mutex);
201 _M_version = _M_sequence->_M_version;
205 _M_next = _M_sequence->_M_const_iterators;
207 _M_next->_M_prior = this;
208 _M_sequence->_M_const_iterators = this;
212 _M_next = _M_sequence->_M_iterators;
214 _M_next->_M_prior = this;
215 _M_sequence->_M_iterators = this;
221 _Safe_iterator_base::
224 __gnu_cxx::lock sentry(__gnu_internal::iterator_base_detach_mutex);
227 // Remove us from this sequence's list
229 _M_prior->_M_next = _M_next;
231 _M_next->_M_prior = _M_prior;
233 if (_M_sequence->_M_const_iterators == this)
234 _M_sequence->_M_const_iterators = _M_next;
235 if (_M_sequence->_M_iterators == this)
236 _M_sequence->_M_iterators = _M_next;
246 _Safe_iterator_base::
248 { return !_M_sequence || _M_version != _M_sequence->_M_version; }
251 _Safe_iterator_base::
252 _M_can_compare(const _Safe_iterator_base& __x) const
254 return (!_M_singular() && !__x._M_singular()
255 && _M_sequence == __x._M_sequence);
259 _Error_formatter::_Parameter::
260 _M_print_field(const _Error_formatter* __formatter, const char* __name) const
262 assert(this->_M_kind != _Parameter::__unused_param);
263 const int __bufsize = 64;
264 char __buf[__bufsize];
266 if (_M_kind == __iterator)
268 if (strcmp(__name, "name") == 0)
270 assert(_M_variant._M_iterator._M_name);
271 __formatter->_M_print_word(_M_variant._M_iterator._M_name);
273 else if (strcmp(__name, "address") == 0)
275 __formatter->_M_format_word(__buf, __bufsize, "%p",
276 _M_variant._M_iterator._M_address);
277 __formatter->_M_print_word(__buf);
279 else if (strcmp(__name, "type") == 0)
281 assert(_M_variant._M_iterator._M_type);
283 __formatter->_M_print_word(_M_variant._M_iterator._M_type->name());
285 else if (strcmp(__name, "constness") == 0)
287 static const char* __constness_names[__last_constness] =
293 __formatter->_M_print_word(__constness_names[_M_variant._M_iterator._M_constness]);
295 else if (strcmp(__name, "state") == 0)
297 static const char* __state_names[__last_state] =
301 "dereferenceable (start-of-sequence)",
305 __formatter->_M_print_word(__state_names[_M_variant._M_iterator._M_state]);
307 else if (strcmp(__name, "sequence") == 0)
309 assert(_M_variant._M_iterator._M_sequence);
310 __formatter->_M_format_word(__buf, __bufsize, "%p",
311 _M_variant._M_iterator._M_sequence);
312 __formatter->_M_print_word(__buf);
314 else if (strcmp(__name, "seq_type") == 0)
317 assert(_M_variant._M_iterator._M_seq_type);
318 __formatter->_M_print_word(_M_variant._M_iterator._M_seq_type->name());
323 else if (_M_kind == __sequence)
325 if (strcmp(__name, "name") == 0)
327 assert(_M_variant._M_sequence._M_name);
328 __formatter->_M_print_word(_M_variant._M_sequence._M_name);
330 else if (strcmp(__name, "address") == 0)
332 assert(_M_variant._M_sequence._M_address);
333 __formatter->_M_format_word(__buf, __bufsize, "%p",
334 _M_variant._M_sequence._M_address);
335 __formatter->_M_print_word(__buf);
337 else if (strcmp(__name, "type") == 0)
340 assert(_M_variant._M_sequence._M_type);
341 __formatter->_M_print_word(_M_variant._M_sequence._M_type->name());
346 else if (_M_kind == __integer)
348 if (strcmp(__name, "name") == 0)
350 assert(_M_variant._M_integer._M_name);
351 __formatter->_M_print_word(_M_variant._M_integer._M_name);
356 else if (_M_kind == __string)
358 if (strcmp(__name, "name") == 0)
360 assert(_M_variant._M_string._M_name);
361 __formatter->_M_print_word(_M_variant._M_string._M_name);
373 _Error_formatter::_Parameter::
374 _M_print_description(const _Error_formatter* __formatter) const
376 const int __bufsize = 128;
377 char __buf[__bufsize];
379 if (_M_kind == __iterator)
381 __formatter->_M_print_word("iterator ");
382 if (_M_variant._M_iterator._M_name)
384 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
385 _M_variant._M_iterator._M_name);
386 __formatter->_M_print_word(__buf);
389 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
390 _M_variant._M_iterator._M_address);
391 __formatter->_M_print_word(__buf);
392 if (_M_variant._M_iterator._M_type)
394 __formatter->_M_print_word("type = ");
395 _M_print_field(__formatter, "type");
397 if (_M_variant._M_iterator._M_constness != __unknown_constness)
399 __formatter->_M_print_word(" (");
400 _M_print_field(__formatter, "constness");
401 __formatter->_M_print_word(" iterator)");
403 __formatter->_M_print_word(";\n");
406 if (_M_variant._M_iterator._M_state != __unknown_state)
408 __formatter->_M_print_word(" state = ");
409 _M_print_field(__formatter, "state");
410 __formatter->_M_print_word(";\n");
413 if (_M_variant._M_iterator._M_sequence)
415 __formatter->_M_print_word(" references sequence ");
416 if (_M_variant._M_iterator._M_seq_type)
418 __formatter->_M_print_word("with type `");
419 _M_print_field(__formatter, "seq_type");
420 __formatter->_M_print_word("' ");
423 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n",
424 _M_variant._M_sequence._M_address);
425 __formatter->_M_print_word(__buf);
427 __formatter->_M_print_word("}\n");
429 else if (_M_kind == __sequence)
431 __formatter->_M_print_word("sequence ");
432 if (_M_variant._M_sequence._M_name)
434 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
435 _M_variant._M_sequence._M_name);
436 __formatter->_M_print_word(__buf);
439 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
440 _M_variant._M_sequence._M_address);
441 __formatter->_M_print_word(__buf);
443 if (_M_variant._M_sequence._M_type)
445 __formatter->_M_print_word(" type = ");
446 _M_print_field(__formatter, "type");
447 __formatter->_M_print_word(";\n");
449 __formatter->_M_print_word("}\n");
453 const _Error_formatter&
454 _Error_formatter::_M_message(_Debug_msg_id __id) const
455 { return this->_M_message(_S_debug_messages[__id]); }
458 _Error_formatter::_M_error() const
460 const int __bufsize = 128;
461 char __buf[__bufsize];
463 // Emit file & line number information
468 _M_format_word(__buf, __bufsize, "%s:", _M_file);
469 _M_print_word(__buf);
470 _M_column += strlen(__buf);
475 _M_format_word(__buf, __bufsize, "%u:", _M_line);
476 _M_print_word(__buf);
477 _M_column += strlen(__buf);
481 _M_print_word("error: ");
483 // Print the error message
485 _M_print_string(_M_text);
486 _M_print_word(".\n");
488 // Emit descriptions of the objects involved in the operation
490 bool __has_noninteger_parameters = false;
491 for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
493 if (_M_parameters[__i]._M_kind == _Parameter::__iterator
494 || _M_parameters[__i]._M_kind == _Parameter::__sequence)
496 if (!__has_noninteger_parameters)
498 _M_first_line = true;
499 _M_print_word("\nObjects involved in the operation:\n");
500 __has_noninteger_parameters = true;
502 _M_parameters[__i]._M_print_description(this);
509 template<typename _Tp>
511 _Error_formatter::_M_format_word(char* __buf,
512 int __n __attribute__((__unused__)),
513 const char* __fmt, _Tp __s) const
515 #ifdef _GLIBCXX_USE_C99
516 std::snprintf(__buf, __n, __fmt, __s);
518 std::sprintf(__buf, __fmt, __s);
524 _Error_formatter::_M_print_word(const char* __word) const
528 fprintf(stderr, "%s", __word);
532 size_t __length = strlen(__word);
536 if ((_M_column + __length < _M_max_length)
537 || (__length >= _M_max_length && _M_column == 1))
539 // If this isn't the first line, indent
540 if (_M_column == 1 && !_M_first_line)
542 char __spacing[_M_indent + 1];
543 for (int i = 0; i < _M_indent; ++i)
545 __spacing[_M_indent] = '\0';
546 fprintf(stderr, "%s", __spacing);
547 _M_column += _M_indent;
550 fprintf(stderr, "%s", __word);
551 _M_column += __length;
553 if (__word[__length - 1] == '\n')
555 _M_first_line = false;
563 _M_print_word(__word);
569 _M_print_string(const char* __string) const
571 const char* __start = __string;
572 const char* __end = __start;
573 const int __bufsize = 128;
574 char __buf[__bufsize];
580 // [__start, __end) denotes the next word
582 while (isalnum(*__end))
584 if (__start == __end)
589 const ptrdiff_t __len = __end - __start;
590 assert(__len < __bufsize);
591 memcpy(__buf, __start, __len);
593 _M_print_word(__buf);
596 // Skip extra whitespace
597 while (*__start == ' ')
612 // Get the parameter number
613 assert(*__start >= '1' && *__start <= '9');
614 size_t __param = *__start - '0';
616 assert(__param < _M_num_parameters);
618 // '.' separates the parameter number from the field
619 // name, if there is one.
623 assert(*__start == ';');
626 if (_M_parameters[__param]._M_kind == _Parameter::__integer)
628 _M_format_word(__buf, __bufsize, "%ld",
629 _M_parameters[__param]._M_variant._M_integer._M_value);
630 _M_print_word(__buf);
632 else if (_M_parameters[__param]._M_kind == _Parameter::__string)
633 _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
637 // Extract the field name we want
638 enum { __max_field_len = 16 };
639 char __field[__max_field_len];
642 while (*__start != ';')
645 assert(__field_idx < __max_field_len-1);
646 __field[__field_idx++] = *__start++;
649 __field[__field_idx] = 0;
651 _M_parameters[__param]._M_print_field(this, __field);
658 _Error_formatter::_M_format_word(char* __buf, int __n, const char* __fmt,
659 const void* __s) const;
663 _Error_formatter::_M_format_word(char* __buf, int __n, const char* __fmt,
668 _Error_formatter::_M_format_word(char* __buf, int __n, const char* __fmt,
669 std::size_t __s) const;
673 _Error_formatter::_M_format_word(char* __buf, int __n, const char* __fmt,
674 const char* __s) const;
675 } // namespace __gnu_debug