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>
45 const char* _S_debug_messages
[] =
47 "function requires a valid iterator range [%1.name;, %2.name;)",
48 "attempt to insert into container with a singular iterator",
49 "attempt to insert into container with an iterator"
50 " from a different container",
51 "attempt to erase from container with a %2.state; iterator",
52 "attempt to erase from container with an iterator"
53 " from a different container",
54 "attempt to subscript container with out-of-bounds index %2;,"
55 " but container only holds %3; elements",
56 "attempt to access an element in an empty container",
57 "elements in iterator range [%1.name;, %2.name;)"
58 " are not partitioned by the value %3;",
59 "elements in iterator range [%1.name;, %2.name;)"
60 " are not partitioned by the predicate %3; and value %4;",
61 "elements in iterator range [%1.name;, %2.name;) are not sorted",
62 "elements in iterator range [%1.name;, %2.name;)"
63 " are not sorted according to the predicate %3;",
64 "elements in iterator range [%1.name;, %2.name;) do not form a heap",
65 "elements in iterator range [%1.name;, %2.name;)"
66 " do not form a heap with respect to the predicate %3;",
67 "attempt to write through a singular bitset reference",
68 "attempt to read from a singular bitset reference",
69 "attempt to flip a singular bitset reference",
70 "attempt to splice a list into itself",
71 "attempt to splice lists with inequal allocators",
72 "attempt to splice elements referenced by a %1.state; iterator",
73 "attempt to splice an iterator from a different container",
74 "splice destination %1.name;"
75 " occurs within source range [%2.name;, %3.name;)",
76 "attempt to initialize an iterator that will immediately become singular",
77 "attempt to copy-construct an iterator from a singular iterator",
78 "attempt to construct a constant iterator"
79 " from a singular mutable iterator",
80 "attempt to copy from a singular iterator",
81 "attempt to dereference a %1.state; iterator",
82 "attempt to increment a %1.state; iterator",
83 "attempt to decrement a %1.state; iterator",
84 "attempt to subscript a %1.state; iterator %2; step from"
85 " its current position, which falls outside its dereferenceable range",
86 "attempt to advance a %1.state; iterator %2; steps,"
87 " which falls outside its valid range",
88 "attempt to retreat a %1.state; iterator %2; steps,"
89 " which falls outside its valid range",
90 "attempt to compare a %1.state; iterator to a %2.state; iterator",
91 "attempt to compare iterators from different sequences",
92 "attempt to order a %1.state; iterator to a %2.state; iterator",
93 "attempt to order iterators from different sequences",
94 "attempt to compute the difference between a %1.state;"
95 " iterator to a %2.state; iterator",
96 "attempt to compute the different between two iterators"
97 " from different sequences",
98 "attempt to dereference an end-of-stream istream_iterator",
99 "attempt to increment an end-of-stream istream_iterator",
100 "attempt to output via an ostream_iterator with no associated stream",
101 "attempt to dereference an end-of-stream istreambuf_iterator"
102 " (this is a GNU extension)",
103 "attempt to increment an end-of-stream istreambuf_iterator"
107 _Safe_sequence_base::
110 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
; )
112 _Safe_iterator_base
* __old
= __iter
;
113 __iter
= __iter
->_M_next
;
114 __old
->_M_attach(0, false);
117 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
; )
119 _Safe_iterator_base
* __old
= __iter2
;
120 __iter2
= __iter2
->_M_next
;
121 __old
->_M_attach(0, true);
126 _Safe_sequence_base::
129 for (_Safe_iterator_base
* __iter
= _M_iterators
; __iter
; )
131 _Safe_iterator_base
* __old
= __iter
;
132 __iter
= __iter
->_M_next
;
133 if (__old
->_M_singular())
134 __old
->_M_attach(0, false);
137 for (_Safe_iterator_base
* __iter2
= _M_const_iterators
; __iter2
; )
139 _Safe_iterator_base
* __old
= __iter2
;
140 __iter2
= __iter2
->_M_next
;
141 if (__old
->_M_singular())
142 __old
->_M_attach(0, true);
147 _Safe_sequence_base::
148 _M_revalidate_singular()
150 _Safe_iterator_base
* __iter
;
151 for (__iter
= _M_iterators
; __iter
; __iter
= __iter
->_M_next
)
153 __iter
->_M_version
= _M_version
;
154 __iter
= __iter
->_M_next
;
157 for (__iter
= _M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
159 __iter
->_M_version
= _M_version
;
160 __iter
= __iter
->_M_next
;
165 _Safe_sequence_base::
166 _M_swap(_Safe_sequence_base
& __x
)
168 swap(_M_iterators
, __x
._M_iterators
);
169 swap(_M_const_iterators
, __x
._M_const_iterators
);
170 swap(_M_version
, __x
._M_version
);
171 _Safe_iterator_base
* __iter
;
172 for (__iter
= _M_iterators
; __iter
; __iter
= __iter
->_M_next
)
173 __iter
->_M_sequence
= this;
174 for (__iter
= __x
._M_iterators
; __iter
; __iter
= __iter
->_M_next
)
175 __iter
->_M_sequence
= &__x
;
176 for (__iter
= _M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
177 __iter
->_M_sequence
= this;
178 for (__iter
= __x
._M_const_iterators
; __iter
; __iter
= __iter
->_M_next
)
179 __iter
->_M_sequence
= &__x
;
183 _Safe_iterator_base::
184 _M_attach(_Safe_sequence_base
* __seq
, bool __constant
)
188 // Attach to the new sequence (if there is one)
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::
217 // Remove us from this sequence's list
219 _M_prior
->_M_next
= _M_next
;
221 _M_next
->_M_prior
= _M_prior
;
223 if (_M_sequence
->_M_const_iterators
== this)
224 _M_sequence
->_M_const_iterators
= _M_next
;
225 if (_M_sequence
->_M_iterators
== this)
226 _M_sequence
->_M_iterators
= _M_next
;
236 _Safe_iterator_base::
238 { return !_M_sequence
|| _M_version
!= _M_sequence
->_M_version
; }
241 _Safe_iterator_base::
242 _M_can_compare(const _Safe_iterator_base
& __x
) const
244 return (!_M_singular() && !__x
._M_singular()
245 && _M_sequence
== __x
._M_sequence
);
249 _Error_formatter::_Parameter::
250 _M_print_field(const _Error_formatter
* __formatter
, const char* __name
) const
252 assert(this->_M_kind
!= _Parameter::__unused_param
);
253 const int __bufsize
= 64;
254 char __buf
[__bufsize
];
256 if (_M_kind
== __iterator
)
258 if (strcmp(__name
, "name") == 0)
260 assert(_M_variant
._M_iterator
._M_name
);
261 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_name
);
263 else if (strcmp(__name
, "address") == 0)
265 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
266 _M_variant
._M_iterator
._M_address
);
267 __formatter
->_M_print_word(__buf
);
269 else if (strcmp(__name
, "type") == 0)
271 assert(_M_variant
._M_iterator
._M_type
);
273 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_type
->name());
275 else if (strcmp(__name
, "constness") == 0)
277 static const char* __constness_names
[__last_constness
] =
283 __formatter
->_M_print_word(__constness_names
[_M_variant
._M_iterator
._M_constness
]);
285 else if (strcmp(__name
, "state") == 0)
287 static const char* __state_names
[__last_state
] =
291 "dereferenceable (start-of-sequence)",
295 __formatter
->_M_print_word(__state_names
[_M_variant
._M_iterator
._M_state
]);
297 else if (strcmp(__name
, "sequence") == 0)
299 assert(_M_variant
._M_iterator
._M_sequence
);
300 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
301 _M_variant
._M_iterator
._M_sequence
);
302 __formatter
->_M_print_word(__buf
);
304 else if (strcmp(__name
, "seq_type") == 0)
307 assert(_M_variant
._M_iterator
._M_seq_type
);
308 __formatter
->_M_print_word(_M_variant
._M_iterator
._M_seq_type
->name());
313 else if (_M_kind
== __sequence
)
315 if (strcmp(__name
, "name") == 0)
317 assert(_M_variant
._M_sequence
._M_name
);
318 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_name
);
320 else if (strcmp(__name
, "address") == 0)
322 assert(_M_variant
._M_sequence
._M_address
);
323 __formatter
->_M_format_word(__buf
, __bufsize
, "%p",
324 _M_variant
._M_sequence
._M_address
);
325 __formatter
->_M_print_word(__buf
);
327 else if (strcmp(__name
, "type") == 0)
330 assert(_M_variant
._M_sequence
._M_type
);
331 __formatter
->_M_print_word(_M_variant
._M_sequence
._M_type
->name());
336 else if (_M_kind
== __integer
)
338 if (strcmp(__name
, "name") == 0)
340 assert(_M_variant
._M_integer
._M_name
);
341 __formatter
->_M_print_word(_M_variant
._M_integer
._M_name
);
346 else if (_M_kind
== __string
)
348 if (strcmp(__name
, "name") == 0)
350 assert(_M_variant
._M_string
._M_name
);
351 __formatter
->_M_print_word(_M_variant
._M_string
._M_name
);
363 _Error_formatter::_Parameter::
364 _M_print_description(const _Error_formatter
* __formatter
) const
366 const int __bufsize
= 128;
367 char __buf
[__bufsize
];
369 if (_M_kind
== __iterator
)
371 __formatter
->_M_print_word("iterator ");
372 if (_M_variant
._M_iterator
._M_name
)
374 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
375 _M_variant
._M_iterator
._M_name
);
376 __formatter
->_M_print_word(__buf
);
379 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
380 _M_variant
._M_iterator
._M_address
);
381 __formatter
->_M_print_word(__buf
);
382 if (_M_variant
._M_iterator
._M_type
)
384 __formatter
->_M_print_word("type = ");
385 _M_print_field(__formatter
, "type");
387 if (_M_variant
._M_iterator
._M_constness
!= __unknown_constness
)
389 __formatter
->_M_print_word(" (");
390 _M_print_field(__formatter
, "constness");
391 __formatter
->_M_print_word(" iterator)");
393 __formatter
->_M_print_word(";\n");
396 if (_M_variant
._M_iterator
._M_state
!= __unknown_state
)
398 __formatter
->_M_print_word(" state = ");
399 _M_print_field(__formatter
, "state");
400 __formatter
->_M_print_word(";\n");
403 if (_M_variant
._M_iterator
._M_sequence
)
405 __formatter
->_M_print_word(" references sequence ");
406 if (_M_variant
._M_iterator
._M_seq_type
)
408 __formatter
->_M_print_word("with type `");
409 _M_print_field(__formatter
, "seq_type");
410 __formatter
->_M_print_word("' ");
413 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p\n",
414 _M_variant
._M_sequence
._M_address
);
415 __formatter
->_M_print_word(__buf
);
417 __formatter
->_M_print_word("}\n");
419 else if (_M_kind
== __sequence
)
421 __formatter
->_M_print_word("sequence ");
422 if (_M_variant
._M_sequence
._M_name
)
424 __formatter
->_M_format_word(__buf
, __bufsize
, "\"%s\" ",
425 _M_variant
._M_sequence
._M_name
);
426 __formatter
->_M_print_word(__buf
);
429 __formatter
->_M_format_word(__buf
, __bufsize
, "@ 0x%p {\n",
430 _M_variant
._M_sequence
._M_address
);
431 __formatter
->_M_print_word(__buf
);
433 if (_M_variant
._M_sequence
._M_type
)
435 __formatter
->_M_print_word(" type = ");
436 _M_print_field(__formatter
, "type");
437 __formatter
->_M_print_word(";\n");
439 __formatter
->_M_print_word("}\n");
443 const _Error_formatter
&
444 _Error_formatter::_M_message(_Debug_msg_id __id
) const
445 { return this->_M_message(_S_debug_messages
[__id
]); }
448 _Error_formatter::_M_error() const
450 const int __bufsize
= 128;
451 char __buf
[__bufsize
];
453 // Emit file & line number information
458 _M_format_word(__buf
, __bufsize
, "%s:", _M_file
);
459 _M_print_word(__buf
);
460 _M_column
+= strlen(__buf
);
465 _M_format_word(__buf
, __bufsize
, "%u:", _M_line
);
466 _M_print_word(__buf
);
467 _M_column
+= strlen(__buf
);
471 _M_print_word("error: ");
473 // Print the error message
475 _M_print_string(_M_text
);
476 _M_print_word(".\n");
478 // Emit descriptions of the objects involved in the operation
480 bool __has_noninteger_parameters
= false;
481 for (unsigned int __i
= 0; __i
< _M_num_parameters
; ++__i
)
483 if (_M_parameters
[__i
]._M_kind
== _Parameter::__iterator
484 || _M_parameters
[__i
]._M_kind
== _Parameter::__sequence
)
486 if (!__has_noninteger_parameters
)
488 _M_first_line
= true;
489 _M_print_word("\nObjects involved in the operation:\n");
490 __has_noninteger_parameters
= true;
492 _M_parameters
[__i
]._M_print_description(this);
499 template<typename _Tp
>
501 _Error_formatter::_M_format_word(char* __buf
,
502 int __n
__attribute__((__unused__
)),
503 const char* __fmt
, _Tp __s
) const
505 #ifdef _GLIBCXX_USE_C99
506 std::snprintf(__buf
, __n
, __fmt
, __s
);
508 std::sprintf(__buf
, __fmt
, __s
);
514 _Error_formatter::_M_print_word(const char* __word
) const
518 fprintf(stderr
, "%s", __word
);
522 size_t __length
= strlen(__word
);
526 if ((_M_column
+ __length
< _M_max_length
)
527 || (__length
>= _M_max_length
&& _M_column
== 1))
529 // If this isn't the first line, indent
530 if (_M_column
== 1 && !_M_first_line
)
532 char __spacing
[_M_indent
+ 1];
533 for (int i
= 0; i
< _M_indent
; ++i
)
535 __spacing
[_M_indent
] = '\0';
536 fprintf(stderr
, "%s", __spacing
);
537 _M_column
+= _M_indent
;
540 fprintf(stderr
, "%s", __word
);
541 _M_column
+= __length
;
543 if (__word
[__length
- 1] == '\n')
545 _M_first_line
= false;
553 _M_print_word(__word
);
559 _M_print_string(const char* __string
) const
561 const char* __start
= __string
;
562 const char* __end
= __start
;
563 const int __bufsize
= 128;
564 char __buf
[__bufsize
];
570 // [__start, __end) denotes the next word
572 while (isalnum(*__end
)) ++__end
;
573 if (__start
== __end
) ++__end
;
574 if (isspace(*__end
)) ++__end
;
576 assert(__end
- __start
+ 1< __bufsize
);
577 _M_format_word(__buf
, __end
- __start
+ 1, "%s", __start
);
578 _M_print_word(__buf
);
581 // Skip extra whitespace
582 while (*__start
== ' ')
597 // Get the parameter number
598 assert(*__start
>= '1' && *__start
<= '9');
599 size_t __param
= *__start
- '0';
601 assert(__param
< _M_num_parameters
);
603 // '.' separates the parameter number from the field
604 // name, if there is one.
608 assert(*__start
== ';');
611 if (_M_parameters
[__param
]._M_kind
== _Parameter::__integer
)
613 _M_format_word(__buf
, __bufsize
, "%ld",
614 _M_parameters
[__param
]._M_variant
._M_integer
._M_value
);
615 _M_print_word(__buf
);
617 else if (_M_parameters
[__param
]._M_kind
== _Parameter::__string
)
618 _M_print_string(_M_parameters
[__param
]._M_variant
._M_string
._M_value
);
622 // Extract the field name we want
623 enum { __max_field_len
= 16 };
624 char __field
[__max_field_len
];
627 while (*__start
!= ';')
630 assert(__field_idx
< __max_field_len
-1);
631 __field
[__field_idx
++] = *__start
++;
634 __field
[__field_idx
] = 0;
636 _M_parameters
[__param
]._M_print_field(this, __field
);
643 _Error_formatter::_M_format_word(char* __buf
, int __n
, const char* __fmt
,
644 const void* __s
) const;
648 _Error_formatter::_M_format_word(char* __buf
, int __n
, const char* __fmt
,
653 _Error_formatter::_M_format_word(char* __buf
, int __n
, const char* __fmt
,
654 std::size_t __s
) const;
658 _Error_formatter::_M_format_word(char* __buf
, int __n
, const char* __fmt
,
659 const char* __s
) const;
660 } // namespace __gnu_debug