OLD | NEW |
1 // Copyright (c) 2006, Google Inc. | 1 // Copyright (c) 2006, 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 #endif /* TH32CS_SNAPMODULE32 */ | 79 #endif /* TH32CS_SNAPMODULE32 */ |
80 #endif /* PLATFORM_WINDOWS */ | 80 #endif /* PLATFORM_WINDOWS */ |
81 | 81 |
82 // Re-run fn until it doesn't cause EINTR. | 82 // Re-run fn until it doesn't cause EINTR. |
83 #define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR) | 83 #define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR) |
84 | 84 |
85 // open/read/close can set errno, which may be illegal at this | 85 // open/read/close can set errno, which may be illegal at this |
86 // time, so prefer making the syscalls directly if we can. | 86 // time, so prefer making the syscalls directly if we can. |
87 #ifdef HAVE_SYS_SYSCALL_H | 87 #ifdef HAVE_SYS_SYSCALL_H |
88 # include <sys/syscall.h> | 88 # include <sys/syscall.h> |
| 89 #endif |
| 90 #ifdef SYS_open // solaris 11, at least sometimes, only defines SYS_openat |
89 # define safeopen(filename, mode) syscall(SYS_open, filename, mode) | 91 # define safeopen(filename, mode) syscall(SYS_open, filename, mode) |
| 92 #else |
| 93 # define safeopen(filename, mode) open(filename, mode) |
| 94 #endif |
| 95 #ifdef SYS_read |
90 # define saferead(fd, buffer, size) syscall(SYS_read, fd, buffer, size) | 96 # define saferead(fd, buffer, size) syscall(SYS_read, fd, buffer, size) |
| 97 #else |
| 98 # define saferead(fd, buffer, size) read(fd, buffer, size) |
| 99 #endif |
| 100 #ifdef SYS_close |
91 # define safeclose(fd) syscall(SYS_close, fd) | 101 # define safeclose(fd) syscall(SYS_close, fd) |
92 #else | 102 #else |
93 # define safeopen(filename, mode) open(filename, mode) | |
94 # define saferead(fd, buffer, size) read(fd, buffer, size) | |
95 # define safeclose(fd) close(fd) | 103 # define safeclose(fd) close(fd) |
96 #endif | 104 #endif |
97 | 105 |
98 // ---------------------------------------------------------------------- | 106 // ---------------------------------------------------------------------- |
99 // GetenvBeforeMain() | 107 // GetenvBeforeMain() |
100 // GetUniquePathFromEnv() | 108 // GetUniquePathFromEnv() |
101 // Some non-trivial getenv-related functions. | 109 // Some non-trivial getenv-related functions. |
102 // ---------------------------------------------------------------------- | 110 // ---------------------------------------------------------------------- |
103 | 111 |
104 // It's not safe to call getenv() in the malloc hooks, because they | 112 // It's not safe to call getenv() in the malloc hooks, because they |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 // gcc -- if you do a setenv() in a shared libarary in a global | 180 // gcc -- if you do a setenv() in a shared libarary in a global |
173 // constructor, the environment setting is lost by the time main() is | 181 // constructor, the environment setting is lost by the time main() is |
174 // called. The only safe thing we can do in such a situation is to | 182 // called. The only safe thing we can do in such a situation is to |
175 // modify the existing envvar. So we do a hack: in the parent, we set | 183 // modify the existing envvar. So we do a hack: in the parent, we set |
176 // the high bit of the 1st char of CPUPROFILE. In the child, we | 184 // the high bit of the 1st char of CPUPROFILE. In the child, we |
177 // notice the high bit is set and append the pid(). This works | 185 // notice the high bit is set and append the pid(). This works |
178 // assuming cpuprofile filenames don't normally have the high bit set | 186 // assuming cpuprofile filenames don't normally have the high bit set |
179 // in their first character! If that assumption is violated, we'll | 187 // in their first character! If that assumption is violated, we'll |
180 // still get a profile, but one with an unexpected name. | 188 // still get a profile, but one with an unexpected name. |
181 // TODO(csilvers): set an envvar instead when we can do it reliably. | 189 // TODO(csilvers): set an envvar instead when we can do it reliably. |
182 // | |
183 // In Chromium this hack is intentionally disabled, because the path is not | |
184 // re-initialized upon fork. | |
185 bool GetUniquePathFromEnv(const char* env_name, char* path) { | 190 bool GetUniquePathFromEnv(const char* env_name, char* path) { |
186 char* envval = getenv(env_name); | 191 char* envval = getenv(env_name); |
187 if (envval == NULL || *envval == '\0') | 192 if (envval == NULL || *envval == '\0') |
188 return false; | 193 return false; |
189 if (envval[0] & 128) { // high bit is set | 194 if (envval[0] & 128) { // high bit is set |
190 snprintf(path, PATH_MAX, "%c%s_%u", // add pid and clear high bit | 195 snprintf(path, PATH_MAX, "%c%s_%u", // add pid and clear high bit |
191 envval[0] & 127, envval+1, (unsigned int)(getpid())); | 196 envval[0] & 127, envval+1, (unsigned int)(getpid())); |
192 } else { | 197 } else { |
193 snprintf(path, PATH_MAX, "%s", envval); | 198 snprintf(path, PATH_MAX, "%s", envval); |
194 #if 0 | |
195 envval[0] |= 128; // set high bit for kids to see | 199 envval[0] |= 128; // set high bit for kids to see |
196 #endif | |
197 } | 200 } |
198 return true; | 201 return true; |
199 } | 202 } |
200 | 203 |
201 // ---------------------------------------------------------------------- | 204 // ---------------------------------------------------------------------- |
202 // CyclesPerSecond() | 205 // CyclesPerSecond() |
203 // NumCPUs() | 206 // NumCPUs() |
204 // It's important this not call malloc! -- they may be called at | 207 // It's important this not call malloc! -- they may be called at |
205 // global-construct time, before we've set up all our proper malloc | 208 // global-construct time, before we've set up all our proper malloc |
206 // hooks and such. | 209 // hooks and such. |
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
973 ProcMapsIterator::Buffer linebuf; | 976 ProcMapsIterator::Buffer linebuf; |
974 while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) { | 977 while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) { |
975 int written = it.FormatLine(linebuf.buf_, sizeof(linebuf.buf_), | 978 int written = it.FormatLine(linebuf.buf_, sizeof(linebuf.buf_), |
976 start, end, flags, offset, inode, filename, | 979 start, end, flags, offset, inode, filename, |
977 0); | 980 0); |
978 RawWrite(fd, linebuf.buf_, written); | 981 RawWrite(fd, linebuf.buf_, written); |
979 } | 982 } |
980 } | 983 } |
981 | 984 |
982 } // namespace tcmalloc | 985 } // namespace tcmalloc |
OLD | NEW |