]>
Commit | Line | Data |
---|---|---|
1b78aef5 | 1 | // Copyright (C) Andrew Tridgell 2002 (original file) |
dd936721 | 2 | // Copyright (C) 2006-2010 Red Hat Inc. (systemtap changes) |
dff50e09 | 3 | // |
1b78aef5 DS |
4 | // This program is free software; you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation; either version 2 of the License, or | |
7 | // (at your option) any later version. | |
dff50e09 | 8 | // |
1b78aef5 DS |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
dff50e09 | 13 | // |
1b78aef5 DS |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program; if not, write to the Free Software | |
16 | // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | ||
18 | #include "config.h" | |
19 | #include "session.h" | |
20 | #include "hash.h" | |
21 | #include "util.h" | |
3b579393 FCE |
22 | |
23 | #include <cstdlib> | |
dd936721 JS |
24 | #include <cstring> |
25 | #include <fstream> | |
1b78aef5 DS |
26 | #include <sstream> |
27 | #include <iomanip> | |
28 | #include <cerrno> | |
29 | ||
30 | extern "C" { | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <unistd.h> | |
dd936721 | 34 | #include "mdfour.h" |
1b78aef5 DS |
35 | } |
36 | ||
37 | using namespace std; | |
38 | ||
dd936721 JS |
39 | |
40 | class hash | |
41 | { | |
42 | private: | |
43 | struct mdfour md4; | |
44 | std::ostringstream parm_stream; | |
45 | ||
46 | public: | |
47 | hash() { start(); } | |
48 | hash(const hash &base) { md4 = base.md4; parm_stream << base.parm_stream.str(); } | |
49 | ||
50 | void start(); | |
51 | ||
52 | void add(const unsigned char *buffer, size_t size); | |
9beca2c0 | 53 | template<typename T> void add(const T& x); |
dd936721 JS |
54 | void add(const char *s) { add((const unsigned char *)s, strlen(s)); } |
55 | void add(const std::string& s) { add((const unsigned char *)s.c_str(), | |
56 | s.length()); } | |
9beca2c0 | 57 | |
413c55ed | 58 | void add_path(const std::string& path); |
dd936721 JS |
59 | |
60 | void result(std::string& r); | |
178b5ebc | 61 | std::string get_parms() { return parm_stream.str(); } |
dd936721 JS |
62 | }; |
63 | ||
64 | ||
1b78aef5 DS |
65 | void |
66 | hash::start() | |
67 | { | |
68 | mdfour_begin(&md4); | |
69 | } | |
70 | ||
71 | ||
72 | void | |
73 | hash::add(const unsigned char *buffer, size_t size) | |
74 | { | |
178b5ebc | 75 | parm_stream << buffer << endl; |
1b78aef5 DS |
76 | mdfour_update(&md4, buffer, size); |
77 | } | |
78 | ||
79 | ||
9beca2c0 JS |
80 | template <typename T> void |
81 | hash::add(const T& x) | |
82 | { | |
178b5ebc | 83 | parm_stream << x << endl; |
9beca2c0 JS |
84 | mdfour_update(&md4, (const unsigned char *)&x, sizeof(x)); |
85 | } | |
86 | ||
87 | ||
2a8c27f6 | 88 | void |
413c55ed | 89 | hash::add_path(const std::string& path) |
2a8c27f6 JS |
90 | { |
91 | struct stat st; | |
e7899657 | 92 | memset (&st, 0, sizeof(st)); |
2a8c27f6 | 93 | |
413c55ed | 94 | if (stat(path.c_str(), &st) != 0) |
9a193b06 JS |
95 | st.st_size = st.st_mtime = -1; |
96 | ||
413c55ed | 97 | add(path); |
9a193b06 JS |
98 | add(st.st_size); |
99 | add(st.st_mtime); | |
2a8c27f6 JS |
100 | } |
101 | ||
102 | ||
1b78aef5 DS |
103 | void |
104 | hash::result(string& r) | |
105 | { | |
106 | ostringstream rstream; | |
107 | unsigned char sum[16]; | |
108 | ||
109 | mdfour_update(&md4, NULL, 0); | |
110 | mdfour_result(&md4, sum); | |
111 | ||
112 | for (int i=0; i<16; i++) | |
113 | { | |
114 | rstream << hex << setfill('0') << setw(2) << (unsigned)sum[i]; | |
115 | } | |
116 | rstream << "_" << setw(0) << dec << (unsigned)md4.totalN; | |
117 | r = rstream.str(); | |
118 | } | |
119 | ||
0d1ad607 WH |
120 | void create_hash_log(const string &type_str, const string &parms, const string &result, const string &hash_log_path) |
121 | { | |
122 | ofstream log_file; | |
123 | time_t rawtime; | |
124 | time ( &rawtime ); | |
125 | string time_str(ctime (&rawtime)); | |
126 | ||
127 | log_file.open(hash_log_path.c_str()); | |
128 | log_file << "[" << time_str.substr(0,time_str.length()-1); // erase terminated '\n' | |
178b5ebc JS |
129 | log_file << "] " << type_str << ":" << endl; |
130 | log_file << parms << endl; | |
0d1ad607 WH |
131 | log_file << "result:" << result << endl; |
132 | log_file.close(); | |
133 | } | |
1b78aef5 | 134 | |
9b3c54b2 JS |
135 | static const hash& |
136 | get_base_hash (systemtap_session& s) | |
1b78aef5 | 137 | { |
9b3c54b2 JS |
138 | if (s.base_hash) |
139 | return *s.base_hash; | |
140 | ||
141 | s.base_hash = new hash(); | |
142 | hash& h = *s.base_hash; | |
143 | ||
5c54d49e JS |
144 | // Hash kernel release and arch. |
145 | h.add(s.kernel_release); | |
413c55ed | 146 | h.add_path(s.kernel_build_tree); |
5c54d49e JS |
147 | h.add(s.architecture); |
148 | ||
2a8c27f6 JS |
149 | // Hash a few kernel version/build-id files too |
150 | // (useful for kernel developers reusing a single source tree) | |
413c55ed JS |
151 | h.add_path(s.kernel_build_tree + "/.config"); |
152 | h.add_path(s.kernel_build_tree + "/.version"); | |
153 | h.add_path(s.kernel_build_tree + "/include/linux/compile.h"); | |
154 | h.add_path(s.kernel_build_tree + "/include/linux/version.h"); | |
155 | h.add_path(s.kernel_build_tree + "/include/linux/utsrelease.h"); | |
2a8c27f6 | 156 | |
a5e8d632 JS |
157 | // If the kernel is a git working directory, then add the git HEAD |
158 | // revision to our hash as well. | |
d0d806ed JS |
159 | // XXX avoiding this for now, because it's potentially expensive and has |
160 | // uncertain gain. The only corner case that this may help is if a developer | |
161 | // is switching the source tree without rebuilding the kernel... | |
162 | ///h.add(git_revision(s.kernel_build_tree)); | |
a5e8d632 | 163 | |
5c54d49e | 164 | // Hash runtime path (that gets added in as "-R path"). |
413c55ed | 165 | h.add_path(s.runtime_path); |
5c54d49e JS |
166 | |
167 | // Hash compiler path, size, and mtime. We're just going to assume | |
168 | // we'll be using gcc. XXX: getting kbuild to spit out out would be | |
413c55ed JS |
169 | // better, especially since this is fooled by ccache. |
170 | h.add_path(find_executable("gcc")); | |
5c54d49e JS |
171 | |
172 | // Hash the systemtap size and mtime. We could use VERSION/DATE, | |
173 | // but when developing systemtap that doesn't work well (since you | |
174 | // can compile systemtap multiple times in 1 day). Since we don't | |
175 | // know exactly where we're getting run from, we'll use | |
176 | // /proc/self/exe. | |
413c55ed JS |
177 | // XXX well almost exactly -- valgrind throws this off |
178 | h.add_path("/proc/self/exe"); | |
9b3c54b2 JS |
179 | |
180 | return h; | |
5c54d49e JS |
181 | } |
182 | ||
183 | ||
184 | static bool | |
185 | create_hashdir (systemtap_session& s, const string& result, string& hashdir) | |
186 | { | |
187 | int nlevels = 1; | |
188 | ||
189 | // Use a N level subdir for the cache path to reduce the impact on | |
190 | // filesystems which are slow for large directories. Let N be adjustable. | |
191 | const char *s_n = getenv("SYSTEMTAP_NLEVELS"); | |
192 | if (s_n) | |
1b78aef5 DS |
193 | { |
194 | nlevels = atoi(s_n); | |
195 | if (nlevels < 1) nlevels = 1; | |
196 | if (nlevels > 8) nlevels = 8; | |
197 | } | |
198 | ||
5c54d49e JS |
199 | hashdir = s.cache_path; |
200 | ||
201 | for (int i = 0; i < nlevels; i++) | |
202 | { | |
203 | hashdir += string("/") + result[i*2] + result[i*2 + 1]; | |
204 | if (create_dir(hashdir.c_str()) != 0) | |
205 | { | |
206 | if (! s.suppress_warnings) | |
207 | cerr << "Warning: failed to create cache directory (\"" | |
208 | << hashdir + "\"): " << strerror(errno) | |
209 | << ", disabling cache support." << endl; | |
63d530ab | 210 | s.use_cache = s.use_script_cache = false; |
5c54d49e JS |
211 | return false; |
212 | } | |
213 | } | |
214 | return true; | |
215 | } | |
216 | ||
217 | ||
9b3c54b2 JS |
218 | void |
219 | find_script_hash (systemtap_session& s, const string& script) | |
5c54d49e | 220 | { |
9b3c54b2 | 221 | hash h(get_base_hash(s)); |
5c54d49e JS |
222 | struct stat st; |
223 | ||
1b78aef5 DS |
224 | // Hash getuid. This really shouldn't be necessary (since who you |
225 | // are doesn't change the generated output), but the hash gets used | |
226 | // as the module name. If two different users try to run the same | |
227 | // script at the same time, we need something to differentiate the | |
228 | // module name. | |
229 | h.add(getuid()); | |
230 | ||
1b78aef5 | 231 | // Hash user-specified arguments (that change the generated module). |
9c66f4e0 | 232 | h.add(s.bulk_mode); // '-b' |
9c66f4e0 | 233 | h.add(s.timing); // '-t' |
44f75386 | 234 | h.add(s.prologue_searching); // '-P' |
5f0a03a6 JK |
235 | h.add(s.ignore_vmlinux); // --ignore-vmlinux |
236 | h.add(s.ignore_dwarf); // --ignore-dwarf | |
237 | h.add(s.consult_symtab); // --kelf, --kmap | |
dcfd7fed | 238 | h.add(s.skip_badvars); // --skip-badvars |
df56da84 | 239 | h.add(s.unprivileged); // --unprivileged |
e1704764 | 240 | h.add(s.compatible); // --compatible |
d6c27347 | 241 | h.add(s.omit_werror); // undocumented, evil |
5f0a03a6 JK |
242 | if (!s.kernel_symtab_path.empty()) // --kmap |
243 | { | |
244 | h.add(s.kernel_symtab_path); | |
245 | if (stat(s.kernel_symtab_path.c_str(), &st) == 0) | |
246 | { | |
247 | // NB: stat of /proc/kallsyms always returns size=0, mtime=now... | |
248 | // which is a good reason to use the default /boot/System.map-2.6.xx | |
249 | // instead. | |
250 | h.add(st.st_size); | |
251 | h.add(st.st_mtime); | |
252 | } | |
253 | } | |
1b78aef5 DS |
254 | for (unsigned i = 0; i < s.macros.size(); i++) |
255 | h.add(s.macros[i]); | |
256 | ||
4569f6bb | 257 | // Add any custom kbuild flags (-B) |
1392896d FCE |
258 | for (unsigned i = 0; i < s.kbuildflags.size(); i++) |
259 | h.add(s.kbuildflags[i]); | |
260 | ||
a7f52597 FCE |
261 | // -d MODULE |
262 | for (set<string>::iterator it = s.unwindsym_modules.begin(); | |
263 | it != s.unwindsym_modules.end(); | |
264 | it++) | |
2ccb4199 | 265 | h.add_path(*it); |
a7f52597 FCE |
266 | // XXX: a build-id of each module might be even better |
267 | ||
1b78aef5 DS |
268 | // Add in pass 2 script output. |
269 | h.add(script); | |
270 | ||
5c54d49e JS |
271 | // Get the directory path to store our cached script |
272 | string result, hashdir; | |
1b78aef5 | 273 | h.result(result); |
5c54d49e JS |
274 | if (!create_hashdir(s, result, hashdir)) |
275 | return; | |
1b78aef5 DS |
276 | |
277 | // Update module name to be 'stap_{hash start}'. '{hash start}' | |
278 | // must not be too long. This shouldn't happen, since the maximum | |
279 | // size of a hash is 32 fixed chars + 1 (for the '_') + a max of 11. | |
280 | s.module_name = "stap_" + result; | |
281 | if (s.module_name.size() >= (MODULE_NAME_LEN - 1)) | |
282 | s.module_name.resize(MODULE_NAME_LEN - 1); | |
283 | ||
284 | // 'ccache' would use a hash path of something like: | |
285 | // s.hash_path = hashdir + "/" + result.substr(nlevels); | |
286 | // which would look like: | |
287 | // ~/.stap_cache/A/B/CDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX | |
288 | // | |
289 | // We're using the following so that the module can be used straight | |
290 | // from the cache if desired. This ends up looking like this: | |
291 | // ~/.stap_cache/A/B/stap_ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX.ko | |
292 | s.hash_path = hashdir + "/" + s.module_name + ".ko"; | |
293 | ||
294 | // Update C source name with new module_name. | |
295 | s.translated_source = string(s.tmpdir) + "/" + s.module_name + ".c"; | |
0d1ad607 WH |
296 | create_hash_log(string("script_hash"), h.get_parms(), result, |
297 | hashdir + "/" + s.module_name + "_hash.log"); | |
1b78aef5 | 298 | } |
73267b89 | 299 | |
5c54d49e | 300 | |
9b3c54b2 JS |
301 | void |
302 | find_stapconf_hash (systemtap_session& s) | |
5c54d49e | 303 | { |
9b3c54b2 | 304 | hash h(get_base_hash(s)); |
5c54d49e | 305 | |
4569f6bb JS |
306 | // Add any custom kbuild flags |
307 | for (unsigned i = 0; i < s.kbuildflags.size(); i++) | |
308 | h.add(s.kbuildflags[i]); | |
5c54d49e JS |
309 | |
310 | // Get the directory path to store our cached stapconf parameters | |
311 | string result, hashdir; | |
312 | h.result(result); | |
313 | if (!create_hashdir(s, result, hashdir)) | |
314 | return; | |
315 | ||
de0db58a JS |
316 | s.stapconf_name = "stapconf_" + result + ".h"; |
317 | s.stapconf_path = hashdir + "/" + s.stapconf_name; | |
0d1ad607 WH |
318 | create_hash_log(string("stapconf_hash"), h.get_parms(), result, |
319 | hashdir + "/stapconf_" + result + "_hash.log"); | |
5c54d49e JS |
320 | } |
321 | ||
322 | ||
a2639cb7 | 323 | string |
55e50c24 | 324 | find_tracequery_hash (systemtap_session& s, const vector<string>& headers) |
b278033a | 325 | { |
9b3c54b2 | 326 | hash h(get_base_hash(s)); |
b278033a | 327 | |
55e50c24 JS |
328 | // Add the tracepoint headers to the computed hash |
329 | for (size_t i = 0; i < headers.size(); ++i) | |
413c55ed | 330 | h.add_path(headers[i]); |
b278033a | 331 | |
4569f6bb JS |
332 | // Add any custom kbuild flags |
333 | for (unsigned i = 0; i < s.kbuildflags.size(); i++) | |
334 | h.add(s.kbuildflags[i]); | |
335 | ||
b278033a JS |
336 | // Get the directory path to store our cached module |
337 | string result, hashdir; | |
338 | h.result(result); | |
339 | if (!create_hashdir(s, result, hashdir)) | |
a2639cb7 | 340 | return ""; |
b278033a | 341 | |
0d1ad607 WH |
342 | create_hash_log(string("tracequery_hash"), h.get_parms(), result, |
343 | hashdir + "/tracequery_" + result + "_hash.log"); | |
a2639cb7 | 344 | return hashdir + "/tracequery_" + result + ".ko"; |
b278033a JS |
345 | } |
346 | ||
1f329b5e | 347 | |
a2639cb7 JS |
348 | string |
349 | find_typequery_hash (systemtap_session& s, const string& name) | |
1f329b5e | 350 | { |
9b3c54b2 | 351 | hash h(get_base_hash(s)); |
1f329b5e JS |
352 | |
353 | // Add the typequery name to distinguish the hash | |
354 | h.add(name); | |
355 | ||
4569f6bb JS |
356 | if (name[0] == 'k') |
357 | // Add any custom kbuild flags | |
358 | for (unsigned i = 0; i < s.kbuildflags.size(); i++) | |
359 | h.add(s.kbuildflags[i]); | |
360 | ||
1f329b5e JS |
361 | // Get the directory path to store our cached module |
362 | string result, hashdir; | |
363 | h.result(result); | |
364 | if (!create_hashdir(s, result, hashdir)) | |
a2639cb7 | 365 | return ""; |
1f329b5e | 366 | |
0d1ad607 WH |
367 | create_hash_log(string("typequery_hash"), h.get_parms(), result, |
368 | hashdir + "/typequery_" + result + "_hash.log"); | |
a2639cb7 | 369 | return hashdir + "/typequery_" + result |
1f329b5e JS |
370 | + (name[0] == 'k' ? ".ko" : ".so"); |
371 | } | |
372 | ||
73267b89 | 373 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |