1 // Copyright (C) Andrew Tridgell 2002 (original file)
2 // Copyright (C) 2006-2014 Red Hat Inc. (systemtap changes)
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.
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
43 std::ostringstream parm_stream
;
46 stap_hash() { start(); }
47 stap_hash(const stap_hash
&base
) { md4
= base
.md4
; parm_stream
<< base
.parm_stream
.str(); }
51 void add(const std::string
& description
, const unsigned char *buffer
, size_t size
);
52 template<typename T
> void add(const std::string
& d
, const T
& x
);
53 void add(const std::string
& d
, const char *s
) { add((const std::string
&)d
, (const unsigned char *)s
, strlen(s
)); }
54 void add(const std:: string
& d
, const std::string
& s
) { add(d
, (const unsigned char *)s
.c_str(), s
.length()); }
56 void add_path(const std::string
& description
, const std::string
& path
);
58 void result(std::string
& r
);
59 std::string
get_parms() { return parm_stream
.str(); }
71 stap_hash::add(const std::string
& description
, const unsigned char *buffer
, size_t size
)
73 parm_stream
<< description
<< buffer
<< endl
;
74 mdfour_update(&md4
, buffer
, size
);
78 template <typename T
> void
79 stap_hash::add(const std::string
& d
, const T
& x
)
81 parm_stream
<< d
<< x
<< endl
;
82 mdfour_update(&md4
, (const unsigned char *)&x
, sizeof(x
));
87 stap_hash::add_path(const std::string
& description
, const std::string
& path
)
90 memset (&st
, 0, sizeof(st
));
92 if (stat(path
.c_str(), &st
) != 0)
93 st
.st_size
= st
.st_mtime
= -1;
95 add(description
+ "Path: ", path
);
96 add(description
+ "Size: ", st
.st_size
);
97 add(description
+ "Timestamp: ", st
.st_mtime
);
102 stap_hash::result(string
& r
)
104 ostringstream rstream
;
105 unsigned char sum
[16];
107 mdfour_update(&md4
, NULL
, 0);
108 mdfour_result(&md4
, sum
);
110 for (int i
=0; i
<16; i
++)
112 rstream
<< hex
<< setfill('0') << setw(2) << (unsigned)sum
[i
];
114 rstream
<< "_" << setw(0) << dec
<< (unsigned)md4
.totalN
;
118 void create_hash_log(const string
&type_str
, const string
&parms
, const string
&result
, const string
&hash_log_path
)
123 string
time_str(ctime (&rawtime
));
125 log_file
.open(hash_log_path
.c_str());
126 log_file
<< "[" << time_str
.substr(0,time_str
.length()-1); // erase terminated '\n'
127 log_file
<< "] " << type_str
<< ":" << endl
;
128 log_file
<< parms
<< endl
;
129 log_file
<< _("result:") << result
<< endl
;
133 static const stap_hash
&
134 get_base_hash (systemtap_session
& s
)
139 s
.base_hash
= new stap_hash();
140 stap_hash
& h
= *s
.base_hash
;
142 // Hash systemtap version
143 h
.add("Systemtap version: ", s
.version_string());
145 // Hash kernel release and arch.
146 h
.add("Kernel Release: ", s
.kernel_release
);
147 h
.add_path("Kernel Build Tree ", s
.kernel_build_tree
);
148 h
.add("Architecture: ", s
.architecture
);
150 // Hash a few kernel version/build-id files too
151 // (useful for kernel developers reusing a single source tree)
152 h
.add_path("Kernel Build Tree .config ", s
.kernel_build_tree
+ "/.config");
153 h
.add_path("Kernel Build Tree .version ", s
.kernel_build_tree
+ "/.version");
154 h
.add_path("Kernel Build Tree compile.h ", s
.kernel_build_tree
+ "/include/linux/compile.h");
155 h
.add_path("Kernel Build Tree version.h ", s
.kernel_build_tree
+ "/include/linux/version.h");
156 h
.add_path("Kernel Build Tree utsrelease.h ", s
.kernel_build_tree
+ "/include/linux/utsrelease.h");
158 // Hash runtime path (that gets added in as "-R path").
159 h
.add_path("Runtime ", s
.runtime_path
);
160 h
.add_path("Runtime transport ", s
.runtime_path
+ "/transport");
161 h
.add_path("Runtime unwind ", s
.runtime_path
+ "/unwind");
162 h
.add_path("Runtime sub ", s
.runtime_path
+
163 (s
.runtime_usermode_p() ? "/dyninst" : "/linux"));
165 // Hash compiler path, size, and mtime. We're just going to assume
166 // we'll be using gcc. XXX: getting kbuild to spit out out would be
167 // better, especially since this is fooled by ccache.
168 h
.add_path("Compiler ", find_executable("gcc"));
170 // Hash the systemtap size and mtime. We could use VERSION/DATE,
171 // but when developing systemtap that doesn't work well (since you
172 // can compile systemtap multiple times in 1 day). Since we don't
173 // know exactly where we're getting run from, we'll use
174 // /proc/self/exe (and we resolve it ourselves to help valgrind).
175 h
.add_path("Systemtap ", get_self_path());
182 create_hashdir (systemtap_session
& s
, const string
& result
, string
& hashdir
)
186 // Use a N level subdir for the cache path to reduce the impact on
187 // filesystems which are slow for large directories. Let N be adjustable.
188 const char *s_n
= getenv("SYSTEMTAP_NLEVELS");
192 if (nlevels
< 1) nlevels
= 1;
193 if (nlevels
> 8) nlevels
= 8;
196 hashdir
= s
.cache_path
;
198 for (int i
= 0; i
< nlevels
; i
++)
200 hashdir
+= string("/") + result
[i
*2] + result
[i
*2 + 1];
201 if (create_dir(hashdir
.c_str()) != 0)
203 s
.print_warning("failed to create cache directory (\"" + hashdir
+ "\") " + strerror(errno
) + ", disabling cache support");
204 s
.use_cache
= s
.use_script_cache
= false;
213 find_script_hash (systemtap_session
& s
, const string
& script
)
215 stap_hash
h(get_base_hash(s
));
217 // Hash getuid. This really shouldn't be necessary (since who you
218 // are doesn't change the generated output), but the hash gets used
219 // as the module name. If two different users try to run the same
220 // script at the same time, we need something to differentiate the
222 h
.add("UID: ", getuid());
224 // Hash user-specified arguments (that change the generated module).
225 h
.add("Bulk Mode (-b): ", s
.bulk_mode
);
226 h
.add("Timing (-t): ", s
.timing
);
227 h
.add("Skip Badvars (--skip-badvars): ", s
.skip_badvars
);
228 h
.add("Privilege (--privilege): ", s
.privilege
);
229 h
.add("Compatible (--compatible): ", s
.compatible
);
230 h
.add("Error suppression (--suppress-handler-errors): ", s
.suppress_handler_errors
);
231 h
.add("Suppress Time Limits (--suppress-time-limits): ", s
.suppress_time_limits
);
232 h
.add("Prologue Searching (--prologue-searching[=WHEN]): ", int(s
.prologue_searching_mode
));
234 for (unsigned i
= 0; i
< s
.c_macros
.size(); i
++)
235 h
.add("Macros: ", s
.c_macros
[i
]);
237 // Add any custom kbuild flags (-B)
238 for (unsigned i
= 0; i
< s
.kbuildflags
.size(); i
++)
239 h
.add("Kbuildflags: ", s
.kbuildflags
[i
]);
241 // Add any custom --modinfo strings
242 for (unsigned i
= 0; i
< s
.modinfos
.size(); i
++)
243 h
.add("MODULE_INFO: ", s
.modinfos
[i
]);
246 for (set
<string
>::iterator it
= s
.unwindsym_modules
.begin();
247 it
!= s
.unwindsym_modules
.end();
249 h
.add_path("Unwindsym Modules ", *it
);
251 // Add the build id of each module
252 for(vector
<string
>::iterator it
= s
.build_ids
.begin();
253 it
!= s
.build_ids
.end();
255 h
.add("Build ID: ", *it
);
257 // Add in pass 2 script output.
258 h
.add("Script:\n", script
);
260 // Get the directory path to store our cached script
261 string result
, hashdir
;
263 if (!create_hashdir(s
, result
, hashdir
))
266 // Update module name to be 'stap_{hash start}'. '{hash start}'
267 // must not be too long. This shouldn't happen, since the maximum
268 // size of a hash is 32 fixed chars + 1 (for the '_') + a max of 11.
269 s
.module_name
= "stap_" + result
;
270 if (s
.module_name
.size() >= (MODULE_NAME_LEN
- 1))
271 s
.module_name
.resize(MODULE_NAME_LEN
- 1);
273 // 'ccache' would use a hash path of something like:
274 // s.hash_path = hashdir + "/" + result.substr(nlevels);
275 // which would look like:
276 // ~/.stap_cache/A/B/CDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX
278 // We're using the following so that the module can be used straight
279 // from the cache if desired. This ends up looking like this:
280 // ~/.stap_cache/A/B/stap_ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX.ko
281 s
.hash_path
= hashdir
+ "/" + s
.module_filename();
283 // Update C source name with new module_name.
284 s
.translated_source
= string(s
.tmpdir
) + "/" + s
.module_name
+ "_src.c";
285 create_hash_log(string("script_hash"), h
.get_parms(), result
,
286 hashdir
+ "/" + s
.module_name
+ "_hash.log");
291 find_stapconf_hash (systemtap_session
& s
)
293 stap_hash
h(get_base_hash(s
));
295 // Add any custom kbuild flags
296 for (unsigned i
= 0; i
< s
.kbuildflags
.size(); i
++)
297 h
.add("Kbuildflags: ", s
.kbuildflags
[i
]);
299 // Get the directory path to store our cached stapconf parameters
300 string result
, hashdir
;
302 if (!create_hashdir(s
, result
, hashdir
))
305 s
.stapconf_name
= "stapconf_" + result
+ ".h";
306 s
.stapconf_path
= hashdir
+ "/" + s
.stapconf_name
;
307 create_hash_log(string("stapconf_hash"), h
.get_parms(), result
,
308 hashdir
+ "/stapconf_" + result
+ "_hash.log");
313 find_tracequery_hash (systemtap_session
& s
, const string
& header
)
315 stap_hash
h(get_base_hash(s
));
317 // Add the tracepoint header to the computed hash
318 h
.add_path("Header ", header
);
320 // Add any custom kbuild flags
321 for (unsigned i
= 0; i
< s
.kbuildflags
.size(); i
++)
322 h
.add("Kbuildflags: ", s
.kbuildflags
[i
]);
324 // Get the directory path to store our cached module
325 string result
, hashdir
;
327 if (!create_hashdir(s
, result
, hashdir
))
328 return ""; // XXX: as opposed to throwing an exception?
330 create_hash_log(string("tracequery_hash"), h
.get_parms(), result
,
331 hashdir
+ "/tracequery_" + result
+ "_hash.log");
332 return hashdir
+ "/tracequery_" + result
+ ".o";
337 find_typequery_hash (systemtap_session
& s
, const string
& name
)
339 stap_hash
h(get_base_hash(s
));
341 // Add the typequery name to distinguish the hash
342 h
.add("Typequery Name: ", name
);
345 // Add any custom kbuild flags
346 for (unsigned i
= 0; i
< s
.kbuildflags
.size(); i
++)
347 h
.add("Kbuildflags: ", s
.kbuildflags
[i
]);
349 // Get the directory path to store our cached module
350 string result
, hashdir
;
352 if (!create_hashdir(s
, result
, hashdir
))
355 create_hash_log(string("typequery_hash"), h
.get_parms(), result
,
356 hashdir
+ "/typequery_" + result
+ "_hash.log");
357 return hashdir
+ "/typequery_" + result
358 + (name
[0] == 'k' ? ".ko" : ".so");
363 find_uprobes_hash (systemtap_session
& s
)
365 stap_hash
h(get_base_hash(s
));
367 // Hash runtime uprobes paths
368 h
.add_path("Uprobes Runtime Path /uprobes ", s
.runtime_path
+ "/linux/uprobes");
369 h
.add_path("Uprobes Runtime Path /uprobes2 ", s
.runtime_path
+ "/linux/uprobes2");
371 // Add any custom kbuild flags
372 for (unsigned i
= 0; i
< s
.kbuildflags
.size(); i
++)
373 h
.add("Kbuildflags: ", s
.kbuildflags
[i
]);
375 // Add any custom --modinfo strings
376 for (unsigned i
= 0; i
< s
.modinfos
.size(); i
++)
377 h
.add("MODULE_INFO: ", s
.modinfos
[i
]);
379 // Get the directory path to store our cached module
380 string result
, hashdir
;
382 if (!create_hashdir(s
, result
, hashdir
))
385 create_hash_log(string("uprobes_hash"), h
.get_parms(), result
,
386 hashdir
+ "/uprobes_" + result
+ "_hash.log");
387 return hashdir
+ "/uprobes_" + result
;
390 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */