OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / debug.cc
1 // Debugging mode support code -*- C++ -*-
2
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.
11
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.
16
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.
21
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.
30
31 #include <debug/debug.h>
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_iterator.h>
34 #include <algorithm>
35 #include <cstdlib>
36 #include <cassert>
37 #include <cstring>
38 #include <cstdio>
39 #include <cctype>
40 #include <bits/concurrence.h>
41
42 using namespace std;
43
44 namespace __gnu_internal
45 {
46   __glibcxx_mutex_define_initialized(iterator_base_mutex);
47 } // namespace __gnu_internal
48
49 namespace __gnu_debug
50 {
51   void
52   __fancy_abort(const char* __file, int __line, const char* __function,
53                 const char* __condition)
54   {
55     printf("%s:%d: %s: Assertion '%s' failed.\n", __file, __line,
56            __function, __condition);
57     abort();
58   }
59
60   const char* _S_debug_messages[] = 
61   {
62     "function requires a valid iterator range [%1.name;, %2.name;)",
63     "attempt to insert into container with a singular iterator",
64     "attempt to insert into container with an iterator"
65     " from a different container",
66     "attempt to erase from container with a %2.state; iterator",
67     "attempt to erase from container with an iterator"
68     " from a different container",
69     "attempt to subscript container with out-of-bounds index %2;,"
70     " but container only holds %3; elements",
71     "attempt to access an element in an empty container",
72     "elements in iterator range [%1.name;, %2.name;)"
73     " are not partitioned by the value %3;",
74     "elements in iterator range [%1.name;, %2.name;)"
75     " are not partitioned by the predicate %3; and value %4;",
76     "elements in iterator range [%1.name;, %2.name;) are not sorted",
77     "elements in iterator range [%1.name;, %2.name;)"
78     " are not sorted according to the predicate %3;",
79     "elements in iterator range [%1.name;, %2.name;) do not form a heap",
80     "elements in iterator range [%1.name;, %2.name;)"
81     " do not form a heap with respect to the predicate %3;",
82     "attempt to write through a singular bitset reference",
83     "attempt to read from a singular bitset reference",
84     "attempt to flip a singular bitset reference",
85     "attempt to splice a list into itself",
86     "attempt to splice lists with inequal allocators",
87     "attempt to splice elements referenced by a %1.state; iterator",
88     "attempt to splice an iterator from a different container",
89     "splice destination %1.name;"
90     " occurs within source range [%2.name;, %3.name;)",
91     "attempt to initialize an iterator that will immediately become singular",
92     "attempt to copy-construct an iterator from a singular iterator",
93     "attempt to construct a constant iterator"
94     " from a singular mutable iterator",
95     "attempt to copy from a singular iterator",
96     "attempt to dereference a %1.state; iterator",
97     "attempt to increment a %1.state; iterator",
98     "attempt to decrement a %1.state; iterator",
99     "attempt to subscript a %1.state; iterator %2; step from"
100     " its current position, which falls outside its dereferenceable range",
101     "attempt to advance a %1.state; iterator %2; steps,"
102     " which falls outside its valid range",
103     "attempt to retreat a %1.state; iterator %2; steps,"
104     " which falls outside its valid range",
105     "attempt to compare a %1.state; iterator to a %2.state; iterator",
106     "attempt to compare iterators from different sequences",
107     "attempt to order a %1.state; iterator to a %2.state; iterator",
108     "attempt to order iterators from different sequences",
109     "attempt to compute the difference between a %1.state;"
110     " iterator to a %2.state; iterator",
111     "attempt to compute the different between two iterators"
112     " from different sequences",
113     "attempt to dereference an end-of-stream istream_iterator",
114     "attempt to increment an end-of-stream istream_iterator",
115     "attempt to output via an ostream_iterator with no associated stream",
116     "attempt to dereference an end-of-stream istreambuf_iterator"
117     " (this is a GNU extension)",
118     "attempt to increment an end-of-stream istreambuf_iterator"
119   };
120
121   void 
122   _Safe_sequence_base::
123   _M_detach_all()
124   {
125     for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
126       {
127         _Safe_iterator_base* __old = __iter;
128         __iter = __iter->_M_next;
129         __old->_M_attach(0, false);
130       }
131     
132     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
133       {
134         _Safe_iterator_base* __old = __iter2;
135         __iter2 = __iter2->_M_next;
136         __old->_M_attach(0, true);
137       }
138   }
139
140   void 
141   _Safe_sequence_base::
142   _M_detach_singular()
143   {
144     for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
145       {
146         _Safe_iterator_base* __old = __iter;
147         __iter = __iter->_M_next;
148         if (__old->_M_singular())
149           __old->_M_attach(0, false);
150       }
151
152     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
153       {
154         _Safe_iterator_base* __old = __iter2;
155         __iter2 = __iter2->_M_next;
156         if (__old->_M_singular())
157           __old->_M_attach(0, true);
158       }
159   }
160   
161   void 
162   _Safe_sequence_base::
163   _M_revalidate_singular()
164   {
165     _Safe_iterator_base* __iter;
166     for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
167       {
168         __iter->_M_version = _M_version;
169         __iter = __iter->_M_next;
170       }
171     
172     for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
173       {
174         __iter->_M_version = _M_version;
175         __iter = __iter->_M_next;
176       }
177   }
178
179   void 
180   _Safe_sequence_base::
181   _M_swap(_Safe_sequence_base& __x)
182   {
183     swap(_M_iterators, __x._M_iterators);
184     swap(_M_const_iterators, __x._M_const_iterators);
185     swap(_M_version, __x._M_version);
186     _Safe_iterator_base* __iter;
187     for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
188       __iter->_M_sequence = this;
189     for (__iter = __x._M_iterators; __iter; __iter = __iter->_M_next)
190       __iter->_M_sequence = &__x;
191     for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
192       __iter->_M_sequence = this;
193     for (__iter = __x._M_const_iterators; __iter; __iter = __iter->_M_next)
194       __iter->_M_sequence = &__x;
195   }
196   
197   void 
198   _Safe_iterator_base::
199   _M_attach(_Safe_sequence_base* __seq, bool __constant)
200   {
201     _M_detach();
202     
203     // Attach to the new sequence (if there is one)
204     if (__seq)
205       {
206         __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
207         _M_sequence = __seq;
208         _M_version = _M_sequence->_M_version;
209         _M_prior = 0;
210         if (__constant)
211           {
212             _M_next = _M_sequence->_M_const_iterators;
213             if (_M_next)
214               _M_next->_M_prior = this;
215             _M_sequence->_M_const_iterators = this;
216           }
217         else
218           {
219             _M_next = _M_sequence->_M_iterators;
220             if (_M_next)
221               _M_next->_M_prior = this;
222             _M_sequence->_M_iterators = this;
223           }
224       }
225   }
226
227   void 
228   _Safe_iterator_base::
229   _M_detach()
230   {
231     __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
232     if (_M_sequence)
233       {
234         // Remove us from this sequence's list
235         if (_M_prior) 
236           _M_prior->_M_next = _M_next;
237         if (_M_next)  
238           _M_next->_M_prior = _M_prior;
239         
240         if (_M_sequence->_M_const_iterators == this)
241           _M_sequence->_M_const_iterators = _M_next;
242         if (_M_sequence->_M_iterators == this)
243           _M_sequence->_M_iterators = _M_next;
244       }
245
246     _M_sequence = 0;
247     _M_version = 0;
248     _M_prior = 0;
249     _M_next = 0;
250   }
251   
252   bool
253   _Safe_iterator_base::
254   _M_singular() const
255   { return !_M_sequence || _M_version != _M_sequence->_M_version; }
256     
257   bool
258   _Safe_iterator_base::
259   _M_can_compare(const _Safe_iterator_base& __x) const
260   {
261     return (!_M_singular() && !__x._M_singular() 
262             && _M_sequence == __x._M_sequence);
263   }
264
265   void
266   _Error_formatter::_Parameter::
267   _M_print_field(const _Error_formatter* __formatter, const char* __name) const
268   {
269     assert(this->_M_kind != _Parameter::__unused_param);
270     const int __bufsize = 64;
271     char __buf[__bufsize];
272     
273     if (_M_kind == __iterator)
274       {
275         if (strcmp(__name, "name") == 0)
276           {
277             assert(_M_variant._M_iterator._M_name);
278             __formatter->_M_print_word(_M_variant._M_iterator._M_name);
279           }
280         else if (strcmp(__name, "address") == 0)
281           {
282             __formatter->_M_format_word(__buf, __bufsize, "%p", 
283                                         _M_variant._M_iterator._M_address);
284             __formatter->_M_print_word(__buf);
285           }
286         else if (strcmp(__name, "type") == 0)
287           {
288             assert(_M_variant._M_iterator._M_type);
289             // TBD: demangle!
290             __formatter->_M_print_word(_M_variant._M_iterator._M_type->name());
291           }
292         else if (strcmp(__name, "constness") == 0)
293           {
294             static const char* __constness_names[__last_constness] =
295               {
296                 "<unknown>",
297                 "constant",
298                 "mutable"
299               };
300             __formatter->_M_print_word(__constness_names[_M_variant._M_iterator._M_constness]);
301           }
302         else if (strcmp(__name, "state") == 0)
303           {
304             static const char* __state_names[__last_state] = 
305               {
306                 "<unknown>",
307                 "singular",
308                 "dereferenceable (start-of-sequence)",
309                 "dereferenceable",
310                 "past-the-end"
311               };
312             __formatter->_M_print_word(__state_names[_M_variant._M_iterator._M_state]);
313           }
314         else if (strcmp(__name, "sequence") == 0)
315           {
316             assert(_M_variant._M_iterator._M_sequence);
317             __formatter->_M_format_word(__buf, __bufsize, "%p", 
318                                         _M_variant._M_iterator._M_sequence);
319             __formatter->_M_print_word(__buf);
320           }
321         else if (strcmp(__name, "seq_type") == 0)
322           {
323             // TBD: demangle!
324             assert(_M_variant._M_iterator._M_seq_type);
325             __formatter->_M_print_word(_M_variant._M_iterator._M_seq_type->name());
326           }
327         else
328           assert(false);
329       }
330     else if (_M_kind == __sequence)
331       {
332         if (strcmp(__name, "name") == 0)
333           {
334             assert(_M_variant._M_sequence._M_name);
335             __formatter->_M_print_word(_M_variant._M_sequence._M_name);
336           }
337         else if (strcmp(__name, "address") == 0)
338           {
339             assert(_M_variant._M_sequence._M_address);
340             __formatter->_M_format_word(__buf, __bufsize, "%p", 
341                                         _M_variant._M_sequence._M_address);
342             __formatter->_M_print_word(__buf);
343           }
344         else if (strcmp(__name, "type") == 0)
345           {
346             // TBD: demangle!
347             assert(_M_variant._M_sequence._M_type);
348             __formatter->_M_print_word(_M_variant._M_sequence._M_type->name());
349           }
350         else
351           assert(false);
352       }
353     else if (_M_kind == __integer)
354       {
355         if (strcmp(__name, "name") == 0)
356           {
357             assert(_M_variant._M_integer._M_name);
358             __formatter->_M_print_word(_M_variant._M_integer._M_name);
359           }
360         else
361         assert(false);
362       }
363     else if (_M_kind == __string)
364       {
365         if (strcmp(__name, "name") == 0)
366           {
367             assert(_M_variant._M_string._M_name);
368             __formatter->_M_print_word(_M_variant._M_string._M_name);
369           }
370         else
371           assert(false);
372       }
373     else
374       {
375         assert(false);
376       }
377   }
378   
379   void
380   _Error_formatter::_Parameter::
381   _M_print_description(const _Error_formatter* __formatter) const
382   {
383     const int __bufsize = 128;
384     char __buf[__bufsize];
385     
386     if (_M_kind == __iterator)
387       {
388         __formatter->_M_print_word("iterator ");
389         if (_M_variant._M_iterator._M_name)
390           {
391             __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ", 
392                                         _M_variant._M_iterator._M_name);
393             __formatter->_M_print_word(__buf);
394           }
395         
396         __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n", 
397                                     _M_variant._M_iterator._M_address);
398         __formatter->_M_print_word(__buf);
399         if (_M_variant._M_iterator._M_type)
400           {
401             __formatter->_M_print_word("type = ");
402             _M_print_field(__formatter, "type");
403             
404             if (_M_variant._M_iterator._M_constness != __unknown_constness)
405               {
406                 __formatter->_M_print_word(" (");
407                 _M_print_field(__formatter, "constness");
408                 __formatter->_M_print_word(" iterator)");
409               }
410             __formatter->_M_print_word(";\n");
411           }
412         
413         if (_M_variant._M_iterator._M_state != __unknown_state)
414           {
415             __formatter->_M_print_word("  state = ");
416             _M_print_field(__formatter, "state");
417             __formatter->_M_print_word(";\n");
418           }
419         
420         if (_M_variant._M_iterator._M_sequence)
421           {
422             __formatter->_M_print_word("  references sequence ");
423             if (_M_variant._M_iterator._M_seq_type)
424               {
425                 __formatter->_M_print_word("with type `");
426                 _M_print_field(__formatter, "seq_type");
427                 __formatter->_M_print_word("' ");
428               }
429             
430             __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n", 
431                                         _M_variant._M_sequence._M_address);
432             __formatter->_M_print_word(__buf);
433           }
434         __formatter->_M_print_word("}\n");
435       }
436     else if (_M_kind == __sequence)
437       {
438         __formatter->_M_print_word("sequence ");
439         if (_M_variant._M_sequence._M_name)
440           {
441             __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ", 
442                                         _M_variant._M_sequence._M_name);
443             __formatter->_M_print_word(__buf);
444           }
445         
446         __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n", 
447                                     _M_variant._M_sequence._M_address);
448         __formatter->_M_print_word(__buf);
449         
450         if (_M_variant._M_sequence._M_type)
451           {
452             __formatter->_M_print_word("  type = ");
453             _M_print_field(__formatter, "type");
454             __formatter->_M_print_word(";\n");
455           }       
456         __formatter->_M_print_word("}\n");
457       }
458   }
459
460   const _Error_formatter&
461   _Error_formatter::_M_message(_Debug_msg_id __id) const
462   { return this->_M_message(_S_debug_messages[__id]); }
463   
464   void
465   _Error_formatter::_M_error() const
466   {
467     const int __bufsize = 128;
468     char __buf[__bufsize];
469     
470     // Emit file & line number information
471     _M_column = 1;
472     _M_wordwrap = false;
473     if (_M_file)
474       {
475         _M_format_word(__buf, __bufsize, "%s:", _M_file);
476         _M_print_word(__buf);
477         _M_column += strlen(__buf);
478       }
479     
480     if (_M_line > 0)
481       {
482         _M_format_word(__buf, __bufsize, "%u:", _M_line);
483         _M_print_word(__buf);
484         _M_column += strlen(__buf);
485       }
486     
487     _M_wordwrap = true;
488     _M_print_word("error: ");
489     
490     // Print the error message
491     assert(_M_text);
492     _M_print_string(_M_text);
493     _M_print_word(".\n");
494     
495     // Emit descriptions of the objects involved in the operation
496     _M_wordwrap = false;
497     bool __has_noninteger_parameters = false;
498     for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
499       {
500         if (_M_parameters[__i]._M_kind == _Parameter::__iterator
501             || _M_parameters[__i]._M_kind == _Parameter::__sequence)
502           {
503             if (!__has_noninteger_parameters)
504               {
505                 _M_first_line = true;
506                 _M_print_word("\nObjects involved in the operation:\n");
507                 __has_noninteger_parameters = true;
508               }
509             _M_parameters[__i]._M_print_description(this);
510           }
511       }
512     
513     abort();
514   }
515
516   template<typename _Tp>
517     void
518     _Error_formatter::_M_format_word(char* __buf, 
519                                      int __n __attribute__ ((__unused__)), 
520                                      const char* __fmt, _Tp __s) const
521     {
522 #ifdef _GLIBCXX_USE_C99
523       std::snprintf(__buf, __n, __fmt, __s);
524 #else
525       std::sprintf(__buf, __fmt, __s);
526 #endif
527     }
528
529   
530   void 
531   _Error_formatter::_M_print_word(const char* __word) const
532   {
533     if (!_M_wordwrap) 
534       {
535         fprintf(stderr, "%s", __word);
536         return;
537       }
538     
539     size_t __length = strlen(__word);
540     if (__length == 0)
541       return;
542     
543     if ((_M_column + __length < _M_max_length)
544         || (__length >= _M_max_length && _M_column == 1)) 
545       {
546         // If this isn't the first line, indent
547         if (_M_column == 1 && !_M_first_line)
548           {
549             char __spacing[_M_indent + 1];
550             for (int i = 0; i < _M_indent; ++i)
551               __spacing[i] = ' ';
552             __spacing[_M_indent] = '\0';
553             fprintf(stderr, "%s", __spacing);
554             _M_column += _M_indent;
555           }
556         
557         fprintf(stderr, "%s", __word);
558         _M_column += __length;
559         
560         if (__word[__length - 1] == '\n') 
561           {
562             _M_first_line = false;
563             _M_column = 1;
564           }
565       }
566     else
567       {
568         _M_column = 1;
569         _M_print_word("\n");
570         _M_print_word(__word);
571       }
572   }
573   
574   void
575   _Error_formatter::
576   _M_print_string(const char* __string) const
577   {
578     const char* __start = __string;
579     const char* __end = __start;
580     const int __bufsize = 128;
581     char __buf[__bufsize];
582
583     while (*__start)
584       {
585         if (*__start != '%')
586           {
587             // [__start, __end) denotes the next word
588             __end = __start;
589             while (isalnum(*__end))
590               ++__end;
591             if (__start == __end)
592               ++__end;
593             if (isspace(*__end))
594               ++__end;
595             
596             const ptrdiff_t __len = __end - __start;
597             assert(__len < __bufsize);
598             memcpy(__buf, __start, __len);
599             __buf[__len] = '\0';
600             _M_print_word(__buf);
601             __start = __end;
602             
603             // Skip extra whitespace
604             while (*__start == ' ') 
605               ++__start;
606             
607             continue;
608           } 
609         
610         ++__start;
611         assert(*__start);
612         if (*__start == '%')
613           {
614             _M_print_word("%");
615             ++__start;
616             continue;
617           }
618         
619         // Get the parameter number
620         assert(*__start >= '1' && *__start <= '9');
621         size_t __param = *__start - '0';
622         --__param;
623         assert(__param < _M_num_parameters);
624       
625         // '.' separates the parameter number from the field
626         // name, if there is one.
627         ++__start;
628         if (*__start != '.')
629           {
630             assert(*__start == ';');
631             ++__start;
632             __buf[0] = '\0';
633             if (_M_parameters[__param]._M_kind == _Parameter::__integer)
634               {
635                 _M_format_word(__buf, __bufsize, "%ld", 
636                                _M_parameters[__param]._M_variant._M_integer._M_value);
637                 _M_print_word(__buf);
638               }
639             else if (_M_parameters[__param]._M_kind == _Parameter::__string)
640               _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
641             continue;
642           }
643         
644         // Extract the field name we want
645         enum { __max_field_len = 16 };
646         char __field[__max_field_len];
647         int __field_idx = 0;
648         ++__start;
649         while (*__start != ';')
650           {
651             assert(*__start);
652             assert(__field_idx < __max_field_len-1);
653             __field[__field_idx++] = *__start++;
654           }
655         ++__start;
656         __field[__field_idx] = 0;
657         
658         _M_parameters[__param]._M_print_field(this, __field);             
659       }
660   }
661
662   // Instantiations.
663   template
664     void
665     _Error_formatter::_M_format_word(char*, int, const char*, 
666                                      const void*) const;
667
668   template
669     void
670     _Error_formatter::_M_format_word(char*, int, const char*, long) const;
671
672   template
673     void
674     _Error_formatter::_M_format_word(char*, int, const char*, 
675                                      std::size_t) const;
676
677   template
678     void
679     _Error_formatter::_M_format_word(char*, int, const char*, 
680                                      const char*) const;
681 } // namespace __gnu_debug