OSDN Git Service

2007-04-06 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / debug.cc
1 // Debugging mode support code -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007
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 <cassert>
36 #include <cstring>
37 #include <cctype>
38 #include <cstdio>
39
40 using namespace std;
41
42 namespace
43 {
44   __gnu_cxx::__mutex safe_base_mutex;
45 } // anonymous namespace
46
47 namespace __gnu_debug
48 {
49   const char* _S_debug_messages[] = 
50   {
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"
108   };
109
110   void
111   _Safe_sequence_base::
112   _M_detach_all()
113   {
114     __gnu_cxx::__scoped_lock sentry(safe_base_mutex);
115     for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
116       {
117         _Safe_iterator_base* __old = __iter;
118         __iter = __iter->_M_next;
119         __old->_M_detach_single();
120       }
121     
122     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
123       {
124         _Safe_iterator_base* __old = __iter2;
125         __iter2 = __iter2->_M_next;
126         __old->_M_detach_single();
127       }
128   }
129
130   void
131   _Safe_sequence_base::
132   _M_detach_singular()
133   {
134     __gnu_cxx::__scoped_lock sentry(safe_base_mutex);
135     for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
136       {
137         _Safe_iterator_base* __old = __iter;
138         __iter = __iter->_M_next;
139         if (__old->_M_singular())
140           __old->_M_detach_single();
141       }
142
143     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
144       {
145         _Safe_iterator_base* __old = __iter2;
146         __iter2 = __iter2->_M_next;
147         if (__old->_M_singular())
148           __old->_M_detach_single();
149       }
150   }
151
152   void
153   _Safe_sequence_base::
154   _M_revalidate_singular()
155   {
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;
160
161     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;
162          __iter2 = __iter2->_M_next)
163       __iter2->_M_version = _M_version;
164   }
165
166   void
167   _Safe_sequence_base::
168   _M_swap(_Safe_sequence_base& __x)
169   {
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;
183   }
184
185   __gnu_cxx::__mutex&
186   _Safe_sequence_base::
187   _M_get_mutex()
188   { return safe_base_mutex; }
189
190   void
191   _Safe_iterator_base::
192   _M_attach(_Safe_sequence_base* __seq, bool __constant)
193   {
194     __gnu_cxx::__scoped_lock sentry(safe_base_mutex);
195     _M_attach_single(__seq, __constant);
196   }
197   
198   void
199   _Safe_iterator_base::
200   _M_attach_single(_Safe_sequence_base* __seq, bool __constant)
201   {
202     _M_detach_single();
203     
204     // Attach to the new sequence (if there is one)
205     if (__seq)
206       {
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::__scoped_lock sentry(safe_base_mutex);
232     _M_detach_single();
233   }
234
235   void
236   _Safe_iterator_base::
237   _M_detach_single()
238   {
239     if (_M_sequence)
240       {
241         // Remove us from this sequence's list
242         if (_M_prior) 
243           _M_prior->_M_next = _M_next;
244         if (_M_next)  
245           _M_next->_M_prior = _M_prior;
246         
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;
251       }
252
253     _M_sequence = 0;
254     _M_version = 0;
255     _M_prior = 0;
256     _M_next = 0;
257   }
258
259   bool
260   _Safe_iterator_base::
261   _M_singular() const
262   { return !_M_sequence || _M_version != _M_sequence->_M_version; }
263     
264   bool
265   _Safe_iterator_base::
266   _M_can_compare(const _Safe_iterator_base& __x) const
267   {
268     return (!_M_singular() 
269             && !__x._M_singular() && _M_sequence == __x._M_sequence);
270   }
271
272   __gnu_cxx::__mutex&
273   _Safe_iterator_base::
274   _M_get_mutex()
275   { return safe_base_mutex; }
276
277   void
278   _Error_formatter::_Parameter::
279   _M_print_field(const _Error_formatter* __formatter, const char* __name) const
280   {
281     assert(this->_M_kind != _Parameter::__unused_param);
282     const int __bufsize = 64;
283     char __buf[__bufsize];
284     
285     if (_M_kind == __iterator)
286       {
287         if (strcmp(__name, "name") == 0)
288           {
289             assert(_M_variant._M_iterator._M_name);
290             __formatter->_M_print_word(_M_variant._M_iterator._M_name);
291           }
292         else if (strcmp(__name, "address") == 0)
293           {
294             __formatter->_M_format_word(__buf, __bufsize, "%p", 
295                                         _M_variant._M_iterator._M_address);
296             __formatter->_M_print_word(__buf);
297           }
298         else if (strcmp(__name, "type") == 0)
299           {
300             assert(_M_variant._M_iterator._M_type);
301             // TBD: demangle!
302             __formatter->_M_print_word(_M_variant._M_iterator._M_type->name());
303           }
304         else if (strcmp(__name, "constness") == 0)
305           {
306             static const char* __constness_names[__last_constness] =
307               {
308                 "<unknown>",
309                 "constant",
310                 "mutable"
311               };
312             __formatter->_M_print_word(__constness_names[_M_variant._M_iterator._M_constness]);
313           }
314         else if (strcmp(__name, "state") == 0)
315           {
316             static const char* __state_names[__last_state] = 
317               {
318                 "<unknown>",
319                 "singular",
320                 "dereferenceable (start-of-sequence)",
321                 "dereferenceable",
322                 "past-the-end"
323               };
324             __formatter->_M_print_word(__state_names[_M_variant._M_iterator._M_state]);
325           }
326         else if (strcmp(__name, "sequence") == 0)
327           {
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);
332           }
333         else if (strcmp(__name, "seq_type") == 0)
334           {
335             // TBD: demangle!
336             assert(_M_variant._M_iterator._M_seq_type);
337             __formatter->_M_print_word(_M_variant._M_iterator._M_seq_type->name());
338           }
339         else
340           assert(false);
341       }
342     else if (_M_kind == __sequence)
343       {
344         if (strcmp(__name, "name") == 0)
345           {
346             assert(_M_variant._M_sequence._M_name);
347             __formatter->_M_print_word(_M_variant._M_sequence._M_name);
348           }
349         else if (strcmp(__name, "address") == 0)
350           {
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);
355           }
356         else if (strcmp(__name, "type") == 0)
357           {
358             // TBD: demangle!
359             assert(_M_variant._M_sequence._M_type);
360             __formatter->_M_print_word(_M_variant._M_sequence._M_type->name());
361           }
362         else
363           assert(false);
364       }
365     else if (_M_kind == __integer)
366       {
367         if (strcmp(__name, "name") == 0)
368           {
369             assert(_M_variant._M_integer._M_name);
370             __formatter->_M_print_word(_M_variant._M_integer._M_name);
371           }
372         else
373         assert(false);
374       }
375     else if (_M_kind == __string)
376       {
377         if (strcmp(__name, "name") == 0)
378           {
379             assert(_M_variant._M_string._M_name);
380             __formatter->_M_print_word(_M_variant._M_string._M_name);
381           }
382         else
383           assert(false);
384       }
385     else
386       {
387         assert(false);
388       }
389   }
390   
391   void
392   _Error_formatter::_Parameter::
393   _M_print_description(const _Error_formatter* __formatter) const
394   {
395     const int __bufsize = 128;
396     char __buf[__bufsize];
397     
398     if (_M_kind == __iterator)
399       {
400         __formatter->_M_print_word("iterator ");
401         if (_M_variant._M_iterator._M_name)
402           {
403             __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ", 
404                                         _M_variant._M_iterator._M_name);
405             __formatter->_M_print_word(__buf);
406           }
407         
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)
412           {
413             __formatter->_M_print_word("type = ");
414             _M_print_field(__formatter, "type");
415             
416             if (_M_variant._M_iterator._M_constness != __unknown_constness)
417               {
418                 __formatter->_M_print_word(" (");
419                 _M_print_field(__formatter, "constness");
420                 __formatter->_M_print_word(" iterator)");
421               }
422             __formatter->_M_print_word(";\n");
423           }
424         
425         if (_M_variant._M_iterator._M_state != __unknown_state)
426           {
427             __formatter->_M_print_word("  state = ");
428             _M_print_field(__formatter, "state");
429             __formatter->_M_print_word(";\n");
430           }
431         
432         if (_M_variant._M_iterator._M_sequence)
433           {
434             __formatter->_M_print_word("  references sequence ");
435             if (_M_variant._M_iterator._M_seq_type)
436               {
437                 __formatter->_M_print_word("with type `");
438                 _M_print_field(__formatter, "seq_type");
439                 __formatter->_M_print_word("' ");
440               }
441             
442             __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n", 
443                                         _M_variant._M_sequence._M_address);
444             __formatter->_M_print_word(__buf);
445           }
446         __formatter->_M_print_word("}\n");
447       }
448     else if (_M_kind == __sequence)
449       {
450         __formatter->_M_print_word("sequence ");
451         if (_M_variant._M_sequence._M_name)
452           {
453             __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ", 
454                                         _M_variant._M_sequence._M_name);
455             __formatter->_M_print_word(__buf);
456           }
457         
458         __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n", 
459                                     _M_variant._M_sequence._M_address);
460         __formatter->_M_print_word(__buf);
461         
462         if (_M_variant._M_sequence._M_type)
463           {
464             __formatter->_M_print_word("  type = ");
465             _M_print_field(__formatter, "type");
466             __formatter->_M_print_word(";\n");
467           }       
468         __formatter->_M_print_word("}\n");
469       }
470   }
471
472   const _Error_formatter&
473   _Error_formatter::_M_message(_Debug_msg_id __id) const
474   { return this->_M_message(_S_debug_messages[__id]); }
475   
476   void
477   _Error_formatter::_M_error() const
478   {
479     const int __bufsize = 128;
480     char __buf[__bufsize];
481     
482     // Emit file & line number information
483     _M_column = 1;
484     _M_wordwrap = false;
485     if (_M_file)
486       {
487         _M_format_word(__buf, __bufsize, "%s:", _M_file);
488         _M_print_word(__buf);
489         _M_column += strlen(__buf);
490       }
491     
492     if (_M_line > 0)
493       {
494         _M_format_word(__buf, __bufsize, "%u:", _M_line);
495         _M_print_word(__buf);
496         _M_column += strlen(__buf);
497       }
498     
499     _M_wordwrap = true;
500     _M_print_word("error: ");
501     
502     // Print the error message
503     assert(_M_text);
504     _M_print_string(_M_text);
505     _M_print_word(".\n");
506     
507     // Emit descriptions of the objects involved in the operation
508     _M_wordwrap = false;
509     bool __has_noninteger_parameters = false;
510     for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
511       {
512         if (_M_parameters[__i]._M_kind == _Parameter::__iterator
513             || _M_parameters[__i]._M_kind == _Parameter::__sequence)
514           {
515             if (!__has_noninteger_parameters)
516               {
517                 _M_first_line = true;
518                 _M_print_word("\nObjects involved in the operation:\n");
519                 __has_noninteger_parameters = true;
520               }
521             _M_parameters[__i]._M_print_description(this);
522           }
523       }
524     
525     abort();
526   }
527
528   template<typename _Tp>
529     void
530     _Error_formatter::_M_format_word(char* __buf, 
531                                      int __n __attribute__ ((__unused__)), 
532                                      const char* __fmt, _Tp __s) const
533     {
534 #ifdef _GLIBCXX_USE_C99
535       std::snprintf(__buf, __n, __fmt, __s);
536 #else
537       std::sprintf(__buf, __fmt, __s);
538 #endif
539     }
540
541   
542   void 
543   _Error_formatter::_M_print_word(const char* __word) const
544   {
545     if (!_M_wordwrap) 
546       {
547         fprintf(stderr, "%s", __word);
548         return;
549       }
550     
551     size_t __length = strlen(__word);
552     if (__length == 0)
553       return;
554     
555     if ((_M_column + __length < _M_max_length)
556         || (__length >= _M_max_length && _M_column == 1)) 
557       {
558         // If this isn't the first line, indent
559         if (_M_column == 1 && !_M_first_line)
560           {
561             char __spacing[_M_indent + 1];
562             for (int i = 0; i < _M_indent; ++i)
563               __spacing[i] = ' ';
564             __spacing[_M_indent] = '\0';
565             fprintf(stderr, "%s", __spacing);
566             _M_column += _M_indent;
567           }
568         
569         fprintf(stderr, "%s", __word);
570         _M_column += __length;
571         
572         if (__word[__length - 1] == '\n') 
573           {
574             _M_first_line = false;
575             _M_column = 1;
576           }
577       }
578     else
579       {
580         _M_column = 1;
581         _M_print_word("\n");
582         _M_print_word(__word);
583       }
584   }
585   
586   void
587   _Error_formatter::
588   _M_print_string(const char* __string) const
589   {
590     const char* __start = __string;
591     const char* __finish = __start;
592     const int __bufsize = 128;
593     char __buf[__bufsize];
594
595     while (*__start)
596       {
597         if (*__start != '%')
598           {
599             // [__start, __finish) denotes the next word
600             __finish = __start;
601             while (isalnum(*__finish))
602               ++__finish;
603             if (__start == __finish)
604               ++__finish;
605             if (isspace(*__finish))
606               ++__finish;
607             
608             const ptrdiff_t __len = __finish - __start;
609             assert(__len < __bufsize);
610             memcpy(__buf, __start, __len);
611             __buf[__len] = '\0';
612             _M_print_word(__buf);
613             __start = __finish;
614             
615             // Skip extra whitespace
616             while (*__start == ' ') 
617               ++__start;
618             
619             continue;
620           } 
621         
622         ++__start;
623         assert(*__start);
624         if (*__start == '%')
625           {
626             _M_print_word("%");
627             ++__start;
628             continue;
629           }
630         
631         // Get the parameter number
632         assert(*__start >= '1' && *__start <= '9');
633         size_t __param = *__start - '0';
634         --__param;
635         assert(__param < _M_num_parameters);
636       
637         // '.' separates the parameter number from the field
638         // name, if there is one.
639         ++__start;
640         if (*__start != '.')
641           {
642             assert(*__start == ';');
643             ++__start;
644             __buf[0] = '\0';
645             if (_M_parameters[__param]._M_kind == _Parameter::__integer)
646               {
647                 _M_format_word(__buf, __bufsize, "%ld", 
648                                _M_parameters[__param]._M_variant._M_integer._M_value);
649                 _M_print_word(__buf);
650               }
651             else if (_M_parameters[__param]._M_kind == _Parameter::__string)
652               _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
653             continue;
654           }
655         
656         // Extract the field name we want
657         enum { __max_field_len = 16 };
658         char __field[__max_field_len];
659         int __field_idx = 0;
660         ++__start;
661         while (*__start != ';')
662           {
663             assert(*__start);
664             assert(__field_idx < __max_field_len-1);
665             __field[__field_idx++] = *__start++;
666           }
667         ++__start;
668         __field[__field_idx] = 0;
669         
670         _M_parameters[__param]._M_print_field(this, __field);             
671       }
672   }
673
674   // Instantiations.
675   template
676     void
677     _Error_formatter::_M_format_word(char*, int, const char*, 
678                                      const void*) const;
679
680   template
681     void
682     _Error_formatter::_M_format_word(char*, int, const char*, long) const;
683
684   template
685     void
686     _Error_formatter::_M_format_word(char*, int, const char*, 
687                                      std::size_t) const;
688
689   template
690     void
691     _Error_formatter::_M_format_word(char*, int, const char*, 
692                                      const char*) const;
693 } // namespace __gnu_debug