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>
38 #include <bits/concurrence.h>
42 namespace __gnu_internal
44 static __glibcxx_mutex_define_initialized(iterator_base_mutex
);
45 } // namespace __gnu_internal
51 const char* _S_debug_messages
[] =
53 "function requires a valid iterator range [%1.name;, %2.name;)",
54 "attempt to insert into container with a singular iterator",
55 "attempt to insert into container with an iterator"
56 " from a different container",
57 "attempt to erase from container with a %2.state; iterator",
58 "attempt to erase from container with an iterator"
59 " from a different container",
60 "attempt to subscript container with out-of-bounds index %2;,"
61 " but container only holds %3; elements",
62 "attempt to access an element in an empty container",
63 "elements in iterator range [%1.name;, %2.name;)"
64 " are not partitioned by the value %3;",
65 "elements in iterator range [%1.name;, %2.name;)"
66 " are not partitioned by the predicate %3; and value %4;",
67 "elements in iterator range [%1.name;, %2.name;) are not sorted",
68 "elements in iterator range [%1.name;, %2.name;)"
69 " are not sorted according to the predicate %3;",
70 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
71 "elements in iterator range [%1.name;, %2.name;)"
72 " do not form a heap with respect to the predicate %3;",
73 "attempt to write through a singular bitset reference",
74 "attempt to read from a singular bitset reference",
75 "attempt to flip a singular bitset reference",
76 "attempt to splice a list into itself",
77 "attempt to splice lists with inequal allocators",
78 "attempt to splice elements referenced by a %1.state; iterator",
79 "attempt to splice an iterator from a different container",
80 "splice destination %1.name;"
81 " occurs within source range [%2.name;, %3.name;)",
82 "attempt to initialize an iterator that will immediately become singular",
83 "attempt to copy-construct an iterator from a singular iterator",
84 "attempt to construct a constant iterator"
85 " from a singular mutable iterator",
86 "attempt to copy from a singular iterator",
87 "attempt to dereference a %1.state; iterator",
88 "attempt to increment a %1.state; iterator",
89 "attempt to decrement a %1.state; iterator",
90 "attempt to subscript a %1.state; iterator %2; step from"
91 " its current position, which falls outside its dereferenceable range",
92 "attempt to advance a %1.state; iterator %2; steps,"
93 " which falls outside its valid range",
94 "attempt to retreat a %1.state; iterator %2; steps,"
95 " which falls outside its valid range",
96 "attempt to compare a %1.state; iterator to a %2.state; iterator",
97 "attempt to compare iterators from different sequences",
98 "attempt to order a %1.state; iterator to a %2.state; iterator",
99 "attempt to order iterators from different sequences",
100 "attempt to compute the difference between a %1.state;"
101 " iterator to a %2.state; iterator",
102 "attempt to compute the different between two iterators"
103 " from different sequences",
104 "attempt to dereference an end-of-stream istream_iterator",
105 "attempt to increment an end-of-stream istream_iterator",
106 "attempt to output via an ostream_iterator with no associated stream",
107 "attempt to dereference an end-of-stream istreambuf_iterator"
108 " (this is a GNU extension)",
109 "attempt to increment an end-of-stream istreambuf_iterator"
113 _Safe_sequence_base::
116 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
; )
118 _Safe_iterator_base
* __old
= __iter
;
119 __iter
= __iter
->_M_next
;
120 __old
->_M_attach(0, false);
123 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
; )
125 _Safe_iterator_base
* __old
= __iter2
;
126 __iter2
= __iter2
->_M_next
;
127 __old
->_M_attach(0, true);
132 _Safe_sequence_base::
135 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
; )
137 _Safe_iterator_base
* __old
= __iter
;
138 __iter
= __iter
->_M_next
;
139 if (__old
->_M_singular())
140 __old
->_M_attach(0, false);
143 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
; )
145 _Safe_iterator_base
* __old
= __iter2
;
146 __iter2
= __iter2
->_M_next
;
147 if (__old
->_M_singular())
148 __old
->_M_attach(0, true);
153 _Safe_sequence_base::
154 _M_revalidate_singular()
156 _Safe_iterator_base
* __iter
;
157 for (__iter
= _M_iterators
; __iter
; __iter
= __iter
->_M_next
)
159 __iter
->_M_version
= _M_version
;
160 __iter
= __iter
->_M_next
;
163 for (__iter
= _M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
165 __iter
->_M_version
= _M_version
;
166 __iter
= __iter
->_M_next
;
171 _Safe_sequence_base::
172 _M_swap(_Safe_sequence_base
& __x
)
174 swap(_M_iterators
, __x
._M_iterators
);
175 swap(_M_const_iterators
, __x
._M_const_iterators
);
176 swap(_M_version
, __x
._M_version
);
177 _Safe_iterator_base
* __iter
;
178 for (__iter
= _M_iterators
; __iter
; __iter
= __iter
->_M_next
)
179 __iter
->_M_sequence
= this;
180 for (__iter
= __x
._M_iterators
; __iter
; __iter
= __iter
->_M_next
)
181 __iter
->_M_sequence
= &__x
;
182 for (__iter
= _M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
183 __iter
->_M_sequence
= this;
184 for (__iter
= __x
._M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
185 __iter
->_M_sequence
= &__x
;
189 _Safe_iterator_base::
190 _M_attach(_Safe_sequence_base
* __seq
, bool __constant
)
194 // Attach to the new sequence (if there is one)
197 __gnu_cxx::lock
sentry(__gnu_internal::iterator_base_mutex
);
199 _M_version
= _M_sequence
->_M_version
;
203 _M_next
= _M_sequence
->_M_const_iterators
;
205 _M_next
->_M_prior
= this;
206 _M_sequence
->_M_const_iterators
= this;
210 _M_next
= _M_sequence
->_M_iterators
;
212 _M_next
->_M_prior
= this;
213 _M_sequence
->_M_iterators
= this;
219 _Safe_iterator_base::
222 __gnu_cxx::lock
sentry(__gnu_internal::iterator_base_mutex
);
225 // Remove us from this sequence's list
227 _M_prior
->_M_next
= _M_next
;
229 _M_next
->_M_prior
= _M_prior
;
231 if (_M_sequence
->_M_const_iterators
== this)
232 _M_sequence
->_M_const_iterators
= _M_next
;
233 if (_M_sequence
->_M_iterators
== this)
234 _M_sequence
->_M_iterators
= _M_next
;
244 _Safe_iterator_base::
246 { return !_M_sequence
|| _M_version
!= _M_sequence
->_M_version
; }
249 _Safe_iterator_base::
250 _M_can_compare(const _Safe_iterator_base
& __x
) const
252 return (!_M_singular()
253 && !__x
._M_singular() && _M_sequence
== __x
._M_sequence
);
257 _Error_formatter::_Parameter::
258 _M_print_field(const _Error_formatter
* __formatter
, const char* __name
) const
260 assert(this->_M_kind
!= _Parameter::__unused_param
);
261 const int __bufsize
= 64;
262 char __buf
[__bufsize
];
264 if (_M_kind
== __iterator
)
266 if (strcmp(__name
, "name") == 0)
268 assert(_M_variant
._M_iterator
._M_name
);
269 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_name
);
271 else if (strcmp(__name
, "address") == 0)
273 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
274 _M_variant
._M_iterator
._M_address
);
275 __formatter
->_M_print_word(__buf
);
277 else if (strcmp(__name
, "type") == 0)
279 assert(_M_variant
._M_iterator
._M_type
);
281 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_type
->name());
283 else if (strcmp(__name
, "constness") == 0)
285 static const char* __constness_names
[__last_constness
] =
291 __formatter
->_M_print_word(__constness_names
[_M_variant
._M_iterator
._M_constness
]);
293 else if (strcmp(__name
, "state") == 0)
295 static const char* __state_names
[__last_state
] =
299 "dereferenceable (start-of-sequence)",
303 __formatter
->_M_print_word(__state_names
[_M_variant
._M_iterator
._M_state
]);
305 else if (strcmp(__name
, "sequence") == 0)
307 assert(_M_variant
._M_iterator
._M_sequence
);
308 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
309 _M_variant
._M_iterator
._M_sequence
);
310 __formatter
->_M_print_word(__buf
);
312 else if (strcmp(__name
, "seq_type") == 0)
315 assert(_M_variant
._M_iterator
._M_seq_type
);
316 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_seq_type
->name());
321 else if (_M_kind
== __sequence
)
323 if (strcmp(__name
, "name") == 0)
325 assert(_M_variant
._M_sequence
._M_name
);
326 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_name
);
328 else if (strcmp(__name
, "address") == 0)
330 assert(_M_variant
._M_sequence
._M_address
);
331 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
332 _M_variant
._M_sequence
._M_address
);
333 __formatter
->_M_print_word(__buf
);
335 else if (strcmp(__name
, "type") == 0)
338 assert(_M_variant
._M_sequence
._M_type
);
339 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_type
->name());
344 else if (_M_kind
== __integer
)
346 if (strcmp(__name
, "name") == 0)
348 assert(_M_variant
._M_integer
._M_name
);
349 __formatter
->_M_print_word(_M_variant
._M_integer
._M_name
);
354 else if (_M_kind
== __string
)
356 if (strcmp(__name
, "name") == 0)
358 assert(_M_variant
._M_string
._M_name
);
359 __formatter
->_M_print_word(_M_variant
._M_string
._M_name
);
371 _Error_formatter::_Parameter::
372 _M_print_description(const _Error_formatter
* __formatter
) const
374 const int __bufsize
= 128;
375 char __buf
[__bufsize
];
377 if (_M_kind
== __iterator
)
379 __formatter
->_M_print_word("iterator ");
380 if (_M_variant
._M_iterator
._M_name
)
382 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
383 _M_variant
._M_iterator
._M_name
);
384 __formatter
->_M_print_word(__buf
);
387 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
388 _M_variant
._M_iterator
._M_address
);
389 __formatter
->_M_print_word(__buf
);
390 if (_M_variant
._M_iterator
._M_type
)
392 __formatter
->_M_print_word("type = ");
393 _M_print_field(__formatter
, "type");
395 if (_M_variant
._M_iterator
._M_constness
!= __unknown_constness
)
397 __formatter
->_M_print_word(" (");
398 _M_print_field(__formatter
, "constness");
399 __formatter
->_M_print_word(" iterator)");
401 __formatter
->_M_print_word(";\n");
404 if (_M_variant
._M_iterator
._M_state
!= __unknown_state
)
406 __formatter
->_M_print_word(" state = ");
407 _M_print_field(__formatter
, "state");
408 __formatter
->_M_print_word(";\n");
411 if (_M_variant
._M_iterator
._M_sequence
)
413 __formatter
->_M_print_word(" references sequence ");
414 if (_M_variant
._M_iterator
._M_seq_type
)
416 __formatter
->_M_print_word("with type `");
417 _M_print_field(__formatter
, "seq_type");
418 __formatter
->_M_print_word("' ");
421 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p\n",
422 _M_variant
._M_sequence
._M_address
);
423 __formatter
->_M_print_word(__buf
);
425 __formatter
->_M_print_word("}\n");
427 else if (_M_kind
== __sequence
)
429 __formatter
->_M_print_word("sequence ");
430 if (_M_variant
._M_sequence
._M_name
)
432 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
433 _M_variant
._M_sequence
._M_name
);
434 __formatter
->_M_print_word(__buf
);
437 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
438 _M_variant
._M_sequence
._M_address
);
439 __formatter
->_M_print_word(__buf
);
441 if (_M_variant
._M_sequence
._M_type
)
443 __formatter
->_M_print_word(" type = ");
444 _M_print_field(__formatter
, "type");
445 __formatter
->_M_print_word(";\n");
447 __formatter
->_M_print_word("}\n");
451 const _Error_formatter
&
452 _Error_formatter::_M_message(_Debug_msg_id __id
) const
453 { return this->_M_message(_S_debug_messages
[__id
]); }
456 _Error_formatter::_M_error() const
458 const int __bufsize
= 128;
459 char __buf
[__bufsize
];
461 // Emit file & line number information
466 _M_format_word(__buf
, __bufsize
, "%s:", _M_file
);
467 _M_print_word(__buf
);
468 _M_column
+= strlen(__buf
);
473 _M_format_word(__buf
, __bufsize
, "%u:", _M_line
);
474 _M_print_word(__buf
);
475 _M_column
+= strlen(__buf
);
479 _M_print_word("error: ");
481 // Print the error message
483 _M_print_string(_M_text
);
484 _M_print_word(".\n");
486 // Emit descriptions of the objects involved in the operation
488 bool __has_noninteger_parameters
= false;
489 for (unsigned int __i
= 0; __i
< _M_num_parameters
; ++__i
)
491 if (_M_parameters
[__i
]._M_kind
== _Parameter::__iterator
492 || _M_parameters
[__i
]._M_kind
== _Parameter::__sequence
)
494 if (!__has_noninteger_parameters
)
496 _M_first_line
= true;
497 _M_print_word("\nObjects involved in the operation:\n");
498 __has_noninteger_parameters
= true;
500 _M_parameters
[__i
]._M_print_description(this);
507 template<typename _Tp
>
509 _Error_formatter::_M_format_word(char* __buf
,
510 int __n
__attribute__ ((__unused__
)),
511 const char* __fmt
, _Tp __s
) const
513 #ifdef _GLIBCXX_USE_C99
514 std::snprintf(__buf
, __n
, __fmt
, __s
);
516 std::sprintf(__buf
, __fmt
, __s
);
522 _Error_formatter::_M_print_word(const char* __word
) const
526 fprintf(stderr
, "%s", __word
);
530 size_t __length
= strlen(__word
);
534 if ((_M_column
+ __length
< _M_max_length
)
535 || (__length
>= _M_max_length
&& _M_column
== 1))
537 // If this isn't the first line, indent
538 if (_M_column
== 1 && !_M_first_line
)
540 char __spacing
[_M_indent
+ 1];
541 for (int i
= 0; i
< _M_indent
; ++i
)
543 __spacing
[_M_indent
] = '\0';
544 fprintf(stderr
, "%s", __spacing
);
545 _M_column
+= _M_indent
;
548 fprintf(stderr
, "%s", __word
);
549 _M_column
+= __length
;
551 if (__word
[__length
- 1] == '\n')
553 _M_first_line
= false;
561 _M_print_word(__word
);
567 _M_print_string(const char* __string
) const
569 const char* __start
= __string
;
570 const char* __end
= __start
;
571 const int __bufsize
= 128;
572 char __buf
[__bufsize
];
578 // [__start, __end) denotes the next word
580 while (isalnum(*__end
))
582 if (__start
== __end
)
587 const ptrdiff_t __len
= __end
- __start
;
588 assert(__len
< __bufsize
);
589 memcpy(__buf
, __start
, __len
);
591 _M_print_word(__buf
);
594 // Skip extra whitespace
595 while (*__start
== ' ')
610 // Get the parameter number
611 assert(*__start
>= '1' && *__start
<= '9');
612 size_t __param
= *__start
- '0';
614 assert(__param
< _M_num_parameters
);
616 // '.' separates the parameter number from the field
617 // name, if there is one.
621 assert(*__start
== ';');
624 if (_M_parameters
[__param
]._M_kind
== _Parameter::__integer
)
626 _M_format_word(__buf
, __bufsize
, "%ld",
627 _M_parameters
[__param
]._M_variant
._M_integer
._M_value
);
628 _M_print_word(__buf
);
630 else if (_M_parameters
[__param
]._M_kind
== _Parameter::__string
)
631 _M_print_string(_M_parameters
[__param
]._M_variant
._M_string
._M_value
);
635 // Extract the field name we want
636 enum { __max_field_len
= 16 };
637 char __field
[__max_field_len
];
640 while (*__start
!= ';')
643 assert(__field_idx
< __max_field_len
-1);
644 __field
[__field_idx
++] = *__start
++;
647 __field
[__field_idx
] = 0;
649 _M_parameters
[__param
]._M_print_field(this, __field
);
656 _Error_formatter::_M_format_word(char*, int, const char*,
661 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
665 _Error_formatter::_M_format_word(char*, int, const char*,
670 _Error_formatter::_M_format_word(char*, int, const char*,
672 } // namespace __gnu_debug