OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // --- | 30 // --- |
31 // Sanjay Ghemawat <opensource@google.com> | 31 // Sanjay Ghemawat <opensource@google.com> |
32 | 32 |
33 #include <config.h> | 33 #include <config.h> |
34 #include <stdio.h> | 34 #include "internal_logging.h" |
35 #include <stdarg.h> | 35 #include <stdarg.h> // for va_end, va_start |
| 36 #include <stdio.h> // for vsnprintf, va_list, etc |
| 37 #include <stdlib.h> // for abort |
| 38 #include <string.h> // for strlen, memcpy |
36 #ifdef HAVE_UNISTD_H | 39 #ifdef HAVE_UNISTD_H |
37 #include <unistd.h> // for write() | 40 #include <unistd.h> // for write() |
38 #endif | 41 #endif |
39 #include <string.h> | 42 |
40 #include <google/malloc_extension.h> | 43 #include <google/malloc_extension.h> |
41 #include "internal_logging.h" | 44 #include "base/logging.h" // for perftools_vsnprintf |
| 45 #include "base/spinlock.h" // for SpinLockHolder, SpinLock |
42 | 46 |
43 static const int kLogBufSize = 800; | 47 static const int kLogBufSize = 800; |
44 | 48 |
45 void TCMalloc_MESSAGE(const char* filename, | 49 void TCMalloc_MESSAGE(const char* filename, |
46 int line_number, | 50 int line_number, |
47 const char* format, ...) { | 51 const char* format, ...) { |
48 char buf[kLogBufSize]; | 52 char buf[kLogBufSize]; |
49 const int n = snprintf(buf, sizeof(buf), "%s:%d] ", filename, line_number); | 53 const int n = snprintf(buf, sizeof(buf), "%s:%d] ", filename, line_number); |
50 if (n < kLogBufSize) { | 54 if (n < kLogBufSize) { |
51 va_list ap; | 55 va_list ap; |
52 va_start(ap, format); | 56 va_start(ap, format); |
53 vsnprintf(buf + n, kLogBufSize - n, format, ap); | 57 perftools_vsnprintf(buf + n, kLogBufSize - n, format, ap); |
54 va_end(ap); | 58 va_end(ap); |
55 } | 59 } |
56 write(STDERR_FILENO, buf, strlen(buf)); | 60 write(STDERR_FILENO, buf, strlen(buf)); |
57 } | 61 } |
58 | 62 |
59 static const int kStatsBufferSize = 16 << 10; | 63 static const int kStatsBufferSize = 16 << 10; |
60 static char stats_buffer[kStatsBufferSize] = { 0 }; | 64 static char stats_buffer[kStatsBufferSize] = { 0 }; |
61 | 65 |
62 static void TCMalloc_CRASH_internal(bool dump_stats, | 66 static void TCMalloc_CRASH_internal(bool dump_stats, |
63 const char* filename, | 67 const char* filename, |
64 int line_number, | 68 int line_number, |
65 const char* format, va_list ap) { | 69 const char* format, va_list ap) { |
66 char buf[kLogBufSize]; | 70 char buf[kLogBufSize]; |
67 const int n = snprintf(buf, sizeof(buf), "%s:%d] ", filename, line_number); | 71 const int n = snprintf(buf, sizeof(buf), "%s:%d] ", filename, line_number); |
68 if (n < kLogBufSize) { | 72 if (n < kLogBufSize) { |
69 vsnprintf(buf + n, kLogBufSize - n, format, ap); | 73 perftools_vsnprintf(buf + n, kLogBufSize - n, format, ap); |
70 } | 74 } |
71 write(STDERR_FILENO, buf, strlen(buf)); | 75 write(STDERR_FILENO, buf, strlen(buf)); |
72 if (dump_stats) { | 76 if (dump_stats) { |
73 MallocExtension::instance()->GetStats(stats_buffer, kStatsBufferSize); | 77 MallocExtension::instance()->GetStats(stats_buffer, kStatsBufferSize); |
74 write(STDERR_FILENO, stats_buffer, strlen(stats_buffer)); | 78 write(STDERR_FILENO, stats_buffer, strlen(stats_buffer)); |
75 } | 79 } |
76 | 80 |
77 abort(); | 81 abort(); |
78 } | 82 } |
79 | 83 |
(...skipping 12 matching lines...) Expand all Loading... |
92 va_list ap; | 96 va_list ap; |
93 va_start(ap, format); | 97 va_start(ap, format); |
94 TCMalloc_CRASH_internal(dump_stats_, file_, line_, format, ap); | 98 TCMalloc_CRASH_internal(dump_stats_, file_, line_, format, ap); |
95 va_end(ap); | 99 va_end(ap); |
96 } | 100 } |
97 | 101 |
98 void TCMalloc_Printer::printf(const char* format, ...) { | 102 void TCMalloc_Printer::printf(const char* format, ...) { |
99 if (left_ > 0) { | 103 if (left_ > 0) { |
100 va_list ap; | 104 va_list ap; |
101 va_start(ap, format); | 105 va_start(ap, format); |
102 const int r = vsnprintf(buf_, left_, format, ap); | 106 const int r = perftools_vsnprintf(buf_, left_, format, ap); |
103 va_end(ap); | 107 va_end(ap); |
104 if (r < 0) { | 108 if (r < 0) { |
105 // Perhaps an old glibc that returns -1 on truncation? | 109 // Perhaps an old glibc that returns -1 on truncation? |
106 left_ = 0; | 110 left_ = 0; |
107 } else if (r > left_) { | 111 } else if (r > left_) { |
108 // Truncation | 112 // Truncation |
109 left_ = 0; | 113 left_ = 0; |
110 } else { | 114 } else { |
111 left_ -= r; | 115 left_ -= r; |
112 buf_ += r; | 116 buf_ += r; |
113 } | 117 } |
114 } | 118 } |
115 } | 119 } |
OLD | NEW |