| OLD | NEW |
| 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 30 matching lines...) Expand all Loading... |
| 41 #endif | 41 #endif |
| 42 #ifdef HAVE_SYS_SOCKET_H | 42 #ifdef HAVE_SYS_SOCKET_H |
| 43 #include <sys/socket.h> // for socketpair() -- needed by Symbolize | 43 #include <sys/socket.h> // for socketpair() -- needed by Symbolize |
| 44 #endif | 44 #endif |
| 45 #ifdef HAVE_SYS_WAIT_H | 45 #ifdef HAVE_SYS_WAIT_H |
| 46 #include <sys/wait.h> // for wait() -- needed by Symbolize | 46 #include <sys/wait.h> // for wait() -- needed by Symbolize |
| 47 #endif | 47 #endif |
| 48 #ifdef HAVE_POLL_H | 48 #ifdef HAVE_POLL_H |
| 49 #include <poll.h> | 49 #include <poll.h> |
| 50 #endif | 50 #endif |
| 51 #ifdef __MACH__ |
| 52 #include <mach-o/dyld.h> // for GetProgramInvocationName() |
| 53 #include <limits.h> // for PATH_MAX |
| 54 #endif |
| 55 #if defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 56 #include <io.h> // for get_osfhandle() |
| 57 #endif |
| 51 #include <string> | 58 #include <string> |
| 52 #include "base/commandlineflags.h" | 59 #include "base/commandlineflags.h" |
| 53 #include "base/sysinfo.h" | 60 #include "base/sysinfo.h" |
| 54 | 61 |
| 55 using std::string; | 62 using std::string; |
| 56 using tcmalloc::DumpProcSelfMaps; // from sysinfo.h | 63 using tcmalloc::DumpProcSelfMaps; // from sysinfo.h |
| 57 | 64 |
| 58 | 65 |
| 59 DEFINE_string(symbolize_pprof, | 66 DEFINE_string(symbolize_pprof, |
| 60 EnvToString("PPROF_PATH", "pprof"), | 67 EnvToString("PPROF_PATH", "pprof"), |
| 61 "Path to pprof to call for reporting function names."); | 68 "Path to pprof to call for reporting function names."); |
| 62 | 69 |
| 63 // heap_profile_table_pprof may be referenced after destructors are | 70 // heap_profile_table_pprof may be referenced after destructors are |
| 64 // called (since that's when leak-checking is done), so we make | 71 // called (since that's when leak-checking is done), so we make |
| 65 // a more-permanent copy that won't ever get destroyed. | 72 // a more-permanent copy that won't ever get destroyed. |
| 66 static string* g_pprof_path = new string(FLAGS_symbolize_pprof); | 73 static string* g_pprof_path = new string(FLAGS_symbolize_pprof); |
| 67 | 74 |
| 75 // Returns NULL if we're on an OS where we can't get the invocation name. |
| 76 // Using a static var is ok because we're not called from a thread. |
| 77 static char* GetProgramInvocationName() { |
| 78 #if defined(HAVE_PROGRAM_INVOCATION_NAME) |
| 79 extern char* program_invocation_name; // gcc provides this |
| 80 return program_invocation_name; |
| 81 #elif defined(__MACH__) |
| 82 // We don't want to allocate memory for this since we may be |
| 83 // calculating it when memory is corrupted. |
| 84 static char program_invocation_name[PATH_MAX]; |
| 85 if (program_invocation_name[0] == '\0') { // first time calculating |
| 86 uint32_t length = sizeof(program_invocation_name); |
| 87 if (_NSGetExecutablePath(program_invocation_name, &length)) |
| 88 return NULL; |
| 89 } |
| 90 return program_invocation_name; |
| 91 #else |
| 92 return NULL; // figure out a way to get argv[0] |
| 93 #endif |
| 94 } |
| 95 |
| 68 void SymbolTable::Add(const void* addr) { | 96 void SymbolTable::Add(const void* addr) { |
| 69 symbolization_table_[addr] = ""; | 97 symbolization_table_[addr] = ""; |
| 70 } | 98 } |
| 71 | 99 |
| 72 const char* SymbolTable::GetSymbol(const void* addr) { | 100 const char* SymbolTable::GetSymbol(const void* addr) { |
| 73 return symbolization_table_[addr]; | 101 return symbolization_table_[addr]; |
| 74 } | 102 } |
| 75 | 103 |
| 76 // Updates symbolization_table with the pointers to symbol names corresponding | 104 // Updates symbolization_table with the pointers to symbol names corresponding |
| 77 // to its keys. The symbol names are stored in out, which is allocated and | 105 // to its keys. The symbol names are stored in out, which is allocated and |
| 78 // freed by the caller of this routine. | 106 // freed by the caller of this routine. |
| 79 // Note that the forking/etc is not thread-safe or re-entrant. That's | 107 // Note that the forking/etc is not thread-safe or re-entrant. That's |
| 80 // ok for the purpose we need -- reporting leaks detected by heap-checker | 108 // ok for the purpose we need -- reporting leaks detected by heap-checker |
| 81 // -- but be careful if you decide to use this routine for other purposes. | 109 // -- but be careful if you decide to use this routine for other purposes. |
| 82 int SymbolTable::Symbolize() { | 110 int SymbolTable::Symbolize() { |
| 83 #if !defined(HAVE_UNISTD_H) || !defined(HAVE_SYS_SOCKET_H) || !defined(HAVE_SYS
_WAIT_H) | 111 #if !defined(HAVE_UNISTD_H) || !defined(HAVE_SYS_SOCKET_H) || !defined(HAVE_SYS
_WAIT_H) |
| 84 return 0; | 112 return 0; |
| 85 #elif !defined(HAVE_PROGRAM_INVOCATION_NAME) | |
| 86 return 0; // TODO(csilvers): get argv[0] somehow | |
| 87 #else | 113 #else |
| 114 const char* argv0 = GetProgramInvocationName(); |
| 115 if (argv0 == NULL) // can't call symbolize if we can't figure out our name |
| 116 return 0; |
| 117 |
| 88 // All this work is to do two-way communication. ugh. | 118 // All this work is to do two-way communication. ugh. |
| 89 extern char* program_invocation_name; // gcc provides this | |
| 90 int *child_in = NULL; // file descriptors | 119 int *child_in = NULL; // file descriptors |
| 91 int *child_out = NULL; // for now, we don't worry about child_err | 120 int *child_out = NULL; // for now, we don't worry about child_err |
| 92 int child_fds[5][2]; // socketpair may be called up to five times below | 121 int child_fds[5][2]; // socketpair may be called up to five times below |
| 93 | 122 |
| 94 // The client program may close its stdin and/or stdout and/or stderr | 123 // The client program may close its stdin and/or stdout and/or stderr |
| 95 // thus allowing socketpair to reuse file descriptors 0, 1 or 2. | 124 // thus allowing socketpair to reuse file descriptors 0, 1 or 2. |
| 96 // In this case the communication between the forked processes may be broken | 125 // In this case the communication between the forked processes may be broken |
| 97 // if either the parent or the child tries to close or duplicate these | 126 // if either the parent or the child tries to close or duplicate these |
| 98 // descriptors. The loop below produces two pairs of file descriptors, each | 127 // descriptors. The loop below produces two pairs of file descriptors, each |
| 99 // greater than 2 (stderr). | 128 // greater than 2 (stderr). |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 close(0); | 164 close(0); |
| 136 close(1); | 165 close(1); |
| 137 if (dup2(child_in[0], 0) == -1) _exit(1); | 166 if (dup2(child_in[0], 0) == -1) _exit(1); |
| 138 if (dup2(child_out[0], 1) == -1) _exit(2); | 167 if (dup2(child_out[0], 1) == -1) _exit(2); |
| 139 // Unset vars that might cause trouble when we fork | 168 // Unset vars that might cause trouble when we fork |
| 140 unsetenv("CPUPROFILE"); | 169 unsetenv("CPUPROFILE"); |
| 141 unsetenv("HEAPPROFILE"); | 170 unsetenv("HEAPPROFILE"); |
| 142 unsetenv("HEAPCHECK"); | 171 unsetenv("HEAPCHECK"); |
| 143 unsetenv("PERFTOOLS_VERBOSE"); | 172 unsetenv("PERFTOOLS_VERBOSE"); |
| 144 execlp(g_pprof_path->c_str(), g_pprof_path->c_str(), | 173 execlp(g_pprof_path->c_str(), g_pprof_path->c_str(), |
| 145 "--symbols", program_invocation_name, NULL); | 174 "--symbols", argv0, NULL); |
| 146 _exit(3); // if execvp fails, it's bad news for us | 175 _exit(3); // if execvp fails, it's bad news for us |
| 147 } | 176 } |
| 148 default: { // parent | 177 default: { // parent |
| 149 close(child_in[0]); // child uses the 0's, parent uses the 1's | 178 close(child_in[0]); // child uses the 0's, parent uses the 1's |
| 150 close(child_out[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 |
| 151 #ifdef HAVE_POLL_H | 180 #ifdef HAVE_POLL_H |
| 152 // For maximum safety, we check to make sure the execlp | 181 // For maximum safety, we check to make sure the execlp |
| 153 // succeeded before trying to write. (Otherwise we'll get a | 182 // succeeded before trying to write. (Otherwise we'll get a |
| 154 // SIGPIPE.) For systems without poll.h, we'll just skip this | 183 // SIGPIPE.) For systems without poll.h, we'll just skip this |
| 155 // check, and trust that the user set PPROF_PATH correctly! | 184 // check, and trust that the user set PPROF_PATH correctly! |
| 156 struct pollfd pfd = { child_in[1], POLLOUT, 0 }; | 185 struct pollfd pfd = { child_in[1], POLLOUT, 0 }; |
| 157 if (!poll(&pfd, 1, 0) || !(pfd.revents & POLLOUT) || | 186 if (!poll(&pfd, 1, 0) || !(pfd.revents & POLLOUT) || |
| 158 (pfd.revents & (POLLHUP|POLLERR))) { | 187 (pfd.revents & (POLLHUP|POLLERR))) { |
| 159 return 0; | 188 return 0; |
| 160 } | 189 } |
| 161 #endif | 190 #endif |
| 191 #if defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 192 // On cygwin, DumpProcSelfMaps() takes a HANDLE, not an fd. Convert. |
| 193 const HANDLE symbols_handle = (HANDLE) get_osfhandle(child_in[1]); |
| 194 DumpProcSelfMaps(symbols_handle); |
| 195 #else |
| 162 DumpProcSelfMaps(child_in[1]); // what pprof expects on stdin | 196 DumpProcSelfMaps(child_in[1]); // what pprof expects on stdin |
| 197 #endif |
| 163 | 198 |
| 164 // Allocate 24 bytes = ("0x" + 8 bytes + "\n" + overhead) for each | 199 // Allocate 24 bytes = ("0x" + 8 bytes + "\n" + overhead) for each |
| 165 // address to feed to pprof. | 200 // address to feed to pprof. |
| 166 const int kOutBufSize = 24 * symbolization_table_.size(); | 201 const int kOutBufSize = 24 * symbolization_table_.size(); |
| 167 char *pprof_buffer = new char[kOutBufSize]; | 202 char *pprof_buffer = new char[kOutBufSize]; |
| 168 int written = 0; | 203 int written = 0; |
| 169 for (SymbolMap::const_iterator iter = symbolization_table_.begin(); | 204 for (SymbolMap::const_iterator iter = symbolization_table_.begin(); |
| 170 iter != symbolization_table_.end(); ++iter) { | 205 iter != symbolization_table_.end(); ++iter) { |
| 171 written += snprintf(pprof_buffer + written, kOutBufSize - written, | 206 written += snprintf(pprof_buffer + written, kOutBufSize - written, |
| 172 // pprof expects format to be 0xXXXXXX | 207 // pprof expects format to be 0xXXXXXX |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 fill++; | 245 fill++; |
| 211 num_symbols++; | 246 num_symbols++; |
| 212 } | 247 } |
| 213 } | 248 } |
| 214 return num_symbols; | 249 return num_symbols; |
| 215 } | 250 } |
| 216 } | 251 } |
| 217 return 0; // shouldn't be reachable | 252 return 0; // shouldn't be reachable |
| 218 #endif | 253 #endif |
| 219 } | 254 } |
| OLD | NEW |