1 // Debugging mode support code -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006
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 <ext/concurrence.h>
44 __gnu_cxx::__mutex iterator_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 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);
130 _Safe_sequence_base::
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);
151 _Safe_sequence_base::
152 _M_revalidate_singular()
154 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
;
155 __iter
= __iter
->_M_next
)
156 __iter
->_M_version
= _M_version
;
158 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
;
159 __iter2
= __iter2
->_M_next
)
160 __iter2
->_M_version
= _M_version
;
164 _Safe_sequence_base::
165 _M_swap(_Safe_sequence_base
& __x
)
167 swap(_M_iterators
, __x
._M_iterators
);
168 swap(_M_const_iterators
, __x
._M_const_iterators
);
169 swap(_M_version
, __x
._M_version
);
170 _Safe_iterator_base
* __iter
;
171 for (__iter
= _M_iterators
; __iter
; __iter
= __iter
->_M_next
)
172 __iter
->_M_sequence
= this;
173 for (__iter
= __x
._M_iterators
; __iter
; __iter
= __iter
->_M_next
)
174 __iter
->_M_sequence
= &__x
;
175 for (__iter
= _M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
176 __iter
->_M_sequence
= this;
177 for (__iter
= __x
._M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
178 __iter
->_M_sequence
= &__x
;
182 _Safe_iterator_base::
183 _M_attach(_Safe_sequence_base
* __seq
, bool __constant
)
187 // Attach to the new sequence (if there is one)
190 __gnu_cxx::__scoped_lock
sentry(iterator_base_mutex
);
192 _M_version
= _M_sequence
->_M_version
;
196 _M_next
= _M_sequence
->_M_const_iterators
;
198 _M_next
->_M_prior
= this;
199 _M_sequence
->_M_const_iterators
= this;
203 _M_next
= _M_sequence
->_M_iterators
;
205 _M_next
->_M_prior
= this;
206 _M_sequence
->_M_iterators
= this;
212 _Safe_iterator_base::
215 __gnu_cxx::__scoped_lock
sentry(iterator_base_mutex
);
218 // Remove us from this sequence's list
220 _M_prior
->_M_next
= _M_next
;
222 _M_next
->_M_prior
= _M_prior
;
224 if (_M_sequence
->_M_const_iterators
== this)
225 _M_sequence
->_M_const_iterators
= _M_next
;
226 if (_M_sequence
->_M_iterators
== this)
227 _M_sequence
->_M_iterators
= _M_next
;
237 _Safe_iterator_base::
239 { return !_M_sequence
|| _M_version
!= _M_sequence
->_M_version
; }
242 _Safe_iterator_base::
243 _M_can_compare(const _Safe_iterator_base
& __x
) const
245 return (!_M_singular()
246 && !__x
._M_singular() && _M_sequence
== __x
._M_sequence
);
250 _Error_formatter::_Parameter::
251 _M_print_field(const _Error_formatter
* __formatter
, const char* __name
) const
253 assert(this->_M_kind
!= _Parameter::__unused_param
);
254 const int __bufsize
= 64;
255 char __buf
[__bufsize
];
257 if (_M_kind
== __iterator
)
259 if (strcmp(__name
, "name") == 0)
261 assert(_M_variant
._M_iterator
._M_name
);
262 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_name
);
264 else if (strcmp(__name
, "address") == 0)
266 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
267 _M_variant
._M_iterator
._M_address
);
268 __formatter
->_M_print_word(__buf
);
270 else if (strcmp(__name
, "type") == 0)
272 assert(_M_variant
._M_iterator
._M_type
);
274 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_type
->name());
276 else if (strcmp(__name
, "constness") == 0)
278 static const char* __constness_names
[__last_constness
] =
284 __formatter
->_M_print_word(__constness_names
[_M_variant
._M_iterator
._M_constness
]);
286 else if (strcmp(__name
, "state") == 0)
288 static const char* __state_names
[__last_state
] =
292 "dereferenceable (start-of-sequence)",
296 __formatter
->_M_print_word(__state_names
[_M_variant
._M_iterator
._M_state
]);
298 else if (strcmp(__name
, "sequence") == 0)
300 assert(_M_variant
._M_iterator
._M_sequence
);
301 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
302 _M_variant
._M_iterator
._M_sequence
);
303 __formatter
->_M_print_word(__buf
);
305 else if (strcmp(__name
, "seq_type") == 0)
308 assert(_M_variant
._M_iterator
._M_seq_type
);
309 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_seq_type
->name());
314 else if (_M_kind
== __sequence
)
316 if (strcmp(__name
, "name") == 0)
318 assert(_M_variant
._M_sequence
._M_name
);
319 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_name
);
321 else if (strcmp(__name
, "address") == 0)
323 assert(_M_variant
._M_sequence
._M_address
);
324 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
325 _M_variant
._M_sequence
._M_address
);
326 __formatter
->_M_print_word(__buf
);
328 else if (strcmp(__name
, "type") == 0)
331 assert(_M_variant
._M_sequence
._M_type
);
332 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_type
->name());
337 else if (_M_kind
== __integer
)
339 if (strcmp(__name
, "name") == 0)
341 assert(_M_variant
._M_integer
._M_name
);
342 __formatter
->_M_print_word(_M_variant
._M_integer
._M_name
);
347 else if (_M_kind
== __string
)
349 if (strcmp(__name
, "name") == 0)
351 assert(_M_variant
._M_string
._M_name
);
352 __formatter
->_M_print_word(_M_variant
._M_string
._M_name
);
364 _Error_formatter::_Parameter::
365 _M_print_description(const _Error_formatter
* __formatter
) const
367 const int __bufsize
= 128;
368 char __buf
[__bufsize
];
370 if (_M_kind
== __iterator
)
372 __formatter
->_M_print_word("iterator ");
373 if (_M_variant
._M_iterator
._M_name
)
375 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
376 _M_variant
._M_iterator
._M_name
);
377 __formatter
->_M_print_word(__buf
);
380 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
381 _M_variant
._M_iterator
._M_address
);
382 __formatter
->_M_print_word(__buf
);
383 if (_M_variant
._M_iterator
._M_type
)
385 __formatter
->_M_print_word("type = ");
386 _M_print_field(__formatter
, "type");
388 if (_M_variant
._M_iterator
._M_constness
!= __unknown_constness
)
390 __formatter
->_M_print_word(" (");
391 _M_print_field(__formatter
, "constness");
392 __formatter
->_M_print_word(" iterator)");
394 __formatter
->_M_print_word(";\n");
397 if (_M_variant
._M_iterator
._M_state
!= __unknown_state
)
399 __formatter
->_M_print_word(" state = ");
400 _M_print_field(__formatter
, "state");
401 __formatter
->_M_print_word(";\n");
404 if (_M_variant
._M_iterator
._M_sequence
)
406 __formatter
->_M_print_word(" references sequence ");
407 if (_M_variant
._M_iterator
._M_seq_type
)
409 __formatter
->_M_print_word("with type `");
410 _M_print_field(__formatter
, "seq_type");
411 __formatter
->_M_print_word("' ");
414 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p\n",
415 _M_variant
._M_sequence
._M_address
);
416 __formatter
->_M_print_word(__buf
);
418 __formatter
->_M_print_word("}\n");
420 else if (_M_kind
== __sequence
)
422 __formatter
->_M_print_word("sequence ");
423 if (_M_variant
._M_sequence
._M_name
)
425 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
426 _M_variant
._M_sequence
._M_name
);
427 __formatter
->_M_print_word(__buf
);
430 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
431 _M_variant
._M_sequence
._M_address
);
432 __formatter
->_M_print_word(__buf
);
434 if (_M_variant
._M_sequence
._M_type
)
436 __formatter
->_M_print_word(" type = ");
437 _M_print_field(__formatter
, "type");
438 __formatter
->_M_print_word(";\n");
440 __formatter
->_M_print_word("}\n");
444 const _Error_formatter
&
445 _Error_formatter::_M_message(_Debug_msg_id __id
) const
446 { return this->_M_message(_S_debug_messages
[__id
]); }
449 _Error_formatter::_M_error() const
451 const int __bufsize
= 128;
452 char __buf
[__bufsize
];
454 // Emit file & line number information
459 _M_format_word(__buf
, __bufsize
, "%s:", _M_file
);
460 _M_print_word(__buf
);
461 _M_column
+= strlen(__buf
);
466 _M_format_word(__buf
, __bufsize
, "%u:", _M_line
);
467 _M_print_word(__buf
);
468 _M_column
+= strlen(__buf
);
472 _M_print_word("error: ");
474 // Print the error message
476 _M_print_string(_M_text
);
477 _M_print_word(".\n");
479 // Emit descriptions of the objects involved in the operation
481 bool __has_noninteger_parameters
= false;
482 for (unsigned int __i
= 0; __i
< _M_num_parameters
; ++__i
)
484 if (_M_parameters
[__i
]._M_kind
== _Parameter::__iterator
485 || _M_parameters
[__i
]._M_kind
== _Parameter::__sequence
)
487 if (!__has_noninteger_parameters
)
489 _M_first_line
= true;
490 _M_print_word("\nObjects involved in the operation:\n");
491 __has_noninteger_parameters
= true;
493 _M_parameters
[__i
]._M_print_description(this);
500 template<typename _Tp
>
502 _Error_formatter::_M_format_word(char* __buf
,
503 int __n
__attribute__ ((__unused__
)),
504 const char* __fmt
, _Tp __s
) const
506 #ifdef _GLIBCXX_USE_C99
507 std::snprintf(__buf
, __n
, __fmt
, __s
);
509 std::sprintf(__buf
, __fmt
, __s
);
515 _Error_formatter::_M_print_word(const char* __word
) const
519 fprintf(stderr
, "%s", __word
);
523 size_t __length
= strlen(__word
);
527 if ((_M_column
+ __length
< _M_max_length
)
528 || (__length
>= _M_max_length
&& _M_column
== 1))
530 // If this isn't the first line, indent
531 if (_M_column
== 1 && !_M_first_line
)
533 char __spacing
[_M_indent
+ 1];
534 for (int i
= 0; i
< _M_indent
; ++i
)
536 __spacing
[_M_indent
] = '\0';
537 fprintf(stderr
, "%s", __spacing
);
538 _M_column
+= _M_indent
;
541 fprintf(stderr
, "%s", __word
);
542 _M_column
+= __length
;
544 if (__word
[__length
- 1] == '\n')
546 _M_first_line
= false;
554 _M_print_word(__word
);
560 _M_print_string(const char* __string
) const
562 const char* __start
= __string
;
563 const char* __end
= __start
;
564 const int __bufsize
= 128;
565 char __buf
[__bufsize
];
571 // [__start, __end) denotes the next word
573 while (isalnum(*__end
))
575 if (__start
== __end
)
580 const ptrdiff_t __len
= __end
- __start
;
581 assert(__len
< __bufsize
);
582 memcpy(__buf
, __start
, __len
);
584 _M_print_word(__buf
);
587 // Skip extra whitespace
588 while (*__start
== ' ')
603 // Get the parameter number
604 assert(*__start
>= '1' && *__start
<= '9');
605 size_t __param
= *__start
- '0';
607 assert(__param
< _M_num_parameters
);
609 // '.' separates the parameter number from the field
610 // name, if there is one.
614 assert(*__start
== ';');
617 if (_M_parameters
[__param
]._M_kind
== _Parameter::__integer
)
619 _M_format_word(__buf
, __bufsize
, "%ld",
620 _M_parameters
[__param
]._M_variant
._M_integer
._M_value
);
621 _M_print_word(__buf
);
623 else if (_M_parameters
[__param
]._M_kind
== _Parameter::__string
)
624 _M_print_string(_M_parameters
[__param
]._M_variant
._M_string
._M_value
);
628 // Extract the field name we want
629 enum { __max_field_len
= 16 };
630 char __field
[__max_field_len
];
633 while (*__start
!= ';')
636 assert(__field_idx
< __max_field_len
-1);
637 __field
[__field_idx
++] = *__start
++;
640 __field
[__field_idx
] = 0;
642 _M_parameters
[__param
]._M_print_field(this, __field
);
649 _Error_formatter::_M_format_word(char*, int, const char*,
654 _Error_formatter::_M_format_word(char*, int, const char*, long) const;
658 _Error_formatter::_M_format_word(char*, int, const char*,
663 _Error_formatter::_M_format_word(char*, int, const char*,
665 } // namespace __gnu_debug