1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007
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>
44 __gnu_cxx::__mutex safe_base_mutex
;
45 } // anonymous namespace
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"
111 _Safe_sequence_base::
114 __gnu_cxx::__scoped_lock
sentry(safe_base_mutex
);
115 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
;)
117 _Safe_iterator_base
* __old
= __iter
;
118 __iter
= __iter
->_M_next
;
119 __old
->_M_detach_single();
122 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
;)
124 _Safe_iterator_base
* __old
= __iter2
;
125 __iter2
= __iter2
->_M_next
;
126 __old
->_M_detach_single();
131 _Safe_sequence_base::
134 __gnu_cxx::__scoped_lock
sentry(safe_base_mutex
);
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_detach_single();
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_detach_single();
153 _Safe_sequence_base::
154 _M_revalidate_singular()
156 __gnu_cxx::__scoped_lock
sentry(safe_base_mutex
);
157 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
;
158 __iter
= __iter
->_M_next
)
159 __iter
->_M_version
= _M_version
;
161 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
;
162 __iter2
= __iter2
->_M_next
)
163 __iter2
->_M_version
= _M_version
;
167 _Safe_sequence_base::
168 _M_swap(_Safe_sequence_base
& __x
)
170 __gnu_cxx::__scoped_lock
sentry(safe_base_mutex
);
171 swap(_M_iterators
, __x
._M_iterators
);
172 swap(_M_const_iterators
, __x
._M_const_iterators
);
173 swap(_M_version
, __x
._M_version
);
174 _Safe_iterator_base
* __iter
;
175 for (__iter
= _M_iterators
; __iter
; __iter
= __iter
->_M_next
)
176 __iter
->_M_sequence
= this;
177 for (__iter
= __x
._M_iterators
; __iter
; __iter
= __iter
->_M_next
)
178 __iter
->_M_sequence
= &__x
;
179 for (__iter
= _M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
180 __iter
->_M_sequence
= this;
181 for (__iter
= __x
._M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
182 __iter
->_M_sequence
= &__x
;
186 _Safe_sequence_base::
188 { return safe_base_mutex
; }
191 _Safe_iterator_base::
192 _M_attach(_Safe_sequence_base
* __seq
, bool __constant
)
194 __gnu_cxx::__scoped_lock
sentry(safe_base_mutex
);
195 _M_attach_single(__seq
, __constant
);
199 _Safe_iterator_base::
200 _M_attach_single(_Safe_sequence_base
* __seq
, bool __constant
)
204 // Attach to the new sequence (if there is one)
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::__scoped_lock
sentry(safe_base_mutex
);
236 _Safe_iterator_base::
241 // Remove us from this sequence's list
243 _M_prior
->_M_next
= _M_next
;
245 _M_next
->_M_prior
= _M_prior
;
247 if (_M_sequence
->_M_const_iterators
== this)
248 _M_sequence
->_M_const_iterators
= _M_next
;
249 if (_M_sequence
->_M_iterators
== this)
250 _M_sequence
->_M_iterators
= _M_next
;
260 _Safe_iterator_base::
262 { return !_M_sequence
|| _M_version
!= _M_sequence
->_M_version
; }
265 _Safe_iterator_base::
266 _M_can_compare(const _Safe_iterator_base
& __x
) const
268 return (!_M_singular()
269 && !__x
._M_singular() && _M_sequence
== __x
._M_sequence
);
273 _Safe_iterator_base::
275 { return safe_base_mutex
; }
278 _Error_formatter::_Parameter::
279 _M_print_field(const _Error_formatter
* __formatter
, const char* __name
) const
281 assert(this->_M_kind
!= _Parameter::__unused_param
);
282 const int __bufsize
= 64;
283 char __buf
[__bufsize
];
285 if (_M_kind
== __iterator
)
287 if (strcmp(__name
, "name") == 0)
289 assert(_M_variant
._M_iterator
._M_name
);
290 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_name
);
292 else if (strcmp(__name
, "address") == 0)
294 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
295 _M_variant
._M_iterator
._M_address
);
296 __formatter
->_M_print_word(__buf
);
298 else if (strcmp(__name
, "type") == 0)
300 assert(_M_variant
._M_iterator
._M_type
);
302 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_type
->name());
304 else if (strcmp(__name
, "constness") == 0)
306 static const char* __constness_names
[__last_constness
] =
312 __formatter
->_M_print_word(__constness_names
[_M_variant
._M_iterator
._M_constness
]);
314 else if (strcmp(__name
, "state") == 0)
316 static const char* __state_names
[__last_state
] =
320 "dereferenceable (start-of-sequence)",
324 __formatter
->_M_print_word(__state_names
[_M_variant
._M_iterator
._M_state
]);
326 else if (strcmp(__name
, "sequence") == 0)
328 assert(_M_variant
._M_iterator
._M_sequence
);
329 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
330 _M_variant
._M_iterator
._M_sequence
);
331 __formatter
->_M_print_word(__buf
);
333 else if (strcmp(__name
, "seq_type") == 0)
336 assert(_M_variant
._M_iterator
._M_seq_type
);
337 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_seq_type
->name());
342 else if (_M_kind
== __sequence
)
344 if (strcmp(__name
, "name") == 0)
346 assert(_M_variant
._M_sequence
._M_name
);
347 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_name
);
349 else if (strcmp(__name
, "address") == 0)
351 assert(_M_variant
._M_sequence
._M_address
);
352 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
353 _M_variant
._M_sequence
._M_address
);
354 __formatter
->_M_print_word(__buf
);
356 else if (strcmp(__name
, "type") == 0)
359 assert(_M_variant
._M_sequence
._M_type
);
360 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_type
->name());
365 else if (_M_kind
== __integer
)
367 if (strcmp(__name
, "name") == 0)
369 assert(_M_variant
._M_integer
._M_name
);
370 __formatter
->_M_print_word(_M_variant
._M_integer
._M_name
);
375 else if (_M_kind
== __string
)
377 if (strcmp(__name
, "name") == 0)
379 assert(_M_variant
._M_string
._M_name
);
380 __formatter
->_M_print_word(_M_variant
._M_string
._M_name
);
392 _Error_formatter::_Parameter::
393 _M_print_description(const _Error_formatter
* __formatter
) const
395 const int __bufsize
= 128;
396 char __buf
[__bufsize
];
398 if (_M_kind
== __iterator
)
400 __formatter
->_M_print_word("iterator ");
401 if (_M_variant
._M_iterator
._M_name
)
403 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
404 _M_variant
._M_iterator
._M_name
);
405 __formatter
->_M_print_word(__buf
);
408 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
409 _M_variant
._M_iterator
._M_address
);
410 __formatter
->_M_print_word(__buf
);
411 if (_M_variant
._M_iterator
._M_type
)
413 __formatter
->_M_print_word("type = ");
414 _M_print_field(__formatter
, "type");
416 if (_M_variant
._M_iterator
._M_constness
!= __unknown_constness
)
418 __formatter
->_M_print_word(" (");
419 _M_print_field(__formatter
, "constness");
420 __formatter
->_M_print_word(" iterator)");
422 __formatter
->_M_print_word(";\n");
425 if (_M_variant
._M_iterator
._M_state
!= __unknown_state
)
427 __formatter
->_M_print_word(" state = ");
428 _M_print_field(__formatter
, "state");
429 __formatter
->_M_print_word(";\n");
432 if (_M_variant
._M_iterator
._M_sequence
)
434 __formatter
->_M_print_word(" references sequence ");
435 if (_M_variant
._M_iterator
._M_seq_type
)
437 __formatter
->_M_print_word("with type `");
438 _M_print_field(__formatter
, "seq_type");
439 __formatter
->_M_print_word("' ");
442 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p\n",
443 _M_variant
._M_sequence
._M_address
);
444 __formatter
->_M_print_word(__buf
);
446 __formatter
->_M_print_word("}\n");
448 else if (_M_kind
== __sequence
)
450 __formatter
->_M_print_word("sequence ");
451 if (_M_variant
._M_sequence
._M_name
)
453 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
454 _M_variant
._M_sequence
._M_name
);
455 __formatter
->_M_print_word(__buf
);
458 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
459 _M_variant
._M_sequence
._M_address
);
460 __formatter
->_M_print_word(__buf
);
462 if (_M_variant
._M_sequence
._M_type
)
464 __formatter
->_M_print_word(" type = ");
465 _M_print_field(__formatter
, "type");
466 __formatter
->_M_print_word(";\n");
468 __formatter
->_M_print_word("}\n");
472 const _Error_formatter
&
473 _Error_formatter::_M_message(_Debug_msg_id __id
) const
474 { return this->_M_message(_S_debug_messages
[__id
]); }
477 _Error_formatter::_M_error() const
479 const int __bufsize
= 128;
480 char __buf
[__bufsize
];
482 // Emit file & line number information
487 _M_format_word(__buf
, __bufsize
, "%s:", _M_file
);
488 _M_print_word(__buf
);
489 _M_column
+= strlen(__buf
);
494 _M_format_word(__buf
, __bufsize
, "%u:", _M_line
);
495 _M_print_word(__buf
);
496 _M_column
+= strlen(__buf
);
500 _M_print_word("error: ");
502 // Print the error message
504 _M_print_string(_M_text
);
505 _M_print_word(".\n");
507 // Emit descriptions of the objects involved in the operation
509 bool __has_noninteger_parameters
= false;
510 for (unsigned int __i
= 0; __i
< _M_num_parameters
; ++__i
)
512 if (_M_parameters
[__i
]._M_kind
== _Parameter::__iterator
513 || _M_parameters
[__i
]._M_kind
== _Parameter::__sequence
)
515 if (!__has_noninteger_parameters
)
517 _M_first_line
= true;
518 _M_print_word("\nObjects involved in the operation:\n");
519 __has_noninteger_parameters
= true;
521 _M_parameters
[__i
]._M_print_description(this);
528 template<typename _Tp
>
530 _Error_formatter::_M_format_word(char* __buf
,
531 int __n
__attribute__ ((__unused__
)),
532 const char* __fmt
, _Tp __s
) const
534 #ifdef _GLIBCXX_USE_C99
535 std::snprintf(__buf
, __n
, __fmt
, __s
);
537 std::sprintf(__buf
, __fmt
, __s
);
543 _Error_formatter::_M_print_word(const char* __word
) const
547 fprintf(stderr
, "%s", __word
);
551 size_t __length
= strlen(__word
);
555 if ((_M_column
+ __length
< _M_max_length
)
556 || (__length
>= _M_max_length
&& _M_column
== 1))
558 // If this isn't the first line, indent
559 if (_M_column
== 1 && !_M_first_line
)
561 char __spacing
[_M_indent
+ 1];
562 for (int i
= 0; i
< _M_indent
; ++i
)
564 __spacing
[_M_indent
] = '\0';
565 fprintf(stderr
, "%s", __spacing
);
566 _M_column
+= _M_indent
;
569 fprintf(stderr
, "%s", __word
);
570 _M_column
+= __length
;
572 if (__word
[__length
- 1] == '\n')
574 _M_first_line
= false;
582 _M_print_word(__word
);
588 _M_print_string(const char* __string
) const
590 const char* __start
= __string
;
591 const char* __finish
= __start
;
592 const int __bufsize
= 128;
593 char __buf
[__bufsize
];
599 // [__start, __finish) denotes the next word
601 while (isalnum(*__finish
))
603 if (__start
== __finish
)
605 if (isspace(*__finish
))
608 const ptrdiff_t __len
= __finish
- __start
;
609 assert(__len
< __bufsize
);
610 memcpy(__buf
, __start
, __len
);
612 _M_print_word(__buf
);
615 // Skip extra whitespace
616 while (*__start
== ' ')
631 // Get the parameter number
632 assert(*__start
>= '1' && *__start
<= '9');
633 size_t __param
= *__start
- '0';
635 assert(__param
< _M_num_parameters
);
637 // '.' separates the parameter number from the field
638 // name, if there is one.
642 assert(*__start
== ';');
645 if (_M_parameters
[__param
]._M_kind
== _Parameter::__integer
)
647 _M_format_word(__buf
, __bufsize
, "%ld",
648 _M_parameters
[__param
]._M_variant
._M_integer
._M_value
);
649 _M_print_word(__buf
);
651 else if (_M_parameters
[__param
]._M_kind
== _Parameter::__string
)
652 _M_print_string(_M_parameters
[__param
]._M_variant
._M_string
._M_value
);
656 // Extract the field name we want
657 enum { __max_field_len
= 16 };
658 char __field
[__max_field_len
];
661 while (*__start
!= ';')
664 assert(__field_idx
< __max_field_len
-1);
665 __field
[__field_idx
++] = *__start
++;
668 __field
[__field_idx
] = 0;
670 _M_parameters
[__param
]._M_print_field(this, __field
);
677 _Error_formatter::_M_format_word(char*, int, const char*,
682 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
686 _Error_formatter::_M_format_word(char*, int, const char*,
691 _Error_formatter::_M_format_word(char*, int, const char*,
693 } // namespace __gnu_debug