Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: third_party/tcmalloc/vendor/src/symbolize.cc

Issue 9316021: Update the tcmalloc vendor branch to r144 (gperftools 2.0). (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Reuploading Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009, Google Inc. 1 // Copyright (c) 2009, 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 #endif 50 #endif
51 #ifdef __MACH__ 51 #ifdef __MACH__
52 #include <mach-o/dyld.h> // for GetProgramInvocationName() 52 #include <mach-o/dyld.h> // for GetProgramInvocationName()
53 #include <limits.h> // for PATH_MAX 53 #include <limits.h> // for PATH_MAX
54 #endif 54 #endif
55 #if defined(__CYGWIN__) || defined(__CYGWIN32__) 55 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
56 #include <io.h> // for get_osfhandle() 56 #include <io.h> // for get_osfhandle()
57 #endif 57 #endif
58 #include <string> 58 #include <string>
59 #include "base/commandlineflags.h" 59 #include "base/commandlineflags.h"
60 #include "base/logging.h"
60 #include "base/sysinfo.h" 61 #include "base/sysinfo.h"
61 62
62 using std::string; 63 using std::string;
63 using tcmalloc::DumpProcSelfMaps; // from sysinfo.h 64 using tcmalloc::DumpProcSelfMaps; // from sysinfo.h
64 65
65 66
66 DEFINE_string(symbolize_pprof, 67 DEFINE_string(symbolize_pprof,
67 EnvToString("PPROF_PATH", "pprof"), 68 EnvToString("PPROF_PATH", "pprof"),
68 "Path to pprof to call for reporting function names."); 69 "Path to pprof to call for reporting function names.");
69 70
(...skipping 16 matching lines...) Expand all
86 uint32_t length = sizeof(program_invocation_name); 87 uint32_t length = sizeof(program_invocation_name);
87 if (_NSGetExecutablePath(program_invocation_name, &length)) 88 if (_NSGetExecutablePath(program_invocation_name, &length))
88 return NULL; 89 return NULL;
89 } 90 }
90 return program_invocation_name; 91 return program_invocation_name;
91 #else 92 #else
92 return NULL; // figure out a way to get argv[0] 93 return NULL; // figure out a way to get argv[0]
93 #endif 94 #endif
94 } 95 }
95 96
97 // Prints an error message when you can't run Symbolize().
98 static void PrintError(const char* reason) {
99 RAW_LOG(ERROR,
100 "*** WARNING: Cannot convert addresses to symbols in output below.\n"
101 "*** Reason: %s\n"
102 "*** If you cannot fix this, try running pprof directly.\n",
103 reason);
104 }
105
96 void SymbolTable::Add(const void* addr) { 106 void SymbolTable::Add(const void* addr) {
97 symbolization_table_[addr] = ""; 107 symbolization_table_[addr] = "";
98 } 108 }
99 109
100 const char* SymbolTable::GetSymbol(const void* addr) { 110 const char* SymbolTable::GetSymbol(const void* addr) {
101 return symbolization_table_[addr]; 111 return symbolization_table_[addr];
102 } 112 }
103 113
104 // Updates symbolization_table with the pointers to symbol names corresponding 114 // Updates symbolization_table with the pointers to symbol names corresponding
105 // to its keys. The symbol names are stored in out, which is allocated and 115 // to its keys. The symbol names are stored in out, which is allocated and
106 // freed by the caller of this routine. 116 // freed by the caller of this routine.
107 // Note that the forking/etc is not thread-safe or re-entrant. That's 117 // Note that the forking/etc is not thread-safe or re-entrant. That's
108 // ok for the purpose we need -- reporting leaks detected by heap-checker 118 // ok for the purpose we need -- reporting leaks detected by heap-checker
109 // -- but be careful if you decide to use this routine for other purposes. 119 // -- but be careful if you decide to use this routine for other purposes.
120 // Returns number of symbols read on error. If can't symbolize, returns 0
121 // and emits an error message about why.
110 int SymbolTable::Symbolize() { 122 int SymbolTable::Symbolize() {
111 #if !defined(HAVE_UNISTD_H) || !defined(HAVE_SYS_SOCKET_H) || !defined(HAVE_SYS _WAIT_H) 123 #if !defined(HAVE_UNISTD_H) || !defined(HAVE_SYS_SOCKET_H) || !defined(HAVE_SYS _WAIT_H)
124 PrintError("Perftools does not know how to call a sub-process on this O/S");
112 return 0; 125 return 0;
113 #else 126 #else
114 const char* argv0 = GetProgramInvocationName(); 127 const char* argv0 = GetProgramInvocationName();
115 if (argv0 == NULL) // can't call symbolize if we can't figure out our name 128 if (argv0 == NULL) { // can't call symbolize if we can't figure out our name
129 PrintError("Cannot figure out the name of this executable (argv0)");
116 return 0; 130 return 0;
131 }
132 if (access(g_pprof_path->c_str(), R_OK) != 0) {
133 PrintError("Cannot find 'pprof' (is PPROF_PATH set correctly?)");
134 return 0;
135 }
117 136
118 // All this work is to do two-way communication. ugh. 137 // All this work is to do two-way communication. ugh.
119 int *child_in = NULL; // file descriptors 138 int *child_in = NULL; // file descriptors
120 int *child_out = NULL; // for now, we don't worry about child_err 139 int *child_out = NULL; // for now, we don't worry about child_err
121 int child_fds[5][2]; // socketpair may be called up to five times below 140 int child_fds[5][2]; // socketpair may be called up to five times below
122 141
123 // The client program may close its stdin and/or stdout and/or stderr 142 // The client program may close its stdin and/or stdout and/or stderr
124 // thus allowing socketpair to reuse file descriptors 0, 1 or 2. 143 // thus allowing socketpair to reuse file descriptors 0, 1 or 2.
125 // In this case the communication between the forked processes may be broken 144 // In this case the communication between the forked processes may be broken
126 // if either the parent or the child tries to close or duplicate these 145 // if either the parent or the child tries to close or duplicate these
127 // descriptors. The loop below produces two pairs of file descriptors, each 146 // descriptors. The loop below produces two pairs of file descriptors, each
128 // greater than 2 (stderr). 147 // greater than 2 (stderr).
129 for (int i = 0; i < 5; i++) { 148 for (int i = 0; i < 5; i++) {
130 if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_fds[i]) == -1) { 149 if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_fds[i]) == -1) {
131 for (int j = 0; j < i; j++) { 150 for (int j = 0; j < i; j++) {
132 close(child_fds[j][0]); 151 close(child_fds[j][0]);
133 close(child_fds[j][1]); 152 close(child_fds[j][1]);
153 PrintError("Cannot create a socket pair");
134 return 0; 154 return 0;
135 } 155 }
136 } else { 156 } else {
137 if ((child_fds[i][0] > 2) && (child_fds[i][1] > 2)) { 157 if ((child_fds[i][0] > 2) && (child_fds[i][1] > 2)) {
138 if (child_in == NULL) { 158 if (child_in == NULL) {
139 child_in = child_fds[i]; 159 child_in = child_fds[i];
140 } else { 160 } else {
141 child_out = child_fds[i]; 161 child_out = child_fds[i];
142 for (int j = 0; j < i; j++) { 162 for (int j = 0; j < i; j++) {
143 if (child_fds[j] == child_in) continue; 163 if (child_fds[j] == child_in) continue;
144 close(child_fds[j][0]); 164 close(child_fds[j][0]);
145 close(child_fds[j][1]); 165 close(child_fds[j][1]);
146 } 166 }
147 break; 167 break;
148 } 168 }
149 } 169 }
150 } 170 }
151 } 171 }
152 172
153 switch (fork()) { 173 switch (fork()) {
154 case -1: { // error 174 case -1: { // error
155 close(child_in[0]); 175 close(child_in[0]);
156 close(child_in[1]); 176 close(child_in[1]);
157 close(child_out[0]); 177 close(child_out[0]);
158 close(child_out[1]); 178 close(child_out[1]);
179 PrintError("Unknown error calling fork()");
159 return 0; 180 return 0;
160 } 181 }
161 case 0: { // child 182 case 0: { // child
162 close(child_in[1]); // child uses the 0's, parent uses the 1's 183 close(child_in[1]); // child uses the 0's, parent uses the 1's
163 close(child_out[1]); // child uses the 0's, parent uses the 1's 184 close(child_out[1]); // child uses the 0's, parent uses the 1's
164 close(0); 185 close(0);
165 close(1); 186 close(1);
166 if (dup2(child_in[0], 0) == -1) _exit(1); 187 if (dup2(child_in[0], 0) == -1) _exit(1);
167 if (dup2(child_out[0], 1) == -1) _exit(2); 188 if (dup2(child_out[0], 1) == -1) _exit(2);
168 // Unset vars that might cause trouble when we fork 189 // Unset vars that might cause trouble when we fork
169 unsetenv("CPUPROFILE"); 190 unsetenv("CPUPROFILE");
170 unsetenv("HEAPPROFILE"); 191 unsetenv("HEAPPROFILE");
171 unsetenv("HEAPCHECK"); 192 unsetenv("HEAPCHECK");
172 unsetenv("PERFTOOLS_VERBOSE"); 193 unsetenv("PERFTOOLS_VERBOSE");
173 execlp(g_pprof_path->c_str(), g_pprof_path->c_str(), 194 execlp(g_pprof_path->c_str(), g_pprof_path->c_str(),
174 "--symbols", argv0, NULL); 195 "--symbols", argv0, NULL);
175 _exit(3); // if execvp fails, it's bad news for us 196 _exit(3); // if execvp fails, it's bad news for us
176 } 197 }
177 default: { // parent 198 default: { // parent
178 close(child_in[0]); // child uses the 0's, parent uses the 1's 199 close(child_in[0]); // child uses the 0's, parent uses the 1's
179 close(child_out[0]); // child uses the 0's, parent uses the 1's 200 close(child_out[0]); // child uses the 0's, parent uses the 1's
180 #ifdef HAVE_POLL_H 201 #ifdef HAVE_POLL_H
202 // Waiting for 1ms seems to give the OS time to notice any errors.
203 poll(0, 0, 1);
181 // For maximum safety, we check to make sure the execlp 204 // For maximum safety, we check to make sure the execlp
182 // succeeded before trying to write. (Otherwise we'll get a 205 // succeeded before trying to write. (Otherwise we'll get a
183 // SIGPIPE.) For systems without poll.h, we'll just skip this 206 // SIGPIPE.) For systems without poll.h, we'll just skip this
184 // check, and trust that the user set PPROF_PATH correctly! 207 // check, and trust that the user set PPROF_PATH correctly!
185 struct pollfd pfd = { child_in[1], POLLOUT, 0 }; 208 struct pollfd pfd = { child_in[1], POLLOUT, 0 };
186 if (!poll(&pfd, 1, 0) || !(pfd.revents & POLLOUT) || 209 if (!poll(&pfd, 1, 0) || !(pfd.revents & POLLOUT) ||
187 (pfd.revents & (POLLHUP|POLLERR))) { 210 (pfd.revents & (POLLHUP|POLLERR))) {
211 PrintError("Cannot run 'pprof' (is PPROF_PATH set correctly?)");
188 return 0; 212 return 0;
189 } 213 }
190 #endif 214 #endif
191 #if defined(__CYGWIN__) || defined(__CYGWIN32__) 215 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
192 // On cygwin, DumpProcSelfMaps() takes a HANDLE, not an fd. Convert. 216 // On cygwin, DumpProcSelfMaps() takes a HANDLE, not an fd. Convert.
193 const HANDLE symbols_handle = (HANDLE) get_osfhandle(child_in[1]); 217 const HANDLE symbols_handle = (HANDLE) get_osfhandle(child_in[1]);
194 DumpProcSelfMaps(symbols_handle); 218 DumpProcSelfMaps(symbols_handle);
195 #else 219 #else
196 DumpProcSelfMaps(child_in[1]); // what pprof expects on stdin 220 DumpProcSelfMaps(child_in[1]); // what pprof expects on stdin
197 #endif 221 #endif
(...skipping 15 matching lines...) Expand all
213 const int kSymbolBufferSize = kSymbolSize * symbolization_table_.size(); 237 const int kSymbolBufferSize = kSymbolSize * symbolization_table_.size();
214 int total_bytes_read = 0; 238 int total_bytes_read = 0;
215 delete[] symbol_buffer_; 239 delete[] symbol_buffer_;
216 symbol_buffer_ = new char[kSymbolBufferSize]; 240 symbol_buffer_ = new char[kSymbolBufferSize];
217 memset(symbol_buffer_, '\0', kSymbolBufferSize); 241 memset(symbol_buffer_, '\0', kSymbolBufferSize);
218 while (1) { 242 while (1) {
219 int bytes_read = read(child_out[1], symbol_buffer_ + total_bytes_read, 243 int bytes_read = read(child_out[1], symbol_buffer_ + total_bytes_read,
220 kSymbolBufferSize - total_bytes_read); 244 kSymbolBufferSize - total_bytes_read);
221 if (bytes_read < 0) { 245 if (bytes_read < 0) {
222 close(child_out[1]); 246 close(child_out[1]);
247 PrintError("Cannot read data from pprof");
223 return 0; 248 return 0;
224 } else if (bytes_read == 0) { 249 } else if (bytes_read == 0) {
225 close(child_out[1]); 250 close(child_out[1]);
226 wait(NULL); 251 wait(NULL);
227 break; 252 break;
228 } else { 253 } else {
229 total_bytes_read += bytes_read; 254 total_bytes_read += bytes_read;
230 } 255 }
231 } 256 }
232 // We have successfully read the output of pprof into out. Make sure 257 // We have successfully read the output of pprof into out. Make sure
233 // the last symbol is full (we can tell because it ends with a \n). 258 // the last symbol is full (we can tell because it ends with a \n).
234 if (total_bytes_read == 0 || symbol_buffer_[total_bytes_read - 1] != '\n') 259 if (total_bytes_read == 0 || symbol_buffer_[total_bytes_read - 1] != '\n')
235 return 0; 260 return 0;
236 // make the symbolization_table_ values point to the output vector 261 // make the symbolization_table_ values point to the output vector
237 SymbolMap::iterator fill = symbolization_table_.begin(); 262 SymbolMap::iterator fill = symbolization_table_.begin();
238 int num_symbols = 0; 263 int num_symbols = 0;
239 const char *current_name = symbol_buffer_; 264 const char *current_name = symbol_buffer_;
240 for (int i = 0; i < total_bytes_read; i++) { 265 for (int i = 0; i < total_bytes_read; i++) {
241 if (symbol_buffer_[i] == '\n') { 266 if (symbol_buffer_[i] == '\n') {
242 fill->second = current_name; 267 fill->second = current_name;
243 symbol_buffer_[i] = '\0'; 268 symbol_buffer_[i] = '\0';
244 current_name = symbol_buffer_ + i + 1; 269 current_name = symbol_buffer_ + i + 1;
245 fill++; 270 fill++;
246 num_symbols++; 271 num_symbols++;
247 } 272 }
248 } 273 }
249 return num_symbols; 274 return num_symbols;
250 } 275 }
251 } 276 }
277 PrintError("Unkown error (should never occur!)");
252 return 0; // shouldn't be reachable 278 return 0; // shouldn't be reachable
253 #endif 279 #endif
254 } 280 }
OLDNEW
« no previous file with comments | « third_party/tcmalloc/vendor/src/static_vars.cc ('k') | third_party/tcmalloc/vendor/src/system-alloc.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698