PR target/23602
[official-gcc.git] / libstdc++-v3 / src / debug.cc
blob6f32fde5e65145321464f0f7d68e60c5c9878292
1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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>
34 #include <algorithm>
35 #include <cassert>
36 #include <cstring>
37 #include <cctype>
38 #include <bits/concurrence.h>
40 using namespace std;
42 namespace __gnu_internal
44 static __glibcxx_mutex_define_initialized(iterator_base_mutex);
45 } // namespace __gnu_internal
47 namespace __gnu_debug
49 const char* _S_debug_messages[] =
51 "function requires a valid iterator range [%1.name;, %2.name;)",
52 "attempt to insert into container with a singular iterator",
53 "attempt to insert into container with an iterator"
54 " from a different container",
55 "attempt to erase from container with a %2.state; iterator",
56 "attempt to erase from container with an iterator"
57 " from a different container",
58 "attempt to subscript container with out-of-bounds index %2;,"
59 " but container only holds %3; elements",
60 "attempt to access an element in an empty container",
61 "elements in iterator range [%1.name;, %2.name;)"
62 " are not partitioned by the value %3;",
63 "elements in iterator range [%1.name;, %2.name;)"
64 " are not partitioned by the predicate %3; and value %4;",
65 "elements in iterator range [%1.name;, %2.name;) are not sorted",
66 "elements in iterator range [%1.name;, %2.name;)"
67 " are not sorted according to the predicate %3;",
68 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
69 "elements in iterator range [%1.name;, %2.name;)"
70 " do not form a heap with respect to the predicate %3;",
71 "attempt to write through a singular bitset reference",
72 "attempt to read from a singular bitset reference",
73 "attempt to flip a singular bitset reference",
74 "attempt to splice a list into itself",
75 "attempt to splice lists with inequal allocators",
76 "attempt to splice elements referenced by a %1.state; iterator",
77 "attempt to splice an iterator from a different container",
78 "splice destination %1.name;"
79 " occurs within source range [%2.name;, %3.name;)",
80 "attempt to initialize an iterator that will immediately become singular",
81 "attempt to copy-construct an iterator from a singular iterator",
82 "attempt to construct a constant iterator"
83 " from a singular mutable iterator",
84 "attempt to copy from a singular iterator",
85 "attempt to dereference a %1.state; iterator",
86 "attempt to increment a %1.state; iterator",
87 "attempt to decrement a %1.state; iterator",
88 "attempt to subscript a %1.state; iterator %2; step from"
89 " its current position, which falls outside its dereferenceable range",
90 "attempt to advance a %1.state; iterator %2; steps,"
91 " which falls outside its valid range",
92 "attempt to retreat a %1.state; iterator %2; steps,"
93 " which falls outside its valid range",
94 "attempt to compare a %1.state; iterator to a %2.state; iterator",
95 "attempt to compare iterators from different sequences",
96 "attempt to order a %1.state; iterator to a %2.state; iterator",
97 "attempt to order iterators from different sequences",
98 "attempt to compute the difference between a %1.state;"
99 " iterator to a %2.state; iterator",
100 "attempt to compute the different between two iterators"
101 " from different sequences",
102 "attempt to dereference an end-of-stream istream_iterator",
103 "attempt to increment an end-of-stream istream_iterator",
104 "attempt to output via an ostream_iterator with no associated stream",
105 "attempt to dereference an end-of-stream istreambuf_iterator"
106 " (this is a GNU extension)",
107 "attempt to increment an end-of-stream istreambuf_iterator"
110 void
111 _Safe_sequence_base::
112 _M_detach_all()
114 for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
116 _Safe_iterator_base* __old = __iter;
117 __iter = __iter->_M_next;
118 __old->_M_attach(0, false);
121 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
123 _Safe_iterator_base* __old = __iter2;
124 __iter2 = __iter2->_M_next;
125 __old->_M_attach(0, true);
129 void
130 _Safe_sequence_base::
131 _M_detach_singular()
133 for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
135 _Safe_iterator_base* __old = __iter;
136 __iter = __iter->_M_next;
137 if (__old->_M_singular())
138 __old->_M_attach(0, false);
141 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
143 _Safe_iterator_base* __old = __iter2;
144 __iter2 = __iter2->_M_next;
145 if (__old->_M_singular())
146 __old->_M_attach(0, true);
150 void
151 _Safe_sequence_base::
152 _M_revalidate_singular()
154 _Safe_iterator_base* __iter;
155 for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
157 __iter->_M_version = _M_version;
158 __iter = __iter->_M_next;
161 for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
163 __iter->_M_version = _M_version;
164 __iter = __iter->_M_next;
168 void
169 _Safe_sequence_base::
170 _M_swap(_Safe_sequence_base& __x)
172 swap(_M_iterators, __x._M_iterators);
173 swap(_M_const_iterators, __x._M_const_iterators);
174 swap(_M_version, __x._M_version);
175 _Safe_iterator_base* __iter;
176 for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
177 __iter->_M_sequence = this;
178 for (__iter = __x._M_iterators; __iter; __iter = __iter->_M_next)
179 __iter->_M_sequence = &__x;
180 for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
181 __iter->_M_sequence = this;
182 for (__iter = __x._M_const_iterators; __iter; __iter = __iter->_M_next)
183 __iter->_M_sequence = &__x;
186 void
187 _Safe_iterator_base::
188 _M_attach(_Safe_sequence_base* __seq, bool __constant)
190 _M_detach();
192 // Attach to the new sequence (if there is one)
193 if (__seq)
195 __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
196 _M_sequence = __seq;
197 _M_version = _M_sequence->_M_version;
198 _M_prior = 0;
199 if (__constant)
201 _M_next = _M_sequence->_M_const_iterators;
202 if (_M_next)
203 _M_next->_M_prior = this;
204 _M_sequence->_M_const_iterators = this;
206 else
208 _M_next = _M_sequence->_M_iterators;
209 if (_M_next)
210 _M_next->_M_prior = this;
211 _M_sequence->_M_iterators = this;
216 void
217 _Safe_iterator_base::
218 _M_detach()
220 __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
221 if (_M_sequence)
223 // Remove us from this sequence's list
224 if (_M_prior)
225 _M_prior->_M_next = _M_next;
226 if (_M_next)
227 _M_next->_M_prior = _M_prior;
229 if (_M_sequence->_M_const_iterators == this)
230 _M_sequence->_M_const_iterators = _M_next;
231 if (_M_sequence->_M_iterators == this)
232 _M_sequence->_M_iterators = _M_next;
235 _M_sequence = 0;
236 _M_version = 0;
237 _M_prior = 0;
238 _M_next = 0;
241 bool
242 _Safe_iterator_base::
243 _M_singular() const
244 { return !_M_sequence || _M_version != _M_sequence->_M_version; }
246 bool
247 _Safe_iterator_base::
248 _M_can_compare(const _Safe_iterator_base& __x) const
250 return (!_M_singular() && !__x._M_singular()
251 && _M_sequence == __x._M_sequence);
254 void
255 _Error_formatter::_Parameter::
256 _M_print_field(const _Error_formatter* __formatter, const char* __name) const
258 assert(this->_M_kind != _Parameter::__unused_param);
259 const int __bufsize = 64;
260 char __buf[__bufsize];
262 if (_M_kind == __iterator)
264 if (strcmp(__name, "name") == 0)
266 assert(_M_variant._M_iterator._M_name);
267 __formatter->_M_print_word(_M_variant._M_iterator._M_name);
269 else if (strcmp(__name, "address") == 0)
271 __formatter->_M_format_word(__buf, __bufsize, "%p",
272 _M_variant._M_iterator._M_address);
273 __formatter->_M_print_word(__buf);
275 else if (strcmp(__name, "type") == 0)
277 assert(_M_variant._M_iterator._M_type);
278 // TBD: demangle!
279 __formatter->_M_print_word(_M_variant._M_iterator._M_type->name());
281 else if (strcmp(__name, "constness") == 0)
283 static const char* __constness_names[__last_constness] =
285 "<unknown>",
286 "constant",
287 "mutable"
289 __formatter->_M_print_word(__constness_names[_M_variant._M_iterator._M_constness]);
291 else if (strcmp(__name, "state") == 0)
293 static const char* __state_names[__last_state] =
295 "<unknown>",
296 "singular",
297 "dereferenceable (start-of-sequence)",
298 "dereferenceable",
299 "past-the-end"
301 __formatter->_M_print_word(__state_names[_M_variant._M_iterator._M_state]);
303 else if (strcmp(__name, "sequence") == 0)
305 assert(_M_variant._M_iterator._M_sequence);
306 __formatter->_M_format_word(__buf, __bufsize, "%p",
307 _M_variant._M_iterator._M_sequence);
308 __formatter->_M_print_word(__buf);
310 else if (strcmp(__name, "seq_type") == 0)
312 // TBD: demangle!
313 assert(_M_variant._M_iterator._M_seq_type);
314 __formatter->_M_print_word(_M_variant._M_iterator._M_seq_type->name());
316 else
317 assert(false);
319 else if (_M_kind == __sequence)
321 if (strcmp(__name, "name") == 0)
323 assert(_M_variant._M_sequence._M_name);
324 __formatter->_M_print_word(_M_variant._M_sequence._M_name);
326 else if (strcmp(__name, "address") == 0)
328 assert(_M_variant._M_sequence._M_address);
329 __formatter->_M_format_word(__buf, __bufsize, "%p",
330 _M_variant._M_sequence._M_address);
331 __formatter->_M_print_word(__buf);
333 else if (strcmp(__name, "type") == 0)
335 // TBD: demangle!
336 assert(_M_variant._M_sequence._M_type);
337 __formatter->_M_print_word(_M_variant._M_sequence._M_type->name());
339 else
340 assert(false);
342 else if (_M_kind == __integer)
344 if (strcmp(__name, "name") == 0)
346 assert(_M_variant._M_integer._M_name);
347 __formatter->_M_print_word(_M_variant._M_integer._M_name);
349 else
350 assert(false);
352 else if (_M_kind == __string)
354 if (strcmp(__name, "name") == 0)
356 assert(_M_variant._M_string._M_name);
357 __formatter->_M_print_word(_M_variant._M_string._M_name);
359 else
360 assert(false);
362 else
364 assert(false);
368 void
369 _Error_formatter::_Parameter::
370 _M_print_description(const _Error_formatter* __formatter) const
372 const int __bufsize = 128;
373 char __buf[__bufsize];
375 if (_M_kind == __iterator)
377 __formatter->_M_print_word("iterator ");
378 if (_M_variant._M_iterator._M_name)
380 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
381 _M_variant._M_iterator._M_name);
382 __formatter->_M_print_word(__buf);
385 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
386 _M_variant._M_iterator._M_address);
387 __formatter->_M_print_word(__buf);
388 if (_M_variant._M_iterator._M_type)
390 __formatter->_M_print_word("type = ");
391 _M_print_field(__formatter, "type");
393 if (_M_variant._M_iterator._M_constness != __unknown_constness)
395 __formatter->_M_print_word(" (");
396 _M_print_field(__formatter, "constness");
397 __formatter->_M_print_word(" iterator)");
399 __formatter->_M_print_word(";\n");
402 if (_M_variant._M_iterator._M_state != __unknown_state)
404 __formatter->_M_print_word(" state = ");
405 _M_print_field(__formatter, "state");
406 __formatter->_M_print_word(";\n");
409 if (_M_variant._M_iterator._M_sequence)
411 __formatter->_M_print_word(" references sequence ");
412 if (_M_variant._M_iterator._M_seq_type)
414 __formatter->_M_print_word("with type `");
415 _M_print_field(__formatter, "seq_type");
416 __formatter->_M_print_word("' ");
419 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n",
420 _M_variant._M_sequence._M_address);
421 __formatter->_M_print_word(__buf);
423 __formatter->_M_print_word("}\n");
425 else if (_M_kind == __sequence)
427 __formatter->_M_print_word("sequence ");
428 if (_M_variant._M_sequence._M_name)
430 __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ",
431 _M_variant._M_sequence._M_name);
432 __formatter->_M_print_word(__buf);
435 __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n",
436 _M_variant._M_sequence._M_address);
437 __formatter->_M_print_word(__buf);
439 if (_M_variant._M_sequence._M_type)
441 __formatter->_M_print_word(" type = ");
442 _M_print_field(__formatter, "type");
443 __formatter->_M_print_word(";\n");
445 __formatter->_M_print_word("}\n");
449 const _Error_formatter&
450 _Error_formatter::_M_message(_Debug_msg_id __id) const
451 { return this->_M_message(_S_debug_messages[__id]); }
453 void
454 _Error_formatter::_M_error() const
456 const int __bufsize = 128;
457 char __buf[__bufsize];
459 // Emit file & line number information
460 _M_column = 1;
461 _M_wordwrap = false;
462 if (_M_file)
464 _M_format_word(__buf, __bufsize, "%s:", _M_file);
465 _M_print_word(__buf);
466 _M_column += strlen(__buf);
469 if (_M_line > 0)
471 _M_format_word(__buf, __bufsize, "%u:", _M_line);
472 _M_print_word(__buf);
473 _M_column += strlen(__buf);
476 _M_wordwrap = true;
477 _M_print_word("error: ");
479 // Print the error message
480 assert(_M_text);
481 _M_print_string(_M_text);
482 _M_print_word(".\n");
484 // Emit descriptions of the objects involved in the operation
485 _M_wordwrap = false;
486 bool __has_noninteger_parameters = false;
487 for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
489 if (_M_parameters[__i]._M_kind == _Parameter::__iterator
490 || _M_parameters[__i]._M_kind == _Parameter::__sequence)
492 if (!__has_noninteger_parameters)
494 _M_first_line = true;
495 _M_print_word("\nObjects involved in the operation:\n");
496 __has_noninteger_parameters = true;
498 _M_parameters[__i]._M_print_description(this);
502 abort();
505 template<typename _Tp>
506 void
507 _Error_formatter::_M_format_word(char* __buf,
508 int __n __attribute__ ((__unused__)),
509 const char* __fmt, _Tp __s) const
511 #ifdef _GLIBCXX_USE_C99
512 std::snprintf(__buf, __n, __fmt, __s);
513 #else
514 std::sprintf(__buf, __fmt, __s);
515 #endif
519 void
520 _Error_formatter::_M_print_word(const char* __word) const
522 if (!_M_wordwrap)
524 fprintf(stderr, "%s", __word);
525 return;
528 size_t __length = strlen(__word);
529 if (__length == 0)
530 return;
532 if ((_M_column + __length < _M_max_length)
533 || (__length >= _M_max_length && _M_column == 1))
535 // If this isn't the first line, indent
536 if (_M_column == 1 && !_M_first_line)
538 char __spacing[_M_indent + 1];
539 for (int i = 0; i < _M_indent; ++i)
540 __spacing[i] = ' ';
541 __spacing[_M_indent] = '\0';
542 fprintf(stderr, "%s", __spacing);
543 _M_column += _M_indent;
546 fprintf(stderr, "%s", __word);
547 _M_column += __length;
549 if (__word[__length - 1] == '\n')
551 _M_first_line = false;
552 _M_column = 1;
555 else
557 _M_column = 1;
558 _M_print_word("\n");
559 _M_print_word(__word);
563 void
564 _Error_formatter::
565 _M_print_string(const char* __string) const
567 const char* __start = __string;
568 const char* __end = __start;
569 const int __bufsize = 128;
570 char __buf[__bufsize];
572 while (*__start)
574 if (*__start != '%')
576 // [__start, __end) denotes the next word
577 __end = __start;
578 while (isalnum(*__end))
579 ++__end;
580 if (__start == __end)
581 ++__end;
582 if (isspace(*__end))
583 ++__end;
585 const ptrdiff_t __len = __end - __start;
586 assert(__len < __bufsize);
587 memcpy(__buf, __start, __len);
588 __buf[__len] = '\0';
589 _M_print_word(__buf);
590 __start = __end;
592 // Skip extra whitespace
593 while (*__start == ' ')
594 ++__start;
596 continue;
599 ++__start;
600 assert(*__start);
601 if (*__start == '%')
603 _M_print_word("%");
604 ++__start;
605 continue;
608 // Get the parameter number
609 assert(*__start >= '1' && *__start <= '9');
610 size_t __param = *__start - '0';
611 --__param;
612 assert(__param < _M_num_parameters);
614 // '.' separates the parameter number from the field
615 // name, if there is one.
616 ++__start;
617 if (*__start != '.')
619 assert(*__start == ';');
620 ++__start;
621 __buf[0] = '\0';
622 if (_M_parameters[__param]._M_kind == _Parameter::__integer)
624 _M_format_word(__buf, __bufsize, "%ld",
625 _M_parameters[__param]._M_variant._M_integer._M_value);
626 _M_print_word(__buf);
628 else if (_M_parameters[__param]._M_kind == _Parameter::__string)
629 _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
630 continue;
633 // Extract the field name we want
634 enum { __max_field_len = 16 };
635 char __field[__max_field_len];
636 int __field_idx = 0;
637 ++__start;
638 while (*__start != ';')
640 assert(*__start);
641 assert(__field_idx < __max_field_len-1);
642 __field[__field_idx++] = *__start++;
644 ++__start;
645 __field[__field_idx] = 0;
647 _M_parameters[__param]._M_print_field(this, __field);
651 // Instantiations.
652 template
653 void
654 _Error_formatter::_M_format_word(char*, int, const char*,
655 const void*) const;
657 template
658 void
659 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
661 template
662 void
663 _Error_formatter::_M_format_word(char*, int, const char*,
664 std::size_t) const;
666 template
667 void
668 _Error_formatter::_M_format_word(char*, int, const char*,
669 const char*) const;
670 } // namespace __gnu_debug